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.mapd.bench.BenchmarkCloud.java

String executeQuery(Connection conn1, String qid, String sql, int iterations) {
    Statement stmt = null;//from w  w  w  .  j ava  2  s  .c  o  m
    Connection conn = getConnection(url, iUser, iPasswd);

    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;
    int resultCount = 0;
    try {

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

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

            long timer = System.currentTimeMillis();
            if (loop == 0) {
                System.out.println(String.format("Query Id is %s : query is '%s'", qid, sql));
            }
            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
            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();
        System.exit(4);
    } catch (Exception e) {
        //Handle errors for Class.forName
        e.printStackTrace();
        System.exit(3);
    } 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();
            System.exit(6);
        } //end finally try
    } //end try

    // write it to the db here as well
    String insertPart = String.format(insertDescriptor, this.rid, this.rTimestamp, url, this.driver, label,
            gpuCount, this.tableName, qid, resultCount, "", 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, targetDBVersion);

    LResult.add("Insert into results values " + insertPart);

    return String.format(lineDescriptor, qid, 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);

}

From source file:edu.snu.leader.hierarchy.simple.DefaultReporter.java

/**
 * Report the final results of the simulation
 *
 * @see edu.snu.leader.hierarchy.simple.Reporter#reportFinalResults()
 *//*  ww  w .j ava  2  s. c  o m*/
@Override
public void reportFinalResults() {
    // Create some handy variables
    long firstActiveTimestep = Long.MAX_VALUE;
    long lastActiveTimestep = Long.MIN_VALUE;
    int initiatorCount = 0;

    // Gather some statistics
    DescriptiveStatistics immediateFollowerStats = new DescriptiveStatistics();
    DescriptiveStatistics initiatorDistanceStats = new DescriptiveStatistics();
    DescriptiveStatistics activeTimestepStats = new DescriptiveStatistics();

    // Iterate through all the individuals
    Iterator<Individual> indIter = _simState.getAllIndividuals().iterator();
    while (indIter.hasNext()) {
        Individual ind = indIter.next();

        // Get some statistics
        immediateFollowerStats.addValue(ind.getImmediateFollowerCount());
        initiatorDistanceStats.addValue(ind.getDistanceToInitiator());
        activeTimestepStats.addValue(ind.getActiveTimestep());

        // Build the prefix
        String prefix = "individual." + ind.getID() + ".";

        // Log out important information
        _writer.println(prefix + "group-id = " + ind.getGroupID());
        _writer.println(prefix + "active-timestep = " + ind.getActiveTimestep());
        _writer.println(prefix + "immediate-follower-count = " + ind.getImmediateFollowerCount());
        _writer.println(prefix + "total-follower-count = " + ind.getTotalFollowerCount());
        _writer.println(prefix + "distance-to-initiator = " + ind.getDistanceToInitiator());
        _writer.println(prefix + "location = " + ind.getLocation().getX() + " " + ind.getLocation().getY());
        _writer.println(prefix + "threshold = " + ind.getThreshold());
        _writer.println(prefix + "skill = " + ind.getSkill());
        _writer.println(prefix + "confidence = " + ind.getConfidence());
        _writer.println(prefix + "reputation = " + ind.getReputation());
        _writer.println(prefix + "boldness = " + ind.getBoldness());

        // Get the leader's ID, if it exists
        Object leaderID = "";
        if (null != ind.getLeader()) {
            leaderID = ind.getLeader().getIndividual().getID();
        } else {
            ++initiatorCount;
        }
        _writer.println(prefix + "leader = " + leaderID);

        // Build the list of neighbor ID's
        StringBuilder builder = new StringBuilder();
        Iterator<Neighbor> neighborIter = ind.getNearestNeighbors().iterator();
        while (neighborIter.hasNext()) {
            builder.append(neighborIter.next().getIndividual().getID());
            builder.append(" ");
        }
        _writer.println(prefix + "nearest-neighbors = " + builder.toString());

        // Build the list of follower ID's
        builder = new StringBuilder();
        neighborIter = ind.getFollowers().iterator();
        while (neighborIter.hasNext()) {
            builder.append(neighborIter.next().getIndividual().getID());
            builder.append(" ");
        }
        _writer.println(prefix + "immediate-followers = " + builder.toString());

        // Check the activity time
        if (firstActiveTimestep > ind.getActiveTimestep()) {
            firstActiveTimestep = ind.getActiveTimestep();
        }
        if (lastActiveTimestep < ind.getActiveTimestep()) {
            lastActiveTimestep = ind.getActiveTimestep();
        }

        _writer.println();
    }

    // Log the simulation information
    _writer.println("simulation.first-active-timestep = " + firstActiveTimestep);
    _writer.println("simulation.last-active-timestep = " + lastActiveTimestep);
    _writer.println("simulation.initiator-count = " + initiatorCount);

    // Log the stats
    _writer.println("statistics.immediate-followers.mean = " + immediateFollowerStats.getMean());
    _writer.println(
            "statistics.immediate-followers.std-dev = " + immediateFollowerStats.getStandardDeviation());
    _writer.println("statistics.immediate-followers.min = " + immediateFollowerStats.getMin());
    _writer.println("statistics.immediate-followers.max = " + immediateFollowerStats.getMax());

    _writer.println("statistics.initiator-distance.mean = " + initiatorDistanceStats.getMean());
    _writer.println("statistics.initiator-distance.std-dev = " + initiatorDistanceStats.getStandardDeviation());
    _writer.println("statistics.initiator-distance.min = " + initiatorDistanceStats.getMin());
    _writer.println("statistics.initiator-distance.max = " + initiatorDistanceStats.getMax());

    _writer.println("statistics.active-timestep.mean = " + activeTimestepStats.getMean());
    _writer.println("statistics.active-timestep.std-dev = " + activeTimestepStats.getStandardDeviation());
    _writer.println("statistics.active-timestep.min = " + activeTimestepStats.getMin());
    _writer.println("statistics.active-timestep.max = " + activeTimestepStats.getMax());

    // Log out the stop time
    _writer.println();
    _writer.println(_STATS_SPACER);
    _writer.println("# Finished: " + (new Date()));

    // Close out the writer
    _writer.close();
}

