Search in sources :

Example 31 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class PSFCreator method fitPSF.

/**
	 * Fit the new PSF image and show a graph of the amplitude/width
	 * 
	 * @param psf
	 * @param loess
	 * @param averageRange
	 * @param fitCom
	 * @return The width of the PSF in the z-centre
	 */
private double fitPSF(ImageStack psf, LoessInterpolator loess, int cz, double averageRange, double[][] fitCom) {
    IJ.showStatus("Fitting final PSF");
    // is not appropriate for a normalised PSF. 
    if (fitConfig.getFitSolver() == FitSolver.MLE) {
        Utils.log("  Maximum Likelihood Estimation (MLE) is not appropriate for final PSF fitting.");
        Utils.log("  Switching to Least Square Estimation");
        fitConfig.setFitSolver(FitSolver.LVM);
        if (interactiveMode) {
            GlobalSettings settings = new GlobalSettings();
            settings.setFitEngineConfiguration(config);
            PeakFit.configureFitSolver(settings, null, false, false);
        }
    }
    // Update the box radius since this is used in the fitSpot method.
    boxRadius = psf.getWidth() / 2;
    int x = boxRadius, y = boxRadius;
    FitConfiguration fitConfig = config.getFitConfiguration();
    final double shift = fitConfig.getCoordinateShiftFactor();
    fitConfig.setInitialPeakStdDev0(fitConfig.getInitialPeakStdDev0() * magnification);
    fitConfig.setInitialPeakStdDev1(fitConfig.getInitialPeakStdDev1() * magnification);
    // Need to be updated after the widths have been set
    fitConfig.setCoordinateShiftFactor(shift);
    fitConfig.setBackgroundFitting(false);
    // Since the PSF will be normalised
    fitConfig.setMinPhotons(0);
    //fitConfig.setLog(new IJLogger());
    MemoryPeakResults results = fitSpot(psf, psf.getWidth(), psf.getHeight(), x, y);
    if (results.size() < 5) {
        Utils.log("  Final PSF: Not enough fit results %d", results.size());
        return 0;
    }
    // Get the results for the spot centre and width
    double[] z = new double[results.size()];
    double[] xCoord = new double[z.length];
    double[] yCoord = new double[z.length];
    double[] sd = new double[z.length];
    double[] a = new double[z.length];
    int i = 0;
    // Set limits for the fit
    final float maxWidth = (float) (FastMath.max(fitConfig.getInitialPeakStdDev0(), fitConfig.getInitialPeakStdDev1()) * magnification * 4);
    // PSF is normalised to 1  
    final float maxSignal = 2;
    for (PeakResult peak : results.getResults()) {
        // Remove bad fits where the width/signal is above the expected
        final float w = FastMath.max(peak.getXSD(), peak.getYSD());
        if (peak.getSignal() > maxSignal || w > maxWidth)
            continue;
        z[i] = peak.getFrame();
        fitCom[0][peak.getFrame() - 1] = xCoord[i] = peak.getXPosition() - x;
        fitCom[1][peak.getFrame() - 1] = yCoord[i] = peak.getYPosition() - y;
        sd[i] = w;
        a[i] = peak.getAmplitude();
        i++;
    }
    // Truncate
    z = Arrays.copyOf(z, i);
    xCoord = Arrays.copyOf(xCoord, i);
    yCoord = Arrays.copyOf(yCoord, i);
    sd = Arrays.copyOf(sd, i);
    a = Arrays.copyOf(a, i);
    // Extract the average smoothed range from the individual fits
    int r = (int) Math.ceil(averageRange / 2);
    int start = 0, stop = z.length - 1;
    for (int j = 0; j < z.length; j++) {
        if (z[j] > cz - r) {
            start = j;
            break;
        }
    }
    for (int j = z.length; j-- > 0; ) {
        if (z[j] < cz + r) {
            stop = j;
            break;
        }
    }
    // Extract xy centre coords and smooth
    double[] smoothX = new double[stop - start + 1];
    double[] smoothY = new double[smoothX.length];
    double[] smoothSd = new double[smoothX.length];
    double[] smoothA = new double[smoothX.length];
    double[] newZ = new double[smoothX.length];
    int smoothCzIndex = 0;
    for (int j = start, k = 0; j <= stop; j++, k++) {
        smoothX[k] = xCoord[j];
        smoothY[k] = yCoord[j];
        smoothSd[k] = sd[j];
        smoothA[k] = a[j];
        newZ[k] = z[j];
        if (newZ[k] == cz)
            smoothCzIndex = k;
    }
    smoothX = loess.smooth(newZ, smoothX);
    smoothY = loess.smooth(newZ, smoothY);
    smoothSd = loess.smooth(newZ, smoothSd);
    smoothA = loess.smooth(newZ, smoothA);
    // Update the widths and positions using the magnification
    final double scale = 1.0 / magnification;
    for (int j = 0; j < xCoord.length; j++) {
        xCoord[j] *= scale;
        yCoord[j] *= scale;
        sd[j] *= scale;
    }
    for (int j = 0; j < smoothX.length; j++) {
        smoothX[j] *= scale;
        smoothY[j] *= scale;
        smoothSd[j] *= scale;
    }
    showPlots(z, a, newZ, smoothA, xCoord, yCoord, sd, newZ, smoothX, smoothY, smoothSd, cz);
    // Store the data for replotting
    this.z = z;
    this.a = a;
    this.smoothAz = newZ;
    this.smoothA = smoothA;
    this.xCoord = xCoord;
    this.yCoord = yCoord;
    this.sd = sd;
    this.newZ = newZ;
    this.smoothX = smoothX;
    this.smoothY = smoothY;
    this.smoothSd = smoothSd;
    //maximumIndex = findMinimumIndex(smoothSd, maximumIndex - start);
    return smoothSd[smoothCzIndex];
}
Also used : FitConfiguration(gdsc.smlm.fitting.FitConfiguration) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint) PeakResult(gdsc.smlm.results.PeakResult)

