Search in sources :

Example 1 with MaximaSpotFilter

use of uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter in project GDSC-SMLM by aherbert.

the class FitEngineConfiguration method createSpotFilter.

/**
 * Create the spot filter for identifying candidate maxima. The actual border, search width and
 * smoothing parameters can be configured relative to the configured standard deviations or left
 * absolute. The standard deviation is used to determine the Half-Width at Half-Maximum (HWHM) for
 * each dimension and the parameters set as follows.
 *
 * <pre>
 * int search = (int) Math.ceil(getSearch() * hwhmMax);
 * int border = (int) Math.floor(getBorder() * hwhmMax);
 * // For each filter
 * double smooth = getSmooth(i) * hwhmMin;
 * </pre>
 *
 * @return the maxima spot filter
 */
public MaximaSpotFilter createSpotFilter() {
    // Respect the absolute parameter absolute flag for all the distances.
    // Get the half-width at half maximum
    final double hwhmMin = getHwhmMin();
    final double hwhmMax = getHwhmMax();
    // Note: rounding to 2 decimal places is a simple method for removing small errors
    // in floating point precision from creating an incorrect integer
    // Region for maxima finding
    int search = (int) Math.ceil(convert(getSearchParameter(), hwhmMax, 2));
    if (search < 1) {
        search = 1;
    }
    // Border where peaks are ignored
    int border = (int) Math.floor(convert(getBorderParameter(), hwhmMax, 2));
    if (border < 0) {
        border = 0;
    }
    final DataProcessor processor0 = createDataProcessor(border, 0, hwhmMin);
    final DataFilterSettings f = fitEngineSettings.getDataFilterSettings();
    final int filterCount = f.getDataFiltersCount();
    final MaximaSpotFilter spotFilter;
    if (f.getDataFilterType() == DataFilterType.JURY && filterCount > 1) {
        final DataProcessor[] processors = new DataProcessor[filterCount];
        processors[0] = processor0;
        for (int i = 1; i < filterCount; i++) {
            processors[i] = createDataProcessor(border, i, hwhmMin);
        }
        spotFilter = new JurySpotFilter(search, border, processors);
    } else if (f.getDataFilterType() == DataFilterType.DIFFERENCE && filterCount > 1) {
        final DataProcessor processor1 = createDataProcessor(border, 1, hwhmMin);
        spotFilter = new DifferenceSpotFilter(search, border, processor0, processor1);
    } else {
        spotFilter = new SingleSpotFilter(search, border, processor0);
    }
    if (getFitConfiguration().isPerPixelCameraType()) {
        if (!spotFilter.isWeighted()) {
            throw new IllegalStateException("Camera type requires a weighted spot filter: " + fitConfiguration.getCameraType());
        }
        final CameraModel model = fitConfiguration.getCameraModel();
        if (model == null || !model.isPerPixelModel()) {
            throw new IllegalStateException("Weighted spot filter requires a per-pixel camera model");
        }
    }
    return spotFilter;
}
Also used : CameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CameraModel) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) JurySpotFilter(uk.ac.sussex.gdsc.smlm.filters.JurySpotFilter) DifferenceSpotFilter(uk.ac.sussex.gdsc.smlm.filters.DifferenceSpotFilter) GaussianDataProcessor(uk.ac.sussex.gdsc.smlm.filters.GaussianDataProcessor) MedianDataProcessor(uk.ac.sussex.gdsc.smlm.filters.MedianDataProcessor) CircularMeanDataProcessor(uk.ac.sussex.gdsc.smlm.filters.CircularMeanDataProcessor) DataProcessor(uk.ac.sussex.gdsc.smlm.filters.DataProcessor) AverageDataProcessor(uk.ac.sussex.gdsc.smlm.filters.AverageDataProcessor) BlockAverageDataProcessor(uk.ac.sussex.gdsc.smlm.filters.BlockAverageDataProcessor) DataFilterSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterSettings) SingleSpotFilter(uk.ac.sussex.gdsc.smlm.filters.SingleSpotFilter)