From source file:knop.psfj.BeadImage.java

/**
 * load the stack into memory./*from   w  w w  .ja va2 s .  c o m*/
 */
public synchronized void workFromMemory() {
    // if the fileAddress is not equal to null, this means that
    // the image has not been previously load into memory
    if (fileAddress != null && stack == null) {
        System.out.println("Loading image in memory...");
        setProgress(0, "Loading image in memory...");

        try {
            // BF.openImagePlus(path)

            // stack = BF.openImagePlus(fileAddress)[0].getImageStack();
            // stack = new
            // Opener().openImage(fileAddress).getImageStack();//
            // IJ.openImage(fileAddress).getStack();
            ImageProcessorReader ipr = new ImageProcessorReader(
                    new ChannelSeparator(LociPrefs.makeImageReader()));
            DescriptiveStatistics standardDeviations = new DescriptiveStatistics();

            ipr.setId(fileAddress);

            int width = ipr.getSizeX();
            int height = ipr.getSizeY();
            int num = ipr.getImageCount();
            int numChannel = ipr.getSizeC();
            int bitsPerPixel = ipr.getBitsPerPixel();

            double stdDev;
            double min;
            double max;

            // if a second channel in the image is detected
            boolean isSecondChannel = false;
            BeadImage secondChannel = null;
            ImageStack secondStack = null;

            if (numChannel == 2) {

                secondChannel = new BeadImage();
                secondChannel.setFileAddress(fileAddress);
                secondChannel.setImageName(secondChannel.getImageName() + "_channel_2");
                secondStack = new ImageStack(width, height);
                isSecondChannel = true;
                secondChannel.setStack(secondStack);

                notifyObservers(MSG_NEW_CHANNEL_DETECTED, "Two channels detected", null, secondChannel);

            }

            stack = new ImageStack(width, height);

            for (int i = 0; i != num; i++) {
                setProgress(i, num);
                setStatus("Loading slice " + (i + 1) + "/" + num + "...");

                ImageProcessor ip = ipr.openProcessors(i)[0];

                if (isSecondChannel) {
                    i++;
                    ImageProcessor ip2 = ipr.openProcessors(i)[0];
                    secondStack.addSlice(ip2);
                    secondChannel.setStatus("Loading slice " + (i + 1) + "/" + num + "...");
                    secondChannel.setProgress(i, num);
                }

                min = ip.getMin();
                max = ip.getMax();

                if (min < minIntentisyOfWholeStack) {
                    minIntentisyOfWholeStack = min;
                }
                if (max > maxIntensityOfWholeStack) {
                    maxIntensityOfWholeStack = max;
                }

                stdDev = ip.getStatistics().stdDev;

                if (standardDeviations.getMax() < stdDev) {
                    beadFocusPlane = i;
                }
                standardDeviations.addValue(stdDev);
                stack.addSlice(ip);
                updateView(ip);
            }
            if (isSecondChannel) {
                autoFocus();
                secondChannel.autoFocus();
                secondChannel.setProgress(100);
            }
            try {
                ipr.close();
            } catch (Exception e) {
                System.err.println("Error when closing the image reader.");
            }
            setChanged();
            notifyObservers(new Message(this, MSG_IMAGE_OKAY));
            setStatus("Done.");
            setProgress(100);

            setImageHeight(stack.getHeight());
            setImageWidth(stack.getWidth());

            isValid = true;
            new ImagePlus("", stack).resetDisplayRange();
            // openImage();

        } catch (NullPointerException e) {
            notifyError("Image not valid.");
            e.printStackTrace();
            setProgress(0, "image not okay");
            return;
        } catch (FormatException e) {
            setProgress(0, "This image format is not supported.");
            notifyError("This image format is not supported");
            e.printStackTrace();
        } catch (IOException e) {

            notifyError("File not accessible");
            setProgress(0, "Image not reachable.");
            e.printStackTrace();
        }

    }
}

