use of uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration 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.DynamicMultipleTargetTracing.DmttConfiguration in project GDSC-SMLM by aherbert.
the class DynamicMultipleTargetTracingTest method testTraceMoleculesDisableIntensityModel.
/**
* Test trace molecules using 2 molecules. One is fixed and the other moves past it. The tracing
* should assign the fixed molecule correctly as it has a low local diffusion rate.
*/
@Test
void testTraceMoleculesDisableIntensityModel() {
final UniformRandomProvider rng = RngUtils.create(125631236L);
final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
// localisation precision (in pixels)
final double s = 0.1;
final SharedStateContinuousSampler intensity = SamplerUtils.createGaussianSampler(rng, 1000, 100);
final MemoryPeakResults results = new MemoryPeakResults(100);
final CalibrationWriter writer = results.getCalibrationWriterSafe();
// 0.1 um pixels, 1 second exposure time
writer.setDistanceUnit(DistanceUnit.PIXEL);
writer.setNmPerPixel(100);
writer.setExposureTime(1000);
results.setCalibration(writer.getCalibration());
// First molecule diffuses roughly across the field from top-left to bottom-right.
// 5 frames is the default for local stats, 15 frames for trajectory removal.
// Use 20 so we build local stats and can expire a trajectory.
final int size = 20;
final float x1 = size / 2 + 0.5f;
for (int i = 0; i < size; i++) {
results.add(new PeakResult(i, (float) (x1 + gauss.sample() * s), (float) (i + gauss.sample() * s), (float) (intensity.sample())));
}
// Second molecule is fixed in the centre with a same intensity
final int x = size / 2;
for (int i = 0; i < size; i++) {
results.add(new PeakResult(i, (float) (x + gauss.sample() * s), (float) (x + gauss.sample() * s), (float) (intensity.sample())));
}
// Add a single molecule that will not connect to anything in the second frame.
// This should create a trajectory that will expire.
results.add(new PeakResult(1, x1, size, (float) (intensity.sample())));
// Move centre to centre each jump => 0.1 um or 0.01 um^2
// MSD = 4D => D = 0.01 / 4 = 0.0025
final DmttConfiguration config = DmttConfiguration.newBuilder(0.0025).setDisableIntensityModel(true).setTemporalWindow(10).build();
final List<Trace> traces = new DynamicMultipleTargetTracing(results).traceMolecules(config);
// Should have 3 traces
Assertions.assertEquals(3, traces.size());
// Assert ids start from 1
for (int i = 0; i < traces.size(); i++) {
Assertions.assertEquals(i + 1, traces.get(i).getId());
}
// Traces should be 2 full length and 1 single peak
Assertions.assertEquals(size, traces.get(0).size());
Assertions.assertEquals(size, traces.get(1).size());
Assertions.assertEquals(1, traces.get(2).size());
// Do an analysis on the actual tracks.
// One should be based in the centre and the other should have parts close to position (i,i)
// for each frame i.
final PeakResult[] peaks = results.toArray();
// Assume traces are initially created using the input order of the results.
final Trace t1 = traces.get(0);
final Trace t2 = traces.get(1);
for (int i = 0; i < size; i++) {
Assertions.assertSame(peaks[i], t1.get(i));
Assertions.assertSame(peaks[i + size], t2.get(i));
}
}
use of uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration in project GDSC-SMLM by aherbert.
the class DynamicMultipleTargetTracingTest method checkBuilder.
@SeededTest
void checkBuilder(RandomSeed seed) {
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
final int temporalWindow = 2 + rng.nextInt(10);
final double localDiffusionWeight = rng.nextDouble();
final double diffusionCoefficientMaximum = 1 + rng.nextDouble();
final double onIntensityWeight = rng.nextDouble();
final double disappearanceDecayFactor = 1 + rng.nextDouble();
final int disappearanceThreshold = 1 + rng.nextInt(10);
final boolean disableIntensityModel = rng.nextBoolean();
final boolean disableLocalDiffusionModel = rng.nextBoolean();
final DmttConfiguration.Builder b = DmttConfiguration.newBuilder(45);
final DmttConfiguration c1 = b.setTemporalWindow(temporalWindow).setLocalDiffusionWeight(localDiffusionWeight).setDiffusionCoefficientMaximum(diffusionCoefficientMaximum).setOnIntensityWeight(onIntensityWeight).setDisappearanceDecayFactor(disappearanceDecayFactor).setDisappearanceThreshold(disappearanceThreshold).setDisableIntensityModel(disableIntensityModel).setDisableLocalDiffusionModel(disableLocalDiffusionModel).build();
// Check round-trip
for (final DmttConfiguration config : new DmttConfiguration[] { c1, c1.toBuilder().build() }) {
Assertions.assertEquals(diffusionCoefficientMaximum, config.getDiffusionCoefficientMaximum());
Assertions.assertEquals(temporalWindow, config.getTemporalWindow());
Assertions.assertEquals(localDiffusionWeight, config.getLocalDiffusionWeight());
Assertions.assertEquals(onIntensityWeight, config.getOnIntensityWeight());
Assertions.assertEquals(disappearanceDecayFactor, config.getDisappearanceDecayFactor());
Assertions.assertEquals(disappearanceThreshold, config.getDisappearanceThreshold());
Assertions.assertEquals(disableIntensityModel, config.isDisableIntensityModel());
Assertions.assertEquals(disableLocalDiffusionModel, config.isDisableLocalDiffusionModel());
}
}
use of uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration in project GDSC-SMLM by aherbert.
the class TraceMolecules method showDynamicTraceDialog.
private boolean showDynamicTraceDialog() {
pluginTitle = outputName + " Molecules";
final ExtendedGenericDialog gd = new ExtendedGenericDialog(pluginTitle);
gd.addHelp(HelpUrls.getUrl("dynamic-trace-molecules"));
readSettings();
ResultsManager.addInput(gd, pluginSettings.inputOption, InputSource.MEMORY);
// Dynamic Multiple Target Tracing
final TextField tfD = gd.addAndGetNumericField("Diffusion_coefficient", settings.getDiffusionCoefficentMaximum(), 3, 6, "um^2/s");
final TextField tfW = gd.addAndGetNumericField("Temporal_window", settings.getTemporalWindow(), 0, 6, "frames");
final TextField tfLdw = gd.addAndGetNumericField("Local_diffusion_weight", settings.getLocalDiffusionWeight(), 2);
final TextField tfOiw = gd.addAndGetNumericField("On_intensity_weight", settings.getOnIntensityWeight(), 2);
final TextField tfDdf = gd.addAndGetNumericField("Disappearance_decay_factor", settings.getDisappearanceDecayFactor(), 0, 6, "frames");
final TextField tfDt = gd.addAndGetNumericField("Disappearance_threshold", settings.getDisappearanceThreshold(), 0, 6, "frames");
final Checkbox cbDld = gd.addAndGetCheckbox("Disable_local_diffusion_model", settings.getDisableLocalDiffusionModel());
final Checkbox cbDim = gd.addAndGetCheckbox("Disable_intensity_model", settings.getDisableIntensityModel());
// Allow reset to defaults
gd.addAndGetButton("Defaults", e -> {
final DmttConfiguration config = DmttConfiguration.newBuilder(1).build();
tfD.setText(String.valueOf(settings.getDiffusionCoefficentMaximum()));
tfW.setText(String.valueOf(config.getTemporalWindow()));
tfLdw.setText(String.valueOf(config.getLocalDiffusionWeight()));
tfOiw.setText(String.valueOf(config.getOnIntensityWeight()));
tfDdf.setText(String.valueOf(config.getDisappearanceDecayFactor()));
tfDt.setText(String.valueOf(config.getDisappearanceThreshold()));
cbDld.setState(config.isDisableLocalDiffusionModel());
cbDim.setState(config.isDisableIntensityModel());
});
gd.addCheckbox("Save_traces", settings.getSaveTraces());
gd.addCheckbox("Show_histograms", settings.getShowHistograms());
gd.addCheckbox("Save_trace_data", settings.getSaveTraceData());
gd.showDialog();
if (gd.wasCanceled() || !readDynamicTraceDialog(gd)) {
return false;
}
// Load the results
results = ResultsManager.loadInputResults(pluginSettings.inputOption, true, null, null);
if (MemoryPeakResults.isEmpty(results)) {
IJ.error(pluginTitle, "No results could be loaded");
IJ.showStatus("");
return false;
}
// Store exposure time in seconds
exposureTime = results.getCalibrationReader().getExposureTime() / 1000;
return true;
}
use of uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration in project GDSC-SMLM by aherbert.
the class TraceMolecules method run.
@Override
public void run(String arg) {
SmlmUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(pluginTitle, "No localisations in memory");
return;
}
altKeyDown = ImageJUtils.isExtraOptions();
Trace[] traces = null;
int totalFiltered = 0;
if ("dynamic".equals(arg)) {
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Dynamic Mutliple Target Tracing
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
outputName = "Dynamic Trace";
if (!showDynamicTraceDialog()) {
return;
}
final DmttConfiguration config = createDmttConfiguration();
traces = new DynamicMultipleTargetTracing(results).traceMolecules(config).toArray(new Trace[0]);
} else if ("cluster".equals(arg)) {
// -=-=-=-=-=
// Clustering
// -=-=-=-=-=
outputName = "Cluster";
if (!showClusterDialog()) {
return;
}
final ClusteringEngine engine = new ClusteringEngine(Prefs.getThreads(), getClusteringAlgorithm(settings.getClusteringAlgorithm()), SimpleImageJTrackProgress.getInstance());
if (settings.getSplitPulses()) {
engine.setPulseInterval(settings.getPulseInterval());
limitTimeThreshold(settings.getPulseInterval());
}
final List<Cluster> clusters = engine.findClusters(convertToClusterPoints(), getDistance(settings.getDistanceThreshold(), results.getCalibration()), timeThresholdInFrames());
if (clusters == null) {
ImageJUtils.log("Aborted");
return;
}
traces = convertToTraces(clusters);
} else {
// -=-=-=-
// Tracing
// -=-=-=-
outputName = "Trace";
if (!showDialog()) {
return;
}
final TraceManager manager = new TraceManager(results);
manager.setTraceMode(getTraceMode(settings.getTraceMode()));
manager.setActivationFrameInterval(settings.getPulseInterval());
manager.setActivationFrameWindow(settings.getPulseWindow());
manager.setDistanceExclusion(getDistance(settings.getDistanceExclusion(), results.getCalibration()));
if (settings.getOptimise()) {
// Optimise before configuring for a pulse interval
runOptimiser(manager);
}
if (settings.getSplitPulses()) {
manager.setPulseInterval(settings.getPulseInterval());
limitTimeThreshold(settings.getPulseInterval());
}
manager.setTracker(SimpleImageJTrackProgress.getInstance());
manager.traceMolecules(getDistance(settings.getDistanceThreshold(), results.getCalibration()), timeThresholdInFrames());
traces = manager.getTraces();
totalFiltered = manager.getTotalFiltered();
}
// --=-=-=-=-=-
// Results processing
// --=-=-=-=-=-
outputName += (outputName.endsWith("e") ? "" : "e") + "d";
saveResults(results, traces, outputName);
// Save singles + single localisations in a trace
saveCentroidResults(results, getSingles(traces), outputName + " Singles");
final Trace[] multiTraces = getTraces(traces);
saveResults(results, multiTraces, outputName + " Multi");
// Save centroids
outputName += " Centroids";
final MemoryPeakResults tracedResults = saveCentroidResults(results, traces, outputName);
// Save traces separately
saveCentroidResults(results, multiTraces, outputName + " Multi");
// 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.
sortByTime(traces);
if (settings.getSaveTraces()) {
saveTraces(traces);
}
summarise(createSummaryTable(), traces, totalFiltered, settings.getDistanceThreshold(), timeThresholdInSeconds());
IJ.showStatus(String.format("%d localisations => %d traces (%d filtered)", results.size(), tracedResults.size(), totalFiltered));
}
Aggregations