Search in sources :

Example 1 with PeakResult

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

the class CreateData method saveFixedAndMoving.

private void saveFixedAndMoving(MemoryPeakResults results) {
    if (simpleMode || benchmarkMode || spotMode) {
        return;
    }
    if (settings.getDiffusionRate() <= 0 || settings.getFixedFraction() >= 1) {
        return;
    }
    final MemoryPeakResults fixedResults = copyMemoryPeakResults("Fixed");
    final MemoryPeakResults movingResults = copyMemoryPeakResults("Moving");
    final PeakResult[] peakResults = results.toArray();
    // Sort using the ID
    Arrays.sort(peakResults, new Comparator<PeakResult>() {

        @Override
        public int compare(PeakResult o1, PeakResult o2) {
            return o1.getId() - o2.getId();
        }
    });
    MemoryPeakResults currentResults = movingResults;
    final FrameCounter counter = new FrameCounter(-1);
    for (final PeakResult p : peakResults) {
        if (counter.advance(p.getId())) {
            currentResults = (movingMolecules.contains(p.getId())) ? movingResults : fixedResults;
        }
        currentResults.add(p);
    }
    movingResults.end();
    fixedResults.end();
}
Also used : FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImmutableMemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.ImmutableMemoryPeakResults) IdPeakResult(uk.ac.sussex.gdsc.smlm.results.IdPeakResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult)

Example 2 with PeakResult

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

the class CreateData method loadBenchmarkData.

/**
 * Load benchmark data using an open image and a XYZ text file.
 */
private void loadBenchmarkData() {
    if (!showLoadDialog()) {
        // resetMemory();
        return;
    }
    // Load the image
    final ImagePlus imp = WindowManager.getImage(benchmarkImage);
    if (imp == null) {
        IJ.error(TITLE, "No benchmark image: " + benchmarkImage);
        // resetMemory();
        return;
    }
    // Load the results
    final MemoryPeakResults results = getSimulationResults();
    if (results == null) {
        IJ.error(TITLE, "No benchmark results: " + benchmarkResultsName);
        // resetMemory();
        return;
    }
    results.setName(imp.getTitle() + " (Results)");
    results.setBounds(new Rectangle(0, 0, imp.getWidth(), imp.getHeight()));
    final IJImageSource imageSource = new IJImageSource(imp);
    results.setSource(imageSource);
    // Load the settings as these are used in the dialog
    settings = SettingsManager.readCreateDataSettings(0).toBuilder();
    simulationParameters = showSimulationParametersDialog(imp, results);
    if (simulationParameters != null) {
        // Convert data to allow analysis as if a Gaussian2D PSF
        final boolean isGaussian2D = PsfHelper.isGaussian2D(results.getPsf());
        if (isGaussian2D) {
            Gaussian2DPeakResultHelper.addMeanIntensity(results.getPsf(), results);
        } else if (simulationParameters.sd > 0) {
            final TypeConverter<DistanceUnit> dc = results.getDistanceConverter(DistanceUnit.NM);
            final PSF.Builder psf = PsfProtosHelper.getDefaultPsf(PSFType.ONE_AXIS_GAUSSIAN_2D).toBuilder();
            psf.getParametersBuilder(0).setValue(dc.convertBack(simulationParameters.sd));
            results.setPsf(psf.build());
            // Update all the results. This assumes the results do not have data for a custom PSF,
            // i.e. the parameters only have [t,i,x,y,z]
            final LocalList<PeakResult> newResults = new LocalList<>(results.size());
            final float sd = (float) dc.convertBack(simulationParameters.sd);
            final double meanFactor = Gaussian2DPeakResultHelper.getMeanSignalUsingP05(1, sd, sd);
            results.forEach((PeakResultProcedure) r -> {
                final PeakResult peak = r.resize(PeakResult.STANDARD_PARAMETERS + 1);
                peak.setMeanIntensity((float) (peak.getIntensity() * meanFactor));
                peak.setParameter(PeakResult.STANDARD_PARAMETERS, sd);
                newResults.add(peak);
            });
            results.begin();
            results.addAll(newResults);
            results.end();
        }
        setBackground(results);
        setNoise(results, imp);
        setBenchmarkResults(imp, results);
        IJ.showStatus("Loaded " + TextUtils.pleural(results.size(), "result"));
    } else {
        resetMemory();
    }
}
Also used : TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) AtomOrBuilder(uk.ac.sussex.gdsc.smlm.data.config.MoleculeProtos.AtomOrBuilder) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImmutableMemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.ImmutableMemoryPeakResults) ImagePlus(ij.ImagePlus) IdPeakResult(uk.ac.sussex.gdsc.smlm.results.IdPeakResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult)

Example 3 with PeakResult

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

the class PsfImagePeakResults method addAll.

@Override
public void addAll(PeakResult[] results) {
    if (!imageActive) {
        return;
    }
    // TODO - Make this more efficient. It could use worker threads to increase speed.
    int counter = 0;
    for (final PeakResult result : results) {
        addPeak(result.getFrame(), result.getNoise(), result.getParameters());
        if (++counter % 64 == 0) {
            updateImage();
            if (!imageActive) {
                return;
            }
        }
    }
    updateImage();
}
Also used : PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 4 with PeakResult

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

