Search in sources :

Example 91 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator 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 92 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class PolyRegLearnerNodeModel method getCellFactory.

private CellFactory getCellFactory(final int dependentIndex) {
    final int degree = m_settings.getDegree();
    return new CellFactory() {

        @Override
        public DataCell[] getCells(final DataRow row) {
            double sum = m_betas[0];
            int betaCount = 1;
            double y = 0;
            for (int col = 0; col < row.getNumCells(); col++) {
                if ((col == dependentIndex || m_colSelected[col]) && row.getCell(col).isMissing()) {
                    switch(m_settings.getMissingValueHandling()) {
                        case ignore:
                            return new DataCell[] { DataType.getMissingCell(), DataType.getMissingCell() };
                        case fail:
                            throw new IllegalStateException("Should failed earlier!");
                        default:
                            throw new UnsupportedOperationException("Not supported missing handling strategy: " + m_settings.getMissingValueHandling());
                    }
                }
                if ((col != dependentIndex) && m_colSelected[col]) {
                    final double value = ((DoubleValue) row.getCell(col)).getDoubleValue();
                    double poly = 1;
                    for (int d = 1; d <= degree; d++) {
                        poly *= value;
                        sum += m_betas[betaCount++] * poly;
                    }
                } else if (col == dependentIndex) {
                    y = ((DoubleValue) row.getCell(col)).getDoubleValue();
                }
            }
            double err = Math.abs(sum - y);
            m_squaredError += err * err;
            return new DataCell[] { new DoubleCell(sum), new DoubleCell(err) };
        }

        @Override
        public DataColumnSpec[] getColumnSpecs() {
            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 DataColumnSpec[] { col1, col2 };
        }

        @Override
        public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor execMon) {
        // do nothing
        }
    };
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory)

Example 93 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class ClusterNodeModel method createAppendedSpec.

private DataTableSpec createAppendedSpec(final DataTableSpec originalSpec) {
    // determine the possible values of the appended column
    DataCell[] possibleValues = new DataCell[m_nrOfClusters.getIntValue()];
    for (int i = 0; i < m_nrOfClusters.getIntValue(); i++) {
        DataCell key = new StringCell(CLUSTER + i);
        possibleValues[i] = key;
    }
    // create the domain
    // 1) guess an unused name for the new column (fixes bug #1022)
    String colNameGuess = "Cluster";
    int uniqueNr = 0;
    while (originalSpec.getColumnSpec(colNameGuess) != null) {
        uniqueNr++;
        colNameGuess = "Cluster_" + uniqueNr;
    }
    // 2) create spec
    DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(possibleValues);
    DataColumnSpecCreator creator = new DataColumnSpecCreator(colNameGuess, StringCell.TYPE);
    creator.setDomain(domainCreator.createDomain());
    // create the appended column spec
    DataColumnSpec labelColSpec = creator.createSpec();
    return new DataTableSpec(originalSpec, new DataTableSpec(labelColSpec));
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString)

Example 94 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class ClusterNodeModel method createClusterCentersSpec.

private DataTableSpec createClusterCentersSpec(final DataTableSpec spec) {
    // Create spec for cluster center table
    DataTableSpecCreator clusterCenterSpecCreator = new DataTableSpecCreator();
    for (int i = 0; i < m_dimension; i++) {
        if (!m_ignoreColumn[i]) {
            clusterCenterSpecCreator.addColumns(new DataColumnSpecCreator(spec.getColumnSpec(i).getName(), DoubleCell.TYPE).createSpec());
        }
    }
    clusterCenterSpecCreator.dropAllDomains();
    return clusterCenterSpecCreator.createSpec();
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator)

Example 95 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class HierarchicalClusterNodeModel method generateOutSpec.

/**
 * Generate output spec based on input spec (appends column).
 */
private static DataTableSpec generateOutSpec(final DataTableSpec inSpec) {
    int oldColCount = inSpec.getNumColumns();
    int colCount = oldColCount + 1;
    DataColumnSpec[] colSpecs = new DataColumnSpec[colCount];
    for (int i = 0; i < oldColCount; i++) {
        colSpecs[i] = inSpec.getColumnSpec(i);
    }
    // the additional column contains the cluster information
    DataColumnSpecCreator colspeccreator = new DataColumnSpecCreator("Cluster", StringCell.TYPE);
    colSpecs[oldColCount] = colspeccreator.createSpec();
    return new DataTableSpec(colSpecs);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator)

Aggregations

DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)267 DataColumnSpec (org.knime.core.data.DataColumnSpec)210 DataTableSpec (org.knime.core.data.DataTableSpec)132 DataCell (org.knime.core.data.DataCell)92 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)77 DataType (org.knime.core.data.DataType)74 DataRow (org.knime.core.data.DataRow)73 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)57 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)51 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)48 ArrayList (java.util.ArrayList)46 DoubleCell (org.knime.core.data.def.DoubleCell)45 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)44 StringCell (org.knime.core.data.def.StringCell)29 BufferedDataTable (org.knime.core.node.BufferedDataTable)23 DoubleValue (org.knime.core.data.DoubleValue)22 HashSet (java.util.HashSet)19 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)17 DataColumnDomain (org.knime.core.data.DataColumnDomain)16 DefaultRow (org.knime.core.data.def.DefaultRow)16