use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class RegressionPredictorNodeModel 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");
}
for (String learnColName : regModelSpec.getLearningFields()) {
if (!dataSpec.containsName(learnColName)) {
throw new InvalidSettingsException("Learning column \"" + learnColName + "\" not found in input data to be predicted");
}
}
ColumnRearranger rearranger = createRearranger(dataSpec, regModelSpec, null);
DataTableSpec outSpec = rearranger.createSpec();
return new DataTableSpec[] { outSpec };
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class RegressionPredictorNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
PMMLPortObject regModel = (PMMLPortObject) inData[0];
List<Node> models = regModel.getPMMLValue().getModels(PMMLModelType.RegressionModel);
if (models.isEmpty()) {
String msg = "No Regression Model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLRegressionTranslator trans = new PMMLRegressionTranslator();
regModel.initializeModelTranslator(trans);
BufferedDataTable data = (BufferedDataTable) inData[1];
DataTableSpec spec = data.getDataTableSpec();
ColumnRearranger c = createRearranger(spec, regModel.getSpec(), trans);
BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class RegressionPredictorNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec inSpec, final PMMLPortObjectSpec regModelSpec, final PMMLRegressionTranslator regModel) throws InvalidSettingsException {
if (regModelSpec == null) {
throw new InvalidSettingsException("No input");
}
// exclude last (response column)
String targetCol = "Response";
for (String s : regModelSpec.getTargetFields()) {
targetCol = s;
break;
}
final List<String> learnFields;
if (regModel != null) {
RegressionTable regTable = regModel.getRegressionTable();
learnFields = new ArrayList<String>();
for (NumericPredictor p : regTable.getVariables()) {
learnFields.add(p.getName());
}
} else {
learnFields = new ArrayList<String>(regModelSpec.getLearningFields());
}
final int[] colIndices = new int[learnFields.size()];
int k = 0;
for (String learnCol : learnFields) {
int index = inSpec.findColumnIndex(learnCol);
if (index < 0) {
throw new InvalidSettingsException("Missing column for " + "regressor variable : \"" + learnCol + "\"");
}
DataColumnSpec regressor = inSpec.getColumnSpec(index);
String name = regressor.getName();
DataColumnSpec col = inSpec.getColumnSpec(index);
if (!col.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("Incompatible type of " + "column \"" + name + "\": " + col.getType());
}
colIndices[k++] = index;
}
// try to use some smart naming scheme for the append column
String oldName = targetCol;
if (inSpec.containsName(oldName) && !oldName.toLowerCase().endsWith("(prediction)")) {
oldName = oldName + " (prediction)";
}
String newColName = DataTableSpec.getUniqueColumnName(inSpec, oldName);
DataColumnSpec newCol = new DataColumnSpecCreator(newColName, DoubleCell.TYPE).createSpec();
SingleCellFactory fac = new SingleCellFactory(newCol) {
@Override
public DataCell getCell(final DataRow row) {
RegressionTable t = regModel.getRegressionTable();
int j = 0;
double result = t.getIntercept();
for (NumericPredictor p : t.getVariables()) {
DataCell c = row.getCell(colIndices[j++]);
if (c.isMissing()) {
return DataType.getMissingCell();
}
double v = ((DoubleValue) c).getDoubleValue();
if (p.getExponent() != 1) {
v = Math.pow(v, p.getExponent());
}
result += p.getCoefficient() * v;
}
return new DoubleCell(result);
}
};
ColumnRearranger c = new ColumnRearranger(inSpec);
c.append(fac);
return c;
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class OptionsPanel method getMissingColSpecName.
@SuppressWarnings("null")
private static String getMissingColSpecName(final DataTableSpec spec, final String[] includedNames, final String[] excludedNames) {
ColumnRearranger r = new ColumnRearranger(spec);
// remove columns we know from the include list
for (String colName : includedNames) {
if (spec.containsName(colName)) {
r.remove(colName);
}
}
// remove columns we know from the exclude list
for (String colName : excludedNames) {
if (spec.containsName(colName)) {
r.remove(colName);
}
}
DataTableSpec tableSpecWithMissing = r.createSpec();
DataColumnSpec formerTargetSpec = null;
// were either in the include or exclude list
for (DataColumnSpec colSpec : tableSpecWithMissing) {
DataType colType = colSpec.getType();
if (colType.isCompatible(NominalValue.class) || colType.isCompatible(DoubleValue.class)) {
formerTargetSpec = colSpec;
break;
}
}
assert formerTargetSpec != null : "The former target spec is no longer part of the table, please check.";
return formerTargetSpec.getName();
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class SimpleStreamableFunctionNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec in = inSpecs[0];
ColumnRearranger r = createColumnRearranger(in);
DataTableSpec out = r.createSpec();
return new DataTableSpec[] { out };
}
Aggregations