use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class Many2OneColPMMLNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec spec, final AbstractMany2OneCellFactory cellFactory) {
ColumnRearranger rearranger = new ColumnRearranger(spec);
if (!m_keepColumns.getBooleanValue()) {
rearranger.remove(cellFactory.getIncludedColIndices());
}
rearranger.append(cellFactory);
return rearranger;
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class CollectionSplitNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable table = inData[0];
DataTableSpec spec = table.getDataTableSpec();
ExecutionMonitor execForCR = exec;
// validate settings
getTargetColIndex(spec);
DataColumnSpec[] colSpecs;
switch(m_settings.getCountElementsPolicy()) {
case Count:
execForCR = exec.createSubProgress(0.7);
ExecutionMonitor e = exec.createSubProgress(0.3);
colSpecs = countNewColumns(table, e);
break;
case UseElementNamesOrFail:
colSpecs = getColSpecsByElementNames(spec);
break;
case BestEffort:
try {
colSpecs = getColSpecsByElementNames(spec);
} catch (InvalidSettingsException ise) {
execForCR = exec.createSubProgress(0.7);
e = exec.createSubProgress(0.3);
colSpecs = countNewColumns(table, e);
}
break;
default:
throw new InvalidSettingsException("Unsupported policy: " + m_settings.getCountElementsPolicy());
}
Pair<ColumnRearranger, SplitCellFactory> pair = createColumnRearranger(spec, colSpecs);
BufferedDataTable out = exec.createColumnRearrangeTable(table, pair.getFirst(), execForCR);
String warnMessage = pair.getSecond().getWarnMessage();
if (warnMessage != null) {
setWarningMessage(warnMessage);
}
if (m_settings.isDetermineMostSpecificDataType()) {
out = refineTypes(out, pair.getSecond(), exec);
}
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class RuleEngineNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules) throws InvalidSettingsException {
ColumnRearranger crea = new ColumnRearranger(inSpec);
String newColName = DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName());
final int defaultLabelColumnIndex;
if (m_settings.getDefaultLabelIsColumn()) {
if (m_settings.getDefaultLabel().length() < 3) {
throw new InvalidSettingsException("Default label is not a column reference");
}
if (!m_settings.getDefaultLabel().startsWith("$") || !m_settings.getDefaultLabel().endsWith("$")) {
throw new InvalidSettingsException("Column references in default label must be enclosed in $");
}
String colRef = m_settings.getDefaultLabel().substring(1, m_settings.getDefaultLabel().length() - 1);
defaultLabelColumnIndex = inSpec.findColumnIndex(colRef);
if (defaultLabelColumnIndex == -1) {
throw new InvalidSettingsException("Column '" + m_settings.getDefaultLabel() + "' for default label does not exist in input table");
}
} else {
defaultLabelColumnIndex = -1;
}
// determine output type
List<DataType> types = new ArrayList<DataType>();
// add outcome column types
for (Rule r : rules) {
if (r.getOutcome() instanceof ColumnReference) {
types.add(((ColumnReference) r.getOutcome()).spec.getType());
} else if (r.getOutcome() instanceof Double) {
types.add(DoubleCell.TYPE);
} else if (r.getOutcome() instanceof Integer) {
types.add(IntCell.TYPE);
} else if (r.getOutcome().toString().length() > 0) {
types.add(StringCell.TYPE);
}
}
if (defaultLabelColumnIndex >= 0) {
types.add(inSpec.getColumnSpec(defaultLabelColumnIndex).getType());
} else if (m_settings.getDefaultLabel().length() > 0) {
try {
Integer.parseInt(m_settings.getDefaultLabel());
types.add(IntCell.TYPE);
} catch (NumberFormatException ex) {
try {
Double.parseDouble(m_settings.getDefaultLabel());
types.add(DoubleCell.TYPE);
} catch (NumberFormatException ex1) {
types.add(StringCell.TYPE);
}
}
}
final DataType outType;
if (types.size() > 0) {
DataType temp = types.get(0);
for (int i = 1; i < types.size(); i++) {
temp = DataType.getCommonSuperType(temp, types.get(i));
}
if ((temp.getValueClasses().size() == 1) && temp.getValueClasses().contains(DataValue.class)) {
// a non-native type, we replace it with string
temp = StringCell.TYPE;
}
outType = temp;
} else {
outType = StringCell.TYPE;
}
DataColumnSpec cs = new DataColumnSpecCreator(newColName, outType).createSpec();
crea.append(new SingleCellFactory(cs) {
@Override
public DataCell getCell(final DataRow row) {
for (Rule r : rules) {
if (r.matches(row)) {
Object outcome = r.getOutcome();
if (outcome instanceof ColumnReference) {
DataCell cell = row.getCell(((ColumnReference) outcome).index);
if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
return new StringCell(cell.toString());
} else {
return cell;
}
} else if (outType.equals(IntCell.TYPE)) {
return new IntCell((Integer) outcome);
} else if (outType.equals(DoubleCell.TYPE)) {
return new DoubleCell((Double) outcome);
} else {
return new StringCell(outcome.toString());
}
}
}
if (defaultLabelColumnIndex >= 0) {
DataCell cell = row.getCell(defaultLabelColumnIndex);
if (outType.equals(StringCell.TYPE) && !cell.getType().equals(StringCell.TYPE)) {
return new StringCell(cell.toString());
} else {
return cell;
}
} else if (m_settings.getDefaultLabel().length() > 0) {
String l = m_settings.getDefaultLabel();
if (outType.equals(StringCell.TYPE)) {
return new StringCell(l);
}
try {
int i = Integer.parseInt(l);
return new IntCell(i);
} catch (NumberFormatException ex) {
try {
double d = Double.parseDouble(l);
return new DoubleCell(d);
} catch (NumberFormatException ex1) {
return new StringCell(l);
}
}
} else {
return DataType.getMissingCell();
}
}
});
return crea;
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class RuleEngineNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
List<Rule> rules = parseRules(inData[0].getDataTableSpec());
ColumnRearranger crea = createRearranger(inData[0].getDataTableSpec(), rules);
return new BufferedDataTable[] { exec.createColumnRearrangeTable(inData[0], crea, exec) };
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class GeneralRegressionPredictorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
PMMLPortObjectSpec regModelSpec = (PMMLPortObjectSpec) inSpecs[0];
DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
if (dataSpec == null || regModelSpec == null) {
throw new InvalidSettingsException("No input specification available");
}
RegressionPredictorSettings s = createRegressionPredictorSettings(regModelSpec, dataSpec);
if (null != RegressionPredictorCellFactory.createColumnSpec(regModelSpec, dataSpec, s)) {
ColumnRearranger c = new ColumnRearranger(dataSpec);
c.append(new RegressionPredictorCellFactory(regModelSpec, dataSpec, s) {
@Override
public DataCell[] getCells(final DataRow row) {
// not called during configure.
return null;
}
});
DataTableSpec outSpec = c.createSpec();
return new DataTableSpec[] { outSpec };
} else {
return null;
}
}
Aggregations