Search in sources :

Example 26 with PeakResult

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

the class SpotAnalysis method saveSpot.

private void saveSpot() {
    if (onFrames.isEmpty() || !updated) {
        return;
    }
    createResultsWindow();
    id++;
    double signal = 0;
    Trace trace = null;
    final float psfWidth = Float.parseFloat(widthTextField.getText());
    final float cx = areaBounds.x + areaBounds.width / 2.0f;
    final float cy = areaBounds.y + areaBounds.height / 2.0f;
    for (final Spot s : onFrames) {
        // Get the signal again since the background may have changed.
        final double spotSignal = getSignal(s.frame);
        signal += spotSignal;
        final float[] params = Gaussian2DPeakResultHelper.createOneAxisParams(0, (float) (spotSignal), cx, cy, 0, psfWidth);
        final PeakResult result = new PeakResult(s.frame, (int) cx, (int) cy, 0, 0, 0, 0, params, null);
        if (trace == null) {
            trace = new Trace(result);
        } else {
            trace.add(result);
        }
    }
    if (trace == null) {
        return;
    }
    final Statistics tOn = Statistics.create(trace.getOnTimes());
    final Statistics tOff = Statistics.create(trace.getOffTimes());
    resultsWindow.append(String.format("%d\t%.1f\t%.1f\t%s\t%s\t%s\t%d\t%s\t%s\t%s", id, cx, cy, MathUtils.rounded(signal, 4), MathUtils.rounded(tOn.getSum() * msPerFrame, 3), MathUtils.rounded(tOff.getSum() * msPerFrame, 3), trace.getBlinks() - 1, MathUtils.rounded(tOn.getMean() * msPerFrame, 3), MathUtils.rounded(tOff.getMean() * msPerFrame, 3), imp.getTitle()));
    // Save the individual on/off times for use in creating a histogram
    traces.put(id, trace);
    updated = false;
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 27 with PeakResult

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

the class ResultsMatchCalculator method savePairs.

private void savePairs(MemoryPeakResults results1, MemoryPeakResults results2, List<PointPair> allMatches) {
    if (!settings.savePairs) {
        return;
    }
    // Get the directory
    final String directory = ImageJUtils.getDirectory("Pairs_directory", settings.pairsDirectory);
    if (directory == null) {
        return;
    }
    settings.pairsDirectory = directory;
    final double[] distanceThresholds = getDistances(settings.distanceThreshold, settings.increments, settings.delta);
    // Create output files for each distance band
    final PeakResults[] output1 = new PeakResults[distanceThresholds.length];
    final PeakResults[] output2 = new PeakResults[distanceThresholds.length];
    double high = 0;
    for (int i = 0; i < distanceThresholds.length; i++) {
        final double low = high;
        high = distanceThresholds[i];
        output1[i] = createFilePeakResults(directory, 1, results1, low, high);
        output2[i] = createFilePeakResults(directory, 2, results2, low, high);
    }
    // Square the thresholds
    SimpleArrayUtils.apply(distanceThresholds, v -> v * v);
    final double[] pairDistances = getPairDistances(allMatches);
    int index = 0;
    for (final PointPair pair : allMatches) {
        final int insert = search(distanceThresholds, pairDistances[index++]);
        if (insert != -1) {
            final PeakResult r1 = ((PeakResultPoint) pair.getPoint1()).getPeakResult();
            final PeakResult r2 = ((PeakResultPoint) pair.getPoint2()).getPeakResult();
            output1[insert].add(r1);
            output2[insert].add(r2);
        }
    }
    for (int i = 0; i < output1.length; i++) {
        output1[i].end();
        output2[i].end();
    }
}
Also used : MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) PointPair(uk.ac.sussex.gdsc.core.match.PointPair) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) Point(java.awt.Point) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 28 with PeakResult

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

the class DensityImage method cropBorder.

/**
 * Remove any results which fall in the radius border added around the ROI. Results are modified
 * in place and a new density array is returned.
 *
 * @param results the results
 * @param density the density
 * @return the density array
 */
