Search in sources :

Example 1 with IEvaluation

use of ambit2.core.data.model.IEvaluation in project ambit-mirror by ideaconsult.

the class ModelJSONReporter method parseContent.

protected void parseContent(ModelQueryResults model) {
    ObjectInputStream ois = null;
    try {
        Form form = new Form(model.getContent());
        InputStream in = new ByteArrayInputStream(Base64.decode(form.getFirstValue("model")));
        ois = new ObjectInputStream(in);
        Object o = ois.readObject();
        if (o == null)
            throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, String.format("Error when reading model %s", model.getName()));
        List<IEvaluation<String>> e = new ArrayList<IEvaluation<String>>();
        for (EVType evt : EVType.values()) {
            String[] evals = form.getValuesArray(evt.name());
            for (int i = 0; i < evals.length; i++) {
                EvaluationStats<String> stats = new EvaluationStats<String>(evt, evals[i]);
                for (EVStatsType evst : EVStatsType.values()) {
                    Object value = form.getFirstValue(evt.name() + "_" + evst.name());
                    if (value == null)
                        continue;
                    try {
                        stats.getStats().put(evst, Double.parseDouble(value.toString()));
                    } catch (Exception x) {
                    }
                }
                e.add(stats);
            }
        }
        if (e.size() == 0)
            model.setEvaluation(null);
        else
            model.setEvaluation(e);
    } catch (Exception x) {
    } finally {
        try {
            ois.close();
        } catch (Exception x) {
        }
    }
}
Also used : Form(org.restlet.data.Form) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) EVType(ambit2.core.data.model.IEvaluation.EVType) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) IEvaluation(ambit2.core.data.model.IEvaluation) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceException(org.restlet.resource.ResourceException) EVStatsType(ambit2.core.data.model.IEvaluation.EVStatsType) EvaluationStats(ambit2.model.evaluation.EvaluationStats) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with IEvaluation

use of ambit2.core.data.model.IEvaluation in project ambit-mirror by ideaconsult.

the class WekaModelBuilder method serializeModel.

protected void serializeModel(Object predictor, Instances newInstances, ModelQueryResults m) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // serialize model
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(predictor);
    oos.flush();
    oos.close();
    byte[] content = out.toByteArray();
    Form form = new Form();
    form.add("model", Base64.encode(content));
    newInstances.delete();
    if (newInstances.classIndex() >= 0)
        form.add("classIndex", Integer.toString(newInstances.classIndex()));
    newInstances.delete();
    form.add("header", newInstances.toString());
    if (m.getEvaluation() != null)
        for (IEvaluation stats : m.getEvaluation()) {
            form.add(stats.getType().name(), stats.getContent().toString());
            if (stats instanceof EvaluationStats)
                for (EVStatsType st : EVStatsType.values()) try {
                    Object val = ((EvaluationStats) stats).getStats().get(st);
                    if (val == null)
                        continue;
                    form.add(stats.getType().name() + "_" + st.name(), String.format(Locale.ENGLISH, "%6.3f", (Double) val));
                } catch (Exception x) {
                    logger.warning(x.getMessage());
                }
        }
    m.setContent(form.getWebRepresentation().getText());
}
Also used : IEvaluation(ambit2.core.data.model.IEvaluation) Form(org.restlet.data.Form) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EVStatsType(ambit2.core.data.model.IEvaluation.EVStatsType) ObjectOutputStream(java.io.ObjectOutputStream) EvaluationStats(ambit2.model.evaluation.EvaluationStats) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException)

Example 3 with IEvaluation

use of ambit2.core.data.model.IEvaluation in project ambit-mirror by ideaconsult.

the class FilteredWekaModelBuilder method serializeModel.

protected void serializeModel(Object predictor, Instances newInstances, ModelQueryResults m) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // serialize model
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(predictor);
    oos.flush();
    oos.close();
    byte[] content = out.toByteArray();
    Form form = new Form();
    form.add("model", Base64.encode(content));
    newInstances.delete();
    if (newInstances.classIndex() >= 0)
        form.add("classIndex", Integer.toString(newInstances.classIndex()));
    newInstances.delete();
    form.add("header", newInstances.toString());
    if (m.getEvaluation() != null)
        for (IEvaluation stats : m.getEvaluation()) {
            form.add(stats.getType().name(), stats.getContent().toString());
            if (stats instanceof EvaluationStats)
                for (EVStatsType st : EVStatsType.values()) try {
                    Object val = ((EvaluationStats) stats).getStats().get(st);
                    if (val == null)
                        continue;
                    form.add(stats.getType().name() + "_" + st.name(), String.format(Locale.ENGLISH, "%6.3f", (Double) val));
                } catch (Exception x) {
                    logger.warning(x.getMessage());
                }
        }
    m.setContent(form.getWebRepresentation().getText());
}
Also used : IEvaluation(ambit2.core.data.model.IEvaluation) Form(org.restlet.data.Form) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EVStatsType(ambit2.core.data.model.IEvaluation.EVStatsType) ObjectOutputStream(java.io.ObjectOutputStream) EvaluationStats(ambit2.model.evaluation.EvaluationStats) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException)

Example 4 with IEvaluation

use of ambit2.core.data.model.IEvaluation in project ambit-mirror by ideaconsult.

the class ModelJSONReporter method processItem.

