Search in sources :

Example 1 with Trace

use of uk.ac.sussex.gdsc.smlm.results.Trace 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;
}
Also used : AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TDoubleArrayList(gnu.trove.list.array.TDoubleArrayList) ArrayList(java.util.ArrayList) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Example 2 with Trace

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

the class TcPalmAnalysis method analyseRois.

/**
 * Analyses all the ROIs in the ROI manager.
 *
 * @param event the event
 */
private void analyseRois(ActionEvent event) {
    final RoiManager manager = RoiManager.getInstance();
    if (manager == null) {
        IJ.error(TITLE, "ROI manager is not open");
        return;
    }
    final LocalList<Roi> rois = Arrays.stream(manager.getRoisAsArray()).filter(Roi::isArea).collect(LocalCollectors.toLocalList());
    if (rois.isEmpty()) {
        IJ.error(TITLE, "No area ROIs");
        return;
    }
    // Check for overlaps.
    if (!settings.getDisableOverlapCheck() && anyOverlap(rois)) {
        final GenericDialog gd = new GenericDialog(TITLE);
        gd.addMessage(TextUtils.wrap("WARNING - Bounding rectangles of ROIs overlap. You can verify " + "the ROIs on the image using the ROI manager 'Show all' function.", 80));
        gd.setOKLabel("Continue");
        gd.showDialog();
        if (gd.wasCanceled()) {
            return;
        }
    }
    // For each ROI:
    // - Extract the current groups
    // - Build the cumulative count plot
    // - Identify the bursts
    // - Extract ClusterData for each burst
    final TcPalmAnalysisSettings settings = this.settings.build();
    final LocalList<ClusterData> allClusters = rois.parallelStream().map(roi -> {
        final Rectangle2D scaledBounds = createScaledBounds(roi);
        final BiPredicate<ClusterData, Rectangle2D> filter = createSelectionFilter(roi, settings);
        // Filter all cluster groups
        final LocalList<ClusterData> clusters = new LocalList<>();
        clusterData.forEach(c -> {
            if (filter.test(c, scaledBounds)) {
                clusters.add(c);
            }
        });
        // Extract activation bursts
        final CumulativeCountData countData = createCumulativeCountData(clusters, false);
        final LocalList<int[]> bursts = runBurstAnalysis(settings, countData);
        final LocalList<LocalList<PeakResult>> burstLocalisations = createBurstLocalisations(clusters, bursts);
        clusters.clear();
        burstLocalisations.forEach(list -> {
            final ClusterData d = new ClusterData(clusters.size() + 1, list);
            // Save this for analysis
            d.sourceRoi = roi;
            d.getArea();
            clusters.add(d);
        });
        return clusters;
    }).collect(LocalList::new, LocalList::addAll, LocalList::addAll);
    // Reorder
    final Counter count = new Counter();
    allClusters.forEach(c -> c.id = count.incrementAndGet());
    // Display in a table
    final ClusterDataTableModelFrame frame = createAllClustersTable();
    frame.getModel().setData(allClusters, dataCalibration);
    // Allow the results to be repeated
    frame.selectedAction = clusters -> {
        // Expecting a single cluster. No clusters occurs when the table (and selection) is cleared.
        if (clusters.size() == 1) {
            final ClusterData c = clusters.get(0);
            // Push the correct ROI and settings to the analysis action.
            // We do not directly update the ROI or dialog settings as
            // these trigger events that are processed to add work with a delay.
            // Updating them at the end should generate events that are
            // ignored when finally executed as the ROI/settings should be the same.
            addWork(0, c.sourceRoi, settings, () -> {
                // When analysis has finished update the settings and image ROI.
                image.getImagePlus().setRoi(c.sourceRoi);
                darkTimeToleranceTextField.setText(Integer.toString(settings.getDarkTimeTolerance()));
                minClusterSizeTextField.setText(Integer.toString(settings.getMinClusterSize()));
                // When analysis has finished the cluster should be selected in the
                // current clusters table.
                final ClusterDataTableModelFrame currentClusters = currentClustersTable.get();
                if (currentClusters != null) {
                    currentClusters.select(c);
                }
            });
        }
    };
    // Show histogram of cluster size/duration
    reportAnalysis(settings, allClusters, dataCalibration);
    // Save clusters to memory
    final Trace[] traces = allClusters.stream().map(c -> {
        final Trace t = new Trace();
        t.setId(c.id);
        c.results.forEach(t::add);
        return t;
    }).toArray(Trace[]::new);
    TraceMolecules.saveResults(results, traces, "TC PALM");
    IJ.showStatus(TITLE + ": " + TextUtils.pleural(allClusters.size(), "cluster"));
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) IntUnaryOperator(java.util.function.IntUnaryOperator) Rectangle2D(java.awt.geom.Rectangle2D) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) IdFramePeakResultComparator(uk.ac.sussex.gdsc.smlm.results.sort.IdFramePeakResultComparator) UnaryOperator(java.util.function.UnaryOperator) Hull(uk.ac.sussex.gdsc.core.math.hull.Hull) TableCellRenderer(javax.swing.table.TableCellRenderer) ResultsSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsSettings) FloatUnaryOperator(uk.ac.sussex.gdsc.core.utils.function.FloatUnaryOperator) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) RoiManager(ij.plugin.frame.RoiManager) RowSorter(javax.swing.RowSorter) SoftLock(uk.ac.sussex.gdsc.core.utils.SoftLock) JFrame(javax.swing.JFrame) LutHelper(uk.ac.sussex.gdsc.core.ij.process.LutHelper) KeyStroke(javax.swing.KeyStroke) TableModelEvent(javax.swing.event.TableModelEvent) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) KeyEvent(java.awt.event.KeyEvent) WindowAdapter(java.awt.event.WindowAdapter) ConcurrencyUtils(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrencyUtils) Component(java.awt.Component) Hull2d(uk.ac.sussex.gdsc.core.math.hull.Hull2d) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) Executors(java.util.concurrent.Executors) CalibrationHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationHelper) ImagePlus(ij.ImagePlus) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) ToDoubleFunction(java.util.function.ToDoubleFunction) TcPalmAnalysisSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.TcPalmAnalysisSettings) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PlugIn(ij.plugin.PlugIn) ListSelectionModel(javax.swing.ListSelectionModel) ActionListener(java.awt.event.ActionListener) PolygonRoi(ij.gui.PolygonRoi) StoredData(uk.ac.sussex.gdsc.core.utils.StoredData) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) WindowManager(ij.WindowManager) ConvexHull2d(uk.ac.sussex.gdsc.core.math.hull.ConvexHull2d) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) PointRoi(ij.gui.PointRoi) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) GenericDialog(ij.gui.GenericDialog) AbstractTableModel(javax.swing.table.AbstractTableModel) SortUtils(uk.ac.sussex.gdsc.core.utils.SortUtils) RounderUtils(uk.ac.sussex.gdsc.core.data.utils.RounderUtils) Overlay(ij.gui.Overlay) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) Window(java.awt.Window) IOException(java.io.IOException) JScrollPane(javax.swing.JScrollPane) RoiListener(ij.gui.RoiListener) Paths(java.nio.file.Paths) ConcurrentMonoStack(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrentMonoStack) ListSelectionListener(javax.swing.event.ListSelectionListener) TIntArrayList(gnu.trove.list.array.TIntArrayList) IdentityTypeConverter(uk.ac.sussex.gdsc.core.data.utils.IdentityTypeConverter) Point(java.awt.Point) CoordinatePredicateUtils(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicateUtils) ResultsImageSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageSettings) ImageJPluginLoggerHelper(uk.ac.sussex.gdsc.core.ij.ImageJPluginLoggerHelper) LocalCollectors(uk.ac.sussex.gdsc.core.utils.LocalCollectors) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ScreenDimensionHelper(uk.ac.sussex.gdsc.core.ij.gui.ScreenDimensionHelper) PlotWindow(ij.gui.PlotWindow) ListSelectionEvent(javax.swing.event.ListSelectionEvent) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) JMenuBar(javax.swing.JMenuBar) CoordinatePredicate(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicate) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) JMenu(javax.swing.JMenu) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) WindowEvent(java.awt.event.WindowEvent) DoubleStream(java.util.stream.DoubleStream) Objects(java.util.Objects) List(java.util.List) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) JTable(javax.swing.JTable) LUT(ij.process.LUT) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) Roi(ij.gui.Roi) Rectangle(java.awt.Rectangle) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) AtomicReference(java.util.concurrent.atomic.AtomicReference) SwingConstants(javax.swing.SwingConstants) TextField(java.awt.TextField) Level(java.util.logging.Level) AWTEvent(java.awt.AWTEvent) BiPredicate(java.util.function.BiPredicate) SwingUtilities(javax.swing.SwingUtilities) JMenuItem(javax.swing.JMenuItem) ImagePeakResultsFactory(uk.ac.sussex.gdsc.smlm.ij.results.ImagePeakResultsFactory) UnitHelper(uk.ac.sussex.gdsc.smlm.data.config.UnitHelper) ExecutorService(java.util.concurrent.ExecutorService) LutColour(uk.ac.sussex.gdsc.core.ij.process.LutHelper.LutColour) ActionEvent(java.awt.event.ActionEvent) Rounder(uk.ac.sussex.gdsc.core.data.utils.Rounder) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) Consumer(java.util.function.Consumer) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) TableColumnAdjuster(uk.ac.sussex.gdsc.smlm.ij.gui.TableColumnAdjuster) IJ(ij.IJ) DoublePredicate(java.util.function.DoublePredicate) Collections(java.util.Collections) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Rectangle2D(java.awt.geom.Rectangle2D) PolygonRoi(ij.gui.PolygonRoi) PointRoi(ij.gui.PointRoi) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) Roi(ij.gui.Roi) RoiManager(ij.plugin.frame.RoiManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) GenericDialog(ij.gui.GenericDialog) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) TcPalmAnalysisSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.TcPalmAnalysisSettings) BiPredicate(java.util.function.BiPredicate)

