Search in sources :

Example 16 with MemoryPeakResults

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

the class TraceExporter method export.

private void export(MemoryPeakResults results) {
    // Copy to allow manipulation
    results = results.copy();
    // Strip results with no trace Id
    results.removeIf(result -> result.getId() <= 0);
    // Sort by ID then time
    results.sort(IdFramePeakResultComparator.INSTANCE);
    // Split traces with big jumps and long tracks into smaller tracks
    results = splitTraces(results);
    results = splitLongTraces(results);
    // Count each ID and remove short traces
    int id = 0;
    int count = 0;
    int tracks = 0;
    int maxLength = 0;
    final TIntHashSet remove = new TIntHashSet();
    for (int i = 0, size = results.size(); i < size; i++) {
        final PeakResult result = results.get(i);
        if (result.getId() != id) {
            if (count < settings.minLength) {
                remove.add(id);
            } else {
                tracks++;
                maxLength = Math.max(maxLength, count);
            }
            count = 0;
            id = result.getId();
        }
        count += getLength(result);
    }
    // Final ID
    if (count < settings.minLength) {
        remove.add(id);
    } else {
        tracks++;
        maxLength = Math.max(maxLength, count);
    }
    if (!remove.isEmpty()) {
        results.removeIf(result -> remove.contains(result.getId()));
        results.sort(IdFramePeakResultComparator.INSTANCE);
    }
    if (settings.wobble > 0) {
        // Just leave any exceptions to trickle up and kill the plugin
        final TypeConverter<DistanceUnit> c = results.getDistanceConverter(DistanceUnit.NM);
        final double w = c.convertBack(settings.wobble);
        final UniformRandomProvider rng = UniformRandomProviders.create();
        final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
        final boolean is3D = results.is3D();
        results.forEach((PeakResultProcedure) peakResult -> {
            peakResult.setXPosition((float) (peakResult.getXPosition() + w * gauss.sample()));
            peakResult.setYPosition((float) (peakResult.getYPosition() + w * gauss.sample()));
            if (is3D) {
                peakResult.setZPosition((float) (peakResult.getZPosition() + w * gauss.sample()));
            }
        });
    }
    if (settings.format == 2) {
        exportVbSpt(results);
    } else if (settings.format == 1) {
        exportAnaDda(results);
    } else {
        exportSpotOn(results);
    }
    ImageJUtils.log("Exported %s: %s in %s", results.getName(), TextUtils.pleural(results.size(), "localisation"), TextUtils.pleural(tracks, "track"));
    if (settings.showTraceLengths) {
        // We store and index (count-1)
        final int[] h = new int[maxLength];
        id = 0;
        for (int i = 0, size = results.size(); i < size; i++) {
            final PeakResult result = results.get(i);
            if (result.getId() != id) {
                h[count - 1]++;
                count = 0;
                id = result.getId();
            }
            count += getLength(result);
        }
        h[count - 1]++;
        final String title = TITLE + ": " + results.getName();
        final Plot plot = new Plot(title, "Length", "Frequency");
        plot.addPoints(SimpleArrayUtils.newArray(h.length, 1, 1.0f), SimpleArrayUtils.toFloat(h), Plot.BAR);
        plot.setLimits(SimpleArrayUtils.findIndex(h, i -> i != 0), maxLength + 1, 0, Double.NaN);
        ImageJUtils.display(title, plot);
    }
}
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) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)

Example 17 with MemoryPeakResults

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

the class TraceExporter method splitLongTraces.

private MemoryPeakResults splitLongTraces(MemoryPeakResults results) {
    if (settings.maxLength < 1) {
        // Disabled
        return results;
    }
    final MemoryPeakResults results2 = new MemoryPeakResults(results.size());
    results2.copySettings(results);
    int nextId = results.getLast().getId();
    int id = 0;
    int idOut = 0;
    // The length that has been output under the current ID
    int length = 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;
            length = 0;
        } else if (length >= settings.maxLength) {
            idOut = ++nextId;
            length = 0;
        }
        final AttributePeakResult r2 = new AttributePeakResult(r);
        r2.setId(idOut);
        results2.add(r2);
        length += getLength(r);
    }
    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 18 with MemoryPeakResults

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

