Search in sources :

Example 66 with DataCell

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

the class AppendVariableToTableNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    ColumnRearranger arranger = new ColumnRearranger(spec);
    Set<String> nameHash = new HashSet<String>();
    for (DataColumnSpec c : spec) {
        nameHash.add(c.getName());
    }
    List<Pair<String, FlowVariable.Type>> vars;
    if (m_settings.getIncludeAll()) {
        vars = getAllVariables();
    } else {
        vars = m_settings.getVariablesOfInterest();
    }
    if (vars.isEmpty()) {
        throw new InvalidSettingsException("No variables selected");
    }
    DataColumnSpec[] specs = new DataColumnSpec[vars.size()];
    final DataCell[] values = new DataCell[vars.size()];
    for (int i = 0; i < vars.size(); i++) {
        Pair<String, FlowVariable.Type> c = vars.get(i);
        String name = c.getFirst();
        DataType type;
        switch(c.getSecond()) {
            case DOUBLE:
                type = DoubleCell.TYPE;
                try {
                    double dValue = peekFlowVariableDouble(name);
                    values[i] = new DoubleCell(dValue);
                } catch (NoSuchElementException e) {
                    throw new InvalidSettingsException("No such flow variable (of type double): " + name);
                }
                break;
            case INTEGER:
                type = IntCell.TYPE;
                try {
                    int iValue = peekFlowVariableInt(name);
                    values[i] = new IntCell(iValue);
                } catch (NoSuchElementException e) {
                    throw new InvalidSettingsException("No such flow variable (of type int): " + name);
                }
                break;
            case STRING:
                type = StringCell.TYPE;
                try {
                    String sValue = peekFlowVariableString(name);
                    sValue = sValue == null ? "" : sValue;
                    values[i] = new StringCell(sValue);
                } catch (NoSuchElementException e) {
                    throw new InvalidSettingsException("No such flow variable (of type String): " + name);
                }
                break;
            default:
                throw new InvalidSettingsException("Unsupported variable type: " + c.getSecond());
        }
        if (nameHash.contains(name) && !name.toLowerCase().endsWith("(variable)")) {
            name = name.concat(" (variable)");
        }
        String newName = name;
        int uniquifier = 1;
        while (!nameHash.add(newName)) {
            newName = name + " (#" + (uniquifier++) + ")";
        }
        specs[i] = new DataColumnSpecCreator(newName, type).createSpec();
    }
    arranger.append(new AbstractCellFactory(specs) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataCell[] getCells(final DataRow row) {
            return values;
        }
    });
    return arranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) HashSet(java.util.HashSet) Pair(org.knime.core.util.Pair) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) PortType(org.knime.core.node.port.PortType) DataType(org.knime.core.data.DataType) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 67 with DataCell

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

the class ListFiles method addLocationToContainer.

/**
 * Adds a File to the table.
 *
 * @param file
 */
