Search in sources :

Example 1 with Precision

use of org.apache.commons.math3.util.Precision in project EnrichmentMapApp by BaderLab.

the class MannWhitneyUTestSided method calculateAsymptoticPValue.

/**
     * @param Umin smallest Mann-Whitney U value
     * @param Umin smallest Mann-Whitney U1 value
     * @param Umin smallest Mann-Whitney U2 value
     * @param n1 number of subjects in first sample
     * @param n2 number of subjects in second sample
     * @return two-sided asymptotic p-value
     * @throws ConvergenceException if the p-value can not be computed
     * due to a convergence error
     * @throws MaxCountExceededException if the maximum number of
     * iterations is exceeded
     */
private double calculateAsymptoticPValue(final double Umin, final double U1, final double U2, final int n1, final int n2, final Type side) throws ConvergenceException, MaxCountExceededException {
    /* long multiplication to avoid overflow (double not used due to efficiency
         * and to avoid precision loss)
         */
    final long n1n2prod = (long) n1 * n2;
    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;
    final double z = (Umin - EU) / FastMath.sqrt(VarU);
    // No try-catch or advertised exception because args are valid
    final NormalDistribution standardNormal = new NormalDistribution(0, 1);
    double p = 2 * standardNormal.cumulativeProbability(z);
    if (side == Type.TWO_SIDED) {
        return p;
    }
    if (side == Type.LESS) {
        if (U1 < U2) {
            return 0.5 * p;
        } else {
            return 1.0 - (0.5 * p);
        }
    } else {
        if (U1 > U2) {
            return 0.5 * p;
        } else {
            return 1.0 - (0.5 * p);
        }
    }
}
Also used : NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution)

Example 2 with Precision

use of org.apache.commons.math3.util.Precision in project GDSC-SMLM by aherbert.

the class FIRE method showDialog.

