use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class PMMLRuleEditorNodeModel method createPMMLPortObjectSpec.
/**
* Initializes the {@link PMMLPortObjectSpec} based on the model, input and the used column.
*
* @param modelSpec The preprocessing model, can be {@code null}.
* @param spec The input table spec.
* @param usedColumns The columns used by the rules.
* @return The {@link PMMLPortObjectSpec} filled with proper data for configuration.
*/
private PMMLPortObjectSpec createPMMLPortObjectSpec(final DataTableSpec spec, final List<String> usedColumns) {
// this assumes that the new column is always the last column in the spec; which is the case if
// #createRearranger uses ColumnRearranger.append.
String targetCol = m_settings.isAppendColumn() ? spec.getColumnSpec(spec.getNumColumns() - 1).getName() : m_settings.getReplaceColumn();
Set<String> set = new LinkedHashSet<>(usedColumns);
List<String> learnCols = new LinkedList<>();
for (int i = 0; i < spec.getNumColumns(); i++) {
DataColumnSpec columnSpec = spec.getColumnSpec(i);
String col = columnSpec.getName();
if (!col.equals(targetCol) && set.contains(col) && (columnSpec.getType().isCompatible(DoubleValue.class) || columnSpec.getType().isCompatible(NominalValue.class) && (/*!m_skipColumns.getBooleanValue() ||*/
columnSpec.getDomain().hasValues()))) {
learnCols.add(spec.getColumnSpec(i).getName());
}
}
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(spec);
pmmlSpecCreator.setLearningColsNames(learnCols);
pmmlSpecCreator.setTargetColName(targetCol);
return pmmlSpecCreator.createSpec();
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class CategoryToNumberNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
if (m_settings.getIncludedColumns().length == 0) {
// nothing to convert, let's return the input table.
setWarningMessage("No columns selected," + " returning input.");
}
BufferedDataTable inData = (BufferedDataTable) inObjects[0];
DataTableSpec inSpec = (DataTableSpec) inObjects[0].getSpec();
ColumnRearranger rearranger = createRearranger(inSpec);
BufferedDataTable outTable = exec.createColumnRearrangeTable(inData, rearranger, exec);
// the optional PMML in port (can be null)
PMMLPortObject inPMMLPort = (PMMLPortObject) inObjects[1];
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, rearranger.createSpec());
PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort);
for (CategoryToNumberCellFactory factory : m_factories) {
PMMLMapValuesTranslator trans = new PMMLMapValuesTranslator(factory.getConfig(), new DerivedFieldMapper(inPMMLPort));
outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
}
return new PortObject[] { outTable, outPMMLPort };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class CategoryToNumberNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec inSpec = (DataTableSpec) inSpecs[0];
List<String> inputCols = new ArrayList<String>();
for (DataColumnSpec column : inSpec) {
if (column.getType().isCompatible(StringValue.class)) {
inputCols.add(column.getName());
}
}
// Auto configuration when included columns are not set
if (null == m_settings.getIncludedColumns()) {
// when there is no column with nominal data
if (inputCols.isEmpty()) {
throw new InvalidSettingsException("No column in " + "the input compatible to \"StringValue\".");
} else {
// auto-configure
m_settings.setIncludedColumns(inputCols.toArray(new String[inputCols.size()]));
}
} else {
if (!m_settings.getIncludeAll()) {
if (m_settings.getIncludedColumns().length == 0) {
setWarningMessage("No columns selected.");
}
List<String> included = new ArrayList<String>();
included.addAll(Arrays.asList(m_settings.getIncludedColumns()));
included.removeAll(inputCols);
if (!included.isEmpty()) {
// output first missing column
throw new InvalidSettingsException("Missing column: " + included.get(0).toString());
}
} else {
if (inputCols.isEmpty()) {
throw new InvalidSettingsException("No column in " + "the input compatible to \"StringValue\".");
} else {
// automatically add all columns
m_settings.setIncludedColumns(inputCols.toArray(new String[inputCols.size()]));
}
}
}
ColumnRearranger rearranger = createRearranger(inSpec);
PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inSpec);
pmmlSpecCreator.addPreprocColNames(inputCols);
return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class Many2OneColPMMLNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec inDataSpec = (DataTableSpec) inSpecs[0];
if (m_includedColumns.getIncludeList().size() <= 0) {
setWarningMessage("No column selected. Node will have no effect!");
}
// if it is not a reg exp it must be double compatible
if (!m_includeMethod.getStringValue().equals(IncludeMethod.RegExpPattern.name())) {
for (String colName : m_includedColumns.getIncludeList()) {
if (!inDataSpec.getColumnSpec(colName).getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("For selected include method '" + m_includeMethod.getStringValue() + "' only double compatible values are allowed." + " Column '" + colName + "' is not.");
}
}
}
ColumnRearranger rearranger = createRearranger(inDataSpec, getCellFactory(inDataSpec));
if (m_pmmlEnabled) {
PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inDataSpec);
return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
} else {
return new DataTableSpec[] { rearranger.createSpec() };
}
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class Many2OneColPMMLNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws CanceledExecutionException, Exception {
BufferedDataTable inData = (BufferedDataTable) inObjects[0];
AbstractMany2OneCellFactory cellFactory = getCellFactory(inData.getDataTableSpec());
BufferedDataTable outData = exec.createColumnRearrangeTable(inData, createRearranger(inData.getDataTableSpec(), cellFactory), exec);
if (m_pmmlEnabled) {
if (IncludeMethod.valueOf(m_includeMethod.getStringValue()) == IncludeMethod.RegExpPattern) {
setWarningMessage("Regular Expressions are not supported in PMML. " + "The generated PMML document is invalid.");
}
// the optional PMML in port (can be null)
PMMLPortObject inPMMLPort = (PMMLPortObject) inObjects[1];
/*
PMMLOne2ManyTranslator trans = new PMMLOne2ManyTranslator(
cellFactory.getColumnMapping(),
new DerivedFieldMapper(inPMMLPort));
*/
int[] sourceColIndices = cellFactory.getIncludedColIndices();
String[] sourceColNames = new String[sourceColIndices.length];
for (int i = 0; i < sourceColIndices.length; i++) {
sourceColNames[i] = inData.getDataTableSpec().getColumnSpec(sourceColIndices[i]).getName();
}
PMMLMany2OneTranslator trans = new PMMLMany2OneTranslator(cellFactory.getAppendedColumnName(), sourceColNames, IncludeMethod.valueOf(m_includeMethod.getStringValue()));
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, outData.getDataTableSpec());
PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort);
outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
return new PortObject[] { outData, outPMMLPort };
} else {
return new PortObject[] { outData };
}
}
Aggregations