Example usage for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getMax

List of usage examples for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getMax

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getMax.

Prototype

public double getMax() 

Source Link

Document

Returns the maximum of the available values

Usage

From source file:com.github.jessemull.microflex.stat.statbiginteger.MaxBigIntegerTest.java

/**
 * Tests the aggregated plate statistics method using the values between the indices of
 * the array.//from ww  w  .  j  a  v  a  2 s  . c  om
 */
@Test
public void testAggregatedSetArrayIndices() {

    int size = arrayIndices[0].first().size();
    int begin = random.nextInt(size - 5);
    int end = (begin + 4) + random.nextInt(size - (begin + 4) + 1);

    WellSetBigInteger[] setArrayIndices = new WellSetBigInteger[arrayIndices.length];

    for (int i = 0; i < setArrayIndices.length; i++) {
        setArrayIndices[i] = arrayIndices[i].dataSet();
    }

    Map<WellSetBigInteger, BigDecimal> aggregatedReturnedMap = max.setsAggregated(setArrayIndices, begin,
            end - begin);
    Map<WellSetBigInteger, BigDecimal> aggregatedResultMap = new TreeMap<WellSetBigInteger, BigDecimal>();

    for (WellSetBigInteger set : setArrayIndices) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();

        for (WellBigInteger well : set) {
            resultList.addAll(well.toBigDecimal().subList(begin, end));
        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMax();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble);

        aggregatedResultMap.put(set, aggregatedResult);
    }

    for (WellSetBigInteger plate : setArrayIndices) {

        BigDecimal result = aggregatedResultMap.get(plate);
        BigDecimal returned = aggregatedReturnedMap.get(plate);
        BigDecimal[] corrected = correctRoundingErrors(result, returned);

        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.itemanalysis.jmetrik.stats.descriptives.DescriptiveAnalysis.java

public void publishTable(VariableAttributes v) {
    TextTable table = null;/*from w w  w  .  ja  v  a 2 s  . c  o  m*/
    TextTableColumnFormat[] cformats = new TextTableColumnFormat[2];
    cformats[0] = new TextTableColumnFormat();
    cformats[0].setStringFormat(15, TextTableColumnFormat.OutputAlignment.LEFT);
    cformats[1] = new TextTableColumnFormat();
    cformats[1].setDoubleFormat(10, 4, TextTableColumnFormat.OutputAlignment.RIGHT);

    DescriptiveStatistics temp = data.get(v);
    table = new TextTable();
    table.addAllColumnFormats(cformats, 17);
    table.getRowAt(0).addHeader(0, 2, v.getName().toString(), TextTablePosition.CENTER);
    table.getRowAt(1).addHorizontalRule(0, 2, "=");
    table.getRowAt(2).addHeader(0, 1, "Statistic", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(1, 1, "Value", TextTablePosition.CENTER);
    table.getRowAt(3).addHorizontalRule(0, 2, "-");

    table.addStringAt(4, 0, "N");
    table.addDoubleAt(4, 1, maxProgress);
    table.addStringAt(5, 0, "Valid N");
    table.addDoubleAt(5, 1, temp.getN());
    table.addStringAt(6, 0, "Min");
    table.addDoubleAt(6, 1, temp.getMin());
    table.addStringAt(7, 0, "Max");
    table.addDoubleAt(7, 1, temp.getMax());
    table.addStringAt(8, 0, "Mean");
    table.addDoubleAt(8, 1, temp.getMean());
    table.addStringAt(9, 0, "Std. Dev.");
    table.addDoubleAt(9, 1, temp.getStandardDeviation());
    table.addStringAt(10, 0, "Skewness");
    table.addDoubleAt(10, 1, temp.getSkewness());
    table.addStringAt(11, 0, "Kurtosis");
    table.addDoubleAt(11, 1, temp.getKurtosis());
    table.addStringAt(12, 0, "First Quartile");
    table.addDoubleAt(12, 1, temp.getPercentile(25));
    table.addStringAt(13, 0, "Median");
    table.addDoubleAt(13, 1, temp.getPercentile(50));
    table.addStringAt(14, 0, "Third Quartile");
    table.addDoubleAt(14, 1, temp.getPercentile(75));
    table.addStringAt(15, 0, "IQR");
    table.addDoubleAt(15, 1, temp.getPercentile(75) - temp.getPercentile(25));
    table.getRowAt(16).addHorizontalRule(0, 2, "=");

    publish(table.toString() + "\n");

}

From source file:gdsc.smlm.ij.plugins.SpotInspector.java

public void run(String arg) {
    if (MemoryPeakResults.countMemorySize() == 0) {
        IJ.error(TITLE, "No localisations in memory");
        return;//from   ww  w . j a  v  a  2  s  . c  o  m
    }

    if (!showDialog())
        return;

    // Load the results
    results = ResultsManager.loadInputResults(inputOption, false);
    if (results == null || results.size() == 0) {
        IJ.error(TITLE, "No results could be loaded");
        IJ.showStatus("");
        return;
    }

    // Check if the original image is open
    ImageSource source = results.getSource();
    if (source == null) {
        IJ.error(TITLE, "Unknown original source image");
        return;
    }
    source = source.getOriginal();
    if (!source.open()) {
        IJ.error(TITLE, "Cannot open original source image: " + source.toString());
        return;
    }
    final float stdDevMax = getStandardDeviation(results);
    if (stdDevMax < 0) {
        // TODO - Add dialog to get the initial peak width
        IJ.error(TITLE, "Fitting configuration (for initial peak width) is not available");
        return;
    }

    // Rank spots
    rankedResults = new ArrayList<PeakResultRank>(results.size());
    final double a = results.getNmPerPixel();
    final double gain = results.getGain();
    final boolean emCCD = results.isEMCCD();

    for (PeakResult r : results.getResults()) {
        float[] score = getScore(r, a, gain, emCCD, stdDevMax);
        rankedResults.add(new PeakResultRank(r, score[0], score[1]));
    }
    Collections.sort(rankedResults);

    // Prepare results table. Get bias if necessary
    if (showCalibratedValues) {
        // Get a bias if required
        Calibration calibration = results.getCalibration();
        if (calibration.bias == 0) {
            GenericDialog gd = new GenericDialog(TITLE);
            gd.addMessage("Calibrated results requires a camera bias");
            gd.addNumericField("Camera_bias (ADUs)", calibration.bias, 2);
            gd.showDialog();
            if (!gd.wasCanceled()) {
                calibration.bias = Math.abs(gd.getNextNumber());
            }
        }
    }

    IJTablePeakResults table = new IJTablePeakResults(false, results.getName(), true);
    table.copySettings(results);
    table.setTableTitle(TITLE);
    table.setAddCounter(true);
    table.setShowCalibratedValues(showCalibratedValues);
    table.begin();

    // Add a mouse listener to jump to the frame for the clicked line
    textPanel = table.getResultsWindow().getTextPanel();

    // We must ignore old instances of this class from the mouse listeners
    id = ++currentId;
    textPanel.addMouseListener(this);

    // Add results to the table
    int n = 0;
    for (PeakResultRank rank : rankedResults) {
        rank.rank = n++;
        PeakResult r = rank.peakResult;
        table.add(r.peak, r.origX, r.origY, r.origValue, r.error, r.noise, r.params, r.paramsStdDev);
    }
    table.end();

    if (plotScore || plotHistogram) {
        // Get values for the plots
        float[] xValues = null, yValues = null;
        double yMin, yMax;

        int spotNumber = 0;
        xValues = new float[rankedResults.size()];
        yValues = new float[xValues.length];
        for (PeakResultRank rank : rankedResults) {
            xValues[spotNumber] = spotNumber + 1;
            yValues[spotNumber++] = recoverScore(rank.score);
        }

        // Set the min and max y-values using 1.5 x IQR 
        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (float v : yValues)
            stats.addValue(v);
        if (removeOutliers) {
            double lower = stats.getPercentile(25);
            double upper = stats.getPercentile(75);
            double iqr = upper - lower;

            yMin = FastMath.max(lower - iqr, stats.getMin());
            yMax = FastMath.min(upper + iqr, stats.getMax());

            IJ.log(String.format("Data range: %f - %f. Plotting 1.5x IQR: %f - %f", stats.getMin(),
                    stats.getMax(), yMin, yMax));
        } else {
            yMin = stats.getMin();
            yMax = stats.getMax();

            IJ.log(String.format("Data range: %f - %f", yMin, yMax));
        }

        plotScore(xValues, yValues, yMin, yMax);
        plotHistogram(yValues, yMin, yMax);
    }

    // Extract spots into a stack
    final int w = source.getWidth();
    final int h = source.getHeight();
    final int size = 2 * radius + 1;
    ImageStack spots = new ImageStack(size, size, rankedResults.size());

    // To assist the extraction of data from the image source, process them in time order to allow 
    // frame caching. Then set the appropriate slice in the result stack
    Collections.sort(rankedResults, new Comparator<PeakResultRank>() {
        public int compare(PeakResultRank o1, PeakResultRank o2) {
            if (o1.peakResult.peak < o2.peakResult.peak)
                return -1;
            if (o1.peakResult.peak > o2.peakResult.peak)
                return 1;
            return 0;
        }
    });

    for (PeakResultRank rank : rankedResults) {
        PeakResult r = rank.peakResult;

        // Extract image
        // Note that the coordinates are relative to the middle of the pixel (0.5 offset)
        // so do not round but simply convert to int
        final int x = (int) (r.params[Gaussian2DFunction.X_POSITION]);
        final int y = (int) (r.params[Gaussian2DFunction.Y_POSITION]);

        // Extract a region but crop to the image bounds
        int minX = x - radius;
        int minY = y - radius;
        int maxX = FastMath.min(x + radius + 1, w);
        int maxY = FastMath.min(y + radius + 1, h);

        int padX = 0, padY = 0;
        if (minX < 0) {
            padX = -minX;
            minX = 0;
        }
        if (minY < 0) {
            padY = -minY;
            minY = 0;
        }
        int sizeX = maxX - minX;
        int sizeY = maxY - minY;

        float[] data = source.get(r.peak, new Rectangle(minX, minY, sizeX, sizeY));
        // Prevent errors with missing data
        if (data == null)
            data = new float[sizeX * sizeY];
        ImageProcessor spotIp = new FloatProcessor(sizeX, sizeY, data, null);

        // Pad if necessary, i.e. the crop is too small for the stack
        if (padX > 0 || padY > 0 || sizeX < size || sizeY < size) {
            ImageProcessor spotIp2 = spotIp.createProcessor(size, size);
            spotIp2.insert(spotIp, padX, padY);
            spotIp = spotIp2;
        }
        int slice = rank.rank + 1;
        spots.setPixels(spotIp.getPixels(), slice);
        spots.setSliceLabel(Utils.rounded(rank.originalScore), slice);
    }

    source.close();

    ImagePlus imp = Utils.display(TITLE, spots);
    imp.setRoi((PointRoi) null);

    // Make bigger      
    for (int i = 10; i-- > 0;)
        imp.getWindow().getCanvas().zoomIn(imp.getWidth() / 2, imp.getHeight() / 2);
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.Step6GraphTransitivityCleaner.java

public GraphCleaningResults processSingleFile(File file, File outputDir, String prefix,
        Boolean collectGeneratedArgumentPairs) throws Exception {
    GraphCleaningResults result = new GraphCleaningResults();

    File outFileTable = new File(outputDir, prefix + file.getName() + "_table.csv");
    File outFileInfo = new File(outputDir, prefix + file.getName() + "_info.txt");

    PrintStream psTable = new PrintStream(new FileOutputStream(outFileTable));
    PrintStream psInfo = new PrintStream(new FileOutputStream(outFileInfo));

    // load one topic/side
    List<AnnotatedArgumentPair> pairs = new ArrayList<>(
            (List<AnnotatedArgumentPair>) XStreamTools.getXStream().fromXML(file));

    int fullDataSize = pairs.size();

    // filter out missing gold data
    Iterator<AnnotatedArgumentPair> iterator = pairs.iterator();
    while (iterator.hasNext()) {
        AnnotatedArgumentPair pair = iterator.next();
        if (pair.getGoldLabel() == null) {
            iterator.remove();//ww  w  .ja v  a  2s  .  c  o  m
        }
        // or we want to completely remove equal edges in advance!
        else if (this.removeEqualEdgesParam && "equal".equals(pair.getGoldLabel())) {
            iterator.remove();
        }
    }

    // sort pairs by their weight
    this.argumentPairListSorter.sortArgumentPairs(pairs);

    int preFilteredDataSize = pairs.size();

    // compute correlation between score threshold and number of removed edges
    double[] correlationEdgeWeights = new double[pairs.size()];
    double[] correlationRemovedEdges = new double[pairs.size()];

    // only cycles of length 0 to 5 are interesting (5+ are too big)
    Range<Integer> range = Range.between(0, 5);

    psTable.print(
            "EdgeWeightThreshold\tPairs\tignoredEdgesCount\tIsDAG\tTransitivityScoreMean\tTransitivityScoreMax\tTransitivityScoreSamples\tEdges\tNodes\t");
    for (int j = range.getMinimum(); j <= range.getMaximum(); j++) {
        psTable.print("Cycles_" + j + "\t");
    }
    psTable.println();

    // store the indices of all pairs (edges) that have been successfully added without
    // generating cycles
    TreeSet<Integer> addedPairsIndices = new TreeSet<>();

    // number of edges ignored as they generated cycles
    int ignoredEdgesCount = 0;

    Graph lastGraph = null;

    // flag that the first cycle was already processed
    boolean firstCycleAlreadyHit = false;

    for (int i = 1; i < pairs.size(); i++) {
        // now filter the finalArgumentPairList and add only pairs that have not generated cycles
        List<AnnotatedArgumentPair> subList = new ArrayList<>();

        for (Integer index : addedPairsIndices) {
            subList.add(pairs.get(index));
        }

        // and add the current at the end
        subList.add(pairs.get(i));

        // what is the current lowest value of a pair weight?
        double weakestEdgeWeight = computeEdgeWeight(subList.get(subList.size() - 1), LAMBDA_PENALTY);

        //            Graph graph = buildGraphFromArgumentPairs(finalArgumentPairList);
        int numberOfLoops;

        // map for storing cycles by their length
        TreeMap<Integer, TreeSet<String>> lengthCyclesMap = new TreeMap<>();

        Graph graph = buildGraphFromArgumentPairs(subList);

        lastGraph = graph;

        List<List<Object>> cyclesInGraph = findCyclesInGraph(graph);

        DescriptiveStatistics transitivityScore = new DescriptiveStatistics();

        if (cyclesInGraph.isEmpty()) {
            // we have DAG
            transitivityScore = computeTransitivityScores(graph);

            // update results
            result.maxTransitivityScore = (int) transitivityScore.getMax();
            result.avgTransitivityScore = transitivityScore.getMean();
        }

        numberOfLoops = cyclesInGraph.size();

        // initialize map
        for (int r = range.getMinimum(); r <= range.getMaximum(); r++) {
            lengthCyclesMap.put(r, new TreeSet<String>());
        }

        // we hit a loop
        if (numberOfLoops > 0) {
            // let's update the result

            if (!firstCycleAlreadyHit) {
                result.graphSizeEdgesBeforeFirstCycle = graph.getEdgeCount();
                result.graphSizeNodesBeforeFirstCycle = graph.getNodeCount();

                // find the shortest cycle
                int shortestCycleLength = Integer.MAX_VALUE;

                for (List<Object> cycle : cyclesInGraph) {
                    shortestCycleLength = Math.min(shortestCycleLength, cycle.size());
                }
                result.lengthOfFirstCircle = shortestCycleLength;

                result.pairsBeforeFirstCycle = i;

                firstCycleAlreadyHit = true;
            }

            // ignore this edge further
            ignoredEdgesCount++;

            // update counts of different cycles lengths
            for (List<Object> cycle : cyclesInGraph) {
                int currentSize = cycle.size();

                // convert to sorted set of nodes
                List<String> cycleAsSortedIDs = new ArrayList<>();
                for (Object o : cycle) {
                    cycleAsSortedIDs.add(o.toString());
                }
                Collections.sort(cycleAsSortedIDs);

                if (range.contains(currentSize)) {
                    lengthCyclesMap.get(currentSize).add(cycleAsSortedIDs.toString());
                }
            }
        } else {
            addedPairsIndices.add(i);
        }

        // we hit the first cycle

        // collect loop sizes
        StringBuilder loopsAsString = new StringBuilder();
        for (int j = range.getMinimum(); j <= range.getMaximum(); j++) {
            //                    loopsAsString.append(j).append(":");
            loopsAsString.append(lengthCyclesMap.get(j).size());
            loopsAsString.append("\t");
        }

        psTable.printf(Locale.ENGLISH, "%.4f\t%d\t%d\t%b\t%.2f\t%d\t%d\t%d\t%d\t%s%n", weakestEdgeWeight, i,
                ignoredEdgesCount, numberOfLoops == 0,
                Double.isNaN(transitivityScore.getMean()) ? 0d : transitivityScore.getMean(),
                (int) transitivityScore.getMax(), transitivityScore.getN(), graph.getEdgeCount(),
                graph.getNodeCount(), loopsAsString.toString().trim());

        // update result
        result.finalGraphSizeEdges = graph.getEdgeCount();
        result.finalGraphSizeNodes = graph.getNodeCount();
        result.ignoredEdgesThatBrokeDAG = ignoredEdgesCount;

        // update stats for correlation
        correlationEdgeWeights[i] = weakestEdgeWeight;
        //            correlationRemovedEdges[i] =  (double) ignoredEdgesCount;
        // let's try: if we keep = 0, if we remove = 1
        correlationRemovedEdges[i] = numberOfLoops == 0 ? 0.0 : 1.0;
    }

    psInfo.println("Original: " + fullDataSize + ", removed by MACE: " + (fullDataSize - preFilteredDataSize)
            + ", final: " + (preFilteredDataSize - ignoredEdgesCount) + " (removed: " + ignoredEdgesCount
            + ")");

    double[][] matrix = new double[correlationEdgeWeights.length][];
    for (int i = 0; i < correlationEdgeWeights.length; i++) {
        matrix[i] = new double[2];
        matrix[i][0] = correlationEdgeWeights[i];
        matrix[i][1] = correlationRemovedEdges[i];
    }

    PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation(matrix);

    double pValue = pearsonsCorrelation.getCorrelationPValues().getEntry(0, 1);
    double correlation = pearsonsCorrelation.getCorrelationMatrix().getEntry(0, 1);

    psInfo.printf(Locale.ENGLISH, "Correlation: %.3f, p-Value: %.4f%n", correlation, pValue);
    if (lastGraph == null) {
        throw new IllegalStateException("Graph is null");
    }

    // close
    psInfo.close();
    psTable.close();

    // save filtered final gold data
    List<AnnotatedArgumentPair> finalArgumentPairList = new ArrayList<>();

    for (Integer index : addedPairsIndices) {
        finalArgumentPairList.add(pairs.get(index));
    }
    XStreamTools.toXML(finalArgumentPairList, new File(outputDir, prefix + file.getName()));

    // TODO: here, we can add newly generated edges from graph transitivity
    if (collectGeneratedArgumentPairs) {
        Set<GeneratedArgumentPair> generatedArgumentPairs = new HashSet<>();
        // collect all arguments
        Map<String, Argument> allArguments = new HashMap<>();
        for (ArgumentPair argumentPair : pairs) {
            allArguments.put(argumentPair.getArg1().getId(), argumentPair.getArg1());
            allArguments.put(argumentPair.getArg2().getId(), argumentPair.getArg2());
        }

        Graph finalGraph = buildGraphFromArgumentPairs(finalArgumentPairList);
        for (Edge e : finalGraph.getEdgeSet()) {
            e.setAttribute(WEIGHT, 1.0);
        }

        for (Node j : finalGraph) {
            for (Node k : finalGraph) {
                if (j != k) {
                    // is there a path between?
                    BellmanFord bfShortest = new BellmanFord(WEIGHT, j.getId());
                    bfShortest.init(finalGraph);
                    bfShortest.compute();

                    Path shortestPath = bfShortest.getShortestPath(k);

                    if (shortestPath.size() > 0) {
                        // we have a path
                        GeneratedArgumentPair ap = new GeneratedArgumentPair();
                        Argument arg1 = allArguments.get(j.getId());

                        if (arg1 == null) {
                            throw new IllegalStateException("Cannot find argument " + j.getId());
                        }
                        ap.setArg1(arg1);

                        Argument arg2 = allArguments.get(k.getId());

                        if (arg2 == null) {
                            throw new IllegalStateException("Cannot find argument " + k.getId());
                        }
                        ap.setArg2(arg2);

                        ap.setGoldLabel("a1");
                        generatedArgumentPairs.add(ap);
                    }
                }
            }
        }
        // and now add the reverse ones
        Set<GeneratedArgumentPair> generatedReversePairs = new HashSet<>();
        for (GeneratedArgumentPair pair : generatedArgumentPairs) {
            GeneratedArgumentPair ap = new GeneratedArgumentPair();
            ap.setArg1(pair.getArg2());
            ap.setArg2(pair.getArg1());
            ap.setGoldLabel("a2");
            generatedReversePairs.add(ap);
        }
        generatedArgumentPairs.addAll(generatedReversePairs);
        // and save it
        XStreamTools.toXML(generatedArgumentPairs, new File(outputDir, "generated_" + prefix + file.getName()));
    }

    result.fullPairsSize = fullDataSize;
    result.removedApriori = (fullDataSize - preFilteredDataSize);
    result.finalPairsRetained = finalArgumentPairList.size();

    // save the final graph
    Graph outGraph = cleanCopyGraph(lastGraph);
    FileSinkDGS dgs1 = new FileSinkDGS();
    File outFile = new File(outputDir, prefix + file.getName() + ".dgs");

    System.out.println("Saved to " + outFile);
    FileWriter w1 = new FileWriter(outFile);

    dgs1.writeAll(outGraph, w1);
    w1.close();

    return result;
}

From source file:com.alibaba.dubbo.demo.consumer.DemoAction.java

public void start() throws Exception {
    int threads = 100;

    final DescriptiveStatistics stats = new SynchronizedDescriptiveStatistics();

    DubboBenchmark.BenchmarkMessage msg = prepareArgs();
    final byte[] msgBytes = msg.toByteArray();

    int n = 1000000;
    final CountDownLatch latch = new CountDownLatch(n);

    ExecutorService es = Executors.newFixedThreadPool(threads);

    final AtomicInteger trans = new AtomicInteger(0);
    final AtomicInteger transOK = new AtomicInteger(0);

    long start = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        es.submit(() -> {/* w ww.ja v a2 s  .c o  m*/
            try {

                long t = System.currentTimeMillis();
                DubboBenchmark.BenchmarkMessage m = testSay(msgBytes);
                t = System.currentTimeMillis() - t;
                stats.addValue(t);

                trans.incrementAndGet();

                if (m != null && m.getField1().equals("OK")) {
                    transOK.incrementAndGet();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                latch.countDown();
            }
        });
    }

    latch.await();

    start = System.currentTimeMillis() - start;

    System.out.printf("sent     requests    : %d\n", n);
    System.out.printf("received requests    : %d\n", trans.get());
    System.out.printf("received requests_OK : %d\n", transOK.get());
    System.out.printf("throughput  (TPS)    : %d\n", n * 1000 / start);

    System.out.printf("mean: %f\n", stats.getMean());
    System.out.printf("median: %f\n", stats.getPercentile(50));
    System.out.printf("max: %f\n", stats.getMax());
    System.out.printf("min: %f\n", stats.getMin());

    System.out.printf("99P: %f\n", stats.getPercentile(90));
}

From source file:net.adamjak.thomas.graph.application.run.TestRunner.java

private void save(Map<String, Object> results, boolean rawData) {
    SnarkTestTypes testType = (SnarkTestTypes) results.get("testType");

    if (this.outputFile.getName().split("\\.")[this.outputFile.getName().split("\\.").length - 1].toLowerCase()
            .equals("ods")) {

        String[] columnNames;// w w  w. j  a  v a  2s . co  m
        Object[][] data;

        if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            columnNames = String.valueOf("Algorithm,Graph ID,Avarage time,Standard deviation,Minimum,Maximum")
                    .split(",");
            data = new Object[graphTestResult[0].length][6];

            for (int cls = 0; cls < graphTestResult[0][0].length; cls++) {
                Class<?> c = (Class<?>) graphTestResult[0][0][cls].getValue("algorithmClass");

                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    SummaryStatistics summaryStatistics = new SummaryStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        summaryStatistics
                                .addValue((double) graphTestResult[run][graph][cls].getValue("timeInSeconds"));
                    }

                    data[graph][0] = c.getSimpleName();
                    data[graph][1] = graph;
                    data[graph][2] = summaryStatistics.getMean();
                    data[graph][3] = summaryStatistics.getStandardDeviation();
                    data[graph][4] = summaryStatistics.getMin();
                    data[graph][5] = summaryStatistics.getMax();
                }
            }
        } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            columnNames = String
                    .valueOf("Graph ID,Start vertex,Avarage time,Standard deviation,Minimum,Maximum")
                    .split(",");
            data = new Object[graphTestResult[0].length][6];

            for (int vid = 0; vid < graphTestResult[0][0].length; vid++) {
                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    SummaryStatistics summaryStatistics = new SummaryStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        summaryStatistics
                                .addValue((double) graphTestResult[run][graph][vid].getValue("timeInSeconds"));
                    }

                    data[graph][0] = graph;
                    data[graph][1] = vid;
                    data[graph][2] = summaryStatistics.getMean();
                    data[graph][3] = summaryStatistics.getStandardDeviation();
                    data[graph][4] = summaryStatistics.getMin();
                    data[graph][5] = summaryStatistics.getMax();
                }
            }
        } else {
            GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

            columnNames = String.valueOf("Graph ID,Avarage time,Standard deviation,Minimum,Maximum").split(",");
            data = new Object[graphTestResult[0].length][5];

            for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                SummaryStatistics summaryStatistics = new SummaryStatistics();

                for (int run = 0; run < graphTestResult.length; run++) {
                    summaryStatistics.addValue((double) graphTestResult[run][graph].getValue("timeInSeconds"));
                }

                data[graph][0] = graph;
                data[graph][1] = summaryStatistics.getMean();
                data[graph][2] = summaryStatistics.getStandardDeviation();
                data[graph][3] = summaryStatistics.getMin();
                data[graph][4] = summaryStatistics.getMax();
            }
        }

        try {
            SpreadSheet.createEmpty(new JTable(data, columnNames).getModel()).saveAs(outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (rawData == true) {
            if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                columnNames = String.valueOf("Class,Run,Graph,Time").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length
                        * graphTestResult[0][0].length][4];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            data[row][0] = graphTestResult[i][j][k].getValue("algorithmClass");
                            data[row][1] = i;
                            data[row][2] = j;
                            data[row][3] = graphTestResult[i][j][k].getValue("time");
                            row++;
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                columnNames = String.valueOf("Run,Graph,Vertex,Time").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length
                        * graphTestResult[0][0].length][4];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            data[row][0] = i;
                            data[row][1] = j;
                            data[row][2] = k;
                            data[row][3] = graphTestResult[i][j][k].getValue("time");
                            row++;
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ALGORITHM_COMPARATION) {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                columnNames = String.valueOf("Run,Graph,Time,Class").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length][4];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        data[row][0] = i;
                        data[row][1] = j;
                        data[row][2] = graphTestResult[i][j].getValue("time");
                        data[row][3] = ((Class<?>) graphTestResult[i][j]
                                .getValue(GraphTestResult.SNARK_TESTER_CLASS_KEY)).getSimpleName();
                        row++;
                    }
                }
            } else {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                columnNames = String.valueOf("Run,Graph,Time").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length][3];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        data[row][0] = i;
                        data[row][1] = j;
                        data[row][2] = graphTestResult[i][j].getValue("time");
                        row++;
                    }
                }
            }

            try {
                SpreadSheet.createEmpty(new JTable(data, columnNames).getModel()).saveAs(outputFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        StringBuilder sbData = new StringBuilder();

        if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            sbData.append(",,All data,,,,,Data without extremes,,,,,\n");
            sbData.append(
                    "Graph ID,Graph ID,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval\n");

            for (int cls = 0; cls < graphTestResult[0][0].length; cls++) {
                Class<?> c = (Class<?>) graphTestResult[0][0][cls].getValue("algorithmClass");

                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        descriptiveStatistics
                                .addValue((double) graphTestResult[run][graph][cls].getValue("timeInSeconds"));
                    }

                    DescriptiveStatistics descriptiveStatisticsWithoutExtremes = StatisticsUtils
                            .statisticsWithoutExtremes(descriptiveStatistics, StatisticsUtils.GrubbsLevel.L005);

                    sbData.append(c.getSimpleName());
                    sbData.append(",");
                    sbData.append(graph);
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatistics,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatisticsWithoutExtremes,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append("\n");
                }
            }
        } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            sbData.append(",,All data,,,,,Data without extremes,,,,,\n");
            sbData.append(
                    "Graph ID,Start vertex,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval\n");

            for (int vid = 0; vid < graphTestResult[0][0].length; vid++) {
                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        descriptiveStatistics
                                .addValue((double) graphTestResult[run][graph][vid].getValue("timeInSeconds"));
                    }

                    DescriptiveStatistics descriptiveStatisticsWithoutExtremes = StatisticsUtils
                            .statisticsWithoutExtremes(descriptiveStatistics, StatisticsUtils.GrubbsLevel.L005);

                    sbData.append(graph);
                    sbData.append(",");
                    sbData.append(vid);
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatistics,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatisticsWithoutExtremes,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append("\n");
                }
            }
        } else {

            GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

            sbData.append(",All data,,,,,Data without extremes,,,,,\n");
            sbData.append(
                    "Graph ID,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval\n");

            for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

                for (int run = 0; run < graphTestResult.length; run++) {
                    descriptiveStatistics
                            .addValue((double) graphTestResult[run][graph].getValue("timeInSeconds"));
                }

                DescriptiveStatistics descriptiveStatisticsWithoutExtremes = StatisticsUtils
                        .statisticsWithoutExtremes(descriptiveStatistics, StatisticsUtils.GrubbsLevel.L005);

                sbData.append(graph);
                sbData.append(",");
                sbData.append(descriptiveStatistics.getMean());
                sbData.append(",");
                sbData.append(descriptiveStatistics.getStandardDeviation());
                sbData.append(",");
                sbData.append(descriptiveStatistics.getMin());
                sbData.append(",");
                sbData.append(descriptiveStatistics.getMax());
                sbData.append(",");
                sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatistics,
                        StatisticsUtils.NormCritical.U0050));
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getMean());
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getStandardDeviation());
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getMin());
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getMax());
                sbData.append(",");
                sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatisticsWithoutExtremes,
                        StatisticsUtils.NormCritical.U0050));
                sbData.append("\n");
            }

        }

        this.saveStringIntoFile(this.outputFile, sbData.toString());

        if (rawData == true) {
            StringBuilder sbRawData = new StringBuilder();

            if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                sbRawData.append("Class,Run,Graph,Time\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            sbRawData.append(graphTestResult[i][j][k].getValue("algorithmClass"));
                            sbRawData.append(",");
                            sbRawData.append(i);
                            sbRawData.append(",");
                            sbRawData.append(j);
                            sbRawData.append(",");
                            sbRawData.append(graphTestResult[i][j][k].getValue("time"));
                            sbRawData.append("\n");
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                sbRawData.append("Run,Graph,Vertex,Time\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            sbRawData.append(i);
                            sbRawData.append(",");
                            sbRawData.append(j);
                            sbRawData.append(",");
                            sbRawData.append(k);
                            sbRawData.append(",");
                            sbRawData.append(graphTestResult[i][j][k].getValue("time"));
                            sbRawData.append("\n");
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ALGORITHM_COMPARATION) {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                sbRawData.append("Run,Graph,Time,Class\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        sbRawData.append(i);
                        sbRawData.append(",");
                        sbRawData.append(j);
                        sbRawData.append(",");
                        sbRawData.append(graphTestResult[i][j].getValue("time"));
                        sbRawData.append(",");
                        sbRawData.append(((Class<?>) graphTestResult[i][j]
                                .getValue(GraphTestResult.SNARK_TESTER_CLASS_KEY)).getSimpleName());
                        sbRawData.append("\n");
                    }
                }
            } else {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                sbRawData.append("Run,Graph,Time\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        sbRawData.append(i);
                        sbRawData.append(",");
                        sbRawData.append(j);
                        sbRawData.append(",");
                        sbRawData.append(graphTestResult[i][j].getValue("time"));
                        sbRawData.append("\n");
                    }
                }
            }

            this.saveStringIntoFile(new File(this.outputFile.getParent(), "raw_" + this.outputFile.getName()),
                    sbRawData.toString());
        }
    }
}

