Search in sources :

Example 1 with CreateModel

use of ambit2.db.update.model.CreateModel in project ambit-mirror by ideaconsult.

the class Model_crud_test method createQuery.

@Override
protected IQueryUpdate<Object, ModelQueryResults> createQuery() throws Exception {
    ModelQueryResults q = new ModelQueryResults();
    q.setAlgorithm("http://localhost:8080/algorithm/pka");
    q.setName("Test model");
    q.setContent("Nothing");
    q.setParameters(new String[] { "-M" });
    q.setPredictors(new Template("New template"));
    q.setDependent(new Template("BCF"));
    q.setPredicted(new PredictedVarsTemplate("BCF"));
    q.setCreator("test-user");
    return new CreateModel(q);
}
Also used : PredictedVarsTemplate(ambit2.base.data.PredictedVarsTemplate) CreateModel(ambit2.db.update.model.CreateModel) ModelQueryResults(ambit2.core.data.model.ModelQueryResults) PredictedVarsTemplate(ambit2.base.data.PredictedVarsTemplate) Template(ambit2.base.data.Template)

Example 2 with CreateModel

use of ambit2.db.update.model.CreateModel in project ambit-mirror by ideaconsult.

the class Model_crud_test method createQueryNew.

@Override
protected IQueryUpdate<Object, ModelQueryResults> createQueryNew() throws Exception {
    ModelQueryResults q = new ModelQueryResults();
    q.setName("Test model");
    q.setContent("Nothing");
    Template t1 = new Template("Predictors template");
    t1.add(new Property("New predictor 1"));
    t1.add(new Property("New predictor 2"));
    q.setPredictors(t1);
    Template t2 = new Template("Dependent template");
    t2.add(new Property("New dependent"));
    q.setDependent(t2);
    PredictedVarsTemplate t3 = new PredictedVarsTemplate("Predicted template");
    t3.add(new Property("New predicted"));
    q.setPredicted(t3);
    return new CreateModel(q);
}
Also used : PredictedVarsTemplate(ambit2.base.data.PredictedVarsTemplate) CreateModel(ambit2.db.update.model.CreateModel) ModelQueryResults(ambit2.core.data.model.ModelQueryResults) Property(ambit2.base.data.Property) PredictedVarsTemplate(ambit2.base.data.PredictedVarsTemplate) Template(ambit2.base.data.Template)

Example 3 with CreateModel

use of ambit2.db.update.model.CreateModel in project ambit-mirror by ideaconsult.

the class CallableWekaModelCreator method createReference.

@Override
protected TaskResult createReference(Connection connection) throws Exception {
    builder.setTrainingData(((RDFInstancesParser) batch).getInstances());
    UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
    try {
        model = createModel();
        CreateModel update = new CreateModel(model);
        x.setConnection(connection);
        x.process(update);
        writeAnnotations(model.getPredicted(), x);
        return new TaskResult(builder.getModelReporter().getURI(model));
    } catch (WekaException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
    } catch (AmbitException e) {
        Context.getCurrentLogger().severe(e.getMessage());
        if ((e.getCause() != null) && (e.getCause() instanceof WekaException))
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getCause().getMessage(), e.getCause());
        else
            throw e;
    } catch (Exception e) {
        Context.getCurrentLogger().severe(e.getMessage());
        throw e;
    } finally {
        try {
            x.close();
        } catch (Exception xx) {
        }
    }
}
Also used : WekaException(weka.core.WekaException) CreateModel(ambit2.db.update.model.CreateModel) UpdateExecutor(ambit2.db.UpdateExecutor) TaskResult(ambit2.rest.task.TaskResult) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 4 with CreateModel

use of ambit2.db.update.model.CreateModel in project ambit-mirror by ideaconsult.

the class SimpleModelBuilder method process.

public ModelQueryResults process(Algorithm algorithm) throws AmbitException {
    try {
        List<Property> p = createProperties(algorithm);
        if ((p == null) || (p.size() == 0))
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Can't create a model from " + algorithm);
        ModelQueryResults mr = createModel(algorithm);
        Template dependent = new Template("Empty");
        mr.setDependent(dependent);
        PredictedVarsTemplate predicted = createPredictedTemplate(algorithm);
        mr.setPredicted(predicted);
        for (Property property : p) {
            property.setEnabled(true);
            if (property.getName() == null)
                continue;
            predicted.add(property);
            if (algorithm.getEndpoint() != null)
                property.setLabel(algorithm.getEndpoint());
            property.getReference().setType(modelHidden ? _type.Algorithm : _type.Model);
        }
        return mr;
    } catch (AmbitException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getCause() == null ? x.getMessage() : x.getCause().getMessage(), x);
    }
}
Also used : PredictedVarsTemplate(ambit2.base.data.PredictedVarsTemplate) ModelQueryResults(ambit2.core.data.model.ModelQueryResults) ResourceException(org.restlet.resource.ResourceException) Property(ambit2.base.data.Property) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) PredictedVarsTemplate(ambit2.base.data.PredictedVarsTemplate) Template(ambit2.base.data.Template) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 5 with CreateModel

