use of dontweave.gson.JsonElement in project h2o-2 by h2oai.
the class WebAPI method exportModel.
/**
* Exports a model to a JSON file.
*/
static void exportModel() throws Exception {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(URL + "/2/ExportModel.json?model=MyInitialNeuralNet");
int status = client.executeMethod(get);
if (status != 200)
throw new Exception(get.getStatusText());
JsonObject response = (JsonObject) new JsonParser().parse(new InputStreamReader(get.getResponseBodyAsStream()));
JsonElement model = response.get("model");
JsonWriter writer = new JsonWriter(new FileWriter(JSON_FILE));
writer.setLenient(true);
writer.setIndent(" ");
Streams.write(model, writer);
writer.close();
get.releaseConnection();
}
use of dontweave.gson.JsonElement in project h2o-2 by h2oai.
the class RequestStatics method encodeRedirectArgs.
protected static String encodeRedirectArgs(JsonObject args, Object[] args2) {
if (args == null && args2 == null)
return "";
if (args2 != null) {
StringBuilder sb = new StringBuilder();
// Must be field-name / value pairs
assert (args2.length & 1) == 0 : "Number of arguments shoud be power of 2.";
for (int i = 0; i < args2.length; i += 2) {
sb.append(i == 0 ? '?' : '&').append(args2[i]).append('=');
try {
sb.append(URLEncoder.encode(args2[i + 1].toString(), "UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw Log.errRTExcept(ex);
}
}
return sb.toString();
}
StringBuilder sb = new StringBuilder();
sb.append("?");
for (Map.Entry<String, JsonElement> entry : args.entrySet()) {
JsonElement e = entry.getValue();
if (sb.length() != 1)
sb.append("&");
sb.append(entry.getKey());
sb.append("=");
try {
sb.append(URLEncoder.encode(e.getAsString(), "UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw Log.errRTExcept(ex);
}
}
return sb.toString();
}
use of dontweave.gson.JsonElement in project h2o-2 by h2oai.
the class Jobs method serve.
@Override
protected Response serve() {
JsonObject result = new JsonObject();
JsonArray array = new JsonArray();
Job[] jobs = Job.all();
for (int i = jobs.length - 1; i >= 0; i--) {
JsonObject json = new JsonObject();
json.addProperty(KEY, jobs[i].self().toString());
json.addProperty(DESCRIPTION, jobs[i].description);
json.addProperty(DEST_KEY, jobs[i].dest() != null ? jobs[i].dest().toString() : "");
json.addProperty(START_TIME, RequestBuilders.ISO8601.get().format(new Date(jobs[i].start_time)));
long end = jobs[i].end_time;
JsonObject jobResult = new JsonObject();
Job job = jobs[i];
boolean cancelled;
if (cancelled = (job.state == JobState.CANCELLED || job.state == JobState.FAILED)) {
if (job.exception != null) {
jobResult.addProperty("exception", "1");
jobResult.addProperty("val", jobs[i].exception);
} else {
jobResult.addProperty("val", "CANCELLED");
}
} else if (job.state == JobState.DONE)
jobResult.addProperty("val", "OK");
json.addProperty(END_TIME, end == 0 ? "" : RequestBuilders.ISO8601.get().format(new Date(end)));
json.addProperty(PROGRESS, job.state == JobState.RUNNING || job.state == JobState.DONE ? jobs[i].progress() : -1);
json.addProperty(PROGRESS, end == 0 ? (cancelled ? -2 : jobs[i].progress()) : (cancelled ? -2 : -1));
json.addProperty(CANCELLED, cancelled);
json.add("result", jobResult);
array.add(json);
}
result.add(JOBS, array);
Response r = Response.done(result);
r.setBuilder(JOBS, new ArrayBuilder() {
@Override
public String caption(JsonArray array, String name) {
return "";
}
});
r.setBuilder(JOBS + "." + KEY, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
String html;
if (!Job.isRunning(Key.make(elm.getAsString())))
html = "<button disabled class='btn btn-mini'>X</button>";
else {
String keyParam = KEY + "=" + elm.getAsString();
html = "<a href='/Cancel.html?" + keyParam + "'><button class='btn btn-danger btn-mini'>X</button></a>";
}
return html;
}
});
r.setBuilder(JOBS + "." + DEST_KEY, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
String str = elm.getAsString();
String key = null;
try {
key = URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
key = str;
}
return ("".equals(key) || DKV.get(Key.make(str)) == null) ? key : Inspector.link(str, str);
}
});
r.setBuilder(JOBS + "." + START_TIME, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
return date(elm.toString());
}
});
r.setBuilder(JOBS + "." + END_TIME, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
return date(elm.toString());
}
});
r.setBuilder(JOBS + "." + PROGRESS, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
return progress(Float.parseFloat(elm.getAsString()));
}
});
r.setBuilder(JOBS + "." + "result", new ElementBuilder() {
@Override
public String objectToString(JsonObject obj, String contextName) {
if (obj.has("exception")) {
String rid = Key.make().toString();
String ex = obj.get("val").getAsString().replace("'", "");
String[] lines = ex.split("\n");
StringBuilder sb = new StringBuilder(lines[0]);
for (int i = 1; i < lines.length; ++i) {
sb.append("\\n" + lines[i]);
}
// ex = ex.substring(0,ex.indexOf('\n'));
ex = sb.toString();
String res = "\n<a onClick=\"" + "var showhide=document.getElementById('" + rid + "');" + "if(showhide.innerHTML == '') showhide.innerHTML = '<pre>" + ex + "</pre>';" + "else showhide.innerHTML = '';" + "\">FAILED</a>\n<div id='" + rid + "'></div>\n";
return res;
} else if (obj.has("val")) {
return obj.get("val").getAsString();
}
return "";
}
@Override
public String build(String elementContents, String elementName) {
return "<td>" + elementContents + "</td>";
}
});
return r;
}
use of dontweave.gson.JsonElement in project h2o-2 by h2oai.
the class Models method whitelistJsonObject.
private static Map whitelistJsonObject(JsonObject unfiltered, Set<String> whitelist) {
// If we create a new JsonObject here and serialize it the key/value pairs are inside
// a superflouous "members" object, so create a Map instead.
JsonObject filtered = new JsonObject();
Set<Map.Entry<String, JsonElement>> entries = unfiltered.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {
String key = entry.getKey();
if (whitelist.contains(key))
filtered.add(key, entry.getValue());
}
return gson.fromJson(gson.toJson(filtered), Map.class);
}
use of dontweave.gson.JsonElement in project h2o-2 by h2oai.
the class SpeeDRFModel method buildCM.
public void buildCM(StringBuilder sb) {
int tasks = this.N;
int finished = this.size();
int modelSize = tasks * 25 / 100;
modelSize = modelSize == 0 || finished == tasks ? finished : modelSize * (finished / modelSize);
if (confusion != null && confusion.valid() && modelSize > 0) {
//finished += 1;
JsonObject cm = new JsonObject();
JsonArray cmHeader = new JsonArray();
JsonArray matrix = new JsonArray();
cm.addProperty(JSON_CM_TYPE, oobee ? "OOB" : "training");
cm.addProperty(JSON_CM_CLASS_ERR, confusion.classError());
cm.addProperty(JSON_CM_ROWS_SKIPPED, confusion.skippedRows());
cm.addProperty(JSON_CM_ROWS, confusion.rows());
// create the header
for (String s : cfDomain(confusion, 1024)) cmHeader.add(new JsonPrimitive(s));
cm.add(JSON_CM_HEADER, cmHeader);
// add the matrix
final int nclasses = confusion.dimension();
JsonArray classErrors = new JsonArray();
for (int crow = 0; crow < nclasses; ++crow) {
JsonArray row = new JsonArray();
int classHitScore = 0;
for (int ccol = 0; ccol < nclasses; ++ccol) {
row.add(new JsonPrimitive(confusion.matrix(crow, ccol)));
if (crow != ccol)
classHitScore += confusion.matrix(crow, ccol);
}
// produce infinity members in case of 0.f/0
classErrors.add(new JsonPrimitive((float) classHitScore / (classHitScore + confusion.matrix(crow, crow))));
matrix.add(row);
}
cm.add(JSON_CM_CLASSES_ERRORS, classErrors);
cm.add(JSON_CM_MATRIX, matrix);
cm.addProperty(JSON_CM_TREES, modelSize);
// Signal end only and only if all trees were generated and confusion matrix is valid
DocGen.HTML.section(sb, "Confusion Matrix:");
if (cm.has(JSON_CM_MATRIX)) {
sb.append("<dl class='dl-horizontal'>");
sb.append("<dt>classification error</dt><dd>").append(String.format("%5.5f %%", 100 * cm.get(JSON_CM_CLASS_ERR).getAsFloat())).append("</dd>");
long rows = cm.get(JSON_CM_ROWS).getAsLong();
long skippedRows = cm.get(JSON_CM_ROWS_SKIPPED).getAsLong();
sb.append("<dt>used / skipped rows </dt><dd>").append(String.format("%d / %d (%3.1f %%)", rows, skippedRows, (double) skippedRows * 100 / (skippedRows + rows))).append("</dd>");
sb.append("<dt>trees used</dt><dd>").append(cm.get(JSON_CM_TREES).getAsInt()).append("</dd>");
sb.append("</dl>");
sb.append("<table class='table table-striped table-bordered table-condensed'>");
sb.append("<tr style='min-width: 60px;'><th style='min-width: 60px;'>Actual \\ Predicted</th>");
JsonArray header = (JsonArray) cm.get(JSON_CM_HEADER);
for (JsonElement e : header) sb.append("<th style='min-width: 60px;'>").append(e.getAsString()).append("</th>");
sb.append("<th style='min-width: 60px;'>Error</th></tr>");
int classes = header.size();
long[] totals = new long[classes];
JsonArray matrix2 = (JsonArray) cm.get(JSON_CM_MATRIX);
long sumTotal = 0;
long sumError = 0;
for (int crow = 0; crow < classes; ++crow) {
JsonArray row = (JsonArray) matrix2.get(crow);
long total = 0;
long error = 0;
sb.append("<tr style='min-width: 60px;'><th style='min-width: 60px;'>").append(header.get(crow).getAsString()).append("</th>");
for (int ccol = 0; ccol < classes; ++ccol) {
long num = row.get(ccol).getAsLong();
total += num;
totals[ccol] += num;
if (ccol == crow) {
sb.append("<td style='background-color:LightGreen; min-width: 60px;'>");
} else {
sb.append("<td styile='min-width: 60px;'>");
error += num;
}
sb.append(num);
sb.append("</td>");
}
sb.append("<td style='min-width: 60px;'>");
sb.append(String.format("%.05f = %,d / %d", (double) error / total, error, total));
sb.append("</td></tr>");
sumTotal += total;
sumError += error;
}
sb.append("<tr style='min-width: 60px;'><th style='min-width: 60px;'>Totals</th>");
for (long total : totals) sb.append("<td style='min-width: 60px;'>").append(total).append("</td>");
sb.append("<td style='min-width: 60px;'><b>");
sb.append(String.format("%.05f = %,d / %d", (double) sumError / sumTotal, sumError, sumTotal));
sb.append("</b></td></tr>");
sb.append("</table>");
} else {
sb.append("<div class='alert alert-info'>");
sb.append("Confusion matrix is being computed into the key:</br>");
sb.append(cm.get(JSON_CONFUSION_KEY).getAsString());
sb.append("</div>");
}
}
}
Aggregations