Search in sources :

Example 16 with ModelContent

use of org.knime.core.node.ModelContent in project knime-core by knime.

the class SotaNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File internDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    // Save in data container
    DataContainer.writeToZip(m_sota.getInDataContainer(), new File(internDir, IN_DATA_FILE), exec);
    // Save original Data
    DataContainer.writeToZip(m_sota.getOriginalData(), new File(internDir, ORIG_DATA_FILE), exec);
    // Save tree
    ModelContent modelContent = new ModelContent(INTERNAL_MODEL);
    m_sota.getRoot().saveTo(modelContent, 0);
    // Save settings
    modelContent.addBoolean(SotaPortObject.CFG_KEY_USE_FUZZY_HIERARCHY, m_sota.isUseHierarchicalFuzzyData());
    modelContent.addInt(SotaPortObject.CFG_KEY_MAX_FUZZY_LEVEL, m_sota.getMaxHierarchicalLevel());
    modelContent.addInt(SotaPortObject.CFG_KEY_INDATA_SIZE, m_sota.getInDataContainer().size());
    modelContent.addInt(SotaPortObject.CFG_KEY_ORIGDATA_SIZE, m_sota.getOriginalData().size());
    File file = new File(internDir, TREE_FILE);
    FileOutputStream fos = new FileOutputStream(file);
    modelContent.saveToXML(fos);
    fos.close();
}
Also used : ModelContent(org.knime.core.node.ModelContent) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 17 with ModelContent

use of org.knime.core.node.ModelContent in project knime-core by knime.

the class BasisFunctionLearnerNodeModel method execute.

/**
 * Starts the learning algorithm in the learner.
 *
 * @param inData the input training data at index 0
 * @param exec the execution monitor
 * @return the output fuzzy rule model
 * @throws CanceledExecutionException if the training was canceled
 */
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException {
    BufferedDataTable data = (BufferedDataTable) inData[0];
    // find all double cell columns in the data
    DataTableSpec tSpec = data.getDataTableSpec();
    LinkedHashSet<String> columns = new LinkedHashSet<String>(tSpec.getNumColumns());
    List<String> targetHash = Arrays.asList(m_targetColumns);
    for (int c = 0; c < tSpec.getNumColumns(); c++) {
        DataColumnSpec cSpec = tSpec.getColumnSpec(c);
        String name = cSpec.getName();
        if (!targetHash.contains(name)) {
            // TODO only numeric columns allowed
            if (cSpec.getType().isCompatible(DoubleValue.class)) {
                columns.add(cSpec.getName());
            }
        }
    }
    // get all data columns without target columns
    String[] dataCols = BasisFunctionFactory.findDataColumns(tSpec, targetHash);
    columns.addAll(Arrays.asList(dataCols));
    // add target columns at the end
    columns.addAll(Arrays.asList(m_targetColumns));
    // filter selected columns from input data
    String[] cols = columns.toArray(new String[] {});
    ColumnRearranger colRe = new ColumnRearranger(tSpec);
    colRe.keepOnly(cols);
    BufferedDataTable trainData = exec.createColumnRearrangeTable(data, colRe, exec);
    // print settings info
    LOGGER.debug("distance      : " + getDistance());
    LOGGER.debug("missing       : " + getMissingFct());
    LOGGER.debug("target columns: " + Arrays.toString(m_targetColumns));
    LOGGER.debug("shrink commit : " + isShrinkAfterCommit());
    LOGGER.debug("max coverage  : " + isMaxClassCoverage());
    LOGGER.debug("max no. epochs: " + m_maxEpochs);
    // create factory
    BasisFunctionFactory factory = getFactory(trainData.getDataTableSpec());
    // start training
    BasisFunctionLearnerTable table = new BasisFunctionLearnerTable(trainData, dataCols, m_targetColumns, factory, BasisFunctionLearnerTable.MISSINGS[m_missing], m_shrinkAfterCommit, m_maxCoverage, m_maxEpochs, exec);
    DataTableSpec modelSpec = table.getDataTableSpec();
    DataColumnSpec[] modelSpecs = new DataColumnSpec[modelSpec.getNumColumns()];
    for (int i = 0; i < modelSpecs.length; i++) {
        DataColumnSpecCreator creator = new DataColumnSpecCreator(modelSpec.getColumnSpec(i));
        creator.removeAllHandlers();
        modelSpecs[i] = creator.createSpec();
    }
    // set translator mapping
    m_translator.setMapper(table.getHiLiteMapper());
    ModelContent modelInfo = new ModelContent(MODEL_INFO);
    table.saveInfos(modelInfo);
    m_modelInfo = modelInfo;
    // return rules[0] and rule_model[1]
    return new PortObject[] { exec.createBufferedDataTable(table, exec), createPortObject(new BasisFunctionModelContent(table.getDataTableSpec(), table.getBasisFunctions())) };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ModelContent(org.knime.core.node.ModelContent) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject)

