Search in sources :

Example 36 with MemoryPeakResults

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

the class BenchmarkFilterAnalysis method saveResults.

/**
 * Save the results to memory.
 *
 * @param filterResults The results from running the filter (or null)
 * @param filter the filter
 */
private void saveResults(PreprocessedPeakResult[] filterResults, DirectFilter filter) {
    final MemoryPeakResults newResults = createResults(filterResults, filter, true);
    MemoryPeakResults.addResults(newResults);
}
Also used : MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 37 with MemoryPeakResults

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

the class BenchmarkFilterAnalysis method getCoordinates.

private static TIntObjectHashMap<UniqueIdPeakResult[]> getCoordinates(MemoryPeakResults results) {
    final TIntObjectHashMap<UniqueIdPeakResult[]> coords = new TIntObjectHashMap<>();
    if (results.size() > 0) {
        // Do not use HashMap directly to build the coords object since there
        // will be many calls to getEntry(). Instead sort the results and use
        // a new list for each time point
        results.sort();
        final Counter uniqueId = new Counter();
        final FrameCounter counter = new FrameCounter();
        final LocalList<PeakResult> tmp = new LocalList<>();
        // Add the results to the lists
        results.forEach((PeakResultProcedure) result -> {
            if (counter.advanceAndReset(result.getFrame()) && !tmp.isEmpty()) {
                coords.put(counter.previousFrame(), tmp.toArray(new UniqueIdPeakResult[0]));
                tmp.clear();
            }
            tmp.add(new UniqueIdPeakResult(tmp.size(), uniqueId.getAndIncrement(), result));
        });
        if (!tmp.isEmpty()) {
            coords.put(counter.currentFrame(), tmp.toArray(new UniqueIdPeakResult[0]));
        }
    }
    return coords;
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) CoordinateStoreFactory(uk.ac.sussex.gdsc.smlm.results.filter.CoordinateStoreFactory) Macro(ij.Macro) ConvergenceChecker(uk.ac.sussex.gdsc.smlm.search.ConvergenceChecker) BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) MultiPathFitResults(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFitResults) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) SearchResult(uk.ac.sussex.gdsc.smlm.search.SearchResult) HelpUrls(uk.ac.sussex.gdsc.smlm.ij.plugins.HelpUrls) Vector(java.util.Vector) SearchSpace(uk.ac.sussex.gdsc.smlm.search.SearchSpace) Pair(org.apache.commons.lang3.tuple.Pair) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) Map(java.util.Map) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) Triple(org.apache.commons.lang3.tuple.Triple) Chromosome(uk.ac.sussex.gdsc.smlm.ga.Chromosome) FilterScore(uk.ac.sussex.gdsc.smlm.results.filter.FilterScore) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) BlockingQueue(java.util.concurrent.BlockingQueue) StopWatch(org.apache.commons.lang3.time.StopWatch) ConcurrencyUtils(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrencyUtils) MultiPathFitResult(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFitResult) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) Plot(ij.gui.Plot) PeakFit(uk.ac.sussex.gdsc.smlm.ij.plugins.PeakFit) TIntHashSet(gnu.trove.set.hash.TIntHashSet) ImagePlus(ij.ImagePlus) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TextArea(java.awt.TextArea) PeakFractionalAssignment(uk.ac.sussex.gdsc.smlm.results.filter.PeakFractionalAssignment) RampedSelectionStrategy(uk.ac.sussex.gdsc.smlm.ga.RampedSelectionStrategy) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) ConvergenceToleranceChecker(uk.ac.sussex.gdsc.smlm.search.ConvergenceToleranceChecker) FitnessFunction(uk.ac.sussex.gdsc.smlm.ga.FitnessFunction) PlugIn(ij.plugin.PlugIn) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Prefs(ij.Prefs) StoredData(uk.ac.sussex.gdsc.core.utils.StoredData) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) SimpleDateFormat(java.text.SimpleDateFormat) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MatchResult(uk.ac.sussex.gdsc.core.match.MatchResult) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ToleranceChecker(uk.ac.sussex.gdsc.smlm.ga.ToleranceChecker) GenericDialog(ij.gui.GenericDialog) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) Overlay(ij.gui.Overlay) Population(uk.ac.sussex.gdsc.smlm.ga.Population) BenchmarkSpotFilterResult(uk.ac.sussex.gdsc.smlm.ij.plugins.benchmark.BenchmarkSpotFilter.BenchmarkSpotFilterResult) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) FileOutputStream(java.io.FileOutputStream) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) File(java.io.File) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) ResultsMatchCalculator(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsMatchCalculator) PreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.PreprocessedPeakResult) SimpleSelectionStrategy(uk.ac.sussex.gdsc.smlm.ga.SimpleSelectionStrategy) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) BufferedReader(java.io.BufferedReader) DoubleEquality(uk.ac.sussex.gdsc.core.utils.DoubleEquality) FractionScoreStore(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter.FractionScoreStore) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) SimpleRecombiner(uk.ac.sussex.gdsc.smlm.ga.SimpleRecombiner) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) Date(java.util.Date) FilterCandidates(uk.ac.sussex.gdsc.smlm.ij.plugins.benchmark.BenchmarkSpotFit.FilterCandidates) TextWindow(ij.text.TextWindow) BenchmarkSpotFitResult(uk.ac.sussex.gdsc.smlm.ij.plugins.benchmark.BenchmarkSpotFit.BenchmarkSpotFitResult) ParameterType(uk.ac.sussex.gdsc.smlm.results.filter.ParameterType) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultAssignment(uk.ac.sussex.gdsc.smlm.results.filter.ResultAssignment) PlotWindow(ij.gui.PlotWindow) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) TIntObjectProcedure(gnu.trove.procedure.TIntObjectProcedure) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) Recombiner(uk.ac.sussex.gdsc.smlm.ga.Recombiner) SimpleMutator(uk.ac.sussex.gdsc.smlm.ga.SimpleMutator) BufferedTextWindow(uk.ac.sussex.gdsc.core.ij.BufferedTextWindow) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) SpotFinderPreview(uk.ac.sussex.gdsc.smlm.ij.plugins.SpotFinderPreview) Coordinate(uk.ac.sussex.gdsc.core.match.Coordinate) Gaussian2DPeakResultHelper(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultHelper) Logger(java.util.logging.Logger) FractionalAssignment(uk.ac.sussex.gdsc.core.match.FractionalAssignment) ResultAssignmentDistanceComparator(uk.ac.sussex.gdsc.smlm.results.filter.ResultAssignmentDistanceComparator) FilterXStreamUtils(uk.ac.sussex.gdsc.smlm.results.filter.FilterXStreamUtils) SettingsList(uk.ac.sussex.gdsc.core.utils.SettingsList) List(java.util.List) SearchDimension(uk.ac.sussex.gdsc.smlm.search.SearchDimension) FullScoreFunction(uk.ac.sussex.gdsc.smlm.search.FullScoreFunction) ResultsImageSampler(uk.ac.sussex.gdsc.smlm.ij.results.ResultsImageSampler) Rectangle(java.awt.Rectangle) PeakResultGridManager(uk.ac.sussex.gdsc.smlm.results.PeakResultGridManager) ConsecutiveFailCounter(uk.ac.sussex.gdsc.smlm.results.count.ConsecutiveFailCounter) GridCoordinateStore(uk.ac.sussex.gdsc.smlm.results.filter.GridCoordinateStore) PrecisionMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.PrecisionMethod) FixedDimension(uk.ac.sussex.gdsc.smlm.search.FixedDimension) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) AtomicReference(java.util.concurrent.atomic.AtomicReference) TrackProgress(uk.ac.sussex.gdsc.core.logging.TrackProgress) TextField(java.awt.TextField) Level(java.util.logging.Level) ParameterUtils(uk.ac.sussex.gdsc.smlm.ij.plugins.ParameterUtils) ImageListener(ij.ImageListener) TemplateSettings(uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings) OutputStreamWriter(java.io.OutputStreamWriter) LinkedList(java.util.LinkedList) ScoreFunctionHelper(uk.ac.sussex.gdsc.smlm.search.ScoreFunctionHelper) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) GUIFilterSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.GUIFilterSettings) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) Iterator(java.util.Iterator) RampedScore(uk.ac.sussex.gdsc.core.utils.RampedScore) CoordinateStore(uk.ac.sussex.gdsc.smlm.results.filter.CoordinateStore) Checkbox(java.awt.Checkbox) FilterType(uk.ac.sussex.gdsc.smlm.results.filter.FilterType) ConfigurationTemplate(uk.ac.sussex.gdsc.smlm.ij.plugins.ConfigurationTemplate) FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) ClassificationResult(uk.ac.sussex.gdsc.core.match.ClassificationResult) FileInputStream(java.io.FileInputStream) Recorder(ij.plugin.frame.Recorder) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) SelectionStrategy(uk.ac.sussex.gdsc.smlm.ga.SelectionStrategy) Consumer(java.util.function.Consumer) ImageWindow(ij.gui.ImageWindow) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) SmlmUsageTracker(uk.ac.sussex.gdsc.smlm.ij.plugins.SmlmUsageTracker) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) Comparator(java.util.Comparator) Collections(java.util.Collections) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) ConsecutiveFailCounter(uk.ac.sussex.gdsc.smlm.results.count.ConsecutiveFailCounter) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) PreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.PreprocessedPeakResult)

