Search in sources :

Example 1 with EVType

use of ambit2.core.data.model.IEvaluation.EVType 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 EVType

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

the class ModelPredictor method createPredictor.

public Predictor createPredictor(ModelQueryResults model) throws ResourceException {
    this.model = model;
    try {
        Form form = new Form(model.getContent());
        Object o = null;
        Object modeltype = form.getFirstValue("objecttype");
        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(Base64.decode(form.getFirstValue("model"))))) {
            o = ois.readObject();
            if (o == null)
                throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, String.format("Error when reading model %s", model.getName()));
            if (modeltype != null && AlgorithmFormat.JAVA_CLASS.name().equals(modeltype))
                try {
                    o = Class.forName(o.toString()).newInstance();
                } catch (Exception x) {
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Wrong model type %s %s", model.getName(), o.getClass().getName()));
                }
            if (!isSupported(o))
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Wrong model type %s %s", model.getName(), o.getClass().getName()));
        }
        for (EVType evt : EVType.values()) {
            String[] evals = form.getValuesArray(evt.name());
            if (evals == null)
                continue;
            for (String eval : evals) model.addEvaluation(new EvaluationStats<String>(evt, eval));
        }
        header = getHeader(form);
        classIndex = getClassIndex(form);
        if ((header != null) && (classIndex >= 0) && (classIndex < header.numAttributes()))
            header.setClassIndex(classIndex);
        if (header != null) {
            String[] options = new String[2];
            options[0] = "-R";
            options[1] = "1";
            Remove remove = new Remove();
            try {
                remove.setOptions(options);
            } catch (Exception x) {
            }
            ;
            filter = remove;
        } else
            filter = null;
        return (Predictor) o;
    } catch (ResourceException x) {
        throw x;
    } catch (NumberFormatException x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
    } catch (IOException x) {
        throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, x.getMessage(), x);
    } catch (ClassNotFoundException x) {
        throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE, x.getMessage(), x);
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
    } finally {
    }
}
Also used : Form(org.restlet.data.Form) EVType(ambit2.core.data.model.IEvaluation.EVType) Remove(weka.filters.unsupervised.attribute.Remove) IOException(java.io.IOException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceException(org.restlet.resource.ResourceException) EvaluationStats(ambit2.model.evaluation.EvaluationStats) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

EVType (ambit2.core.data.model.IEvaluation.EVType)2 EvaluationStats (ambit2.model.evaluation.EvaluationStats)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)2 Form (org.restlet.data.Form)2 ResourceException (org.restlet.resource.ResourceException)2 IEvaluation (ambit2.core.data.model.IEvaluation)1 EVStatsType (ambit2.core.data.model.IEvaluation.EVStatsType)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)1 Remove (weka.filters.unsupervised.attribute.Remove)1