Search in sources :

Example 1 with PsfSpot

use of uk.ac.sussex.gdsc.smlm.ij.plugins.PsfSpot in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFilter method getSimulationCoordinates.

/**
 * Gets the coordinates for the current simulation. This extract all the results in memory into a
 * list per frame and is cached for the simulation Id.
 *
 * @return the coordinates
 */
private TIntObjectHashMap<PsfSpot[]> getSimulationCoordinates() {
    Pair<Integer, TIntObjectHashMap<PsfSpot[]>> coords = coordinateCache.get();
    if (coords.getKey() != simulationParameters.id) {
        // Always use float coordinates.
        // The Worker adds a pixel offset for the spot coordinates.
        final TIntObjectHashMap<List<Coordinate>> coordinates = ResultsMatchCalculator.getCoordinates(results, false);
        // Spot PSFs may overlap so we must determine the amount of signal overlap and amplitude
        // effect for each spot...
        final int nThreads = Prefs.getThreads();
        final BlockingQueue<Integer> jobs = new ArrayBlockingQueue<>(nThreads * 2);
        final List<OverlapWorker> workers = new LinkedList<>();
        final List<Thread> threads = new LinkedList<>();
        final Ticker overlapTicker = ImageJUtils.createTicker(coordinates.size(), nThreads, "Computing PSF overlap ...");
        for (int i = 0; i < nThreads; i++) {
            final OverlapWorker worker = new OverlapWorker(jobs, coordinates, overlapTicker);
            final Thread t = new Thread(worker);
            workers.add(worker);
            threads.add(t);
            t.start();
        }
        // Process the frames
        coordinates.forEachKey(value -> {
            put(jobs, value);
            return true;
        });
        // Finish all the worker threads by passing in a null job
        for (int i = 0; i < threads.size(); i++) {
            put(jobs, -1);
        }
        // Wait for all to finish
        final TIntObjectHashMap<PsfSpot[]> actualCoordinates = new TIntObjectHashMap<>();
        for (int i = 0; i < threads.size(); i++) {
            try {
                threads.get(i).join();
            } catch (final InterruptedException ex) {
                Thread.currentThread().interrupt();
                throw new ConcurrentRuntimeException("Unexpected interrupt", ex);
            }
            actualCoordinates.putAll(workers.get(i).coordinates);
        }
        threads.clear();
        // For testing
        final SimpleRegression regression = new SimpleRegression(false);
        for (final PsfSpot[] spots : actualCoordinates.valueCollection()) {
            for (final PsfSpot spot : spots) {
                regression.addData(spot.getAmplitude(), calculator.getAmplitude(spot.getPeakResult().getParameters()));
            }
        }
        ImageJUtils.log("PixelAmplitude vs Amplitude = %f, slope=%f, n=%d", regression.getR(), regression.getSlope(), regression.getN());
        ImageJUtils.finished();
        coords = Pair.of(simulationParameters.id, actualCoordinates);
        coordinateCache.set(coords);
    }
    return coords.getRight();
}
Also used : Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PsfSpot(uk.ac.sussex.gdsc.smlm.ij.plugins.PsfSpot) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) SettingsList(uk.ac.sussex.gdsc.core.utils.SettingsList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList)

Example 2 with PsfSpot

use of uk.ac.sussex.gdsc.smlm.ij.plugins.PsfSpot in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFilter method showOverlay.

@SuppressWarnings("null")
private void showOverlay(ImagePlus imp, BenchmarkSpotFilterResult filterResult) {
    final Overlay o = new Overlay();
    filterResult.filterResults.forEachValue(result -> {
        final int size = result.spots.length;
        float[] tx = null;
        float[] ty = null;
        float[] fx = null;
        float[] fy = null;
        if (settings.showTP) {
            tx = new float[size];
            ty = new float[size];
        }
        if (settings.showFP) {
            fx = new float[size];
            fy = new float[size];
        }
        int tc = 0;
        int fc = 0;
        for (final ScoredSpot s : result.spots) {
            if (s.match) {
                if (settings.showTP) {
                    tx[tc] = s.spot.x + 0.5f;
                    ty[tc++] = s.spot.y + 0.5f;
                }
            } else if (settings.showFP) {
                fx[fc] = s.spot.x + 0.5f;
                fy[fc++] = s.spot.y + 0.5f;
            }
        }
        if (settings.showTP) {
            SpotFinderPreview.addRoi(result.frame, o, tx, ty, tc, Color.green);
        }
        if (settings.showFP) {
            SpotFinderPreview.addRoi(result.frame, o, fx, fy, fc, Color.red);
        }
        if (settings.showFN) {
            // We need the FN ...
            final PsfSpot[] actual = result.actual;
            final boolean[] actualAssignment = result.actualAssignment;
            final float[] nx = new float[actual.length];
            final float[] ny = new float[actual.length];
            int npoints = 0;
            for (int i = 0; i < actual.length; i++) {
                if (!actualAssignment[i]) {
                    nx[npoints] = actual[i].getX();
                    ny[npoints++] = actual[i].getY();
                }
            }
            SpotFinderPreview.addRoi(result.frame, o, nx, ny, npoints, Color.yellow);
        }
        return true;
    });
    imp.setOverlay(o);
}
Also used : PsfSpot(uk.ac.sussex.gdsc.smlm.ij.plugins.PsfSpot) Overlay(ij.gui.Overlay) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint)

Aggregations

BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)2 PsfSpot (uk.ac.sussex.gdsc.smlm.ij.plugins.PsfSpot)2 PeakResultPoint (uk.ac.sussex.gdsc.smlm.results.PeakResultPoint)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)1 Overlay (ij.gui.Overlay)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)1 SimpleRegression (org.apache.commons.math3.stat.regression.SimpleRegression)1 Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)1 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)1 SettingsList (uk.ac.sussex.gdsc.core.utils.SettingsList)1