Example 38 with MemoryPeakResults

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

the class BenchmarkFilterAnalysis method createResults.

/**
 * Create peak results.
 *
 * @param filterResults The results from running the filter (or null)
 * @param filter the filter
 */
private MemoryPeakResults createResults(PreprocessedPeakResult[] filterResults, DirectFilter filter, boolean withBorder) {
    if (filterResults == null) {
        final MultiPathFilter multiPathFilter = createMpf(filter, defaultMinimalFilter);
        filterResults = filterResults(multiPathFilter);
    }
    final MemoryPeakResults newResults = new MemoryPeakResults();
    newResults.copySettings(this.results);
    newResults.setName(TITLE);
    if (withBorder) {
        // To produce the same results as the PeakFit plugin we must implement the border
        // functionality used in the FitWorker. This respects the border of the spot filter.
        final FitEngineConfiguration config = new FitEngineConfiguration();
        updateAllConfiguration(config);
        final MaximaSpotFilter spotFilter = config.createSpotFilter();
        final int border = spotFilter.getBorder();
        final Rectangle bounds = getBounds();
        final int borderLimitX = bounds.x + bounds.width - border;
        final int borderLimitY = bounds.y + bounds.height - border;
        for (final PreprocessedPeakResult spot : filterResults) {
            if (spot.getX() > border && spot.getX() < borderLimitX && spot.getY() > border && spot.getY() < borderLimitY) {
                final double[] p = spot.toGaussian2DParameters();
                final float[] params = new float[p.length];
                for (int j = 0; j < p.length; j++) {
                    params[j] = (float) p[j];
                }
                final int frame = spot.getFrame();
                final int origX = (int) p[Gaussian2DFunction.X_POSITION];
                final int origY = (int) p[Gaussian2DFunction.Y_POSITION];
                newResults.add(frame, origX, origY, 0, 0, spot.getNoise(), spot.getMeanSignal(), params, null);
            }
        }
    } else {
        for (final PreprocessedPeakResult spot : filterResults) {
            final double[] p = spot.toGaussian2DParameters();
            final float[] params = new float[p.length];
            for (int j = 0; j < p.length; j++) {
                params[j] = (float) p[j];
            }
            final int frame = spot.getFrame();
            final int origX = (int) p[Gaussian2DFunction.X_POSITION];
            final int origY = (int) p[Gaussian2DFunction.Y_POSITION];
            newResults.add(frame, origX, origY, 0, 0, spot.getNoise(), spot.getMeanSignal(), params, null);
        }
    }
    return newResults;
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) Rectangle(java.awt.Rectangle) BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.PreprocessedPeakResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 39 with MemoryPeakResults

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

