V1_0.java [src/java/m/example/stats] Revision: 40aab24da33ea707a6675327de07c49f9c39b07a  Date: Sat Aug 18 14:10:53 MDT 2018
/*
 * 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 oms3.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 {

    @Override
    protected void doProcess() throws Exception {
        
        List<Double> l = new ArrayList<>();
        
        JSONArray v = getJSONArrayParam("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);
        
        putResult("mean", s.mean(),"Mean");
        putResult("count", s.count(),"Count");
        putResult("pop_stdev", s.populationStandardDeviation(), "populationStandardDeviation");
        putResult("pop_var", s.populationVariance(), "populationVariance");
        putResult("min", min,"minimum");
        putResult("q1", q1,"1st Quartile");
        putResult("median", med,"Median");
        putResult("q3", q3,"3nd Quartile");
        putResult("max", max,"maximum");
       
        
    }
    
    public static double[] toDoubleArray(List<Double> data) {
        return data.stream().mapToDouble(Double::doubleValue).toArray();
    }
}