Search in sources :

Example 21 with DescriptiveStatistics

use of org.apache.commons.math3.stat.descriptive.DescriptiveStatistics in project jmeter by apache.

the class SamplerMetric method resetForTimeInterval.

/**
 * Reset metric except for percentile related data
 */
public synchronized void resetForTimeInterval() {
    switch(globalWindowMode) {
        case FIXED:
            // http://commons.apache.org/proper/commons-math/userguide/stat.html
            break;
        case TIMED:
            for (DescriptiveStatistics stat : windowedStats) {
                stat.clear();
            }
            break;
        default:
    }
    errors.clear();
    successes = 0;
    failures = 0;
    hits = 0;
    sentBytes = 0;
    receivedBytes = 0;
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)

Example 22 with DescriptiveStatistics

use of org.apache.commons.math3.stat.descriptive.DescriptiveStatistics in project groovy by apache.

the class CompilerPerformanceTest method main.

public static void main(String[] args) throws Exception {
    List<File> sources = new ArrayList<>();
    List<URL> classpath = new ArrayList<>();
    boolean isCp = false;
    List<String> argsLeft = new ArrayList<>();
    Collections.addAll(argsLeft, args);
    String outputFile = argsLeft.remove(0);
    for (String arg : argsLeft) {
        if ("-cp".equals(arg)) {
            isCp = true;
        } else if (isCp) {
            for (String s : arg.split(":")) {
                classpath.add(new File(s).toURI().toURL());
            }
            isCp = false;
        } else {
            sources.add(new File(arg));
        }
    }
    ScriptCompilationExecuter executer = new ScriptCompilationExecuter(sources.toArray(new File[sources.size()]), classpath);
    System.out.println("Using Groovy " + GROOVY_VERSION);
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < WARMUP + REPEAT; i++) {
        if (i % 10 == 0) {
            if (i < WARMUP) {
                System.out.println("Warmup #" + (i + 1));
            } else {
                System.out.println("Round #" + (i - WARMUP));
            }
        }
        long dur = executer.execute();
        System.gc();
        System.out.printf("Compile time = %dms%n", dur);
        if (i >= WARMUP) {
            stats.addValue((double) dur);
        }
    }
    System.out.println("Compilation took " + stats.getMean() + "ms ± " + stats.getStandardDeviation() + "ms");
    FileWriter wrt = new FileWriter(new File(outputFile), false);
    wrt.append(String.format("%s;%s;%s\n", GROOVY_VERSION, stats.getMean(), stats.getStandardDeviation()));
    wrt.close();
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) URL(java.net.URL) File(java.io.File)

Example 23 with DescriptiveStatistics

use of org.apache.commons.math3.stat.descriptive.DescriptiveStatistics in project jmeter by apache.

the class TestStatCalculator method testPercentagePointBug.

