use of ambit2.rest.dataset.RDFInstancesParser 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.rest.dataset.RDFInstancesParser 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