Search in sources :

Example 86 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class TraceDiffusion method showTraceDialog.

private boolean showTraceDialog(ArrayList<MemoryPeakResults> allResults) {
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(HelpUrls.getUrl("trace-diffusion"));
    if (!multiMode) {
        ResultsManager.addInput(gd, settings.inputOption, InputSource.MEMORY);
    }
    clusteringSettings = SettingsManager.readClusteringSettings(0).toBuilder();
    gd.addChoice("Mode", TRACE_MODE, clusteringSettings.getTraceDiffusionMode(), new OptionListener<Integer>() {

        @Override
        public boolean collectOptions(Integer value) {
            clusteringSettings.setTraceDiffusionMode(value);
            return collectOptions(false);
        }

        @Override
        public boolean collectOptions() {
            return collectOptions(true);
        }

        private boolean collectOptions(boolean silent) {
            final ExtendedGenericDialog egd = new ExtendedGenericDialog("Trace diffusion options", null);
            // Only 2 modes
            if (clusteringSettings.getTraceDiffusionMode() == 1) {
                // Dynamic Multiple Target Tracing
                final TextField tfD = egd.addAndGetNumericField("Diffusion_coefficient", clusteringSettings.getDiffusionCoefficentMaximum(), 3, 6, "um^2/s");
                final TextField tfW = egd.addAndGetNumericField("Temporal_window", clusteringSettings.getTemporalWindow(), 0, 6, "frames");
                final TextField tfLdw = egd.addAndGetNumericField("Local_diffusion_weight", clusteringSettings.getLocalDiffusionWeight(), 2);
                final TextField tfOiw = egd.addAndGetNumericField("On_intensity_weight", clusteringSettings.getOnIntensityWeight(), 2);
                final TextField tfDdf = egd.addAndGetNumericField("Disappearance_decay_factor", clusteringSettings.getDisappearanceDecayFactor(), 0, 6, "frames");
                final TextField tfDt = egd.addAndGetNumericField("Disappearance_threshold", clusteringSettings.getDisappearanceThreshold(), 0, 6, "frames");
                final Checkbox cbDld = egd.addAndGetCheckbox("Disable_local_diffusion_model", clusteringSettings.getDisableLocalDiffusionModel());
                final Checkbox cbDim = egd.addAndGetCheckbox("Disable_intensity_model", clusteringSettings.getDisableIntensityModel());
                // Allow reset to defaults
                egd.addAndGetButton("Defaults", e -> {
                    final DmttConfiguration config = DmttConfiguration.newBuilder(1).build();
                    tfD.setText(String.valueOf(clusteringSettings.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());
                });
            } else {
                // Nearest Neighbour
                egd.addNumericField("Distance_Threshold (nm)", clusteringSettings.getDistanceThreshold(), 0);
                egd.addNumericField("Distance_Exclusion (nm)", clusteringSettings.getDistanceExclusion(), 0);
            }
            egd.setSilent(silent);
            egd.showDialog(true, gd);
            if (egd.wasCanceled()) {
                return false;
            }
            if (clusteringSettings.getTraceDiffusionMode() == 1) {
                // Dynamic Multiple Target Tracing
                clusteringSettings.setDiffusionCoefficentMaximum(egd.getNextNumber());
                clusteringSettings.setTemporalWindow((int) egd.getNextNumber());
                clusteringSettings.setLocalDiffusionWeight(egd.getNextNumber());
                clusteringSettings.setOnIntensityWeight(egd.getNextNumber());
                clusteringSettings.setDisappearanceDecayFactor(egd.getNextNumber());
                clusteringSettings.setDisappearanceThreshold((int) egd.getNextNumber());
                clusteringSettings.setDisableLocalDiffusionModel(egd.getNextBoolean());
                clusteringSettings.setDisableIntensityModel(egd.getNextBoolean());
            } else {
                // Nearest Neighbour
                clusteringSettings.setDistanceThreshold(egd.getNextNumber());
                clusteringSettings.setDistanceExclusion(Math.abs(egd.getNextNumber()));
            }
            return true;
        }
    });
    gd.addSlider("Min_trace_length", 2, 20, clusteringSettings.getMinimumTraceLength());
    gd.addCheckbox("Ignore_ends", clusteringSettings.getIgnoreEnds());
    gd.addCheckbox("Save_traces", clusteringSettings.getSaveTraces());
    gd.showDialog();
    if (gd.wasCanceled() || !readTraceDialog(gd)) {
        return false;
    }
    // Update the settings
    SettingsManager.writeSettings(clusteringSettings.build());
    // Load the results
    if (!multiMode) {
        final MemoryPeakResults results = ResultsManager.loadInputResults(settings.inputOption, true, null, null);
        if (MemoryPeakResults.isEmpty(results)) {
            IJ.error(TITLE, "No results could be loaded");
            IJ.showStatus("");
            return false;
        }
        if (!checkCalibration(results)) {
            return false;
        }
        allResults.add(results);
    }
    return true;
}
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) Checkbox(java.awt.Checkbox) TextField(java.awt.TextField) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 87 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class TraceExporter method exportAnaDda.

