Example #1
0
    def run(self):
        ctime = time.time()

        while True:
            # Go through the expected power levels
            for gain in range(32):
                # Go through every expected receiver
                for recv in known_hosts.values():
                    # find the probability of detecting the tag at the current
                    # position and make a probability test to determine if it
                    # was detected
                    p = self.cdata.get((recv, gain, self.pos))
                    if random.random() < p:
                        observation = (recv, gain, [self.tag], ctime)
                    else:
                        observation = (recv, gain, [], ctime)

                    # if the queue is iterable lets assume its a list of queues and
                    # write the observation to every one of them
                    if not hasattr(self.queue, '__iter__'):
                        self.queue.put(observation)
                    else:
                        for queue in self.queue:
                            queue.put(observation)

                    # Increment the current time by a second
                    ctime += 1.0
                    self.countdown = self.countdown - 1

                    # If it is time to move to the next position do so
                    if self.countdown <= 0:
                        self.countdown = self.mobility
                        self.movepos = (self.movepos + 1) % len(self.movement)
                        self.pos = self.movement[self.movepos]
    def infer(self, tag):
        """
            Infers the likelihood of a tag existing at every location defined
            in the room_positions variable from the Thesis.constants module.
            
            Tag is the ID number of the tag to be inferred.
            
            Returns a dictionary whose keys are the positions defined in the
            keys of room_positions and whose value is the inferred likelihood.
        """
        likelihood = dict()
        for pos in room_positions.keys():
            likelihood[pos] = 0
            for recv in known_hosts.values():
                for gain in range(32):
                    p = self.cdata[(recv, gain, pos)]
                    n, x = self.obsman.get(tag, recv, gain)
                    likelihood[pos] += logbinomial(x, n, p)

        lowest = min(likelihood[x] for x in room_positions.keys())

        for x in room_positions.keys():
            likelihood[x] -= lowest

        return likelihood
    def __init__(self,
                 RecvList=known_hosts.values(),
                 TagList=known_tags.values(),
                 PoolSize=10):
        # Create the worker queue
        self.workqueue = Queue()

        # Create the Thread Pool
        self.workpool = Queue(PoolSize)
        for i in range(PoolSize):
            thread = WorkerThread(self)
            thread.start()
            self.workpool.put(thread)

        # Create the observation tables for each expected tag
        self.tables = dict()
        for tagid in TagList:
            self.tables[tagid] = ObservationsTable(gains=range(32),
                                                   receivers=RecvList)

        # Save off the tag and receiver list for later usage
        self.recvlist = RecvList
        self.taglist = TagList

        # A counter variable
        self.observationCount = 0

        # A set of callbacks to call after a number of new observations
        self.callbacks = []
        self.notifylock = threading.RLock()

        # Initialize the thread
        threading.Thread.__init__(self)
 def infer(self,tag):
     """
         Infers the likelihood of a tag existing at every location defined
         in the room_positions variable from the Thesis.constants module.
         
         Tag is the ID number of the tag to be inferred.
         
         Returns a dictionary whose keys are the positions defined in the
         keys of room_positions and whose value is the inferred likelihood.
     """
     likelihood = dict()
     for pos in room_positions.keys():
         likelihood[pos] = 0
         for recv in known_hosts.values():
             for gain in range(32):
                 p                = self.cdata[(recv,gain,pos)]
                 n,x              = self.obsman.get(tag,recv,gain)
                 likelihood[pos] += logbinomial(x, n, p)
                 
     lowest = min(likelihood[x] for x in room_positions.keys())
     
     for x in room_positions.keys():
         likelihood[x] -= lowest 
     
     return likelihood
 def __init__(self,RecvList=known_hosts.values(),TagList=known_tags.values(),PoolSize=10):
     # Create the worker queue
     self.workqueue = Queue()
     
     # Create the Thread Pool
     self.workpool = Queue(PoolSize)
     for i in range(PoolSize):
         thread = WorkerThread(self)
         thread.start()
         self.workpool.put( thread )
     
     # Create the observation tables for each expected tag
     self.tables = dict()
     for tagid in TagList:
         self.tables[tagid] = ObservationsTable(gains=range(32),receivers=RecvList)
         
     # Save off the tag and receiver list for later usage
     self.recvlist = RecvList
     self.taglist = TagList
     
     # A counter variable
     self.observationCount = 0
     
     # A set of callbacks to call after a number of new observations
     self.callbacks = []
     self.notifylock = threading.RLock()
     
     # Initialize the thread        
     threading.Thread.__init__(self)
 def run(self):
     ctime = time.time()
     
     while True:
         # Go through the expected power levels
         for gain in range(32):
             # Go through every expected receiver
             for recv in known_hosts.values():
                 # find the probability of detecting the tag at the current
                 # position and make a probability test to determine if it
                 # was detected
                 p = self.cdata.get((recv, gain, self.pos))
                 if random.random() < p:
                     observation = (recv, gain, [self.tag], ctime)
                 else:
                     observation = (recv, gain, [], ctime)
                     
                 # if the queue is iterable lets assume its a list of queues and
                 # write the observation to every one of them
                 if not hasattr(self.queue,'__iter__'):
                     self.queue.put(observation)
                 else:
                     for queue in self.queue:
                         queue.put(observation)
                 
                 # Increment the current time by a second
                 ctime += 1.0
                 self.countdown = self.countdown - 1
                 
                 # If it is time to move to the next position do so
                 if self.countdown <= 0:
                     self.countdown = self.mobility
                     self.movepos = (self.movepos + 1) % len(self.movement)
                     self.pos = self.movement[ self.movepos ]
    VisDump = optlist.has_key("--vis-dump")
    VisPrefix = optlist.get("--vis-prefix", "")

    UseLegend = optlist.has_key("--legend")

    if not CalibrationFile:
        usage("No calibration data file specified.")

    ## Parse the data and visualize it
    ##-------------------------------------------------------------------------
    data = ParseCalibrationFile(CalibrationFile)

    for pos in PosList:
        lines = [[data[(host, g, pos)] for g in range(32)]
                 for host in known_hosts.values()]

        pylab.ion()
        pylab.figure(1)
        pylab.ioff()

        pylab.clf()

        pylab.plot(range(32), [prob for prob in izip(*lines)])

        pylab.xlim(0, 31)
        pylab.ylim(0, 1)

        if UseLegend:
            pylab.legend(known_hosts.keys())
        pylab.grid()
 PosList         = [int(x) for x in optlist.get("--position", default).split(",")]
 
 VisDump         = optlist.has_key("--vis-dump")
 VisPrefix       = optlist.get("--vis-prefix","")
 
 UseLegend       = optlist.has_key("--legend")
     
 if not CalibrationFile:
     usage("No calibration data file specified.")
 
 ## Parse the data and visualize it 
 ##-------------------------------------------------------------------------
 data = ParseCalibrationFile(CalibrationFile)
 
 for pos in PosList:
     lines = [[data[(host,g,pos)] for g in range(32)] for host in known_hosts.values()]
         
     pylab.ion()
     pylab.figure(1)
     pylab.ioff()
         
     pylab.clf()
         
     pylab.plot(range(32),[prob for prob in izip(*lines)])
     
     pylab.xlim(0,31)
     pylab.ylim(0,1)
 
     if UseLegend:
         pylab.legend(known_hosts.keys())
     pylab.grid()