Search in sources :

Example 81 with MemoryPeakResults

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

the class OverlayResults method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "There are no fitting results in memory");
        return;
    }
    names = new String[MemoryPeakResults.getResultNames().size() + 1];
    ids = new int[names.length];
    int c = 0;
    names[c++] = "(None)";
    for (MemoryPeakResults results : MemoryPeakResults.getAllResults()) {
        if (results.getSource().getOriginal() instanceof IJImageSource) {
            IJImageSource source = (IJImageSource) (results.getSource().getOriginal());
            ImagePlus imp = WindowManager.getImage(source.getName());
            if (imp != null) {
                ids[c] = imp.getID();
                names[c++] = results.getName();
            }
        }
    }
    if (c == 1) {
        IJ.error(TITLE, "There are no result images available");
        return;
    }
    names = Arrays.copyOf(names, c);
    Thread t = null;
    Worker w = null;
    NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
    gd.addMessage("Overlay results on current image frame");
    gd.addChoice("Results", names, (name == null) ? "" : name);
    gd.addCheckbox("Show_table", showTable);
    gd.addMessage("");
    gd.addHelp(About.HELP_URL);
    gd.hideCancelButton();
    gd.setOKLabel("Close");
    if (!(IJ.isMacro() || java.awt.GraphicsEnvironment.isHeadless())) {
        choice = (Choice) gd.getChoices().get(0);
        choice.addItemListener(this);
        checkbox = (Checkbox) gd.getCheckboxes().get(0);
        checkbox.addItemListener(this);
        label = (Label) gd.getMessage();
        // Listen for changes to an image
        ImagePlus.addImageListener(this);
        show();
        t = new Thread(w = new Worker());
        t.setDaemon(true);
        t.start();
    }
    gd.showDialog();
    if (!(IJ.isMacro() || java.awt.GraphicsEnvironment.isHeadless()))
        ImagePlus.removeImageListener(this);
    if (!gd.wasCanceled()) {
        name = gd.getNextChoice();
        showTable = gd.getNextBoolean();
    }
    if (t != null) {
        w.running = false;
        inbox.close();
        try {
            t.join(0);
        } catch (InterruptedException e) {
        }
        t = null;
    }
}
Also used : IJImageSource(gdsc.smlm.ij.IJImageSource) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ImagePlus(ij.ImagePlus) Point(java.awt.Point)

Example 82 with MemoryPeakResults

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

the class LoadLocalisations method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    GlobalSettings globalSettings = SettingsManager.loadSettings();
    CreateDataSettings settings = globalSettings.getCreateDataSettings();
    String[] path = Utils.decodePath(settings.localisationsFilename);
    OpenDialog chooser = new OpenDialog("Localisations_File", path[0], path[1]);
    if (chooser.getFileName() == null)
        return;
    settings.localisationsFilename = chooser.getDirectory() + chooser.getFileName();
    SettingsManager.saveSettings(globalSettings);
    LocalisationList localisations = loadLocalisations(settings.localisationsFilename);
    if (localisations == null)
        // Cancelled
        return;
    if (localisations.isEmpty()) {
        IJ.error(TITLE, "No localisations could be loaded");
        return;
    }
    MemoryPeakResults results = localisations.toPeakResults();
    // Ask the user what depth to use to create the in-memory results
    if (!getZDepth(results))
        return;
    if (myLimitZ) {
        MemoryPeakResults results2 = new MemoryPeakResults(results.size());
        results.setName(name);
        results.copySettings(results);
        for (PeakResult peak : results.getResults()) {
            if (peak.error < minz || peak.error > maxz)
                continue;
            results2.add(peak);
        }
        results = results2;
    }
    // Create the in-memory results
    if (results.size() > 0) {
        MemoryPeakResults.addResults(results);
    }
    IJ.showStatus(String.format("Loaded %d localisations", results.size()));
    if (myLimitZ)
        Utils.log("Loaded %d localisations, z between %.2f - %.2f", results.size(), minz, maxz);
    else
        Utils.log("Loaded %d localisations", results.size());
}
Also used : CreateDataSettings(gdsc.smlm.ij.settings.CreateDataSettings) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) PeakResult(gdsc.smlm.results.PeakResult) AttributePeakResult(gdsc.smlm.results.AttributePeakResult) OpenDialog(ij.io.OpenDialog)

Example 83 with MemoryPeakResults

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

the class SplitResults method splitResults.

