Search in sources :

Example 1 with ExtendedPeakResult

use of gdsc.smlm.results.ExtendedPeakResult in project GDSC-SMLM by aherbert.

the class PeakFit method processResults.

private boolean processResults(FitEngine engine, ArrayList<PeakResult> sliceCandidates, int slice) {
    // Process results
    int[] maxIndices = new int[sliceCandidates.size()];
    int count = 0;
    ArrayList<PeakResult> processedResults = new ArrayList<PeakResult>(sliceCandidates.size());
    for (PeakResult result : sliceCandidates) {
        // Add ExtendedPeakResults to the results if they span multiple frames (they are the result of previous fitting).
        if (result instanceof ExtendedPeakResult && result.getFrame() != result.getEndFrame()) {
            processedResults.add(result);
        } else {
            // Fit single frame results.
            maxIndices[count++] = result.origX + bounds.width * result.origY;
        }
    }
    if (!processedResults.isEmpty())
        this.results.addAll(processedResults);
    if (count != 0) {
        float[] data = source.get(slice);
        if (data == null)
            return false;
        FitParameters fitParams = new FitParameters();
        fitParams.maxIndices = Arrays.copyOf(maxIndices, count);
        FitJob job = new ParameterisedFitJob(fitParams, slice, data, bounds);
        engine.run(job);
    }
    return true;
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) FitParameters(gdsc.smlm.engine.FitParameters) ParameterisedFitJob(gdsc.smlm.engine.ParameterisedFitJob) ArrayList(java.util.ArrayList) ParameterisedFitJob(gdsc.smlm.engine.ParameterisedFitJob) FitJob(gdsc.smlm.engine.FitJob) PeakResult(gdsc.smlm.results.PeakResult) ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult)

Example 2 with ExtendedPeakResult

use of gdsc.smlm.results.ExtendedPeakResult in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method record.

private int record(double[] xyz, int id, int peak, Statistics stats2D, Statistics stats3D, StoredDataStatistics jumpDistances2D, StoredDataStatistics jumpDistances3D, double[] origin, MemoryPeakResults results) {
    final double dx = xyz[0] - origin[0];
    final double dy = xyz[1] - origin[1];
    final double dz = xyz[2] - origin[2];
    final double jump2D = dx * dx + dy * dy;
    jumpDistances2D.add(jump2D);
    jumpDistances3D.add(jump2D + dz * dz);
    for (int i = 0; i < 3; i++) origin[i] = xyz[i];
    final double d2 = squared2D(xyz);
    stats2D.add(d2);
    stats3D.add(d2 + xyz[2] * xyz[2]);
    final float[] params = new float[7];
    params[Gaussian2DFunction.X_POSITION] = (float) xyz[0];
    params[Gaussian2DFunction.Y_POSITION] = (float) xyz[1];
    params[Gaussian2DFunction.SIGNAL] = 10f;
    params[Gaussian2DFunction.X_SD] = params[Gaussian2DFunction.Y_SD] = 1;
    final float noise = 0.1f;
    results.add(new ExtendedPeakResult(peak, (int) params[Gaussian2DFunction.X_POSITION], (int) params[Gaussian2DFunction.Y_POSITION], 10, 0, noise, params, null, peak, id));
    return ++peak;
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult)

Example 3 with ExtendedPeakResult

use of gdsc.smlm.results.ExtendedPeakResult in project GDSC-SMLM by aherbert.

the class PCPALMClusters method doClustering.

/**
	 * Extract the results from the PCPALM molecules using the area ROI and then do clustering to obtain the histogram
	 * of molecules per cluster.
	 * 
	 * @return
	 */