@SuppressWarnings("resource")
private void exportAnaDda(MemoryPeakResults results) {
    // anaDDA list of tracked localisations file format:
    // https://github.com/HohlbeinLab/anaDDA
    // Matlab matrix file.
    // 5 columns for n rows of localisations
    // 1. x coordinate (μm)
    // 2. y coordinate (μm)
    // 3. frame number
    // 4. track id
    // 5. frame time (s)
    // Count the number of localisations including start/end frames
    final Counter row = new Counter();
    results.forEach((PeakResultProcedure) result -> {
        row.increment(getLength(result));
    });
    // Create the matrix
    final int rows = row.getCount();
    final Matrix out = Mat5.newMatrix(rows, 5);
    // Set up column offsets
    final int col1 = rows * 1;
    final int col2 = rows * 2;
    final int col3 = rows * 3;
    final int col4 = rows * 4;
    // Frame time in seconds. This is not the frame time point converted to seconds
    // but the exposure duration of the frame.
    final double frameTime = results.getCalibrationReader().getExposureTime() / 1000;
    row.reset();
    results.forEach(DistanceUnit.UM, (XyrResultProcedure) (x, y, result) -> {
        if (result.hasEndFrame()) {
            for (int t = result.getFrame(); t <= result.getEndFrame(); t++) {
                final int index = row.getAndIncrement();
                out.setDouble(index, x);
                out.setDouble(index + col1, y);
                out.setDouble(index + col2, t);
                out.setDouble(index + col3, result.getId());
                out.setDouble(index + col4, frameTime);
            }
        } else {
            // Column major index: row + rows * col
            final int index = row.getAndIncrement();
            out.setDouble(index, x);
            out.setDouble(index + col1, y);
            out.setDouble(index + col2, result.getFrame());
            out.setDouble(index + col3, result.getId());
            out.setDouble(index + col4, frameTime);
        }
    });
    try (MatFile matFile = Mat5.newMatFile()) {
        matFile.addArray("tracks", out);
        Mat5.writeToFile(matFile, Paths.get(settings.directory, results.getName() + ".mat").toFile());
    } catch (final IOException ex) {
        handleException(ex);
    }
}
Also used : MemoryResultsList(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.MemoryResultsList) Cell(us.hebi.matlab.mat.types.Cell) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) IdFramePeakResultComparator(uk.ac.sussex.gdsc.smlm.results.sort.IdFramePeakResultComparator) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MatFile(us.hebi.matlab.mat.types.MatFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matrix(us.hebi.matlab.mat.types.Matrix) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) IOException(java.io.IOException) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Logger(java.util.logging.Logger) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) XyzrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyzrResultProcedure) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) Mat5(us.hebi.matlab.mat.format.Mat5) PlugIn(ij.plugin.PlugIn) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Matrix(us.hebi.matlab.mat.types.Matrix) MatFile(us.hebi.matlab.mat.types.MatFile) IOException(java.io.IOException)

