Example usage for org.apache.commons.math.stat.descriptive DescriptiveStatistics addValue

List of usage examples for org.apache.commons.math.stat.descriptive DescriptiveStatistics addValue

Introduction

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

Prototype

public void addValue(double v) 

Source Link

Document

Adds the value to the dataset.

Usage

From source file:playground.johannes.studies.coopsim.TimeSamplerTest.java

/**
 * @param args/*from   w  w  w.  j  a va 2 s .c om*/
 * @throws FunctionEvaluationException 
 * @throws IOException 
 */
public static void main(String[] args) throws FunctionEvaluationException, IOException {
    Random random = new Random();

    AdditiveDistribution arrTimePDF = new AdditiveDistribution();
    arrTimePDF.addComponent(new GaussDistribution(2151, 43030, 267));
    arrTimePDF.addComponent(new GaussDistribution(14227, 58036, 1382));

    TimeSampler arrivalSampler = new TimeSampler(arrTimePDF, 86400, random);

    AdditiveDistribution arrDurPDF = new AdditiveDistribution();
    arrDurPDF.addComponent(new GaussDistribution(7210, 41513, 133759479));
    arrDurPDF.addComponent(new GaussDistribution(15660, 73033, 277912890));

    //      TimeSampler arrDurSampler = new TimeSampler(arrDurPDF, 86400, random);
    //      
    //      LogNormalDistribution durPDF = new LogNormalDistribution(0.6883, 8.4954, 522.9869);
    //      TimeSampler durSampler = new TimeSampler(durPDF, 86400, random);
    DescriptiveStatistics durations = new DescriptiveStatistics();
    DescriptiveStatistics arrivals = new DescriptiveStatistics();

    ProgressLogger.init(10000, 1, 5);
    double sigma = 0.6883;
    for (int i = 0; i < 10000; i++) {
        int t_arr = arrivalSampler.nextSample();
        int dur_mean = (int) arrDurPDF.value(t_arr);
        if (dur_mean > 0) {
            double mu = Math.log(dur_mean) - Math.pow(sigma, 2) / 2.0;

            TimeSampler sampler = new TimeSampler(new LogNormalDistribution(sigma, mu, 522), 86400, random);
            double dur = sampler.nextSample();
            durations.addValue(dur);
            arrivals.addValue(t_arr);
            ProgressLogger.step();
        }
    }

    TDoubleDoubleHashMap hist = Histogram.createHistogram(durations,
            FixedSampleSizeDiscretizer.create(durations.getValues(), 1, 30), true);
    Histogram.normalize(hist);
    TXTWriter.writeMap(hist, "t", "n",
            "/Users/jillenberger/Work/socialnets/locationChoice/output/durations.txt");

    TDoubleDoubleHashMap correl = Correlations.mean(arrivals.getValues(), durations.getValues(),
            FixedSampleSizeDiscretizer.create(arrivals.getValues(), 1, 24));
    TXTWriter.writeMap(correl, "arr", "dur",
            "/Users/jillenberger/Work/socialnets/locationChoice/output/dur_arr.txt");
}

From source file:playground.johannes.studies.ivt.DensityPlotBiTree.java

