Search in sources :

Example 36 with PortObjectSpec

use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.

the class DecTreePredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
public PortObject[] execute(final PortObject[] inPorts, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    exec.setMessage("Decision Tree Predictor: Loading predictor...");
    PMMLPortObject port = (PMMLPortObject) inPorts[INMODELPORT];
    List<Node> models = port.getPMMLValue().getModels(PMMLModelType.TreeModel);
    if (models.isEmpty()) {
        String msg = "Decision Tree evaluation failed: " + "No tree model found.";
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    }
    PMMLDecisionTreeTranslator trans = new PMMLDecisionTreeTranslator();
    port.initializeModelTranslator(trans);
    DecisionTree decTree = trans.getDecisionTree();
    decTree.resetColorInformation();
    BufferedDataTable inData = (BufferedDataTable) inPorts[INDATAPORT];
    // get column with color information
    String colorColumn = null;
    for (DataColumnSpec s : inData.getDataTableSpec()) {
        if (s.getColorHandler() != null) {
            colorColumn = s.getName();
            break;
        }
    }
    decTree.setColorColumn(colorColumn);
    exec.setMessage("Decision Tree Predictor: start execution.");
    PortObjectSpec[] inSpecs = new PortObjectSpec[] { inPorts[0].getSpec(), inPorts[1].getSpec() };
    DataTableSpec outSpec = createOutTableSpec(inSpecs);
    BufferedDataContainer outData = exec.createDataContainer(outSpec);
    long coveredPattern = 0;
    long nrPattern = 0;
    long rowCount = 0;
    final long numberRows = inData.size();
    exec.setMessage("Classifying...");
    List<String> predictionValues = getPredictionStrings((PMMLPortObjectSpec) inPorts[INMODELPORT].getSpec());
    for (DataRow thisRow : inData) {
        DataCell cl = null;
        LinkedHashMap<String, Double> classDistrib = null;
        try {
            Pair<DataCell, LinkedHashMap<DataCell, Double>> pair = decTree.getWinnerAndClasscounts(thisRow, inData.getDataTableSpec());
            cl = pair.getFirst();
            LinkedHashMap<DataCell, Double> classCounts = pair.getSecond();
            classDistrib = getDistribution(classCounts);
            if (coveredPattern < m_maxNumCoveredPattern.getIntValue()) {
                // remember this one for HiLite support
                decTree.addCoveredPattern(thisRow, inData.getDataTableSpec());
                coveredPattern++;
            } else {
                // too many patterns for HiLite - at least remember color
                decTree.addCoveredColor(thisRow, inData.getDataTableSpec());
            }
            nrPattern++;
        } catch (Exception e) {
            LOGGER.error("Decision Tree evaluation failed: " + e.getMessage());
            throw e;
        }
        if (cl == null) {
            LOGGER.error("Decision Tree evaluation failed: result empty");
            throw new Exception("Decision Tree evaluation failed.");
        }
        DataCell[] newCells = new DataCell[outSpec.getNumColumns()];
        int numInCells = thisRow.getNumCells();
        for (int i = 0; i < numInCells; i++) {
            newCells[i] = thisRow.getCell(i);
        }
        if (m_showDistribution.getBooleanValue()) {
            assert predictionValues.size() >= newCells.length - 1 - numInCells : "Could not determine the prediction values: " + newCells.length + "; " + numInCells + "; " + predictionValues;
            for (int i = numInCells; i < newCells.length - 1; i++) {
                String predClass = predictionValues.get(i - numInCells);
                if (classDistrib != null && classDistrib.get(predClass) != null) {
                    newCells[i] = new DoubleCell(classDistrib.get(predClass));
                } else {
                    newCells[i] = new DoubleCell(0.0);
                }
            }
        }
        newCells[newCells.length - 1] = cl;
        outData.addRowToTable(new DefaultRow(thisRow.getKey(), newCells));
        rowCount++;
        if (rowCount % 100 == 0) {
            exec.setProgress(rowCount / (double) numberRows, "Classifying... Row " + rowCount + " of " + numberRows);
        }
        exec.checkCanceled();
    }
    if (coveredPattern < nrPattern) {
        // let the user know that we did not store all available pattern
        // for HiLiting.
        this.setWarningMessage("Tree only stored first " + m_maxNumCoveredPattern.getIntValue() + " (of " + nrPattern + ") rows for HiLiting!");
    }
    outData.close();
    m_decTree = decTree;
    exec.setMessage("Decision Tree Predictor: end execution.");
    return new BufferedDataTable[] { outData.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) DoubleCell(org.knime.core.data.def.DoubleCell) Node(org.w3c.dom.Node) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 37 with PortObjectSpec

use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.

the class DecTreePredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    String predCol = m_predictionColumn.getStringValue();
    CheckUtils.checkSetting(!m_overridePrediction.getBooleanValue() || (predCol != null && !predCol.trim().isEmpty()), "Prediction column name cannot be empty");
    PMMLPortObjectSpec treeSpec = (PMMLPortObjectSpec) inSpecs[INMODELPORT];
    DataTableSpec inSpec = (DataTableSpec) inSpecs[1];
    for (String learnColName : treeSpec.getLearningFields()) {
        if (!inSpec.containsName(learnColName)) {
            throw new InvalidSettingsException("Learning column \"" + learnColName + "\" not found in input " + "data to be predicted");
        }
    }
    return new PortObjectSpec[] { createOutTableSpec(inSpecs) };
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 38 with PortObjectSpec

use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.

the class RPropNodeModel method configure.

/**
 * returns null.
 *
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if (m_classcol.getStringValue() != null) {
        List<String> learningCols = new LinkedList<String>();
        List<String> targetCols = new LinkedList<String>();
        boolean classcolinspec = false;
        for (DataColumnSpec colspec : (DataTableSpec) inSpecs[INDATA]) {
            if (!(colspec.getName().toString().compareTo(m_classcol.getStringValue()) == 0)) {
                if (!colspec.getType().isCompatible(DoubleValue.class)) {
                    throw new InvalidSettingsException("Only double columns for input");
                } else {
                    learningCols.add(colspec.getName());
                    DataColumnDomain domain = colspec.getDomain();
                    if (domain.hasBounds()) {
                        double lower = ((DoubleValue) domain.getLowerBound()).getDoubleValue();
                        double upper = ((DoubleValue) domain.getUpperBound()).getDoubleValue();
                        if (lower < 0 || upper > 1) {
                            setWarningMessage("Input data not normalized." + " Please consider using the " + "Normalizer Node first.");
                        }
                    }
                }
            } else {
                targetCols.add(colspec.getName());
                classcolinspec = true;
                // TODO: Check what happens to other values than double
                if (colspec.getType().isCompatible(DoubleValue.class)) {
                    // check if the values are in range [0,1]
                    DataColumnDomain domain = colspec.getDomain();
                    if (domain.hasBounds()) {
                        double lower = ((DoubleValue) domain.getLowerBound()).getDoubleValue();
                        double upper = ((DoubleValue) domain.getUpperBound()).getDoubleValue();
                        if (lower < 0 || upper > 1) {
                            throw new InvalidSettingsException("Domain range for regression in column " + colspec.getName() + " not in range [0,1]");
                        }
                    }
                }
            }
        }
        if (!classcolinspec) {
            throw new InvalidSettingsException("Class column " + m_classcol.getStringValue() + " not found in DataTableSpec");
        }
        return new PortObjectSpec[] { createPMMLPortObjectSpec(m_pmmlInEnabled ? (PMMLPortObjectSpec) inSpecs[1] : null, (DataTableSpec) inSpecs[0], learningCols, targetCols) };
    } else {
        throw new InvalidSettingsException("Class column not set");
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LinkedList(java.util.LinkedList)

Example 39 with PortObjectSpec

use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.

the class CAIMDiscretizationNodeModel method configure.

/**
 * The number of the class columns must be > 0 and < number of input
 * columns. Also create the output table spec replacing the columns to
 * discretize to nominal String values.
 *
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    // if no columns are defined to discretize, return the input spec
    if (m_includedColumnNames.getIncludeList() == null || m_includedColumnNames.getIncludeList().size() == 0) {
        throw new InvalidSettingsException(WARNING_NO_COLS_SELECTED);
    }
    DataTableSpec inDataSpec = (DataTableSpec) inSpecs[DATA_INPORT];
    if (inDataSpec == null) {
        // garbage in, garbage out
        return new PortObjectSpec[] { null, new DataTableSpec() };
    }
    // contained in the in data table
    for (String includedColName : m_includedColumnNames.getIncludeList()) {
        if (!inDataSpec.containsName(includedColName)) {
            throw new InvalidSettingsException("The selected column to bin '" + includedColName + "' does not exist in the input data " + "table. Reconfigure this node!");
        }
    }
    // else replace for each included column the attribute type to
    // string
    DataColumnSpec[] newColumnSpecs = new DataColumnSpec[inDataSpec.getNumColumns()];
    int counter = 0;
    for (DataColumnSpec originalColumnSpec : inDataSpec) {
        // if the column is included for discretizing, change the spec
        if (isIncluded(originalColumnSpec, m_includedColumnNames.getIncludeList().toArray(new String[m_includedColumnNames.getIncludeList().size()])) > -1) {
            // create a nominal string column spec
            newColumnSpecs[counter] = new DataColumnSpecCreator(originalColumnSpec.getName(), StringCell.TYPE).createSpec();
        } else {
            // add it as is
            newColumnSpecs[counter] = originalColumnSpec;
        }
        counter++;
    }
    return new PortObjectSpec[] { new DataTableSpec(newColumnSpecs), createModelSpec(m_includedColumnNames, inDataSpec) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 40 with PortObjectSpec

use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.

the class CaseStartNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    int selectedOutputPort = m_selectedPortModel.getIntValue();
    PortObjectSpec[] outSpecs = new PortObjectSpec[3];
    PortObjectSpec defOutput = m_activateAllOutputsDuringConfigureModel.getBooleanValue() ? inSpecs[0] : InactiveBranchPortObjectSpec.INSTANCE;
    Arrays.fill(outSpecs, defOutput);
    outSpecs[selectedOutputPort] = inSpecs[0];
    return outSpecs;
}
Also used : PortObjectSpec(org.knime.core.node.port.PortObjectSpec) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)

Aggregations

PortObjectSpec (org.knime.core.node.port.PortObjectSpec)123 DataTableSpec (org.knime.core.data.DataTableSpec)91 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)81 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)35 DataColumnSpec (org.knime.core.data.DataColumnSpec)28 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)28 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)24 FlowVariablePortObjectSpec (org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec)19 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)18 IOException (java.io.IOException)17 PortObject (org.knime.core.node.port.PortObject)14 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)14 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)12 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)12 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)12 DataType (org.knime.core.data.DataType)11 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)11 File (java.io.File)10 ArrayList (java.util.ArrayList)10 DoubleValue (org.knime.core.data.DoubleValue)10