def test_ia_util_7(): """ Test margin. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] images[0][10, 5] = 1.2 images[0][10, 6] = 1.1 images[0][10, 7] = 1.1 images[0][20, 10] = 1.1 images[0][20, 11] = 1.2 # Should only find 1 peak. mxf = iaUtilsC.MaximaFinder(margin=6, radius=3, threshold=1, z_values=z_values) [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 1) # Should find two peaks. mxf = iaUtilsC.MaximaFinder(margin=4, radius=3, threshold=1, z_values=z_values) [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 2)
def test_ia_util_4(): """ Multiple z planes test. """ x_size = 100 y_size = 80 images = [ numpy.zeros((x_size, y_size), dtype=numpy.float64), numpy.zeros((x_size, y_size), dtype=numpy.float64), numpy.zeros((x_size, y_size), dtype=numpy.float64) ] z_values = [1.0, 2.0, 3.0] images[0][20, 10] = 1.3 images[1][20, 10] = 1.2 images[2][20, 10] = 1.5 # Default z range (the entire stack). mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_values=z_values) [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 1) assert (abs(z[0] - z_values[2]) < 1.0e-6) # z range is limited to adjacent slices. # mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_range=1, z_values=z_values) [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 2) assert (abs(z[0] - z_values[0]) < 1.0e-6) assert (abs(z[1] - z_values[2]) < 1.0e-6) # z range is limited to current slice. # mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_range=0, z_values=z_values) [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 3) for i in range(z.size): assert (abs(z[i] - z_values[i]) < 1.0e-6)
def test_ia_util_11(): """ Test finding peaks in an image. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] # A row of peaks greater than radius with decreasing heights, there # should still only be a single maxima. images[0][10, 11] = 1.6 images[0][10, 12] = 1.5 images[0][10, 13] = 1.4 images[0][10, 14] = 1.3 images[0][10, 15] = 1.2 images[0][10, 16] = 1.1 mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_values=z_values) [x, y, z] = mxf.findMaxima(images) assert (x.size == 1) assert (abs(x[0] - 11.0) < 1.0e-6) assert (abs(y[0] - 10.0) < 1.0e-6)
def test_ia_util_2(): """ Test finding peaks in an image. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] # Above threshold, unequal height. images[0][10, 11] = 1.1 images[0][10, 12] = 1.5 # Above threshold, equal height. images[0][15, 8] = 1.5 images[0][15, 9] = 1.5 # Below threshold. images[0][20, 11] = 0.5 mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_values=z_values) [x, y, z, h] = mxf.findMaxima(images, want_height=True) assert (x.size == 2) for i in range(z.size): assert (abs(z[i] - z_values[0]) < 1.0e-6) assert (abs(h[i] - 1.5) < 1.0e-6)
def test_ia_util_8(): """ Test allowing multiple duplicates. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] # Single peak. images[0][10, 21] = 1.1 images[0][10, 20] = 1.2 mxf = iaUtilsC.MaximaFinder(margin=1, n_duplicates=2, radius=2, threshold=1, z_values=z_values) # Find the peak. np = [1, 1, 0] for elt in np: [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == elt) # Reset. mxf.resetTaken() # Test again. np = [1, 1, 0] for elt in np: [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == elt)
def test_ia_util_5(): """ Test that limits on the number of peak duplicates are working. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] # Single peak. images[0][10, 21] = 1.1 images[0][10, 20] = 1.2 mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_values=z_values) # Find the peak. [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 1) # This should not find anything, since the peak was # already found above. [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 0) # Reset and now we should find it again. mxf.resetTaken() [x, y, z] = mxf.findMaxima(imagesCopy(images)) assert (x.size == 1)
def __init__(self, parameters = None, psf_objects = None, **kwds): kwds["parameters"] = parameters super(MPPeakFinderArb, self).__init__(**kwds) self.mfilters_z = [] self.n_channels = len(psf_objects) self.psf_objects = psf_objects # Assert that all the PSF objects are the same size. # for i in range(1, len(self.psf_objects)): assert(self.psf_objects[0].getSize() == self.psf_objects[i].getSize()) # Update margin based on the psf object size. # self.margin = self.psf_objects[0].getMargin() # Note the assumption that the PSFs for each plane all use # the same z scale / have the same z range. # # 'z_value' is in units of microns. # self.mfilters_z is the Z position in nanometers. # self.mfilters_z = list(map(lambda x: x * 1.0e+3, parameters.getAttr("z_value", [0.0]))) for zval in self.mfilters_z: assert self.psf_objects[0].isValidZ(zval) self.z_values.append(self.psf_objects[0].getScaledZ(zval)) # Configure maxima finder. # self.mfinder = iaUtilsC.MaximaFinder(margin = self.margin, radius = self.find_max_radius, threshold = self.threshold, z_values = self.z_values) # Load the channel to channel mapping file. We need the correct value # for self.margin for this. # self.loadMapping() # Load pre-specified peak locations, if any. # if parameters.hasAttr("peak_locations"): [self.peak_locations, self.peak_locations_type] = fitting.getPeakLocations(parameters.getAttr("peak_locations"), self.margin, parameters.getAttr("pixel_size"), self.sigma) # Set initial z value (for text files). if (self.peak_locations_type == "text"): self.peak_locations["z"][:] = self.z_values[0] # Convert z value to PSF Z units (HDF5 localization files). else: self.peak_locations["z"] = self.psf_objects[0].getScaledZ(self.peak_locations["z"])
def __init__(self, parameters=None, psf_object=None, **kwds): kwds["parameters"] = parameters super(PeakFinderArbitraryPSF, self).__init__(**kwds) self.fg_mfilter = [] self.fg_mfilter_zval = [] self.fg_vfilter = [] self.psf_object = psf_object self.z_values = [] self.margin = psf_object.getMargin() # Note: self.z_values is the Z position in 'internal' units, i.e. the units # that the PSF generation library uses. For splines for example this # is the spline size in Z. # # 'z_value' is in units of microns. # self.fg_mfilter_zval is the Z position in nanometers. # self.fg_mfilter_zval = list( map(lambda x: x * 1.0e+3, parameters.getAttr("z_value", [0.0]))) for zval in self.fg_mfilter_zval: assert self.psf_object.isValidZ(zval) self.z_values.append(self.psf_object.getScaledZ(zval)) # Configure maxima finder. # self.mfinder = iaUtilsC.MaximaFinder(margin=self.margin, radius=self.find_max_radius, threshold=self.threshold, z_values=self.z_values) if parameters.hasAttr("peak_locations"): [self.peak_locations, self.peak_locations_type ] = getPeakLocations(parameters.getAttr("peak_locations"), self.margin, parameters.getAttr("pixel_size"), self.sigma) # Set initial z value (for text files). if (self.peak_locations_type == "text"): self.peak_locations["z"][:] = self.z_values[0] # Convert z value to PSF units (for HDF5 localization files). else: # If the HDF5 file does not have any "z" information we use the first # value of self.z_values. # if not "z" in self.peak_locations: self.peak_locations["z"] = numpy.zeros( self.peak_locations["x"].size) self.peak_locations["z"][:] = self.z_values[0] self.peak_locations["z"] = self.psf_object.getScaledZ( self.peak_locations["z"])
def test_ia_util_15(): """ Test non-integer radius values. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] images[0][8, 10] = 1.1 images[0][10, 10] = 1.5 images[0][11, 11] = 1.1 mxf = iaUtilsC.MaximaFinder(margin=1, radius=1.0, threshold=1, z_values=z_values) [x, y, z, h] = mxf.findMaxima(images, want_height=True) assert (numpy.allclose(x, numpy.array([10, 10, 11]))) assert (numpy.allclose(y, numpy.array([8, 10, 11]))) mxf = iaUtilsC.MaximaFinder(margin=1, radius=1.99, threshold=1, z_values=z_values) [x, y, z, h] = mxf.findMaxima(images, want_height=True) assert (numpy.allclose(x, numpy.array([10, 10]))) assert (numpy.allclose(y, numpy.array([8, 10]))) mxf = iaUtilsC.MaximaFinder(margin=1, radius=2.01, threshold=1, z_values=z_values) [x, y, z, h] = mxf.findMaxima(images, want_height=True) assert (numpy.allclose(x, numpy.array([10]))) assert (numpy.allclose(y, numpy.array([10])))
def __init__(self, parameters=None, **kwds): """ This is called once at the start of analysis to initialize the parameters that will be used for peak fitting. parameters - A parameters object. """ kwds["parameters"] = parameters super(PeakFinderGaussian, self).__init__(**kwds) # Figure out what margin and ROI to use. if (self.parameters.getAttr("roi_size", -1) != -1): self.roi_size = parameters.getAttr("roi_size") else: # Calculate roi size based on sigma. self.roi_size = int(8.0 * self.sigma) # Make it even larger for variable width fitters. if (parameters.getAttr("model") != "2dfixed"): self.roi_size = int(1.5 * self.roi_size) self.margin = int(self.roi_size / 2 + 2) # Initialized from parameters. self.z_value = self.parameters.getAttr( "z_value", 0.0) # The starting z value to use for peak fitting. # Other member variables. self.fg_mfilter = None # Foreground MatchedFilter object (may be None). self.fg_vfilter = None # Foreground variance MatchedFilter object, will # be none if self.fg_mfilter is None. # Configure maxima finder. # self.mfinder = iaUtilsC.MaximaFinder(margin=self.margin, radius=self.find_max_radius, threshold=self.threshold, z_values=[self.z_value]) # Load peak locations if specified. # # FIXME: The starting z value is always 0.0. Not sure why we don't use # self.z_value for this. Though I guess it would only really be # relevant for the 'Z' fitting model. # if parameters.hasAttr("peak_locations"): [self.peak_locations, self.peak_locations_type ] = getPeakLocations(parameters.getAttr("peak_locations"), self.margin, parameters.getAttr("pixel_size"), self.sigma)
def __init__(self, roi_size=None, sigma=None, threshold=None, **kwds): super().__init__(**kwds) self.fg_filter = None self.roi_size = roi_size self.sigma = sigma size = (2 * self.roi_size, 2 * self.roi_size) #self.c2dg = corr2DGauss.Corr2DGaussPyNCG(size = size, sigma = sigma) self.c2dg = corr2DGauss.Corr2DGaussCNCG(size=size, sigma=sigma) self.mxf = iaUtilsC.MaximaFinder(margin=self.roi_size, radius=2 * self.sigma, threshold=threshold, z_values=[0.0])
def test_ia_util_1(): """ Test finding peaks in an empty image. """ x_size = 100 y_size = 80 images = [numpy.zeros((x_size, y_size), dtype=numpy.float64)] z_values = [0.1] mxf = iaUtilsC.MaximaFinder(margin=1, radius=2, threshold=1, z_values=z_values) [x, y, z] = mxf.findMaxima(images) assert (x.size == 0)
def __init__(self, offset = None, sigma = None, threshold = None, **kwds): super().__init__(**kwds) self.mfit = None self.offset = offset self.roi_size = 10 self.sigma = sigma # Filter for smoothing the image for peak finding. # self.fg_filter = None self.mxf = iaUtilsC.MaximaFinder(margin = self.roi_size, radius = 2 * self.sigma, threshold = threshold + self.offset, z_values = [0.0])
def test_ia_util_3(): """ Test agreement with fitting regarding orientation. """ height = 20.0 sigma = 1.5 x_size = 100 y_size = 120 background = numpy.zeros((x_size, y_size)) + 10.0 image = dg.drawGaussians((x_size, y_size), numpy.array([[20.0, 40.0, height, sigma, sigma]])) image += background # Configure fitter. mfit = daoFitC.MultiFitter2D() mfit.initializeC(image) mfit.newImage(image) mfit.newBackground(background) # Configure finder. z_values = [0.0] mxf = iaUtilsC.MaximaFinder(margin=1, radius=2 * sigma, threshold=background[0, 0] + 0.5 * height, z_values=z_values) # Find peaks. [x, y, z] = mxf.findMaxima([image]) sigma = numpy.ones(x.size) * sigma peaks = {"x": x, "y": y, "z": z, "sigma": sigma} # Pass peaks to fitter. mfit.newPeaks(peaks, "finder") # Check height. h = mfit.getPeakProperty("height") for i in range(h.size): assert (abs(h[i] - height) / height < 0.1) # Check background. bg = mfit.getPeakProperty("background") for i in range(bg.size): assert (abs(bg[i] - 10.0) < 1.0e-6) mfit.cleanup(verbose=False)
def __init__(self, parameters=None, n_channels=None, **kwds): kwds["parameters"] = parameters super(MPPeakFinderDao, self).__init__(**kwds) self.n_channels = n_channels self.z_values = [0.0] # Figure out what margin and ROI to use. if (self.parameters.getAttr("roi_size", -1) != -1): self.roi_size = parameters.getAttr("roi_size") else: # Calculate roi size based on sigma. self.roi_size = int(20.0 * self.sigma) self.margin = int(self.roi_size / 2 + 2) # Configure maxima finder. # self.mfinder = iaUtilsC.MaximaFinder(margin=self.margin, radius=self.find_max_radius, threshold=self.threshold, z_values=self.z_values) # Load the channel to channel mapping file. We need the correct value # for self.margin for this. # self.loadMapping() # Load pre-specified peak locations, if any. # if parameters.hasAttr("peak_locations"): [self.peak_locations, self.peak_locations_type ] = fitting.getPeakLocations(parameters.getAttr("peak_locations"), self.margin, parameters.getAttr("pixel_size"), self.sigma) # Set initial z value to 0.0. This is just a placeholder, this # value won't be used for anything. # self.peak_locations["z"][:] = self.z_values[0]