public static void main(String[] args) throws IOException, FactoryException {
    SocialSampledGraphProjection<SocialSparseGraph, SocialSparseVertex, SocialSparseEdge> graph = GraphReaderFacade
            .read("/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/graph.graphml");

    SocialSampledGraphProjectionBuilder<SocialSparseGraph, SocialSparseVertex, SocialSparseEdge> builder = new SocialSampledGraphProjectionBuilder<SocialSparseGraph, SocialSparseVertex, SocialSparseEdge>();

    SpatialSparseGraph popData = new Population2SpatialGraph(CRSUtils.getCRS(21781))
            .read("/Users/jillenberger/Work/socialnets/data/schweiz/complete/plans/plans.0.10.xml");

    SimpleFeature feature = FeatureSHP/*from  ww  w.j  av  a2s  .  c o  m*/
            .readFeatures("/Users/jillenberger/Work/socialnets/data/schweiz/complete/zones/G1L08.shp")
            .iterator().next();
    Geometry chBorder = (Geometry) feature.getDefaultGeometry();
    chBorder.setSRID(21781);

    graph.getDelegate().transformToCRS(CRSUtils.getCRS(21781));

    logger.info("Applying spatial filter...");
    SpatialFilter filter = new SpatialFilter((GraphBuilder) builder, chBorder);
    graph = (SocialSampledGraphProjection<SocialSparseGraph, SocialSparseVertex, SocialSparseEdge>) filter
            .apply(graph);

    Set<Point> points = new HashSet<Point>();
    for (SpatialVertex v : graph.getVertices()) {
        points.add(v.getPoint());
    }

    GeometryFactory factory = new GeometryFactory();
    Point zrh = factory.createPoint(new Coordinate(8.55, 47.36));
    zrh = CRSUtils.transformPoint(zrh,
            CRS.findMathTransform(DefaultGeographicCRS.WGS84, CRSUtils.getCRS(21781)));

    //      graph.getDelegate().transformToCRS(DefaultGeographicCRS.WGS84);
    //      graph2.transformToCRS(DefaultGeographicCRS.WGS84);

    logger.info("Segmenting tiles...");
    //      BiTreeGrid<Double> sampleGrid = BiTreeGridBuilder.createEqualCountGrid(points, 200, 1000);
    Envelope env = PointUtils.envelope(points);
    SpatialGrid<Double> sampleGrid = new SpatialGrid<Double>(env.getMinX(), env.getMinY(), env.getMaxX(),
            env.getMaxY(), 5000);

    DistanceCalculator calc = new CartesianDistanceCalculator();
    Discretizer disc = new LinearDiscretizer(1000.0);

    logger.info("Creating survey grid...");
    for (Point p : points) {
        //         Tile<Double> tile = sampleGrid.getTile(p.getCoordinate());
        Double data = sampleGrid.getValue(p);
        double val = 0;
        //         if(tile.data != null)
        //            val = tile.data;
        if (data != null)
            val = data.doubleValue();

        double d = disc.discretize(calc.distance(p, zrh));
        d = Math.max(1, d);
        double proba = Math.pow(d, -1.4);
        //         val += 1/proba;
        val++;
        //         tile.data = new Double(val);
        sampleGrid.setValue(val, p);
    }

    logger.info("Creating population grid...");
    //      BiTreeGrid<Integer> popGrid = new BiTreeGrid<Integer>(sampleGrid);
    SpatialGrid<Integer> popGrid = new SpatialGrid<Integer>(sampleGrid);
    for (SpatialVertex v : popData.getVertices()) {
        //         Tile<Integer> tile = popGrid.getTile(v.getPoint().getCoordinate());
        Integer data = popGrid.getValue(v.getPoint());
        //         if (tile != null) {
        //            int val = 0;
        //            if (tile.data != null)
        //               val = tile.data;
        //            val++;
        //            tile.data = new Integer(val);
        //         }
        int val = 0;
        if (data != null) {
            val = data.intValue();
        }
        val++;
        popGrid.setValue(val, v.getPoint());
    }

    logger.info("Creating density grid...");
    //      BiTreeGrid<Double> densityGrid = new BiTreeGrid<Double>(sampleGrid);
    SpatialGrid<Double> densityGrid = new SpatialGrid<Double>(sampleGrid);

    //      Set<Tile<Double>> tiles = densityGrid.tiles();
    //      for(Tile<Double> tile : tiles) {
    //         Tile<Double> surveyTile = sampleGrid.getTile(tile.envelope.centre());
    //         Tile<Integer> popTile = popGrid.getTile(tile.envelope.centre());
    //         
    //         if(surveyTile != null && popTile != null) {
    //            if(surveyTile.data != null && popTile.data != null)
    //               tile.data = surveyTile.data/(double)popTile.data;
    //            else
    //               tile.data = new Double(0);
    //         } else
    //            tile.data = new Double(0);
    //      }

    for (int row = 0; row < densityGrid.getNumRows(); row++) {
        for (int col = 0; col < densityGrid.getNumCols(row); col++) {
            Integer inhabitants = popGrid.getValue(row, col);
            Double samples = sampleGrid.getValue(row, col);
            if (inhabitants != null && samples != null) {
                double density = samples / (double) inhabitants;
                densityGrid.setValue(row, col, density);
            } else {
                densityGrid.setValue(row, col, 0.0);
            }
        }
    }

    ZoneLayer<Double> layer = ZoneUtils.createGridLayer(5000, chBorder);
    layer.overwriteCRS(CRSUtils.getCRS(21781));
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int row = 0; row < densityGrid.getNumRows(); row++) {
        for (int col = 0; col < densityGrid.getNumCols(row); col++) {
            Point p = makeCoordinate(densityGrid, row, col);
            p.setSRID(21781);
            Zone<Double> z = layer.getZone(p);
            if (z != null) {
                double val = densityGrid.getValue(row, col);
                if (val > 0) {
                    stats.addValue(val);
                }
                z.setAttribute(val);
            }
        }
    }

    //      double min = stats.getMin();
    //      double max = stats.getPercentile(80);
    //      double bins = 20;
    //      double width = (max-min)/bins;
    //      Discretizer discretizer = new BoundedLinearDiscretizer(width, min, max);
    //      for(Zone<Double> z : layer.getZones()) {
    //         Double val = z.getAttribute();
    //         if(val != null && val > 0) {
    //            val = discretizer.index(val-min);
    //            z.setAttribute(val);
    //         }
    //      }

    ZoneLayerSHP.write(layer,
            "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/density.grid.shp");
    //      logger.info("Writing KML...");
    //      SpatialGridKMLWriter writer = new SpatialGridKMLWriter();
    //      writer.write(densityGrid, CRSUtils.getCRS(21781), "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/density.grid.kml");
    //      writer.write(densityGrid, "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/density.grid.kml");
    //      GeometryFactory factory = new GeometryFactory();
    //      Set<Geometry> geometries = new HashSet<Geometry>();
    //      TObjectDoubleHashMap values = new TObjectDoubleHashMap();
    ////      tiles = sampleGrid.tiles();
    //      for(Tile<Double> n : tiles) {
    //         Coordinate[] coords = new Coordinate[5];
    //         coords[0] = new Coordinate(n.envelope.getMinX(), n.envelope.getMinY());
    //         coords[1] = new Coordinate(n.envelope.getMinX(), n.envelope.getMaxY());
    //         coords[2] = new Coordinate(n.envelope.getMaxX(), n.envelope.getMaxY());
    //         coords[3] = new Coordinate(n.envelope.getMaxX(), n.envelope.getMinY());
    //         coords[4] = new Coordinate(n.envelope.getMinX(), n.envelope.getMinY());
    //         LinearRing shell = factory.createLinearRing(coords);
    //         shell.setSRID(21781);
    //         geometries.add(shell);
    //         values.put(shell, n.data);
    //      }
    //      
    //      NumericAttributeColorizer colorizer = new NumericAttributeColorizer(values);
    //      colorizer.setLogscale(true);
    //      FeatureKMLWriter writer = new FeatureKMLWriter();
    //      writer.setColorizable(colorizer);
    //      writer.write(geometries, "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/density.kml");
}