the class TraceDiffusion method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    jumpDistanceParametersRef.set(null);
    extraOptions = ImageJUtils.isExtraOptions();
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    settings = Settings.load();
    // Saved by reference so just save now
    settings.save();
    final ArrayList<MemoryPeakResults> allResults = new ArrayList<>();
    // Option to pick multiple input datasets together using a list box.
    if ("multi".equals(arg) && !showMultiDialog(allResults)) {
        return;
    }
    // This shows the dialog for selecting trace options
    if (!showTraceDialog(allResults)) {
        return;
    }
    if (allResults.isEmpty()) {
        return;
    }
    ImageJUtils.log(TITLE + "...");
    // - Trace each single dataset (and store in memory)
    // - Combine trace results held in memory
    final Trace[] traces = getTraces(allResults);
    // This still allows a zero entry in the results table.
    if (traces.length > 0 && !showDialog()) {
        return;
    }
    final int count = traces.length;
    double[] fitMsdResult = null;
    int numberOfDataPoints = 0;
    double[][] jdParams = null;
    if (count > 0) {
        calculatePrecision(traces, allResults.size() > 1);
        // --- MSD Analysis ---
        // Conversion constants
        final double px2ToUm2 = MathUtils.pow2(results.getCalibrationReader().getNmPerPixel()) / 1e6;
        final double px2ToUm2PerSecond = px2ToUm2 / exposureTime;
        // Get the maximum trace length
        int length = clusteringSettings.getMinimumTraceLength();
        if (!clusteringSettings.getTruncate()) {
            for (final Trace trace : traces) {
                if (length < trace.size()) {
                    length = trace.size();
                }
            }
        }
        // Get the localisation error (4s^2) in um^2
        final double error = (clusteringSettings.getPrecisionCorrection()) ? 4 * precision * precision / 1e6 : 0;
        // Pre-calculate MSD correction factors. This accounts for the fact that the distance moved
        // in the start/end frames is reduced due to the averaging of the particle location over the
        // entire frame into a single point. The true MSD may be restored by applying a factor.
        // Note: These are used for the calculation of the diffusion coefficients per molecule and
        // the MSD passed to the Jump Distance analysis. However the error is not included in the
        // jump distance analysis so will be subtracted from the fitted D coefficients later.
        final double[] factors;
        if (clusteringSettings.getMsdCorrection()) {
            factors = new double[length];
            for (int t = 1; t < length; t++) {
                factors[t] = JumpDistanceAnalysis.getConversionfactor(t);
            }
        } else {
            factors = SimpleArrayUtils.newArray(length, 0.0, 1.0);
        }
        // Extract the mean-squared distance statistics
        final Statistics[] stats = new Statistics[length];
        for (int i = 0; i < stats.length; i++) {
            stats[i] = new Statistics();
        }
        final ArrayList<double[]> distances = (settings.saveTraceDistances || settings.displayTraceLength) ? new ArrayList<>(traces.length) : null;
        // Store all the jump distances at the specified interval
        final StoredDataStatistics jumpDistances = new StoredDataStatistics();
        final int jumpDistanceInterval = clusteringSettings.getJumpDistance();
        // Compute squared distances
        final StoredDataStatistics msdPerMoleculeAllVsAll = new StoredDataStatistics();
        final StoredDataStatistics msdPerMoleculeAdjacent = new StoredDataStatistics();
        for (final Trace trace : traces) {
            final PeakResultStoreList results = trace.getPoints();
            // Sum the MSD and the time
            final int traceLength = (clusteringSettings.getTruncate()) ? clusteringSettings.getMinimumTraceLength() : trace.size();
            // Get the mean for each time separation
            final double[] sumDistance = new double[traceLength + 1];
            final double[] sumTime = new double[sumDistance.length];
            // Do the distances to the origin (saving if necessary)
            final float x0 = results.get(0).getXPosition();
            final float y0 = results.get(0).getYPosition();
            if (distances != null) {
                final double[] msd = new double[traceLength - 1];
                for (int j = 1; j < traceLength; j++) {
                    final int t = j;
                    final double d = distance2(x0, y0, results.get(j));
                    msd[j - 1] = px2ToUm2 * d;
                    if (t == jumpDistanceInterval) {
                        jumpDistances.add(msd[j - 1]);
                    }
                    sumDistance[t] += d;
                    sumTime[t] += t;
                }
                distances.add(msd);
            } else {
                for (int j = 1; j < traceLength; j++) {
                    final int t = j;
                    final double d = distance2(x0, y0, results.get(j));
                    if (t == jumpDistanceInterval) {
                        jumpDistances.add(px2ToUm2 * d);
                    }
                    sumDistance[t] += d;
                    sumTime[t] += t;
                }
            }
            if (clusteringSettings.getInternalDistances()) {
                // Do the internal distances
                for (int i = 1; i < traceLength; i++) {
                    final float x = results.get(i).getXPosition();
                    final float y = results.get(i).getYPosition();
                    for (int j = i + 1; j < traceLength; j++) {
                        final int t = j - i;
                        final double d = distance2(x, y, results.get(j));
                        if (t == jumpDistanceInterval) {
                            jumpDistances.add(px2ToUm2 * d);
                        }
                        sumDistance[t] += d;
                        sumTime[t] += t;
                    }
                }
                // Add the average distance per time separation to the population
                for (int t = 1; t < traceLength; t++) {
                    // Note: (traceLength - t) == count
                    stats[t].add(sumDistance[t] / (traceLength - t));
                }
            } else {
                // Add the distance per time separation to the population
                for (int t = 1; t < traceLength; t++) {
                    stats[t].add(sumDistance[t]);
                }
            }
            // Fix this for the precision and MSD adjustment.
            // It may be necessary to:
            // - sum the raw distances for each time interval (this is sumDistance[t])
            // - subtract the precision error
            // - apply correction factor for the n-frames to get actual MSD
            // - sum the actual MSD
            double sumD = 0;
            final double sumD_adjacent = Math.max(0, sumDistance[1] - error) * factors[1];
            double sumT = 0;
            final double sumT_adjacent = sumTime[1];
            for (int t = 1; t < traceLength; t++) {
                sumD += Math.max(0, sumDistance[t] - error) * factors[t];
                sumT += sumTime[t];
            }
            // Calculate the average displacement for the trace (do not simply use the largest
            // time separation since this will miss moving molecules that end up at the origin)
            msdPerMoleculeAllVsAll.add(px2ToUm2PerSecond * sumD / sumT);
            msdPerMoleculeAdjacent.add(px2ToUm2PerSecond * sumD_adjacent / sumT_adjacent);
        }
        StoredDataStatistics dperMoleculeAllVsAll = null;
        StoredDataStatistics dperMoleculeAdjacent = null;
        if (settings.saveTraceDistances || (clusteringSettings.getShowHistograms() && settings.displayDHistogram)) {
            dperMoleculeAllVsAll = calculateDiffusionCoefficient(msdPerMoleculeAllVsAll);
            dperMoleculeAdjacent = calculateDiffusionCoefficient(msdPerMoleculeAdjacent);
        }
        if (settings.saveTraceDistances) {
            saveTraceDistances(traces.length, distances, msdPerMoleculeAllVsAll, msdPerMoleculeAdjacent, dperMoleculeAllVsAll, dperMoleculeAdjacent);
        }
        if (settings.displayTraceLength) {
            final StoredDataStatistics lengths = calculateTraceLengths(distances);
            showHistogram(lengths, "Trace length (um)");
        }
        if (settings.displayTraceSize) {
            final StoredDataStatistics sizes = calculateTraceSizes(traces);
            showHistogram(sizes, "Trace size", true);
        }
        // Plot the per-trace histogram of MSD and D
        if (clusteringSettings.getShowHistograms()) {
            if (settings.displayMsdHistogram) {
                showHistogram(msdPerMoleculeAllVsAll, "MSD/Molecule (all-vs-all)");
                showHistogram(msdPerMoleculeAdjacent, "MSD/Molecule (adjacent)");
            }
            if (settings.displayDHistogram) {
                showHistogram(dperMoleculeAllVsAll, "D/Molecule (all-vs-all)");
                showHistogram(dperMoleculeAdjacent, "D/Molecule (adjacent)");
            }
        }
        // Calculate the mean squared distance (MSD)
        final double[] x = new double[stats.length];
        final double[] y = new double[x.length];
        final double[] sd = new double[x.length];
        // Intercept is the 4s^2 (in um^2)
        y[0] = 4 * precision * precision / 1e6;
        for (int i = 1; i < stats.length; i++) {
            x[i] = i * exposureTime;
            y[i] = stats[i].getMean() * px2ToUm2;
            // sd[i] = stats[i].getStandardDeviation() * px2ToUm2;
            sd[i] = stats[i].getStandardError() * px2ToUm2;
        }
        final String title = TITLE + " MSD";
        final Plot plot = plotMsd(x, y, sd, title);
        // Fit the MSD using a linear fit
        fitMsdResult = fitMsd(x, y, title, plot);
        // Jump Distance analysis
        if (settings.saveRawData) {
            saveStatistics(jumpDistances, "Jump Distance", "Distance (um^2)", false);
        }
        // Calculate the cumulative jump-distance histogram
        final double[][] jdHistogram = JumpDistanceAnalysis.cumulativeHistogram(jumpDistances.getValues());
        // Always show the jump distance histogram
        jdTitle = TITLE + " Jump Distance";
        jdPlot = new Plot(jdTitle, "Distance (um^2)", "Cumulative Probability");
        jdPlot.addPoints(jdHistogram[0], jdHistogram[1], Plot.LINE);
        display(jdTitle, jdPlot);
        // Fit Jump Distance cumulative probability
        numberOfDataPoints = jumpDistances.getN();
        jdParams = fitJumpDistance(jumpDistances, jdHistogram);
        jumpDistanceParametersRef.set(jdParams);
    }
    summarise(traces, fitMsdResult, numberOfDataPoints, jdParams);
}
Also used : PeakResultStoreList(uk.ac.sussex.gdsc.smlm.results.PeakResultStoreList) Plot(ij.gui.Plot) ArrayList(java.util.ArrayList) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 40 with MemoryPeakResults

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

