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);
}
}
}
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());
}
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;
}
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);
}
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 + "]");
}
Aggregations