Search in sources :

Example 36 with PeakResult

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

the class ResultsManager method cropToRoi.

private MemoryPeakResults cropToRoi(MemoryPeakResults results) {
    if (roiBounds == null)
        return results;
    // Adjust bounds relative to input results image
    double xscale = roiImageWidth / results.getBounds().width;
    double yscale = roiImageHeight / results.getBounds().height;
    roiBounds.x /= xscale;
    roiBounds.width /= xscale;
    roiBounds.y /= yscale;
    roiBounds.height /= yscale;
    float minX = (int) (roiBounds.x);
    float maxX = (int) Math.ceil(roiBounds.x + roiBounds.width);
    float minY = (int) (roiBounds.y);
    float maxY = (int) Math.ceil(roiBounds.y + roiBounds.height);
    // Create a new set of results within the bounds
    MemoryPeakResults newResults = new MemoryPeakResults();
    newResults.begin();
    for (PeakResult peakResult : results.getResults()) {
        float x = peakResult.params[Gaussian2DFunction.X_POSITION];
        float y = peakResult.params[Gaussian2DFunction.Y_POSITION];
        if (x < minX || x > maxX || y < minY || y > maxY)
            continue;
        newResults.add(peakResult);
    }
    newResults.end();
    newResults.copySettings(results);
    newResults.setBounds(new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY)));
    return newResults;
}
Also used : Rectangle(java.awt.Rectangle) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) PeakResult(gdsc.smlm.results.PeakResult) ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult)

Example 37 with PeakResult

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

the class PeakFit method processResults.

private boolean processResults(FitEngine engine, ArrayList<PeakResult> sliceCandidates, int slice) {
    // Process results
    int[] maxIndices = new int[sliceCandidates.size()];
    int count = 0;
    ArrayList<PeakResult> processedResults = new ArrayList<PeakResult>(sliceCandidates.size());
    for (PeakResult result : sliceCandidates) {
        // Add ExtendedPeakResults to the results if they span multiple frames (they are the result of previous fitting).
        if (result instanceof ExtendedPeakResult && result.getFrame() != result.getEndFrame()) {
            processedResults.add(result);
        } else {
            // Fit single frame results.
            maxIndices[count++] = result.origX + bounds.width * result.origY;
        }
    }
    if (!processedResults.isEmpty())
        this.results.addAll(processedResults);
    if (count != 0) {
        float[] data = source.get(slice);
        if (data == null)
            return false;
        FitParameters fitParams = new FitParameters();
        fitParams.maxIndices = Arrays.copyOf(maxIndices, count);
        FitJob job = new ParameterisedFitJob(fitParams, slice, data, bounds);
        engine.run(job);
    }
    return true;
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) FitParameters(gdsc.smlm.engine.FitParameters) ParameterisedFitJob(gdsc.smlm.engine.ParameterisedFitJob) ArrayList(java.util.ArrayList) ParameterisedFitJob(gdsc.smlm.engine.ParameterisedFitJob) FitJob(gdsc.smlm.engine.FitJob) PeakResult(gdsc.smlm.results.PeakResult) ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult)

Example 38 with PeakResult

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

the class PulseActivationAnalysis method runSimulation.