private HistogramData doClustering() {
    // Perform clustering analysis to generate the histogram of cluster sizes
    PCPALMAnalysis analysis = new PCPALMAnalysis();
    ArrayList<Molecule> molecules = analysis.cropToRoi(WindowManager.getCurrentImage());
    if (molecules.size() < 2) {
        error("No results within the crop region");
        return null;
    }
    Utils.log("Using %d molecules (Density = %s um^-2) @ %s nm", molecules.size(), Utils.rounded(molecules.size() / analysis.croppedArea), Utils.rounded(distance));
    long s1 = System.nanoTime();
    ClusteringEngine engine = new ClusteringEngine(1, clusteringAlgorithm, new IJTrackProgress());
    if (multiThread)
        engine.setThreadCount(Prefs.getThreads());
    engine.setTracker(new IJTrackProgress());
    IJ.showStatus("Clustering ...");
    ArrayList<Cluster> clusters = engine.findClusters(convertToPoint(molecules), distance);
    IJ.showStatus("");
    if (clusters == null) {
        Utils.log("Aborted");
        return null;
    }
    nMolecules = molecules.size();
    Utils.log("Finished : %d total clusters (%s ms)", clusters.size(), Utils.rounded((System.nanoTime() - s1) / 1e6));
    // Save cluster centroids to a results set in memory. Then they can be plotted.
    MemoryPeakResults results = new MemoryPeakResults(clusters.size());
    results.setName(TITLE);
    // Set an arbitrary calibration so that the lifetime of the results is stored in the exposure time
    // The results will be handled as a single mega-frame containing all localisation. 
    results.setCalibration(new Calibration(100, 1, PCPALMMolecules.seconds * 1000));
    // Make the standard deviation such that the Gaussian volume will be 95% at the distance threshold
    final float sd = (float) (distance / 1.959964);
    int id = 0;
    for (Cluster c : clusters) {
        results.add(new ExtendedPeakResult((float) c.x, (float) c.y, sd, c.n, ++id));
    }
    MemoryPeakResults.addResults(results);
    // Get the data for fitting
    float[] values = new float[clusters.size()];
    for (int i = 0; i < values.length; i++) values[i] = clusters.get(i).n;
    float yMax = (int) Math.ceil(Maths.max(values));
    int nBins = (int) (yMax + 1);
    float[][] hist = Utils.calcHistogram(values, 0, yMax, nBins);
    HistogramData histogramData = (calibrateHistogram) ? new HistogramData(hist, frames, area, units) : new HistogramData(hist);
    saveHistogram(histogramData);
    return histogramData;
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) IJTrackProgress(gdsc.core.ij.IJTrackProgress) Cluster(gdsc.core.clustering.Cluster) Calibration(gdsc.smlm.results.Calibration) ClusterPoint(gdsc.core.clustering.ClusterPoint) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) ClusteringEngine(gdsc.core.clustering.ClusteringEngine)

Example 4 with ExtendedPeakResult

use of gdsc.smlm.results.ExtendedPeakResult in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method aggregateIntoFrames.

private void aggregateIntoFrames(ArrayList<Point> points, boolean addError, double precisionInPixels, RandomGenerator[] random) {
    if (myAggregateSteps < 1)
        return;
    MemoryPeakResults results = new MemoryPeakResults(points.size() / myAggregateSteps);
    Calibration cal = new Calibration(settings.pixelPitch, 1, myAggregateSteps * 1000.0 / settings.stepsPerSecond);
    results.setCalibration(cal);
    results.setName(TITLE + " Aggregated");
    MemoryPeakResults.addResults(results);
    lastSimulatedDataset[1] = results.getName();
    int id = 0;
    int peak = 1;
    int n = 0;
    double cx = 0, cy = 0;
    // Get the mean square distance
    double sum = 0;
    int count = 0;
    PeakResult last = null;
    for (Point result : points) {
        final boolean newId = result.id != id;
        if (n >= myAggregateSteps || newId) {
            if (n != 0) {
                final float[] params = new float[7];
                double[] xyz = new double[] { cx / n, cy / n };
                if (addError)
                    xyz = addError(xyz, precisionInPixels, random);
                params[Gaussian2DFunction.X_POSITION] = (float) xyz[0];
                params[Gaussian2DFunction.Y_POSITION] = (float) xyz[1];
                params[Gaussian2DFunction.SIGNAL] = n;
                params[Gaussian2DFunction.X_SD] = params[Gaussian2DFunction.Y_SD] = 1;
                final float noise = 0.1f;
                PeakResult r = new ExtendedPeakResult(peak, (int) params[Gaussian2DFunction.X_POSITION], (int) params[Gaussian2DFunction.Y_POSITION], n, 0, noise, params, null, peak, id);
                results.add(r);
                if (last != null) {
                    sum += last.distance2(r);
                    count++;
                }
                last = r;
                n = 0;
                cx = cy = 0;
                peak++;
            }
            if (newId) {
                // Increment the frame so that tracing analysis can distinguish traces
                peak++;
                last = null;
                id = result.id;
            }
        }
        n++;
        cx += result.x;
        cy += result.y;
    }
    // Final peak
    if (n != 0) {
        final float[] params = new float[7];
        double[] xyz = new double[] { cx / n, cy / n };
        if (addError)
            xyz = addError(xyz, precisionInPixels, random);
        params[Gaussian2DFunction.X_POSITION] = (float) xyz[0];
        params[Gaussian2DFunction.Y_POSITION] = (float) xyz[1];
        params[Gaussian2DFunction.SIGNAL] = n;
        params[Gaussian2DFunction.X_SD] = params[Gaussian2DFunction.Y_SD] = 1;
        final float noise = 0.1f;
        PeakResult r = new ExtendedPeakResult(peak, (int) params[Gaussian2DFunction.X_POSITION], (int) params[Gaussian2DFunction.Y_POSITION], n, 0, noise, params, null, peak, id);
        results.add(r);
        if (last != null) {
            sum += last.distance2(r);
            count++;
        }
    }
    // MSD in pixels^2 / frame
    double msd = sum / count;
    // Convert to um^2/second
    Utils.log("Aggregated data D=%s um^2/s, Precision=%s nm, N=%d, step=%s s, mean=%s um^2, MSD = %s um^2/s", Utils.rounded(settings.diffusionRate), Utils.rounded(myPrecision), count, Utils.rounded(results.getCalibration().getExposureTime() / 1000), Utils.rounded(msd / conversionFactor), Utils.rounded((msd / conversionFactor) / (results.getCalibration().getExposureTime() / 1000)));
    msdAnalysis(points);
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) Calibration(gdsc.smlm.results.Calibration) PeakResult(gdsc.smlm.results.PeakResult) ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult)