From source file:playground.johannes.studies.ivt.DiaryAnalyzer.java

/**
 * @param args/*from w w w. ja v a2 s . c o  m*/
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(
            new FileReader("/Users/jillenberger/Work/socialnets/data/ivt2009/05-2011/raw/Diary.csv"));

    String header = reader.readLine();
    String[] colNames = header.split(";");
    int typeIdx = getIndex(colNames, "\"Activity\"");
    int accHhIdx = getIndex(colNames, "\"Activity_Acc_HH\"");
    int accOtherIdx = getIndex(colNames, "\"Activity_Acc_Others\"");

    DescriptiveStatistics accHhStats = new DescriptiveStatistics();
    DescriptiveStatistics accOtherStats = new DescriptiveStatistics();

    String line;
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(";");
        String type = tokens[typeIdx];
        if (type.equals("\"Freizeit (offen)\"")) {
            String accHh = tokens[accHhIdx];
            String accOther = tokens[accOtherIdx];

            if (!accHh.equals("NA")) {
                int n = Integer.parseInt(accHh);
                if (n > 0)
                    accHhStats.addValue(n);
            }

            if (!accOther.equals("NA")) {
                int n = Integer.parseInt(accOther);
                if (n > 0)
                    accOtherStats.addValue(n);
            }
        }
    }
    System.out.println("Houshold members:");
    System.out.println(accHhStats.toString());
    System.out.println("Others:");
    System.out.println(accOtherStats.toString());
}

