Search in sources :

Example 46 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class ResultsMatchCalculator method savePairs.

private void savePairs(MemoryPeakResults results1, MemoryPeakResults results2, List<PointPair> allMatches) {
    if (!settings.savePairs) {
        return;
    }
    // Get the directory
    final String directory = ImageJUtils.getDirectory("Pairs_directory", settings.pairsDirectory);
    if (directory == null) {
        return;
    }
    settings.pairsDirectory = directory;
    final double[] distanceThresholds = getDistances(settings.distanceThreshold, settings.increments, settings.delta);
    // Create output files for each distance band
    final PeakResults[] output1 = new PeakResults[distanceThresholds.length];
    final PeakResults[] output2 = new PeakResults[distanceThresholds.length];
    double high = 0;
    for (int i = 0; i < distanceThresholds.length; i++) {
        final double low = high;
        high = distanceThresholds[i];
        output1[i] = createFilePeakResults(directory, 1, results1, low, high);
        output2[i] = createFilePeakResults(directory, 2, results2, low, high);
    }
    // Square the thresholds
    SimpleArrayUtils.apply(distanceThresholds, v -> v * v);
    final double[] pairDistances = getPairDistances(allMatches);
    int index = 0;
    for (final PointPair pair : allMatches) {
        final int insert = search(distanceThresholds, pairDistances[index++]);
        if (insert != -1) {
            final PeakResult r1 = ((PeakResultPoint) pair.getPoint1()).getPeakResult();
            final PeakResult r2 = ((PeakResultPoint) pair.getPoint2()).getPeakResult();
            output1[insert].add(r1);
            output2[insert].add(r2);
        }
    }
    for (int i = 0; i < output1.length; i++) {
        output1[i].end();
        output2[i].end();
    }
}
Also used : MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) PointPair(uk.ac.sussex.gdsc.core.match.PointPair) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) Point(java.awt.Point) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 47 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class ResultsMatchCalculator method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    if (!showDialog()) {
        return;
    }
    // Load the results
    final MemoryPeakResults results1 = ResultsManager.loadInputResults(settings.inputOption1, false, null, null);
    final MemoryPeakResults results2 = ResultsManager.loadInputResults(settings.inputOption2, false, null, null);
    IJ.showStatus("");
    if (results1 == null || results1.size() == 0) {
        IJ.error(TITLE, "No results 1 could be loaded");
        return;
    }
    if (results2 == null || results2.size() == 0) {
        IJ.error(TITLE, "No results 2 could be loaded");
        return;
    }
    if (results1.getDistanceUnit() != results2.getDistanceUnit()) {
        IJ.error(TITLE, "Distance unit should be the same for the results");
        return;
    }
    final long start = System.nanoTime();
    runCompareCoordinates(results1, results2);
    final long nanoseconds = System.nanoTime() - start;
    IJ.showStatus(String.format("%s = %s", TITLE, TextUtils.nanosToString(nanoseconds)));
}
Also used : MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 48 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class TraceDiffusion method getTraces.

private Trace[] getTraces(ArrayList<MemoryPeakResults> allResults) {
    this.results = allResults.get(0);
    // Results should be checked for calibration by this point
    exposureTime = results.getCalibrationReader().getExposureTime() / 1000;
    final Function<MemoryPeakResults, Trace[]> traceFunction = createTraceFunction();
    final ArrayList<Trace> allTraces = new ArrayList<>();
    additionalDatasets = -1;
    for (final MemoryPeakResults r : allResults) {
        additionalDatasets++;
        Trace[] traces = traceFunction.apply(r);
        traces = filterTraces(r.getName(), traces, clusteringSettings.getMinimumTraceLength(), clusteringSettings.getIgnoreEnds());
        allTraces.addAll(Arrays.asList(traces));
        // --- Save results ---
        if (traces.length != 0) {
            // Save the traces to memory
            TraceMolecules.saveResults(r, traces, "Tracks");
            if (clusteringSettings.getSaveTraces()) {
                // Sort traces by time to assist the results source in extracting frames sequentially.
                // Do this before saving to assist in debugging using the saved traces file.
                TraceMolecules.sortByTime(traces);
                final String newFilename = TraceMolecules.saveTraces(r, traces, createSettingsComment(), settings.tracesFilename, additionalDatasets);
                // Only keep the main filename in memory
                if (additionalDatasets == 0) {
                    settings.tracesFilename = newFilename;
                }
            }
        }
    }
    final Trace[] all = allTraces.toArray(new Trace[0]);
    if (additionalDatasets != 0) {
        ImageJUtils.log("Multiple inputs provide %d traces", allTraces.size());
        final MemoryPeakResults tracedResults = TraceManager.toPeakResults(all, results.getCalibration(), true);
        tracedResults.copySettings(results);
        tracedResults.setName(createCombinedName() + " Tracks");
        MemoryPeakResults.addResults(tracedResults);
    }
    return all;
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) ArrayList(java.util.ArrayList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 49 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class DriftCalculator method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    // Require some fit results and selected regions
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "There are no fitting results in memory");
        return;
    }
    final Roi[] rois = getRois();
    final String[] stackTitles = createStackImageList();
    if (!showDialog(rois, stackTitles)) {
        return;
    }
    final MemoryPeakResults results = ResultsManager.loadInputResults(settings.inputOption, false, DistanceUnit.PIXEL, null);
    if (results == null || results.size() < 2) {
        IJ.error(TITLE, "There are not enough fitting results for drift correction");
        return;
    }
    double[][] drift = null;
    final int[] limits = findTimeLimits(results);
    try {
        if (settings.method.equals(Settings.MARKED_ROIS)) {
            drift = calculateUsingMarkers(results, limits, rois);
        } else if (settings.method.equals(Settings.STACK_ALIGNMENT)) {
            final ImageStack stack = showStackDialog(stackTitles);
            if (stack == null) {
                return;
            }
            drift = calculateUsingImageStack(stack, limits);
        } else if (settings.method.equals(Settings.DRIFT_FILE)) {
            drift = calculateUsingDriftFile(limits);
        } else {
            if (!showSubImageDialog()) {
                return;
            }
            drift = calculateUsingFrames(results, limits, Integer.parseInt(settings.reconstructionSize));
        }
    } finally {
        if (executor != null) {
            executor.shutdown();
        }
    }
    if (drift == null) {
        return;
    }
    ImageJUtils.log("Drift correction interpolated for frames [%d - %d] of [%d - %d] (%s%%)", interpolationStart, interpolationEnd, limits[0], limits[1], MathUtils.rounded((100.0 * (interpolationEnd - interpolationStart + 1)) / (limits[1] - limits[0] + 1)));
    applyDriftCorrection(results, drift);
}
Also used : ImageStack(ij.ImageStack) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) Roi(ij.gui.Roi)

