use of dontweave.gson.JsonObject in project h2o-2 by h2oai.
the class Get method serve.
@Override
public NanoHTTPD.Response serve(NanoHTTPD server, Properties args, RequestType type) {
if (type == RequestType.json) {
JsonObject resp = new JsonObject();
resp.addProperty(ERROR, "This request is only provided for browser connections");
return wrap(server, resp);
} else if (type != RequestType.www) {
return super.serve(server, args, type);
}
String query = checkArguments(args, type);
if (query != null)
return wrap(server, query, type);
try {
Value val = _key.value();
Key key = val._key;
if (!key.user_allowed())
return wrap(server, build(Response.error("Not a user key: " + key)));
// HTML file save of Value
NanoHTTPD.Response res = server.new Response(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_DEFAULT_BINARY, val.openStream());
res.addHeader("Content-Length", Long.toString(val.length()));
res.addHeader("Content-Disposition", "attachment; filename=" + key.toString());
return res;
} catch (Throwable e) {
return wrap(server, build(Response.error(e)));
}
}
use of dontweave.gson.JsonObject in project h2o-2 by h2oai.
the class HTMLOnlyRequest method serve.
@Override
public final NanoHTTPD.Response serve(NanoHTTPD server, Properties args, RequestType type) {
if (type == RequestType.json) {
JsonObject resp = new JsonObject();
resp.addProperty(ERROR, "This request is only provided for browser connections");
return wrap(server, resp);
}
return super.serve(server, args, type);
}
use of dontweave.gson.JsonObject in project h2o-2 by h2oai.
the class Cancel method serve.
@Override
protected Response serve() {
String key = _key.value();
try {
Job.findJob(Key.make(key)).cancel();
} catch (Throwable e) {
return Response.error(e);
}
JsonObject response = new JsonObject();
return Response.redirect(response, Jobs.class, null);
}
use of dontweave.gson.JsonObject 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>");
}
}
}
use of dontweave.gson.JsonObject in project h2o-2 by h2oai.
the class AutoBuffer2JSONTest method testFloatFromRequest.
//---- Only Request1 tests
@Test
public void testFloatFromRequest() {
JsonObject o = new JsonObject();
o.addProperty("f1", Float.NaN);
o.addProperty("f2", Float.POSITIVE_INFINITY);
o.addProperty("f3", Float.NEGATIVE_INFINITY);
o.addProperty("f4", 3.141527f);
o = JsonUtil.escape(o);
Assert.assertEquals("{\"f1\":\"NaN\",\"f2\":\"Infinity\",\"f3\":\"-Infinity\",\"f4\":3.141527}", o.toString());
}
Aggregations