From source file:playground.johannes.studies.ivt.FracDimOpportunities.java

/**
 * @param args// ww w .  j  a  v  a2 s .  c  o m
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    String targetsFile = "/Users/jillenberger/Work/socialnets/data/schweiz/complete/plans/plans.0.001.xml";
    String chborderFile = "/Users/jillenberger/Work/socialnets/data/schweiz/complete/zones/G1L08.shp";
    String graphFile = "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/graph.graphml";
    String outFile = "/Users/jillenberger/Work/phd/doc/tex/ch3/fig/data/fdim.txt";

    SpatialSparseGraph targetGraph = new Population2SpatialGraph(CRSUtils.getCRS(21781)).read(targetsFile);
    List<Point> targetPoints = new ArrayList<Point>(targetGraph.getVertices().size());
    for (SpatialVertex v : targetGraph.getVertices()) {
        targetPoints.add(v.getPoint());
    }

    SimpleFeature feature = FeatureSHP.readFeatures(chborderFile).iterator().next();
    Geometry geometry = (Geometry) feature.getDefaultGeometry();
    geometry.setSRID(21781);

    SocialSampledGraphProjection<SocialSparseGraph, SocialSparseVertex, SocialSparseEdge> graph = GraphReaderFacade
            .read(graphFile);
    graph.getDelegate().transformToCRS(CRSUtils.getCRS(21781));
    Set<? extends SpatialVertex> egos = SnowballPartitions.createSampledPartition(graph.getVertices());
    List<Point> startPoints = new ArrayList<Point>(egos.size());
    for (SpatialVertex v : egos) {
        if (v.getPoint() != null) {
            if (geometry.contains(v.getPoint()))
                startPoints.add(v.getPoint());
        }
    }

    DescriptiveStatistics stats = new DescriptiveStatistics();

    DistanceCalculator dCalc = new CartesianDistanceCalculator();

    int N = (int) (startPoints.size() * targetPoints.size());
    ProgressLogger.init(N, 1, 5);

    for (int i = 0; i < startPoints.size(); i++) {
        Point p1 = startPoints.get(i);

        for (int j = 0; j < targetPoints.size(); j++) {
            Point p2 = targetPoints.get(j);
            double d = dCalc.distance(p1, p2);
            if (d > 0)
                stats.addValue(d);
            ProgressLogger.step();
        }
    }
    System.out.println("Creating histograms...");
    TDoubleDoubleHashMap hist = Histogram.createHistogram(stats,
            FixedSampleSizeDiscretizer.create(stats.getValues(), 100, 500), true);
    Histogram.normalize(hist);
    TXTWriter.writeMap(hist, "d", "p", outFile);
}

From source file:pro.foundev.strategies.BenchmarkStrategy.java

private void exec(Runnable runnable, String name, int runs, BenchmarkReport report) {
    logger.info("Starting run of " + name);
    DescriptiveStatistics stats = new DescriptiveStatistics();

    Stopwatch timer = new Stopwatch();
    for (int i = 0; i < runs; i++) {
        timer.start();/* w ww  .j av  a2 s.c o  m*/
        runnable.run();
        timer.stop();
        logger.info("Time to execute load run #" + i + " it took " + timer);
        stats.addValue(timer.elapsed(TimeUnit.MILLISECONDS));
        timer.reset();
    }
    logger.info("Finished run of " + name);
    report.addLine(name, stats.getMin(), stats.getMax(), stats.getPercentile(50), stats.getPercentile(90),
            stats.getMean());
}

