Search in sources :

Example 56 with Nullable

use of uk.ac.sussex.gdsc.core.annotation.Nullable in project gdsc-smlm by aherbert.

the class CameraModelFisherInformationAnalysis method createExponents.

@Nullable
private double[] createExponents() {
    final int n = 1 + Math.max(0, settings.getSubDivisions());
    final double minExp = settings.getMinExponent();
    final double maxExp = settings.getMaxExponent();
    final double size = (maxExp - minExp) * n + 1;
    if (size > 100) {
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        gd.addMessage("Number of exponents is " + Math.ceil(size) + ". OK to continue?");
        gd.showDialog();
        if (gd.wasCanceled()) {
            return null;
        }
    }
    final double h = 1.0 / n;
    final DoubleArrayList list = new DoubleArrayList();
    for (int i = 0; ; i++) {
        final double e = minExp + i * h;
        list.add(e);
        if (e >= settings.getMaxExponent()) {
            break;
        }
    }
    return list.toDoubleArray();
}
Also used : NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) DoubleArrayList(it.unimi.dsi.fastutil.doubles.DoubleArrayList) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 57 with Nullable

use of uk.ac.sussex.gdsc.core.annotation.Nullable in project gdsc-smlm by aherbert.

the class BenchmarkFilterAnalysis method findOptimum.

@Nullable
@Override
public SearchResult<FilterScore> findOptimum(double[][] points) {
    gaIteration++;
    SimpleFilterScore max = filterScoreOptimum;
    final FilterScoreResult[] scoreResults = scoreFilters(setStrength(new FilterSet(searchSpaceToFilters(points))), false);
    if (scoreResults == null) {
        return null;
    }
    for (final FilterScoreResult scoreResult : scoreResults) {
        final SimpleFilterScore result = new SimpleFilterScore(scoreResult, true, scoreResult.criteria >= minCriteria);
        if (result.compareTo(max) < 0) {
            max = result;
        }
    }
    filterScoreOptimum = max;
    // Add the best filter to the table
    // This filter may not have been part of the scored subset so use the entire results set for
    // reporting
    final DirectFilter filter = max.result.filter;
    final FractionClassificationResult r = scoreFilter(filter, DEFAULT_MINIMUM_FILTER, gaResultsList, coordinateStore);
    final StringBuilder text = createResult(filter, r);
    add(text, gaIteration);
    gaWindow.accept(text.toString());
    return new SearchResult<>(filter.getParameters(), max);
}
Also used : FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) SearchResult(uk.ac.sussex.gdsc.smlm.search.SearchResult) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 58 with Nullable

use of uk.ac.sussex.gdsc.core.annotation.Nullable in project gdsc-smlm by aherbert.

the class DriftCalculator method createStackImageList.

/**
 * Build a list of suitable stack images.
 *
 * @return the list of suitable stack images.
 */
@Nullable
private static String[] createStackImageList() {
    final int[] idList = WindowManager.getIDList();
    if (idList != null) {
        final String[] list = new String[idList.length];
        int count = 0;
        for (final int id : idList) {
            final ImagePlus imp = WindowManager.getImage(id);
            if (imp != null && imp.getStackSize() > 1) {
                list[count++] = imp.getTitle();
            }
        }
        return Arrays.copyOf(list, count);
    }
    return null;
}
Also used : ImagePlus(ij.ImagePlus) Point(java.awt.Point) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 59 with Nullable

use of uk.ac.sussex.gdsc.core.annotation.Nullable in project gdsc-smlm by aherbert.

the class BenchmarkFilterAnalysis method showOverlay.

/**
 * Show overlay.
 *
 * <ul>
 *
 * <li>Green = TP
 *
 * <li>Red = FP
 *
 * <li>Magenta = FP (Ignored from analysis)
 *
 * <li>Yellow = FN
 *
 * <li>Orange = FN (Outside border)
 *
 * </ul>
 *
 * @param allAssignments The assignments generated from running the filter (or null)
 * @param filter the filter
 * @return The results from running the filter (or null)
 */
