Search in sources :

Example 1 with TraceManager

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

the class NeighbourAnalysis method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    if (!showDialog())
        return;
    TraceManager manager = new TraceManager(results);
    // Run the tracing
    manager.setTracker(new IJTrackProgress());
    Trace[] traces = manager.findNeighbours(distanceThreshold, timeThreshold);
    saveTraces(traces);
}
Also used : Trace(gdsc.smlm.results.Trace) IJTrackProgress(gdsc.core.ij.IJTrackProgress) TraceManager(gdsc.smlm.results.TraceManager)

Example 2 with TraceManager

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

the class HysteresisFilter method setup.

@Override
public void setup(MemoryPeakResults peakResults) {
    ok = new HashSet<PeakResult>();
    // Create a set of candidates and valid peaks
    MemoryPeakResults traceResults = new MemoryPeakResults();
    // Initialise peaks to check
    LinkedList<PeakResult> candidates = new LinkedList<PeakResult>();
    for (PeakResult result : peakResults.getResults()) {
        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;
    int myTimeThreshold;
    switch(timeThresholdMode) {
        case 1:
            if (peakResults.getCalibration() != null) {
                myTimeThreshold = (int) Math.round((this.timeThreshold / peakResults.getCalibration().getExposureTime()));
            } else
                myTimeThreshold = 1;
            break;
        case 0:
        default:
            myTimeThreshold = (int) this.timeThreshold;
    }
    if (myTimeThreshold <= 0)
        return;
    // Trace through candidates
    TraceManager tm = new TraceManager(traceResults);
    tm.setTraceMode(TraceMode.LATEST_FORERUNNER);
    tm.traceMolecules(distanceThreshold, myTimeThreshold);
    Trace[] traces = tm.getTraces();
    for (Trace trace : traces) {
        if (trace.size() > 1) {
            // Check if the trace touches a valid point
            boolean isOk = false;
            for (PeakResult result : trace.getPoints()) {
                if (ok.contains(result)) {
                    isOk = true;
                    break;
                }
            }
            // Add the entire trace to the OK points
            if (isOk) {
                for (PeakResult result : trace.getPoints()) {
                    ok.add(result);
                }
            }
        }
    }
}
Also used : Trace(gdsc.smlm.results.Trace) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) TraceManager(gdsc.smlm.results.TraceManager) PeakResult(gdsc.smlm.results.PeakResult) LinkedList(java.util.LinkedList)

Example 3 with TraceManager

use of 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();
    int min = results.getHead().getFrame();
    int max = results.getTail().getEndFrame();
    // Trace results
    double d = searchDistance / results.getCalibration().getNmPerPixel();
    int range = max - min + 1;
    if (maxDarkTime > 0)
        range = FastMath.max(1, (int) Math.round(maxDarkTime * 1000 / msPerFrame));
    IJTrackProgress tracker = new IJTrackProgress();
    tracker.status("Analysing ...");
    tracker.log("Analysing (d=%s nm (%s px) t=%s s (%d frames)) ...", Utils.rounded(searchDistance), Utils.rounded(d), Utils.rounded(range * msPerFrame / 1000.0), range);
    Trace[] traces;
    if (method == 0) {
        TraceManager tm = new TraceManager(results);
        tm.setTracker(tracker);
        tm.traceMolecules(d, range);
        traces = tm.getTraces();
    } else {
        ClusteringEngine engine = new ClusteringEngine(Prefs.getThreads(), algorithms[method - 1], tracker);
        List<PeakResult> peakResults = results.getResults();
        ArrayList<Cluster> clusters = engine.findClusters(TraceMolecules.convertToClusterPoints(peakResults), d, range);
        traces = TraceMolecules.convertToTraces(peakResults, clusters);
    }
    tracker.status("Computing histogram ...");
    // Build dark-time histogram
    int[] times = new int[range];
    StoredData stats = new StoredData();
    for (Trace trace : traces) {
        if (trace.getNBlinks() > 1) {
            for (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];
    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;
        y[i] = (100.0 * times[i]) / total;
        if (// 100%
        times[i] == total) {
            truncate = i + 1;
            break;
        }
    }
    if (truncate > 0) {
        x = Arrays.copyOf(x, truncate);
        y = Arrays.copyOf(y, truncate);
    }
    String title = "Cumulative Dark-time";
    Plot2 plot = new Plot2(title, "Time (ms)", "Percentile", x, y);
    Utils.display(title, plot);
    // Report percentile
    for (int i = 0; i < y.length; i++) {
        if (y[i] >= percentile) {
            Utils.log("Dark-time Percentile %.1f @ %s ms = %s s", percentile, Utils.rounded(x[i]), Utils.rounded(x[i] / 1000));
            break;
        }
    }
    tracker.status("");
}
Also used : IJTrackProgress(gdsc.core.ij.IJTrackProgress) Cluster(gdsc.core.clustering.Cluster) Plot2(ij.gui.Plot2) TraceManager(gdsc.smlm.results.TraceManager) PeakResult(gdsc.smlm.results.PeakResult) Trace(gdsc.smlm.results.Trace) StoredData(gdsc.core.utils.StoredData) ClusteringEngine(gdsc.core.clustering.ClusteringEngine)