Example 5 with ExtendedPeakResult

use of gdsc.smlm.results.ExtendedPeakResult in project GDSC-SMLM by aherbert.

the class CreateData method savePulses.

/**
	 * Create a set of results that represent the molecule continuous on-times (pulses)
	 * 
	 * @param localisations
	 * @param results
	 * @param title
	 */
private void savePulses(List<LocalisationModel> localisations, MemoryPeakResults results, String title) {
    sortLocalisationsByIdThenTime(localisations);
    MemoryPeakResults traceResults = copyMemoryPeakResults("Pulses");
    LocalisationModel start = null;
    int currentId = -1;
    int n = 0;
    float[] params = new float[7];
    double noise = 0;
    int lastT = -1;
    for (LocalisationModel localisation : localisations) {
        if (currentId != localisation.getId() || lastT + 1 != localisation.getTime()) {
            if (n > 0) {
                params[Gaussian2DFunction.BACKGROUND] /= n;
                params[Gaussian2DFunction.X_POSITION] /= n;
                params[Gaussian2DFunction.Y_POSITION] /= n;
                params[Gaussian2DFunction.X_SD] /= n;
                params[Gaussian2DFunction.Y_SD] /= n;
                ExtendedPeakResult p = new ExtendedPeakResult(start.getTime(), (int) Math.round(start.getX()), (int) Math.round(start.getY()), 0, 0, (float) (Math.sqrt(noise)), params, null, lastT, currentId);
                // if (p.getPrecision(107, 1) > 2000)
                // {
                // System.out.printf("Weird precision = %g (%d)\n", p.getPrecision(107, 1), n);
                // }
                traceResults.add(p);
            }
            start = localisation;
            currentId = localisation.getId();
            n = 0;
            params = new float[7];
            noise = 0;
        }
        final double[] data = localisation.getData();
        params[Gaussian2DFunction.BACKGROUND] += data[0];
        params[Gaussian2DFunction.X_POSITION] += localisation.getX();
        params[Gaussian2DFunction.Y_POSITION] += localisation.getY();
        params[Gaussian2DFunction.SIGNAL] += localisation.getIntensity();
        noise += data[1] * data[1];
        params[Gaussian2DFunction.X_SD] += data[2];
        params[Gaussian2DFunction.Y_SD] += data[3];
        n++;
        lastT = localisation.getTime();
    }
    // Final pulse
    if (n > 0) {
        params[Gaussian2DFunction.BACKGROUND] /= n;
        params[Gaussian2DFunction.X_POSITION] /= n;
        params[Gaussian2DFunction.Y_POSITION] /= n;
        params[Gaussian2DFunction.X_SD] /= n;
        params[Gaussian2DFunction.Y_SD] /= n;
        traceResults.add(new ExtendedPeakResult(start.getTime(), (int) Math.round(start.getX()), (int) Math.round(start.getY()), 0, 0, (float) (Math.sqrt(noise)), params, null, lastT, currentId));
    }
    traceResults.end();
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) LocalisationModel(gdsc.smlm.model.LocalisationModel) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults)

Aggregations

ExtendedPeakResult (gdsc.smlm.results.ExtendedPeakResult)5 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)3 Calibration (gdsc.smlm.results.Calibration)2 PeakResult (gdsc.smlm.results.PeakResult)2 Cluster (gdsc.core.clustering.Cluster)1 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 ClusteringEngine (gdsc.core.clustering.ClusteringEngine)1 IJTrackProgress (gdsc.core.ij.IJTrackProgress)1 FitJob (gdsc.smlm.engine.FitJob)1 FitParameters (gdsc.smlm.engine.FitParameters)1 ParameterisedFitJob (gdsc.smlm.engine.ParameterisedFitJob)1 LocalisationModel (gdsc.smlm.model.LocalisationModel)1 ArrayList (java.util.ArrayList)1