use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class DBApplyBinnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected final PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
PMMLPortObject pmmlPortObject = (PMMLPortObject) inData[0];
DatabasePortObject databasePortObject = (DatabasePortObject) inData[1];
DatabaseQueryConnectionSettings connectionSettings = databasePortObject.getConnectionSettings(getCredentialsProvider());
return new PortObject[] { createDatabasePortObject(databasePortObject.getSpec(), connectionSettings, pmmlPortObject) };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class DBNumericBinnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected final PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
exec.setMessage("Retrieving metadata from database");
checkDuplicateBinNames();
final DatabasePortObject inDatabasePortObject = (DatabasePortObject) inData[0];
final DatabasePortObjectSpec inDatabasePortObjectSpec = inDatabasePortObject.getSpec();
DatabaseQueryConnectionSettings connectionSettings = inDatabasePortObject.getConnectionSettings(getCredentialsProvider());
DataTableSpec outDataTableSpec = DBAutoBinner.createNewDataTableSpec(inDatabasePortObjectSpec.getDataTableSpec(), m_columnToAppended);
PMMLPortObject outPMMLPortObject = createPMMLPortObject(inDatabasePortObjectSpec.getDataTableSpec(), outDataTableSpec);
DBBinnerMaps binnerMaps = DBAutoBinner.intoBinnerMaps(outPMMLPortObject, inDatabasePortObjectSpec.getDataTableSpec());
DatabasePortObjectSpec outDatabasePortObjectSpec = createDatabasePortObjectSpec(connectionSettings, inDatabasePortObjectSpec, binnerMaps);
return new PortObject[] { new DatabasePortObject(outDatabasePortObjectSpec), outPMMLPortObject };
}
use of org.knime.core.node.port.pmml.PMMLPortObject 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.PMMLPortObject 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.PMMLPortObject in project knime-core by knime.
the class DBAutoBinner method translate.
/**
* This method translates a {@link PMMLPreprocDiscretize} object into {@link PMMLPortObject}.
*
* @param pmmlDiscretize {@link PMMLPreprocDiscretize} object
* @param dataTableSpec {@link DataTableSpec} if incoming {@link BufferedDataTable}
* @return a {@link PMMLPortObject} containing required parameters for binning operation
*/
public static PMMLPortObject translate(final PMMLPreprocDiscretize pmmlDiscretize, final DataTableSpec dataTableSpec) {
final Map<String, Bin[]> columnToBins = new HashMap<>();
final Map<String, String> columnToAppend = new HashMap<>();
List<String> replacedColumnNames = pmmlDiscretize.getConfiguration().getNames();
for (String replacedColumnName : replacedColumnNames) {
PMMLDiscretize discretize = pmmlDiscretize.getConfiguration().getDiscretize(replacedColumnName);
List<PMMLDiscretizeBin> bins = discretize.getBins();
String originalColumnName = discretize.getField();
if (replacedColumnName.equals(originalColumnName)) {
// wenn replaced, dann nicht anhängen
columnToAppend.put(originalColumnName, null);
} else {
// nicht replaced -> anhängen
columnToAppend.put(originalColumnName, replacedColumnName);
}
NumericBin[] numericBin = new NumericBin[bins.size()];
int counter = 0;
for (PMMLDiscretizeBin bin : bins) {
String binName = bin.getBinValue();
List<PMMLInterval> intervals = bin.getIntervals();
boolean leftOpen = false;
boolean rightOpen = false;
double leftMargin = 0;
double rightMargin = 0;
// always returns only one interval
for (PMMLInterval interval : intervals) {
Closure closure = interval.getClosure();
switch(closure) {
case openClosed:
leftOpen = true;
rightOpen = false;
break;
case openOpen:
leftOpen = true;
rightOpen = true;
break;
case closedOpen:
leftOpen = false;
rightOpen = true;
case closedClosed:
leftOpen = false;
rightOpen = false;
break;
default:
leftOpen = true;
rightOpen = false;
break;
}
leftMargin = interval.getLeftMargin();
rightMargin = interval.getRightMargin();
}
numericBin[counter] = new NumericBin(binName, leftOpen, leftMargin, rightOpen, rightMargin);
counter++;
}
columnToBins.put(originalColumnName, numericBin);
}
// ColumnRearranger createColReg = createColReg(dataTableSpec, columnToBins, columnToAppended);
DataTableSpec newDataTableSpec = createNewDataTableSpec(dataTableSpec, columnToAppend);
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(newDataTableSpec);
PMMLPortObject pmmlPortObject = new PMMLPortObject(pmmlSpecCreator.createSpec(), null, newDataTableSpec);
PMMLBinningTranslator trans = new PMMLBinningTranslator(columnToBins, columnToAppend, new DerivedFieldMapper(pmmlPortObject));
TransformationDictionary exportToTransDict = trans.exportToTransDict();
pmmlPortObject.addGlobalTransformations(exportToTransDict);
return pmmlPortObject;
}
Aggregations