def execute(self, namespace):
        inp = namespace[self.inputName]

        filtered = tabular.ResultsFilter(inp, **self.filters)

        if 'mdh' in dir(inp):
            filtered.mdh = inp.mdh

        namespace[self.outputName] = filtered
    def execute(self, namespace):
        from PYME.IO import tabular

        if self.lowerMinPtsPerCluster > self.higherMinPtsPerCluster:
            print(
                'Swapping low and high MinPtsPerCluster - input was reversed')
            temp = self.lowerMinPtsPerCluster
            self.lowerMinPtsPerCluster = self.higherMinPtsPerCluster
            self.higherMinPtsPerCluster = temp

        iters = (int(np.max(namespace[self.inputName]['t'])) /
                 int(self.stepSize)) + 2

        # other counts
        lowDensMinPtsClumps = np.empty(iters)
        lowDensMinPtsClumps[0] = 0
        hiDensMinPtsClumps = np.empty(iters)
        hiDensMinPtsClumps[0] = 0
        t = np.empty(iters)
        t[0] = 0

        inp = tabular.MappingFilter(namespace[self.inputName])

        for ind in range(
                1, iters):  # start from 1 since t=[0,0] will yield no clumps
            # filter time
            inc = tabular.ResultsFilter(inp, t=[0, self.stepSize * ind])
            t[ind] = np.max(inc['t'])

            cid, counts = np.unique(inc[self.labelsKey], return_counts=True)
            # cmask = np.in1d(inc['DBSCAN_allFrames'], cid)

            cidL = cid[counts >= self.lowerMinPtsPerCluster]
            lowDensMinPtsClumps[ind] = np.sum(
                cidL != -1)  # ignore unclumped in count
            cid = cid[counts >= self.higherMinPtsPerCluster]
            hiDensMinPtsClumps[ind] = np.sum(
                cid != -1)  # ignore unclumped in count

        res = tabular.MappingFilter({
            't':
            t,
            'N_labelsWithLowMinPoints':
            lowDensMinPtsClumps,
            'N_labelsWithHighMinPoints':
            hiDensMinPtsClumps
        })

        # propagate metadata, if present
        try:
            res.mdh = namespace[self.inputName].mdh
        except AttributeError:
            pass

        namespace[self.outputName] = res
Example #3
0
def findTracks2(datasource,
                rad_var='error_x',
                multiplier='2.0',
                nFrames=20,
                minClumpSize=0):
    import PYME.Analysis.points.DeClump as deClump
    from PYME.IO import tabular

    with_clumps = tabular.MappingFilter(datasource)

    if rad_var == '1.0':
        delta_x = 0 * datasource['x'] + multiplier
    else:
        delta_x = multiplier * datasource[rad_var]

    t = datasource['t'].astype('i')
    x = datasource['x'].astype('f4')
    y = datasource['y'].astype('f4')
    delta_x = delta_x.astype('f4')

    I = np.argsort(t)

    clumpIndices = np.zeros(len(x), dtype='i')
    clumpIndices[I] = deClump.findClumps(t[I], x[I], y[I], delta_x[I], nFrames)

    numPerClump, b = np.histogram(clumpIndices,
                                  np.arange(clumpIndices.max() + 1.5) + .5)

    trackVelocities = 0 * x
    trackVelocities[I] = calcTrackVelocity(x[I], y[I], clumpIndices[I],
                                           t.astype('f')[I])
    #print b

    with_clumps.addColumn('clumpIndex', clumpIndices)
    with_clumps.addColumn('clumpSize', numPerClump[clumpIndices - 1])
    with_clumps.addColumn('trackVelocity', trackVelocities)

    if minClumpSize > 0:
        filt = tabular.ResultsFilter(with_clumps,
                                     clumpSize=[minClumpSize, 1e6])
    else:
        filt = with_clumps

    try:
        filt.mdh = datasource.mdh
    except AttributeError:
        pass

    return with_clumps, ClumpManager(filt)
Example #4
0
    def RegenFilter(self):
        if not self.selectedDataSource is None:
            self.filter = inpFilt.ResultsFilter(self.selectedDataSource,
                                                **self.filterKeys)
            if self.mapping:
                self.mapping.resultsSource = self.filter
            else:
                self.mapping = inpFilt.MappingFilter(self.filter)

            if not self.colourFilter:
                self.colourFilter = inpFilt.ColourFilter(self.mapping, self)

        self.edb = None
        self.objects = None

        self.GeneratedMeasures = {}
