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);
}
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);
}
}
}
}
}
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("");
}
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;
}
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;
}
Aggregations