Search in sources :

Example 31 with PortObjectSpec

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

the class PCAComputeNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if (m_inputColumns.getIncludeList().size() == 0) {
        selectDefaultColumns(inSpecs);
    } else {
        m_inputColumnIndices = new int[m_inputColumns.getIncludeList().size()];
        m_inputColumnNames = new String[m_inputColumns.getIncludeList().size()];
        int colIndex = 0;
        for (final String colName : m_inputColumns.getIncludeList()) {
            final DataColumnSpec colspec = ((DataTableSpec) inSpecs[DATA_INPORT]).getColumnSpec(colName);
            if (colspec == null || !colspec.getType().isCompatible(DoubleValue.class)) {
                setWarningMessage("column \"" + colName + "\" not found, selected default columns");
                selectDefaultColumns(inSpecs);
                break;
            }
            m_inputColumnIndices[colIndex] = ((DataTableSpec) inSpecs[DATA_INPORT]).findColumnIndex(colName);
            m_inputColumnNames[colIndex] = colName;
            colIndex++;
        }
    }
    return new PortObjectSpec[] { PCANodeModel.createCovarianceMatrixSpec(m_inputColumnNames), PCANodeModel.createDecompositionTableSpec(m_inputColumnNames), new PCAModelPortObjectSpec(m_inputColumnNames) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString)

Example 32 with PortObjectSpec

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

the class PolyRegLearnerNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    PMMLPortObjectSpec pmmlSpec = m_pmmlInEnabled ? (PMMLPortObjectSpec) inSpecs[1] : null;
    String[] selectedCols = computeSelectedColumns(tableSpec);
    m_columnNames = selectedCols;
    for (String colName : selectedCols) {
        DataColumnSpec dcs = tableSpec.getColumnSpec(colName);
        if (dcs == null) {
            throw new InvalidSettingsException("Selected column '" + colName + "' does not exist in input table");
        }
        if (!dcs.getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Selected column '" + dcs.getName() + "' from the input table is not a numeric column.");
        }
    }
    if (m_settings.getTargetColumn() == null) {
        throw new InvalidSettingsException("No target column selected");
    }
    if (tableSpec.findColumnIndex(m_settings.getTargetColumn()) == -1) {
        throw new InvalidSettingsException("Target column '" + m_settings.getTargetColumn() + "' does not exist.");
    }
    DataColumnSpecCreator crea = new DataColumnSpecCreator("PolyReg prediction", DoubleCell.TYPE);
    DataColumnSpec col1 = crea.createSpec();
    crea = new DataColumnSpecCreator("Prediction Error", DoubleCell.TYPE);
    DataColumnSpec col2 = crea.createSpec();
    return new PortObjectSpec[] { createModelSpec(pmmlSpec, tableSpec), AppendedColumnTable.getTableSpec(tableSpec, col1, col2), STATS_SPEC };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec)

Example 33 with PortObjectSpec

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