private int[] cropBorder(MemoryPeakResults results, int[] density) {
    if (roiBounds == null) {
        return density;
    }
    final float minX = (int) (scaledRoiMinX);
    final float maxX = (int) Math.ceil(scaledRoiMaxX);
    final float minY = (int) (scaledRoiMinY);
    final float maxY = (int) Math.ceil(scaledRoiMaxY);
    // Clone the results then add back those that are within the bounds
    final PeakResult[] peakResults = results.toArray();
    results.begin();
    int count = 0;
    for (int i = 0; i < peakResults.length; i++) {
        final PeakResult peakResult = peakResults[i];
        final float x = peakResult.getXPosition();
        final float y = peakResult.getYPosition();
        if (x < minX || x > maxX || y < minY || y > maxY) {
            continue;
        }
        results.add(peakResult);
        density[count++] = density[i];
    }
    results.end();
    results.setBounds(new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY)));
    return Arrays.copyOf(density, count);
}
Also used : Rectangle(java.awt.Rectangle) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 29 with PeakResult

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

the class DiffusionRateTest method aggregateIntoFrames.

private void aggregateIntoFrames(ArrayList<Point> points, boolean addError, double precisionInPixels, NormalizedGaussianSampler gauss) {
    if (myAggregateSteps < 1) {
        return;
    }
    final MemoryPeakResults results = new MemoryPeakResults(points.size() / myAggregateSteps);
    results.setCalibration(CalibrationHelper.create(settings.getPixelPitch(), 1, myAggregateSteps * 1000.0 / settings.getStepsPerSecond()));
    results.setName(TITLE + " Aggregated");
    results.setPsf(PsfHelper.create(PSFType.CUSTOM));
    MemoryPeakResults.addResults(results);
    simulation.dataset[1] = results.getName();
    int id = 0;
    int peak = 1;
    int number = 0;
    double cx = 0;
    double cy = 0;
    // Get the mean square distance
    double sum = 0;
    int count = 0;
    PeakResult last = null;
    for (final Point result : points) {
        final boolean newId = result.id != id;
        if (number >= myAggregateSteps || newId) {
            if (number != 0) {
                double[] xyz = new double[] { cx / number, cy / number };
                if (addError) {
                    xyz = addError(xyz, precisionInPixels, gauss);
                }
                final float[] params = PeakResult.createParams(0, number, (float) xyz[0], (float) xyz[1], 0);
                final float noise = 0.1f;
                final PeakResult r = new ExtendedPeakResult(peak, (int) params[PeakResult.X], (int) params[PeakResult.Y], number, 0, noise, 0, params, null, peak, id);
                results.add(r);
                if (last != null) {
                    sum += last.distance2(r);
                    count++;
                }
                last = r;
                number = 0;
                cx = cy = 0;
                peak++;
            }
            if (newId) {
                // Increment the frame so that tracing analysis can distinguish traces
                peak++;
                last = null;
                id = result.id;
            }
        }
        number++;
        cx += result.x;
        cy += result.y;
    }
    // Final peak
    if (number != 0) {
        double[] xyz = new double[] { cx / number, cy / number };
        if (addError) {
            xyz = addError(xyz, precisionInPixels, gauss);
        }
        final float[] params = PeakResult.createParams(0, number, (float) xyz[0], (float) xyz[1], 0);
        final float noise = 0.1f;
        final PeakResult r = new ExtendedPeakResult(peak, (int) params[PeakResult.X], (int) params[PeakResult.Y], number, 0, noise, 0, params, null, peak, id);
        results.add(r);
        if (last != null) {
            sum += last.distance2(r);
            count++;
        }
    }
    // MSD in pixels^2 / frame
    final double msd = sum / count;
    // Convert to um^2/second
    ImageJUtils.log("Aggregated data D=%s um^2/s, Precision=%s nm, N=%d, step=%s s, mean=%s um^2, " + "MSD = %s um^2/s", MathUtils.rounded(settings.getDiffusionRate()), MathUtils.rounded(myPrecision), count, MathUtils.rounded(results.getCalibrationReader().getExposureTime() / 1000), MathUtils.rounded(msd / conversionFactor), MathUtils.rounded((msd / conversionFactor) / (results.getCalibrationReader().getExposureTime() / 1000)));
    msdAnalysis(points);
}
Also used : ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult)

Example 30 with PeakResult

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

the class CropResults method cropResults.

/**
 * Apply the filters to the data.
 */
