use of org.knime.core.data.def.DefaultRow 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.DefaultRow in project knime-core by knime.
the class AddEmptyRowsNodeModel method createNewRowsTable.
private BufferedDataTable createNewRowsTable(final DataTableSpec inSpec, final long rowCount, final ExecutionContext subExec) throws CanceledExecutionException {
DataCell[] cells = new DataCell[inSpec.getNumColumns()];
for (int c = 0; c < cells.length; c++) {
DataType type = inSpec.getColumnSpec(c).getType();
if (type.isASuperTypeOf(DoubleCell.TYPE)) {
if (m_config.isUseMissingDouble()) {
cells[c] = DataType.getMissingCell();
} else {
cells[c] = new DoubleCell(m_config.getFillValueDouble());
}
} else if (type.isASuperTypeOf(IntCell.TYPE)) {
if (m_config.isUseMissingInt()) {
cells[c] = DataType.getMissingCell();
} else {
cells[c] = new IntCell(m_config.getFillValueInt());
}
} else if (type.isASuperTypeOf(StringCell.TYPE)) {
if (m_config.isUseMissingString()) {
cells[c] = DataType.getMissingCell();
} else {
cells[c] = new StringCell(m_config.getFillValueString());
}
} else {
cells[c] = DataType.getMissingCell();
}
}
BufferedDataContainer cont = subExec.createDataContainer(inSpec);
for (long i = 0; i < rowCount; i++) {
RowKey key = new RowKey(m_config.getNewRowKeyPrefix() + i);
subExec.setProgress(i / (double) rowCount, "Creating row \"" + key + "\", " + i + "/" + rowCount);
subExec.checkCanceled();
cont.addRowToTable(new DefaultRow(key, cells));
}
cont.close();
return cont.getTable();
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class AggregateOutputNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
// retrieve variables from the stack which the head of this
// loop hopefully put there:
int count;
int maxCount;
try {
count = peekFlowVariableInt("currentIteration");
maxCount = peekFlowVariableInt("maxIterations");
} catch (NoSuchElementException e) {
throw new Exception("No matching Loop Start node!", e);
}
if (count < 0 || count >= maxCount) {
throw new Exception("Conflicting loop variables, count is " + count + " and max count is " + maxCount);
}
final BufferedDataTable in = inData[0];
final DataTableSpec inSpec = in.getDataTableSpec();
if (count == 0) {
m_firstIterationSpec = in.getDataTableSpec();
m_predictionTable = exec.createDataContainer(createPredictionSpec(in.getDataTableSpec()));
} else if (m_predictionTable == null) {
throw new Exception("Loop Head claims this is NOT the first iteration" + " but the tail believes it is?!");
} else {
if (!inSpec.equalStructure(m_firstIterationSpec)) {
StringBuilder error = new StringBuilder("Input table's structure differs from reference " + "(first iteration) table: ");
if (inSpec.getNumColumns() != m_firstIterationSpec.getNumColumns()) {
error.append("different column counts ");
error.append(inSpec.getNumColumns());
error.append(" vs. ").append(m_firstIterationSpec.getNumColumns());
} else {
for (int i = 0; i < inSpec.getNumColumns(); i++) {
DataColumnSpec inCol = inSpec.getColumnSpec(i);
DataColumnSpec predCol = m_firstIterationSpec.getColumnSpec(i);
if (!inCol.equalStructure(predCol)) {
error.append("Column ").append(i).append(" [");
error.append(inCol).append("] vs. [");
error.append(predCol).append("]");
}
}
}
throw new IllegalArgumentException(error.toString());
}
}
final int rowCount = in.getRowCount();
final int targetColIndex = in.getDataTableSpec().findColumnIndex(m_settings.targetColumn());
final int predictColIndex = in.getDataTableSpec().findColumnIndex(m_settings.predictionColumn());
final boolean numericMode = in.getDataTableSpec().getColumnSpec(predictColIndex).getType().isCompatible(DoubleValue.class);
ExecutionMonitor subExec = exec.createSubProgress(count == maxCount - 1 ? 0.9 : 1);
final DataCell foldNumber = new IntCell(m_foldStatistics.size());
if (numericMode) {
double errorSum = 0;
int r = 0;
for (DataRow row : in) {
RowKey key = row.getKey();
DoubleValue target = (DoubleValue) row.getCell(targetColIndex);
DoubleValue predict = (DoubleValue) row.getCell(predictColIndex);
double d = (target.getDoubleValue() - predict.getDoubleValue());
errorSum += d * d;
r++;
if (m_settings.addFoldId()) {
m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
} else {
m_predictionTable.addRowToTable(row);
}
subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
subExec.checkCanceled();
}
DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(errorSum), new DoubleCell(errorSum / rowCount), new IntCell(rowCount));
m_foldStatistics.add(stats);
} else {
int incorrect = 0;
int r = 0;
for (DataRow row : in) {
RowKey key = row.getKey();
DataCell target = row.getCell(targetColIndex);
DataCell predict = row.getCell(predictColIndex);
if (!target.equals(predict)) {
incorrect++;
}
r++;
if (m_settings.addFoldId()) {
m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
} else {
m_predictionTable.addRowToTable(row);
}
subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
subExec.checkCanceled();
}
DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(100.0 * incorrect / rowCount), new IntCell(rowCount), new IntCell(incorrect));
m_foldStatistics.add(stats);
}
if (count < maxCount - 1) {
continueLoop();
return new BufferedDataTable[2];
} else {
BufferedDataContainer cont = exec.createDataContainer(numericMode ? NUMERIC_STATISTICS_SPEC : NOMINAL_STATISTICS_SPEC);
for (DataRow row : m_foldStatistics) {
cont.addRowToTable(row);
}
cont.close();
m_predictionTable.close();
return new BufferedDataTable[] { m_predictionTable.getTable(), cont.getTable() };
}
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class ClassAttributeModel method createDataRows.
/**
* {@inheritDoc}
*/
@Override
void createDataRows(final ExecutionMonitor exec, final BufferedDataContainer dc, final boolean ignoreMissing, final AtomicInteger rowId) throws CanceledExecutionException {
final List<String> sortedClassVal = AttributeModel.sortCollection(m_recsCounterByClassVal.keySet());
if (sortedClassVal == null) {
return;
}
final StringCell attributeNameCell = new StringCell(getAttributeName());
for (final String classVal : sortedClassVal) {
final StringCell classCell = new StringCell(classVal);
final List<DataCell> cells = new LinkedList<>();
cells.add(attributeNameCell);
cells.add(DataType.getMissingCell());
cells.add(classCell);
cells.add(new IntCell(getNoOfRecs4ClassValue(classVal)));
if (!ignoreMissing) {
cells.add(new IntCell(getNoOfMissingVals()));
}
cells.add(DataType.getMissingCell());
cells.add(DataType.getMissingCell());
dc.addRowToTable(new DefaultRow(RowKey.createRowKey(rowId.getAndIncrement()), cells.toArray(new DataCell[0])));
}
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class NominalAttributeModel method createDataRows.
/**
* {@inheritDoc}
*/
@Override
void createDataRows(final ExecutionMonitor exec, final BufferedDataContainer dc, final boolean ignoreMissing, final AtomicInteger rowId) throws CanceledExecutionException {
final List<String> sortedClassVal = AttributeModel.sortCollection(m_classValues.keySet());
if (sortedClassVal == null) {
return;
}
final List<String> sortedAttrValues = AttributeModel.sortCollection(m_attributeVals);
final StringCell attributeNameCell = new StringCell(getAttributeName());
for (final String attrVal : sortedAttrValues) {
final StringCell attributeValueCell = new StringCell(attrVal);
for (final String classVal : sortedClassVal) {
final StringCell classCell = new StringCell(classVal);
final NominalClassValue classValue = m_classValues.get(classVal);
final List<DataCell> cells = new LinkedList<>();
cells.add(attributeNameCell);
cells.add(attributeValueCell);
cells.add(classCell);
cells.add(new IntCell(classValue.getNoOfRows4AttributeValue(attrVal)));
if (!ignoreMissing) {
cells.add(new IntCell(classValue.getNoOfMissingValueRecs()));
}
cells.add(DataType.getMissingCell());
cells.add(DataType.getMissingCell());
dc.addRowToTable(new DefaultRow(RowKey.createRowKey(rowId.getAndIncrement()), cells.toArray(new DataCell[0])));
}
}
}
Aggregations