@Nullable
@SuppressWarnings("null")
private PreprocessedPeakResult[] showOverlay(List<FractionalAssignment[]> allAssignments, DirectFilter filter) {
    final ImagePlus imp = CreateData.getImage();
    if (imp == null) {
        return null;
    }
    // Run the filter manually to get the results that pass.
    if (allAssignments == null) {
        allAssignments = getAssignments(filter);
    }
    final Overlay o = new Overlay();
    // Do TP
    final IntOpenHashSet actual = new IntOpenHashSet();
    final IntOpenHashSet predicted = new IntOpenHashSet();
    for (final FractionalAssignment[] assignments : allAssignments) {
        if (assignments == null || assignments.length == 0) {
            continue;
        }
        float[] tx = null;
        float[] ty = null;
        int count = 0;
        if (settings.showTP) {
            tx = new float[assignments.length];
            ty = new float[assignments.length];
        }
        int frame = 0;
        for (final FractionalAssignment assignment : assignments) {
            final CustomFractionalAssignment c = (CustomFractionalAssignment) assignment;
            final UniqueIdPeakResult peak = (UniqueIdPeakResult) c.peak;
            final BasePreprocessedPeakResult spot = (BasePreprocessedPeakResult) c.peakResult;
            actual.add(peak.uniqueId);
            predicted.add(spot.getUniqueId());
            frame = spot.getFrame();
            if (settings.showTP) {
                tx[count] = spot.getX();
                ty[count++] = spot.getY();
            }
        }
        if (settings.showTP) {
            SpotFinderPreview.addRoi(frame, o, tx, ty, count, Color.green);
        }
    }
    float[] x = new float[10];
    float[] y = new float[x.length];
    float[] x2 = new float[10];
    float[] y2 = new float[x2.length];
    // Do FP (all remaining results that are not a TP)
    PreprocessedPeakResult[] filterResults = null;
    if (settings.showFP) {
        final MultiPathFilter multiPathFilter = createMpf(filter, DEFAULT_MINIMUM_FILTER);
        filterResults = filterResults(multiPathFilter);
        int frame = 0;
        int c1 = 0;
        int c2 = 0;
        for (final PreprocessedPeakResult filterResult : filterResults) {
            if (frame != filterResult.getFrame()) {
                if (c1 != 0) {
                    SpotFinderPreview.addRoi(frame, o, x, y, c1, Color.red);
                }
                if (c2 != 0) {
                    SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.magenta);
                }
                c1 = c2 = 0;
            }
            frame = filterResult.getFrame();
            if (predicted.contains(filterResult.getUniqueId())) {
                continue;
            }
            if (filterResult.ignore()) {
                if (x2.length == c2) {
                    x2 = Arrays.copyOf(x2, c2 * 2);
                    y2 = Arrays.copyOf(y2, c2 * 2);
                }
                x2[c2] = filterResult.getX();
                y2[c2++] = filterResult.getY();
            } else {
                if (x.length == c1) {
                    x = Arrays.copyOf(x, c1 * 2);
                    y = Arrays.copyOf(y, c1 * 2);
                }
                x[c1] = filterResult.getX();
                y[c1++] = filterResult.getY();
            }
        }
        if (c1 != 0) {
            SpotFinderPreview.addRoi(frame, o, x, y, c1, Color.red);
        }
        if (c2 != 0) {
            SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.magenta);
        }
    }
    // Do FN (all remaining peaks that have not been matched)
    if (settings.showFN) {
        final boolean checkBorder = (filterResult.analysisBorder != null && filterResult.analysisBorder.x != 0);
        final float border;
        final float xlimit;
        final float ylimit;
        if (checkBorder) {
            final Rectangle lastAnalysisBorder = filterResult.analysisBorder;
            border = lastAnalysisBorder.x;
            xlimit = lastAnalysisBorder.x + lastAnalysisBorder.width;
            ylimit = lastAnalysisBorder.y + lastAnalysisBorder.height;
        } else {
            border = xlimit = ylimit = 0;
        }
        // Add the results to the lists
        actualCoordinates.forEach(new CustomIntObjectProcedure(x, y, x2, y2) {

            @Override
            public void accept(int frame, UniqueIdPeakResult[] results) {
                int c1 = 0;
                int c2 = 0;
                if (x.length <= results.length) {
                    x = new float[results.length];
                    y = new float[results.length];
                }
                if (x2.length <= results.length) {
                    x2 = new float[results.length];
                    y2 = new float[results.length];
                }
                for (final UniqueIdPeakResult result : results) {
                    // Ignore those that were matched by TP
                    if (actual.contains(result.uniqueId)) {
                        continue;
                    }
                    if (checkBorder && outsideBorder(result, border, xlimit, ylimit)) {
                        x2[c2] = result.getXPosition();
                        y2[c2++] = result.getYPosition();
                    } else {
                        x[c1] = result.getXPosition();
                        y[c1++] = result.getYPosition();
                    }
                }
                if (c1 != 0) {
                    SpotFinderPreview.addRoi(frame, o, x, y, c1, Color.yellow);
                }
                if (c2 != 0) {
                    SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.orange);
                }
            }
        });
    }
    imp.setOverlay(o);
    return filterResults;
}
Also used : BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) IntOpenHashSet(it.unimi.dsi.fastutil.ints.IntOpenHashSet) PeakFractionalAssignment(uk.ac.sussex.gdsc.smlm.results.filter.PeakFractionalAssignment) FractionalAssignment(uk.ac.sussex.gdsc.core.match.FractionalAssignment) BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.PreprocessedPeakResult) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) Overlay(ij.gui.Overlay) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 60 with Nullable