the class PeakResultTableModelFrame method doSourceOverlay.

private void doSourceOverlay() {
    final PeakResultTableModel model = getModel();
    if (model == null) {
        return;
    }
    final PeakResult[] list = table.getSelectedData();
    if (list.length == 0) {
        return;
    }
    final ImageSource source = model.getSource();
    if (source == null) {
        return;
    }
    final String title = source.getOriginal().getName();
    final ImagePlus imp = WindowManager.getImage(title);
    if (imp == null) {
        return;
    }
    // Assumes 3D stack (no channel/time)
    if (imp.getNDimensions() > 3) {
        return;
    }
    try {
        final TypeConverter<DistanceUnit> converter = CalibrationHelper.getDistanceConverter(model.getCalibration(), DistanceUnit.PIXEL);
        final Overlay o = new Overlay();
        if (list.length == 1) {
            final PeakResult p = list[0];
            final PointRoi roi = new OffsetPointRoi(converter.convert(p.getXPosition()), converter.convert(p.getYPosition()));
            roi.setPointType(3);
            roi.setPosition(p.getFrame());
            o.add(roi);
        } else {
            Arrays.sort(list, FramePeakResultComparator.INSTANCE);
            final TFloatArrayList ox = new TFloatArrayList(list.length);
            final TFloatArrayList oy = new TFloatArrayList(list.length);
            int frame = list[0].getFrame() - 1;
            for (int i = 0; i < list.length; i++) {
                if (frame != list[i].getFrame()) {
                    if (ox.size() > 0) {
                        final PointRoi roi = new OffsetPointRoi(ox.toArray(), oy.toArray());
                        roi.setPointType(3);
                        roi.setPosition(frame);
                        ox.resetQuick();
                        oy.resetQuick();
                        o.add(roi);
                    }
                    frame = list[i].getFrame();
                }
                ox.add(converter.convert(list[i].getXPosition()));
                oy.add(converter.convert(list[i].getYPosition()));
            }
            if (ox.size() > 0) {
                final PointRoi roi = new OffsetPointRoi(ox.toArray(), oy.toArray());
                roi.setPointType(3);
                roi.setPosition(frame);
                o.add(roi);
            }
        }
        imp.setOverlay(o);
        final PeakResult p = list[0];
        imp.setSlice(p.getFrame());
        ImageJUtils.adjustSourceRect(imp, 0, (int) converter.convert(p.getXPosition()), (int) converter.convert(p.getYPosition()));
        imp.getWindow().toFront();
    } catch (final ConversionException ex) {
    // Ignore
    }
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) ImagePlus(ij.ImagePlus) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) ImageSource(uk.ac.sussex.gdsc.smlm.results.ImageSource) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) SeriesImageSource(uk.ac.sussex.gdsc.smlm.ij.SeriesImageSource) Overlay(ij.gui.Overlay) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) PointRoi(ij.gui.PointRoi) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi)

Example 5 with PeakResult

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

the class ResultsImageSampler method createResultSamples.

/**
 * Creates the result samples. Do this by storing the coordinates at the region index.
 */
private void createResultSamples() {
    final TLongObjectHashMap<ResultsSample> map = new TLongObjectHashMap<>(results.size());
    ResultsSample next = ResultsSample.create(-1);
    // For SNR computation
    Object[] pixelArray = stack.getImageArray();
    int width = stack.getWidth();
    int height = stack.getHeight();
    float[] buffer = null;
    // Use a null float[] as this is not used for the getBoxRegionBounds method
    ImageExtractor ie = ImageExtractor.wrap(null, width, height);
    for (final PeakResult p : results.toArray()) {
        // Avoid invalid slices
        if (p.getFrame() < 1 || p.getFrame() > stack.getSize()) {
            continue;
        }
        // Avoid low SNR results. Get the SNR using a 3x3 region around the spot.
        final Rectangle bounds = ie.getBoxRegionBounds((int) p.getXPosition(), (int) p.getYPosition(), 1);
        buffer = ImageJImageConverter.getData(pixelArray[p.getFrame()], width, height, bounds, buffer);
        final Statistics stats = new Statistics();
        stats.add(buffer, 0, bounds.width * bounds.height);
        // SNR will be NaN if the region has no size
        final double snr = stats.getMean() / stats.getStandardDeviation();
        if (snr > 3) {
            final long index = getIndex(p.getXPosition(), p.getYPosition(), p.getFrame());
            ResultsSample current = map.putIfAbsent(index, next);
            if (current == null) {
                // If the return value is null then this is a new insertion.
                // Set the current value as the one we just added and create the next insertion object.
                current = next;
                current.index = index;
                next = ResultsSample.create(-1);
            }
            current.add(p);
        }
    }
    // Create an array of all the sample entries.
    // This is used to sample regions by density.
    data = map.values(new ResultsSample[map.size()]);
}
Also used : Rectangle(java.awt.Rectangle) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ImageExtractor(uk.ac.sussex.gdsc.core.utils.ImageExtractor)

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