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;
}
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);
}
}
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++;
}
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;
}
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;
}
}
Aggregations