use of uk.ac.sussex.gdsc.core.annotation.Nullable in project gdsc-smlm by aherbert.

the class GaussianFit method fit.

/**
 * Fits a single 2D Gaussian to the data. The fit is initialised at the highest value and then
 * optimised.
 *
 * <p>Data must be arranged in yx block order, i.e. height rows of width.
 *
 * <p>The angle parameter is only set if using elliptical Gaussian fitting.
 *
 * <p>Note: The returned fit coordinates should be offset by 0.5 if the input data represents
 * pixels
 *
 * @param data the data
 * @param width the width
 * @param height the height
 * @return Array containing the fitted curve data: Background, Amplitude, PosX, PosY, StdDevX,
 *         StdDevY, Angle. Null if no fit is possible.
 */
@Nullable
public double[] fit(float[] data, int width, int height) {
    if (data == null || data.length != width * height) {
        return null;
    }
    // Get the limits
    float max = Float.MIN_VALUE;
    int maxIndex = -1;
    for (int i = data.length; i-- > 0; ) {
        final float f = data[i];
        if (max < f) {
            max = f;
            maxIndex = i;
        }
    }
    if (maxIndex < 0) {
        return null;
    }
    final Gaussian2DFitter gf = createGaussianFitter(false);
    final FitResult fitResult = gf.fit(SimpleArrayUtils.toDouble(data), width, height, new int[] { maxIndex });
    if (fitResult.getStatus() == FitStatus.OK) {
        chiSquared = fitResult.getError();
        final double[] params = fitResult.getParameters();
        // Check bounds
        final double x = params[Gaussian2DFunction.X_POSITION];
        final double y = params[Gaussian2DFunction.Y_POSITION];
        if (x < 0 || x >= width || y < 0 || y >= height) {
            return null;
        }
        // Re-arrange order for backwards compatibility with old code.
        final double background = params[Gaussian2DFunction.BACKGROUND];
        final double intensity = params[Gaussian2DFunction.SIGNAL];
        final double sx = params[Gaussian2DFunction.X_SD];
        final double sy = params[Gaussian2DFunction.Y_SD];
        final double angle = params[Gaussian2DFunction.ANGLE];
        final double amplitude = Gaussian2DPeakResultHelper.getAmplitude(intensity, sx, sy);
        return new double[] { background, amplitude, x, y, sx, sy, angle };
    }
    return null;
}
Also used : Gaussian2DFitter(uk.ac.sussex.gdsc.smlm.fitting.Gaussian2DFitter) FitResult(uk.ac.sussex.gdsc.smlm.fitting.FitResult) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Aggregations

Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)87 Point (java.awt.Point)16 LinkedList (java.util.LinkedList)13 FractionalAssignment (uk.ac.sussex.gdsc.core.match.FractionalAssignment)12 ArrayList (java.util.ArrayList)11 Rectangle (java.awt.Rectangle)10 PeakFractionalAssignment (uk.ac.sussex.gdsc.smlm.results.filter.PeakFractionalAssignment)9 ImagePlus (ij.ImagePlus)8 Plot (ij.gui.Plot)8 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)8 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)8 HistogramPlot (uk.ac.sussex.gdsc.core.ij.HistogramPlot)8 DirectFilter (uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter)8 IDirectFilter (uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter)8 FloatProcessor (ij.process.FloatProcessor)7 MultiPathFilter (uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter)7 List (java.util.List)6 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)6 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)6 ImageProcessor (ij.process.ImageProcessor)5