V1_0.java [src/java/m/example/stats] Revision:   Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.example.stats;

import com.google.common.math.Quantiles;
import com.google.common.math.Stats;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Path;
import csip.annotations.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

@Name("Stats")
@Description("Statistics")
@Path("m/stats/1.0")
public class V1_0 extends csip.ModelDataService {

  // this is comment.
  @Override
  protected void doProcess() throws Exception {

    List<Double> l = new ArrayList<>();

    JSONArray v = parameter().getJSONArray("result");
    JSONObject ts = v.getJSONObject(0);
    JSONArray a = ts.getJSONArray("data");
    for (int i = 0; i < a.length(); i++) {
      JSONArray elem = a.getJSONArray(i);
      l.add(elem.getDouble(1));
    }

    double[] vals = toDoubleArray(l);
    System.out.println(l);

    Stats s = Stats.of(vals);
    Map<Integer, Double> uncertResult = Quantiles.quartiles().indexes(0, 1, 2, 3, 4).compute(vals);
    double min = uncertResult.get(0);
    double q1 = uncertResult.get(1);
    double med = uncertResult.get(2);
    double q3 = uncertResult.get(3);
    double max = uncertResult.get(4);

    int i = 0;
//    for (i = 0; i < 100000; i++) {
      results().put("mean" + i , s.mean(), "Mean");
      results().put("count"+ i, s.count(), "Count");
      results().put("pop_stdev+ i", s.populationStandardDeviation(), "populationStandardDeviation");
      results().put("pop_var"+ i, s.populationVariance(), "populationVariance");
      results().put("min"+ i, min, "minimum");
      results().put("q1"+ i, q1, "1st Quartile");
      results().put("median"+ i, med, "Median");
      results().put("q3"+ i, q3, "3nd Quartile");
      results().put("max"+ i, max, "maximum");
//    }
  }


  public static double[] toDoubleArray(List<Double> data) {
    return data.stream().mapToDouble(Double::doubleValue).toArray();
  }
}