Search in sources :

Example 11 with StorageService

use of org.dkpro.lab.storage.StorageService in project dkpro-tc by dkpro.

the class TcBatchReportBase method getId2Outcome.

/**
 * Retrieves the id2outcome file in a train test setup. The behavior of this method in cross
 * validation tasks is undefined.
 *
 * @param id
 *            context id of machine learning adapter
 *
 * @return file to the id2 outcome file in the machine learning adapter or null if the folder of
 *         machine learning adapter was not found
 * @throws Exception
 *             in case of errors
 */
protected File getId2Outcome(String id) throws Exception {
    StorageService store = getContext().getStorageService();
    File id2outcomeFile = store.locateKey(id, ID_OUTCOME_KEY);
    return id2outcomeFile;
}
Also used : File(java.io.File) StorageService(org.dkpro.lab.storage.StorageService)

Example 12 with StorageService

use of org.dkpro.lab.storage.StorageService in project dkpro-tc by dkpro.

the class TcBatchReportBase method getBaselineMajorityClassId2Outcome.

/**
 * Retrieves the id2outcome file in a train test setup. The behavior of this method in cross
 * validation tasks is undefined.
 *
 * @param id
 *            context id of machine learning adapter
 *
 * @return file to the majority class id2 outcome file in the machine learning adapter or null
 *         if the folder of machine learning adapter was not found, i.e. majority class is not
 *         defined for regression tasks
 * @throws Exception
 *             in case of errors
 */
protected File getBaselineMajorityClassId2Outcome(String id) throws Exception {
    StorageService store = getContext().getStorageService();
    File id2outcomeFile = store.locateKey(id, BASELINE_MAJORITIY_ID_OUTCOME_KEY);
    return id2outcomeFile;
}
Also used : File(java.io.File) StorageService(org.dkpro.lab.storage.StorageService)

Example 13 with StorageService

use of org.dkpro.lab.storage.StorageService 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 14 with StorageService

use of org.dkpro.lab.storage.StorageService in project dkpro-lab by dkpro.

the class DefaultTaskContext method getStorageLocation.

@Deprecated
@Override
public File getStorageLocation(String aKey, AccessMode aMode) {
    StorageKey key;
    StorageService storage = getStorageService();
    Map<String, String> imports = getMetadata().getImports();
    if (storage.containsKey(getId(), aKey)) {
        // If the context contains the key, we do nothing. Locally available data always
        // supersedes imported data.
        key = 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);
        }
        if ("file".equals(uri.getScheme()) && new File(uri).isDirectory()) {
            if (aMode == AccessMode.READONLY) {
                return new File(uri);
            } else {
                // Here we should probably just copy the imported folder into the context
                throw new DataAccessResourceFailureException("READWRITE access of imported " + "folders is not implemented yet.");
            }
        } else {
            key = resolve(aKey, aMode, true);
        }
    } else {
        key = resolve(aKey, aMode, true);
    }
    return getStorageService().getStorageFolder(key.contextId, key.key);
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) StorageKey(org.dkpro.lab.storage.StorageService.StorageKey) StorageService(org.dkpro.lab.storage.StorageService)

Example 15 with StorageService

use of org.dkpro.lab.storage.StorageService 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

StorageService (org.dkpro.lab.storage.StorageService)19 File (java.io.File)14 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)4 StorageKey (org.dkpro.lab.storage.StorageService.StorageKey)4 PropertiesAdapter (org.dkpro.lab.storage.impl.PropertiesAdapter)4 TaskContextMetadata (org.dkpro.lab.task.TaskContextMetadata)4 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Properties (java.util.Properties)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 ConversionService (org.dkpro.lab.conversion.ConversionService)1 EmbeddingTask (org.dkpro.tc.core.task.deep.EmbeddingTask)1 PreparationTask (org.dkpro.tc.core.task.deep.PreparationTask)1 ID2OutcomeCombiner (org.dkpro.tc.ml.report.util.ID2OutcomeCombiner)1