From source file:org.alfresco.bm.api.v1.ResultsRestAPI.java

/**
 * Retrieve an approximate number of results, allowing for a smoothing factor
 * (<a href=http://en.wikipedia.org/wiki/Moving_average#Simple_moving_average>Simple Moving Average</a>) -
 * the number of data results to including in the moving average.
 * // w w w  .j  a va  2 s .  co m
 * @param fromTime              the approximate time to start from
 * @param timeUnit              the units of the 'reportPeriod' (default SECONDS).  See {@link TimeUnit}.
 * @param reportPeriod          how often a result should be output.  This is expressed as a multiple of the 'timeUnit'.
 * @param smoothing             the number of results to include in the Simple Moving Average calculations
 * @param chartOnly             <tt>true</tt> to filter out results that are not of interest in performance charts
 * 
 * @return                      JSON representing the event start time (x-axis) and the smoothed average execution time
 *                              along with data such as the events per second, failures per second, etc.
 */
@GET
@Path("/ts")
@Produces(MediaType.APPLICATION_JSON)
public String getTimeSeriesResults(@DefaultValue("0") @QueryParam("fromTime") long fromTime,
        @DefaultValue("SECONDS") @QueryParam("timeUnit") String timeUnit,
        @DefaultValue("1") @QueryParam("reportPeriod") long reportPeriod,
        @DefaultValue("1") @QueryParam("smoothing") int smoothing,
        @DefaultValue("true") @QueryParam("chartOnly") boolean chartOnly) {
    if (logger.isDebugEnabled()) {
        logger.debug("Inbound: " + "[test:" + test + ",fromTime:" + fromTime + ",timeUnit:" + timeUnit
                + ",reportPeriod:" + reportPeriod + ",smoothing:" + smoothing + ",chartOnly:" + chartOnly
                + "]");
    }
    if (reportPeriod < 1) {
        throwAndLogException(Status.BAD_REQUEST, "'reportPeriod' must be 1 or more.");
    }
    if (smoothing < 1) {
        throwAndLogException(Status.BAD_REQUEST, "'smoothing' must be 1 or more.");
    }
    TimeUnit timeUnitEnum = null;
    try {
        timeUnitEnum = TimeUnit.valueOf(timeUnit.toUpperCase());
    } catch (Exception e) {
        // Invalid time unit
        throwAndLogException(Status.BAD_REQUEST, e);
    }

    final ResultService resultService = getResultService();

    // Calculate the window size
    long reportPeriodMs = timeUnitEnum.toMillis(reportPeriod);
    long windowSize = reportPeriodMs * smoothing;

    // This is just too convenient an API
    final BasicDBList events = new BasicDBList();
    ResultHandler handler = new ResultHandler() {
        @Override
        public boolean processResult(long fromTime, long toTime,
                Map<String, DescriptiveStatistics> statsByEventName, Map<String, Integer> failuresByEventName)
                throws Throwable {
            for (Map.Entry<String, DescriptiveStatistics> entry : statsByEventName.entrySet()) {
                String eventName = entry.getKey();
                DescriptiveStatistics stats = entry.getValue();
                Integer failures = failuresByEventName.get(eventName);
                if (failures == null) {
                    logger.error("Found null failure count: " + entry);
                    // Do nothing with it and stop
                    return false;
                }
                // Per second
                double numPerSec = (double) stats.getN() / ((double) (toTime - fromTime) / 1000.0);
                double failuresPerSec = (double) failures / ((double) (toTime - fromTime) / 1000.0);
                // Push into an object
                DBObject eventObj = BasicDBObjectBuilder.start().add("time", toTime).add("name", eventName)
                        .add("mean", stats.getMean()).add("min", stats.getMin()).add("max", stats.getMax())
                        .add("stdDev", stats.getStandardDeviation()).add("num", stats.getN())
                        .add("numPerSec", numPerSec).add("fail", failures).add("failPerSec", failuresPerSec)
                        .get();
                // Add the object to the list of events
                events.add(eventObj);
            }
            // Go for the next result
            return true;
        }
    };
    try {
        // Get all the results
        resultService.getResults(handler, fromTime, windowSize, reportPeriodMs, chartOnly);
        // Muster into JSON
        String json = events.toString();

        // Done
        if (logger.isDebugEnabled()) {
            int jsonLen = json.length();
            if (jsonLen < 500) {
                logger.debug("Outbound: " + json);
            } else {
                logger.debug("Outbound: " + json.substring(0, 250) + " ... "
                        + json.substring(jsonLen - 250, jsonLen));
            }
        }
        return json;

    } catch (WebApplicationException e) {
        throw e;
    } catch (Exception e) {
        throwAndLogException(Status.INTERNAL_SERVER_ERROR, e);
        return null;
    }
}