Example 2 with MaximaSpotFilter

use of uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method createResults.

/**
 * Create peak results.
 *
 * @param filterResults The results from running the filter (or null)
 * @param filter the filter
 */
private MemoryPeakResults createResults(PreprocessedPeakResult[] filterResults, DirectFilter filter, boolean withBorder) {
    if (filterResults == null) {
        final MultiPathFilter multiPathFilter = createMpf(filter, defaultMinimalFilter);
        filterResults = filterResults(multiPathFilter);
    }
    final MemoryPeakResults newResults = new MemoryPeakResults();
    newResults.copySettings(this.results);
    newResults.setName(TITLE);
    if (withBorder) {
        // To produce the same results as the PeakFit plugin we must implement the border
        // functionality used in the FitWorker. This respects the border of the spot filter.
        final FitEngineConfiguration config = new FitEngineConfiguration();
        updateAllConfiguration(config);
        final MaximaSpotFilter spotFilter = config.createSpotFilter();
        final int border = spotFilter.getBorder();
        final Rectangle bounds = getBounds();
        final int borderLimitX = bounds.x + bounds.width - border;
        final int borderLimitY = bounds.y + bounds.height - border;
        for (final PreprocessedPeakResult spot : filterResults) {
            if (spot.getX() > border && spot.getX() < borderLimitX && spot.getY() > border && spot.getY() < borderLimitY) {
                final double[] p = spot.toGaussian2DParameters();
                final float[] params = new float[p.length];
                for (int j = 0; j < p.length; j++) {
                    params[j] = (float) p[j];
                }
                final int frame = spot.getFrame();
                final int origX = (int) p[Gaussian2DFunction.X_POSITION];
                final int origY = (int) p[Gaussian2DFunction.Y_POSITION];
                newResults.add(frame, origX, origY, 0, 0, spot.getNoise(), spot.getMeanSignal(), params, null);
            }
        }
    } else {
        for (final PreprocessedPeakResult spot : filterResults) {
            final double[] p = spot.toGaussian2DParameters();
            final float[] params = new float[p.length];
            for (int j = 0; j < p.length; j++) {
                params[j] = (float) p[j];
            }
            final int frame = spot.getFrame();
            final int origX = (int) p[Gaussian2DFunction.X_POSITION];
            final int origY = (int) p[Gaussian2DFunction.Y_POSITION];
            newResults.add(frame, origX, origY, 0, 0, spot.getNoise(), spot.getMeanSignal(), params, null);
        }
    }
    return newResults;
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) Rectangle(java.awt.Rectangle) BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.PreprocessedPeakResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 3 with MaximaSpotFilter

use of uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter in project GDSC-SMLM by aherbert.

the class SmoothImage method run.

@Override
public void run(ImageProcessor ip) {
    final Rectangle bounds = ip.getRoi();
    // Crop to the ROI
    FloatProcessor fp = ip.crop().toFloat(0, null);
    float[] data = (float[]) fp.getPixels();
    final MaximaSpotFilter filter = createSpotFilter();
    final int width = fp.getWidth();
    final int height = fp.getHeight();
    data = filter.preprocessData(data, width, height);
    fp = new FloatProcessor(width, height, data);
    ip.insert(fp, bounds.x, bounds.y);
    if (settings.autoAdjustConstrast) {
        ip.setMinAndMax(fp.getMin(), fp.getMax());
    } else {
        ip.setMinAndMax(min, max);
    }
}
Also used : FloatProcessor(ij.process.FloatProcessor) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) Rectangle(java.awt.Rectangle)

Example 4 with MaximaSpotFilter

use of uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFilter method runAnalysis.

