Search in sources :

Example 1 with IJImagePeakResults

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

the class FIRE method createImages.

/**
	 * Creates the images to use for the FIRE calculation. This must be called after
	 * {@link #initialise(MemoryPeakResults, MemoryPeakResults)}.
	 *
	 * @param fourierImageScale
	 *            the fourier image scale (set to zero to auto compute)
	 * @param imageSize
	 *            the image size
	 * @param useSignal
	 *            Use the localisation signal to weight the intensity. The default uses a value of 1 per localisation.
	 * @return the fire images
	 */
public FireImages createImages(double fourierImageScale, int imageSize, boolean useSignal) {
    if (results == null)
        return null;
    final SignalProvider signalProvider = (useSignal && (results.getHead().getSignal() > 0)) ? new PeakSignalProvider() : new FixedSignalProvider();
    // Draw images using the existing IJ routines.
    Rectangle bounds = new Rectangle(0, 0, (int) Math.ceil(dataBounds.getWidth()), (int) Math.ceil(dataBounds.getHeight()));
    boolean weighted = true;
    boolean equalised = false;
    double imageScale;
    if (fourierImageScale <= 0) {
        double size = FastMath.max(bounds.width, bounds.height);
        if (size <= 0)
            size = 1;
        imageScale = imageSize / size;
    } else
        imageScale = fourierImageScale;
    IJImagePeakResults image1 = ImagePeakResultsFactory.createPeakResultsImage(ResultsImage.NONE, weighted, equalised, "IP1", bounds, 1, 1, imageScale, 0, ResultsMode.ADD);
    image1.setDisplayImage(false);
    image1.begin();
    IJImagePeakResults image2 = ImagePeakResultsFactory.createPeakResultsImage(ResultsImage.NONE, weighted, equalised, "IP2", bounds, 1, 1, imageScale, 0, ResultsMode.ADD);
    image2.setDisplayImage(false);
    image2.begin();
    float minx = (float) dataBounds.getX();
    float miny = (float) dataBounds.getY();
    if (this.results2 != null) {
        // Two image comparison
        for (PeakResult p : results) {
            float x = p.getXPosition() - minx;
            float y = p.getYPosition() - miny;
            image1.add(x, y, signalProvider.getSignal(p));
        }
        for (PeakResult p : results2) {
            float x = p.getXPosition() - minx;
            float y = p.getYPosition() - miny;
            image2.add(x, y, signalProvider.getSignal(p));
        }
    } else {
        // Block sampling.
        // Ensure we have at least 2 even sized blocks.
        int blockSize = Math.min(results.size() / 2, Math.max(1, FIRE.blockSize));
        int nBlocks = (int) Math.ceil((double) results.size() / blockSize);
        while (nBlocks <= 1 && blockSize > 1) {
            blockSize /= 2;
            nBlocks = (int) Math.ceil((double) results.size() / blockSize);
        }
        if (nBlocks <= 1)
            // This should not happen since the results should contain at least 2 localisations
            return null;
        if (blockSize != FIRE.blockSize)
            IJ.log(TITLE + " Warning: Changed block size to " + blockSize);
        int i = 0;
        int block = 0;
        PeakResult[][] blocks = new PeakResult[nBlocks][blockSize];
        for (PeakResult p : results) {
            if (i == blockSize) {
                block++;
                i = 0;
            }
            blocks[block][i++] = p;
        }
        // Truncate last block
        blocks[block] = Arrays.copyOf(blocks[block], i);
        final int[] indices = Utils.newArray(nBlocks, 0, 1);
        if (randomSplit)
            MathArrays.shuffle(indices);
        for (int index : indices) {
            // Split alternating so just rotate
            IJImagePeakResults image = image1;
            image1 = image2;
            image2 = image;
            for (PeakResult p : blocks[index]) {
                float x = p.getXPosition() - minx;
                float y = p.getYPosition() - miny;
                image.add(x, y, signalProvider.getSignal(p));
            }
        }
    }
    image1.end();
    ImageProcessor ip1 = image1.getImagePlus().getProcessor();
    image2.end();
    ImageProcessor ip2 = image2.getImagePlus().getProcessor();
    if (maxPerBin > 0 && signalProvider instanceof FixedSignalProvider) {
        // We can eliminate over-sampled pixels
        for (int i = ip1.getPixelCount(); i-- > 0; ) {
            if (ip1.getf(i) > maxPerBin)
                ip1.setf(i, maxPerBin);
            if (ip2.getf(i) > maxPerBin)
                ip2.setf(i, maxPerBin);
        }
    }
    return new FireImages(ip1, ip2, nmPerPixel / imageScale);
}
Also used : Rectangle(java.awt.Rectangle) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) PeakResult(gdsc.smlm.results.PeakResult) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ImageProcessor(ij.process.ImageProcessor)

Example 2 with IJImagePeakResults

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

the class DensityImage method plotResults.

/**
	 * Draw an image of the density for each localisation. Optionally filter results below a threshold.
	 * 
	 * @param results
	 * @param density
	 * @param scoreCalculator
	 * @return
	 */
