use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class PMMLImport method init.
/**
* @param pmmlDoc
*/
private void init(final PMMLDocument pmmlDoc) {
PMMLDataDictionaryTranslator dictTrans = new PMMLDataDictionaryTranslator();
dictTrans.initializeFrom(pmmlDoc);
DataTableSpec tableSpec = dictTrans.getDataTableSpec();
List<String> activeDerivedFields = dictTrans.getActiveDerivedFields();
PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(tableSpec);
PMMLMiningSchemaTranslator miningTrans = new PMMLMiningSchemaTranslator();
miningTrans.initializeFrom(pmmlDoc);
Set<String> activeFields = new LinkedHashSet<String>();
List<String> miningFields = miningTrans.getActiveFields();
/* If we have a model all active fields of the data dictionary
* are passed through the mining schema. */
activeFields.addAll(miningFields);
activeFields.addAll(activeDerivedFields);
specCreator.setLearningColsNames(new LinkedList<String>(activeFields));
specCreator.addPreprocColNames(activeDerivedFields);
specCreator.setTargetColsNames(miningTrans.getTargetFields());
PMMLPortObjectSpec portObjectSpec = specCreator.createSpec();
m_portObject = new PMMLPortObject(portObjectSpec, pmmlDoc);
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class PMMLReaderNodeModel method createPMMLOutSpec.
/**
* @param inModelSpec the spec of the optional PMML in port
* @param parsedSpec the spec of the parsed PMML document
* @return the merged {@link PMMLPortObjectSpec}
*/
private PMMLPortObjectSpec createPMMLOutSpec(final PMMLPortObjectSpec inModelSpec, final PMMLPortObjectSpec parsedSpec) throws InvalidSettingsException {
PMMLPortObjectSpec outSpec = parsedSpec;
if (inModelSpec != null) {
List<String> preprocCols = inModelSpec.getPreprocessingFields();
for (String colName : preprocCols) {
if (!parsedSpec.getActiveFields().contains(colName)) {
throw new InvalidSettingsException("Preprocessing column " + colName + " is not contained in the read PMML file.");
}
}
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(parsedSpec);
creator.addPreprocColNames(preprocCols);
outSpec = creator.createSpec();
}
return outSpec;
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class DBNumericBinnerNodeModel method createPMMLPortObject.
private PMMLPortObject createPMMLPortObject(final DataTableSpec inDataTableSpec, final DataTableSpec outDataTableSpec) {
PMMLPortObjectSpec initPMMLSpec = new PMMLPortObjectSpecCreator(outDataTableSpec).createSpec();
PMMLPortObject initPMMLPortObject = new PMMLPortObject(initPMMLSpec, null, outDataTableSpec);
PMMLBinningTranslator pmmlBinningTranslator = new PMMLBinningTranslator(m_columnToBins, m_columnToAppended, new DerivedFieldMapper(initPMMLPortObject));
PMMLPortObject outPMMLPortObject = new PMMLPortObject(initPMMLSpec, initPMMLPortObject, inDataTableSpec);
outPMMLPortObject.addGlobalTransformations(pmmlBinningTranslator.exportToTransDict());
return outPMMLPortObject;
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class DecisionTreeLearnerNodeModel method execute.
/**
* Start of decision tree induction.
*
* @param exec the execution context for this run
* @param data the input data to build the decision tree from
* @return an empty data table array, as just a model is provided
* @throws Exception any type of exception, e.g. for cancellation,
* invalid input,...
* @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
*/
@Override
protected PortObject[] execute(final PortObject[] data, final ExecutionContext exec) throws Exception {
// holds the warning message displayed after execution
StringBuilder warningMessageSb = new StringBuilder();
ParallelProcessing parallelProcessing = new ParallelProcessing(m_parallelProcessing.getIntValue());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Number available threads: " + parallelProcessing.getMaxNumberThreads() + " used threads: " + parallelProcessing.getCurrentThreadsInUse());
}
exec.setProgress("Preparing...");
// check input data
assert (data != null && data[DATA_INPORT] != null);
BufferedDataTable inData = (BufferedDataTable) data[DATA_INPORT];
// get column with color information
String colorColumn = null;
for (DataColumnSpec s : inData.getDataTableSpec()) {
if (s.getColorHandler() != null) {
colorColumn = s.getName();
break;
}
}
// the data table must have more than 2 records
if (inData.getRowCount() <= 1) {
throw new IllegalArgumentException("Input data table must have at least 2 records!");
}
// get class column index
int classColumnIndex = inData.getDataTableSpec().findColumnIndex(m_classifyColumn.getStringValue());
assert classColumnIndex > -1;
// create initial In-Memory table
exec.setProgress("Create initial In-Memory table...");
InMemoryTableCreator tableCreator = new InMemoryTableCreator(inData, classColumnIndex, m_minNumberRecordsPerNode.getIntValue(), m_skipColumns.getBooleanValue());
InMemoryTable initialTable = tableCreator.createInMemoryTable(exec.createSubExecutionContext(0.05));
int removedRows = tableCreator.getRemovedRowsDueToMissingClassValue();
if (removedRows == inData.getRowCount()) {
throw new IllegalArgumentException("Class column contains only " + "missing values");
}
if (removedRows > 0) {
warningMessageSb.append(removedRows);
warningMessageSb.append(" rows removed due to missing class value;");
}
// the all over row count is used to report progress
m_alloverRowCount = initialTable.getSumOfWeights();
// set the finishing counter
// this counter will always be incremented when a leaf node is
// created, as this determines the recursion end and can thus
// be used for progress indication
m_finishedCounter = new AtomicDouble(0);
// get the number of attributes
m_numberAttributes = initialTable.getNumAttributes();
// create the quality measure
final SplitQualityMeasure splitQualityMeasure;
if (m_splitQualityMeasureType.getStringValue().equals(SPLIT_QUALITY_GINI)) {
splitQualityMeasure = new SplitQualityGini();
} else {
splitQualityMeasure = new SplitQualityGainRatio();
}
// build the tree
// before this set the node counter to 0
m_counter.set(0);
exec.setMessage("Building tree...");
DecisionTreeNode root = null;
root = buildTree(initialTable, exec, 0, splitQualityMeasure, parallelProcessing);
boolean isBinaryNominal = m_binaryNominalSplitMode.getBooleanValue();
boolean isFilterInvalidAttributeValues = m_filterNominalValuesFromParent.getBooleanValue();
if (isBinaryNominal && isFilterInvalidAttributeValues) {
// traverse tree nodes and remove from the children the attribute
// values that were filtered out further up in the tree. "Bug" 3124
root.filterIllegalAttributes(Collections.EMPTY_MAP);
}
// the decision tree model saved as PMML at the second out-port
DecisionTree decisionTree = new DecisionTree(root, m_classifyColumn.getStringValue(), /* strategy has to be set explicitly as the default in PMML is
none, which means rows with missing values are not
classified. */
PMMLMissingValueStrategy.LAST_PREDICTION);
decisionTree.setColorColumn(colorColumn);
// prune the tree
exec.setMessage("Prune tree with " + m_pruningMethod.getStringValue() + "...");
pruneTree(decisionTree);
// add highlight patterns and color information
exec.setMessage("Adding hilite and color info to tree...");
addHiliteAndColorInfo(inData, decisionTree);
LOGGER.info("Decision tree consisting of " + decisionTree.getNumberNodes() + " nodes created with pruning method " + m_pruningMethod.getStringValue());
// set the warning message if available
if (warningMessageSb.length() > 0) {
setWarningMessage(warningMessageSb.toString());
}
// reset the number available threads
parallelProcessing.reset();
parallelProcessing = null;
// no data out table is created -> return an empty table array
exec.setMessage("Creating PMML decision tree model...");
// handle the optional PMML input
PMMLPortObject inPMMLPort = (PMMLPortObject) data[1];
DataTableSpec inSpec = inData.getSpec();
PMMLPortObjectSpec outPortSpec = createPMMLPortObjectSpec(inPMMLPort == null ? null : inPMMLPort.getSpec(), inSpec);
PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, inData.getSpec());
outPMMLPort.addModelTranslater(new PMMLDecisionTreeTranslator(decisionTree));
m_decisionTree = decisionTree;
return new PortObject[] { outPMMLPort };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class One2ManyColPMMLNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec inDataSpec = (DataTableSpec) inSpecs[0];
m_appendOrgColName = false;
if (m_includedColumns.getIncludeList() == null || m_includedColumns.getIncludeList().size() <= 0) {
setWarningMessage("No columns to transfrom selected. Will have no effect!");
}
// check if the values are present in the current spec
if (m_includedColumns.getIncludeList() != null && m_includedColumns.getIncludeList().size() > 0) {
checkColumnsSpecs(inDataSpec);
}
CellFactory cellFactory = new One2ManyCellFactory(inDataSpec, m_includedColumns.getIncludeList(), m_appendOrgColName);
ColumnRearranger rearranger = createRearranger(inDataSpec, cellFactory);
if (m_pmmlEnabled) {
PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inDataSpec);
return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
} else {
return new PortObjectSpec[] { rearranger.createSpec() };
}
}
Aggregations