From source file:knop.psfj.FovDataSet.java

/**
 * Gets the min and max./* w ww  .jav a 2s .c om*/
 *
 * @param yColumn the y column
 * @return the min and max
 */
public double[] getMinAndMax(String yColumn) {
    double[] minAndMax = new double[3];

    DescriptiveStatistics stats = getColumnStatistics(yColumn);

    double line;
    double min;
    double max;

    if (yColumn.equals("z_profile")) {
        minAndMax[0] = -1;
        minAndMax[1] = 1;
        minAndMax[2] = 0;
        return minAndMax;
    }

    // scale to the theoritical value
    if (scalingMode == SCALE_TO_THEORITICAL) {

        // there is a theoretical value
        if (getTheoriticalValue(yColumn) != null) {
            line = getTheoriticalValue(yColumn);
            max = line * 2;
            min = line / 2;
        }
        // no theoritical value but the min and max are fixed
        else if (getMetaDataValueAsDouble(yColumn + "_interval_min") != null) {
            min = getMetaDataValueAsDouble(yColumn + "_interval_min");
            max = getMetaDataValueAsDouble(yColumn + "_interval_max");
            line = -1;
        }
        // no theoritical value, then we can only scale to min and max
        else {
            min = stats.getMin();
            max = stats.getMax();
            line = -1;
        }
    }

    else {

        min = stats.getMin();
        max = stats.getMax();
        if (getTheoriticalValue(yColumn) != null) {
            line = getTheoriticalValue(yColumn);
        } else {
            line = -1;
        }
    }

    minAndMax[0] = min;
    minAndMax[1] = max;
    minAndMax[2] = line;

    return minAndMax;
}