From source file:rs.fon.whibo.GDT.component.removeInsignificantAttributes.ChiSquareTestCategorical.java

@Override
public LinkedList<Attribute> removeAttributes(ExampleSet exampleSet,
        LinkedList<Attribute> attributesForSplitting) {

    // checks if the example set is pure, and if it is, it exits the method
    Attribute label = exampleSet.getAttributes().getLabel();
    if (Tools.getAllCategories(exampleSet, label).size() < 2)
        return attributesForSplitting;

    // selects the attributes to be evaluated for removal (by calculating
    // chi-square probability for each attribute)
    ArrayList<Attribute> attributesToRemove = new ArrayList<Attribute>();
    ArrayList<Double> attributeProbabilities = new ArrayList<Double>();
    for (Attribute attr : attributesForSplitting)
        if (attr.isNominal()) {
            // calculate chi-square probability of the attribute
            double probability = 0;
            try {
                long[][] matrixForAttribute = getContigencyTable(exampleSet, attr);
                ChiSquareTestImpl chiTest = new ChiSquareTestImpl();
                probability = chiTest.chiSquareTest(matrixForAttribute);
            } catch (MathException me) {
                // System.out.println("Error in calculating math formula (chiTest)");
            }//www.ja  v a 2 s .  c o m
            // add the attribute to the list
            attributesToRemove.add(attr);
            attributeProbabilities.add(new Double(probability));
        }

    // calculates the percentile of the required percentage. Percentile
    // variable in code represents the percentage of attributes to be kept
    // (not removed)
    double percentile;
    DescriptiveStatistics stat = new DescriptiveStatistics();
    for (Double d : attributeProbabilities)
        stat.addValue(d.doubleValue());
    percentile = stat.getPercentile((1 - Percentage_Remove) * 100);

    // evaluates attributes and chooses the ones for removal (actually saves
    // the ones not for removal)
    Iterator<Attribute> iattr = attributesToRemove.iterator();
    Iterator<Double> iprob = attributeProbabilities.iterator();
    while (iattr.hasNext()) {
        iattr.next();
        Double prob = iprob.next();
        if (Use_Percentage_Instead == 0) {
            if (prob <= Alpha_Value) {
                iattr.remove();
                iprob.remove();
            }
        } else {
            if (prob <= percentile) {
                iattr.remove();
                iprob.remove();
            }
        }
    }

    // removes the attributes
    for (Attribute attr : attributesToRemove)
        attributesForSplitting.remove(attr);
    return attributesForSplitting;
}

From source file:rs.fon.whibo.GDT.component.removeInsignificantAttributes.FTestNumerical.java