From source file:org.alfresco.bm.report.XLSXReporter.java

private void createEventSheets(final XSSFWorkbook workbook) {
    // Create the fonts we need
    Font fontBold = workbook.createFont();
    fontBold.setBoldweight(Font.BOLDWEIGHT_BOLD);

    // Create the styles we need
    CreationHelper helper = workbook.getCreationHelper();
    final XSSFCellStyle dataStyle = workbook.createCellStyle();
    dataStyle.setAlignment(HorizontalAlignment.RIGHT);
    final XSSFCellStyle headerStyle = workbook.createCellStyle();
    headerStyle.setAlignment(HorizontalAlignment.RIGHT);
    headerStyle.setFont(fontBold);//from w w w  . ja  v a2s  .  co  m
    final XSSFCellStyle dateStyle = workbook.createCellStyle();
    dateStyle.setDataFormat(helper.createDataFormat().getFormat("HH:mm:ss"));

    // Calculate a good window size
    ResultService resultService = getResultService();
    EventRecord firstResult = resultService.getFirstResult();
    EventRecord lastResult = resultService.getLastResult();
    if (firstResult == null || lastResult == null) {
        return;
    }
    long start = firstResult.getStartTime();
    long end = lastResult.getStartTime();
    long windowSize = AbstractEventReporter.getWindowSize(start, end, 100); // Well-known window sizes

    // Keep track of sheets by event name. Note that XLSX truncates sheets to 31 chars, so use 28 chars and ~01, ~02
    final Map<String, String> sheetNames = new HashMap<String, String>(31);
    final Map<String, XSSFSheet> sheets = new HashMap<String, XSSFSheet>(31);
    final Map<String, AtomicInteger> rowNums = new HashMap<String, AtomicInteger>(31);

    ResultHandler handler = new ResultHandler() {
        @Override
        public boolean processResult(long fromTime, long toTime,
                Map<String, DescriptiveStatistics> statsByEventName, Map<String, Integer> failuresByEventName)
                throws Throwable {
            // Get or create a sheet for each event
            for (String eventName : statsByEventName.keySet()) {
                // What sheet name to we use?
                String sheetName = sheetNames.get(eventName);
                if (sheetName == null) {
                    sheetName = eventName;
                    if (eventName.length() > 28) {
                        int counter = 1;
                        // Find a sheet name not in use
                        while (true) {
                            sheetName = eventName.substring(0, 28);
                            sheetName = String.format("%s~%02d", sheetName, counter);
                            // Have we used this, yet?
                            if (sheets.containsKey(sheetName)) {
                                // Yes, we have used it.
                                counter++;
                                continue;
                            }
                            // This is unique
                            break;
                        }
                    }
                    sheetNames.put(eventName, sheetName);
                }
                // Get and create the sheet, if necessary
                XSSFSheet sheet = sheets.get(sheetName);
                if (sheet == null) {
                    // Create
                    try {
                        sheet = workbook.createSheet(sheetName);
                        sheets.put(sheetName, sheet);
                        sheet.getHeader().setCenter(title + " - " + eventName);
                        sheet.getPrintSetup().setFitWidth((short) 1);
                        sheet.getPrintSetup().setLandscape(true);
                    } catch (Exception e) {
                        logger.error("Unable to create workbook sheet for event: " + eventName, e);
                        continue;
                    }
                    // Intro
                    XSSFCell cell = sheet.createRow(0).createCell(0);
                    cell.setCellValue(title + " - " + eventName + ":");
                    cell.setCellStyle(headerStyle);
                    // Headings
                    XSSFRow row = sheet.createRow(1);
                    cell = row.createCell(0);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("time");
                    cell = row.createCell(1);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("mean");
                    cell = row.createCell(2);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("min");
                    cell = row.createCell(3);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("max");
                    cell = row.createCell(4);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("stdDev");
                    cell = row.createCell(5);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("num");
                    cell = row.createCell(6);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("numPerSec");
                    cell = row.createCell(7);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("fail");
                    cell = row.createCell(8);
                    cell.setCellStyle(headerStyle);
                    cell.setCellValue("failPerSec");
                    // Size the columns
                    sheet.autoSizeColumn(0);
                    sheet.autoSizeColumn(1);
                    sheet.autoSizeColumn(2);
                    sheet.autoSizeColumn(3);
                    sheet.autoSizeColumn(4);
                    sheet.autoSizeColumn(5);
                    sheet.autoSizeColumn(6);
                    sheet.autoSizeColumn(7);
                    sheet.autoSizeColumn(8);
                }
                AtomicInteger rowNum = rowNums.get(eventName);
                if (rowNum == null) {
                    rowNum = new AtomicInteger(2);
                    rowNums.put(eventName, rowNum);
                }

                DescriptiveStatistics stats = statsByEventName.get(eventName);
                Integer failures = failuresByEventName.get(eventName);

                double numPerSec = (double) stats.getN() / ((double) (toTime - fromTime) / 1000.0);
                double failuresPerSec = (double) failures / ((double) (toTime - fromTime) / 1000.0);

                XSSFRow row = sheet.createRow(rowNum.getAndIncrement());
                XSSFCell cell;
                cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC);
                cell.setCellStyle(dateStyle);
                cell.setCellValue(new Date(toTime));
                cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(stats.getN());
                cell = row.createCell(6, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(numPerSec);
                cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(failures);
                cell = row.createCell(8, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(failuresPerSec);
                // Leave out values if there is no mean
                if (Double.isNaN(stats.getMean())) {
                    continue;
                }
                cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(stats.getMean());
                cell = row.createCell(2, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(stats.getMin());
                cell = row.createCell(3, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(stats.getMax());
                cell = row.createCell(4, Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(stats.getStandardDeviation());
            }
            return true;
        }
    };
    resultService.getResults(handler, start, windowSize, windowSize, false);

    // Create charts in the sheets
    for (String eventName : sheetNames.keySet()) {
        // Get the sheet name
        String sheetName = sheetNames.get(eventName);
        if (sheetName == null) {
            logger.error("Did not find sheet for event: " + eventName);
            continue;
        }
        // Get the sheet
        XSSFSheet sheet = sheets.get(sheetName);
        if (sheet == null) {
            logger.error("Did not find sheet for name: " + sheetName);
            continue;
        }
        // What row did we get up to
        AtomicInteger rowNum = rowNums.get(eventName);
        if (rowNum == null) {
            logger.error("Did not find row number for event: " + sheetName);
            continue;
        }

        // This axis is common to both charts
        ChartDataSource<Number> xTime = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(1, rowNum.intValue() - 1, 0, 0));

        // Graph of event times
        XSSFDrawing drawingTimes = sheet.createDrawingPatriarch();
        ClientAnchor anchorTimes = drawingTimes.createAnchor(0, 0, 0, 0, 0, 5, 15, 25);
        Chart chartTimes = drawingTimes.createChart(anchorTimes);
        ChartLegend legendTimes = chartTimes.getOrCreateLegend();
        legendTimes.setPosition(LegendPosition.BOTTOM);

        LineChartData chartDataTimes = chartTimes.getChartDataFactory().createLineChartData();

        ChartAxis bottomAxisTimes = chartTimes.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        bottomAxisTimes.setNumberFormat("#,##0;-#,##0");
        ValueAxis leftAxisTimes = chartTimes.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);

        // Mean
        ChartDataSource<Number> yMean = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(1, rowNum.intValue() - 1, 1, 1));
        LineChartSeries yMeanSerie = chartDataTimes.addSeries(xTime, yMean);
        yMeanSerie.setTitle(title + " - " + eventName + ": Mean (ms)");

        // Std Dev
        ChartDataSource<Number> yStdDev = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(1, rowNum.intValue() - 1, 4, 4));
        LineChartSeries yStdDevSerie = chartDataTimes.addSeries(xTime, yStdDev);
        yStdDevSerie.setTitle(title + " - " + eventName + ": Standard Deviation (ms)");

        // Plot event times
        chartTimes.plot(chartDataTimes, bottomAxisTimes, leftAxisTimes);

        // Graph of event volumes

        // Graph of event times
        XSSFDrawing drawingVolumes = sheet.createDrawingPatriarch();
        ClientAnchor anchorVolumes = drawingVolumes.createAnchor(0, 0, 0, 0, 0, 25, 15, 35);
        Chart chartVolumes = drawingVolumes.createChart(anchorVolumes);
        ChartLegend legendVolumes = chartVolumes.getOrCreateLegend();
        legendVolumes.setPosition(LegendPosition.BOTTOM);

        LineChartData chartDataVolumes = chartVolumes.getChartDataFactory().createLineChartData();

        ChartAxis bottomAxisVolumes = chartVolumes.getChartAxisFactory()
                .createCategoryAxis(AxisPosition.BOTTOM);
        bottomAxisVolumes.setNumberFormat("#,##0;-#,##0");
        ValueAxis leftAxisVolumes = chartVolumes.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);

        // Number per second
        ChartDataSource<Number> yNumPerSec = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(1, rowNum.intValue() - 1, 6, 6));
        LineChartSeries yNumPerSecSerie = chartDataVolumes.addSeries(xTime, yNumPerSec);
        yNumPerSecSerie.setTitle(title + " - " + eventName + ": Events per Second");

        // Failures per second
        ChartDataSource<Number> yFailPerSec = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(1, rowNum.intValue() - 1, 8, 8));
        LineChartSeries yFailPerSecSerie = chartDataVolumes.addSeries(xTime, yFailPerSec);
        yFailPerSecSerie.setTitle(title + " - " + eventName + ": Failures per Second");

        // Plot volumes
        chartVolumes.plot(chartDataVolumes, bottomAxisVolumes, leftAxisVolumes);
    }
}