From source file:knop.psfj.BeadImage.java

public int getMaxaximumIntensityOfTheWholeStack() {
    // TODO Auto-generated method stub

    if (maxIntensityOfWholeStack == NOT_SET) {

        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (int i = 0; i != stack.getSize(); i++) {

            stats.addValue(stack.getProcessor(i + 1).getStatistics().max);

        }//from   www .  j  a  v  a 2  s.  com
        maxIntensityOfWholeStack = stats.getMax();

    }

    return MathUtils.round(maxIntensityOfWholeStack);
}

From source file:gdsc.smlm.ij.plugins.pcpalm.PCPALMMolecules.java

/**
 * Calculate the average precision by fitting a skewed Gaussian to the histogram of the precision distribution.
 * <p>/* w w  w . ja va 2  s  . c o m*/
 * A simple mean and SD of the histogram is computed. If the mean of the Skewed Gaussian does not fit within 3 SDs
 * of the simple mean then the simple mean is returned.
 * 
 * @param molecules
 * @param title
 *            the plot title (null if no plot should be displayed)
 * @param histogramBins
 * @param logFitParameters
 *            Record the fit parameters to the ImageJ log
 * @param removeOutliers
 *            The distribution is created using all values within 1.5x the inter-quartile range (IQR) of the data
 * @return The average precision
 */
