Search in sources :

Example 1 with TraceManager

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

the class PcPalmMolecules method traceMolecules.

/**
 * Trace localisations.
 *
 * @param results The results
 * @param precisions the precisions
 * @param distance The distance threshold (nm)
 * @param time The time threshold (frames)
 * @param singles a list of the singles (not grouped into molecules)
 * @return a list of molecules
 */
private static ArrayList<Molecule> traceMolecules(MemoryPeakResults results, double[] precisions, double distance, int time, ArrayList<Molecule> singles) {
    // These plugins are not really supported so just leave them to throw an exception if
    // the data cannot be handled
    final TypeConverter<IntensityUnit> ic = results.getCalibrationReader().getIntensityConverter(IntensityUnit.PHOTON);
    final TypeConverter<DistanceUnit> dc = results.getCalibrationReader().getDistanceConverter(DistanceUnit.NM);
    // Create a new dataset with the precision
    final MemoryPeakResults results2 = new MemoryPeakResults(results.size());
    for (int i = 0, size = results.size(); i < size; i++) {
        final AttributePeakResult peak2 = new AttributePeakResult(results.get(i));
        peak2.setPrecision(precisions[i]);
        results2.add(peak2);
    }
    final TraceManager tm = new TraceManager(results2);
    final double distanceThreshold = dc.convertBack(distance);
    tm.traceMolecules(distanceThreshold, time);
    final Trace[] traces = tm.getTraces();
    final ArrayList<Molecule> molecules = new ArrayList<>(traces.length);
    for (final Trace t : traces) {
        final double p = t.getLocalisationPrecision(dc);
        final float[] centroid = t.getCentroid();
        final List<Molecule> list = t.size() == 1 ? singles : molecules;
        list.add(new Molecule(dc.convert(centroid[0]), dc.convert(centroid[1]), p, ic.convert(t.getSignal())));
    }
    log("  %d localisations traced to %d molecules (%d singles, %d traces) using d=%.2f nm," + " t=%d frames (%s s)", results.size(), molecules.size() + singles.size(), singles.size(), molecules.size(), distance, time, MathUtils.rounded(time * results.getCalibrationReader().getExposureTime() / 1000.0));
    return molecules;
}
Also used : AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TDoubleArrayList(gnu.trove.list.array.TDoubleArrayList) ArrayList(java.util.ArrayList) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Example 2 with TraceManager

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

the class NeighbourAnalysis 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;
    }
    final TraceManager manager = new TraceManager(results);
    // Run the tracing
    manager.setTracker(SimpleImageJTrackProgress.getInstance());
    final Trace[] traces = manager.findNeighbours(settings.distanceThreshold, settings.timeThreshold);
    saveTraces(traces);
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager)

Example 3 with TraceManager

use of uk.ac.sussex.gdsc.smlm.results.TraceManager 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)

Example 4 with TraceManager

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

the class DarkTimeAnalysis method analyse.

private void analyse(MemoryPeakResults results) {
    // Find min and max time frames
    results.sort();
    final int min = results.getFirstFrame();
    final int max = results.getLastFrame();
    // Trace results:
    // TODO - The search distance could have units to avoid assuming the results are in pixels
    final double d = settings.searchDistance / results.getCalibrationReader().getNmPerPixel();
    int range = max - min + 1;
    if (settings.maxDarkTime > 0) {
        range = Math.max(1, (int) Math.round(settings.maxDarkTime * 1000 / msPerFrame));
    }
    final TrackProgress tracker = SimpleImageJTrackProgress.getInstance();
    tracker.status("Analysing ...");
    tracker.log("Analysing (d=%s nm (%s px) t=%s s (%d frames)) ...", MathUtils.rounded(settings.searchDistance), MathUtils.rounded(d), MathUtils.rounded(range * msPerFrame / 1000.0), range);
    Trace[] traces;
    if (settings.method == 0) {
        final TraceManager tm = new TraceManager(results);
        tm.setTracker(tracker);
        tm.traceMolecules(d, range);
        traces = tm.getTraces();
    } else {
        final ClusteringEngine engine = new ClusteringEngine(Prefs.getThreads(), algorithms[settings.method - 1], tracker);
        final List<Cluster> clusters = engine.findClusters(TraceMolecules.convertToClusterPoints(results), d, range);
        traces = TraceMolecules.convertToTraces(results, clusters);
    }
    tracker.status("Computing histogram ...");
    // Build dark-time histogram
    final int[] times = new int[range];
    final StoredData stats = new StoredData();
    for (final Trace trace : traces) {
        if (trace.getBlinks() > 1) {
            for (final int t : trace.getOffTimes()) {
                times[t]++;
            }
            stats.add(trace.getOffTimes());
        }
    }
    plotDarkTimeHistogram(stats);
    // Cumulative histogram
    for (int i = 1; i < times.length; i++) {
        times[i] += times[i - 1];
    }
    final int total = times[times.length - 1];
    // Plot dark-time up to 100%
    double[] x = new double[range];
    double[] y = new double[range];
    int truncate = 0;
    for (int i = 0; i < x.length; i++) {
        x[i] = i * msPerFrame;
        if (times[i] == total) {
            // Final value at 100%
            y[i] = 100.0;
            truncate = i + 1;
            break;
        }
        y[i] = (100.0 * times[i]) / total;
    }
    if (truncate > 0) {
        x = Arrays.copyOf(x, truncate);
        y = Arrays.copyOf(y, truncate);
    }
    final String title = "Cumulative Dark-time";
    final Plot plot = new Plot(title, "Time (ms)", "Percentile");
    plot.addPoints(x, y, Plot.LINE);
    ImageJUtils.display(title, plot);
    // Report percentile
    for (int i = 0; i < y.length; i++) {
        if (y[i] >= settings.percentile) {
            ImageJUtils.log("Dark-time Percentile %.1f @ %s ms = %s s", settings.percentile, MathUtils.rounded(x[i]), MathUtils.rounded(x[i] / 1000));
            break;
        }
    }
    tracker.status("");
}
Also used : Plot(ij.gui.Plot) SimpleImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.SimpleImageJTrackProgress) TrackProgress(uk.ac.sussex.gdsc.core.logging.TrackProgress) Cluster(uk.ac.sussex.gdsc.core.clustering.Cluster) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) StoredData(uk.ac.sussex.gdsc.core.utils.StoredData) ClusteringEngine(uk.ac.sussex.gdsc.core.clustering.ClusteringEngine)

