aboutsummaryrefslogtreecommitdiff
path: root/operationscore/PixelMapper.py
blob: 39379730ac800bab91f89afd0b98662fd65d719e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from operationscore.SmootCoreObject import *
from logger import main_log
import pdb
class PixelMapper(SmootCoreObject):
    """PixelMapper is the parent class for PixelMappers.  Inheriting classes should define
    mappingFunction which takes an eventLocation and a screen and returns a list of (weight, pixels).  PixelMapper
    handles caching automatically."""
    def init(self):
        self.mem = {} #Dictionary of all seen events
        self.totalCalls = 0
        self.cachehits = 0
    def mapEvent(self, eventLocation, screen):
        self.totalCalls += 1
        if self.totalCalls % 100 == 0:
            main_log.info('Cache percentage for :', self['Id'], self.cachehits /\
                float(self.totalCalls))
        if eventLocation in self.mem:
            self.cachehits += 1
            return self.mem[eventLocation]
        else:
            self.mem[eventLocation] = self.mappingFunction(eventLocation, screen)
            return self.mem[eventLocation]
    #Takes a Screen and returns a list of tuples
    #(pixel, weight), with the sum of weights = 1
    def mappingFunction(self,eventLocation, screen):
        """Takes a Screen and event location and returns a list of tuples (pixel,weight) with
        sum(weights)=1"""
        raise Exception('Mapping function not defined!')