public double calculateAveragePrecision(ArrayList<Molecule> molecules, String title, int histogramBins,
        boolean logFitParameters, boolean removeOutliers) {
    // Plot histogram of the precision
    float[] data = new float[molecules.size()];
    DescriptiveStatistics stats = new DescriptiveStatistics();
    double yMin = Double.NEGATIVE_INFINITY, yMax = 0;
    for (int i = 0; i < data.length; i++) {
        data[i] = (float) molecules.get(i).precision;
        stats.addValue(data[i]);
    }

    // Set the min and max y-values using 1.5 x IQR 
    if (removeOutliers) {
        double lower = stats.getPercentile(25);
        double upper = stats.getPercentile(75);
        if (Double.isNaN(lower) || Double.isNaN(upper)) {
            if (logFitParameters)
                Utils.log("Error computing IQR: %f - %f", lower, upper);
        } else {
            double iqr = upper - lower;

            yMin = FastMath.max(lower - iqr, stats.getMin());
            yMax = FastMath.min(upper + iqr, stats.getMax());

            if (logFitParameters)
                Utils.log("  Data range: %f - %f. Plotting 1.5x IQR: %f - %f", stats.getMin(), stats.getMax(),
                        yMin, yMax);
        }
    }

    if (yMin == Double.NEGATIVE_INFINITY) {
        yMin = stats.getMin();
        yMax = stats.getMax();

        if (logFitParameters)
            Utils.log("  Data range: %f - %f", yMin, yMax);
    }

    float[][] hist = Utils.calcHistogram(data, yMin, yMax, histogramBins);

    Plot2 plot = null;
    if (title != null) {
        plot = new Plot2(title, "Precision", "Frequency");
        float[] xValues = hist[0];
        float[] yValues = hist[1];
        if (xValues.length > 0) {
            double xPadding = 0.05 * (xValues[xValues.length - 1] - xValues[0]);
            plot.setLimits(xValues[0] - xPadding, xValues[xValues.length - 1] + xPadding, 0,
                    Maths.max(yValues) * 1.05);
        }
        plot.addPoints(xValues, yValues, Plot2.BAR);
        Utils.display(title, plot);
    }

    // Extract non-zero data
    float[] x = Arrays.copyOf(hist[0], hist[0].length);
    float[] y = hist[1];
    int count = 0;
    float dx = (x[1] - x[0]) * 0.5f;
    for (int i = 0; i < y.length; i++)
        if (y[i] > 0) {
            x[count] = x[i] + dx;
            y[count] = y[i];
            count++;
        }
    x = Arrays.copyOf(x, count);
    y = Arrays.copyOf(y, count);

    // Sense check to fitted data. Get mean and SD of histogram
    double[] stats2 = Utils.getHistogramStatistics(x, y);
    double mean = stats2[0];
    if (logFitParameters)
        log("  Initial Statistics: %f +/- %f", stats2[0], stats2[1]);

    // Standard Gaussian fit
    double[] parameters = fitGaussian(x, y);
    if (parameters == null) {
        log("  Failed to fit initial Gaussian");
        return mean;
    }
    double newMean = parameters[1];
    double error = Math.abs(stats2[0] - newMean) / stats2[1];
    if (error > 3) {
        log("  Failed to fit Gaussian: %f standard deviations from histogram mean", error);
        return mean;
    }
    if (newMean < yMin || newMean > yMax) {
        log("  Failed to fit Gaussian: %f outside data range %f - %f", newMean, yMin, yMax);
        return mean;
    }

    mean = newMean;

    if (logFitParameters)
        log("  Initial Gaussian: %f @ %f +/- %f", parameters[0], parameters[1], parameters[2]);

    double[] initialSolution = new double[] { parameters[0], parameters[1], parameters[2], -1 };

    // Fit to a skewed Gaussian (or appropriate function)
    double[] skewParameters = fitSkewGaussian(x, y, initialSolution);
    if (skewParameters == null) {
        log("  Failed to fit Skewed Gaussian");
        return mean;
    }

    SkewNormalFunction sn = new SkewNormalFunction(skewParameters);
    if (logFitParameters)
        log("  Skewed Gaussian: %f @ %f +/- %f (a = %f) => %f +/- %f", skewParameters[0], skewParameters[1],
                skewParameters[2], skewParameters[3], sn.getMean(), Math.sqrt(sn.getVariance()));

    newMean = sn.getMean();
    error = Math.abs(stats2[0] - newMean) / stats2[1];
    if (error > 3) {
        log("  Failed to fit Skewed Gaussian: %f standard deviations from histogram mean", error);
        return mean;
    }
    if (newMean < yMin || newMean > yMax) {
        log("  Failed to fit Skewed Gaussian: %f outside data range %f - %f", newMean, yMin, yMax);
        return mean;
    }

    // Use original histogram x-axis to maintain all the bins
    if (plot != null) {
        x = hist[0];
        for (int i = 0; i < y.length; i++)
            x[i] += dx;
        plot.setColor(Color.red);
        addToPlot(plot, x, skewParameters, Plot2.LINE);

        plot.setColor(Color.black);
        Utils.display(title, plot);
    }

    // Return the average precision from the fitted curve
    return newMean;
}