Example 3 with Trace

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

the class PulseActivationAnalysis method createActivations.

/**
 * Creates the activations. This splits the input traces into continuous chains of localisations.
 * Each chain is an activation. A new activation is created if there are more than the configured
 * number of dark frames since the last localisation. The start frame for the activation defines
 * the channel the activation is assigned to (this may be channel 0 if the start frame is not in a
 * pulse start frame).
 */
@SuppressWarnings("null")
private void createActivations() {
    final LocalList<Activation> activations = new LocalList<>(traces.length);
    // Activations are only counted if there are at least
    // n frames between localisations.
    final int n = settings.darkFramesForNewActivation + 1;
    for (final Trace trace : traces) {
        // Time-order
        trace.sort();
        final PeakResultStoreList points = trace.getPoints();
        // Define the frame for a new activation
        int nextActivationStartFrame = Integer.MIN_VALUE;
        Trace current = null;
        int channel = 0;
        for (int j = 0; j < points.size(); j++) {
            final PeakResult p = points.get(j);
            // Check if this is an activation
            if (p.getFrame() >= nextActivationStartFrame) {
                if (current != null) {
                    // Store the last
                    activations.add(new Activation(current, channel));
                }
                // Create a new activation
                current = new Trace(p);
                channel = getChannel(p);
            } else {
                // This is the same chain of localisations
                current.add(p);
            }
            nextActivationStartFrame = p.getEndFrame() + n;
        }
        if (current != null) {
            activations.add(new Activation(current, channel));
        }
    }
    save(activations);
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) PeakResultStoreList(uk.ac.sussex.gdsc.smlm.results.PeakResultStoreList) IdPeakResult(uk.ac.sussex.gdsc.smlm.results.IdPeakResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 4 with Trace

use of uk.ac.sussex.gdsc.smlm.results.Trace 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);
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager)

