Search in sources :

Example 11 with TaskContextMetadata

use of org.dkpro.lab.task.TaskContextMetadata in project dkpro-tc by dkpro.

the class ScatterplotReport method execute.

@Override
public void execute() throws Exception {
    for (TaskContextMetadata subcontext : getSubtasks()) {
        if (TcTaskTypeUtil.isCrossValidationTask(getContext().getStorageService(), subcontext.getId())) {
            File id2outcomeFile = getContext().getStorageService().locateKey(subcontext.getId(), Constants.FILE_COMBINED_ID_OUTCOME_KEY);
            EvaluationData<Double> data = Tc2LtlabEvalConverter.convertRegressionModeId2Outcome(id2outcomeFile);
            double[] gold = new double[(int) data.size()];
            double[] prediction = new double[(int) data.size()];
            Iterator<EvaluationEntry<Double>> iterator = data.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                EvaluationEntry<Double> next = iterator.next();
                gold[i] = next.getGold();
                prediction[i] = next.getPredicted();
                i++;
            }
            ScatterplotRenderer renderer = new ScatterplotRenderer(gold, prediction);
            getContext().storeBinary("scatterplot.pdf", renderer);
        } else if (TcTaskTypeUtil.isMachineLearningAdapterTask(getContext().getStorageService(), subcontext.getId())) {
            File id2outcomeFile = getContext().getStorageService().locateKey(subcontext.getId(), Constants.ID_OUTCOME_KEY);
            EvaluationData<Double> data = Tc2LtlabEvalConverter.convertRegressionModeId2Outcome(id2outcomeFile);
            double[] gold = new double[(int) data.size()];
            double[] prediction = new double[(int) data.size()];
            Iterator<EvaluationEntry<Double>> iterator = data.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                EvaluationEntry<Double> next = iterator.next();
                gold[i] = next.getGold();
                prediction[i] = next.getPredicted();
                i++;
            }
            ScatterplotRenderer renderer = new ScatterplotRenderer(gold, prediction);
            getContext().storeBinary("scatterplot.pdf", renderer);
        }
    }
}
Also used : TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) EvaluationEntry(de.unidue.ltl.evaluation.core.EvaluationEntry) EvaluationData(de.unidue.ltl.evaluation.core.EvaluationData) Iterator(java.util.Iterator) File(java.io.File) ScatterplotRenderer(org.dkpro.tc.ml.report.util.ScatterplotRenderer)

Example 12 with TaskContextMetadata

use of org.dkpro.lab.task.TaskContextMetadata in project dkpro-lab by dkpro.

the class UimaAsExecutionEngineTest method testInit.

@Test
public void testInit() throws Exception {
    File repo = new File("target/repository");
    FileUtils.deleteQuietly(repo);
    ((FileSystemStorageService) storageService).setStorageRoot(repo);
    assertNotNull(executionService);
    assertNotNull(contextFactory);
    AnalysisEngineDescription desc = createEngineDescription(DummyAE.class);
    DefaultUimaTask cfg = new DefaultUimaTask();
    cfg.setReaderDescription(createReaderDescription(TestReader.class));
    cfg.setAnalysisEngineDescription(desc);
    TaskExecutionEngine runner = executionService.createEngine(cfg);
    String uuid = runner.run(cfg);
    System.out.println("=== Experiments in repository ===");
    List<TaskContextMetadata> experiments = storageService.getContexts();
    for (TaskContextMetadata e : experiments) {
        System.out.println(e);
    }
    final StringBuilder sb = new StringBuilder();
    storageService.retrieveBinary(uuid, "test", new StreamReader() {

        @Override
        public void read(InputStream aInputStream) throws IOException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Util.shoveAndClose(aInputStream, bos);
            sb.append(new String(bos.toByteArray(), "UTF-8"));
        }
    });
    assertEquals("works", sb.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TaskExecutionEngine(org.dkpro.lab.engine.TaskExecutionEngine) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultUimaTask(org.dkpro.lab.uima.task.impl.DefaultUimaTask) TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) StreamReader(org.dkpro.lab.storage.StreamReader) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) File(java.io.File) FileSystemStorageService(org.dkpro.lab.storage.filesystem.FileSystemStorageService) Test(org.junit.Test)

Example 13 with TaskContextMetadata

use of org.dkpro.lab.task.TaskContextMetadata in project dkpro-lab by dkpro.

the class FileSystemStorageService method getContexts.

@Override
public List<TaskContextMetadata> getContexts() {
    List<TaskContextMetadata> contexts = new ArrayList<TaskContextMetadata>();
    for (File child : storageRoot.listFiles()) {
        if (new File(child, METADATA_KEY).exists()) {
            contexts.add(retrieveBinary(child.getName(), METADATA_KEY, new TaskContextMetadata()));
        }
    }
    Collections.sort(contexts, new Comparator<TaskContextMetadata>() {

        @Override
        public int compare(TaskContextMetadata aO1, TaskContextMetadata aO2) {
            return Long.signum(aO2.getEnd() - aO1.getEnd());
        }
    });
    return contexts;
}
Also used : TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) ArrayList(java.util.ArrayList) File(java.io.File)

Example 14 with TaskContextMetadata

use of org.dkpro.lab.task.TaskContextMetadata in project dkpro-lab by dkpro.

