use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class NewToOldTimeNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public StreamableOperatorInternals saveInternals() {
return null;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
final DataTableSpec inSpec = in.getDataTableSpec();
String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndeces = Arrays.stream(includeList).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
DataRow row;
while ((row = in.poll()) != null) {
exec.checkCanceled();
DataCell[] datacells = new DataCell[includeIndeces.length];
for (int i = 0; i < includeIndeces.length; i++) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includeList[i], DateAndTimeCell.TYPE);
final ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i]);
datacells[i] = cellFac.getCells(row)[0];
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includeList[i] + m_suffix.getStringValue(), DateAndTimeCell.TYPE);
final ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColSpec, includeIndeces[i]);
datacells[i] = cellFac.getCells(row)[0];
}
}
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
out.push(new ReplacedColumnsDataRow(row, datacells, includeIndeces));
} else {
out.push(new AppendedColumnRow(row, datacells));
}
}
in.close();
out.close();
}
};
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DateTimeToStringNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
final DataTableSpec inSpec = in.getDataTableSpec();
final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndeces = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
final boolean isReplace = m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE);
DataRow row;
while ((row = in.poll()) != null) {
exec.checkCanceled();
DataCell[] datacells = new DataCell[includeIndeces.length];
for (int i = 0; i < includeIndeces.length; i++) {
if (isReplace) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includeList[i], StringCell.TYPE);
final TimeToStringCellFactory cellFac = new TimeToStringCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i]);
datacells[i] = cellFac.getCell(row);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includeList[i] + m_suffix.getStringValue(), StringCell.TYPE);
final TimeToStringCellFactory cellFac = new TimeToStringCellFactory(dataColSpec, includeIndeces[i]);
datacells[i] = cellFac.getCell(row);
}
}
if (isReplace) {
out.push(new ReplacedColumnsDataRow(row, datacells, includeIndeces));
} else {
out.push(new AppendedColumnRow(row, datacells));
}
}
in.close();
out.close();
}
};
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class NaiveBayesPredictorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
// check the input data
assert (inSpecs != null && inSpecs.length == 2 && inSpecs[DATA_IN_PORT] != null && inSpecs[MODEL_IN_PORT] != null);
final PortObjectSpec modelObject = inSpecs[MODEL_IN_PORT];
if (!(modelObject instanceof NaiveBayesPortObjectSpec)) {
throw new IllegalArgumentException("Invalid input data");
}
final DataTableSpec trainingSpec = ((NaiveBayesPortObjectSpec) modelObject).getTableSpec();
final DataColumnSpec classColumn = ((NaiveBayesPortObjectSpec) modelObject).getClassColumn();
if (trainingSpec == null) {
throw new InvalidSettingsException("No model spec available");
}
final PortObjectSpec inSpec = inSpecs[DATA_IN_PORT];
if (!(inSpec instanceof DataTableSpec)) {
throw new IllegalArgumentException("TableSpec must not be null");
}
final DataTableSpec spec = (DataTableSpec) inSpec;
// check the input data for columns with the wrong name or wrong type
final List<String> unknownCols = check4UnknownCols(trainingSpec, spec);
if (unknownCols.size() >= spec.getNumColumns()) {
setWarningMessage("No known attribute columns found use " + "class prior probability to predict the class membership");
} else if (unknownCols.size() == 1) {
setWarningMessage("Input column " + unknownCols.get(0) + " is unknown and will be skipped.");
} else if (unknownCols.size() > 1) {
final StringBuilder buf = new StringBuilder();
buf.append("The following input columns are unknown and " + "will be skipped: ");
for (int i = 0, length = unknownCols.size(); i < length; i++) {
if (i != 0) {
buf.append(", ");
}
if (i > 3) {
buf.append("...");
break;
}
buf.append(unknownCols.get(i));
}
setWarningMessage(buf.toString());
}
// check if the learned model contains columns which are not in the
// input data
final List<String> missingInputCols = check4MissingCols(trainingSpec, classColumn.getName(), spec);
if (missingInputCols.size() == 1) {
setWarningMessage("Attribute " + missingInputCols.get(0) + " is missing in the input data");
} else if (missingInputCols.size() > 1) {
final StringBuilder buf = new StringBuilder();
buf.append("The following attributes are missing in " + "the input data: ");
for (int i = 0, length = missingInputCols.size(); i < length; i++) {
if (i != 0) {
buf.append(", ");
}
if (i > 3) {
buf.append("...");
break;
}
buf.append(missingInputCols.get(i));
}
setWarningMessage(buf.toString());
}
final DataColumnSpec resultColSpecs = NaiveBayesCellFactory.createResultColSpecs(classColumn, spec, m_inclProbVals.getBooleanValue());
if (resultColSpecs != null) {
return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(spec, resultColSpecs) };
}
return null;
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class NaiveBayesLearnerNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
// check the internal variables if they are valid
final String classColumn = m_classifyColumnName.getStringValue();
if (classColumn == null || classColumn.length() < 1) {
throw new InvalidSettingsException("Please define the classification column");
}
final PortObjectSpec inSpec = inSpecs[TRAINING_DATA_PORT];
if (!(inSpec instanceof DataTableSpec)) {
throw new IllegalArgumentException("Invalid input data");
}
final DataTableSpec tableSpec = (DataTableSpec) inSpec;
if (tableSpec.findColumnIndex(classColumn) < 0) {
throw new InvalidSettingsException("Please define the classification column");
}
if (tableSpec.getNumColumns() < 2) {
throw new InvalidSettingsException("Input table should contain at least 2 columns");
}
final int maxNoOfNominalVals = m_maxNoOfNominalVals.getIntValue();
// check if the table contains at least one nominal column
// and check each nominal column with a valid domain
// if it contains more values than allowed
boolean containsNominalCol = false;
final List<String> toBigNominalColumns = new ArrayList<>();
for (int i = 0, length = tableSpec.getNumColumns(); i < length; i++) {
final DataColumnSpec colSpec = tableSpec.getColumnSpec(i);
if (colSpec.getType().isCompatible(NominalValue.class)) {
containsNominalCol = true;
final DataColumnDomain domain = colSpec.getDomain();
if (domain != null && domain.getValues() != null) {
if (domain.getValues().size() > maxNoOfNominalVals) {
// unique values
if (colSpec.getName().equals(classColumn)) {
// contains too many unique values
throw new InvalidSettingsException("Class column domain contains too many unique values" + " (" + domain.getValues().size() + ")");
}
toBigNominalColumns.add(colSpec.getName() + " (" + domain.getValues().size() + ")");
}
}
}
}
if (!containsNominalCol) {
throw new InvalidSettingsException("No possible class attribute found in input table");
}
if (toBigNominalColumns.size() == 1) {
setWarningMessage("Column " + toBigNominalColumns.get(0) + " will possibly be skipped.");
} else if (toBigNominalColumns.size() > 1) {
final StringBuilder buf = new StringBuilder();
buf.append("The following columns will possibly be skipped: ");
for (int i = 0, length = toBigNominalColumns.size(); i < length; i++) {
if (i != 0) {
buf.append(", ");
}
if (i > 3) {
buf.append("...");
break;
}
buf.append(toBigNominalColumns.get(i));
}
setWarningMessage(buf.toString());
}
if (tableSpec.getNumColumns() - toBigNominalColumns.size() < 1) {
throw new InvalidSettingsException("Not enough valid columns");
}
return new PortObjectSpec[] { new NaiveBayesPortObjectSpec(tableSpec, tableSpec.getColumnSpec(classColumn)) };
}
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;
long numberRows = inData.size();
exec.setMessage("Classifying...");
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()) {
for (int i = numInCells; i < newCells.length - 1; i++) {
String predClass = outSpec.getColumnSpec(i).getName();
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() };
}
Aggregations