public LinkedList<Attribute> removeAttributes(ExampleSet exampleSet,
        LinkedList<Attribute> attributesForSplitting) {
    // checks if the example set is pure, and if it is, it exits the method
    Attribute label = exampleSet.getAttributes().getLabel();
    if (Tools.getAllCategories(exampleSet, label).size() < 2)
        return attributesForSplitting;

    // selects the attributes to be evaluated for removal (by calculating
    // F-test probability for each attribute)
    ArrayList<Attribute> attributesToRemove = new ArrayList<Attribute>();
    ArrayList<Double> attributeProbabilities = new ArrayList<Double>();
    for (Attribute attr : attributesForSplitting)
        if (attr.isNumerical()) {
            // calculate F-test probability of the attribute
            double probability = 0;
            try {

                OneWayAnova fTest = new OneWayAnovaImpl();
                List<double[]> paramForFTest = getArraysByLabel(exampleSet, attr);

                // tests if no arrays for f-test has fewer that 2 elements
                boolean fTestImpossible = false;
                for (double[] i : paramForFTest)
                    if (i.length < 2)
                        fTestImpossible = true;

                // calculates ftest probability
                if (!fTestImpossible)
                    probability = fTest.anovaPValue(paramForFTest);

            } catch (Exception e) {
                // System.out.println("Error in calculating math formula (FTest)");
            }/*w  ww . j ava2  s  .  c om*/
            // add the attribute to the list
            attributesToRemove.add(attr);
            attributeProbabilities.add(new Double(probability));
        }

    if (attributesToRemove.size() == 0)
        return attributesForSplitting;

    // calculates the percentile of the required percentage. Percentile
    // variable in code represents the percentage of attributes to be kept
    // (not removed)
    double percentile;
    DescriptiveStatistics stat = new DescriptiveStatistics();
    for (Double d : attributeProbabilities)
        stat.addValue(d.doubleValue());
    percentile = stat.getPercentile((1 - Percentage_Remove) * 100);

    // evaluates attributes and chooses the ones for removal (actually saves
    // the ones not for removal)
    Iterator<Attribute> iattr = attributesToRemove.iterator();
    Iterator<Double> iprob = attributeProbabilities.iterator();
    while (iattr.hasNext()) {
        iattr.next();
        Double prob = iprob.next();
        if (Use_Percentage_Instead == 0) {
            if (prob <= Alpha_Value) {
                iattr.remove();
                iprob.remove();
            }
        } else {
            if (prob <= percentile) {
                iattr.remove();
                iprob.remove();
            }
        }
    }

    // removes the attributes
    for (Attribute attr : attributesToRemove)
        attributesForSplitting.remove(attr);
    return attributesForSplitting;

}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