use of ambit2.db.update.model.CreateModel in project ambit-mirror by ideaconsult.

the class CallableWafflesModelCreator method createReference.

/**
 *	TODO download ARFF directly, instead of reading Weka instances in memory
 *	protected AbstractBatchProcessor createBatch(Object target) throws Exception{
 *		if (target == null) throw new Exception("No dataset URI");
 *
 *		File.createTempFile(prefix, suffix);
 *		DownloadTool.download(url, file);
 *		builder.setTrainingData(trainingData);
 *		return null;
 *	}
 */
@Override
protected TaskResult createReference(Connection connection) throws Exception {
    File trainingData = File.createTempFile("wfltrain_", ".arff");
    trainingData.deleteOnExit();
    Instances instances = ((RDFInstancesParser) batch).getInstances();
    instances.deleteAttributeAt(0);
    // sort attributes by name (i.e. attribute URI).
    List<Attribute> sorted = new ArrayList<Attribute>();
    for (int i = 0; i < instances.numAttributes(); i++) sorted.add(instances.attribute(i));
    Collections.sort(sorted, new Comparator<Attribute>() {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.name().compareTo(o2.toString());
        }
    });
    StringBuilder order = null;
    for (int i = 0; i < sorted.size(); i++) {
        if (order == null)
            order = new StringBuilder();
        else
            order.append(",");
        order.append(sorted.get(i).index() + 1);
    }
    Reorder reorder = new Reorder();
    String[] options = new String[2];
    options[0] = "-R";
    options[1] = order.toString();
    reorder.setOptions(options);
    reorder.setInputFormat(instances);
    instances = Filter.useFilter(instances, reorder);
    SparseToNonSparse sp = new SparseToNonSparse();
    sp.setInputFormat(instances);
    Instances newInstances = Filter.useFilter(instances, sp);
    ArffSaver saver = new ArffSaver();
    saver.setInstances(newInstances);
    saver.setFile(trainingData);
    saver.writeBatch();
    // Leave the header only
    newInstances.delete();
    builder.setHeader(newInstances);
    builder.setTrainingData(trainingData);
    UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
    try {
        model = createModel();
        // trainingData.delete();
        CreateModel update = new CreateModel(model);
        x.setConnection(connection);
        x.process(update);
        writeAnnotations(model.getPredicted(), x);
        return new TaskResult(builder.getModelReporter().getURI(model));
    } catch (WekaException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
    } catch (Exception e) {
        Context.getCurrentLogger().severe(e.getMessage());
        throw e;
    } finally {
        try {
            x.close();
        } catch (Exception xx) {
        }
    }
}
Also used : WekaException(weka.core.WekaException) Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList) ArffSaver(weka.core.converters.ArffSaver) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) Instances(weka.core.Instances) Reorder(weka.filters.unsupervised.attribute.Reorder) CreateModel(ambit2.db.update.model.CreateModel) SparseToNonSparse(weka.filters.unsupervised.instance.SparseToNonSparse) UpdateExecutor(ambit2.db.UpdateExecutor) TaskResult(ambit2.rest.task.TaskResult) ResourceException(org.restlet.resource.ResourceException) RDFInstancesParser(ambit2.rest.dataset.RDFInstancesParser) File(java.io.File)

Aggregations

ModelQueryResults (ambit2.core.data.model.ModelQueryResults)6 CreateModel (ambit2.db.update.model.CreateModel)5 PredictedVarsTemplate (ambit2.base.data.PredictedVarsTemplate)3 Template (ambit2.base.data.Template)3 UpdateExecutor (ambit2.db.UpdateExecutor)3 TaskResult (ambit2.rest.task.TaskResult)3 ResourceException (org.restlet.resource.ResourceException)3 Property (ambit2.base.data.Property)2 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)2 WekaException (weka.core.WekaException)2 QueryExecutor (ambit2.db.search.QueryExecutor)1 ReadModel (ambit2.db.update.model.ReadModel)1 RDFInstancesParser (ambit2.rest.dataset.RDFInstancesParser)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Attribute (weka.core.Attribute)1 Instances (weka.core.Instances)1 ArffSaver (weka.core.converters.ArffSaver)1 Reorder (weka.filters.unsupervised.attribute.Reorder)1