package water.api;

import hex.AUC2;
import hex.ModelMetricsBinomial;
import water.util.TwoDimTable;

import java.util.Arrays;

public class ModelMetricsBinomialV3<I extends ModelMetricsBinomial, S extends ModelMetricsBinomialV3<I, S>> extends ModelMetricsBase<I,S> {
//  @API(help="The standard deviation of the training response.", direction=API.Direction.OUTPUT)
//  public double sigma; // Belongs in a mythical ModelMetricsSupervisedV3

  @API(help="The R^2 for this scoring run.", direction=API.Direction.OUTPUT)
  public double r2;

  @API(help="The logarithmic loss for this scoring run.", direction=API.Direction.OUTPUT)
  public double logloss;

  @API(help="The AUC for this scoring run.", direction=API.Direction.OUTPUT)
  public double AUC;

  @API(help="The Gini score for this scoring run.", direction=API.Direction.OUTPUT)
  public double Gini;

//  @API(help = "The ConfusionMatrix at the threshold for maximum F1.", direction = API.Direction.OUTPUT)
//  public ConfusionMatrixBase cm;

  @API(help = "The Metrics for various thresholds.", direction = API.Direction.OUTPUT)
  public TwoDimTableBase thresholds_and_metric_scores;

  @API(help = "The Metrics for various criteria.", direction = API.Direction.OUTPUT)
  public TwoDimTableBase max_criteria_and_metric_scores;

  @Override
  public ModelMetricsBinomialV3 fillFromImpl(ModelMetricsBinomial modelMetrics) {
    super.fillFromImpl(modelMetrics);
//    sigma = modelMetrics._sigma;
    r2 = modelMetrics.r2();
    logloss = modelMetrics._logloss;

    AUC2 auc = modelMetrics._auc;
    if (null != auc) {
      AUC  = auc._auc;
      Gini = auc._gini;

      // Fill TwoDimTable
      String[] thresholds = new String[auc._nBins];
      for( int i=0; i<auc._nBins; i++ )
        thresholds[i] = Double.toString(auc._ths[i]);
      AUC2.ThresholdCriterion crits[] = AUC2.ThresholdCriterion.VALUES;
      String[] colHeaders = new String[crits.length+1];
      String[] types      = new String[crits.length+1];
      String[] formats    = new String[crits.length+1];
      int i=0;
      for( i=0; i<crits.length; i++ ) {
        colHeaders[i] = crits[i].toString();
        types     [i] = crits[i]._isInt ? "long" : "double";
        formats   [i] = crits[i]._isInt ? "%d"   : "%f"    ;
      }
      colHeaders[i] = "idx"; types[i] = "integer"; formats[i] = "%d";
      TwoDimTable thresholdsByMetrics = new TwoDimTable("Thresholds x Metric Scores", null, thresholds, colHeaders, types, formats, "Thresholds" );
      for( i=0; i<auc._nBins; i++ ) {
        int j;
        for (j = 0; j < crits.length; j++) {
          double d = crits[j].exec(auc, i); // Note: casts to Object are NOT redundant
          thresholdsByMetrics.set(i, j, crits[j]._isInt ? (Object) ((long) d) : d);
        }
        thresholdsByMetrics.set(i, j, i);
      }
      this.thresholds_and_metric_scores = new TwoDimTableV3().fillFromImpl(thresholdsByMetrics);

      // Fill TwoDimTable
      TwoDimTable maxMetrics = new TwoDimTable("Maximum Metric", null, Arrays.copyOf(colHeaders, crits.length),
              new String[]{"Threshold","Value","idx"},
              new String[]{"double",   "double","long"},
              new String[]{"%f",       "%f",    "%d"},
              "Metric" );
      for( i=0; i<crits.length; i++ ) {
        int idx = crits[i].max_criterion_idx(auc);
        maxMetrics.set(i,0,idx==-1 ? Double.NaN : auc._ths[idx]);
        maxMetrics.set(i,1,idx==-1 ? Double.NaN : crits[i].exec(auc,idx));
        maxMetrics.set(i,2,idx);
      }

      max_criteria_and_metric_scores = new TwoDimTableV3().fillFromImpl(maxMetrics);
    }
    return this;
  }
}

@@ -1909,33 +1909,33 @@
 ng%5Bcrits.length+
-1
+2
 %5D;%0A      String%5B
@@ -1965,33 +1965,33 @@
 ng%5Bcrits.length+
-1
+2
 %5D;%0A      String%5B
@@ -2033,12 +2033,99 @@
 gth+
-1
+2
 %5D;%0A
+      colHeaders%5B0%5D = %22Threshold%22;%0A      types%5B0%5D = %22double%22;%0A      formats%5B0%5D = %22%25f%22;%0A
     
@@ -2131,18 +2131,16 @@
    int i
-=0
 ;%0A      
@@ -2189,24 +2189,26 @@
 colHeaders%5Bi
++1
 %5D = crits%5Bi%5D
@@ -2236,24 +2236,26 @@
 types     %5Bi
++1
 %5D = crits%5Bi%5D
@@ -2303,16 +2303,18 @@
 ats   %5Bi
++1
 %5D = crit
@@ -2372,16 +2372,18 @@
 eaders%5Bi
++1
 %5D = %22idx
@@ -2392,16 +2392,18 @@
  types%5Bi
++1
 %5D = %22int
@@ -2418,16 +2418,18 @@
 ormats%5Bi
++1
 %5D = %22%25d%22
@@ -2491,53 +2491,67 @@
 le(%22
-Thresholds x Metric Scores%22, null, thresholds
+Metric Scores for Thresholds%22, null, new String%5Bauc._nBins%5D
 , co
@@ -2580,20 +2580,12 @@
 ts, 
-%22Thresholds%22
+null
  );%0A
@@ -2635,16 +2635,72 @@
    int j
+=0;%0A        thresholdsByMetrics.set(i, j, thresholds%5Bi%5D)
 ;%0A      
@@ -2862,16 +2862,18 @@
 .set(i, 
+1+
 j, crits
@@ -2956,16 +2956,18 @@
 .set(i, 
+1+
 j, i);%0A 
@@ -3159,16 +3159,17 @@
 m Metric
+s
 %22, null,
@@ -3182,16 +3182,21 @@
 s.copyOf
+Range
 (colHead
@@ -3199,16 +3199,19 @@
 Headers,
+ 1,
  crits.l
@@ -3215,16 +3215,18 @@
 s.length
++1
 ),%0A     

Revert "Revert "Fix formatting of Model Metrics TwoDimTable: don't use thresholds as rowHeaders, and rename tables.""