From source file:org.apache.hadoop.hive.metastore.tools.BenchmarkSuite.java

/**
 * Produce printable result/*w  w  w.  j a  v  a 2  s  .co m*/
 * @param fmt text formatter - destination of formatted results.
 * @param name benchmark name
 * @param stats benchmark data
 */
private void displayStats(@NotNull Formatter fmt, @NotNull String name, @NotNull DescriptiveStatistics stats) {
    double mean = stats.getMean();
    double err = stats.getStandardDeviation() / mean * 100;
    long conv = scale.toNanos(1);

    fmt.format("%-30s %-8.4g %-8.4g %-8.4g %-8.4g %-8.4g%n", name, mean / conv, median(stats) / conv,
            stats.getMin() / conv, stats.getMax() / conv, err);
}

From source file:org.apache.hadoop.hive.metastore.tools.BenchmarkSuite.java

/**
 * Produce results in printable CSV format, separated by separator.
 * @param fmt text formatter - destination of formatted results.
 * @param name benchmark name//from   www  . j  av  a  2s  .  c  om
 * @param stats benchmark data
 * @param separator field separator
 */
private void displayCSV(@NotNull Formatter fmt, @NotNull String name, @NotNull DescriptiveStatistics stats,
        @NotNull String separator) {
    double mean = stats.getMean();
    double err = stats.getStandardDeviation() / mean * 100;
    long conv = scale.toNanos(1);

    fmt.format("%s%s%g%s%g%s%g%s%g%s%g%n", name, separator, mean / conv, separator, median(stats) / conv,
            separator, stats.getMin() / conv, separator, stats.getMax() / conv, separator, err);
}

