Skip to content

Commit

Permalink
PP-6: first iteration of /2/Models endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpeck committed Apr 9, 2014
1 parent 725e00d commit e17a0d8
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ target/
.settings/*
.idea/*
h2o.iml
h2o.eml
*.eml
h2o-samples/h2o-samples.iml
experiments/experiments.iml
/libpeerconnection.log
Expand All @@ -40,3 +40,5 @@ R/.Rhistory
R/tests/Rsandbox/
Rsandbox
h2o-perf/perf-target/*jar
semantic.cache
.Rhistory
6 changes: 4 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/bash
#! /bin/bash -x

# determine the correct separator of multiple paths
if [ `uname` = "Darwin" ]
Expand Down Expand Up @@ -65,6 +65,8 @@ fi
JAVAC_ARGS="-g
-source 1.6
-target 1.6
-J-Xms1024m
-J-Xmx1024m
-XDignore.symbol.file
-Xlint:all
-Xlint:-deprecation
Expand Down Expand Up @@ -188,7 +190,7 @@ function build_javadoc() {
echo "creating javadoc files..."
local CLASSPATH="${JAR_ROOT}${SEP}${DEPENDENCIES}${SEP}${JAR_ROOT}/hadoop/${DEFAULT_HADOOP_VERSION}/*"
mkdir -p target/logs
"${JAVADOC}" -overview ${SRC}/overview.html -classpath "${CLASSPATH}" -d "${OUTDIR}"/javadoc -sourcepath "${SRC}" -subpackages hex:water >& target/logs/javadoc_build.log
"${JAVADOC}" -J-Xms256m -J-Xmx256m -overview ${SRC}/overview.html -classpath "${CLASSPATH}" -d "${OUTDIR}"/javadoc -sourcepath "${SRC}" -subpackages hex:water >& target/logs/javadoc_build.log
}

function junit() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/hex/glm/GLMModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ public static class GLMXValidationTask extends GLMValidationTask<GLMXValidationT
}
}

public GLMParams getParams() {
return glm;
}

public Key getJobKey() {
return job_key;
}

@Override
public String toString(){
final double [] beta = beta(), norm_beta = norm_beta();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hex/glm/GLMParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,8 @@ static final double y_log_y(double y, double mu) {
return (y != 0) ? (y * Math.log(y / mu)) : 0;
}

public Family getFamily() {
return family;
}

}
150 changes: 150 additions & 0 deletions src/main/java/water/api/Models.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package water.api;

import java.util.*;

import com.google.gson.*;
import water.*;
import water.util.Log;

public class Models extends Request2 {

///////////////////////
// Request2 boilerplate
///////////////////////
static final int API_WEAVER=1; // This file has auto-gen'd doc & json fields
static public DocGen.FieldDoc[] DOC_FIELDS; // Initialized from Auto-Gen code.

// This Request supports the HTML 'GET' command, and this is the help text
// for GET.
static final String DOC_GET = "Return the list of models.";

public static String link(Key k, String content){
return "<a href='/2/models'>" + content + "</a>";
}
///////////////////////


public static final Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().setPrettyPrinting().create();

private class ModelSummary {
public String model_type = "unknown";
public String family = "unknown";
public String state = "unknown";
public List<String> input_column_names = new ArrayList<String>();
public String response_column_name = null;
}

/**
* Summarize fields which are generic to water.Model.
*/
private void summarizeModel(ModelSummary summary, Value value, water.Model model) {
String[] names = model._names;

summary.model_type = model.getClass().toString();

summary.response_column_name = names[names.length - 1];

for (int i = 0; i < names.length - 1; i++)
summary.input_column_names.add(names[i]);
}

/**
* Summarize fields which are specific to hex.glm.GLMModel.
*/
private void summarizeGLMModel(ModelSummary summary, Value value, hex.glm.GLMModel model) {
// add generic fields such as column names
summarizeModel(summary, value, model);

summary.model_type = "GLM";
summary.family = model.getParams().getFamily().toString();

// Job.JobHandle = (Job)DKV.get(model.getJobKey());
// summary.state = job.state.toString());
}

/**
* Summarize fields which are specific to hex.drf.DRF.DRFModel.
*/
private void summarizeDRFModel(ModelSummary summary, Value value, hex.drf.DRF.DRFModel model) {
// add generic fields such as column names
summarizeModel(summary, value, model);

summary.model_type = "DRF";
// summary.family = model.getParams().getFamily().toString();

// Job.JobHandle = (Job)DKV.get(model.getJobKey());
// summary.state = job.state.toString());
}

/**
* Summarize fields which are specific to hex.deeplearning.DeepLearningModel.
*/
private void summarizeDeepLearningModel(ModelSummary summary, Value value, hex.deeplearning.DeepLearningModel model) {
// add generic fields such as column names
summarizeModel(summary, value, model);

summary.model_type = "DeepLearning";
// summary.family = model.getParams().getFamily().toString();

// Job.JobHandle = (Job)DKV.get(model.getJobKey());
// summary.state = job.state.toString());
}

/**
* Summarize fields which are specific to hex.gbm.GBM.GBMModel.
*/
private void summarizeGBMModel(ModelSummary summary, Value value, hex.gbm.GBM.GBMModel model) {
// add generic fields such as column names
summarizeModel(summary, value, model);

summary.model_type = "GBM";
// summary.family = model.getParams().getFamily().toString();

// Job.JobHandle = (Job)DKV.get(model.getJobKey());
// summary.state = job.state.toString());
}

@Override
protected Response serve() {

// Get all the model keys. Right now it's a hack to determine which values are models.
Set<Key> keySet = H2O.globalKeySet(null);

Map map = new TreeMap(); // Sort for pretty display and reliable ordering.
for (Key key : keySet) {
String keyString = key.toString();
ModelSummary summary = new ModelSummary();

Value value = DKV.get(key);
// TODO: we don't have a way right now of getting the type without deserializing to a POJO.
// This is going to deserialize the enture KV store. We need a less brute-force way.
Iced pojo = value.get();

if (pojo instanceof hex.glm.GLMModel) {
summarizeGLMModel(summary, value, (hex.glm.GLMModel) pojo);
} else if (pojo instanceof hex.drf.DRF.DRFModel) {
summarizeDRFModel(summary, value, (hex.drf.DRF.DRFModel) pojo);
} else if (pojo instanceof hex.deeplearning.DeepLearningModel) {
summarizeDeepLearningModel(summary, value, (hex.deeplearning.DeepLearningModel) pojo);
} else if (pojo instanceof hex.gbm.GBM.GBMModel) {
summarizeGBMModel(summary, value, (hex.gbm.GBM.GBMModel) pojo);
} else if (pojo instanceof water.Model) {
// catch-all
summarizeModel(summary, value, (water.Model) pojo);
} else {
// skip
continue;
}

map.put(keyString, summary);
}

// TODO: temporary hack to get things going
String json = gson.toJson(map);
Log.info("Json for kv store: " + json);

JsonObject result = gson.fromJson(json, JsonElement.class).getAsJsonObject();
return Response.done(result);
} // serve()

}
3 changes: 3 additions & 0 deletions src/main/java/water/api/RequestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public enum API_VERSION {
// registerRequest(new GLMValidationView());
registerRequest(new LaunchJar());
Request.initializeNavBar();

// Pure APIs, no HTML, to support The New World
registerRequest(new Models());
}

/**
Expand Down

0 comments on commit e17a0d8

Please sign in to comment.