Example 5 with Trace

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

the class NeighbourAnalysis method saveTraces.

private void saveTraces(Trace[] traces) {
    final String[] path = ImageJUtils.decodePath(settings.filename);
    final OpenDialog chooser = new OpenDialog("Traces_File", path[0], path[1]);
    if (chooser.getFileName() != null) {
        settings.filename = chooser.getDirectory() + chooser.getFileName();
        // Remove extension and replace with .xls
        final int index = settings.filename.lastIndexOf('.');
        if (index > 0) {
            settings.filename = settings.filename.substring(0, index);
        }
        settings.filename += ".xls";
        final boolean showDeviations = results.hasDeviations();
        final TextFilePeakResults traceResults = new TextFilePeakResults(settings.filename, showDeviations);
        traceResults.copySettings(results);
        traceResults.begin();
        if (!traceResults.isActive()) {
            IJ.error(TITLE, "Failed to write to file: " + settings.filename);
            return;
        }
        traceResults.addComment(createSettingsComment());
        for (final Trace trace : traces) {
            // addTrace(...) does a sort on the results
            traceResults.addCluster(trace);
        }
        traceResults.end();
    }
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) OpenDialog(ij.io.OpenDialog)

Aggregations

Trace (uk.ac.sussex.gdsc.smlm.results.Trace)25 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)11 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)11 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)8 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)8 TraceManager (uk.ac.sussex.gdsc.smlm.results.TraceManager)8 ArrayList (java.util.ArrayList)7 Plot (ij.gui.Plot)6 Color (java.awt.Color)5 WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)5 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)5 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)5 List (java.util.List)4 ClusterPoint (uk.ac.sussex.gdsc.core.clustering.ClusterPoint)4 HistogramPlotBuilder (uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder)4 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 IJ (ij.IJ)3 PlugIn (ij.plugin.PlugIn)3 TextField (java.awt.TextField)3 BufferedWriter (java.io.BufferedWriter)3