def OnTrack(self, event): pipeline = self.dsviewer.pipeline if self.tracker == None: featNames = [s.strip() for s in self.features.split(',')] def _calcWeights(s): fw = s.split('*') if len(fw) == 2: return float(fw[0]), fw[1] else: return 1.0, s weightedFeats = [_calcWeights(s) for s in featNames] feats = np.vstack([w * pipeline[fn] for w, fn in weightedFeats]) self.tracker = tracking.Tracker(pipeline['t'], feats) self.tracker.pNew = self.pNew self.tracker.r0 = self.r0 self.tracker.linkageCuttoffProb = self.pLinkCutoff for i in range(1, self.dsviewer.image.data.shape[2]): L = self.tracker.calcLinkages(i, i - 1) self.tracker.updateTrack(i, L) pipeline.selectedDataSource.clumps = self.tracker.clumpIndex pipeline.selectedDataSource.setMapping('clumpIndex', 'clumps') clumpSizes = np.zeros_like(self.tracker.clumpIndex) for i in set(self.tracker.clumpIndex): ind = (self.tracker.clumpIndex == i) clumpSizes[ind] = ind.sum() pipeline.selectedDataSource.clumpSizes = clumpSizes pipeline.selectedDataSource.setMapping('clumpSize', 'clumpSizes')
def Track(self, objects, newTracker=False): """Track objects based on a given set of feature vectors""" from PYME.Analysis.Tracking import trackUtils if (self._tracker is None) or not (len(self._tracker.t) == len( objects['t'])) or newTracker: featNames = [s.strip() for s in self.features.split(',')] def _calcWeights(s): fw = s.split('*') if len(fw) == 2: return float(fw[0]), fw[1] else: return 1.0, s weightedFeats = [_calcWeights(s) for s in featNames] feats = np.vstack( [w * np.array(objects[fn]) for w, fn in weightedFeats]) self._tracker = tracking.Tracker( np.array(objects['t']).astype('i'), feats) self._tracker.pNew = self.pNew self._tracker.r0 = self.r0 self._tracker.linkageCuttoffProb = self.pLinkCutoff for i in range(1, (int(objects['t'].max()) + 1)): L = self._tracker.calcLinkages(i, i - 1) self._tracker.updateTrack(i, L) clumpSizes = np.zeros_like(self._tracker.clumpIndex) for i in set(self._tracker.clumpIndex): ind = (self._tracker.clumpIndex == i) clumpSizes[ind] = ind.sum() trackVelocities = trackUtils.calcTrackVelocity( objects['x'], objects['y'], self._tracker.clumpIndex, objects['t']) clumpInfo = { 'clumpIndex': self._tracker.clumpIndex, 'clumpSize': clumpSizes, 'trackVelocity': trackVelocities } pipe = {} pipe.update(clumpInfo) pipe.update(objects) # pipe = pd.DataFrame(pipe) from PYME.IO.tabular import DictSource pipe = DictSource(pipe) if 'mdh' in dir(objects): #propagate metadata pipe.mdh = objects.mdh clumps = trackUtils.ClumpManager(pipe) if self.maxParticleSize > 0 and 'area' in clumps[0].keys(): clumps = [ c for c in clumps.all if (c.nEvents > self.minTrackLength) and ( c.featuremean['area'] < self.maxParticleSize) ] else: clumps = [ c for c in clumps.all if (c.nEvents > self.minTrackLength) ] # clumpInfo = pd.DataFrame(clumpInfo) clumpInfo = DictSource(clumpInfo) if 'mdh' in dir(objects): #propagate metadata clumpInfo.mdh = objects.mdh return clumpInfo, clumps