use of org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec 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.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec 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.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec in project knime-core by knime.
the class NaiveBayesPredictorNodeDialog method extractTargetColumn.
/**
* {@inheritDoc}
*/
@Override
protected void extractTargetColumn(final PortObjectSpec[] specs) {
if (specs[0] instanceof NaiveBayesPortObjectSpec) {
NaiveBayesPortObjectSpec spec = (NaiveBayesPortObjectSpec) specs[0];
setLastTargetColumn(spec.getClassColumn());
} else {
throw new IllegalStateException(specs[0].getClass().toString());
}
}
use of org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec 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 PredictorHelper predictorHelper = PredictorHelper.getInstance();
final DataColumnSpec resultColSpecs = NaiveBayesCellFactory.createResultColSpecs(predictorHelper.computePredictionColumnName(m_predictionColumnName.getStringValue(), m_overridePredicted.getBooleanValue(), classColumn.getName()), classColumn.getType(), spec, m_inclProbVals.getBooleanValue());
if (resultColSpecs != null) {
return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(spec, resultColSpecs) };
}
return null;
}
Aggregations