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);
}
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);
}
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) {
}
}
}
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);
}
}
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) {
}
}
}
Aggregations