the class PolyRegLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    final int colCount = inSpec.getNumColumns();
    String[] selectedCols = computeSelectedColumns(inSpec);
    Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
    m_colSelected = new boolean[colCount];
    for (int i = 0; i < colCount; i++) {
        m_colSelected[i] = hash.contains(inTable.getDataTableSpec().getColumnSpec(i).getName());
    }
    final int rowCount = inTable.getRowCount();
    String[] temp = new String[m_columnNames.length + 1];
    System.arraycopy(m_columnNames, 0, temp, 0, m_columnNames.length);
    temp[temp.length - 1] = m_settings.getTargetColumn();
    FilterColumnTable filteredTable = new FilterColumnTable(inTable, temp);
    final DataArray rowContainer = new DefaultDataArray(filteredTable, 1, m_settings.getMaxRowsForView());
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
    PortObjectSpec[] outputSpec = configure((inPMMLPort == null) ? new PortObjectSpec[] { inData[0].getSpec(), null } : new PortObjectSpec[] { inData[0].getSpec(), inPMMLPort.getSpec() });
    Learner learner = new Learner((PMMLPortObjectSpec) outputSpec[0], 0d, m_settings.getMissingValueHandling() == MissingValueHandling.fail, m_settings.getDegree());
    try {
        PolyRegContent polyRegContent = learner.perform(inTable, exec);
        m_betas = fillBeta(polyRegContent);
        m_meanValues = polyRegContent.getMeans();
        ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
        crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
        PortObject[] bdt = new PortObject[] { createPMMLModel(inPMMLPort, inSpec), exec.createColumnRearrangeTable(inTable, crea, exec.createSilentSubExecutionContext(.2)), polyRegContent.createTablePortObject(exec.createSubExecutionContext(0.2)) };
        m_squaredError /= rowCount;
        if (polyRegContent.getWarningMessage() != null) {
            setWarningMessage(polyRegContent.getWarningMessage());
        }
        double[] stdErrors = PolyRegViewData.mapToArray(polyRegContent.getStandardErrors(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptStdErr());
        double[] tValues = PolyRegViewData.mapToArray(polyRegContent.getTValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptTValue());
        double[] pValues = PolyRegViewData.mapToArray(polyRegContent.getPValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptPValue());
        m_viewData = new PolyRegViewData(m_meanValues, m_betas, stdErrors, tValues, pValues, m_squaredError, polyRegContent.getAdjustedRSquared(), m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        return bdt;
    } catch (ModelSpecificationException e) {
        final String origWarning = getWarningMessage();
        final String warning = (origWarning != null && !origWarning.isEmpty()) ? (origWarning + "\n") : "" + e.getMessage();
        setWarningMessage(warning);
        final ExecutionContext subExec = exec.createSubExecutionContext(.1);
        final BufferedDataContainer empty = subExec.createDataContainer(STATS_SPEC);
        int rowIdx = 1;
        for (final String column : m_columnNames) {
            for (int d = 1; d <= m_settings.getDegree(); ++d) {
                empty.addRowToTable(new DefaultRow("Row" + rowIdx++, new StringCell(column), new IntCell(d), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
            }
        }
        empty.addRowToTable(new DefaultRow("Row" + rowIdx, new StringCell("Intercept"), new IntCell(0), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
        double[] nans = new double[m_columnNames.length * m_settings.getDegree() + 1];
        Arrays.fill(nans, Double.NaN);
        m_betas = new double[nans.length];
        // Mean only for the linear tags
        m_meanValues = new double[nans.length / m_settings.getDegree()];
        m_viewData = new PolyRegViewData(m_meanValues, m_betas, nans, nans, nans, m_squaredError, Double.NaN, m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        empty.close();
        ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
        crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
        BufferedDataTable rearrangerTable = exec.createColumnRearrangeTable(inTable, crea, exec.createSubProgress(0.6));
        PMMLPortObject model = createPMMLModel(inPMMLPort, inTable.getDataTableSpec());
        PortObject[] bdt = new PortObject[] { model, rearrangerTable, empty.getTable() };
        return bdt;
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) DoubleCell(org.knime.core.data.def.DoubleCell) FilterColumnTable(org.knime.base.data.filter.column.FilterColumnTable) DataArray(org.knime.base.node.util.DataArray) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ModelSpecificationException(org.apache.commons.math3.stat.regression.ModelSpecificationException) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) HashSet(java.util.HashSet) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ExecutionContext(org.knime.core.node.ExecutionContext) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) StringCell(org.knime.core.data.def.StringCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 34 with PortObjectSpec

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

the class ClusterNodeModel method configure.

/**
 * Returns <code>true</code> always and passes the current input spec to
 * the output spec which is identical to the input specification - after
 * all, we are building cluster centers in the original feature space.
 *
 * @param inSpecs the specifications of the input port(s) - should be one
 * @return the copied input spec
 * @throws InvalidSettingsException if PMML incompatible type was found
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec spec = (DataTableSpec) inSpecs[0];
    // input is output spec with all double compatible values set to
    // Double.
    m_dimension = spec.getNumColumns();
    // Find out which columns we can use (must be Double compatible)
    // Note that, for simplicity, we still use the entire dimensionality
    // for cluster prototypes below and simply ignore useless columns.
    m_ignoreColumn = new boolean[m_dimension];
    m_nrIgnoredColumns = 0;
    LinkedList<String> includes = new LinkedList<String>();
    includes.addAll(m_usedColumns.getIncludeList());
    LinkedList<String> excludes = new LinkedList<String>();
    excludes.addAll(m_usedColumns.getExcludeList());
    LinkedList<String> includes2 = new LinkedList<String>();
    includes2.addAll(m_usedColumns.getIncludeList());
    LinkedList<String> excludes2 = new LinkedList<String>();
    excludes2.addAll(m_usedColumns.getExcludeList());
    // First check if all incoming columns are either excluded or included
    for (String col : spec.getColumnNames()) {
        if (m_usedColumns.getIncludeList().contains(col)) {
            includes2.remove(col);
        } else if (m_usedColumns.getExcludeList().contains(col)) {
            excludes2.remove(col);
        } else {
            includes.add(col);
        }
    }
    // Leftover included columns that do not exist in the incoming table
    for (String col : includes2) {
        includes.remove(col);
    }
    // Same for excluded columns
    for (String col : excludes2) {
        excludes.remove(col);
    }
    m_usedColumns.setExcludeList(excludes);
    m_usedColumns.setIncludeList(includes);
    if (m_usedColumns.isKeepAllSelected()) {
        boolean hasNumericColumn = false;
        for (DataColumnSpec colSpec : spec) {
            if (colSpec.getType().isCompatible(DoubleValue.class)) {
                hasNumericColumn = true;
                break;
            }
        }
        if (!hasNumericColumn) {
            throw new InvalidSettingsException("No numeric columns in input");
        }
    } else {
        // double compatible columns
        if (m_usedColumns.getIncludeList().size() == 0 && m_usedColumns.getExcludeList().size() == 0) {
            List<String> includedColumns = new ArrayList<String>();
            List<String> excludedColumns = new ArrayList<String>();
            for (int i = 0; i < spec.getNumColumns(); i++) {
                DataColumnSpec colSpec = spec.getColumnSpec(i);
                if (colSpec.getType().isCompatible(DoubleValue.class)) {
                    includedColumns.add(colSpec.getName());
                } else {
                    excludedColumns.add(colSpec.getName());
                }
            }
            // set all double compatible columns as include list
            m_usedColumns.setIncludeList(includedColumns);
            m_usedColumns.setExcludeList(excludedColumns);
        }
        // check if some columns are included
        if (m_usedColumns.getIncludeList().size() <= 0) {
            setWarningMessage("No column in include list! Produces one huge cluster");
        }
    }
    addExcludeColumnsToIgnoreList(spec);
    DataTableSpec appendedSpec = createAppendedSpec(spec);
    // return spec for data and model outport!
    PMMLPortObjectSpec pmmlSpec;
    if (m_pmmlInEnabled) {
        pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
    } else {
        pmmlSpec = new PMMLPortObjectSpecCreator(spec).createSpec();
    }
    if (m_outputCenters) {
        return new PortObjectSpec[] { appendedSpec, createClusterCentersSpec(spec), createPMMLSpec(pmmlSpec, spec) };
    } else {
        return new PortObjectSpec[] { appendedSpec, createPMMLSpec(pmmlSpec, spec) };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) LinkedList(java.util.LinkedList) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 35 with PortObjectSpec

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

the class DecisionTreeLearnerNodeModel2 method configure.

/**
 * The number of the class column must be &gt; 0 and &lt; number of input columns.
 *
 * @param inSpecs the tabel specs on the input port to use for configuration
 * @see NodeModel#configure(DataTableSpec[])
 * @throws InvalidSettingsException thrown if the configuration is not
 *             correct
 * @return the table specs for the output ports
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inSpec = (DataTableSpec) inSpecs[DATA_INPORT];
    PMMLPortObjectSpec modelSpec = m_pmmlInEnabled ? (PMMLPortObjectSpec) inSpecs[MODEL_INPORT] : null;
    // check spec with selected column
    String classifyColumn = m_classifyColumn.getStringValue();
    DataColumnSpec columnSpec = inSpec.getColumnSpec(classifyColumn);
    boolean isValid = columnSpec != null && columnSpec.getType().isCompatible(NominalValue.class);
    if (classifyColumn != null && !isValid) {
        throw new InvalidSettingsException("Class column \"" + classifyColumn + "\" not found or incompatible");
    }
    if (classifyColumn == null) {
        // auto-guessing
        assert !isValid : "No class column set but valid configuration";
        // get the first useful one starting at the end of the table
        for (int i = inSpec.getNumColumns() - 1; i >= 0; i--) {
            if (inSpec.getColumnSpec(i).getType().isCompatible(NominalValue.class)) {
                m_classifyColumn.setStringValue(inSpec.getColumnSpec(i).getName());
                super.setWarningMessage("Guessing target column: \"" + m_classifyColumn.getStringValue() + "\".");
                break;
            }
        }
        if (m_classifyColumn.getStringValue() == null) {
            throw new InvalidSettingsException("Table contains no nominal" + " attribute for classification.");
        }
    }
    if (m_useFirstSplitCol.getBooleanValue()) {
        String firstSplitCol = m_firstSplitCol.getStringValue();
        DataColumnSpec firstSplitSpec = inSpec.getColumnSpec(firstSplitCol);
        if (firstSplitCol == null) {
            throw new InvalidSettingsException("Root split column should be used but is not specified.");
        } else if (firstSplitSpec == null) {
            throw new InvalidSettingsException("The selected column for the root split \"" + firstSplitCol + "\" is not in the table.");
        } else if (firstSplitSpec.equals(columnSpec)) {
            throw new InvalidSettingsException("The class column can not be selected as the" + " first column to split on.");
        }
    }
    return new PortObjectSpec[] { createPMMLPortObjectSpec(modelSpec, inSpec) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NominalValue(org.knime.core.data.NominalValue) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

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