private boolean showDialog() {
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addMessage("Compute the resolution using Fourier Ring Correlation");
    gd.addHelp(About.HELP_URL);
    boolean single = results2 == null;
    gd.addMessage("Image construction options:");
    gd.addChoice("Image_scale", SCALE_ITEMS, SCALE_ITEMS[imageScaleIndex]);
    gd.addChoice("Auto_image_size", IMAGE_SIZE_ITEMS, IMAGE_SIZE_ITEMS[imageSizeIndex]);
    if (extraOptions)
        gd.addCheckbox("Use_signal (if present)", useSignal);
    gd.addNumericField("Max_per_bin", maxPerBin, 0);
    gd.addMessage("Fourier options:");
    String[] fourierMethodNames = SettingsManager.getNames((Object[]) FRC.FourierMethod.values());
    gd.addChoice("Fourier_method", fourierMethodNames, fourierMethodNames[fourierMethodIndex]);
    String[] samplingMethodNames = SettingsManager.getNames((Object[]) FRC.SamplingMethod.values());
    gd.addChoice("Sampling_method", samplingMethodNames, samplingMethodNames[samplingMethodIndex]);
    gd.addSlider("Sampling_factor", 0.2, 4, perimeterSamplingFactor);
    gd.addMessage("FIRE options:");
    String[] thresholdMethodNames = SettingsManager.getNames((Object[]) FRC.ThresholdMethod.values());
    gd.addChoice("Threshold_method", thresholdMethodNames, thresholdMethodNames[thresholdMethodIndex]);
    gd.addCheckbox("Show_FRC_curve", showFRCCurve);
    if (single) {
        gd.addMessage("For single datasets:");
        Label l = (Label) gd.getMessage();
        gd.addNumericField("Block_size", blockSize, 0);
        gd.addCheckbox("Random_split", randomSplit);
        gd.addNumericField("Repeats", repeats, 0);
        gd.addCheckbox("Show_FRC_curve_repeats", showFRCCurveRepeats);
        gd.addCheckbox("Show_FRC_time_evolution", showFRCTimeEvolution);
        gd.addCheckbox("Spurious correlation correction", spuriousCorrelationCorrection);
        gd.addNumericField("Q-value", qValue, 3);
        gd.addNumericField("Precision_Mean", mean, 2, 6, "nm");
        gd.addNumericField("Precision_Sigma", sigma, 2, 6, "nm");
        if (extraOptions)
            gd.addNumericField("Threads", getLastNThreads(), 0);
        // Rearrange the dialog
        if (gd.getLayout() != null) {
            GridBagLayout grid = (GridBagLayout) gd.getLayout();
            int xOffset = 0, yOffset = 0;
            int lastY = -1, rowCount = 0;
            for (Component comp : gd.getComponents()) {
                // Check if this should be the second major column
                if (comp == l) {
                    xOffset += 2;
                    // Skip title row
                    yOffset = yOffset - rowCount + 1;
                }
                // Reposition the field
                GridBagConstraints c = grid.getConstraints(comp);
                if (lastY != c.gridy)
                    rowCount++;
                lastY = c.gridy;
                c.gridx = c.gridx + xOffset;
                c.gridy = c.gridy + yOffset;
                c.insets.left = c.insets.left + 10 * xOffset;
                c.insets.top = 0;
                c.insets.bottom = 0;
                grid.setConstraints(comp, c);
            }
            if (IJ.isLinux())
                gd.setBackground(new Color(238, 238, 238));
        }
    }
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    imageScaleIndex = gd.getNextChoiceIndex();
    imageSizeIndex = gd.getNextChoiceIndex();
    if (extraOptions)
        myUseSignal = useSignal = gd.getNextBoolean();
    maxPerBin = Math.abs((int) gd.getNextNumber());
    fourierMethodIndex = gd.getNextChoiceIndex();
    fourierMethod = FourierMethod.values()[fourierMethodIndex];
    samplingMethodIndex = gd.getNextChoiceIndex();
    samplingMethod = SamplingMethod.values()[samplingMethodIndex];
    perimeterSamplingFactor = gd.getNextNumber();
    thresholdMethodIndex = gd.getNextChoiceIndex();
    thresholdMethod = FRC.ThresholdMethod.values()[thresholdMethodIndex];
    showFRCCurve = gd.getNextBoolean();
    if (single) {
        blockSize = Math.max(1, (int) gd.getNextNumber());
        randomSplit = gd.getNextBoolean();
        repeats = Math.max(1, (int) gd.getNextNumber());
        showFRCCurveRepeats = gd.getNextBoolean();
        showFRCTimeEvolution = gd.getNextBoolean();
        spuriousCorrelationCorrection = gd.getNextBoolean();
        qValue = Math.abs(gd.getNextNumber());
        mean = Math.abs(gd.getNextNumber());
        sigma = Math.abs(gd.getNextNumber());
        if (extraOptions) {
            setThreads((int) gd.getNextNumber());
            lastNThreads = this.nThreads;
        }
    }
    // Check arguments
    try {
        Parameters.isAboveZero("Perimeter sampling factor", perimeterSamplingFactor);
        if (single && spuriousCorrelationCorrection) {
            Parameters.isAboveZero("Q-value", qValue);
            Parameters.isAboveZero("Precision Mean", mean);
            Parameters.isAboveZero("Precision Sigma", sigma);
            // Set these for use in FIRE computation 
            setCorrectionParameters(qValue, mean, sigma);
        }
    } catch (IllegalArgumentException e) {
        IJ.error(TITLE, e.getMessage());
        return false;
    }
    return true;
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) GridBagLayout(java.awt.GridBagLayout) Color(java.awt.Color) Label(java.awt.Label) NonBlockingExtendedGenericDialog(ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog) Component(java.awt.Component) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint)

Example 3 with Precision

use of org.apache.commons.math3.util.Precision in project GDSC-SMLM by aherbert.

the class FIRE method showQEstimationInputDialog.

