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