use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DecTreePredictorNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inPorts, final ExecutionContext exec) throws CanceledExecutionException, Exception {
exec.setMessage("Decision Tree Predictor: Loading predictor...");
PMMLPortObject port = (PMMLPortObject) inPorts[INMODELPORT];
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.TreeModel);
if (models.isEmpty()) {
String msg = "Decision Tree evaluation failed: " + "No tree model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLDecisionTreeTranslator trans = new PMMLDecisionTreeTranslator();
port.initializeModelTranslator(trans);
DecisionTree decTree = trans.getDecisionTree();
decTree.resetColorInformation();
BufferedDataTable inData = (BufferedDataTable) inPorts[INDATAPORT];
// get column with color information
String colorColumn = null;
for (DataColumnSpec s : inData.getDataTableSpec()) {
if (s.getColorHandler() != null) {
colorColumn = s.getName();
break;
}
}
decTree.setColorColumn(colorColumn);
exec.setMessage("Decision Tree Predictor: start execution.");
PortObjectSpec[] inSpecs = new PortObjectSpec[] { inPorts[0].getSpec(), inPorts[1].getSpec() };
DataTableSpec outSpec = createOutTableSpec(inSpecs);
BufferedDataContainer outData = exec.createDataContainer(outSpec);
long coveredPattern = 0;
long nrPattern = 0;
long rowCount = 0;
final long numberRows = inData.size();
exec.setMessage("Classifying...");
List<String> predictionValues = getPredictionStrings((PMMLPortObjectSpec) inPorts[INMODELPORT].getSpec());
for (DataRow thisRow : inData) {
DataCell cl = null;
LinkedHashMap<String, Double> classDistrib = null;
try {
Pair<DataCell, LinkedHashMap<DataCell, Double>> pair = decTree.getWinnerAndClasscounts(thisRow, inData.getDataTableSpec());
cl = pair.getFirst();
LinkedHashMap<DataCell, Double> classCounts = pair.getSecond();
classDistrib = getDistribution(classCounts);
if (coveredPattern < m_maxNumCoveredPattern.getIntValue()) {
// remember this one for HiLite support
decTree.addCoveredPattern(thisRow, inData.getDataTableSpec());
coveredPattern++;
} else {
// too many patterns for HiLite - at least remember color
decTree.addCoveredColor(thisRow, inData.getDataTableSpec());
}
nrPattern++;
} catch (Exception e) {
LOGGER.error("Decision Tree evaluation failed: " + e.getMessage());
throw e;
}
if (cl == null) {
LOGGER.error("Decision Tree evaluation failed: result empty");
throw new Exception("Decision Tree evaluation failed.");
}
DataCell[] newCells = new DataCell[outSpec.getNumColumns()];
int numInCells = thisRow.getNumCells();
for (int i = 0; i < numInCells; i++) {
newCells[i] = thisRow.getCell(i);
}
if (m_showDistribution.getBooleanValue()) {
assert predictionValues.size() >= newCells.length - 1 - numInCells : "Could not determine the prediction values: " + newCells.length + "; " + numInCells + "; " + predictionValues;
for (int i = numInCells; i < newCells.length - 1; i++) {
String predClass = predictionValues.get(i - numInCells);
if (classDistrib != null && classDistrib.get(predClass) != null) {
newCells[i] = new DoubleCell(classDistrib.get(predClass));
} else {
newCells[i] = new DoubleCell(0.0);
}
}
}
newCells[newCells.length - 1] = cl;
outData.addRowToTable(new DefaultRow(thisRow.getKey(), newCells));
rowCount++;
if (rowCount % 100 == 0) {
exec.setProgress(rowCount / (double) numberRows, "Classifying... Row " + rowCount + " of " + numberRows);
}
exec.checkCanceled();
}
if (coveredPattern < nrPattern) {
// let the user know that we did not store all available pattern
// for HiLiting.
this.setWarningMessage("Tree only stored first " + m_maxNumCoveredPattern.getIntValue() + " (of " + nrPattern + ") rows for HiLiting!");
}
outData.close();
m_decTree = decTree;
exec.setMessage("Decision Tree Predictor: end execution.");
return new BufferedDataTable[] { outData.getTable() };
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DecTreePredictorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
String predCol = m_predictionColumn.getStringValue();
CheckUtils.checkSetting(!m_overridePrediction.getBooleanValue() || (predCol != null && !predCol.trim().isEmpty()), "Prediction column name cannot be empty");
PMMLPortObjectSpec treeSpec = (PMMLPortObjectSpec) inSpecs[INMODELPORT];
DataTableSpec inSpec = (DataTableSpec) inSpecs[1];
for (String learnColName : treeSpec.getLearningFields()) {
if (!inSpec.containsName(learnColName)) {
throw new InvalidSettingsException("Learning column \"" + learnColName + "\" not found in input " + "data to be predicted");
}
}
return new PortObjectSpec[] { createOutTableSpec(inSpecs) };
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class RPropNodeModel method configure.
/**
* returns null.
*
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
if (m_classcol.getStringValue() != null) {
List<String> learningCols = new LinkedList<String>();
List<String> targetCols = new LinkedList<String>();
boolean classcolinspec = false;
for (DataColumnSpec colspec : (DataTableSpec) inSpecs[INDATA]) {
if (!(colspec.getName().toString().compareTo(m_classcol.getStringValue()) == 0)) {
if (!colspec.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("Only double columns for input");
} else {
learningCols.add(colspec.getName());
DataColumnDomain domain = colspec.getDomain();
if (domain.hasBounds()) {
double lower = ((DoubleValue) domain.getLowerBound()).getDoubleValue();
double upper = ((DoubleValue) domain.getUpperBound()).getDoubleValue();
if (lower < 0 || upper > 1) {
setWarningMessage("Input data not normalized." + " Please consider using the " + "Normalizer Node first.");
}
}
}
} else {
targetCols.add(colspec.getName());
classcolinspec = true;
// TODO: Check what happens to other values than double
if (colspec.getType().isCompatible(DoubleValue.class)) {
// check if the values are in range [0,1]
DataColumnDomain domain = colspec.getDomain();
if (domain.hasBounds()) {
double lower = ((DoubleValue) domain.getLowerBound()).getDoubleValue();
double upper = ((DoubleValue) domain.getUpperBound()).getDoubleValue();
if (lower < 0 || upper > 1) {
throw new InvalidSettingsException("Domain range for regression in column " + colspec.getName() + " not in range [0,1]");
}
}
}
}
}
if (!classcolinspec) {
throw new InvalidSettingsException("Class column " + m_classcol.getStringValue() + " not found in DataTableSpec");
}
return new PortObjectSpec[] { createPMMLPortObjectSpec(m_pmmlInEnabled ? (PMMLPortObjectSpec) inSpecs[1] : null, (DataTableSpec) inSpecs[0], learningCols, targetCols) };
} else {
throw new InvalidSettingsException("Class column not set");
}
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class CAIMDiscretizationNodeModel method configure.
/**
* The number of the class columns must be > 0 and < number of input
* columns. Also create the output table spec replacing the columns to
* discretize to nominal String values.
*
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
// if no columns are defined to discretize, return the input spec
if (m_includedColumnNames.getIncludeList() == null || m_includedColumnNames.getIncludeList().size() == 0) {
throw new InvalidSettingsException(WARNING_NO_COLS_SELECTED);
}
DataTableSpec inDataSpec = (DataTableSpec) inSpecs[DATA_INPORT];
if (inDataSpec == null) {
// garbage in, garbage out
return new PortObjectSpec[] { null, new DataTableSpec() };
}
// contained in the in data table
for (String includedColName : m_includedColumnNames.getIncludeList()) {
if (!inDataSpec.containsName(includedColName)) {
throw new InvalidSettingsException("The selected column to bin '" + includedColName + "' does not exist in the input data " + "table. Reconfigure this node!");
}
}
// else replace for each included column the attribute type to
// string
DataColumnSpec[] newColumnSpecs = new DataColumnSpec[inDataSpec.getNumColumns()];
int counter = 0;
for (DataColumnSpec originalColumnSpec : inDataSpec) {
// if the column is included for discretizing, change the spec
if (isIncluded(originalColumnSpec, m_includedColumnNames.getIncludeList().toArray(new String[m_includedColumnNames.getIncludeList().size()])) > -1) {
// create a nominal string column spec
newColumnSpecs[counter] = new DataColumnSpecCreator(originalColumnSpec.getName(), StringCell.TYPE).createSpec();
} else {
// add it as is
newColumnSpecs[counter] = originalColumnSpec;
}
counter++;
}
return new PortObjectSpec[] { new DataTableSpec(newColumnSpecs), createModelSpec(m_includedColumnNames, inDataSpec) };
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class CaseStartNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
int selectedOutputPort = m_selectedPortModel.getIntValue();
PortObjectSpec[] outSpecs = new PortObjectSpec[3];
PortObjectSpec defOutput = m_activateAllOutputsDuringConfigureModel.getBooleanValue() ? inSpecs[0] : InactiveBranchPortObjectSpec.INSTANCE;
Arrays.fill(outSpecs, defOutput);
outSpecs[selectedOutputPort] = inSpecs[0];
return outSpecs;
}
Aggregations