use of org.knime.core.node.port.PortObject in project knime-core by knime.
the class AutoBinnerApplyNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
PMMLPreprocPortObject pmmlPort = (PMMLPreprocPortObject) inObjects[0];
BufferedDataTable inTable = (BufferedDataTable) inObjects[1];
validatePMMLPort(pmmlPort);
PMMLPreprocDiscretize op = (PMMLPreprocDiscretize) pmmlPort.getOperations().get(0);
AutoBinnerApply applier = new AutoBinnerApply();
BufferedDataTable binnedData = applier.execute(op, inTable, exec);
return new PortObject[] { binnedData };
}
use of org.knime.core.node.port.PortObject 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 port = (PMMLPortObject) inData[0];
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.GeneralRegressionModel);
if (models.isEmpty()) {
// KNIME currently (Sep '17) still uses the 'Regression' Model Type in the PolyReg learner
@SuppressWarnings("deprecation") org.knime.base.node.mine.regression.predict.RegressionPredictorNodeModel regrPredictor = new org.knime.base.node.mine.regression.predict.RegressionPredictorNodeModel();
@SuppressWarnings("deprecation") PortObject[] regrPredOut = regrPredictor.execute(inData, exec);
if (regrPredOut.length > 0 && regrPredOut[0] instanceof BufferedDataTable) {
BufferedDataTable regrPredOutTable = (BufferedDataTable) regrPredOut[0];
// replace name of prediction column (the last column of regrPredOutTable)
return new PortObject[] { adjustSpecOfRegressionPredictorTable(regrPredOutTable, inData, exec) };
} else {
return regrPredOut;
}
}
PMMLGeneralRegressionTranslator trans = new PMMLGeneralRegressionTranslator();
port.initializeModelTranslator(trans);
BufferedDataTable data = (BufferedDataTable) inData[1];
DataTableSpec spec = data.getDataTableSpec();
ColumnRearranger c = createRearranger(trans.getContent(), port.getSpec(), spec);
BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.node.port.PortObject in project knime-core by knime.
the class NaiveBayesLearnerNodeModel2 method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
LOGGER.debug("Entering execute of " + NaiveBayesLearnerNodeModel2.class.getName());
assert (inData != null && ((inData.length == 2 && m_pmmlInEnabled) || (inData.length == 1 && !m_pmmlInEnabled)) && inData[TRAINING_DATA_PORT] != null);
final PortObject inObject = inData[TRAINING_DATA_PORT];
if (!(inObject instanceof BufferedDataTable)) {
throw new IllegalArgumentException("Invalid input data");
}
final BufferedDataTable trainingTable = (BufferedDataTable) inObject;
final boolean ignoreMissingVals = m_ignoreMissingVals.getBooleanValue();
final boolean pmmlCompatible = m_pmmlCompatible.getBooleanValue();
final int maxNoOfNomVals = m_maxNoOfNominalVals.getIntValue();
m_model = new NaiveBayesModel(trainingTable, m_classifyColumnName.getStringValue(), exec, maxNoOfNomVals, ignoreMissingVals, pmmlCompatible, m_threshold.getDoubleValue());
final List<String> missingModels = m_model.getAttributesWithMissingVals();
if (missingModels.size() > 0) {
final StringBuilder buf = new StringBuilder();
buf.append("The following attributes contain missing values: ");
for (int i = 0, length = missingModels.size(); i < length; i++) {
if (i != 0) {
buf.append(", ");
}
if (i > 3) {
buf.append("...(see View)");
break;
}
buf.append(missingModels.get(i));
}
setWarningMessage(buf.toString());
}
if (m_model.containsSkippedAttributes()) {
setWarningMessage(m_model.getSkippedAttributesString(3));
}
LOGGER.debug("Exiting execute of " + NaiveBayesLearnerNodeModel2.class.getName());
// handle the optional PMML input
final PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[MODEL_INPORT] : null;
final DataTableSpec tableSpec = trainingTable.getSpec();
final PMMLPortObjectSpec outPortSpec = createPMMLSpec(tableSpec, inPMMLPort == null ? null : inPMMLPort.getSpec(), m_model.getPMMLLearningCols(), m_model.getClassColumnName());
final PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, tableSpec);
outPMMLPort.addModelTranslater(new PMMLNaiveBayesModelTranslator(m_model));
return new PortObject[] { outPMMLPort, m_model.getStatisticsTable() };
}
use of org.knime.core.node.port.PortObject in project knime-core by knime.
the class BasisFunctionLearnerNodeModel method execute.
/**
* Starts the learning algorithm in the learner.
*
* @param inData the input training data at index 0
* @param exec the execution monitor
* @return the output fuzzy rule model
* @throws CanceledExecutionException if the training was canceled
*/
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException {
BufferedDataTable data = (BufferedDataTable) inData[0];
// find all double cell columns in the data
DataTableSpec tSpec = data.getDataTableSpec();
LinkedHashSet<String> columns = new LinkedHashSet<String>(tSpec.getNumColumns());
List<String> targetHash = Arrays.asList(m_targetColumns);
for (int c = 0; c < tSpec.getNumColumns(); c++) {
DataColumnSpec cSpec = tSpec.getColumnSpec(c);
String name = cSpec.getName();
if (!targetHash.contains(name)) {
// TODO only numeric columns allowed
if (cSpec.getType().isCompatible(DoubleValue.class)) {
columns.add(cSpec.getName());
}
}
}
// get all data columns without target columns
String[] dataCols = BasisFunctionFactory.findDataColumns(tSpec, targetHash);
columns.addAll(Arrays.asList(dataCols));
// add target columns at the end
columns.addAll(Arrays.asList(m_targetColumns));
// filter selected columns from input data
String[] cols = columns.toArray(new String[] {});
ColumnRearranger colRe = new ColumnRearranger(tSpec);
colRe.keepOnly(cols);
BufferedDataTable trainData = exec.createColumnRearrangeTable(data, colRe, exec);
// print settings info
LOGGER.debug("distance : " + getDistance());
LOGGER.debug("missing : " + getMissingFct());
LOGGER.debug("target columns: " + Arrays.toString(m_targetColumns));
LOGGER.debug("shrink commit : " + isShrinkAfterCommit());
LOGGER.debug("max coverage : " + isMaxClassCoverage());
LOGGER.debug("max no. epochs: " + m_maxEpochs);
// create factory
BasisFunctionFactory factory = getFactory(trainData.getDataTableSpec());
// start training
BasisFunctionLearnerTable table = new BasisFunctionLearnerTable(trainData, dataCols, m_targetColumns, factory, BasisFunctionLearnerTable.MISSINGS[m_missing], m_shrinkAfterCommit, m_maxCoverage, m_maxEpochs, exec);
DataTableSpec modelSpec = table.getDataTableSpec();
DataColumnSpec[] modelSpecs = new DataColumnSpec[modelSpec.getNumColumns()];
for (int i = 0; i < modelSpecs.length; i++) {
DataColumnSpecCreator creator = new DataColumnSpecCreator(modelSpec.getColumnSpec(i));
creator.removeAllHandlers();
modelSpecs[i] = creator.createSpec();
}
// set translator mapping
m_translator.setMapper(table.getHiLiteMapper());
ModelContent modelInfo = new ModelContent(MODEL_INFO);
table.saveInfos(modelInfo);
m_modelInfo = modelInfo;
// return rules[0] and rule_model[1]
return new PortObject[] { exec.createBufferedDataTable(table, exec), createPortObject(new BasisFunctionModelContent(table.getDataTableSpec(), table.getBasisFunctions())) };
}
use of org.knime.core.node.port.PortObject in project knime-core by knime.
the class NaiveBayesPredictorNodeModel2 method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
LOGGER.debug("Entering execute(inData, exec) of class NaiveBayesPredictorNodeModel.");
// check input data
assert (inData != null && inData.length == 2 && inData[DATA_IN_PORT] != null && inData[MODEL_IN_PORT] != null);
final PortObject dataObject = inData[DATA_IN_PORT];
if (!(dataObject instanceof BufferedDataTable)) {
throw new IllegalArgumentException("Invalid input data");
}
final BufferedDataTable data = (BufferedDataTable) dataObject;
final PortObject modelObject = inData[MODEL_IN_PORT];
if (!(modelObject instanceof PMMLPortObject)) {
throw new IllegalArgumentException("Invalid input data");
}
final PMMLPortObject modelPort = (PMMLPortObject) modelObject;
final Collection<Node> models = modelPort.getPMMLValue().getModels(PMMLModelType.NaiveBayesModel);
if (models == null || models.isEmpty()) {
throw new Exception("Node not properly configured. No Naive Bayes Model available.");
}
if (models.size() > 1) {
throw new Exception("Node supports only one Naive Bayes Model at a time.");
}
exec.setMessage("Classifying rows...");
ColumnRearranger rearranger = createColumnRearranger(modelPort, data.getDataTableSpec());
final BufferedDataTable returnVal = exec.createColumnRearrangeTable(data, rearranger, exec);
LOGGER.debug("Exiting execute(inData, exec) of class NaiveBayesPredictorNodeModel.");
return new PortObject[] { returnVal };
}
Aggregations