Search in sources :

Example 1 with SparseToNonSparse

use of weka.filters.unsupervised.instance.SparseToNonSparse 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

UpdateExecutor (ambit2.db.UpdateExecutor)1 CreateModel (ambit2.db.update.model.CreateModel)1 RDFInstancesParser (ambit2.rest.dataset.RDFInstancesParser)1 TaskResult (ambit2.rest.task.TaskResult)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ResourceException (org.restlet.resource.ResourceException)1 Attribute (weka.core.Attribute)1 Instances (weka.core.Instances)1 WekaException (weka.core.WekaException)1 ArffSaver (weka.core.converters.ArffSaver)1 Reorder (weka.filters.unsupervised.attribute.Reorder)1 SparseToNonSparse (weka.filters.unsupervised.instance.SparseToNonSparse)1