@Override
public Object processItem(ModelQueryResults model) throws AmbitException {
    try {
        String uri = getURI(model);
        parseContent(model);
        AlgorithmFormat algFormat = AlgorithmFormat.JAVA_CLASS;
        for (AlgorithmFormat af : AlgorithmFormat.values()) if (af.getMediaType().equals(model.getContentMediaType())) {
            algFormat = af;
            break;
        }
        if (comma != null)
            getOutput().write(comma);
        StringBuilder evals = new StringBuilder();
        evals.append("{\n");
        if (model.getEvaluation() != null)
            for (int i = 0; i < model.getEvaluation().size(); i++) {
                IEvaluation ev = model.getEvaluation().get(i);
                if (i > 0)
                    evals.append(",");
                evals.append(String.format("\t\"%s\":\n\t\t{\"content\":\"%s\"", ev.getType().name(), JSONUtils.jsonEscape(ev.getContent().toString())));
                if (ev instanceof EvaluationStats)
                    for (EVStatsType evst : EVStatsType.values()) {
                        Object value = ((EvaluationStats) ev).getStats().get(evst);
                        if (value == null)
                            continue;
                        evals.append(String.format(",\n\t\t\"%s\":%s", evst.name(), value));
                    }
                evals.append("\t\n}");
            }
        evals.append("\t}");
        StringBuilder p = new StringBuilder();
        if (model.getParameters() != null) {
            String d = "";
            for (String param : model.getParameters()) {
                p.append(d);
                p.append(JSONUtils.jsonQuote(JSONUtils.jsonEscape(param)));
                d = ",";
            }
        }
        getOutput().write(String.format("\n{" + // uri
        "\n\"%s\":\"%s\"," + // id
        "\n\"%s\":%d," + // title
        "\n\"%s\":\"%s\"," + // stars
        "\n\"%s\":%d," + // algorithm
        "\n\"%s\":{\n\t\"URI\":\"%s\",\n\t\"%s\":\"%s\",\n\t\"img\":\"%s\"\n}," + // dataset
        "\n\"%s\":\"%s\"," + // vars
        "\n\"%s\":\"%s\"," + // vars
        "\n\"%s\":\"%s\"," + // vars
        "\n\"%s\":\"%s\"," + "\n\"%s\":{" + "\n\t\"%s\":\"%s\"," + "\n\t\"%s\":\"%s\"," + "\n\t\"%s\":\"%s\"," + "\n\t\"%s\":\"%s\"," + "\n\t\"%s\":%s," + "\n\t\"%s\":%s, " + "\n\t\"%s\":[%s] " + "\n\n}}", jsonModel.URI.jsonname(), uri, "id", model.getId(), jsonModel.title.jsonname(), JSONUtils.jsonEscape(model.getName()), jsonModel.stars.jsonname(), model.getStars(), jsonModel.algorithm.jsonname(), model.getAlgorithm() == null ? "" : JSONUtils.jsonEscape(model.getAlgorithm()), jsonModel.algFormat.jsonname(), JSONUtils.jsonEscape(algFormat.name()), String.format("/%s", model.getAlgorithm().indexOf("org.openscience.cdk") >= 0 ? "images/cdk.png" : model.getAlgorithm().indexOf("toxtree") >= 0 ? "images/toxtree.png" : model.getAlgorithm().indexOf("ambit2.descriptors") >= 0 ? "images/ambit.png" : algFormat.getImage()), jsonModel.trainingDataset.jsonname(), model.getTrainingInstances() == null ? "" : JSONUtils.jsonEscape(model.getTrainingInstances()), jsonModel.independent.jsonname(), String.format("%s/independent", uri), jsonModel.dependent.jsonname(), String.format("%s/dependent", uri), jsonModel.predicted.jsonname(), String.format("%s/predicted", uri), "ambitprop", jsonModel.legend.jsonname(), String.format("%s?media=image/png", uri), jsonModel.creator.jsonname(), JSONUtils.jsonEscape(model.getCreator()), jsonModel.mimetype.jsonname(), model.getContentMediaType(), jsonModel.content.jsonname(), "application/java".equals(model.getContentMediaType()) ? JSONUtils.jsonEscape(model.getContent()) : (uri + "?media=text/plain"), jsonModel.evaluations.jsonname(), evals.toString(), jsonModel.stars.jsonname(), model.getStars(), jsonModel.parameters.jsonname(), p.toString()));
        comma = ",";
    } catch (Exception x) {
        x.printStackTrace();
    }
    return model;
}
Also used : IEvaluation(ambit2.core.data.model.IEvaluation) AlgorithmFormat(ambit2.core.data.model.Algorithm.AlgorithmFormat) EVStatsType(ambit2.core.data.model.IEvaluation.EVStatsType) EvaluationStats(ambit2.model.evaluation.EvaluationStats) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException)

Aggregations

IEvaluation (ambit2.core.data.model.IEvaluation)4 EVStatsType (ambit2.core.data.model.IEvaluation.EVStatsType)4 EvaluationStats (ambit2.model.evaluation.EvaluationStats)4 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)4 ResourceException (org.restlet.resource.ResourceException)4 Form (org.restlet.data.Form)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 WekaException (weka.core.WekaException)2 AlgorithmFormat (ambit2.core.data.model.Algorithm.AlgorithmFormat)1 EVType (ambit2.core.data.model.IEvaluation.EVType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1