Search in sources :

Example 51 with PMMLPortObject

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

the class DBApplyBinnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected final PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    PMMLPortObject pmmlPortObject = (PMMLPortObject) inData[0];
    DatabasePortObject databasePortObject = (DatabasePortObject) inData[1];
    DatabaseQueryConnectionSettings connectionSettings = databasePortObject.getConnectionSettings(getCredentialsProvider());
    return new PortObject[] { createDatabasePortObject(databasePortObject.getSpec(), connectionSettings, pmmlPortObject) };
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject)

Example 52 with PMMLPortObject

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

the class DBNumericBinnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected final PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    exec.setMessage("Retrieving metadata from database");
    checkDuplicateBinNames();
    final DatabasePortObject inDatabasePortObject = (DatabasePortObject) inData[0];
    final DatabasePortObjectSpec inDatabasePortObjectSpec = inDatabasePortObject.getSpec();
    DatabaseQueryConnectionSettings connectionSettings = inDatabasePortObject.getConnectionSettings(getCredentialsProvider());
    DataTableSpec outDataTableSpec = DBAutoBinner.createNewDataTableSpec(inDatabasePortObjectSpec.getDataTableSpec(), m_columnToAppended);
    PMMLPortObject outPMMLPortObject = createPMMLPortObject(inDatabasePortObjectSpec.getDataTableSpec(), outDataTableSpec);
    DBBinnerMaps binnerMaps = DBAutoBinner.intoBinnerMaps(outPMMLPortObject, inDatabasePortObjectSpec.getDataTableSpec());
    DatabasePortObjectSpec outDatabasePortObjectSpec = createDatabasePortObjectSpec(connectionSettings, inDatabasePortObjectSpec, binnerMaps);
    return new PortObject[] { new DatabasePortObject(outDatabasePortObjectSpec), outPMMLPortObject };
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DBBinnerMaps(org.knime.base.node.io.database.binning.DBBinnerMaps) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PortObject(org.knime.core.node.port.PortObject)

Example 53 with PMMLPortObject

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

the class DBNumericBinnerNodeModel method createPMMLPortObject.

private PMMLPortObject createPMMLPortObject(final DataTableSpec inDataTableSpec, final DataTableSpec outDataTableSpec) {
    PMMLPortObjectSpec initPMMLSpec = new PMMLPortObjectSpecCreator(outDataTableSpec).createSpec();
    PMMLPortObject initPMMLPortObject = new PMMLPortObject(initPMMLSpec, null, outDataTableSpec);
    PMMLBinningTranslator pmmlBinningTranslator = new PMMLBinningTranslator(m_columnToBins, m_columnToAppended, new DerivedFieldMapper(initPMMLPortObject));
    PMMLPortObject outPMMLPortObject = new PMMLPortObject(initPMMLSpec, initPMMLPortObject, inDataTableSpec);
    outPMMLPortObject.addGlobalTransformations(pmmlBinningTranslator.exportToTransDict());
    return outPMMLPortObject;
}
Also used : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLBinningTranslator(org.knime.base.node.preproc.pmml.binner.PMMLBinningTranslator) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 54 with PMMLPortObject

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

the class DecisionTreeLearnerNodeModel method execute.

/**
 * Start of decision tree induction.
 *
 * @param exec the execution context for this run
 * @param data the input data to build the decision tree from
 * @return an empty data table array, as just a model is provided
 * @throws Exception any type of exception, e.g. for cancellation,
 *         invalid input,...
 * @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
 */