private int plotResults(MemoryPeakResults results, float[] density, ScoreCalculator scoreCalculator) {
    // Filter results using 5x higher than average density of the sample in a 150nm radius:
    // Annibale, et al (2011). Identification of clustering artifacts in photoactivated localization microscopy.
    // Nature Methods, 8, pp527-528
    MemoryPeakResults newResults = null;
    // No filtering
    float densityThreshold = Float.NEGATIVE_INFINITY;
    if (filterLocalisations) {
        densityThreshold = scoreCalculator.getThreshold();
        newResults = new MemoryPeakResults();
        newResults.copySettings(results);
        newResults.setName(results.getName() + " Density Filter");
    }
    // Draw an image - Use error so that a floating point value can be used on a single pixel
    List<PeakResult> peakResults = results.getResults();
    IJImagePeakResults image = ImagePeakResultsFactory.createPeakResultsImage(ResultsImage.ERROR, false, false, results.getName() + " Density", results.getBounds(), results.getNmPerPixel(), results.getGain(), imageScale, 0, (cumulativeImage) ? ResultsMode.ADD : ResultsMode.MAX);
    image.setDisplayFlags(image.getDisplayFlags() | IJImagePeakResults.DISPLAY_NEGATIVES);
    image.setLutName("grays");
    image.begin();
    for (int i = 0; i < density.length; i++) {
        if (density[i] < densityThreshold)
            continue;
        PeakResult r = peakResults.get(i);
        image.add(0, 0, 0, 0, density[i], 0, r.params, null);
        if (newResults != null)
            newResults.add(r);
    }
    image.end();
    // Add to memory
    if (newResults != null && newResults.size() > 0)
        MemoryPeakResults.addResults(newResults);
    return image.size();
}
Also used : MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) PeakResult(gdsc.smlm.results.PeakResult)

Example 3 with IJImagePeakResults

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

the class DriftCalculator method calculateDriftUsingFrames.

/**
	 * Calculate the drift by aligning N consecutive frames with the overall image. Update the current drift parameters.
	 * 
	 * @param blocks
	 * @param blockT
	 * @param bounds
	 * @param scale
	 * @param dx
	 * @param dy
	 * @param smoothing
	 * @param iterations
	 * @return
	 */
private double calculateDriftUsingFrames(ArrayList<ArrayList<Localisation>> blocks, int[] blockT, Rectangle bounds, float scale, double[] dx, double[] dy, double[] originalDriftTimePoints, double smoothing, int iterations) {
    // Construct images using the current drift
    tracker.status("Constructing images");
    // Built an image for each block of results.
    final ImageProcessor[] images = new ImageProcessor[blocks.size()];
    List<Future<?>> futures = new LinkedList<Future<?>>();
    progressCounter = 0;
    totalCounter = images.length * 2;
    for (int i = 0; i < images.length; i++) {
        futures.add(threadPool.submit(new ImageBuilder(blocks.get(i), images, i, bounds, scale, dx, dy)));
    }
    Utils.waitForCompletion(futures);
    for (int i = 0; i < blocks.size(); i++) {
        tracker.progress(i, blocks.size());
        IJImagePeakResults blockImage = newImage(bounds, scale);
        for (Localisation r : blocks.get(i)) {
            blockImage.add(r.t, (float) (r.x + dx[r.t]), (float) (r.y + dy[r.t]), r.s);
        }
        images[i] = getImage(blockImage);
    }
    // Build an image with all results.
    FloatProcessor allIp = new FloatProcessor(images[0].getWidth(), images[0].getHeight());
    for (ImageProcessor ip : images) allIp.copyBits(ip, 0, 0, Blitter.ADD);
    return calculateDrift(blockT, scale, dx, dy, originalDriftTimePoints, smoothing, iterations, images, allIp, true);
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor) Future(java.util.concurrent.Future) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) LinkedList(java.util.LinkedList) Point(java.awt.Point)

Example 4 with IJImagePeakResults

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

the class PulseActivationAnalysis method addImageResults.

private void addImageResults(PeakResultsList resultsList, String title, Rectangle bounds, double nmPerPixel, double gain) {
    if (resultsSettings.getResultsImage() != ResultsImage.NONE) {
        IJImagePeakResults image = ImagePeakResultsFactory.createPeakResultsImage(resultsSettings.getResultsImage(), resultsSettings.weightedImage, resultsSettings.equalisedImage, title, bounds, nmPerPixel, gain, resultsSettings.imageScale, resultsSettings.precision, ResultsMode.ADD);
        image.setLiveImage(false);
        image.setDisplayImage(channels == 1);
        resultsList.addOutput(image);
    }
}
Also used : IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults)

Example 5 with IJImagePeakResults

use of gdsc.smlm.ij.results.IJImagePeakResults 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)

Aggregations

IJImagePeakResults (gdsc.smlm.ij.results.IJImagePeakResults)14 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)5 ImageProcessor (ij.process.ImageProcessor)5 Rectangle (java.awt.Rectangle)5 PeakResult (gdsc.smlm.results.PeakResult)3 FloatProcessor (ij.process.FloatProcessor)3 Calibration (gdsc.smlm.results.Calibration)2 PeakResults (gdsc.smlm.results.PeakResults)2 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)2 Well19937c (org.apache.commons.math3.random.Well19937c)2 Test (org.junit.Test)2 TimingService (gdsc.core.test.TimingService)1 IJTablePeakResults (gdsc.smlm.ij.results.IJTablePeakResults)1 BinaryFilePeakResults (gdsc.smlm.results.BinaryFilePeakResults)1 FilePeakResults (gdsc.smlm.results.FilePeakResults)1 IdPeakResult (gdsc.smlm.results.IdPeakResult)1 MALKFilePeakResults (gdsc.smlm.results.MALKFilePeakResults)1 ImagePlus (ij.ImagePlus)1 Point (java.awt.Point)1 ArrayList (java.util.ArrayList)1