@Test
@Disabled
public // Disabled due to in progress Bug 61071
void testPercentagePointBug() throws Exception {
    long[] values = new long[] { 10L, 9L, 5L, 6L, 1L, 3L, 8L, 2L, 7L, 4L };
    DescriptiveStatistics statistics = new DescriptiveStatistics();
    for (long l : values) {
        calc.addValue(l);
        statistics.addValue(l);
    }
    assertEquals(9, calc.getPercentPoint(0.8999999).intValue());
    assertEquals(Math.round(statistics.getPercentile(90)), calc.getPercentPoint(0.9).intValue());
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 24 with DescriptiveStatistics

use of org.apache.commons.math3.stat.descriptive.DescriptiveStatistics in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method showDoubleHistogram.

private double[] showDoubleHistogram(StoredDataStatistics[][] stats, final int index, WindowOrganiser wo, double[][] matchScores) {
    final String xLabel = filterCriteria[index].name;
    LowerLimit lower = filterCriteria[index].lower;
    UpperLimit upper = filterCriteria[index].upper;
    double[] jaccard = null;
    double[] metric = null;
    double maxJaccard = 0;
    if (index <= FILTER_PRECISION && (settings.showFilterScoreHistograms || upper.requiresJaccard || lower.requiresJaccard)) {
        // Jaccard score verses the range of the metric
        for (final double[] d : matchScores) {
            if (!Double.isFinite(d[index])) {
                System.out.printf("Error in fit data [%d]: %s%n", index, d[index]);
            }
        }
        // Do not use Double.compare(double, double) so we get exceptions in the sort for inf/nan
        Arrays.sort(matchScores, (o1, o2) -> {
            if (o1[index] < o2[index]) {
                return -1;
            }
            if (o1[index] > o2[index]) {
                return 1;
            }
            return 0;
        });
        final int scoreIndex = FILTER_PRECISION + 1;
        final int n = results.size();
        double tp = 0;
        double fp = 0;
        jaccard = new double[matchScores.length + 1];
        metric = new double[jaccard.length];
        for (int k = 0; k < matchScores.length; k++) {
            final double score = matchScores[k][scoreIndex];
            tp += score;
            fp += (1 - score);
            jaccard[k + 1] = tp / (fp + n);
            metric[k + 1] = matchScores[k][index];
        }
        metric[0] = metric[1];
        maxJaccard = MathUtils.max(jaccard);
        if (settings.showFilterScoreHistograms) {
            final String title = TITLE + " Jaccard " + xLabel;
            final Plot plot = new Plot(title, xLabel, "Jaccard");
            plot.addPoints(metric, jaccard, Plot.LINE);
            // Remove outliers
            final double[] limitsx = MathUtils.limits(metric);
            final Percentile p = new Percentile();
            final double l = p.evaluate(metric, 25);
            final double u = p.evaluate(metric, 75);
            final double iqr = 1.5 * (u - l);
            limitsx[1] = Math.min(limitsx[1], u + iqr);
            plot.setLimits(limitsx[0], limitsx[1], 0, MathUtils.max(jaccard));
            ImageJUtils.display(title, plot, wo);
        }
    }
    // [0] is all
    // [1] is matches
    // [2] is no match
    final StoredDataStatistics s1 = stats[0][index];
    final StoredDataStatistics s2 = stats[1][index];
    final StoredDataStatistics s3 = stats[2][index];
    if (s1.getN() == 0) {
        return new double[4];
    }
    final DescriptiveStatistics d = s1.getStatistics();
    double median = 0;
    Plot plot = null;
    String title = null;
    if (settings.showFilterScoreHistograms) {
        median = d.getPercentile(50);
        final String label = String.format("n = %d. Median = %s nm", s1.getN(), MathUtils.rounded(median));
        final HistogramPlot histogramPlot = new HistogramPlotBuilder(TITLE, s1, xLabel).setMinBinWidth(filterCriteria[index].minBinWidth).setRemoveOutliersOption((filterCriteria[index].restrictRange) ? 1 : 0).setPlotLabel(label).build();
        final PlotWindow plotWindow = histogramPlot.show(wo);
        if (plotWindow == null) {
            IJ.log("Failed to show the histogram: " + xLabel);
            return new double[4];
        }
        title = plotWindow.getTitle();
        // Reverse engineer the histogram settings
        plot = histogramPlot.getPlot();
        final double[] xvalues = histogramPlot.getPlotXValues();
        final int bins = xvalues.length;
        final double yMin = xvalues[0];
        final double binSize = xvalues[1] - xvalues[0];
        final double yMax = xvalues[0] + (bins - 1) * binSize;
        if (s2.getN() > 0) {
            final double[] values = s2.getValues();
            final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
            if (hist[0].length > 0) {
                plot.setColor(Color.red);
                plot.addPoints(hist[0], hist[1], Plot.BAR);
                ImageJUtils.display(title, plot);
            }
        }
        if (s3.getN() > 0) {
            final double[] values = s3.getValues();
            final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
            if (hist[0].length > 0) {
                plot.setColor(Color.blue);
                plot.addPoints(hist[0], hist[1], Plot.BAR);
                ImageJUtils.display(title, plot);
            }
        }
    }
    // Do cumulative histogram
    final double[][] h1 = MathUtils.cumulativeHistogram(s1.getValues(), true);
    final double[][] h2 = MathUtils.cumulativeHistogram(s2.getValues(), true);
    final double[][] h3 = MathUtils.cumulativeHistogram(s3.getValues(), true);
    if (settings.showFilterScoreHistograms) {
        title = TITLE + " Cumul " + xLabel;
        plot = new Plot(title, xLabel, "Frequency");
        // Find limits
        double[] xlimit = MathUtils.limits(h1[0]);
        xlimit = MathUtils.limits(xlimit, h2[0]);
        xlimit = MathUtils.limits(xlimit, h3[0]);
        // Restrict using the inter-quartile range
        if (filterCriteria[index].restrictRange) {
            final double q1 = d.getPercentile(25);
            final double q2 = d.getPercentile(75);
            final double iqr = (q2 - q1) * 2.5;
            xlimit[0] = MathUtils.max(xlimit[0], median - iqr);
            xlimit[1] = MathUtils.min(xlimit[1], median + iqr);
        }
        plot.setLimits(xlimit[0], xlimit[1], 0, 1.05);
        plot.addPoints(h1[0], h1[1], Plot.LINE);
        plot.setColor(Color.red);
        plot.addPoints(h2[0], h2[1], Plot.LINE);
        plot.setColor(Color.blue);
        plot.addPoints(h3[0], h3[1], Plot.LINE);
    }
    // Determine the maximum difference between the TP and FP
    double maxx1 = 0;
    double maxx2 = 0;
    double max1 = 0;
    double max2 = 0;
    // We cannot compute the delta histogram, or use percentiles
    if (s2.getN() == 0) {
        upper = UpperLimit.ZERO;
        lower = LowerLimit.ZERO;
    }
    final boolean requireLabel = (settings.showFilterScoreHistograms && filterCriteria[index].requireLabel);
    if (requireLabel || upper.requiresDeltaHistogram() || lower.requiresDeltaHistogram()) {
        if (s2.getN() != 0 && s3.getN() != 0) {
            final LinearInterpolator li = new LinearInterpolator();
            final PolynomialSplineFunction f1 = li.interpolate(h2[0], h2[1]);
            final PolynomialSplineFunction f2 = li.interpolate(h3[0], h3[1]);
            for (final double x : h1[0]) {
                if (x < h2[0][0] || x < h3[0][0]) {
                    continue;
                }
                try {
                    final double v1 = f1.value(x);
                    final double v2 = f2.value(x);
                    final double diff = v2 - v1;
                    if (diff > 0) {
                        if (max1 < diff) {
                            max1 = diff;
                            maxx1 = x;
                        }
                    } else if (max2 > diff) {
                        max2 = diff;
                        maxx2 = x;
                    }
                } catch (final OutOfRangeException ex) {
                    // Because we reached the end
                    break;
                }
            }
        }
    }
    if (plot != null) {
        // We use bins=1 on charts where we do not need a label
        if (requireLabel) {
            final String label = String.format("Max+ %s @ %s, Max- %s @ %s", MathUtils.rounded(max1), MathUtils.rounded(maxx1), MathUtils.rounded(max2), MathUtils.rounded(maxx2));
            plot.setColor(Color.black);
            plot.addLabel(0, 0, label);
        }
        ImageJUtils.display(title, plot, wo);
    }
    // Now compute the bounds using the desired limit
    double lowerBound;
    double upperBound;
    switch(lower) {
        case MAX_NEGATIVE_CUMUL_DELTA:
            // Switch to percentiles if we have no delta histogram
            if (maxx2 < 0) {
                lowerBound = maxx2;
                break;
            }
        // fall-through
        case ONE_PERCENT:
            lowerBound = getPercentile(h2, 0.01);
            break;
        case MIN:
            lowerBound = getPercentile(h2, 0.0);
            break;
        case ZERO:
            lowerBound = 0;
            break;
        case HALF_MAX_JACCARD_VALUE:
            lowerBound = getXValue(metric, jaccard, maxJaccard * 0.5);
            break;
        default:
            throw new IllegalStateException("Missing lower limit method");
    }
    switch(upper) {
        case MAX_POSITIVE_CUMUL_DELTA:
            // Switch to percentiles if we have no delta histogram
            if (maxx1 > 0) {
                upperBound = maxx1;
                break;
            }
        // fall-through
        case NINETY_NINE_PERCENT:
            upperBound = getPercentile(h2, 0.99);
            break;
        case NINETY_NINE_NINE_PERCENT:
            upperBound = getPercentile(h2, 0.999);
            break;
        case ZERO:
            upperBound = 0;
            break;
        case MAX_JACCARD2:
            upperBound = getXValue(metric, jaccard, maxJaccard) * 2;
            // System.out.printf("MaxJ = %.4f @ %.3f\n", maxJ, u / 2);
            break;
        default:
            throw new IllegalStateException("Missing upper limit method");
    }
    final double min = getPercentile(h1, 0);
    final double max = getPercentile(h1, 1);
    return new double[] { lowerBound, upperBound, min, max };
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Percentile(org.apache.commons.math3.stat.descriptive.rank.Percentile) Plot(ij.gui.Plot) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) PlotWindow(ij.gui.PlotWindow) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) OutOfRangeException(org.apache.commons.math3.exception.OutOfRangeException)