Example 32 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class SpotInspector method getStandardDeviation.

private float getStandardDeviation(MemoryPeakResults results2) {
    // Standard deviation is only needed for the width filtering
    if (sortOrderIndex != 8)
        return 0;
    FitEngineConfiguration config = (FitEngineConfiguration) XmlUtils.fromXML(results.getConfiguration());
    if (config == null || config.getFitConfiguration() == null) {
        return -1;
    }
    FitConfiguration fitConfig = config.getFitConfiguration();
    float stdDevMax = (float) ((fitConfig.getInitialPeakStdDev0() > 0) ? fitConfig.getInitialPeakStdDev0() : 1);
    if (fitConfig.getInitialPeakStdDev1() > 0)
        stdDevMax = (float) FastMath.max(fitConfig.getInitialPeakStdDev1(), stdDevMax);
    return stdDevMax;
}
Also used : FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(gdsc.smlm.fitting.FitConfiguration)

Aggregations

FitConfiguration (gdsc.smlm.fitting.FitConfiguration)32 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)16 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)8 GenericDialog (ij.gui.GenericDialog)6 Rectangle (java.awt.Rectangle)5 BasePoint (gdsc.core.match.BasePoint)4 Calibration (gdsc.smlm.results.Calibration)4 MultiPathFilter (gdsc.smlm.results.filter.MultiPathFilter)4 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)4 IJLogger (gdsc.core.ij.IJLogger)3 Gaussian2DFitter (gdsc.smlm.fitting.Gaussian2DFitter)3 PeakResultPoint (gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint)3 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)3 DirectFilter (gdsc.smlm.results.filter.DirectFilter)3 Checkbox (java.awt.Checkbox)3 ImageExtractor (gdsc.core.utils.ImageExtractor)2 FitEngine (gdsc.smlm.engine.FitEngine)2 MaximaSpotFilter (gdsc.smlm.filters.MaximaSpotFilter)2 FitResult (gdsc.smlm.fitting.FitResult)2 FitStatus (gdsc.smlm.fitting.FitStatus)2