the class TraceDiffusion method createTraceFunction.

/**
 * Creates the trace function for the configured trace diffusion mode.
 *
 * @return the function
 */
private Function<MemoryPeakResults, Trace[]> createTraceFunction() {
    if (clusteringSettings.getTraceDiffusionMode() == 1) {
        final DmttConfiguration config = createDmttConfiguration();
        return r -> new DynamicMultipleTargetTracing(r).traceMolecules(config).toArray(new Trace[0]);
    }
    // Nearest neighbour
    // Convert from NM to the native units of the results
    final Converter c = CalibrationHelper.getDistanceConverter(results.getCalibration(), DistanceUnit.NM);
    final double distanceThreshold = c.convertBack(clusteringSettings.getDistanceThreshold());
    final double distanceExclusion = c.convertBack(clusteringSettings.getDistanceExclusion());
    return r -> {
        final TraceManager manager = new TraceManager(r);
        // Run the tracing
        manager.setTracker(SimpleImageJTrackProgress.getInstance());
        manager.setDistanceExclusion(distanceExclusion);
        manager.traceMolecules(distanceThreshold, 1);
        return manager.getTraces();
    };
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) TextWindow(ij.text.TextWindow) RealVector(org.apache.commons.math3.linear.RealVector) ImageJPluginLoggerHelper(uk.ac.sussex.gdsc.core.ij.ImageJPluginLoggerHelper) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) Path(java.nio.file.Path) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) ArrayPeakResultStore(uk.ac.sussex.gdsc.smlm.results.ArrayPeakResultStore) ClusteringSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.ClusteringSettings) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) ConvergenceException(org.apache.commons.math3.exception.ConvergenceException) MultivariateVectorFunction(org.apache.commons.math3.analysis.MultivariateVectorFunction) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Gaussian2DPeakResultHelper(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultHelper) Logger(java.util.logging.Logger) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) CalibrationHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationHelper) List(java.util.List) LeastSquaresBuilder(org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PlugIn(ij.plugin.PlugIn) JumpDistanceAnalysis(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TextField(java.awt.TextField) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) CurveLogger(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.CurveLogger) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) SimpleImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.SimpleImageJTrackProgress) Files(java.nio.file.Files) Checkbox(java.awt.Checkbox) BufferedWriter(java.io.BufferedWriter) StdMath(uk.ac.sussex.gdsc.smlm.utils.StdMath) IOException(java.io.IOException) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) DynamicMultipleTargetTracing(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing) TooManyIterationsException(org.apache.commons.math3.exception.TooManyIterationsException) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) Consumer(java.util.function.Consumer) Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) PeakResultStoreList(uk.ac.sussex.gdsc.smlm.results.PeakResultStoreList) Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) DynamicMultipleTargetTracing(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager)

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