package io.sigpipe.sing.dataset;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.sigpipe.sing.adapters.ReadMetadata;
import io.sigpipe.sing.dataset.feature.Feature;
import io.sigpipe.sing.stat.RunningStatistics;
import io.sigpipe.sing.stat.SquaredError;
import io.sigpipe.sing.util.TestConfiguration;

public class TickEvaluator {

    private Quantizer quantizer;
    private Map<Feature, RunningStatistics> statMap = new HashMap<>();

    public TickEvaluator(Quantizer quantizer) {
        this.quantizer = quantizer;
    }

    public void train(List<Feature> features) {
        for (Feature feature : features) {
            Feature q = quantizer.quantize(feature);
            RunningStatistics rs = statMap.get(q);
            if (rs == null) {
                rs = new RunningStatistics();
                statMap.put(q, rs);
            }
            rs.put(feature.getDouble());
        }
    }

    public void evaluate(List<Feature> features) {
        RunningStatistics allStats = new RunningStatistics();

        Map<Feature, SquaredError> errors = new HashMap<>();
        for (Feature feature : features) {
            allStats.put(feature.getDouble());

            Feature q = quantizer.quantize(feature);
            RunningStatistics rs = statMap.get(q);
            double mean = rs.mean();
            double actual = feature.getDouble();
            SquaredError se = errors.get(q);
            if (se == null) {
                se = new SquaredError();
                errors.put(q, se);
            }
            se.put(actual, mean);
        }

        for (Feature q : errors.keySet()) {
            SquaredError se = errors.get(q);
            double nrmse = se.RMSE() / (allStats.max() - allStats.min());
            double cvrmse = se.RMSE() / allStats.mean();
            System.out.println(q + "\t" + se.RMSE() + "\t" + nrmse
                    + "\t" + cvrmse);
        }

        System.out.println(allStats);
    }
    }
}

@@ -2025,21 +2025,946 @@
 tats);%0A    %7D
+%0A%0A    public static void main(String%5B%5D args)%0A    throws Exception %7B%0A        String targetFeature = %22temperature_surface%22;%0A        List%3CFeature%3E features = new ArrayList%3C%3E();%0A        for (String fileName : args) %7B%0A            System.err.println(%22Reading: %22 + fileName);%0A            List%3CMetadata%3E meta = ReadMetadata.readMetaBlob(new File(fileName));%0A            for (Metadata m : meta) %7B%0A                Feature f = m.getAttribute(targetFeature);%0A                if (f == null) %7B%0A                    System.err.println(%22Feature not found%22);%0A                    continue;%0A                %7D else %7B%0A                    System.out.println(f.getString());%0A                    features.add(f);%0A                %7D%0A            %7D%0A        %7D%0A        System.exit(0);%0A%0A        TickEvaluator te = new TickEvaluator(%0A                TestConfiguration.quantizers.get(targetFeature));%0A        te.train(features);%0A        te.evaluate(features);
 %0A    %7D%0A%7D%0A

Add a simple test scenario