Example 4 with TraceManager

use of gdsc.smlm.results.TraceManager 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.getCalibration().getExposureTime() / 1000;
    final double nmPerPixel = results.getCalibration().getNmPerPixel();
    ArrayList<Trace> allTraces = new ArrayList<Trace>();
    additionalDatasets = -1;
    for (MemoryPeakResults r : allResults) {
        additionalDatasets++;
        TraceManager manager = new TraceManager(r);
        // Run the tracing
        manager.setTracker(new IJTrackProgress());
        manager.setDistanceExclusion(settings.distanceExclusion / nmPerPixel);
        manager.traceMolecules(settings.distanceThreshold / nmPerPixel, 1);
        Trace[] traces = manager.getTraces();
        traces = filterTraces(r.getName(), traces, settings.minimumTraceLength, settings.ignoreEnds);
        allTraces.addAll(Arrays.asList(traces));
        //--- Save results ---
        if (traces.length > 0) {
            // Save the traces to memory
            TraceMolecules.saveResults(r, traces, "Tracks");
            if (settings.saveTraces) {
                // 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);
                String newFilename = TraceMolecules.saveTraces(r, traces, createSettingsComment(), tracesFilename, additionalDatasets);
                // Only keep the main filename in memory
                if (additionalDatasets == 0)
                    tracesFilename = newFilename;
            }
        }
    }
    Trace[] all = allTraces.toArray(new Trace[allTraces.size()]);
    if (additionalDatasets > 0) {
        Utils.log("Multiple inputs provide %d traces", allTraces.size());
        MemoryPeakResults tracedResults = TraceManager.toPeakResults(all, results.getCalibration(), true);
        tracedResults.copySettings(results);
        tracedResults.setName(createCombinedName() + " Tracks");
        MemoryPeakResults.addResults(tracedResults);
    }
    return all;
}
Also used : Trace(gdsc.smlm.results.Trace) ArrayList(java.util.ArrayList) IJTrackProgress(gdsc.core.ij.IJTrackProgress) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) TraceManager(gdsc.smlm.results.TraceManager)

Example 5 with TraceManager

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

the class BlinkEstimator method calculateCounts.

/**
	 * Calculate the counts of molecules using different dark times. The distance threshold for molecule tracing will be
	 * absolute or relative. If relative it is set using the average precision multiplied by the search distance.
	 * <p>
	 * Note that index 0 corresponds to a t-threshold of 1 in the tracing algorithm, i.e. adjacent frames in the
	 * sequence. This is equivalent to a dark time of (up to) the frame acquisition rate, i.e. the molecule is not
	 * allowed to blink.
	 * 
	 * @param results
	 * @param maxDarkTime
	 * @param searchDistance
	 * @param relativeDistance
	 * @param verbose
	 *            Output log messages
	 * @return the counts of molecules
	 */
public double[] calculateCounts(MemoryPeakResults results, int maxDarkTime, double searchDistance, boolean relativeDistance, boolean verbose) {
    double distanceThreshold;
    if (relativeDistance) {
        double averagePrecision = calculateAveragePrecision(results, verbose);
        distanceThreshold = averagePrecision * searchDistance / results.getNmPerPixel();
        if (verbose)
            Utils.log("Average precision = %f, Distance threshold = %f px", averagePrecision, distanceThreshold);
    } else {
        distanceThreshold = searchDistance;
        Utils.log("Distance threshold = %f px", distanceThreshold);
    }
    double[] Ntd = new double[maxDarkTime + 1];
    TraceManager tm = new TraceManager(results);
    IJ.showStatus("Computing counts ...");
    for (int td = 0; td <= maxDarkTime; td++) {
        IJ.showProgress(td, maxDarkTime);
        Ntd[td] = tm.traceMolecules(distanceThreshold, td + 1);
    }
    IJ.showProgress(1);
    IJ.showStatus("");
    return Ntd;
}
Also used : TraceManager(gdsc.smlm.results.TraceManager)

Aggregations

TraceManager (gdsc.smlm.results.TraceManager)8 Trace (gdsc.smlm.results.Trace)7 IJTrackProgress (gdsc.core.ij.IJTrackProgress)4 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)3 PeakResult (gdsc.smlm.results.PeakResult)3 Cluster (gdsc.core.clustering.Cluster)2 ClusteringEngine (gdsc.core.clustering.ClusteringEngine)2 ArrayList (java.util.ArrayList)2 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 StoredData (gdsc.core.utils.StoredData)1 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)1 Plot2 (ij.gui.Plot2)1 LinkedList (java.util.LinkedList)1