From source file:com.mapd.bench.Benchmark.java

String executeQuery(String sql, int expected, int iterations, int queryNum) {
    Connection conn = null;/*  ww  w . j  a  va 2s . com*/
    Statement stmt = null;

    Long firstExecute = 0l;
    Long firstJdbc = 0l;
    Long firstIterate = 0l;

    DescriptiveStatistics statsExecute = new DescriptiveStatistics();
    DescriptiveStatistics statsJdbc = new DescriptiveStatistics();
    DescriptiveStatistics statsIterate = new DescriptiveStatistics();
    DescriptiveStatistics statsTotal = new DescriptiveStatistics();

    long totalTime = 0;

    try {
        //Open a connection
        logger.debug("Connecting to database url :" + url);
        conn = DriverManager.getConnection(url, iUser, iPasswd);

        long startTime = System.currentTimeMillis();
        for (int loop = 0; loop < iterations; loop++) {

            //Execute a query
            stmt = conn.createStatement();

            long timer = System.currentTimeMillis();
            ResultSet rs = stmt.executeQuery(sql);

            long executeTime = 0;
            long jdbcTime = 0;

            // gather internal execute time for MapD as we are interested in that
            if (driver.equals(JDBC_DRIVER)) {
                executeTime = stmt.getQueryTimeout();
                jdbcTime = (System.currentTimeMillis() - timer) - executeTime;
            } else {
                jdbcTime = (System.currentTimeMillis() - timer);
                executeTime = 0;
            }
            // this is fake to get our intenal execute time.
            logger.debug("Query Timeout/AKA internal Execution Time was " + stmt.getQueryTimeout()
                    + " ms Elapsed time in JVM space was " + (System.currentTimeMillis() - timer) + "ms");

            timer = System.currentTimeMillis();
            //Extract data from result set
            int resultCount = 0;
            while (rs.next()) {
                Object obj = rs.getObject(1);
                if (obj != null && obj.equals(statsExecute)) {
                    logger.info("Impossible");
                }
                resultCount++;
            }
            long iterateTime = (System.currentTimeMillis() - timer);

            if (resultCount != expected) {
                logger.error("Expect " + expected + " actual " + resultCount + " for query " + sql);
                // don't run anymore
                break;
            }

            if (loop == 0) {
                firstJdbc = jdbcTime;
                firstExecute = executeTime;
                firstIterate = iterateTime;

            } else {
                statsJdbc.addValue(jdbcTime);
                statsExecute.addValue(executeTime);
                statsIterate.addValue(iterateTime);
                statsTotal.addValue(jdbcTime + executeTime + iterateTime);
            }

            //Clean-up environment
            rs.close();
            stmt.close();
        }
        totalTime = System.currentTimeMillis() - startTime;
        conn.close();
    } catch (SQLException se) {
        //Handle errors for JDBC
        se.printStackTrace();
    } catch (Exception e) {
        //Handle errors for Class.forName
        e.printStackTrace();
    } finally {
        //finally block used to close resources
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException se2) {
        } // nothing we can do
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } //end finally try
    } //end try

    return String.format(lineDescriptor, queryNum, statsTotal.getMean(), statsTotal.getMin(),
            statsTotal.getMax(), statsTotal.getPercentile(85), statsExecute.getMean(), statsExecute.getMin(),
            statsExecute.getMax(), statsExecute.getPercentile(85), statsExecute.getPercentile(25),
            statsExecute.getStandardDeviation(), statsJdbc.getMean(), statsJdbc.getMin(), statsJdbc.getMax(),
            statsJdbc.getPercentile(85), statsIterate.getMean(), statsIterate.getMin(), statsIterate.getMax(),
            statsIterate.getPercentile(85), firstExecute, firstJdbc, firstIterate, iterations, totalTime,
            (long) statsTotal.getSum() + firstExecute + firstJdbc + firstIterate);

}