Search in sources :

Example 36 with BufferedDataTable

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

the class ListFilesNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    ListFiles lister = new ListFiles(m_settings);
    BufferedDataTable table = lister.search(exec);
    return new BufferedDataTable[] { table };
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 37 with BufferedDataTable

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

the class ColumnListLoopStartNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    ColumnRearranger crea = createRearranger(inData[0].getDataTableSpec());
    m_currentColIndex++;
    if (m_settings.iterateAllColumns()) {
        m_lastIteration = m_currentColIndex >= inData[0].getDataTableSpec().getNumColumns();
    } else {
        m_lastIteration = m_currentColIndex >= m_settings.iterateOverColumns().size();
    }
    pushFlowVariableInt("currentIteration", m_iteration);
    // increment counter for next iteration
    m_iteration++;
    return new BufferedDataTable[] { exec.createColumnRearrangeTable(inData[0], crea, exec) };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 38 with BufferedDataTable

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

the class NaiveBayesPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    LOGGER.debug("Entering execute(inData, exec) of class " + "NaiveBayesPredictorNodeModel.");
    // check input data
    assert (inData != null && inData.length == 2 && inData[DATA_IN_PORT] != null && inData[MODEL_IN_PORT] != null);
    final PortObject dataObject = inData[DATA_IN_PORT];
    if (!(dataObject instanceof BufferedDataTable)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final BufferedDataTable data = (BufferedDataTable) dataObject;
    final PortObject modelObject = inData[MODEL_IN_PORT];
    if (!(modelObject instanceof NaiveBayesPortObject)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final NaiveBayesModel model = ((NaiveBayesPortObject) modelObject).getModel();
    exec.setMessage("Classifying rows...");
    if (model == null) {
        throw new Exception("Node not properly configured. " + "No Naive Bayes Model available.");
    }
    final double laplaceCorrector = m_laplaceCorrector.getDoubleValue();
    final NaiveBayesCellFactory appender = new NaiveBayesCellFactory(model, data.getDataTableSpec(), m_inclProbVals.getBooleanValue(), laplaceCorrector);
    final ColumnRearranger rearranger = new ColumnRearranger(data.getDataTableSpec());
    rearranger.append(appender);
    final BufferedDataTable returnVal = exec.createColumnRearrangeTable(data, rearranger, exec);
    LOGGER.debug("Exiting execute(inData, exec) of class " + "NaiveBayesPredictorNodeModel.");
    return new PortObject[] { returnVal };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel.NaiveBayesModel) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) PortObject(org.knime.core.node.port.PortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 39 with BufferedDataTable

use of org.knime.core.node.BufferedDataTable 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;
    long numberRows = inData.size();
    exec.setMessage("Classifying...");
    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()) {
            for (int i = numInCells; i < newCells.length - 1; i++) {
                String predClass = outSpec.getColumnSpec(i).getName();
                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) 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 40 with BufferedDataTable

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

the class MLPPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable testdata = (BufferedDataTable) inData[1];
    PMMLPortObject pmmlPort = (PMMLPortObject) inData[0];
    List<Node> models = pmmlPort.getPMMLValue().getModels(PMMLModelType.NeuralNetwork);
    if (models.isEmpty()) {
        String msg = "Neural network evaluation failed: " + "No neural network model found.";
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    }
    PMMLNeuralNetworkTranslator trans = new PMMLNeuralNetworkTranslator();
    pmmlPort.initializeModelTranslator(trans);
    m_mlp = trans.getMLP();
    m_columns = getLearningColumnIndices(testdata.getDataTableSpec(), pmmlPort.getSpec());
    DataColumnSpec targetCol = pmmlPort.getSpec().getTargetCols().iterator().next();
    MLPClassificationFactory mymlp;
    /*
         * Regression
         */
    if (m_mlp.getMode() == MultiLayerPerceptron.REGRESSION_MODE) {
        mymlp = new MLPClassificationFactory(true, m_columns, targetCol);
    } else if (m_mlp.getMode() == MultiLayerPerceptron.CLASSIFICATION_MODE) {
        /*
             * Classification
             */
        mymlp = new MLPClassificationFactory(false, m_columns, targetCol);
    } else {
        throw new Exception("Unsupported Mode: " + m_mlp.getMode());
    }
    ColumnRearranger colre = new ColumnRearranger(testdata.getDataTableSpec());
    colre.append(mymlp);
    BufferedDataTable bdt = exec.createColumnRearrangeTable(testdata, colre, exec);
    return new BufferedDataTable[] { bdt };
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Node(org.w3c.dom.Node) BufferedDataTable(org.knime.core.node.BufferedDataTable) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Aggregations

BufferedDataTable (org.knime.core.node.BufferedDataTable)460 DataTableSpec (org.knime.core.data.DataTableSpec)221 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)148 DataRow (org.knime.core.data.DataRow)130 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)104 PortObject (org.knime.core.node.port.PortObject)102 DataCell (org.knime.core.data.DataCell)93 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)66 DataColumnSpec (org.knime.core.data.DataColumnSpec)65 DefaultRow (org.knime.core.data.def.DefaultRow)63 RowKey (org.knime.core.data.RowKey)59 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)55 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)54 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)50 ExecutionContext (org.knime.core.node.ExecutionContext)47 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)44 IOException (java.io.IOException)43 ArrayList (java.util.ArrayList)37 LinkedHashMap (java.util.LinkedHashMap)31 Test (org.junit.Test)30