private boolean showQEstimationInputDialog() {
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(About.HELP_URL);
    // Build a list of all images with a region ROI
    List<String> titles = new LinkedList<String>();
    if (WindowManager.getWindowCount() > 0) {
        for (int imageID : WindowManager.getIDList()) {
            ImagePlus imp = WindowManager.getImage(imageID);
            if (imp != null && imp.getRoi() != null && imp.getRoi().isArea())
                titles.add(imp.getTitle());
        }
    }
    gd.addMessage("Estimate the blinking correction parameter Q for Fourier Ring Correlation");
    ResultsManager.addInput(gd, inputOption, InputSource.MEMORY);
    if (!titles.isEmpty())
        gd.addCheckbox((titles.size() == 1) ? "Use_ROI" : "Choose_ROI", chooseRoi);
    gd.addMessage("Image construction options:");
    //gd.addCheckbox("Use_signal (if present)", useSignal);
    gd.addChoice("Image_scale", SCALE_ITEMS, SCALE_ITEMS[imageScaleIndex]);
    gd.addChoice("Auto_image_size", IMAGE_SIZE_ITEMS, IMAGE_SIZE_ITEMS[imageSizeIndex]);
    gd.addNumericField("Block_size", blockSize, 0);
    gd.addCheckbox("Random_split", randomSplit);
    gd.addNumericField("Max_per_bin", maxPerBin, 0);
    gd.addMessage("Fourier options:");
    String[] fourierMethodNames = SettingsManager.getNames((Object[]) FRC.FourierMethod.values());
    gd.addChoice("Fourier_method", fourierMethodNames, fourierMethodNames[fourierMethodIndex]);
    String[] samplingMethodNames = SettingsManager.getNames((Object[]) FRC.SamplingMethod.values());
    gd.addChoice("Sampling_method", samplingMethodNames, samplingMethodNames[samplingMethodIndex]);
    gd.addSlider("Sampling_factor", 0.2, 4, perimeterSamplingFactor);
    gd.addMessage("Estimation options:");
    String[] thresholdMethodNames = SettingsManager.getNames((Object[]) FRC.ThresholdMethod.values());
    gd.addChoice("Threshold_method", thresholdMethodNames, thresholdMethodNames[thresholdMethodIndex]);
    String[] precisionMethodNames = SettingsManager.getNames((Object[]) PrecisionMethod.values());
    gd.addChoice("Precision_method", precisionMethodNames, precisionMethodNames[precisionMethodIndex]);
    gd.addNumericField("Precision_Mean", mean, 2, 6, "nm");
    gd.addNumericField("Precision_Sigma", sigma, 2, 6, "nm");
    gd.addCheckbox("Sample_decay", sampleDecay);
    gd.addCheckbox("LOESS_smoothing", loessSmoothing);
    gd.addCheckbox("Fit_precision", fitPrecision);
    gd.addSlider("MinQ", 0, 0.4, minQ);
    gd.addSlider("MaxQ", 0.1, 0.5, maxQ);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    inputOption = ResultsManager.getInputSource(gd);
    if (!titles.isEmpty())
        chooseRoi = gd.getNextBoolean();
    //useSignal = gd.getNextBoolean();
    imageScaleIndex = gd.getNextChoiceIndex();
    imageSizeIndex = gd.getNextChoiceIndex();
    blockSize = Math.max(1, (int) gd.getNextNumber());
    randomSplit = gd.getNextBoolean();
    maxPerBin = Math.abs((int) gd.getNextNumber());
    fourierMethodIndex = gd.getNextChoiceIndex();
    fourierMethod = FourierMethod.values()[fourierMethodIndex];
    samplingMethodIndex = gd.getNextChoiceIndex();
    samplingMethod = SamplingMethod.values()[samplingMethodIndex];
    perimeterSamplingFactor = gd.getNextNumber();
    thresholdMethodIndex = gd.getNextChoiceIndex();
    thresholdMethod = FRC.ThresholdMethod.values()[thresholdMethodIndex];
    precisionMethodIndex = gd.getNextChoiceIndex();
    precisionMethod = PrecisionMethod.values()[precisionMethodIndex];
    mean = Math.abs(gd.getNextNumber());
    sigma = Math.abs(gd.getNextNumber());
    sampleDecay = gd.getNextBoolean();
    loessSmoothing = gd.getNextBoolean();
    fitPrecision = gd.getNextBoolean();
    minQ = Maths.clip(0, 0.5, gd.getNextNumber());
    maxQ = Maths.clip(0, 0.5, gd.getNextNumber());
    // Check arguments
    try {
        Parameters.isAboveZero("Perimeter sampling factor", perimeterSamplingFactor);
        if (precisionMethod == PrecisionMethod.FIXED) {
            Parameters.isAboveZero("Precision Mean", mean);
            Parameters.isAboveZero("Precision Sigma", sigma);
        }
        Parameters.isAbove("MaxQ", maxQ, minQ);
    } catch (IllegalArgumentException e) {
        IJ.error(TITLE, e.getMessage());
        return false;
    }
    if (!titles.isEmpty() && chooseRoi) {
        if (titles.size() == 1) {
            roiImage = titles.get(0);
            Recorder.recordOption("Image", roiImage);
        } else {
            String[] items = titles.toArray(new String[titles.size()]);
            gd = new ExtendedGenericDialog(TITLE);
            gd.addMessage("Select the source image for the ROI");
            gd.addChoice("Image", items, roiImage);
            gd.showDialog();
            if (gd.wasCanceled())
                return false;
            roiImage = gd.getNextChoice();
        }
        ImagePlus imp = WindowManager.getImage(roiImage);
        roiBounds = imp.getRoi().getBounds();
        roiImageWidth = imp.getWidth();
        roiImageHeight = imp.getHeight();
    } else {
        roiBounds = null;
    }
    return true;
}
Also used : NonBlockingExtendedGenericDialog(ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog) ImagePlus(ij.ImagePlus) LinkedList(java.util.LinkedList) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint)