the class TraceExporter method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    final MemoryResultsList items = new MemoryResultsList(MemoryPeakResults::hasId);
    if (items.isEmpty()) {
        IJ.error(TITLE, "No traced localisations in memory");
        return;
    }
    // Get input options
    if (!showDialog()) {
        return;
    }
    final ArrayList<MemoryPeakResults> allResults = new ArrayList<>();
    // Pick multiple input datasets together using a list box.
    if (!showMultiDialog(allResults, items)) {
        return;
    }
    for (final MemoryPeakResults results : allResults) {
        export(results);
    }
}
Also used : ArrayList(java.util.ArrayList) MemoryResultsList(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.MemoryResultsList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 19 with MemoryPeakResults

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

the class TraceMatchCalculator method extractPulses.

@Nullable
private static Pulse[] extractPulses(MemoryPeakResults results) {
    if (results == null) {
        return null;
    }
    final Pulse[] pulses = new Pulse[results.size()];
    final Counter i = new Counter();
    results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> pulses[i.getAndIncrement()] = new Pulse(x, y, result.getFrame(), result.getEndFrame()));
    return pulses;
}
Also used : CoordinateProvider(uk.ac.sussex.gdsc.smlm.utils.CoordinateProvider) TextWindow(ij.text.TextWindow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WindowManager(ij.WindowManager) Point(java.awt.Point) HashMap(java.util.HashMap) ImageRoiPainter(uk.ac.sussex.gdsc.smlm.ij.utils.ImageRoiPainter) AtomicReference(java.util.concurrent.atomic.AtomicReference) MatchResult(uk.ac.sussex.gdsc.core.match.MatchResult) ArrayList(java.util.ArrayList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) LinkedList(java.util.LinkedList) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) Pulse(uk.ac.sussex.gdsc.core.match.Pulse) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Coordinate(uk.ac.sussex.gdsc.core.match.Coordinate) WindowAdapter(java.awt.event.WindowAdapter) ConcurrencyUtils(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrencyUtils) WindowEvent(java.awt.event.WindowEvent) Consumer(java.util.function.Consumer) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) PointPair(uk.ac.sussex.gdsc.core.match.PointPair) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) MatchCalculator(uk.ac.sussex.gdsc.core.match.MatchCalculator) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) PlugIn(ij.plugin.PlugIn) Collections(java.util.Collections) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Pulse(uk.ac.sussex.gdsc.core.match.Pulse) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 20 with MemoryPeakResults

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

the class TraceMatchCalculator method refresh.

private static WindowAndPainter refresh(AtomicReference<WindowAndPainter> ref, boolean pairs, TextWindow resultsWindow, MemoryPeakResults results1) {
    // Produce a pairs output
    final WindowAndPainter wap = ConcurrencyUtils.refresh(ref, // Test the window is showing
    w -> ImageJUtils.isShowing(w.textWindow), // Create
    () -> {
        final String title = TITLE + ((pairs) ? " Pairs" : " Triples");
        final String header = pairs ? createPairsHeader() : createTriplesHeader();
        final TextWindow window = new TextWindow(title, header, "", 900, 300);
        // Position relative to results window
        final Point p = resultsWindow.getLocation();
        p.y += resultsWindow.getHeight();
        window.setLocation(p);
        final CoordinateProvider coordinateProvider = line -> {
            // Extract the startT and x,y coordinates from the first pulse in the line
            final int[] index = { 0, 5, 12 };
            final String[] fields = line.split("\t");
            for (final int i : index) {
                if (i < fields.length) {
                    if (fields[i].equals("-")) {
                        continue;
                    }
                    final int startT = Integer.parseInt(fields[i]);
                    final double x = Double.parseDouble(fields[i + 2]);
                    final double y = Double.parseDouble(fields[i + 3]);
                    return new double[] { startT, x, y };
                }
            }
            return null;
        };
        final ImageRoiPainter painter = new ImageRoiPainter(window.getTextPanel(), results1.getSource().getOriginal().getName(), coordinateProvider);
        final WindowAndPainter result = new WindowAndPainter(window, painter);
        // Free memory on close
        window.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(WindowEvent event) {
                ref.compareAndSet(result, null);
                super.windowClosed(event);
            }
        });
        return result;
    });
    wap.textWindow.getTextPanel().clear();
    wap.painter.setTitle(results1.getSource().getOriginal().getName());
    return wap;
}
Also used : CoordinateProvider(uk.ac.sussex.gdsc.smlm.utils.CoordinateProvider) TextWindow(ij.text.TextWindow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WindowManager(ij.WindowManager) Point(java.awt.Point) HashMap(java.util.HashMap) ImageRoiPainter(uk.ac.sussex.gdsc.smlm.ij.utils.ImageRoiPainter) AtomicReference(java.util.concurrent.atomic.AtomicReference) MatchResult(uk.ac.sussex.gdsc.core.match.MatchResult) ArrayList(java.util.ArrayList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) LinkedList(java.util.LinkedList) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) Pulse(uk.ac.sussex.gdsc.core.match.Pulse) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Coordinate(uk.ac.sussex.gdsc.core.match.Coordinate) WindowAdapter(java.awt.event.WindowAdapter) ConcurrencyUtils(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrencyUtils) WindowEvent(java.awt.event.WindowEvent) Consumer(java.util.function.Consumer) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) PointPair(uk.ac.sussex.gdsc.core.match.PointPair) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) MatchCalculator(uk.ac.sussex.gdsc.core.match.MatchCalculator) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) PlugIn(ij.plugin.PlugIn) Collections(java.util.Collections) ImageRoiPainter(uk.ac.sussex.gdsc.smlm.ij.utils.ImageRoiPainter) TextWindow(ij.text.TextWindow) CoordinateProvider(uk.ac.sussex.gdsc.smlm.utils.CoordinateProvider) WindowEvent(java.awt.event.WindowEvent) WindowAdapter(java.awt.event.WindowAdapter) Point(java.awt.Point)

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