From source file:org.apache.metron.common.math.stats.OnlineStatisticsProviderTest.java

public static void validateStatisticsProvider(StatisticsProvider statsProvider, SummaryStatistics summaryStats,
        DescriptiveStatistics stats) {
    //N/*from  w  w  w.  j  a v  a  2  s  .  co m*/
    Assert.assertEquals(statsProvider.getCount(), stats.getN());
    //sum
    Assert.assertEquals(statsProvider.getSum(), stats.getSum(), 1e-3);
    //sum of squares
    Assert.assertEquals(statsProvider.getSumSquares(), stats.getSumsq(), 1e-3);
    //sum of squares
    Assert.assertEquals(statsProvider.getSumLogs(), summaryStats.getSumOfLogs(), 1e-3);
    //Mean
    Assert.assertEquals(statsProvider.getMean(), stats.getMean(), 1e-3);
    //Quadratic Mean
    Assert.assertEquals(statsProvider.getQuadraticMean(), summaryStats.getQuadraticMean(), 1e-3);
    //SD
    Assert.assertEquals(statsProvider.getStandardDeviation(), stats.getStandardDeviation(), 1e-3);
    //Variance
    Assert.assertEquals(statsProvider.getVariance(), stats.getVariance(), 1e-3);
    //Min
    Assert.assertEquals(statsProvider.getMin(), stats.getMin(), 1e-3);
    //Max
    Assert.assertEquals(statsProvider.getMax(), stats.getMax(), 1e-3);

    //Kurtosis
    Assert.assertEquals(stats.getKurtosis(), statsProvider.getKurtosis(), 1e-3);

    //Skewness
    Assert.assertEquals(stats.getSkewness(), statsProvider.getSkewness(), 1e-3);
    for (double d = 10.0; d < 100.0; d += 10) {
        //This is a sketch, so we're a bit more forgiving here in our choice of \epsilon.
        Assert.assertEquals("Percentile mismatch for " + d + "th %ile", statsProvider.getPercentile(d),
                stats.getPercentile(d), 1e-2);
    }
}