Example 88 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class TraceExporter method showMultiDialog.

private static boolean showMultiDialog(ArrayList<MemoryPeakResults> allResults, MemoryResultsList items) {
    // Show a list box containing all the results. This should remember the last set of chosen
    // items.
    final MultiDialog md = new MultiDialog(TITLE, items);
    md.setDisplayConverter(items.getDisplayConverter());
    md.setSelected(selectedRef.get());
    md.showDialog();
    if (md.wasCancelled()) {
        return false;
    }
    final List<String> selected = md.getSelectedResults();
    if (selected.isEmpty()) {
        IJ.error(TITLE, "No results were selected");
        return false;
    }
    selectedRef.set(selected);
    for (final String name : selected) {
        final MemoryPeakResults r = MemoryPeakResults.getResults(name);
        if (r != null) {
            allResults.add(r);
        }
    }
    return !allResults.isEmpty();
}
Also used : MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog)

Example 89 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class TraceExporter method doSplit.

private MemoryPeakResults doSplit(MemoryPeakResults results) {
    final MemoryPeakResults results2 = new MemoryPeakResults(results.size());
    results2.copySettings(results);
    int nextId = results.getLast().getId();
    int id = 0;
    int idOut = 0;
    int lastT = 0;
    for (int i = 0, size = results.size(); i < size; i++) {
        final PeakResult r = results.get(i);
        if (r.getId() != id) {
            id = r.getId();
            idOut = id;
        } else if (r.getFrame() - lastT > settings.maxJump) {
            idOut = ++nextId;
        }
        final AttributePeakResult r2 = new AttributePeakResult(r);
        r2.setId(idOut);
        results2.add(r2);
        lastT = r.getEndFrame();
    }
    return results2;
}
Also used : AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult)

Example 90 with MemoryPeakResults

use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class TraceMatchCalculator 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;
    }
    // Load the results
    final MemoryPeakResults results1 = ResultsManager.loadInputResults(settings.inputOption1, false, null, null);
    final MemoryPeakResults results2 = ResultsManager.loadInputResults(settings.inputOption2, false, null, null);
    final MemoryPeakResults results3 = ResultsManager.loadInputResults(settings.inputOption3, false, null, null);
    IJ.showStatus("");
    if (results1 == null || results1.size() == 0) {
        IJ.error(TITLE, "No results 1 could be loaded");
        return;
    }
    if (results2 == null || results2.size() == 0) {
        IJ.error(TITLE, "No results 2 could be loaded");
        return;
    }
    if (results1.getDistanceUnit() != results2.getDistanceUnit()) {
        IJ.error(TITLE, "Distance unit should be the same for the results 1 & 2");
        return;
    }
    if (results3 != null && results1.getDistanceUnit() != results3.getDistanceUnit()) {
        IJ.error(TITLE, "Distance unit should be the same for the results 1 & 3");
        return;
    }
    final long start = System.nanoTime();
    compareCoordinates(results1, results2, results3, settings.distanceThreshold);
    final double seconds = (System.nanoTime() - start) / 1000000000.0;
    IJ.showStatus(String.format("%s = %ss", TITLE, MathUtils.rounded(seconds, 4)));
}
Also used : MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Aggregations

MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)138 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)61 List (java.util.List)47 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)46 IJ (ij.IJ)39 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)39 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)38 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)38 ArrayList (java.util.ArrayList)36 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 PlugIn (ij.plugin.PlugIn)34 MathUtils (uk.ac.sussex.gdsc.core.utils.MathUtils)33 Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)33 ImagePlus (ij.ImagePlus)31 Rectangle (java.awt.Rectangle)31 SimpleArrayUtils (uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils)31 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)28 TextUtils (uk.ac.sussex.gdsc.core.utils.TextUtils)28 SettingsManager (uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager)28 FrameCounter (uk.ac.sussex.gdsc.smlm.results.count.FrameCounter)28