package hex.deeplearning;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import water.DKV;
import water.H2O;
import water.Key;
import water.TestUtil;
import water.fvec.Frame;
import water.fvec.NFSFileVec;
import water.fvec.RebalanceDataSet;
import water.parser.ParseDataset;

/**
 * This test simulates environment
 * produced by Spark - dataset divided into
 * many small chunks,  some of theam are empty.
 */
public class DeepLearningScoreTest extends TestUtil {
  @BeforeClass
  public static void setup() { stall_till_cloudsize(1); }

  /** Load simple dataset, rebalance to a number of chunks > number of rows, and run deep learning */
  @Test @Ignore public void testPubDev928() {
    // Create rebalanced dataset
    Key rebalancedKey = Key.make("rebalanced");
    NFSFileVec nfs = NFSFileVec.make(find_test_file("smalldata/logreg/prostate.csv"));
    Frame fr = ParseDataset.parse(Key.make(), nfs._key);
    RebalanceDataSet rb = new RebalanceDataSet(fr, rebalancedKey, (int)(fr.numRows()+1));
    H2O.submitTask(rb);
    rb.join();
    Frame rebalanced = DKV.get(rebalancedKey).get();

    DeepLearningModel dlModel = null;
    try {
      // Launch Deep Learning
      DeepLearningModel.DeepLearningParameters dlParams = new DeepLearningModel.DeepLearningParameters();
      dlParams._train = rebalancedKey;
      dlParams._epochs = 5;
      dlParams._response_column = "CAPSULE";

      dlModel = new DeepLearning(dlParams).trainModel().get();
    } finally {
      fr.delete();
      rebalanced.delete();
      if (dlModel != null) dlModel.delete();
    }
  }
}

@@ -37,27 +37,22 @@
 g.junit.
-BeforeClass
+Assert
 ;%0Aimport
@@ -62,22 +62,27 @@
 g.junit.
-Ignore
+BeforeClass
 ;%0Aimport
@@ -266,16 +266,39 @@
 ataSet;%0A
+import water.fvec.Vec;%0A
 import w
@@ -580,17 +580,17 @@
 oudsize(
-1
+5
 ); %7D%0A%0A  
@@ -701,16 +701,8 @@
 est 
-@Ignore 
 publ
@@ -1135,16 +1135,181 @@
 get();%0A%0A
+    // Assert that there is at least one 0-len chunk%0A    assertZeroLengthChunk(%22Rebalanced dataset should contain at least one 0-len chunk!%22, rebalanced.anyVec());%0A%0A
     Deep
@@ -1763,24 +1763,24 @@
 l.delete();%0A
-
     %7D%0A  %7D%0A%7D%0A
@@ -1776,11 +1776,322 @@
    %7D%0A  %7D
+%0A%0A  private void assertZeroLengthChunk(String msg, Vec v) %7B%0A    boolean hasZeroLenChunk = false;%0A    for (int i = 0; i %3C v.nChunks(); i++) %7B%0A      hasZeroLenChunk %7C= (v.chunkForChunkIdx(i).len() == 0);%0A      System.out.println(v.chunkForChunkIdx(i).len());%0A    %7D%0A    Assert.assertTrue(msg, hasZeroLenChunk);%0A  %7D
 %0A%7D%0A

Enable test for scoring on 0-len chunks.