Search in sources :

Example 1 with TreeMetaData

use of org.knime.base.node.mine.treeensemble.data.TreeMetaData in project knime-core by knime.

the class RegressionTreeModel method load.

/**
 * Loads and returns new ensemble model, input is NOT closed afterwards.
 *
 * @param in ...
 * @param exec ...
 * @return ...
 * @throws IOException ...
 * @throws CanceledExecutionException ...
 */
public static RegressionTreeModel load(final InputStream in, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    // wrapping the argument (zip input) stream in a buffered stream
    // reduces read operation from, e.g. 42s to 2s
    TreeModelDataInputStream input = new TreeModelDataInputStream(new BufferedInputStream(new NonClosableInputStream(in)));
    int version = input.readInt();
    if (version > 20140201) {
        throw new IOException("Tree Ensemble version " + version + " not supported");
    }
    TreeType type = TreeType.load(input);
    TreeMetaData metaData = TreeMetaData.load(input);
    boolean isRegression = metaData.isRegression();
    TreeModelRegression model;
    try {
        model = TreeModelRegression.load(input, metaData);
        if (input.readByte() != 0) {
            throw new IOException("Model not terminated by 0 byte");
        }
    } catch (IOException e) {
        throw new IOException("Can't read tree model. " + e.getMessage(), e);
    }
    // does not close the method argument stream!!
    input.close();
    return new RegressionTreeModel(metaData, model, type);
}
Also used : TreeType(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModel.TreeType) BufferedInputStream(java.io.BufferedInputStream) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) TreeMetaData(org.knime.base.node.mine.treeensemble.data.TreeMetaData) IOException(java.io.IOException)

Example 2 with TreeMetaData

use of org.knime.base.node.mine.treeensemble.data.TreeMetaData in project knime-core by knime.

the class TreeEnsembleModel method load.

/**
 * Loads and returns new ensemble model, input is NOT closed afterwards.
 *
 * @param in ...
 * @param exec ...
 * @return ...
 * @throws IOException ...
 * @throws CanceledExecutionException ...
 */
public static TreeEnsembleModel load(final InputStream in, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    // wrapping the argument (zip input) stream in a buffered stream
    // reduces read operation from, e.g. 42s to 2s
    TreeModelDataInputStream input = new TreeModelDataInputStream(new BufferedInputStream(new NonClosableInputStream(in)));
    int version = input.readInt();
    if (version > 20140201) {
        throw new IOException("Tree Ensemble version " + version + " not supported");
    }
    TreeType type = TreeType.load(input);
    TreeMetaData metaData = TreeMetaData.load(input);
    int nrModels = input.readInt();
    boolean containsClassDistribution;
    if (version == 20121019) {
        containsClassDistribution = true;
    } else {
        containsClassDistribution = input.readBoolean();
    }
    input.setContainsClassDistribution(containsClassDistribution);
    AbstractTreeModel[] models = new AbstractTreeModel[nrModels];
    boolean isRegression = metaData.isRegression();
    for (int i = 0; i < nrModels; i++) {
        AbstractTreeModel singleModel;
        try {
            singleModel = isRegression ? TreeModelRegression.load(input, metaData) : TreeModelClassification.load(input, metaData);
            if (input.readByte() != 0) {
                throw new IOException("Model not terminated by 0 byte");
            }
        } catch (IOException e) {
            throw new IOException("Can't read tree model " + (i + 1) + "/" + nrModels + ": " + e.getMessage(), e);
        }
        models[i] = singleModel;
    }
    // does not close the method argument stream!!
    input.close();
    return new TreeEnsembleModel(metaData, models, type, containsClassDistribution);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) TreeMetaData(org.knime.base.node.mine.treeensemble.data.TreeMetaData) IOException(java.io.IOException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 TreeMetaData (org.knime.base.node.mine.treeensemble.data.TreeMetaData)2 NonClosableInputStream (org.knime.core.data.util.NonClosableInputStream)2 TreeType (org.knime.base.node.mine.treeensemble.model.TreeEnsembleModel.TreeType)1