Search in sources :

Example 51 with DataColumnSpec

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

the class DBGroupByAggregationPanel method onAddIt.

private void onAddIt() {
    List<DataColumnSpec> selections = m_columns.getSelectedValuesList();
    for (DataColumnSpec spec : selections) {
        String selection = spec.getName();
        m_aggregatedColumnsModel.addRow(new Object[] { selection, m_aggregationFunctionComboBox.getItemAt(0) });
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec)

Example 52 with DataColumnSpec

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

the class NaiveBayesModel method createModelMap.

private static Map<String, AttributeModel> createModelMap(final DataTableSpec tableSpec, final String classColName, final int maxNoOfNominalVals, final boolean skipMissingVals) {
    final int numColumns = tableSpec.getNumColumns();
    final Map<String, AttributeModel> modelMap = new HashMap<String, AttributeModel>(numColumns);
    for (int i = 0; i < numColumns; i++) {
        final DataColumnSpec colSpec = tableSpec.getColumnSpec(i);
        final String colName = colSpec.getName();
        final DataType colType = colSpec.getType();
        final AttributeModel model;
        if (colName.equals(classColName)) {
            model = new ClassAttributeModel(colName, skipMissingVals, maxNoOfNominalVals);
        } else if (colType.isCompatible(DoubleValue.class)) {
            model = new NumericalAttributeModel(colName, skipMissingVals);
        } else if (colType.isCompatible(NominalValue.class)) {
            model = new NominalAttributeModel(colName, skipMissingVals, maxNoOfNominalVals);
        } else if (colType.isCompatible(BitVectorValue.class)) {
            model = new BitVectorAttributeModel(colName, skipMissingVals);
        } else {
            continue;
        }
        modelMap.put(colName, model);
    }
    return modelMap;
}
Also used : HashMap(java.util.HashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Example 53 with DataColumnSpec

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

the class NaiveBayesModel method check4UnknownCols.

/**
 * Checks if the given table specification contains columns which are not
 * covered by the learned model. Either because the name is not known
 * or the type is wrong.
 * @param tableSpec the <code>DataTableSpec</code> to check for unknown
 * columns
 * @return the name of the unknown columns or an empty <code>List</code>
 */
public List<String> check4UnknownCols(final DataTableSpec tableSpec) {
    if (tableSpec == null) {
        throw new NullPointerException("TableSpec must not be null");
    }
    final List<String> unknownCols = new ArrayList<String>();
    for (int i = 0, length = tableSpec.getNumColumns(); i < length; i++) {
        final DataColumnSpec colSpec = tableSpec.getColumnSpec(i);
        final AttributeModel attrModel = getAttributeModel(colSpec.getName());
        if (attrModel == null || !colSpec.getType().isCompatible(attrModel.getCompatibleType())) {
            unknownCols.add(colSpec.getName());
        }
    }
    return unknownCols;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ArrayList(java.util.ArrayList)

Example 54 with DataColumnSpec

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

the class NaiveBayesPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    // check the input data
    assert (inSpecs != null && inSpecs.length == 2 && inSpecs[DATA_IN_PORT] != null && inSpecs[MODEL_IN_PORT] != null);
    final PortObjectSpec modelObject = inSpecs[MODEL_IN_PORT];
    if (!(modelObject instanceof NaiveBayesPortObjectSpec)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final DataTableSpec trainingSpec = ((NaiveBayesPortObjectSpec) modelObject).getTableSpec();
    final DataColumnSpec classColumn = ((NaiveBayesPortObjectSpec) modelObject).getClassColumn();
    if (trainingSpec == null) {
        throw new InvalidSettingsException("No model spec available");
    }
    final PortObjectSpec inSpec = inSpecs[DATA_IN_PORT];
    if (!(inSpec instanceof DataTableSpec)) {
        throw new IllegalArgumentException("TableSpec must not be null");
    }
    final DataTableSpec spec = (DataTableSpec) inSpec;
    // check the input data for columns with the wrong name or wrong type
    final List<String> unknownCols = check4UnknownCols(trainingSpec, spec);
    if (unknownCols.size() >= spec.getNumColumns()) {
        setWarningMessage("No known attribute columns found use " + "class prior probability to predict the class membership");
    } else if (unknownCols.size() == 1) {
        setWarningMessage("Input column " + unknownCols.get(0) + " is unknown and will be skipped.");
    } else if (unknownCols.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following input columns are unknown and " + "will be skipped: ");
        for (int i = 0, length = unknownCols.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(unknownCols.get(i));
        }
        setWarningMessage(buf.toString());
    }
    // check if the learned model contains columns which are not in the
    // input data
    final List<String> missingInputCols = check4MissingCols(trainingSpec, classColumn.getName(), spec);
    if (missingInputCols.size() == 1) {
        setWarningMessage("Attribute " + missingInputCols.get(0) + " is missing in the input data");
    } else if (missingInputCols.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following attributes are missing in " + "the input data: ");
        for (int i = 0, length = missingInputCols.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(missingInputCols.get(i));
        }
        setWarningMessage(buf.toString());
    }
    final DataColumnSpec resultColSpecs = NaiveBayesCellFactory.createResultColSpecs(classColumn, spec, m_inclProbVals.getBooleanValue());
    if (resultColSpecs != null) {
        return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(spec, resultColSpecs) };
    }
    return null;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec)

Example 55 with DataColumnSpec

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

the class NaiveBayesLearnerNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    // check the internal variables if they are valid
    final String classColumn = m_classifyColumnName.getStringValue();
    if (classColumn == null || classColumn.length() < 1) {
        throw new InvalidSettingsException("Please define the classification column");
    }
    final PortObjectSpec inSpec = inSpecs[TRAINING_DATA_PORT];
    if (!(inSpec instanceof DataTableSpec)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final DataTableSpec tableSpec = (DataTableSpec) inSpec;
    if (tableSpec.findColumnIndex(classColumn) < 0) {
        throw new InvalidSettingsException("Please define the classification column");
    }
    if (tableSpec.getNumColumns() < 2) {
        throw new InvalidSettingsException("Input table should contain at least 2 columns");
    }
    final int maxNoOfNominalVals = m_maxNoOfNominalVals.getIntValue();
    // check if the table contains at least one nominal column
    // and check each nominal column with a valid domain
    // if it contains more values than allowed
    boolean containsNominalCol = false;
    final List<String> toBigNominalColumns = new ArrayList<>();
    for (int i = 0, length = tableSpec.getNumColumns(); i < length; i++) {
        final DataColumnSpec colSpec = tableSpec.getColumnSpec(i);
        if (colSpec.getType().isCompatible(NominalValue.class)) {
            containsNominalCol = true;
            final DataColumnDomain domain = colSpec.getDomain();
            if (domain != null && domain.getValues() != null) {
                if (domain.getValues().size() > maxNoOfNominalVals) {
                    // unique values
                    if (colSpec.getName().equals(classColumn)) {
                        // contains too many unique values
                        throw new InvalidSettingsException("Class column domain contains too many unique values" + " (" + domain.getValues().size() + ")");
                    }
                    toBigNominalColumns.add(colSpec.getName() + " (" + domain.getValues().size() + ")");
                }
            }
        }
    }
    if (!containsNominalCol) {
        throw new InvalidSettingsException("No possible class attribute found in input table");
    }
    if (toBigNominalColumns.size() == 1) {
        setWarningMessage("Column " + toBigNominalColumns.get(0) + " will possibly be skipped.");
    } else if (toBigNominalColumns.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following columns will possibly be skipped: ");
        for (int i = 0, length = toBigNominalColumns.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(toBigNominalColumns.get(i));
        }
        setWarningMessage(buf.toString());
    }
    if (tableSpec.getNumColumns() - toBigNominalColumns.size() < 1) {
        throw new InvalidSettingsException("Not enough valid columns");
    }
    return new PortObjectSpec[] { new NaiveBayesPortObjectSpec(tableSpec, tableSpec.getColumnSpec(classColumn)) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec)

Aggregations

DataColumnSpec (org.knime.core.data.DataColumnSpec)800 DataTableSpec (org.knime.core.data.DataTableSpec)351 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)239 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)223 DataCell (org.knime.core.data.DataCell)187 ArrayList (java.util.ArrayList)167 DataType (org.knime.core.data.DataType)149 DataRow (org.knime.core.data.DataRow)124 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)123 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)119 DoubleValue (org.knime.core.data.DoubleValue)104 HashSet (java.util.HashSet)92 BufferedDataTable (org.knime.core.node.BufferedDataTable)77 LinkedHashSet (java.util.LinkedHashSet)65 LinkedHashMap (java.util.LinkedHashMap)56 LinkedList (java.util.LinkedList)47 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)46 DoubleCell (org.knime.core.data.def.DoubleCell)46 StringCell (org.knime.core.data.def.StringCell)45 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)43