private void runSimulation() {
    TITLE += " Simulation";
    if (!showSimulationDialog())
        return;
    long start = System.currentTimeMillis();
    RandomDataGenerator rdg = getRandomDataGenerator();
    // Draw the molecule positions
    Utils.showStatus("Simulating molecules ...");
    float[][][] molecules = new float[3][][];
    MemoryPeakResults[] results = new MemoryPeakResults[3];
    Rectangle bounds = new Rectangle(0, 0, sim_size, sim_size);
    for (int c = 0; c < 3; c++) {
        molecules[c] = simulateMolecules(rdg, c);
        // Create a dataset to store the activations
        MemoryPeakResults r = new MemoryPeakResults();
        r.setCalibration(new Calibration(sim_nmPerPixel, 1, 100));
        r.setBounds(bounds);
        r.setName(TITLE + " C" + (c + 1));
        results[c] = r;
    }
    // Simulate activation
    Utils.showStatus("Simulating activations ...");
    for (int c = 0; c < 3; c++) simulateActivations(rdg, molecules, c, results);
    // Combine
    Utils.showStatus("Producing simulation output ...");
    MemoryPeakResults r = new MemoryPeakResults();
    r.setCalibration(new Calibration(sim_nmPerPixel, 1, 100));
    r.setBounds(new Rectangle(0, 0, sim_size, sim_size));
    r.setName(TITLE);
    ImageProcessor[] images = new ImageProcessor[3];
    for (int c = 0; c < 3; c++) {
        ArrayList<PeakResult> list = (ArrayList<PeakResult>) results[c].getResults();
        r.addAllf(list);
        // Draw the unmixed activations
        IJImagePeakResults image = ImagePeakResultsFactory.createPeakResultsImage(ResultsImage.LOCALISATIONS, true, true, TITLE, bounds, sim_nmPerPixel, 1, 1024.0 / sim_size, 0, ResultsMode.ADD);
        image.setLiveImage(false);
        image.setDisplayImage(false);
        image.begin();
        image.addAll(list);
        image.end();
        images[c] = image.getImagePlus().getProcessor();
    }
    displayComposite(images, TITLE);
    // Add to memory. Set the composite dataset first.
    MemoryPeakResults.addResults(r);
    for (int c = 0; c < 3; c++) MemoryPeakResults.addResults(results[c]);
    // TODO:
    // Show an image of what it looks like with no unmixing, i.e. colours allocated 
    // from the frame
    Utils.showStatus("Simulation complete: " + Utils.timeToString(System.currentTimeMillis() - start));
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Calibration(gdsc.smlm.results.Calibration) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) PeakResult(gdsc.smlm.results.PeakResult) IdPeakResult(gdsc.smlm.results.IdPeakResult) ImageProcessor(ij.process.ImageProcessor) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults)

Example 39 with PeakResult

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

the class PeakFit method setup.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
	 */
public int setup(String arg, ImagePlus imp) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    plugin_flags = FLAGS;
    extraOptions = Utils.isExtraOptions();
    maximaIdentification = (arg != null && arg.contains("spot"));
    fitMaxima = (arg != null && arg.contains("maxima"));
    simpleFit = (arg != null && arg.contains("simple"));
    boolean runSeries = (arg != null && arg.contains("series"));
    ImageSource imageSource = null;
    if (fitMaxima) {
        imp = null;
        // The image source will be found from the peak results.
        if (!showMaximaDialog())
            return DONE;
        MemoryPeakResults results = ResultsManager.loadInputResults(inputOption, false);
        if (results == null || results.size() == 0) {
            IJ.error(TITLE, "No results could be loaded");
            return DONE;
        }
        // Check for single frame
        singleFrame = results.getHead().getFrame();
        for (PeakResult result : results.getResults()) {
            if (singleFrame != result.getFrame()) {
                singleFrame = 0;
                break;
            }
        }
        imageSource = results.getSource();
        plugin_flags |= NO_IMAGE_REQUIRED;
    } else if (runSeries) {
        imp = null;
        // Select input folder
        String inputDirectory;
        inputDirectory = IJ.getDirectory("Select image series ...");
        //inputDirectory = getInputDirectory("Select image series ...");
        if (inputDirectory == null)
            return DONE;
        // Load input series ...
        SeriesOpener series;
        if (extraOptions)
            series = new SeriesOpener(inputDirectory, true, numberOfThreads);
        else
            series = new SeriesOpener(inputDirectory);
        if (series.getNumberOfImages() == 0) {
            IJ.error(TITLE, "No images in the selected directory:\n" + inputDirectory);
            return DONE;
        }
        SeriesImageSource seriesImageSource = new SeriesImageSource(getName(series.getImageList()), series);
        seriesImageSource.setLogProgress(true);
        if (extraOptions) {
            numberOfThreads = Math.max(1, series.getNumberOfThreads());
            seriesImageSource.setNumberOfThreads(numberOfThreads);
        }
        imageSource = seriesImageSource;
        plugin_flags |= NO_IMAGE_REQUIRED;
    } else {
        if (imp == null) {
            IJ.noImage();
            return DONE;
        }
        // Check it is not a previous result
        if (imp.getTitle().endsWith(IJImagePeakResults.IMAGE_SUFFIX)) {
            IJImageSource tmpImageSource = null;
            // Check the image to see if it has an image source XML structure in the info property
            Object o = imp.getProperty("Info");
            Pattern pattern = Pattern.compile("Source: (<.*IJImageSource>.*<.*IJImageSource>)", Pattern.DOTALL);
            Matcher match = pattern.matcher((o == null) ? "" : o.toString());
            if (match.find()) {
                ImageSource source = ImageSource.fromXML(match.group(1));
                if (source instanceof IJImageSource) {
                    tmpImageSource = (IJImageSource) source;
                    if (!tmpImageSource.open()) {
                        tmpImageSource = null;
                    } else {
                        imp = WindowManager.getImage(tmpImageSource.getName());
                    }
                }
            }
            if (tmpImageSource == null) {
                // Look for a parent using the title
                String parentTitle = imp.getTitle().substring(0, imp.getTitle().length() - IJImagePeakResults.IMAGE_SUFFIX.length() - 1);
                ImagePlus parentImp = WindowManager.getImage(parentTitle);
                if (parentImp != null) {
                    tmpImageSource = new IJImageSource(parentImp);
                    imp = parentImp;
                }
            }
            String message = "The selected image may be a previous fit result";
            if (tmpImageSource != null) {
                // are missing
                if (!Utils.isNullOrEmpty(tmpImageSource.getName()))
                    message += " of: \n \n" + tmpImageSource.getName();
                message += " \n \nFit the parent?";
            } else
                message += " \n \nDo you want to continue?";
            YesNoCancelDialog d = new YesNoCancelDialog(null, TITLE, message);
            if (tmpImageSource == null) {
                if (!d.yesPressed())
                    return DONE;
            } else {
                if (d.yesPressed())
                    imageSource = tmpImageSource;
                if (d.cancelPressed())
                    return DONE;
            }
        }
        if (imageSource == null)
            imageSource = new IJImageSource(imp);
    }
    time = -1;
    if (!initialiseImage(imageSource, getBounds(imp), false)) {
        IJ.error(TITLE, "Failed to initialise the source image: " + imageSource.getName());
        return DONE;
    }
    int flags = showDialog(imp);
    if ((flags & DONE) == 0) {
        // Repeat so that we pass in the selected option for ignoring the bounds.
        // This should not be necessary since it is set within the readDialog method.
        //if (ignoreBoundsForNoise)
        //	initialiseImage(imageSource, bounds, ignoreBoundsForNoise);
        initialiseFitting();
    }
    return flags;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SeriesImageSource(gdsc.smlm.ij.SeriesImageSource) SeriesOpener(gdsc.smlm.ij.utils.SeriesOpener) ImagePlus(ij.ImagePlus) PeakResult(gdsc.smlm.results.PeakResult) ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) IJImageSource(gdsc.smlm.ij.IJImageSource) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) YesNoCancelDialog(ij.gui.YesNoCancelDialog) AggregatedImageSource(gdsc.smlm.results.AggregatedImageSource) IJImageSource(gdsc.smlm.ij.IJImageSource) SeriesImageSource(gdsc.smlm.ij.SeriesImageSource) InterlacedImageSource(gdsc.smlm.results.InterlacedImageSource) ImageSource(gdsc.smlm.results.ImageSource)