the class BatchTaskEngine method getLatestExecution.

/**
 * Locate the latest task execution compatible with the given task configuration.
 *
 * @param aContext
 *            the context of the current batch task.
 * @param aType
 *            the type of the task context to find.
 * @param aDiscriminators
 *            the discriminators of the task context to find.
 * @param aConfig
 *            the current parameter configuration.
 * @throws TaskContextNotFoundException
 *             if a matching task context could not be found.
 * @see ImportUtil#matchConstraints(Map, Map, boolean)
 */
private TaskContextMetadata getLatestExecution(TaskContext aContext, String aType, Map<String, String> aDiscriminators, Map<String, Object> aConfig) {
    // Convert parameter values to strings
    Map<String, String> config = new HashMap<String, String>();
    for (Entry<String, Object> e : aConfig.entrySet()) {
        config.put(e.getKey(), Util.toString(e.getValue()));
        // If the conversion service has a registered value override the constraint here
        // accordingly
        Object object = e.getValue();
        ConversionService cs = aContext.getConversionService();
        if (cs.isRegistered(object)) {
            config.put(e.getKey(), cs.getDiscriminableValue(object));
        }
    }
    StorageService storage = aContext.getStorageService();
    List<TaskContextMetadata> metas = storage.getContexts(aType, aDiscriminators);
    for (TaskContextMetadata meta : metas) {
        Map<String, String> discriminators = storage.retrieveBinary(meta.getId(), Task.DISCRIMINATORS_KEY, new PropertiesAdapter()).getMap();
        // interpret the discriminators as constraints on the current configuration.
        if (ImportUtil.matchConstraints(discriminators, config, false)) {
            return meta;
        }
    }
    throw ImportUtil.createContextNotFoundException(aType, aDiscriminators);
}
Also used : TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) PropertiesAdapter(org.dkpro.lab.storage.impl.PropertiesAdapter) HashMap(java.util.HashMap) ConversionService(org.dkpro.lab.conversion.ConversionService) StorageService(org.dkpro.lab.storage.StorageService)

Example 15 with TaskContextMetadata

use of org.dkpro.lab.task.TaskContextMetadata in project dkpro-lab by dkpro.

the class DefaultTaskContext method resolve.

public StorageKey resolve(String aKey, AccessMode aMode, boolean aAllowMissing) {
    StorageService storage = getStorageService();
    Map<String, String> imports = getMetadata().getImports();
    if (storage.containsKey(getId(), aKey)) {
        // supersedes imported data.
        return new StorageKey(getId(), aKey);
    } else if (imports.containsKey(aKey)) {
        URI uri;
        try {
            uri = new URI(imports.get(aKey));
        } catch (URISyntaxException e) {
            throw new DataAccessResourceFailureException("Imported key [" + aKey + "] resolves to illegal URL [" + imports.get(aKey) + "]", e);
        }
        // Try resolving by ID or by type/constraints
        StorageKey key = null;
        if (CONTEXT_ID_SCHEME.equals(uri.getScheme()) || LATEST_CONTEXT_SCHEME.equals(uri.getScheme())) {
            TaskContextMetadata meta = resolve(uri);
            key = new StorageKey(meta.getId(), uri.getPath());
        }
        // imported storage folders now but imported stream-access (files) keys later.
        if (key != null) {
            switch(aMode) {
                case ADD_ONLY:
                case READWRITE:
                    storage.copy(getId(), aKey, key, aMode);
                    return new StorageKey(getId(), aKey);
                case READONLY:
                    return key;
            }
        }
        // If this is an external URL, copy it to the current context and then return a location
        // in the current context.
        InputStream is = null;
        try {
            is = uri.toURL().openStream();
            storage.storeBinary(getId(), aKey, is);
            return new StorageKey(getId(), aKey);
        } catch (MalformedURLException e) {
            throw new DataAccessResourceFailureException("Imported external key [" + aKey + "] resolves to illegal URL [" + uri + "]", e);
        } catch (IOException e) {
            throw new DataAccessResourceFailureException("Unable to read data for external key [" + aKey + "] from [" + uri + "]", e);
        } finally {
            close(is);
        }
    } else if (aAllowMissing) {
        return new StorageKey(getId(), aKey);
    }
    throw new DataAccessResourceFailureException("No resource bound to key [" + aKey + "]");
}
Also used : TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) MalformedURLException(java.net.MalformedURLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) StorageService(org.dkpro.lab.storage.StorageService) StorageKey(org.dkpro.lab.storage.StorageService.StorageKey)

Aggregations

TaskContextMetadata (org.dkpro.lab.task.TaskContextMetadata)15 File (java.io.File)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 StorageService (org.dkpro.lab.storage.StorageService)4 PropertiesAdapter (org.dkpro.lab.storage.impl.PropertiesAdapter)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Map (java.util.Map)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)2 TaskExecutionEngine (org.dkpro.lab.engine.TaskExecutionEngine)2 StreamReader (org.dkpro.lab.storage.StreamReader)2 UnresolvedImportException (org.dkpro.lab.storage.UnresolvedImportException)2 FileSystemStorageService (org.dkpro.lab.storage.filesystem.FileSystemStorageService)2 BatchTask (org.dkpro.lab.task.BatchTask)2