private void addLocationToContainer(final File file) {
    try {
        DataCell[] row = new DataCell[2];
        row[0] = new StringCell(file.getAbsolutePath());
        row[1] = new StringCell(file.getAbsoluteFile().toURI().toURL().toString());
        m_dc.addRowToTable(new DefaultRow("Row " + m_currentRowID, row));
        m_currentRowID++;
    } catch (MalformedURLException e) {
        LOGGER.error("Unable to URL to file " + file.getAbsolutePath(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 68 with DataCell

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

the class NaiveBayesModel method updateModel.

/**
 * Updates the current {@link NaiveBayesModel} with the values from the
 * given {@link DataRow}.
 * @param row DataRow with values for update
 * @param tableSpec underlying DataTableSpec
 * @param classColIdx the index of the class column
 * @throws InvalidSettingsException if missing values occur in class column
 * or an attribute has too many values.
 */
public void updateModel(final DataRow row, final DataTableSpec tableSpec, final int classColIdx) throws InvalidSettingsException {
    if (row == null) {
        throw new NullPointerException("Row must not be null");
    }
    if (tableSpec == null) {
        throw new NullPointerException("TableSpec must not be null");
    }
    final DataCell classCell = row.getCell(classColIdx);
    if (classCell.isMissing()) {
        if (m_skipMissingVals) {
            return;
        }
        // check if the class value is missing
        throw new InvalidSettingsException("Missing class value found in row " + row.getKey() + " to skip missing values tick the box in the dialog");
    }
    final String classVal = classCell.toString();
    final int numColumns = tableSpec.getNumColumns();
    for (int i = 0; i < numColumns; i++) {
        final AttributeModel model = m_modelByAttrName.get(tableSpec.getColumnSpec(i).getName());
        if (model != null) {
            final DataCell cell = row.getCell(i);
            try {
                model.addValue(classVal, cell);
            } catch (final TooManyValuesException e) {
                if (model instanceof ClassAttributeModel) {
                    throw new InvalidSettingsException("Class attribute has too many unique values. " + "To avoid this exception increase the " + "maximum number of allowed nominal " + "values in the node dialog");
                }
                // delete the model if it contains too many unique values
                m_modelByAttrName.remove(model.getAttributeName());
                model.setInvalidCause("Too many values");
                m_skippedAttributes.add(model);
            }
        }
    }
    m_noOfRecs++;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) TooManyValuesException(org.knime.base.node.mine.bayes.naivebayes.datamodel2.TooManyValuesException)

Example 69 with DataCell

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

the class NaiveBayesModel method getClassProbability.

/**
 * Returns the probability of the row to be member of the given class.
 * All not known attributes are skipped. If none of the given attributes
 * is known the method returns the class prior probability.
 * @param attrNames the name of the attributes
 * @param row the row with the value per attribute in the same order
 * like the attribute names
 * @param classValue the class value to compute the probability for
 * @param laplaceCorrector the Laplace corrector to use. A value greater 0
 * overcomes zero counts
 * @return the probability of this row to belong to the given class value
 */
private double getClassProbability(final String[] attrNames, final DataRow row, final String classValue, final double laplaceCorrector) {
    double combinedProbability = getClassPriorProbability(classValue);
    for (int i = 0, length = row.getNumCells(); i < length; i++) {
        final String attrName = attrNames[i];
        final AttributeModel model = m_modelByAttrName.get(attrName);
        if (model == null) {
            // skip unknown attributes
            continue;
        }
        if (model instanceof ClassAttributeModel) {
            // skip the class value column
            continue;
        }
        final DataCell cell = row.getCell(i);
        final Double probability = model.getProbability(classValue, cell, laplaceCorrector);
        if (probability != null) {
            combinedProbability *= probability.doubleValue();
        }
    }
    return combinedProbability;
}
Also used : DataCell(org.knime.core.data.DataCell)

Example 70 with DataCell

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

the class DecTreePredictorNodeModel method getPredictionValues.

private LinkedList<DataCell> getPredictionValues(final PMMLPortObjectSpec treeSpec) {
    String targetCol = treeSpec.getTargetFields().get(0);
    DataColumnSpec colSpec = treeSpec.getDataTableSpec().getColumnSpec(targetCol);
    if (colSpec.getDomain().hasValues()) {
        LinkedList<DataCell> predValues = new LinkedList<DataCell>();
        predValues.addAll(colSpec.getDomain().getValues());
        return predValues;
    } else {
        return null;
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCell(org.knime.core.data.DataCell) LinkedList(java.util.LinkedList)

Aggregations

DataCell (org.knime.core.data.DataCell)780 DataRow (org.knime.core.data.DataRow)268 DataTableSpec (org.knime.core.data.DataTableSpec)175 DataColumnSpec (org.knime.core.data.DataColumnSpec)170 DefaultRow (org.knime.core.data.def.DefaultRow)169 ArrayList (java.util.ArrayList)141 StringCell (org.knime.core.data.def.StringCell)131 DoubleCell (org.knime.core.data.def.DoubleCell)129 DoubleValue (org.knime.core.data.DoubleValue)111 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)109 DataType (org.knime.core.data.DataType)97 RowKey (org.knime.core.data.RowKey)94 BufferedDataTable (org.knime.core.node.BufferedDataTable)93 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)91 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)84 LinkedHashMap (java.util.LinkedHashMap)81 IntCell (org.knime.core.data.def.IntCell)79 HashMap (java.util.HashMap)60 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)57 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)56