package hex.genmodel.algos.deepwater;

import deepwater.backends.BackendParams;
import deepwater.backends.RuntimeOptions;
import deepwater.datasets.ImageDataSet;
import hex.genmodel.ModelMojoReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;


/**
 */
public class DeepwaterMojoReader extends ModelMojoReader<DeepwaterMojoModel> {

  @Override
  protected void readModelData() throws IOException {
    try {
      _model._network = readblob("model_network");
      _model._parameters = readblob("model_params");
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    _model._backend = DeepwaterMojoModel.createDeepWaterBackend((String) readkv("backend")); // new ImageTrain(_width, _height, _channels, _deviceID, (int)parameters.getOrMakeRealSeed(), _gpu);
    _model._problem_type = readkv("problem_type");
    _model._mini_batch_size = readkv("mini_batch_size");
    _model._height = readkv("height");
    _model._width = readkv("width");
    _model._channels = readkv("channels");
    _model._nums = readkv("nums");
    _model._cats = readkv("cats");
    _model._catOffsets = readkv("cat_offsets");
    _model._normMul = readkv("norm_mul");
    _model._normSub = readkv("norm_sub");
    _model._normRespMul = readkv("norm_resp_mul");
    _model._normRespSub = readkv("norm_resp_sub");
    _model._useAllFactorLevels = readkv("use_all_factor_levels");

    _model._imageDataSet = new ImageDataSet(_model._width, _model._height, _model._channels, _model._nclasses);

    _model._opts = new RuntimeOptions();
    _model._opts.setSeed(0); // ignored - not needed during scoring
    _model._opts.setUseGPU((boolean)readkv("gpu"));
    _model._opts.setDeviceID((int[])readkv("device_id"));

    _model._backendParams = new BackendParams();
    _model._backendParams.set("mini_batch_size", 1);

    File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString() + ".json");
    try {
      FileOutputStream os = new FileOutputStream(file.toString());
      os.write(_model._network);
      os.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    _model._model = _model._backend.buildNet(_model._imageDataSet, _model._opts, _model._backendParams, _model._nclasses, file.toString());
    String mean_image_file = readkv("mean_image_file");
    if (mean_image_file != null)
      _model._imageDataSet.setMeanData(_model._backend.loadMeanImage(_model._model, mean_image_file));
    _model._meanImageData = _model._imageDataSet.getMeanData();

    file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    try {
      FileOutputStream os = new FileOutputStream(file.toString());
      os.write(_model._parameters);
      os.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    _model._backend.loadParam(_model._model, file.toString());
  }

  @Override
  protected DeepwaterMojoModel makeModel(String[] columns, String[][] domains) {
    return new DeepwaterMojoModel(columns, domains);
  }
}

@@ -2312,39 +2312,100 @@
     
-String
+// 1) read the raw bytes of the
  mean
-_
+ 
 image
-_
+ 
 file 
+from the MOJO%0A    byte%5B%5D meanBlob 
 = read
-kv
+blob
 (%22me
@@ -2437,132 +2437,654 @@
 mean
-_
+Blob!=null) %7B%0A      // 2) write the mean 
 image
-_
+ 
 file
- != null)%0A      _model._imageDataSet.setMeanData(_model._backend.loadMeanImage(_model._model, mean_image_file));%0A
+%0A      File meanFile = new File(System.getProperty(%22java.io.tmpdir%22), UUID.randomUUID().toString() + %22.mean%22);%0A      try %7B%0A        FileOutputStream os = new FileOutputStream(meanFile.toString());%0A        os.write(meanBlob);%0A        os.close();%0A      %7D catch (IOException e) %7B%0A        e.printStackTrace();%0A      %7D%0A      // 3) tell the backend to use that mean image file (just in case it needs it)%0A      _model._imageDataSet.setMeanData(_model._backend.loadMeanImage(_model._model, meanFile.toString()));%0A%0A      // 4) keep a float%5B%5D version of the mean array to be used during image processing%0A  
     
@@ -3142,16 +3142,22 @@
 nData();
+%0A    %7D
 %0A%0A    fi

Fix putting the mean_image_file into the MOJO.