Example 5 with TraceManager

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

the class HysteresisFilter method setup.

@Override
public void setup(MemoryPeakResults peakResults) {
    ok = new HashSet<>();
    // Create a set of candidates and valid peaks
    final MemoryPeakResults traceResults = new MemoryPeakResults();
    // Initialise peaks to check
    final LinkedList<PeakResult> candidates = new LinkedList<>();
    peakResults.forEach((PeakResultProcedure) result -> {
        switch(getStatus(result)) {
            case OK:
                ok.add(result);
                traceResults.add(result);
                break;
            case CANDIDATE:
                candidates.add(result);
                traceResults.add(result);
                break;
            default:
                break;
        }
    });
    if (candidates.isEmpty()) {
        // No candidates for tracing so just return
        return;
    }
    double distanceThreshold;
    switch(searchDistanceMode) {
        case 1:
            distanceThreshold = searchDistance / peakResults.getNmPerPixel();
            break;
        case 0:
        default:
            distanceThreshold = getSearchDistanceUsingCandidates(peakResults, candidates);
    }
    if (distanceThreshold <= 0) {
        return;
    }
    // This must be in frames
    int myTimeThreshold;
    if (timeThresholdMode == 1) {
        // time threshold is in Seconds.
        // Default to 1 frame if not calibrated.
        myTimeThreshold = 1;
        if (peakResults.hasCalibration()) {
            // Convert time threshold in seconds to frames
            final CalibrationReader cr = peakResults.getCalibrationReader();
            final double et = cr.getExposureTime();
            if (et > 0) {
                myTimeThreshold = (int) Math.round((this.timeThreshold / et));
            }
        }
    } else {
        // frames
        myTimeThreshold = (int) this.timeThreshold;
    }
    if (myTimeThreshold <= 0) {
        return;
    }
    // Trace through candidates
    final TraceManager tm = new TraceManager(traceResults);
    tm.setTraceMode(TraceMode.LATEST_FORERUNNER);
    tm.traceMolecules(distanceThreshold, myTimeThreshold);
    final Trace[] traces = tm.getTraces();
    for (final Trace trace : traces) {
        if (trace.size() > 1) {
            // Check if the trace touches a valid point
            boolean isOk = false;
            for (int i = 0; i < trace.size(); i++) {
                if (ok.contains(trace.get(i))) {
                    isOk = true;
                    break;
                }
            }
            // Add the entire trace to the OK points
            if (isOk) {
                for (int i = 0; i < trace.size(); i++) {
                    ok.add(trace.get(i));
                }
            }
        }
    }
}
Also used : Chromosome(uk.ac.sussex.gdsc.smlm.ga.Chromosome) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) NotImplementedException(uk.ac.sussex.gdsc.core.data.NotImplementedException) Set(java.util.Set) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) Gaussian2DPeakResultHelper(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultHelper) TraceMode(uk.ac.sussex.gdsc.smlm.results.TraceManager.TraceMode) XStreamOmitField(com.thoughtworks.xstream.annotations.XStreamOmitField) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) HashSet(java.util.HashSet) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) XStreamAsAttribute(com.thoughtworks.xstream.annotations.XStreamAsAttribute) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) LinkedList(java.util.LinkedList) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) LinkedList(java.util.LinkedList) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Aggregations

TraceManager (uk.ac.sussex.gdsc.smlm.results.TraceManager)8 Trace (uk.ac.sussex.gdsc.smlm.results.Trace)7 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)4 ArrayList (java.util.ArrayList)3 Plot (ij.gui.Plot)2 List (java.util.List)2 SimpleImageJTrackProgress (uk.ac.sussex.gdsc.core.ij.SimpleImageJTrackProgress)2 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)2 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)2 Gaussian2DPeakResultCalculator (uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator)2 Gaussian2DPeakResultHelper (uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultHelper)2 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)2 XStreamAsAttribute (com.thoughtworks.xstream.annotations.XStreamAsAttribute)1 XStreamOmitField (com.thoughtworks.xstream.annotations.XStreamOmitField)1 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)1 IJ (ij.IJ)1 PlugIn (ij.plugin.PlugIn)1 TextWindow (ij.text.TextWindow)1 Checkbox (java.awt.Checkbox)1 Color (java.awt.Color)1