From source file:org.apache.metron.common.stellar.benchmark.Microbenchmark.java

public static String describe(DescriptiveStatistics stats, Double[] percentiles) {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("round: mean of %dms [+-%d], measured %d rounds;\n", (long) stats.getMean(),
            (long) stats.getStandardDeviation(), stats.getN()));
    sb.append("\tMin - " + (long) stats.getMin() + "\n");
    for (double pctile : percentiles) {
        sb.append("\t" + pctile + " - " + stats.getPercentile(pctile) + "\n");
    }/*from w ww  .ja v a2  s  .c o m*/
    sb.append("\tMax - " + (long) stats.getMax());
    return sb.toString();
}

From source file:org.apache.metron.statistics.outlier.MedianAbsoluteDeviationTest.java

@Test
public void test() {

    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    DescriptiveStatistics stats = new DescriptiveStatistics();
    List<MedianAbsoluteDeviationFunctions.State> states = new ArrayList<>();
    MedianAbsoluteDeviationFunctions.State currentState = null;
    //initialize the state
    currentState = (MedianAbsoluteDeviationFunctions.State) run("OUTLIER_MAD_STATE_MERGE(states, NULL)",
            ImmutableMap.of("states", states));
    for (int i = 0, j = 0; i < 10000; ++i, ++j) {
        Double d = gaussian.nextNormalizedDouble();
        stats.addValue(d);//from   w  ww .j  a v a2  s  .com
        run("OUTLIER_MAD_ADD(currentState, data)", ImmutableMap.of("currentState", currentState, "data", d));
        if (j >= 1000) {
            j = 0;
            List<MedianAbsoluteDeviationFunctions.State> stateWindow = new ArrayList<>();
            for (int stateIndex = Math.max(0, states.size() - 5); stateIndex < states.size(); ++stateIndex) {
                stateWindow.add(states.get(stateIndex));
            }
            currentState = (MedianAbsoluteDeviationFunctions.State) run(
                    "OUTLIER_MAD_STATE_MERGE(states, currentState)",
                    ImmutableMap.of("states", stateWindow, "currentState", currentState));
        }
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)",
                ImmutableMap.of("currentState", currentState, "value", stats.getMin()));
        Assert.assertTrue("Score: " + score + " is not an outlier despite being a minimum.", score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)",
                ImmutableMap.of("currentState", currentState, "value", stats.getMax()));
        Assert.assertTrue("Score: " + score + " is not an outlier despite being a maximum", score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState",
                currentState, "value", stats.getMean() + 4 * stats.getStandardDeviation()));
        Assert.assertTrue(
                "Score: " + score + " is not an outlier despite being 4 std deviations away from the mean",
                score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState",
                currentState, "value", stats.getMean() - 4 * stats.getStandardDeviation()));
        Assert.assertTrue(
                "Score: " + score + " is not an outlier despite being 4 std deviations away from the mean",
                score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)",
                ImmutableMap.of("currentState", currentState, "value", stats.getMean()));
        Assert.assertFalse("Score: " + score + " is an outlier despite being the mean", score > 3.5);
    }
}