public Map<String, List<DiscreteTimePoint>> getTimeSeriesMutantData(String parameter, List<String> genes,
        ArrayList<String> strains, String[] center, String[] sex) throws SolrServerException {

    Map<String, List<DiscreteTimePoint>> finalRes = new HashMap<String, List<DiscreteTimePoint>>(); // <allele_accession,
    // timeSeriesData>

    SolrQuery query = new SolrQuery().addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":experimental")
            .addFilterQuery(ObservationDTO.PARAMETER_STABLE_ID + ":" + parameter);

    String q = (strains.size() > 1) ? "(" + ObservationDTO.STRAIN_ACCESSION_ID + ":\""
            + StringUtils.join(strains.toArray(), "\" OR " + ObservationDTO.STRAIN_ACCESSION_ID + ":\"") + "\")"
            : ObservationDTO.STRAIN_ACCESSION_ID + ":\"" + strains.get(0) + "\"";

    if (genes != null && genes.size() > 0) {
        q += " AND (";
        q += (genes.size() > 1) ? ObservationDTO.GENE_ACCESSION_ID + ":\""
                + StringUtils.join(genes.toArray(), "\" OR " + ObservationDTO.GENE_ACCESSION_ID + ":\"") + "\""
                : ObservationDTO.GENE_ACCESSION_ID + ":\"" + genes.get(0) + "\"";
        q += ")";
    }//  w  w w. j  a va2s .c o m

    if (center != null && center.length > 0) {
        q += " AND (";
        q += (center.length > 1)
                ? ObservationDTO.PHENOTYPING_CENTER + ":\""
                        + StringUtils.join(center, "\" OR " + ObservationDTO.PHENOTYPING_CENTER + ":\"") + "\""
                : ObservationDTO.PHENOTYPING_CENTER + ":\"" + center[0] + "\"";
        q += ")";
    }

    if (sex != null && sex.length == 1) {
        q += " AND " + ObservationDTO.SEX + ":\"" + sex[0] + "\"";
    }

    query.setQuery(q);
    query.set("group.field", ObservationDTO.GENE_SYMBOL);
    query.set("group", true);
    query.set("fl", ObservationDTO.DATA_POINT + "," + ObservationDTO.DISCRETE_POINT);
    query.set("group.limit", 100000); // number of documents to be returned
    // per group
    query.set("group.sort", ObservationDTO.DISCRETE_POINT + " asc");
    query.setRows(10000);

    // System.out.println("+_+_+ " + solr.getBaseURL() + "/select?" +
    // query);
    List<Group> groups = solr.query(query).getGroupResponse().getValues().get(0).getValues();
    // for mutants it doesn't seem we need binning
    // groups are the alleles
    for (Group gr : groups) {
        SolrDocumentList resDocs = gr.getResult();
        DescriptiveStatistics stats = new DescriptiveStatistics();
        float discreteTime = (float) resDocs.get(0).getFieldValue(ObservationDTO.DISCRETE_POINT);
        ArrayList<DiscreteTimePoint> res = new ArrayList<DiscreteTimePoint>();
        for (int i = 0; i < resDocs.getNumFound(); i++) {
            SolrDocument doc = resDocs.get(i);
            stats.addValue((float) doc.getFieldValue(ObservationDTO.DATA_POINT));
            if (discreteTime != (float) doc.getFieldValue(ObservationDTO.DISCRETE_POINT)
                    || i == resDocs.getNumFound() - 1) { // we
                // are
                // at
                // the
                // end
                // of
                // the
                // document
                // list
                // add to list
                float discreteDataPoint = (float) stats.getMean();
                DiscreteTimePoint dp = new DiscreteTimePoint(discreteTime, discreteDataPoint,
                        new Float(stats.getStandardDeviation()));
                List<Float> errorPair = new ArrayList<>();
                Float lower = new Float(discreteDataPoint);
                Float higher = new Float(discreteDataPoint);
                errorPair.add(lower);
                errorPair.add(higher);
                dp.setErrorPair(errorPair);
                res.add(dp);
                // update discrete point
                discreteTime = Float.valueOf(doc.getFieldValue(ObservationDTO.DISCRETE_POINT).toString());
                // update stats
                stats = new DescriptiveStatistics();
            }
        }
        // add list
        finalRes.put(gr.getGroupValue(), res);
    }
    return finalRes;
}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