@Override
protected PortObject[] execute(final PortObject[] data, final ExecutionContext exec) throws Exception {
    // holds the warning message displayed after execution
    StringBuilder warningMessageSb = new StringBuilder();
    ParallelProcessing parallelProcessing = new ParallelProcessing(m_parallelProcessing.getIntValue());
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Number available threads: " + parallelProcessing.getMaxNumberThreads() + " used threads: " + parallelProcessing.getCurrentThreadsInUse());
    }
    exec.setProgress("Preparing...");
    // check input data
    assert (data != null && data[DATA_INPORT] != null);
    BufferedDataTable inData = (BufferedDataTable) data[DATA_INPORT];
    // get column with color information
    String colorColumn = null;
    for (DataColumnSpec s : inData.getDataTableSpec()) {
        if (s.getColorHandler() != null) {
            colorColumn = s.getName();
            break;
        }
    }
    // the data table must have more than 2 records
    if (inData.getRowCount() <= 1) {
        throw new IllegalArgumentException("Input data table must have at least 2 records!");
    }
    // get class column index
    int classColumnIndex = inData.getDataTableSpec().findColumnIndex(m_classifyColumn.getStringValue());
    assert classColumnIndex > -1;
    // create initial In-Memory table
    exec.setProgress("Create initial In-Memory table...");
    InMemoryTableCreator tableCreator = new InMemoryTableCreator(inData, classColumnIndex, m_minNumberRecordsPerNode.getIntValue(), m_skipColumns.getBooleanValue());
    InMemoryTable initialTable = tableCreator.createInMemoryTable(exec.createSubExecutionContext(0.05));
    int removedRows = tableCreator.getRemovedRowsDueToMissingClassValue();
    if (removedRows == inData.getRowCount()) {
        throw new IllegalArgumentException("Class column contains only " + "missing values");
    }
    if (removedRows > 0) {
        warningMessageSb.append(removedRows);
        warningMessageSb.append(" rows removed due to missing class value;");
    }
    // the all over row count is used to report progress
    m_alloverRowCount = initialTable.getSumOfWeights();
    // set the finishing counter
    // this counter will always be incremented when a leaf node is
    // created, as this determines the recursion end and can thus
    // be used for progress indication
    m_finishedCounter = new AtomicDouble(0);
    // get the number of attributes
    m_numberAttributes = initialTable.getNumAttributes();
    // create the quality measure
    final SplitQualityMeasure splitQualityMeasure;
    if (m_splitQualityMeasureType.getStringValue().equals(SPLIT_QUALITY_GINI)) {
        splitQualityMeasure = new SplitQualityGini();
    } else {
        splitQualityMeasure = new SplitQualityGainRatio();
    }
    // build the tree
    // before this set the node counter to 0
    m_counter.set(0);
    exec.setMessage("Building tree...");
    DecisionTreeNode root = null;
    root = buildTree(initialTable, exec, 0, splitQualityMeasure, parallelProcessing);
    boolean isBinaryNominal = m_binaryNominalSplitMode.getBooleanValue();
    boolean isFilterInvalidAttributeValues = m_filterNominalValuesFromParent.getBooleanValue();
    if (isBinaryNominal && isFilterInvalidAttributeValues) {
        // traverse tree nodes and remove from the children the attribute
        // values that were filtered out further up in the tree. "Bug" 3124
        root.filterIllegalAttributes(Collections.EMPTY_MAP);
    }
    // the decision tree model saved as PMML at the second out-port
    DecisionTree decisionTree = new DecisionTree(root, m_classifyColumn.getStringValue(), /* strategy has to be set explicitly as the default in PMML is
                    none, which means rows with missing values are not
                    classified. */
    PMMLMissingValueStrategy.LAST_PREDICTION);
    decisionTree.setColorColumn(colorColumn);
    // prune the tree
    exec.setMessage("Prune tree with " + m_pruningMethod.getStringValue() + "...");
    pruneTree(decisionTree);
    // add highlight patterns and color information
    exec.setMessage("Adding hilite and color info to tree...");
    addHiliteAndColorInfo(inData, decisionTree);
    LOGGER.info("Decision tree consisting of " + decisionTree.getNumberNodes() + " nodes created with pruning method " + m_pruningMethod.getStringValue());
    // set the warning message if available
    if (warningMessageSb.length() > 0) {
        setWarningMessage(warningMessageSb.toString());
    }
    // reset the number available threads
    parallelProcessing.reset();
    parallelProcessing = null;
    // no data out table is created -> return an empty table array
    exec.setMessage("Creating PMML decision tree model...");
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = (PMMLPortObject) data[1];
    DataTableSpec inSpec = inData.getSpec();
    PMMLPortObjectSpec outPortSpec = createPMMLPortObjectSpec(inPMMLPort == null ? null : inPMMLPort.getSpec(), inSpec);
    PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, inData.getSpec());
    outPMMLPort.addModelTranslater(new PMMLDecisionTreeTranslator(decisionTree));
    m_decisionTree = decisionTree;
    return new PortObject[] { outPMMLPort };
}
Also used : DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataColumnSpec(org.knime.core.data.DataColumnSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 55 with PMMLPortObject

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

the class DBAutoBinner method translate.

/**
 * This method translates a {@link PMMLPreprocDiscretize} object into {@link PMMLPortObject}.
 *
 * @param pmmlDiscretize {@link PMMLPreprocDiscretize} object
 * @param dataTableSpec {@link DataTableSpec} if incoming {@link BufferedDataTable}
 * @return a {@link PMMLPortObject} containing required parameters for binning operation
 */
public static PMMLPortObject translate(final PMMLPreprocDiscretize pmmlDiscretize, final DataTableSpec dataTableSpec) {
    final Map<String, Bin[]> columnToBins = new HashMap<>();
    final Map<String, String> columnToAppend = new HashMap<>();
    List<String> replacedColumnNames = pmmlDiscretize.getConfiguration().getNames();
    for (String replacedColumnName : replacedColumnNames) {
        PMMLDiscretize discretize = pmmlDiscretize.getConfiguration().getDiscretize(replacedColumnName);
        List<PMMLDiscretizeBin> bins = discretize.getBins();
        String originalColumnName = discretize.getField();
        if (replacedColumnName.equals(originalColumnName)) {
            // wenn replaced, dann nicht anhängen
            columnToAppend.put(originalColumnName, null);
        } else {
            // nicht replaced -> anhängen
            columnToAppend.put(originalColumnName, replacedColumnName);
        }
        NumericBin[] numericBin = new NumericBin[bins.size()];
        int counter = 0;
        for (PMMLDiscretizeBin bin : bins) {
            String binName = bin.getBinValue();
            List<PMMLInterval> intervals = bin.getIntervals();
            boolean leftOpen = false;
            boolean rightOpen = false;
            double leftMargin = 0;
            double rightMargin = 0;
            // always returns only one interval
            for (PMMLInterval interval : intervals) {
                Closure closure = interval.getClosure();
                switch(closure) {
                    case openClosed:
                        leftOpen = true;
                        rightOpen = false;
                        break;
                    case openOpen:
                        leftOpen = true;
                        rightOpen = true;
                        break;
                    case closedOpen:
                        leftOpen = false;
                        rightOpen = true;
                    case closedClosed:
                        leftOpen = false;
                        rightOpen = false;
                        break;
                    default:
                        leftOpen = true;
                        rightOpen = false;
                        break;
                }
                leftMargin = interval.getLeftMargin();
                rightMargin = interval.getRightMargin();
            }
            numericBin[counter] = new NumericBin(binName, leftOpen, leftMargin, rightOpen, rightMargin);
            counter++;
        }
        columnToBins.put(originalColumnName, numericBin);
    }
    // ColumnRearranger createColReg = createColReg(dataTableSpec, columnToBins, columnToAppended);
    DataTableSpec newDataTableSpec = createNewDataTableSpec(dataTableSpec, columnToAppend);
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(newDataTableSpec);
    PMMLPortObject pmmlPortObject = new PMMLPortObject(pmmlSpecCreator.createSpec(), null, newDataTableSpec);
    PMMLBinningTranslator trans = new PMMLBinningTranslator(columnToBins, columnToAppend, new DerivedFieldMapper(pmmlPortObject));
    TransformationDictionary exportToTransDict = trans.exportToTransDict();
    pmmlPortObject.addGlobalTransformations(exportToTransDict);
    return pmmlPortObject;
}
Also used : NumericBin(org.knime.base.node.preproc.pmml.binner.NumericBin) DataTableSpec(org.knime.core.data.DataTableSpec) Closure(org.knime.base.node.preproc.autobinner.pmml.PMMLInterval.Closure) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) PMMLDiscretizeBin(org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretizeBin) PMMLInterval(org.knime.base.node.preproc.autobinner.pmml.PMMLInterval) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLBinningTranslator(org.knime.base.node.preproc.pmml.binner.PMMLBinningTranslator) PMMLDiscretize(org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretize) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Aggregations

PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)90 BufferedDataTable (org.knime.core.node.BufferedDataTable)53 DataTableSpec (org.knime.core.data.DataTableSpec)50 PortObject (org.knime.core.node.port.PortObject)47 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)35 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)25 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)21 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)18 ExecutionContext (org.knime.core.node.ExecutionContext)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)12 DataRow (org.knime.core.data.DataRow)12 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)11 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)11 Node (org.w3c.dom.Node)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)9 IOException (java.io.IOException)8 DataCell (org.knime.core.data.DataCell)8 PMMLDocument (org.dmg.pmml.PMMLDocument)7 PMMLGeneralRegressionTranslator (org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionTranslator)7 StreamableFunction (org.knime.core.node.streamable.StreamableFunction)7