private void splitResults(MemoryPeakResults results, ImageProcessor ip) {
    IJ.showStatus("Splitting " + Utils.pleural(results.size(), "result"));
    // Create an object mask
    ObjectAnalyzer objectAnalyzer = new ObjectAnalyzer(ip, false);
    final int maxx = ip.getWidth();
    final int maxy = ip.getHeight();
    final float scaleX = (float) results.getBounds().width / maxx;
    final float scaleY = (float) results.getBounds().height / maxy;
    // Create a results set for each object
    final int maxObject = objectAnalyzer.getMaxObject();
    MemoryPeakResults[] resultsSet = new MemoryPeakResults[maxObject + 1];
    for (int object = 0; object <= maxObject; object++) {
        MemoryPeakResults newResults = new MemoryPeakResults();
        newResults.copySettings(results);
        newResults.setName(results.getName() + " " + object);
        resultsSet[object] = newResults;
    }
    final int[] mask = objectAnalyzer.getObjectMask();
    if (showObjectMask) {
        ImageProcessor objectIp = (maxObject <= 255) ? new ByteProcessor(maxx, maxy) : new ShortProcessor(maxx, maxy);
        for (int i = 0; i < mask.length; i++) objectIp.set(i, mask[i]);
        ImagePlus imp = Utils.display(objectMask + " Objects", objectIp);
        imp.setDisplayRange(0, maxObject);
        imp.updateAndDraw();
    }
    // Process the results mapping them to their objects
    int i = 0;
    final int size = results.size();
    final int step = Utils.getProgressInterval(size);
    for (PeakResult result : results.getResults()) {
        if (++i % step == 0)
            IJ.showProgress(i, size);
        // Map to the mask objects
        final int object;
        int x = (int) (result.getXPosition() / scaleX);
        int y = (int) (result.getYPosition() / scaleY);
        if (x < 0 || x >= maxx || y < 0 || y >= maxy) {
            object = 0;
        } else {
            final int index = y * maxx + x;
            if (index < 0 || index >= mask.length)
                object = 0;
            else
                object = mask[index];
        }
        resultsSet[object].add(result);
    }
    IJ.showProgress(1);
    // Add the new results sets to memory
    i = 0;
    for (int object = (nonMaskDataset) ? 0 : 1; object <= maxObject; object++) {
        if (!resultsSet[object].isEmpty()) {
            MemoryPeakResults.addResults(resultsSet[object]);
            i++;
        }
    }
    IJ.showStatus("Split " + Utils.pleural(results.size(), "result") + " into " + Utils.pleural(i, "set"));
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageProcessor(ij.process.ImageProcessor) ObjectAnalyzer(gdsc.smlm.ij.utils.ObjectAnalyzer) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) ImagePlus(ij.ImagePlus) PeakResult(gdsc.smlm.results.PeakResult) ShortProcessor(ij.process.ShortProcessor)

Example 84 with MemoryPeakResults

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