public List<DiscreteTimePoint> getTimeSeriesControlData(String parameter, ArrayList<String> strains,
        String[] center, String[] sex) throws SolrServerException {

    ArrayList<DiscreteTimePoint> res = new ArrayList<DiscreteTimePoint>();
    SolrQuery query = new SolrQuery().addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":control")
            .addFilterQuery(ObservationDTO.PARAMETER_STABLE_ID + ":" + parameter);
    String q = (strains.size() > 1) ? "(" + ObservationDTO.STRAIN_ACCESSION_ID + ":\""
            + StringUtils.join(strains.toArray(), "\" OR " + ObservationDTO.STRAIN_ACCESSION_ID + ":\"") + "\")"
            : ObservationDTO.STRAIN_ACCESSION_ID + ":\"" + strains.get(0) + "\"";

    if (center != null && center.length > 0) {
        q += " AND (";
        q += (center.length > 1)// ww w.j a  v  a2  s .  c  o m
                ? ObservationDTO.PHENOTYPING_CENTER + ":\""
                        + StringUtils.join(center, "\" OR " + ObservationDTO.PHENOTYPING_CENTER + ":\"") + "\""
                : ObservationDTO.PHENOTYPING_CENTER + ":\"" + center[0] + "\"";
        q += ")";
    }

    if (sex != null && sex.length == 1) {
        q += " AND " + ObservationDTO.SEX + ":\"" + sex[0] + "\"";
    }

    query.setQuery(q);
    query.set("group.field", ObservationDTO.DISCRETE_POINT);
    query.set("group", true);
    query.set("fl", ObservationDTO.DATA_POINT + "," + ObservationDTO.DISCRETE_POINT);
    query.set("group.limit", 100000); // number of documents to be returned
    // per group
    query.set("sort", ObservationDTO.DISCRETE_POINT + " asc");
    query.setRows(10000);

    // System.out.println("+_+_+ " + solr.getBaseURL() + "/select?" +
    // query);
    List<Group> groups = solr.query(query).getGroupResponse().getValues().get(0).getValues();
    boolean rounding = false;
    // decide if binning is needed i.e. is the increment points are too
    // scattered, as for calorimetry
    if (groups.size() > 30) { // arbitrary value, just piced it because it
        // seems reasonable for the size of our
        // graphs
        if (Float.valueOf(groups.get(groups.size() - 1).getGroupValue())
                - Float.valueOf(groups.get(0).getGroupValue()) <= 30) { // then
            // rounding
            // will
            // be
            // enough
            rounding = true;
        }
    }
    if (rounding) {
        int bin = Math.round(Float.valueOf(groups.get(0).getGroupValue()));
        for (Group gr : groups) {
            int discreteTime = Math.round(Float.valueOf(gr.getGroupValue()));
            // for calormetry ignore what's before -5 and after 16
            if (parameter.startsWith("IMPC_CAL") || parameter.startsWith("ESLIM_003_001")
                    || parameter.startsWith("M-G-P_003_001")) {
                if (discreteTime < -5) {
                    continue;
                } else if (discreteTime > 16) {
                    break;
                }
            }
            float sum = 0;
            SolrDocumentList resDocs = gr.getResult();
            DescriptiveStatistics stats = new DescriptiveStatistics();
            for (SolrDocument doc : resDocs) {
                sum += (float) doc.getFieldValue(ObservationDTO.DATA_POINT);
                stats.addValue((float) doc.getFieldValue(ObservationDTO.DATA_POINT));
            }
            if (bin < discreteTime || groups.indexOf(gr) == groups.size() - 1) { // finished
                // the
                // groups
                // of
                // filled
                // the
                // bin
                float discreteDataPoint = sum / resDocs.getNumFound();
                DiscreteTimePoint dp = new DiscreteTimePoint((float) discreteTime, discreteDataPoint,
                        new Float(stats.getStandardDeviation()));
                List<Float> errorPair = new ArrayList<>();
                double std = stats.getStandardDeviation();
                Float lower = new Float(discreteDataPoint - std);
                Float higher = new Float(discreteDataPoint + std);
                errorPair.add(lower);
                errorPair.add(higher);
                dp.setErrorPair(errorPair);
                res.add(dp);
                bin = discreteTime;
            }
        }
    } else {
        for (Group gr : groups) {
            Float discreteTime = Float.valueOf(gr.getGroupValue());
            float sum = 0;
            SolrDocumentList resDocs = gr.getResult();
            DescriptiveStatistics stats = new DescriptiveStatistics();
            for (SolrDocument doc : resDocs) {
                sum += (float) doc.getFieldValue(ObservationDTO.DATA_POINT);
                stats.addValue((float) doc.getFieldValue(ObservationDTO.DATA_POINT));
            }
            float discreteDataPoint = sum / resDocs.getNumFound();
            DiscreteTimePoint dp = new DiscreteTimePoint(discreteTime, discreteDataPoint,
                    new Float(stats.getStandardDeviation()));
            List<Float> errorPair = new ArrayList<>();
            double std = stats.getStandardDeviation();
            Float lower = new Float(discreteDataPoint - std);
            Float higher = new Float(discreteDataPoint + std);
            errorPair.add(lower);
            errorPair.add(higher);
            dp.setErrorPair(errorPair);
            res.add(dp);
        }
    }
    return res;
}