Example 4 with Precision

use of org.apache.commons.math3.util.Precision in project GDSC-SMLM by aherbert.

the class FIRE method canCalculatePrecision.

private boolean canCalculatePrecision(MemoryPeakResults results) {
    // Calibration is required to compute the precision
    Calibration cal = results.getCalibration();
    if (cal == null)
        return false;
    if (!cal.hasNmPerPixel() || !cal.hasGain() || !cal.hasEMCCD())
        return false;
    // Check all have a width and signal
    PeakResult[] data = results.toArray();
    for (int i = 0; i < data.length; i++) {
        PeakResult p = data[i];
        if (p.getSD() <= 0 || p.getSignal() <= 0)
            return true;
    }
    // Check for variable width that is not 1 and a variable signal
    for (int i = 0; i < data.length; i++) {
        PeakResult p = data[i];
        // Check this is valid
        if (p.getSD() != 1) {
            // Check the rest for a different value
            float w1 = p.getSD();
            float s1 = p.getSignal();
            for (int j = i + 1; j < data.length; j++) {
                PeakResult p2 = data[j];
                if (p2.getSD() != 1 && p2.getSD() != w1 && p2.getSignal() != s1)
                    return true;
            }
            // All the results are the same, this is not valid
            break;
        }
    }
    return false;
}
Also used : Calibration(gdsc.smlm.results.Calibration) PeakResult(gdsc.smlm.results.PeakResult) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint)

Example 5 with Precision

use of org.apache.commons.math3.util.Precision in project GDSC-SMLM by aherbert.

the class HysteresisFilter method getSearchDistanceUsingCandidates.

/**
	 * Find average precision of the candidates and use it for the search
	 * distance
	 * 
	 * @param peakResults
	 * @param candidates
	 * @return
	 */
private double getSearchDistanceUsingCandidates(MemoryPeakResults peakResults, LinkedList<PeakResult> candidates) {
    SummaryStatistics stats = new SummaryStatistics();
    final double nmPerPixel = peakResults.getNmPerPixel();
    final double gain = peakResults.getGain();
    final boolean emCCD = peakResults.isEMCCD();
    for (PeakResult peakResult : candidates) {
        stats.addValue(peakResult.getPrecision(nmPerPixel, gain, emCCD));
    }
    double distanceThreshold = stats.getMean() * searchDistance / nmPerPixel;
    return distanceThreshold;
}
Also used : SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) PeakResult(gdsc.smlm.results.PeakResult)

Aggregations

WeightedObservedPoint (org.apache.commons.math3.fitting.WeightedObservedPoint)8 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)7 PeakResult (gdsc.smlm.results.PeakResult)6 ArrayList (java.util.ArrayList)6 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)6 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)5 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)5 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)5 ClusterPoint (gdsc.core.clustering.ClusterPoint)4 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)4 MultivariateMatrixFunction (org.apache.commons.math3.analysis.MultivariateMatrixFunction)4 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)4 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)4 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)4 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)4 DiagonalMatrix (org.apache.commons.math3.linear.DiagonalMatrix)4 Statistics (gdsc.core.utils.Statistics)3 Plot2 (ij.gui.Plot2)3 WindowOrganiser (ij.plugin.WindowOrganiser)3 BasePoint (gdsc.core.match.BasePoint)2