use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class StringReplacerNodeModel method createColumnRearranger.
/**
* Creates the column rearranger that computes the new cells.
*
* @param spec the spec of the input table
* @return a column rearranger
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final Pattern pattern = createPattern(m_settings);
DataColumnSpec colSpec;
if (m_settings.createNewColumn()) {
colSpec = new DataColumnSpecCreator(m_settings.newColumnName(), StringCell.TYPE).createSpec();
} else {
colSpec = new DataColumnSpecCreator(m_settings.columnName(), StringCell.TYPE).createSpec();
}
final String replacement;
if (m_settings.patternIsRegex()) {
replacement = m_settings.replacement();
} else {
replacement = m_settings.replacement().replaceAll("(\\$\\d+)", "\\\\$1");
}
final int index = spec.findColumnIndex(m_settings.columnName());
SingleCellFactory cf = new SingleCellFactory(colSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(index);
if (cell.isMissing()) {
return cell;
}
final String stringValue = ((StringValue) cell).getStringValue();
Matcher m = pattern.matcher(stringValue);
if (m_settings.replaceAllOccurrences()) {
return new StringCell(m.replaceAll(replacement));
} else if (m.matches()) {
if (".*".equals(pattern.pattern())) {
// therefore the replacement value is doubled
return new StringCell(replacement);
} else {
return new StringCell(m.replaceAll(replacement));
}
} else {
return new StringCell(stringValue);
}
}
};
ColumnRearranger crea = new ColumnRearranger(spec);
if (m_settings.createNewColumn()) {
if (spec.containsName(m_settings.newColumnName())) {
throw new InvalidSettingsException("Duplicate column name: " + m_settings.newColumnName());
}
crea.append(cf);
} else {
crea.replace(cf, m_settings.columnName());
}
return crea;
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class AdapterNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
if (getNrInPorts() == 0 && getNrOutPorts() == 1) {
// assume simple source node with one table output
BufferedDataContainer cnt = exec.createDataContainer(createDefaultOutputSpec());
cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(0), new DataCell[] { new StringCell("Cell-1.1"), new IntCell(12), new DoubleCell(1.3) }));
cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(1), new DataCell[] { new StringCell("Cell-2.1"), new IntCell(22), new DoubleCell(2.3) }));
cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(2), new DataCell[] { new StringCell("Cell-3.1"), new IntCell(32), new DoubleCell(3.3) }));
cnt.close();
return new BufferedDataTable[] { cnt.getTable() };
}
return inObjects;
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class NodeSettingsTest method testStringDataCell.
/**
* Test write/read of StringCells.
*
* @throws Exception Should not happen.
*/
@Test
public void testStringDataCell() throws Exception {
try {
m_settings.addDataCell(null, new StringCell("null"));
fail();
} catch (IllegalArgumentException iae) {
assertTrue(true);
}
String key = "nullDataCell";
m_settings.addDataCell(key, null);
assertTrue(m_settings.containsKey(key));
assertTrue(m_settings.getDataCell(key) == null);
DataCell nullCell = new StringCell("null");
assertTrue(m_settings.getDataCell(key, nullCell) == null);
key = "kDataCell";
m_settings.addDataCell(key, new StringCell("B"));
assertTrue(m_settings.containsKey(key));
assertTrue(m_settings.getDataCell(key).equals(new StringCell("B")));
assertTrue(m_settings.getDataCell(key, null).equals(new StringCell("B")));
key += "array";
m_settings.addDataCellArray(key, new DataCell[] { new StringCell("T"), new StringCell("P"), new StringCell("M") });
assertTrue(m_settings.containsKey(key));
DataCell[] a = m_settings.getDataCellArray(key);
assertTrue(a[0].equals(new StringCell("T")));
assertTrue(a[1].equals(new StringCell("P")));
assertTrue(a[2].equals(new StringCell("M")));
a = m_settings.getDataCellArray(key, new DataCell[0]);
assertTrue(a[0].equals(new StringCell("T")));
assertTrue(a[1].equals(new StringCell("P")));
assertTrue(a[2].equals(new StringCell("M")));
key = "kDataCell_array_0";
m_settings.addDataCellArray(key, new DataCell[0]);
assertTrue(m_settings.containsKey(key));
assertTrue(m_settings.getDataCellArray(key).length == 0);
assertTrue(m_settings.getDataCellArray(key, new DataCell[1]).length == 0);
key = "kDataCell-";
m_settings.addDataCellArray(key, (DataCell[]) null);
assertTrue(m_settings.containsKey(key));
assertTrue(m_settings.getDataCellArray(key) == null);
key = "unknownDataCell";
DataCell unknownCell = new FuzzyNumberCell(0.0, 1.0, 2.0);
m_settings.addDataCell(key, unknownCell);
assertTrue(m_settings.containsKey(key));
assertTrue(unknownCell.equals(m_settings.getDataCell(key)));
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class SubgroupMinerModel2 method createAssociationRulesOutput.
private BufferedDataTable createAssociationRulesOutput(final DataTableSpec inputSpec, final ExecutionContext exec, final AprioriAlgorithm apriori, final List<DataCell> nameMapping) {
DataTableSpec outSpec = createAssociationRulesSpec(inputSpec);
BufferedDataContainer ruleRows = exec.createDataContainer(outSpec);
assert nameMapping != null;
List<AssociationRule> associationRules = apriori.getAssociationRules(m_confidence.getDoubleValue());
// for every association rule
int rowKeyCounter = 0;
for (AssociationRule r : associationRules) {
// get the support
double support = r.getSupport();
// get the confidence
double confidence = r.getConfidence();
// get lift
double lift = r.getLift();
// get the antecedence (which is one item) -> cell
FrequentItemSet antecedent = r.getAntecedent();
// get the consequence
FrequentItemSet consequent = r.getConsequent();
DataCell[] allCells = new DataCell[6];
allCells[0] = new DoubleCell(support);
allCells[1] = new DoubleCell(confidence);
allCells[2] = new DoubleCell(lift);
// consequent is always only one item -> access with get(0) ok
if (nameMapping.size() > consequent.getItems().get(0)) {
allCells[3] = nameMapping.get(consequent.getItems().get(0));
} else {
allCells[3] = new StringCell("Item" + consequent.getItems().get(0));
}
allCells[4] = new StringCell("<---");
Set<DataCell> allcells = new HashSet<DataCell>();
for (int i = 0; i < antecedent.getItems().size() && i < m_maxItemSetLength.getIntValue() + 5; i++) {
if (nameMapping.size() > antecedent.getItems().get(i)) {
allcells.add(nameMapping.get(antecedent.getItems().get(i)));
} else {
allcells.add(new StringCell("Item" + antecedent.getItems().get(i)));
}
}
allCells[5] = CollectionCellFactory.createSetCell(allcells);
if (antecedent.getItems().size() > 0) {
DataRow row = new DefaultRow("rule" + (rowKeyCounter++), allCells);
ruleRows.addRowToTable(row);
}
}
ruleRows.close();
return ruleRows.getTable();
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class SVMPredictor method getCells.
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
ArrayList<Double> values = new ArrayList<Double>();
for (int i = 0; i < m_colindices.length; i++) {
if (row.getCell(m_colindices[i]).isMissing()) {
if (m_appendProbabilities) {
DataCell[] ret = new DataCell[1 + m_svms.length];
Arrays.fill(ret, new MissingCell("Missing value in input data."));
return ret;
}
return new DataCell[] { DataType.getMissingCell() };
}
DoubleValue dv = (DoubleValue) row.getCell(m_colindices[i]);
values.add(dv.getDoubleValue());
}
String classvalue = doPredict(values);
if (m_appendProbabilities) {
DataCell[] ret = new DataCell[m_svms.length + 1];
double[] probabilities = computeProbabilities(values);
assert ret.length == probabilities.length + 1 : ret.length + " vs. " + (probabilities.length + 1);
for (int i = ret.length - 1; i-- > 0; ) {
ret[i] = new DoubleCell(probabilities[i]);
}
ret[probabilities.length] = new StringCell(classvalue);
return ret;
}
return new DataCell[] { new StringCell(classvalue) };
}
Aggregations