use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class TimeMissValueNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final Map<String, TSMissVHandler> nameToMVHandler, final Map<String, Integer> nameToInt, final DataTableSpec spec) {
ColumnRearranger result = new ColumnRearranger(spec);
for (String thisCol : nameToMVHandler.keySet()) {
final TSMissVHandler mvHandler = nameToMVHandler.get(thisCol);
final int colIndex = spec.findColumnIndex(thisCol);
DataColumnSpec newColSpec = new DataColumnSpecCreator(thisCol, spec.getColumnSpec(thisCol).getType()).createSpec();
SingleCellFactory c = new SingleCellFactory(newColSpec) {
@Override
public DataCell getCell(final DataRow row) {
if (row.getCell(colIndex).isMissing()) {
return mvHandler.getandRemove(row.getKey());
}
return row.getCell(colIndex);
}
};
result.replace(c, nameToInt.get(thisCol));
}
return result;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class RuleEngine2PortsNodeModel method computeRearrangerWithPMML.
/**
* @param spec
* @param rules
* @param flowVars
* @param ruleIdx
* @param outcomeIdx
* @param confidenceIdx
* @param weightIdx
* @param validationIdx
* @param outputColumnName
* @return
* @throws InterruptedException
* @throws InvalidSettingsException
*/
private Pair<ColumnRearranger, PortObject> computeRearrangerWithPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final int confidenceIdx, final int weightIdx, final int validationIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
PortObject po;
ColumnRearranger ret;
PMMLDocument doc = PMMLDocument.Factory.newInstance();
final PMML pmmlObj = doc.addNewPMML();
RuleSetModel ruleSetModel = pmmlObj.addNewRuleSetModel();
RuleSet ruleSet = ruleSetModel.addNewRuleSet();
List<DataType> outcomeTypes = new ArrayList<>();
PMMLRuleParser parser = new PMMLRuleParser(spec, flowVars);
int lineNo = 0;
DataRow ruleRow;
while ((ruleRow = rules.poll()) != null) {
++lineNo;
DataCell rule = ruleRow.getCell(ruleIdx);
CheckUtils.checkSetting(!rule.isMissing(), "Missing rule in row: " + ruleRow.getKey());
if (rule instanceof StringValue) {
StringValue ruleText = (StringValue) rule;
String r = ruleText.getStringValue().replaceAll("[\r\n]+", " ");
if (RuleSupport.isComment(r)) {
continue;
}
if (outcomeIdx >= 0) {
r += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
}
ParseState state = new ParseState(r);
try {
PMMLPredicate condition = parser.parseBooleanExpression(state);
SimpleRule simpleRule = ruleSet.addNewSimpleRule();
setCondition(simpleRule, condition);
state.skipWS();
state.consumeText("=>");
state.skipWS();
Expression outcome = parser.parseOutcomeOperand(state, null);
simpleRule.setScore(outcome.toString());
if (confidenceIdx >= 0) {
DataCell confidenceCell = ruleRow.getCell(confidenceIdx);
if (!confidenceCell.isMissing()) {
if (confidenceCell instanceof DoubleValue) {
DoubleValue dv = (DoubleValue) confidenceCell;
double confidence = dv.getDoubleValue();
simpleRule.setConfidence(confidence);
}
}
}
if (weightIdx >= 0) {
DataCell weightCell = ruleRow.getCell(weightIdx);
boolean missing = true;
if (!weightCell.isMissing()) {
if (weightCell instanceof DoubleValue) {
DoubleValue dv = (DoubleValue) weightCell;
double weight = dv.getDoubleValue();
simpleRule.setWeight(weight);
missing = false;
}
}
if (missing && m_settings.isHasDefaultWeight()) {
simpleRule.setWeight(m_settings.getDefaultWeight());
}
}
CheckUtils.checkSetting(outcome.isConstant(), "Outcome is not constant in line " + lineNo + " (" + ruleRow.getKey() + ") for rule: " + rule);
outcomeTypes.add(outcome.getOutputType());
} catch (ParseException e) {
ParseException error = Util.addContext(e, r, lineNo);
throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
}
} else {
CheckUtils.checkSetting(false, "Wrong type (" + rule.getType() + ") of rule: " + rule + "\nin row: " + ruleRow.getKey());
}
}
ColumnRearranger dummy = new ColumnRearranger(spec);
if (!m_settings.isReplaceColumn()) {
dummy.append(new SingleCellFactory(new DataColumnSpecCreator(outputColumnName, RuleEngineNodeModel.computeOutputType(outcomeTypes, computeOutcomeType(rules.getDataTableSpec()), true, m_settings.isDisallowLongOutputForCompatibility())).createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
return null;
}
});
}
PMMLPortObject pmml = createPMMLPortObject(doc, ruleSetModel, ruleSet, parser, dummy.createSpec());
po = pmml;
m_copy = copy(pmml);
String predictionConfidenceColumn = m_settings.getPredictionConfidenceColumn();
if (predictionConfidenceColumn == null || predictionConfidenceColumn.isEmpty()) {
predictionConfidenceColumn = RuleEngine2PortsSettings.DEFAULT_PREDICTION_CONFIDENCE_COLUMN;
}
ret = PMMLRuleSetPredictorNodeModel.createRearranger(pmml, spec, m_settings.isReplaceColumn(), outputColumnName, m_settings.isComputeConfidence(), DataTableSpec.getUniqueColumnName(dummy.createSpec(), predictionConfidenceColumn), validationIdx);
return Pair.create(ret, po);
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class RowKeyUtil method createColumnRearranger.
/**
* Creates the {@link ColumnRearranger} that appends a new column with the
* values of the row id to a data table.
*
* @param inSpec the <code>DataTableSpec</code> of table were the column
* should be appended
* @param newColName the name of the added column
* @param type the <code>DataType</code> of the new column
* @return the {@link ColumnRearranger} to use
*/
public static ColumnRearranger createColumnRearranger(final DataTableSpec inSpec, final String newColName, final DataType type) {
final ColumnRearranger c = new ColumnRearranger(inSpec);
// column specification of the appended column
final DataColumnSpecCreator colSpecCreater = new DataColumnSpecCreator(newColName, type);
final DataColumnSpec newColSpec = colSpecCreater.createSpec();
// utility object that performs the calculation
final CellFactory factory = new SingleCellFactory(newColSpec) {
@Override
public DataCell getCell(final DataRow row) {
return new StringCell(row.getKey().getString());
}
};
c.append(factory);
return c;
}
Aggregations