Example 25 with DescriptiveStatistics

use of org.apache.commons.math3.stat.descriptive.DescriptiveStatistics in project GDSC-SMLM by aherbert.

the class PsfCreator method getLimits.

/**
 * Get the limits of the array ignoring outliers more than 1.5x the inter quartile range.
 *
 * @param data the data
 * @return the limits
 */
private static double[] getLimits(double[] data) {
    final double[] limits = MathUtils.limits(data);
    final DescriptiveStatistics stats = new DescriptiveStatistics(data);
    final double lower = stats.getPercentile(25);
    final double upper = stats.getPercentile(75);
    final double iqr = (upper - lower) * 2;
    limits[0] = Math.max(lower - iqr, limits[0]);
    limits[1] = Math.min(upper + iqr, limits[1]);
    return limits;
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)

Aggregations

DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)75 ArrayList (java.util.ArrayList)12 TException (org.apache.thrift.TException)6 Plot (ij.gui.Plot)5 List (java.util.List)5 Test (org.junit.jupiter.api.Test)5 JMeterTransactions (uk.co.automatictester.lightning.data.JMeterTransactions)5 PinotDataBuffer (com.linkedin.pinot.core.segment.memory.PinotDataBuffer)4 Rectangle (java.awt.Rectangle)4 MersenneTwister (org.apache.commons.math3.random.MersenneTwister)4 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)4 Percentile (org.apache.commons.math3.stat.descriptive.rank.Percentile)4 PeakResult (gdsc.smlm.results.PeakResult)3 ImagePlus (ij.ImagePlus)3 GenericDialog (ij.gui.GenericDialog)3 ImageProcessor (ij.process.ImageProcessor)3 File (java.io.File)3 WeightedObservedPoint (org.apache.commons.math3.fitting.WeightedObservedPoint)3 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)2 ImageStack (ij.ImageStack)2