package playground.h2o;

import hex.kmeans.KMeans;
import hex.kmeans.KMeansModel;
import water.H2O;
import water.Key;
import water.fvec.Frame;
import water.fvec.NFSFileVec;
import water.fvec.Vec;

import java.io.File;
import java.util.Arrays;

public class Main {
  private static void initialize() {
// Setup cloud name
    String[] args = new String[]{"-name", "h2o_kmeans_test"};
// Build a cloud of 1
    H2O.main(args);
    H2O.waitForCloudSize(1, 10 * 1000 /* ms */);
  }

  private static void runKMeans() {
    File f = new File("/Users/apple/Downloads/whiskey.csv");
    NFSFileVec nfs = NFSFileVec.make(f);
    Frame frame = water.parser.ParseDataset.parse(Key.make(), nfs._key);
    KMeansModel.KMeansParameters params = new KMeansModel.KMeansParameters();
    params._train = frame._key;
    params._k = 5;
    params._max_iterations = 1000;
    KMeans job = new KMeans(params);
    KMeansModel kmm = job.trainModel().get();
    Frame output = kmm.score(frame);
    Vec[] vecs = output.vecs();
    System.out.print(Arrays.toString(vecs));
    for (Vec v : vecs) {
      System.out.println(v + ", " + v.length());
      for (int i = 0; i < v.length(); i++) {
        System.out.println(i + ": " + v.at(i));
      }
    }
    System.out.println();
  }

  public static void main(String[] args) throws Exception {
    initialize();
    runKMeans();
  }
}