the class TraceMolecules method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    altKeyDown = Utils.isExtraOptions();
    Trace[] traces = null;
    int totalFiltered = 0;
    if ("cluster".equals(arg)) {
        // --=-=-=-=-=-
        // Clustering
        // --=-=-=-=-=-
        outputName = "Cluster";
        if (!showClusterDialog())
            return;
        ClusteringEngine engine = new ClusteringEngine(Prefs.getThreads(), settings.getClusteringAlgorithm(), new IJTrackProgress());
        if (settings.splitPulses) {
            engine.setPulseInterval(settings.pulseInterval);
            if (settings.getTimeUnit() == TimeUnit.FRAME) {
                if (settings.getTimeThreshold() > settings.pulseInterval) {
                    settings.setTimeThreshold(settings.pulseInterval);
                }
            } else {
                if (timeInFrames(settings) > settings.pulseInterval) {
                    settings.setTimeThreshold(settings.pulseInterval * exposureTime);
                }
            }
        }
        ArrayList<Cluster> clusters = engine.findClusters(convertToClusterPoints(), settings.distanceThreshold / results.getCalibration().getNmPerPixel(), timeInFrames(settings));
        if (clusters == null) {
            Utils.log("Aborted");
            return;
        }
        traces = convertToTraces(clusters);
    } else {
        // --=-=-=-=-=-
        // Tracing
        // --=-=-=-=-=-
        outputName = "Trace";
        if (!showDialog())
            return;
        TraceManager manager = new TraceManager(results);
        manager.setTraceMode(settings.getTraceMode());
        manager.setActivationFrameInterval(settings.pulseInterval);
        manager.setActivationFrameWindow(settings.pulseWindow);
        manager.setDistanceExclusion(settings.distanceExclusion / results.getCalibration().getNmPerPixel());
        if (settings.optimise) {
            // Optimise before configuring for a pulse interval
            runOptimiser(manager);
        }
        if (settings.splitPulses) {
            manager.setPulseInterval(settings.pulseInterval);
            if (settings.getTimeUnit() == TimeUnit.FRAME) {
                if (settings.getTimeThreshold() > settings.pulseInterval) {
                    settings.setTimeThreshold(settings.pulseInterval);
                }
            } else {
                if (timeInFrames(settings) > settings.pulseInterval) {
                    settings.setTimeThreshold(settings.pulseInterval * exposureTime);
                }
            }
        }
        manager.setTracker(new IJTrackProgress());
        manager.traceMolecules(settings.distanceThreshold / results.getCalibration().getNmPerPixel(), timeInFrames(settings));
        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");
    Trace[] multiTraces = getTraces(traces);
    saveResults(results, multiTraces, outputName + " Multi");
    // Save centroids
    outputName += " Centroids";
    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.saveTraces)
        saveTraces(traces);
    summarise(traces, totalFiltered, settings.distanceThreshold, timeInSeconds(settings));
    IJ.showStatus(String.format("%d localisations => %d traces (%d filtered)", results.size(), tracedResults.size(), totalFiltered));
    // Provide option to refit the traces as single peaks and save to memory
    if (settings.refitOption)
        fitTraces(results, traces);
}
Also used : Trace(gdsc.smlm.results.Trace) IJTrackProgress(gdsc.core.ij.IJTrackProgress) Cluster(gdsc.core.clustering.Cluster) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) ClusteringEngine(gdsc.core.clustering.ClusteringEngine) TraceManager(gdsc.smlm.results.TraceManager) ClusterPoint(gdsc.core.clustering.ClusterPoint)

Example 85 with MemoryPeakResults

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

the class SphericalDistributionTest method drawImage.

private void drawImage(boolean useRejctionMethod) {
    MemoryPeakResults results = new MemoryPeakResults();
    results.setSortAfterEnd(true);
    int radius = 10;
    Rectangle bounds = new Rectangle(0, 0, radius * 2, radius * 2);
    SphericalDistribution dist = new SphericalDistribution(radius, rand);
    dist.setUseRejectionMethod(useRejctionMethod);
    float scale = 10;
    results.begin();
    for (int i = 100000; i-- > 0; ) {
        double[] xyz = dist.next();
        int peak = (int) (1 + scale * radius + Math.round(scale * xyz[2]));
        float[] params = new float[7];
        params[Gaussian2DFunction.X_POSITION] = radius + (float) xyz[0];
        params[Gaussian2DFunction.Y_POSITION] = radius + (float) xyz[1];
        results.addf(peak, 0, 0, 0, 0, 0, params, null);
    }
    results.end();
    IJImagePeakResults image = new IJImagePeakResults((useRejctionMethod) ? "Rejection Method" : "Transformation Method", bounds, scale);
    image.setRollingWindowSize(1);
    image.begin();
    image.addAll(results.getResults());
    // Place breakpoint here in debug mode to view the image. 
    // It should have an even colour through the stack.
    image.end();
}
Also used : Rectangle(java.awt.Rectangle) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults)

Aggregations

MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)86 PeakResult (gdsc.smlm.results.PeakResult)40 Rectangle (java.awt.Rectangle)16 ArrayList (java.util.ArrayList)13 ExtendedPeakResult (gdsc.smlm.results.ExtendedPeakResult)10 ImagePlus (ij.ImagePlus)10 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)8 Statistics (gdsc.core.utils.Statistics)7 IJImageSource (gdsc.smlm.ij.IJImageSource)7 Calibration (gdsc.smlm.results.Calibration)7 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)7 FractionClassificationResult (gdsc.core.match.FractionClassificationResult)6 IJImagePeakResults (gdsc.smlm.ij.results.IJImagePeakResults)6 Trace (gdsc.smlm.results.Trace)6 LinkedList (java.util.LinkedList)6 BasePoint (gdsc.core.match.BasePoint)5 ImageStack (ij.ImageStack)5 Plot2 (ij.gui.Plot2)5 Point (java.awt.Point)5 ClusterPoint (gdsc.core.clustering.ClusterPoint)4