Example 50 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method aggregateIntoFrames.

private void aggregateIntoFrames(ArrayList<Point> points, boolean addError, double precisionInPixels, NormalizedGaussianSampler gauss) {
    if (myAggregateSteps < 1) {
        return;
    }
    final MemoryPeakResults results = new MemoryPeakResults(points.size() / myAggregateSteps);
    results.setCalibration(CalibrationHelper.create(settings.getPixelPitch(), 1, myAggregateSteps * 1000.0 / settings.getStepsPerSecond()));
    results.setName(TITLE + " Aggregated");
    results.setPsf(PsfHelper.create(PSFType.CUSTOM));
    MemoryPeakResults.addResults(results);
    simulation.dataset[1] = results.getName();
    int id = 0;
    int peak = 1;
    int number = 0;
    double cx = 0;
    double cy = 0;
    // Get the mean square distance
    double sum = 0;
    int count = 0;
    PeakResult last = null;
    for (final Point result : points) {
        final boolean newId = result.id != id;
        if (number >= myAggregateSteps || newId) {
            if (number != 0) {
                double[] xyz = new double[] { cx / number, cy / number };
                if (addError) {
                    xyz = addError(xyz, precisionInPixels, gauss);
                }
                final float[] params = PeakResult.createParams(0, number, (float) xyz[0], (float) xyz[1], 0);
                final float noise = 0.1f;
                final PeakResult r = new ExtendedPeakResult(peak, (int) params[PeakResult.X], (int) params[PeakResult.Y], number, 0, noise, 0, params, null, peak, id);
                results.add(r);
                if (last != null) {
                    sum += last.distance2(r);
                    count++;
                }
                last = r;
                number = 0;
                cx = cy = 0;
                peak++;
            }
            if (newId) {
                // Increment the frame so that tracing analysis can distinguish traces
                peak++;
                last = null;
                id = result.id;
            }
        }
        number++;
        cx += result.x;
        cy += result.y;
    }
    // Final peak
    if (number != 0) {
        double[] xyz = new double[] { cx / number, cy / number };
        if (addError) {
            xyz = addError(xyz, precisionInPixels, gauss);
        }
        final float[] params = PeakResult.createParams(0, number, (float) xyz[0], (float) xyz[1], 0);
        final float noise = 0.1f;
        final PeakResult r = new ExtendedPeakResult(peak, (int) params[PeakResult.X], (int) params[PeakResult.Y], number, 0, noise, 0, params, null, peak, id);
        results.add(r);
        if (last != null) {
            sum += last.distance2(r);
            count++;
        }
    }
    // MSD in pixels^2 / frame
    final double msd = sum / count;
    // Convert to um^2/second
    ImageJUtils.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", MathUtils.rounded(settings.getDiffusionRate()), MathUtils.rounded(myPrecision), count, MathUtils.rounded(results.getCalibrationReader().getExposureTime() / 1000), MathUtils.rounded(msd / conversionFactor), MathUtils.rounded((msd / conversionFactor) / (results.getCalibrationReader().getExposureTime() / 1000)));
    msdAnalysis(points);
}
Also used : ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult)

Aggregations

MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)138 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)61 List (java.util.List)47 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)46 IJ (ij.IJ)39 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)39 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)38 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)38 ArrayList (java.util.ArrayList)36 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 PlugIn (ij.plugin.PlugIn)34 MathUtils (uk.ac.sussex.gdsc.core.utils.MathUtils)33 Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)33 ImagePlus (ij.ImagePlus)31 Rectangle (java.awt.Rectangle)31 SimpleArrayUtils (uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils)31 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)28 TextUtils (uk.ac.sussex.gdsc.core.utils.TextUtils)28 SettingsManager (uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager)28 FrameCounter (uk.ac.sussex.gdsc.smlm.results.count.FrameCounter)28