private BenchmarkSpotFilterResult runAnalysis(FitEngineConfiguration config, boolean batchSummary) {
    if (ImageJUtils.isInterrupted()) {
        return null;
    }
    final MaximaSpotFilter spotFilter = config.createSpotFilter();
    if (!batchMode) {
        IJ.showStatus("Computing results ...");
    }
    final ImageStack stack = imp.getImageStack();
    float background = 0;
    if (spotFilter.isAbsoluteIntensity()) {
        if (Float.isNaN(resultsBackground)) {
            // To allow the signal factor to be computed we need to lower the image by the background so
            // that the intensities correspond to the results amplitude.
            // Just assume the simulation background is uniform.
            final StandardResultProcedure s = new StandardResultProcedure(results, IntensityUnit.PHOTON);
            s.getB();
            resultsBackground = (float) (MathUtils.sum(s.background) / results.size());
        }
        background = this.resultsBackground;
    }
    // Create the weights if needed
    if (cameraModel.isPerPixelModel() && spotFilter.isWeighted() && weights == null) {
        bounds = cameraModel.getBounds();
        weights = cameraModel.getNormalisedWeights(bounds);
    }
    // Create a pool of workers
    final int nThreads = Prefs.getThreads();
    final BlockingQueue<Integer> jobs = new ArrayBlockingQueue<>(nThreads * 2);
    final List<Worker> workers = new LocalList<>(nThreads);
    final List<Thread> threads = new LocalList<>(nThreads);
    for (int i = 0; i < nThreads; i++) {
        final Worker worker = new Worker(jobs, stack, spotFilter, background, simulationCoords);
        final Thread t = new Thread(worker);
        workers.add(worker);
        threads.add(t);
        t.start();
    }
    // Fit the frames
    for (int i = 1; i <= stack.getSize(); i++) {
        put(jobs, i);
    }
    // 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
    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);
        }
    }
    threads.clear();
    if (ImageJUtils.isInterrupted()) {
        return null;
    }
    if (!batchMode) {
        IJ.showProgress(-1);
        IJ.showStatus("Collecting results ...");
    }
    TIntObjectHashMap<FilterResult> filterResults = null;
    time = 0;
    for (int i = 0; i < workers.size(); i++) {
        final Worker w = workers.get(i);
        time += w.time;
        if (filterResults == null) {
            filterResults = w.results;
        } else {
            filterResults.putAll(w.results);
            w.results.clear();
            w.results.compact();
        }
    }
    if (filterResults == null) {
        throw new NullPointerException();
    }
    filterResults.compact();
    if (!batchMode) {
        IJ.showStatus("Summarising results ...");
    }
    // Show a table of the results
    final BenchmarkSpotFilterResult filterResult = summariseResults(filterResults, config, spotFilter, batchSummary);
    if (!batchMode) {
        IJ.showStatus("");
    }
    return filterResult;
}
Also used : ImageStack(ij.ImageStack) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) StandardResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.StandardResultProcedure)

Aggregations

MaximaSpotFilter (uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter)4 Rectangle (java.awt.Rectangle)2 ImageStack (ij.ImageStack)1 FloatProcessor (ij.process.FloatProcessor)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)1 BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)1 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)1 DataFilterSettings (uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterSettings)1 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)1 AverageDataProcessor (uk.ac.sussex.gdsc.smlm.filters.AverageDataProcessor)1 BlockAverageDataProcessor (uk.ac.sussex.gdsc.smlm.filters.BlockAverageDataProcessor)1 CircularMeanDataProcessor (uk.ac.sussex.gdsc.smlm.filters.CircularMeanDataProcessor)1 DataProcessor (uk.ac.sussex.gdsc.smlm.filters.DataProcessor)1 DifferenceSpotFilter (uk.ac.sussex.gdsc.smlm.filters.DifferenceSpotFilter)1 GaussianDataProcessor (uk.ac.sussex.gdsc.smlm.filters.GaussianDataProcessor)1 JurySpotFilter (uk.ac.sussex.gdsc.smlm.filters.JurySpotFilter)1 MedianDataProcessor (uk.ac.sussex.gdsc.smlm.filters.MedianDataProcessor)1 SingleSpotFilter (uk.ac.sussex.gdsc.smlm.filters.SingleSpotFilter)1