private void cropResults() {
    final MemoryPeakResults newResults = createNewResults();
    // These bounds are integer. But this is because the results are meant to come from an image.
    final Rectangle integerBounds = results.getBounds(true);
    // The crop bounds can be floating point...
    // Border
    final double border = settings.getBorder();
    final double xx = integerBounds.x + border;
    final double yy = integerBounds.y + border;
    final double w = Math.max(0, integerBounds.width - 2 * border);
    final double h = Math.max(0, integerBounds.height - 2 * border);
    Rectangle2D pixelBounds = new Rectangle2D.Double(xx, yy, w, h);
    // Bounding box
    if (settings.getSelectRegion()) {
        final Rectangle2D boxBounds = new Rectangle2D.Double(settings.getX(), settings.getY(), settings.getWidth(), settings.getHeight());
        pixelBounds = pixelBounds.createIntersection(boxBounds);
    }
    // and create another intersection
    if (myUseRoi) {
        final ImagePlus imp = WindowManager.getImage(settings.getRoiImage());
        if (imp != null && imp.getRoi() != null) {
            final Rectangle roi = imp.getRoi().getBounds();
            final int roiImageWidth = imp.getWidth();
            final int roiImageHeight = imp.getHeight();
            final double xscale = (double) roiImageWidth / integerBounds.width;
            final double yscale = (double) roiImageHeight / integerBounds.height;
            final Rectangle2D roiBounds = new Rectangle2D.Double(roi.x / xscale, roi.y / yscale, roi.width / xscale, roi.height / yscale);
            pixelBounds = pixelBounds.createIntersection(roiBounds);
        }
    }
    final Rectangle2D bounds = pixelBounds;
    final Predicate<PeakResult> testZ = getZFilter();
    // Copy the results if the origin is reset
    final Consumer<PeakResult> consumer = settings.getResetOrigin() ? r -> newResults.add(r.copy()) : newResults::add;
    if (bounds.getWidth() > 0 && bounds.getHeight() > 0) {
        results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> {
            if (bounds.contains(x, y) && testZ.test(result)) {
                consumer.accept(result);
            }
        });
    }
    if (settings.getPreserveBounds()) {
        newResults.setBounds(integerBounds);
    } else {
        // Get the lower and upper integer limits
        final int x = (int) Math.floor(bounds.getX());
        final int y = (int) Math.floor(bounds.getY());
        final int ux = (int) Math.ceil(bounds.getX() + bounds.getWidth());
        final int uy = (int) Math.ceil(bounds.getY() + bounds.getHeight());
        // Ensure the width and height are at least 1
        newResults.setBounds(new Rectangle(x, y, Math.max(1, ux - x), Math.max(1, uy - y)));
        if (settings.getResetOrigin()) {
            newResults.translate(-x, -y);
        }
    }
    IJ.showStatus(newResults.size() + " Cropped localisations");
}
Also used : Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) PassPeakResultPredicate(uk.ac.sussex.gdsc.smlm.results.predicates.PassPeakResultPredicate) IdentityTypeConverter(uk.ac.sussex.gdsc.core.data.utils.IdentityTypeConverter) WindowManager(ij.WindowManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) CoordinatePredicateUtils(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicateUtils) PeakResultValueParameter(uk.ac.sussex.gdsc.smlm.results.PeakResultValueParameter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) UnitHelper(uk.ac.sussex.gdsc.smlm.data.config.UnitHelper) CropResultsSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.CropResultsSettings) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) CoordinatePredicate(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicate) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) Predicate(java.util.function.Predicate) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) CalibrationHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationHelper) Consumer(java.util.function.Consumer) ImagePlus(ij.ImagePlus) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) MinMaxResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.MinMaxResultProcedure) MinMaxPeakResultPredicate(uk.ac.sussex.gdsc.smlm.results.predicates.MinMaxPeakResultPredicate) PlugIn(ij.plugin.PlugIn) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImagePlus(ij.ImagePlus) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Aggregations

PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)64 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)37 List (java.util.List)18 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)18 Rectangle (java.awt.Rectangle)17 Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)17 FrameCounter (uk.ac.sussex.gdsc.smlm.results.count.FrameCounter)17 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)17 ImagePlus (ij.ImagePlus)14 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)14 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)14 IJ (ij.IJ)13 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)12 PlugIn (ij.plugin.PlugIn)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 SimpleArrayUtils (uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils)10 SettingsManager (uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager)10 PointRoi (ij.gui.PointRoi)9 ArrayList (java.util.ArrayList)9 TypeConverter (uk.ac.sussex.gdsc.core.data.utils.TypeConverter)9