Example 18 with ModelContent

use of org.knime.core.node.ModelContent in project knime-core by knime.

the class DecTreeToImageNodeModel method saveInternals.

/**
 * Save internals.
 *
 * @param nodeInternDir The intern node directory to save table to.
 * @param exec Used to report progress or cancel saving.
 * @throws IOException Always, since this method has not been implemented
 *             yet.
 * @see org.knime.core.node.NodeModel
 *      #saveInternals(java.io.File,ExecutionMonitor)
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
    // write the tree as pred params
    ModelContent model = new ModelContent(DEC_TREE_FILE_NAME);
    m_decTree.saveToPredictorParams(model, true);
    File internalsFile = new File(nodeInternDir, DEC_TREE_FILE_NAME);
    BufferedOutputStream out2 = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(internalsFile)));
    model.saveToXML(out2);
    out2.close();
    exec.setProgress(0.5);
    // write the image
    File f = new File(nodeInternDir, IMAGE_FILE_NAME);
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
    m_imageContent.save(out);
    out.close();
    exec.setProgress(1.0);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ModelContent(org.knime.core.node.ModelContent) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 19 with ModelContent

use of org.knime.core.node.ModelContent in project knime-core by knime.

the class DecTreePredictorNodeModel method saveInternals.

/**
 * Save internals.
 *
 * @param nodeInternDir The intern node directory to save table to.
 * @param exec Used to report progress or cancel saving.
 * @throws IOException Always, since this method has not been implemented
 *             yet.
 * @see org.knime.core.node.NodeModel
 *      #saveInternals(java.io.File,ExecutionMonitor)
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
    // write the tree as pred params
    ModelContent model = new ModelContent(INTERNALS_FILE_NAME);
    m_decTree.saveToPredictorParams(model, true);
    File internalsFile = new File(nodeInternDir, INTERNALS_FILE_NAME);
    BufferedOutputStream out2 = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(internalsFile)));
    model.saveToXML(out2);
    out2.close();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ModelContent(org.knime.core.node.ModelContent) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 20 with ModelContent

use of org.knime.core.node.ModelContent in project knime-core by knime.

the class CAIMDiscretizationNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    ModelContent binModel = new ModelContent(SAVE_INTERNALS_FILE_NAME);
    // save the table spec of included names first
    ModelContentWO sub = binModel.addModelContent(CONFIG_KEY_COLUMN_NANES);
    DataTableSpec spec = (DataTableSpec) m_discretizationModel.getSpec();
    spec.save(sub);
    // now save the model.
    m_discretizationModel.saveToModelContent(binModel);
    File internalsFile = new File(nodeInternDir, SAVE_INTERNALS_FILE_NAME);
    BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(internalsFile)));
    binModel.saveToXML(out);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) GZIPOutputStream(java.util.zip.GZIPOutputStream) ModelContent(org.knime.core.node.ModelContent) ModelContentWO(org.knime.core.node.ModelContentWO) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ModelContent (org.knime.core.node.ModelContent)29 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)15 BufferedOutputStream (java.io.BufferedOutputStream)12 GZIPOutputStream (java.util.zip.GZIPOutputStream)11 ModelContentWO (org.knime.core.node.ModelContentWO)7 JComponent (javax.swing.JComponent)6 ModelContentOutPortView (org.knime.core.node.workflow.ModelContentOutPortView)6 DataTableSpec (org.knime.core.data.DataTableSpec)3 ZipEntry (java.util.zip.ZipEntry)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 LinkedHashSet (java.util.LinkedHashSet)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1