Example 40 with PeakResult

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

the class ResultsMatchCalculator method getTimepoints.

/**
	 * Merge the time points from the two sets into a single sorted list of unique time points
	 * 
	 * @param actualPoints
	 * @param predictedPoints
	 * @return
	 */
@SuppressWarnings("unused")
private int[] getTimepoints(List<PeakResult> actualPoints, List<PeakResult> predictedPoints) {
    TIntHashSet set = new TIntHashSet();
    for (PeakResult r : actualPoints) set.add(r.getFrame());
    for (PeakResult r : predictedPoints) set.add(r.getFrame());
    int[] t = set.toArray();
    Arrays.sort(t);
    return t;
}
Also used : TIntHashSet(gnu.trove.set.hash.TIntHashSet) PeakResult(gdsc.smlm.results.PeakResult)

Aggregations

PeakResult (gdsc.smlm.results.PeakResult)89 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)40 ExtendedPeakResult (gdsc.smlm.results.ExtendedPeakResult)18 Rectangle (java.awt.Rectangle)18 ArrayList (java.util.ArrayList)17 IdPeakResult (gdsc.smlm.results.IdPeakResult)13 ImagePlus (ij.ImagePlus)9 Point (java.awt.Point)9 Trace (gdsc.smlm.results.Trace)8 ImageStack (ij.ImageStack)7 FractionClassificationResult (gdsc.core.match.FractionClassificationResult)6 Calibration (gdsc.smlm.results.Calibration)6 PreprocessedPeakResult (gdsc.smlm.results.filter.PreprocessedPeakResult)6 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)6 BasePoint (gdsc.core.match.BasePoint)5 Statistics (gdsc.core.utils.Statistics)5 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)5 IJImagePeakResults (gdsc.smlm.ij.results.IJImagePeakResults)5 GenericDialog (ij.gui.GenericDialog)5 ImageProcessor (ij.process.ImageProcessor)5