Search in sources :

Example 6 with NonClosableInputStream

use of org.knime.core.data.util.NonClosableInputStream 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)

Example 7 with NonClosableInputStream

use of org.knime.core.data.util.NonClosableInputStream in project knime-core by knime.

the class TreeEnsembleModel method load.

public static TreeEnsembleModel load(final InputStream in) throws IOException {
    // 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 > 20160114) {
        throw new IOException("Tree Ensemble version " + version + " not supported");
    }
    byte ensembleType;
    if (version == 20160114) {
        ensembleType = input.readByte();
    } else {
        ensembleType = 'r';
    }
    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();
    if (ensembleType != 'r') {
        isRegression = true;
    }
    final TreeBuildingInterner treeBuildingInterner = new TreeBuildingInterner();
    for (int i = 0; i < nrModels; i++) {
        AbstractTreeModel singleModel;
        try {
            singleModel = isRegression ? TreeModelRegression.load(input, metaData, treeBuildingInterner) : TreeModelClassification.load(input, metaData, treeBuildingInterner);
            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;
    }
    TreeEnsembleModel result;
    switch(ensembleType) {
        case 'r':
            result = new TreeEnsembleModel(metaData, models, type, containsClassDistribution);
            break;
        case 'g':
            result = new GradientBoostingModel(metaData, models, type, containsClassDistribution);
            break;
        case 't':
            result = new GradientBoostedTreesModel(metaData, models, type, containsClassDistribution);
            break;
        case 'm':
            result = new MultiClassGradientBoostedTreesModel(metaData, models, type, containsClassDistribution);
            break;
        default:
            throw new IllegalStateException("Unknown ensemble type: '" + (char) ensembleType + "'");
    }
    result.loadData(input);
    // does not close the method argument stream!!
    input.close();
    return result;
}
Also used : IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) TreeMetaData(org.knime.base.node.mine.treeensemble2.data.TreeMetaData)

Example 8 with NonClosableInputStream

use of org.knime.core.data.util.NonClosableInputStream in project knime-core by knime.

the class DCObjectInputVersion2 method readDataCellPerJavaSerialization.

/**
 * Reads a data cell from the stream using java de-serialization.
 * @return A new data cell instance.
 * @throws IOException If reading fails (also e.g.
 * {@link ClassCastException} are wrapped in such IO exceptions).
 */
DataCell readDataCellPerJavaSerialization() throws IOException {
    PriorityGlobalObjectInputStream gl = new PriorityGlobalObjectInputStream(new NonClosableInputStream(m_dataIn));
    gl.setCurrentClassLoader(m_priorityClassLoader);
    try {
        return (DataCell) gl.readObject();
    } catch (Exception exception) {
        throw new IOException("Unable to restore data cell (" + exception.getClass().getSimpleName() + ")", exception);
    } finally {
        gl.close();
    }
}
Also used : NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) DataCell(org.knime.core.data.DataCell) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

NonClosableInputStream (org.knime.core.data.util.NonClosableInputStream)8 IOException (java.io.IOException)7 BufferedInputStream (java.io.BufferedInputStream)4 ZipEntry (java.util.zip.ZipEntry)2 TreeMetaData (org.knime.base.node.mine.treeensemble.data.TreeMetaData)2 TreeMetaData (org.knime.base.node.mine.treeensemble2.data.TreeMetaData)2 SAXException (org.xml.sax.SAXException)2 LinkedList (java.util.LinkedList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 XmlException (org.apache.xmlbeans.XmlException)1 XmlObject (org.apache.xmlbeans.XmlObject)1 PMMLDocument (org.dmg.pmml.PMMLDocument)1 TreeType (org.knime.base.node.mine.treeensemble.model.TreeEnsembleModel.TreeType)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1