Search in sources :

Example 1 with DmttConfiguration

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();
    };
}
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 2 with DmttConfiguration

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));
    }
}
Also used : SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 3 with DmttConfiguration

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());
    }
}
Also used : DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 4 with DmttConfiguration

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;
}
Also used : DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) Checkbox(java.awt.Checkbox) TextField(java.awt.TextField) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 5 with DmttConfiguration

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));
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) DynamicMultipleTargetTracing(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing) List(java.util.List) ArrayList(java.util.ArrayList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ClusteringEngine(uk.ac.sussex.gdsc.core.clustering.ClusteringEngine) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint)

Aggregations

DmttConfiguration (uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration)9 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)5 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)5 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)5 Checkbox (java.awt.Checkbox)3 TextField (java.awt.TextField)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NormalizedGaussianSampler (org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)3 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)3 Test (org.junit.jupiter.api.Test)3 IJ (ij.IJ)2 Plot (ij.gui.Plot)2 PlugIn (ij.plugin.PlugIn)2 TextWindow (ij.text.TextWindow)2 Color (java.awt.Color)2 BufferedWriter (java.io.BufferedWriter)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2