Example #5
0
    def Rebuild(self, **kwargs):
        """
        Rebuild the pipeline. Called when the selected data source is changed/modified and/or the filter is changed.

        """
        for s in self.dataSources.values():
            if 'setMapping' in dir(s):
                #keep raw measurements available
                s.setMapping('x_raw', 'x')
                s.setMapping('y_raw', 'y')

                if 'z' in s.keys():
                    s.setMapping('z_raw', 'z')

        if not self.selectedDataSource is None:
            if not self.mapping is None:
                # copy any mapping we might have made across to the new mapping filter (should fix drift correction)
                # TODO - make drift correction a recipe module so that we don't need this code. Long term we should be
                # ditching the mapping filter here.
                old_mapping = self.mapping
                self.mapping = tabular.MappingFilter(self.selectedDataSource)
                self.mapping.mappings.update(old_mapping.mappings)
            else:
                self.mapping = tabular.MappingFilter(self.selectedDataSource)

            #the filter, however needs to be re-generated with new keys and or data source
            self.filter = tabular.ResultsFilter(self.mapping,
                                                **self.filterKeys)

            #we can also recycle the colour filter
            if self.colourFilter is None:
                self.colourFilter = tabular.ColourFilter(self.filter)
            else:
                self.colourFilter.resultsSource = self.filter

            #self._process_colour()

            self.ready = True

        self.ClearGenerated()
    def execute(self, namespace):
        from PYME.IO.image import ImageStack

        series = namespace[self.input_series]
        positions = namespace[self.input_positions]

        ox_nm, oy_nm, oz = series.origin
        vs_nm = np.array([series.voxelsize_nm.x, series.voxelsize_nm.y])
        roi_half_nm = (self.roi_half_size + 1) * vs_nm
        x_max, y_max = (series.data.shape[:2] * vs_nm) - roi_half_nm
        
        edge_filtered = tabular.ResultsFilter(positions, 
                                              x=[ox_nm + roi_half_nm[0], x_max],
                                              y=[oy_nm + roi_half_nm[1], y_max])
        n_filtered = len(positions) - len(edge_filtered)
        if n_filtered > 0:
            logger.error('%d positions too close to edge, filtering.' % n_filtered)

        extracted = FitPoints(fitModule='ROIExtractNR', 
                              roiHalfSize=self.roi_half_size, 
                              channel=0).apply_simple(inputImage=series,
                                                      inputPositions=edge_filtered)
        
        # get roi edge position. FFBase._get_roi rounds before extracting
        relative_positions = edge_filtered.to_recarray()
        x_edge = np.round(edge_filtered['x'] / series.voxelsize_nm.x) - self.roi_half_size
        y_edge = np.round(edge_filtered['y'] / series.voxelsize_nm.y) - self.roi_half_size
        # subtract off ofset to ROI edge 
        relative_positions['x'] = edge_filtered['x'] - (x_edge * series.voxelsize_nm.x)
        relative_positions['y'] = edge_filtered['y'] - (y_edge * series.voxelsize_nm.y)
        relative_positions['fitResults_x0'] = relative_positions['x']
        relative_positions['fitResults_y0'] = relative_positions['y']
        relative_positions = tabular.RecArraySource(relative_positions)
        relative_positions.mdh = MetaDataHandler.NestedClassMDHandler(positions.mdh)
        relative_positions.mdh['ExtractROIs.RoiHalfSize'] = self.roi_half_size

        namespace[self.output_rois] = ImageStack(data=np.moveaxis(extracted['data'], 0, 2), 
                                                 mdh=series.mdh)
        namespace[self.output_relative_positions] = relative_positions
Example #7
0
def getPSFSlice(datasource, resultsSource, metadata, zm=None):
    f1 = tabular.ResultsFilter(resultsSource, error_x=[1, 30], A=[10, 500], sig=(150 / 2.35, 900 / 2.35))

    ims, pts, zvals, zis = extractIms(datasource, f1, metadata, zm)
    return getPSF(ims, pts, zvals, zis)
def generateThumbnail(inputFile, thumbSize):
    f1 = tabular.H5RSource(inputFile)

    threeD = False
    stack = False
    split = False

    #print f1.keys()

    if 'fitResults_Ag' in f1.keys():
        #if we used the splitter set up a mapping so we can filter on total amplitude and ratio
        f1_ = tabular.MappingFilter(f1, A='fitResults_Ag + fitResults_Ar', gFrac='fitResults_Ag/(fitResults_Ag + fitResults_Ar)')
        #f2 = inpFilt.resultsFilter(f1_, error_x=[0,30], A=[5, 1e5], sig=[100/2.35, 350/2.35])
        split = True
    else:
        f1_ = f1
        
    if 'fitResults_sigma' in f1.keys():
        f2 = tabular.ResultsFilter(f1_, error_x=[0, 30], A=[5, 1e5], sig=[100 / 2.35, 350 / 2.35])
    else:
        f2 = tabular.ResultsFilter(f1_, error_x=[0, 30], A=[5, 1e5])

    if 'fitResults_z0' in f1_.keys():
        threeD = True

    if 'Events' in dir(f1.h5f.root):
        events = f1.h5f.root.Events[:]

        evKeyNames = set()
        for e in events:
            evKeyNames.add(e['EventName'])

        if b'ProtocolFocus' in evKeyNames:
            stack = True



    xmax = f2['x'].max()
    ymax = f2['y'].max()

    if xmax > ymax:
        step = xmax/thumbSize
    else:
        step = ymax/thumbSize

    im, edx, edy = histogram2d(f2['x'], f2['y'], [arange(0, xmax, step), arange(0, ymax, step)])

    f1.close()

    im = minimum(2*(255*im)/im.max(), 255).T


    im = concatenate((im[:,:,newaxis], im[:,:,newaxis], im[:,:,newaxis]), 2)

    if stack:
        im[-10:, -10:, 0] = 180

    if threeD:
        im[-10:, -10:, 1] = 180

    if split:
        im[-10:-5, :10, 1] = 210
        im[-5:, :10, 0] = 210

    return im.astype('uint8')