def convertToMultiFit(i3data, x_size, y_size, frame, nm_per_pixel, inverted=False): """ Create a 3D-DAOSTORM, sCMOS or Spliner analysis compatible peak array from I3 data. Notes: (1) This uses the non-drift corrected positions. (2) This sets the initial fitting error to zero and the status to RUNNING. """ i3data = maskData(i3data, (i3data['fr'] == frame)) peaks = numpy.zeros((i3data.size, utilC.getNPeakPar())) peaks[:,utilC.getBackgroundIndex()] = i3data['bg'] peaks[:,utilC.getHeightIndex()] = i3data['h'] peaks[:,utilC.getZCenterIndex()] = i3data['z'] * 0.001 if inverted: peaks[:,utilC.getXCenterIndex()] = y_size - i3data['x'] peaks[:,utilC.getYCenterIndex()] = x_size - i3data['y'] ax = i3data['ax'] ww = i3data['w'] peaks[:,utilC.getYWidthIndex()] = 0.5*numpy.sqrt(ww*ww/ax)/nm_per_pixel peaks[:,utilC.getXWidthIndex()] = 0.5*numpy.sqrt(ww*ww*ax)/nm_per_pixel else: peaks[:,utilC.getYCenterIndex()] = i3data['x'] - 1 peaks[:,utilC.getXCenterIndex()] = i3data['y'] - 1 ax = i3data['ax'] ww = i3data['w'] peaks[:,utilC.getXWidthIndex()] = 0.5*numpy.sqrt(ww*ww/ax)/nm_per_pixel peaks[:,utilC.getYWidthIndex()] = 0.5*numpy.sqrt(ww*ww*ax)/nm_per_pixel return peaks
def createFromMultiFit(molecules, x_size, y_size, frame, nm_per_pixel, inverted=False): """ Create an I3 data from the output of 3D-DAOSTORM, sCMOS or Spliner. """ n_molecules = molecules.shape[0] h = molecules[:, 0] if inverted: xc = y_size - molecules[:, utilC.getXCenterIndex()] yc = x_size - molecules[:, utilC.getYCenterIndex()] wx = 2.0 * molecules[:, utilC.getXWidthIndex()] * nm_per_pixel wy = 2.0 * molecules[:, utilC.getYWidthIndex()] * nm_per_pixel else: xc = molecules[:, utilC.getYCenterIndex()] + 1 yc = molecules[:, utilC.getXCenterIndex()] + 1 wx = 2.0 * molecules[:, utilC.getYWidthIndex()] * nm_per_pixel wy = 2.0 * molecules[:, utilC.getXWidthIndex()] * nm_per_pixel bg = molecules[:, utilC.getBackgroundIndex()] zc = molecules[:, utilC.getZCenterIndex( )] * 1000.0 # fitting is done in um, insight works in nm st = numpy.round(molecules[:, utilC.getStatusIndex()]) err = molecules[:, utilC.getErrorIndex()] # # Calculate peak area, which is saved in the "a" field. # # Note that this is assuming that the peak is a 2D gaussian. This # will not calculate the correct area for a Spline.. # parea = 2.0 * 3.14159 * h * molecules[:, utilC.getXWidthIndex( )] * molecules[:, utilC.getYWidthIndex()] ax = wy / wx ww = numpy.sqrt(wx * wy) i3data = createDefaultI3Data(xc.size) posSet(i3data, 'x', xc) posSet(i3data, 'y', yc) posSet(i3data, 'z', zc) setI3Field(i3data, 'h', h) setI3Field(i3data, 'bg', bg) setI3Field(i3data, 'fi', st) setI3Field(i3data, 'a', parea) setI3Field(i3data, 'w', ww) setI3Field(i3data, 'ax', ax) setI3Field(i3data, 'fr', frame) setI3Field(i3data, 'i', err) return i3data
def createFromMultiFit(molecules, x_size, y_size, frame, nm_per_pixel, inverted=False): """ Create an I3 data from the output of 3D-DAOSTORM, sCMOS or Spliner. """ n_molecules = molecules.shape[0] h = molecules[:,0] if inverted: xc = y_size - molecules[:,utilC.getXCenterIndex()] yc = x_size - molecules[:,utilC.getYCenterIndex()] wx = 2.0*molecules[:,utilC.getXWidthIndex()]*nm_per_pixel wy = 2.0*molecules[:,utilC.getYWidthIndex()]*nm_per_pixel else: xc = molecules[:,utilC.getYCenterIndex()] + 1 yc = molecules[:,utilC.getXCenterIndex()] + 1 wx = 2.0*molecules[:,utilC.getYWidthIndex()]*nm_per_pixel wy = 2.0*molecules[:,utilC.getXWidthIndex()]*nm_per_pixel bg = molecules[:,utilC.getBackgroundIndex()] zc = molecules[:,utilC.getZCenterIndex()] * 1000.0 # fitting is done in um, insight works in nm st = numpy.round(molecules[:,utilC.getStatusIndex()]) err = molecules[:,utilC.getErrorIndex()] # # Calculate peak area, which is saved in the "a" field. # # Note that this is assuming that the peak is a 2D gaussian. This # will not calculate the correct area for a Spline.. # parea = 2.0*3.14159*h*molecules[:,utilC.getXWidthIndex()]*molecules[:,utilC.getYWidthIndex()] ax = wy/wx ww = numpy.sqrt(wx*wy) i3data = createDefaultI3Data(xc.size) posSet(i3data, 'x', xc) posSet(i3data, 'y', yc) posSet(i3data, 'z', zc) setI3Field(i3data, 'h', h) setI3Field(i3data, 'bg', bg) setI3Field(i3data, 'fi', st) setI3Field(i3data, 'a', parea) setI3Field(i3data, 'w', ww) setI3Field(i3data, 'ax', ax) setI3Field(i3data, 'fr', frame) setI3Field(i3data, 'i', err) return i3data
def __init__(self, parameters): fitting.PeakFinder.__init__(self, parameters) self.height_rescale = [] self.mfilter = [] self.mfilter_z = [] self.z_value = [] # Load the spline. self.s_to_psf = splineToPSF.loadSpline(parameters.getAttr("spline")) # Update margin based on the spline size. old_margin = self.margin self.margin = int((self.s_to_psf.getSize() + 1)/4 + 2) self.mfilter_z = parameters.getAttr("z_value", [0.0]) for zval in self.mfilter_z: self.z_value.append(self.s_to_psf.getScaledZ(zval)) if parameters.hasAttr("peak_locations"): # Correct for any difference in the margins. self.peak_locations[:,utilC.getXCenterIndex()] += self.margin - old_margin self.peak_locations[:,utilC.getYCenterIndex()] += self.margin - old_margin # Provide the "correct" starting z value. self.peak_locations[:,utilC.getZCenterIndex()] = self.z_value[0]
def analyzeImage(self, new_image, bg_estimate=None, save_residual=False, verbose=False): """ image - The image to analyze. bg_estimate - (Optional) An estimate of the background. save_residual - (Optional) Save the residual image after peak fitting, default is False. return - [Found peaks, Image residual] """ # # Pad out arrays so that we can better analyze localizations # near the edge of the original image. # image = padArray(new_image, self.margin) residual = padArray(new_image, self.margin) if bg_estimate is not None: bg_estimate = padArray(bg_estimate, self.margin) self.peak_finder.newImage(image) self.peak_fitter.newImage(image) if save_residual: resid_dax = daxwriter.DaxWriter("residual.dax", residual.shape[0], residual.shape[1]) peaks = False for i in range(self.peak_finder.iterations): if save_residual: resid_dax.addFrame(residual) no_bg_image = self.peak_finder.subtractBackground( residual, bg_estimate) [found_new_peaks, peaks] = self.peak_finder.findPeaks(no_bg_image, peaks) if isinstance(peaks, numpy.ndarray): [peaks, residual] = self.peak_fitter.fitPeaks(peaks) if verbose: if isinstance(peaks, numpy.ndarray): print(" peaks:", i, found_new_peaks, peaks.shape[0]) else: print(" peaks:", i, found_new_peaks, "NA") if not found_new_peaks: break if save_residual: resid_dax.addFrame(residual) resid_dax.close() if isinstance(peaks, numpy.ndarray): peaks[:, utilC.getXCenterIndex()] -= float(self.margin) peaks[:, utilC.getYCenterIndex()] -= float(self.margin) return [peaks, residual]
def __init__(self, parameters): # Initialized from parameters. self.find_max_radius = parameters.getAttr("find_max_radius", 5) # Radius (in pixels) over which the maxima is maximal. self.iterations = parameters.getAttr("iterations") # Maximum number of cycles of peak finding, fitting and subtraction to perform. self.sigma = parameters.getAttr("sigma") # Peak sigma (in pixels). self.threshold = parameters.getAttr("threshold") # Peak minimum threshold (height, in camera units). self.z_value = parameters.getAttr("z_value", 0.0) # The starting z value to use for peak fitting. # Other member variables. self.background = None # Current estimate of the image background. self.image = None # The original image. self.margin = PeakFinderFitter.margin # Size of the unanalyzed "edge" around the image. self.neighborhood = PeakFinder.unconverged_dist * self.sigma # Radius for marking neighbors as unconverged. self.new_peak_radius = PeakFinder.new_peak_dist # Minimum allowed distance between new peaks and current peaks. self.parameters = parameters # Keep access to the parameters object. self.peak_locations = None # Initial peak locations, as explained below. self.peak_mask = None # Mask for limiting peak identification to a particular AOI. self.taken = None # Spots in the image where a peak has already been added. # # This is for is you already know where your want fitting to happen, as # for example in a bead calibration movie and you just want to use the # approximate locations as inputs for fitting. # # peak_locations is a text file with the peak x, y, height and background # values as white spaced columns (x and y positions are in pixels as # determined using visualizer). # # 1.0 2.0 1000.0 100.0 # 10.0 5.0 2000.0 200.0 # ... # if parameters.hasAttr("peak_locations"): print("Using peak starting locations specified in", parameters.getAttr("peak_locations")) # Only do one cycle of peak finding as we'll always return the same locations. if (self.iterations != 1): print("WARNING: setting number of iterations to 1!") self.iterations = 1 # Load peak x,y locations. peak_locs = numpy.loadtxt(parameters.getAttr("peak_locations"), ndmin = 2) print(peak_locs.shape) # Create peak array. self.peak_locations = numpy.zeros((peak_locs.shape[0], utilC.getNPeakPar())) self.peak_locations[:,utilC.getXCenterIndex()] = peak_locs[:,1] + self.margin self.peak_locations[:,utilC.getYCenterIndex()] = peak_locs[:,0] + self.margin self.peak_locations[:,utilC.getHeightIndex()] = peak_locs[:,2] self.peak_locations[:,utilC.getBackgroundIndex()] = peak_locs[:,3] self.peak_locations[:,utilC.getXWidthIndex()] = numpy.ones(peak_locs.shape[0]) * self.sigma self.peak_locations[:,utilC.getYWidthIndex()] = numpy.ones(peak_locs.shape[0]) * self.sigma
def convertToMultiFit(i3data, x_size, y_size, frame, nm_per_pixel, inverted=False): """ Create a 3D-DAOSTORM, sCMOS or Spliner analysis compatible peak array from I3 data. Notes: (1) This uses the non-drift corrected positions. (2) This sets the initial fitting error to zero and the status to RUNNING. """ i3data = maskData(i3data, (i3data['fr'] == frame)) peaks = numpy.zeros((i3data.size, utilC.getNPeakPar())) peaks[:, utilC.getBackgroundIndex()] = i3data['bg'] peaks[:, utilC.getHeightIndex()] = i3data['h'] peaks[:, utilC.getZCenterIndex()] = i3data['z'] * 0.001 if inverted: peaks[:, utilC.getXCenterIndex()] = y_size - i3data['x'] peaks[:, utilC.getYCenterIndex()] = x_size - i3data['y'] ax = i3data['ax'] ww = i3data['w'] peaks[:, utilC.getYWidthIndex()] = 0.5 * numpy.sqrt( ww * ww / ax) / nm_per_pixel peaks[:, utilC.getXWidthIndex()] = 0.5 * numpy.sqrt( ww * ww * ax) / nm_per_pixel else: peaks[:, utilC.getYCenterIndex()] = i3data['x'] - 1 peaks[:, utilC.getXCenterIndex()] = i3data['y'] - 1 ax = i3data['ax'] ww = i3data['w'] peaks[:, utilC.getXWidthIndex()] = 0.5 * numpy.sqrt( ww * ww / ax) / nm_per_pixel peaks[:, utilC.getYWidthIndex()] = 0.5 * numpy.sqrt( ww * ww * ax) / nm_per_pixel return peaks
def findPeaks(self, peaks): # Adjust x,y for margin. peaks[:, utilC.getXCenterIndex()] += float(self.margin) peaks[:, utilC.getYCenterIndex()] += float(self.margin) # Convert z (in nano-meters) to spline z units. zi = utilC.getZCenterIndex() peaks[:, zi] = self.s_to_psfs[0].getScaledZ(peaks[:, zi]) # Split peaks into per-channel peaks. return self.mpu.splitPeaks(peaks)
def analyzeImage(self, new_image, bg_estimate=None, save_residual=False, verbose=False): image = fitting.padArray(new_image, self.peak_finder.margin) if bg_estimate is not None: bg_estimate = fitting.padArray(bg_estimate, self.peak_finder.margin) self.peak_finder.newImage(image, bg_estimate) self.peak_fitter.newImage(image) # # This is a lot simpler than 3D-DAOSTORM as we only do one pass, # hopefully the compressed sensing (FISTA) deconvolution finds all the # peaks and then we do a single pass of fitting. # if True: peaks = self.peak_finder.findPeaks() [fit_peaks, residual] = self.peak_fitter.fitPeaks(peaks) # # This is for testing if just using FISTA followed by the center # of mass calculation is basically as good as also doing the # additional MLE spline fitting step. # # The short answer is that it appears that it is not. It about # 1.3x worse in XY and about 4x worse in Z. # else: fit_peaks = self.peak_finder.findPeaks() # Adjust z scale. z_index = utilC.getZCenterIndex() z_size = (self.peak_fitter.spline.shape[2] - 1.0) status_index = utilC.getStatusIndex() fit_peaks[:, z_index] = z_size * fit_peaks[:, z_index] # Mark as converged. fit_peaks[:, status_index] = 1.0 residual = None # # Subtract margin so that peaks are in the right # place with respect to the original image. # fit_peaks[:, utilC.getXCenterIndex()] -= float(self.peak_finder.margin) fit_peaks[:, utilC.getYCenterIndex()] -= float(self.peak_finder.margin) return [fit_peaks, residual]
def analyzeImage(self, movie_reader, save_residual=False, verbose=False): """ Analyze an "image" and return a list of the found localizations. Internally the list of localizations is a multiple of the number of planes. """ # # Load and scale the images. # [images, fit_peaks_images] = self.loadImages(movie_reader) # # Load and scale the background estimates. # bg_estimates = self.loadBackgroundEstimates(movie_reader) self.peak_finder.newImages(images) self.peak_fitter.newImages(images) peaks = False for i in range(self.peak_finder.iterations): if verbose: print(" iteration", i) # Update background estimate. for j in range(self.n_planes): self.peak_finder.subtractBackground( images[j] - fit_peaks_images[j], bg_estimates[j], j) # Find new peaks. [found_new_peaks, peaks] = self.peak_finder.findPeaks(fit_peaks_images, peaks) # Fit new peaks. if isinstance(peaks, numpy.ndarray): if verbose: print(" found", peaks.shape[0] / self.n_planes) [peaks, fit_peaks_images] = self.peak_fitter.fitPeaks(peaks) if verbose: print(" fit", peaks.shape[0] / self.n_planes) if not found_new_peaks: break if isinstance(peaks, numpy.ndarray): peaks[:, utilC.getXCenterIndex()] -= float(self.margin) peaks[:, utilC.getYCenterIndex()] -= float(self.margin) # # sa_utilities.std_analysis doesn't do anything with the second # argument, historically the residual, so just return None. # return [peaks, None]
def analyzeImage(self, new_image, bg_estimate = None, save_residual = False, verbose = False): image = fitting.padArray(new_image, self.peak_finder.margin) if bg_estimate is not None: bg_estimate = fitting.padArray(bg_estimate, self.peak_finder.margin) self.peak_finder.newImage(image, bg_estimate) self.peak_fitter.newImage(image) # # This is a lot simpler than 3D-DAOSTORM as we only do one pass, # hopefully the compressed sensing (FISTA) deconvolution finds all the # peaks and then we do a single pass of fitting. # if True: peaks = self.peak_finder.findPeaks() [fit_peaks, residual] = self.peak_fitter.fitPeaks(peaks) # # This is for testing if just using FISTA followed by the center # of mass calculation is basically as good as also doing the # additional MLE spline fitting step. # # The short answer is that it appears that it is not. It about # 1.3x worse in XY and about 4x worse in Z. # else: fit_peaks = self.peak_finder.findPeaks() # Adjust z scale. z_index = utilC.getZCenterIndex() z_size = (self.peak_fitter.spline.shape[2] - 1.0) status_index = utilC.getStatusIndex() fit_peaks[:,z_index] = z_size*fit_peaks[:,z_index] # Mark as converged. fit_peaks[:,status_index] = 1.0 residual = None # # Subtract margin so that peaks are in the right # place with respect to the original image. # fit_peaks[:,utilC.getXCenterIndex()] -= float(self.peak_finder.margin) fit_peaks[:,utilC.getYCenterIndex()] -= float(self.peak_finder.margin) return [fit_peaks, residual]
def getPeaks(self, threshold, margin): """ Extract peaks from the deconvolved image and create an array that can be used by a peak fitter. FIXME: Need to compensate for up-sampling parameter in x,y. """ fx = self.getXVector() # Get area, position, height. fd_peaks = fdUtil.getPeaks(fx, threshold, margin) num_peaks = fd_peaks.shape[0] peaks = numpy.zeros((num_peaks, utilC.getNPeakPar())) peaks[:, utilC.getXWidthIndex()] = numpy.ones(num_peaks) peaks[:, utilC.getYWidthIndex()] = numpy.ones(num_peaks) peaks[:, utilC.getXCenterIndex()] = fd_peaks[:, 2] peaks[:, utilC.getYCenterIndex()] = fd_peaks[:, 1] # Calculate height. # # FIXME: Typically the starting value for the peak height will be # under-estimated unless a large enough number of FISTA # iterations is performed to completely de-convolve the image. # h_index = utilC.getHeightIndex() #peaks[:,h_index] = fd_peaks[:,0] for i in range(num_peaks): peaks[i, h_index] = fd_peaks[i, 0] * self.psf_heights[int( round(fd_peaks[i, 3]))] # Calculate z (0.0 - 1.0). if (fx.shape[2] > 1): peaks[:, utilC.getZCenterIndex()] = fd_peaks[:, 3] / ( float(fx.shape[2]) - 1.0) # Background term calculation. bg_index = utilC.getBackgroundIndex() for i in range(num_peaks): ix = int(round(fd_peaks[i, 1])) iy = int(round(fd_peaks[i, 2])) peaks[i, bg_index] = self.background[ix, iy] return peaks
def analyzeImage(self, new_image, bg_estimate = None, save_residual = False, verbose = False): # # Pad out arrays so that we can better analyze localizations # near the edge of the original image. # image = padArray(new_image, self.margin) residual = padArray(new_image, self.margin) if bg_estimate is not None: bg_estimate = padArray(bg_estimate, self.margin) self.peak_finder.newImage(image) self.peak_fitter.newImage(image) if save_residual: resid_dax = daxwriter.DaxWriter("residual.dax", residual.shape[0], residual.shape[1]) peaks = False for i in range(self.peak_finder.iterations): if save_residual: resid_dax.addFrame(residual) no_bg_image = self.peak_finder.subtractBackground(residual, bg_estimate) [found_new_peaks, peaks] = self.peak_finder.findPeaks(no_bg_image, peaks) if isinstance(peaks, numpy.ndarray): [peaks, residual] = self.peak_fitter.fitPeaks(peaks) if verbose: if isinstance(peaks, numpy.ndarray): print(" peaks:", i, found_new_peaks, peaks.shape[0]) else: print(" peaks:", i, found_new_peaks, "NA") if not found_new_peaks: break if save_residual: resid_dax.addFrame(residual) resid_dax.close() if isinstance(peaks, numpy.ndarray): peaks[:,utilC.getXCenterIndex()] -= float(self.margin) peaks[:,utilC.getYCenterIndex()] -= float(self.margin) return [peaks, residual]
def analyze(base_name, mlist_name, settings_name): parameters = params.ParametersMultiplane().initFromFile(settings_name) # Set to only analyze 1 frame, 1 iteration of peak finding. parameters.setAttr("max_frame", "int", 1) parameters.setAttr("iterations", "int", 1) # Analyze one frame. finder = PFPeakFinder(parameters) fitter = PFPeakFitter(parameters) finder_fitter = findPeaksStd.MPFinderFitter(parameters, finder, fitter) reader = findPeaksStd.MPMovieReader(base_name=base_name, parameters=parameters) stdAnalysis.standardAnalysis(finder_fitter, reader, mlist_name, parameters) # Evaluate found vs fit parameters. hi = utilC.getHeightIndex() xi = utilC.getXCenterIndex() yi = utilC.getYCenterIndex() # First identify peak pairs. fi_x = fitted_peaks[:, xi] fi_y = fitted_peaks[:, yi] fo_x = found_peaks[:, xi] fo_y = found_peaks[:, yi] dd = utilC.peakToPeakDist(fo_x, fo_y, fi_x, fi_y) di = utilC.peakToPeakIndex(fo_x, fo_y, fi_x, fi_y) print(numpy.mean(dd), fi_x.size, fo_x.size) fo_h = [] fi_h = [] for i in range(dd.size): if (dd[i] < 2.0): fo_h.append(found_peaks[i, hi]) fi_h.append(fitted_peaks[di[i], hi]) fi_h = numpy.array(fi_h) fo_h = numpy.array(fo_h) print(numpy.mean(fi_h), fi_h.size) print(numpy.mean(fo_h), fo_h.size) print(numpy.mean(fi_h) / numpy.mean(fo_h))
def getPeaks(self, threshold, margin): fx = self.getXVector() # Get area, position, height. fd_peaks = fdUtil.getPeaks(fx, threshold, margin) num_peaks = fd_peaks.shape[0] peaks = numpy.zeros((num_peaks, utilC.getNResultsPar())) peaks[:, utilC.getXWidthIndex()] = numpy.ones(num_peaks) peaks[:, utilC.getYWidthIndex()] = numpy.ones(num_peaks) peaks[:, utilC.getXCenterIndex()] = fd_peaks[:, 2] peaks[:, utilC.getYCenterIndex()] = fd_peaks[:, 1] # Calculate height. # # FIXME: Typically the starting value for the peak height will be # under-estimated unless a large enough number of FISTA # iterations is performed to completely de-convolve the image. # h_index = utilC.getHeightIndex() #peaks[:,h_index] = fd_peaks[:,0] for i in range(num_peaks): peaks[i, h_index] = fd_peaks[i, 0] * self.psf_heights[int( round(fd_peaks[i, 3]))] # Calculate z (0.0 - 1.0). peaks[:, utilC.getZCenterIndex()] = fd_peaks[:, 3] / (float(fx.shape[2]) - 1.0) # Background term calculation. bg_index = utilC.getBackgroundIndex() for i in range(num_peaks): ix = int(round(fd_peaks[i, 1])) iy = int(round(fd_peaks[i, 2])) peaks[i, bg_index] = self.background[ix, iy] return peaks
def analyzeImage(self, movie_reader, save_residual=False, verbose=False): # Load images & background estimates. [images, fit_peaks_images] = self.loadImages(movie_reader) # Pass images to fitter. self.peak_fitter.newImages(images) # Load ground truth localization positions. truth_peaks = movie_reader.getLocalizations() # Adjust to work with multi-plane. fit_peaks = self.peak_finder.findPeaks(truth_peaks.copy()) # Calculate best fits peaks. [fit_peaks, fit_peaks_images] = self.peak_fitter.fitPeaks(fit_peaks) # Adjust for margin. fit_peaks[:, utilC.getXCenterIndex()] -= float(self.margin) fit_peaks[:, utilC.getYCenterIndex()] -= float(self.margin) return [fit_peaks, None]
def __init__(self, parameters): super(MPPeakFinder, self).__init__(parameters) self.atrans = [None] self.backgrounds = [] self.bg_filter = None self.bg_filter_sigma = parameters.getAttr("bg_filter_sigma") self.check_mode = False self.height_rescale = [] self.images = [] self.mapping_filename = None self.mfilters = [] self.mfilters_z = [] self.mpu = None self.n_channels = 0 self.s_to_psfs = [] self.variances = [] self.vfilters = [] self.xt = [] self.yt = [] self.z_values = [] # Print warning about check mode if self.check_mode: print("Warning: Running in check mode.") # Load the splines. for spline_attr in mpUtilC.getSplineAttrs(parameters): self.s_to_psfs.append( splineToPSF.loadSpline(parameters.getAttr(spline_attr))) self.n_channels += 1 # Assert that all the splines are the same size. for i in range(1, len(self.s_to_psfs)): assert (self.s_to_psfs[0].getSize() == self.s_to_psfs[i].getSize()) # # Update margin based on the spline size. Note the assumption # that all of the splines are the same size, or at least smaller # than the spline for plane 0. # old_margin = self.margin self.margin = int((self.s_to_psfs[0].getSize() + 1) / 4 + 2) # Load the plane to plane mapping data & create affine transform objects. mappings = {} if parameters.hasAttr("mapping"): if os.path.exists(parameters.getAttr("mapping")): self.mapping_filename = parameters.getAttr("mapping") with open(parameters.getAttr("mapping"), 'rb') as fp: mappings = pickle.load(fp) # Use self.margin - 1, because we added 1 to the x,y coordinates when we saved them. for i in range(self.n_channels - 1): self.xt.append( mpUtilC.marginCorrect(mappings["0_" + str(i + 1) + "_x"], self.margin - 1)) self.yt.append( mpUtilC.marginCorrect(mappings["0_" + str(i + 1) + "_y"], self.margin - 1)) self.atrans.append( affineTransformC.AffineTransform(xt=self.xt[i], yt=self.yt[i])) # # Note the assumption that the splines for each plane all use # the same z scale / have the same z range. # self.mfilters_z = parameters.getAttr("z_value", [0.0]) for zval in self.mfilters_z: self.z_values.append(self.s_to_psfs[0].getScaledZ(zval)) if parameters.hasAttr("peak_locations"): # Correct for any difference in the margins. self.peak_locations[:, utilC.getXCenterIndex( )] += self.margin - old_margin self.peak_locations[:, utilC.getYCenterIndex( )] += self.margin - old_margin # Provide the "correct" starting z value. # # FIXME: Should also allow the user to specify the peak z location # as any fixed value could be so far off for some of the # localizations that the fitting will fail. # self.peak_locations[:, utilC.getZCenterIndex()] = self.z_values[0]
def measurePSF(movie_name, zfile_name, movie_mlist, psf_name, want2d = False, aoi_size = 12, z_range = 750.0, z_step = 50.0): """ The actual z range is 2x z_range (i.e. from -z_range to z_range). """ # Load dax file, z offset file and molecule list file. dax_data = datareader.inferReader(movie_name) z_offsets = None if os.path.exists(zfile_name): try: z_offsets = numpy.loadtxt(zfile_name, ndmin = 2)[:,1] except IndexError: z_offsets = None print("z offsets were not loaded.") i3_data = readinsight3.loadI3File(movie_mlist) if want2d: print("Measuring 2D PSF") else: print("Measuring 3D PSF") # # Go through the frames identifying good peaks and adding them # to the average psf. For 3D molecule z positions are rounded to # the nearest 50nm. # z_mid = int(z_range/z_step) max_z = 2 * z_mid + 1 average_psf = numpy.zeros((max_z,4*aoi_size,4*aoi_size)) peaks_used = 0 totals = numpy.zeros(max_z) [dax_x, dax_y, dax_l] = dax_data.filmSize() for curf in range(dax_l): # Select localizations in current frame & not near the edges. mask = (i3_data['fr'] == curf+1) & (i3_data['x'] > aoi_size) & (i3_data['x'] < (dax_x - aoi_size - 1)) & (i3_data['y'] > aoi_size) & (i3_data['y'] < (dax_y - aoi_size - 1)) xr = i3_data['x'][mask] yr = i3_data['y'][mask] # Use the z offset file if it was specified, otherwise use localization z positions. if z_offsets is None: if (curf == 0): print("Using fit z locations.") zr = i3_data['z'][mask] else: if (curf == 0): print("Using z offset file.") zr = numpy.ones(xr.size) * z_offsets[curf] ht = i3_data['h'][mask] # Remove localizations that are too close to each other. in_peaks = numpy.zeros((xr.size,util_c.getNPeakPar())) in_peaks[:,util_c.getXCenterIndex()] = xr in_peaks[:,util_c.getYCenterIndex()] = yr in_peaks[:,util_c.getZCenterIndex()] = zr in_peaks[:,util_c.getHeightIndex()] = ht out_peaks = util_c.removeNeighbors(in_peaks, 2*aoi_size) #out_peaks = util_c.removeNeighbors(in_peaks, aoi_size) print(curf, "peaks in", in_peaks.shape[0], ", peaks out", out_peaks.shape[0]) # Use remaining localizations to calculate spline. image = dax_data.loadAFrame(curf).astype(numpy.float64) xr = out_peaks[:,util_c.getXCenterIndex()] yr = out_peaks[:,util_c.getYCenterIndex()] zr = out_peaks[:,util_c.getZCenterIndex()] ht = out_peaks[:,util_c.getHeightIndex()] for i in range(xr.size): xf = xr[i] yf = yr[i] zf = zr[i] xi = int(xf) yi = int(yf) if want2d: zi = 0 else: zi = int(round(zf/z_step) + z_mid) # check the z is in range if (zi > -1) and (zi < max_z): # get localization image mat = image[xi-aoi_size:xi+aoi_size, yi-aoi_size:yi+aoi_size] # zoom in by 2x psf = scipy.ndimage.interpolation.zoom(mat, 2.0) # re-center image psf = scipy.ndimage.interpolation.shift(psf, (-2.0*(xf-xi), -2.0*(yf-yi)), mode='nearest') # add to average psf accumulator average_psf[zi,:,:] += psf totals[zi] += 1 # Force PSF to be zero (on average) at the boundaries. for i in range(max_z): edge = numpy.concatenate((average_psf[i,0,:], average_psf[i,-1,:], average_psf[i,:,0], average_psf[i,:,-1])) average_psf[i,:,:] -= numpy.mean(edge) # Normalize the PSF. if want2d: max_z = 1 for i in range(max_z): print(i, totals[i]) if (totals[i] > 0.0): average_psf[i,:,:] = average_psf[i,:,:]/numpy.sum(numpy.abs(average_psf[i,:,:])) average_psf = average_psf/numpy.max(average_psf) # Save PSF (in image form). if True: import storm_analysis.sa_library.daxwriter as daxwriter dxw = daxwriter.DaxWriter(os.path.join(os.path.dirname(psf_name), "psf.dax"), average_psf.shape[1], average_psf.shape[2]) for i in range(max_z): dxw.addFrame(1000.0 * average_psf[i,:,:] + 100) dxw.close() # Save PSF. if want2d: psf_dict = {"psf" : average_psf[0,:,:], "type" : "2D"} else: cur_z = -z_range z_vals = [] for i in range(max_z): z_vals.append(cur_z) cur_z += z_step psf_dict = {"psf" : average_psf, "pixel_size" : 0.080, # 1/2 the camera pixel size in nm. "type" : "3D", "zmin" : -z_range, "zmax" : z_range, "zvals" : z_vals} pickle.dump(psf_dict, open(psf_name, 'wb'))
def psfLocalizations(i3_filename, mapping_filename, frame = 1, aoi_size = 8, movie_filename = None): # Load localizations. i3_reader = readinsight3.I3Reader(i3_filename) # Load mapping. mappings = {} if os.path.exists(mapping_filename): with open(mapping_filename, 'rb') as fp: mappings = pickle.load(fp) else: print("Mapping file not found, single channel data?") # Try and determine movie frame size. i3_metadata = readinsight3.loadI3Metadata(i3_filename) if i3_metadata is None: if movie_filename is None: raise Exception("I3 metadata not found and movie filename is not specified.") else: movie_fp = datareader.inferReader(movie_filename) [movie_y, movie_x] = movie_fp.filmSize()[:2] else: movie_data = i3_metadata.find("movie") # FIXME: These may be transposed? movie_x = int(movie_data.find("movie_x").text) movie_y = int(movie_data.find("movie_y").text) # Load localizations in the requested frame. locs = i3_reader.getMoleculesInFrame(frame) print("Loaded", locs.size, "localizations.") # Remove localizations that are too close to each other. in_locs = numpy.zeros((locs["x"].size, util_c.getNPeakPar())) in_locs[:,util_c.getXCenterIndex()] = locs["x"] in_locs[:,util_c.getYCenterIndex()] = locs["y"] out_locs = util_c.removeNeighbors(in_locs, 2 * aoi_size) xf = out_locs[:,util_c.getXCenterIndex()] yf = out_locs[:,util_c.getYCenterIndex()] # # Remove localizations that are too close to the edge or # outside of the image in any of the channels. # is_good = numpy.ones(xf.size, dtype = numpy.bool) for i in range(xf.size): # Check in Channel 0. if (xf[i] < aoi_size) or (xf[i] + aoi_size >= movie_x): is_good[i] = False continue if (yf[i] < aoi_size) or (yf[i] + aoi_size >= movie_y): is_good[i] = False continue # Check other channels. for key in mappings: if not is_good[i]: break coeffs = mappings[key] [ch1, ch2, axis] = key.split("_") if (ch1 == "0"): if (axis == "x"): xm = coeffs[0] + coeffs[1]*xf[i] + coeffs[2]*yf[i] if (xm < aoi_size) or (xm + aoi_size >= movie_x): is_good[i] = False break elif (axis == "y"): ym = coeffs[0] + coeffs[1]*xf[i] + coeffs[2]*yf[i] if (ym < aoi_size) or (ym + aoi_size >= movie_y): is_good[i] = False break # # Save localizations for each channel. # gx = xf[is_good] gy = yf[is_good] basename = os.path.splitext(i3_filename)[0] with writeinsight3.I3Writer(basename + "_c1_psf.bin") as w3: w3.addMoleculesWithXY(gx, gy) index = 1 while ("0_" + str(index) + "_x" in mappings): cx = mappings["0_" + str(index) + "_x"] cy = mappings["0_" + str(index) + "_y"] #cx = mappings[str(index) + "_0" + "_x"] #cy = mappings[str(index) + "_0" + "_y"] xm = cx[0] + cx[1] * gx + cx[2] * gy ym = cy[0] + cy[1] * gx + cy[2] * gy with writeinsight3.I3Writer(basename + "_c" + str(index+1) + "_psf.bin") as w3: w3.addMoleculesWithXY(xm, ym) index += 1 # # Print localizations that were kept. # print(gx.size, "localizations were kept:") for i in range(gx.size): print("ch0: {0:.2f} {1:.2f}".format(gx[i], gy[i])) index = 1 while ("0_" + str(index) + "_x" in mappings): cx = mappings["0_" + str(index) + "_x"] cy = mappings["0_" + str(index) + "_y"] xm = cx[0] + cx[1] * gx[i] + cx[2] * gy[i] ym = cy[0] + cy[1] * gx[i] + cy[2] * gy[i] print("ch" + str(index) + ": {0:.2f} {1:.2f}".format(xm, ym)) index += 1 print("") print("")
def __init__(self, parameters): # Member variables. self.background = None # Current estimate of the image background. self.image = None # The original image. self.iterations = parameters.iterations # Maximum number of cycles of peak finding, fitting and subtraction to perform. self.margin = PeakFinderFitter.margin # Size of the unanalyzed "edge" around the image. self.neighborhood = PeakFinder.unconverged_dist * parameters.sigma # Radius for marking neighbors as unconverged. self.new_peak_radius = PeakFinder.new_peak_dist # Minimum allowed distance between new peaks and current peaks. self.parameters = parameters # Keep access to the parameters object. self.peak_locations = None # Initial peak locations, as explained below. self.peak_mask = None # Mask for limiting peak identification to a particular AOI. self.sigma = parameters.sigma # Peak sigma (in pixels). self.taken = None # Spots in the image where a peak has already been added. self.threshold = parameters.threshold # Peak minimum threshold (height, in camera units). self.z_value = 0.0 # The starting z value to use for peak fitting. # Radius (in pixels) over which the maxima is maximal. if hasattr(parameters, "find_max_radius"): self.find_max_radius = float(parameters.find_max_radius) else: self.find_max_radius = 5 # # This is for is you already know where your want fitting to happen, as # for example in a bead calibration movie and you just want to use the # approximate locations as inputs for fitting. # # peak_locations is a text file with the peak x, y, height and background # values as white spaced columns (x and y positions are in pixels as # determined using visualizer). # # 1.0 2.0 1000.0 100.0 # 10.0 5.0 2000.0 200.0 # ... # if hasattr(parameters, "peak_locations"): print("Using peak starting locations specified in", parameters.peak_locations) # Only do one cycle of peak finding as we'll always return the same locations. if (self.iterations != 1): print("WARNING: setting number of iterations to 1!") self.iterations = 1 # Load peak x,y locations. peak_locs = numpy.loadtxt(parameters.peak_locations, ndmin=2) print(peak_locs.shape) # Create peak array. self.peak_locations = numpy.zeros( (peak_locs.shape[0], util_c.getNResultsPar())) self.peak_locations[:, util_c.getXCenterIndex( )] = peak_locs[:, 1] + self.margin self.peak_locations[:, util_c.getYCenterIndex( )] = peak_locs[:, 0] + self.margin self.peak_locations[:, util_c.getHeightIndex()] = peak_locs[:, 2] self.peak_locations[:, util_c.getBackgroundIndex()] = peak_locs[:, 3] self.peak_locations[:, util_c.getXWidthIndex()] = numpy.ones( peak_locs.shape[0]) * self.sigma self.peak_locations[:, util_c.getYWidthIndex()] = numpy.ones( peak_locs.shape[0]) * self.sigma
def measurePSF(movie_name, zfile_name, movie_mlist, psf_name, want2d=False, aoi_size=12, z_range=750.0, z_step=50.0): """ The actual z range is 2x z_range (i.e. from -z_range to z_range). """ # Load dax file, z offset file and molecule list file. dax_data = datareader.inferReader(movie_name) z_offsets = None if os.path.exists(zfile_name): try: z_offsets = numpy.loadtxt(zfile_name, ndmin=2)[:, 1] except IndexError: z_offsets = None print("z offsets were not loaded.") i3_data = readinsight3.loadI3File(movie_mlist) if want2d: print("Measuring 2D PSF") else: print("Measuring 3D PSF") # # Go through the frames identifying good peaks and adding them # to the average psf. For 3D molecule z positions are rounded to # the nearest 50nm. # z_mid = int(z_range / z_step) max_z = 2 * z_mid + 1 average_psf = numpy.zeros((max_z, 4 * aoi_size, 4 * aoi_size)) peaks_used = 0 totals = numpy.zeros(max_z) [dax_x, dax_y, dax_l] = dax_data.filmSize() for curf in range(dax_l): # Select localizations in current frame & not near the edges. mask = (i3_data['fr'] == curf + 1) & (i3_data['x'] > aoi_size) & ( i3_data['x'] < (dax_x - aoi_size - 1)) & (i3_data['y'] > aoi_size) & (i3_data['y'] < (dax_y - aoi_size - 1)) xr = i3_data['x'][mask] yr = i3_data['y'][mask] # Use the z offset file if it was specified, otherwise use localization z positions. if z_offsets is None: if (curf == 0): print("Using fit z locations.") zr = i3_data['z'][mask] else: if (curf == 0): print("Using z offset file.") zr = numpy.ones(xr.size) * z_offsets[curf] ht = i3_data['h'][mask] # Remove localizations that are too close to each other. in_peaks = numpy.zeros((xr.size, util_c.getNPeakPar())) in_peaks[:, util_c.getXCenterIndex()] = xr in_peaks[:, util_c.getYCenterIndex()] = yr in_peaks[:, util_c.getZCenterIndex()] = zr in_peaks[:, util_c.getHeightIndex()] = ht out_peaks = util_c.removeNeighbors(in_peaks, 2 * aoi_size) #out_peaks = util_c.removeNeighbors(in_peaks, aoi_size) print(curf, "peaks in", in_peaks.shape[0], ", peaks out", out_peaks.shape[0]) # Use remaining localizations to calculate spline. image = dax_data.loadAFrame(curf).astype(numpy.float64) xr = out_peaks[:, util_c.getXCenterIndex()] yr = out_peaks[:, util_c.getYCenterIndex()] zr = out_peaks[:, util_c.getZCenterIndex()] ht = out_peaks[:, util_c.getHeightIndex()] for i in range(xr.size): xf = xr[i] yf = yr[i] zf = zr[i] xi = int(xf) yi = int(yf) if want2d: zi = 0 else: zi = int(round(zf / z_step) + z_mid) # check the z is in range if (zi > -1) and (zi < max_z): # get localization image mat = image[xi - aoi_size:xi + aoi_size, yi - aoi_size:yi + aoi_size] # zoom in by 2x psf = scipy.ndimage.interpolation.zoom(mat, 2.0) # re-center image psf = scipy.ndimage.interpolation.shift( psf, (-2.0 * (xf - xi), -2.0 * (yf - yi)), mode='nearest') # add to average psf accumulator average_psf[zi, :, :] += psf totals[zi] += 1 # Force PSF to be zero (on average) at the boundaries. for i in range(max_z): edge = numpy.concatenate((average_psf[i, 0, :], average_psf[i, -1, :], average_psf[i, :, 0], average_psf[i, :, -1])) average_psf[i, :, :] -= numpy.mean(edge) # Normalize the PSF. if want2d: max_z = 1 for i in range(max_z): print(i, totals[i]) if (totals[i] > 0.0): average_psf[i, :, :] = average_psf[i, :, :] / numpy.sum( numpy.abs(average_psf[i, :, :])) average_psf = average_psf / numpy.max(average_psf) # Save PSF (in image form). if True: import storm_analysis.sa_library.daxwriter as daxwriter dxw = daxwriter.DaxWriter( os.path.join(os.path.dirname(psf_name), "psf.dax"), average_psf.shape[1], average_psf.shape[2]) for i in range(max_z): dxw.addFrame(1000.0 * average_psf[i, :, :] + 100) dxw.close() # Save PSF. if want2d: psf_dict = {"psf": average_psf[0, :, :], "type": "2D"} else: cur_z = -z_range z_vals = [] for i in range(max_z): z_vals.append(cur_z) cur_z += z_step psf_dict = { "psf": average_psf, "pixel_size": 0.080, # 1/2 the camera pixel size in nm. "type": "3D", "zmin": -z_range, "zmax": z_range, "zvals": z_vals } with open(psf_name, 'wb') as fp: pickle.dump(psf_dict, fp)
peaks_used = 0 total = 0.0 [dax_x, dax_y, dax_l] = dax_data.filmSize() while (curf < dax_l) and (peaks_used < min_peaks): # Select localizations in current frame & not near the edges. mask = (i3_data['fr'] == curf) & (i3_data['x'] > aoi_size) & ( i3_data['x'] < (dax_y - aoi_size - 1)) & (i3_data['y'] > aoi_size) & ( i3_data['y'] < (dax_x - aoi_size - 1)) xr = i3_data['x'][mask] yr = i3_data['y'][mask] ht = i3_data['h'][mask] # Remove localizations that are too close to each other. in_peaks = numpy.zeros((xr.size, util_c.getNResultsPar())) in_peaks[:, util_c.getXCenterIndex()] = xr in_peaks[:, util_c.getYCenterIndex()] = yr in_peaks[:, util_c.getHeightIndex()] = ht out_peaks = util_c.removeNeighbors(in_peaks, aoi_size) print(curf, in_peaks.shape, out_peaks.shape) # Use remaining localizations to calculate spline. image = dax_data.loadAFrame(curf - 1).astype(numpy.float64) xr = out_peaks[:, util_c.getXCenterIndex()] yr = out_peaks[:, util_c.getYCenterIndex()] ht = out_peaks[:, util_c.getHeightIndex()] for i in range(xr.size):
average_psf = numpy.zeros((4*aoi_size,4*aoi_size)) curf = 1 peaks_used = 0 total = 0.0 [dax_x, dax_y, dax_l] = dax_data.filmSize() while (curf < dax_l) and (peaks_used < min_peaks): # Select localizations in current frame & not near the edges. mask = (i3_data['fr'] == curf) & (i3_data['x'] > aoi_size) & (i3_data['x'] < (dax_y - aoi_size - 1)) & (i3_data['y'] > aoi_size) & (i3_data['y'] < (dax_x - aoi_size - 1)) xr = i3_data['x'][mask] yr = i3_data['y'][mask] ht = i3_data['h'][mask] # Remove localizations that are too close to each other. in_peaks = numpy.zeros((xr.size,util_c.getNResultsPar())) in_peaks[:,util_c.getXCenterIndex()] = xr in_peaks[:,util_c.getYCenterIndex()] = yr in_peaks[:,util_c.getHeightIndex()] = ht out_peaks = util_c.removeNeighbors(in_peaks, aoi_size) print(curf, in_peaks.shape, out_peaks.shape) # Use remaining localizations to calculate spline. image = dax_data.loadAFrame(curf-1).astype(numpy.float64) xr = out_peaks[:,util_c.getXCenterIndex()] yr = out_peaks[:,util_c.getYCenterIndex()] ht = out_peaks[:,util_c.getHeightIndex()] for i in range(xr.size):