use of org.knime.core.node.InvalidSettingsException 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.InvalidSettingsException in project knime-core by knime.
the class MLPPredictorNodeModel method configure.
/**
* The additional columns are created based on the model which is loaded in
* the execute-method. Therefore, new DataTableSpecs are not available until
* execute has been called.
*
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
PMMLPortObjectSpec modelspec = (PMMLPortObjectSpec) inSpecs[0];
DataTableSpec testSpec = (DataTableSpec) inSpecs[1];
List<DataColumnSpec> targetCols = modelspec.getTargetCols();
if (targetCols.isEmpty()) {
throw new InvalidSettingsException("The PMML model" + " does not specify a target column for the prediction.");
}
DataColumnSpec targetCol = targetCols.iterator().next();
/*
* Check consistency between model and inputs, find columns to work on.
*/
for (String incol : modelspec.getLearningFields()) {
if (!testSpec.containsName(incol)) {
throw new InvalidSettingsException("Could not find " + incol + " in inputspec");
}
}
m_columns = getLearningColumnIndices(testSpec, modelspec);
MLPClassificationFactory mymlp;
// Regression
if (targetCol.getType().isCompatible(DoubleValue.class)) {
mymlp = new MLPClassificationFactory(true, m_columns, targetCol);
} else {
// Classification
mymlp = new MLPClassificationFactory(false, m_columns, targetCol);
}
ColumnRearranger colre = new ColumnRearranger(testSpec);
colre.append(mymlp);
return new DataTableSpec[] { colre.createSpec() };
}
use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.
the class LinRegLearnerNodeModel method computeIncludes.
/**
* Determines the list of variate columns (learning columns). This is
* either the m_includes[] field or, if m_includeAll is set, the list
* of double-compatible columns in the input table spec (excluding the
* response column).
* @param in Spec contributing the column list
* @return A new array containg the variates
* @throws InvalidSettingsException If no double-compatible learning columns
* exist in the input table.
*/
private String[] computeIncludes(final DataTableSpec in) throws InvalidSettingsException {
String[] includes;
if (m_includeAll) {
List<String> includeList = new ArrayList<String>();
for (DataColumnSpec s : in) {
if (s.getType().isCompatible(DoubleValue.class)) {
String name = s.getName();
if (!name.equals(m_target)) {
includeList.add(name);
}
}
}
includes = includeList.toArray(new String[includeList.size()]);
if (includes.length == 0) {
throw new InvalidSettingsException("No double-compatible " + "variables (learning columns) in input table");
}
} else {
if (m_includes == null) {
throw new InvalidSettingsException("No settings available");
}
includes = m_includes.clone();
}
return includes;
}
use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.
the class LogRegPredictor method determineTargetCategories.
/**
* Retrieve the target values from the PMML model.
* @throws InvalidSettingsException if PMML model is inconsistent or ambiguous
*/
private static List<DataCell> determineTargetCategories(final DataColumnSpec targetCol, final PMMLGeneralRegressionContent content) throws InvalidSettingsException {
Map<String, DataCell> domainValues = new HashMap<String, DataCell>();
for (DataCell cell : targetCol.getDomain().getValues()) {
domainValues.put(cell.toString(), cell);
}
// Collect target categories from model
Set<DataCell> modelTargetCategories = new LinkedHashSet<DataCell>();
for (PMMLPCell cell : content.getParamMatrix()) {
modelTargetCategories.add(domainValues.get(cell.getTargetCategory()));
}
String targetReferenceCategory = content.getTargetReferenceCategory();
if (targetReferenceCategory == null || targetReferenceCategory.isEmpty()) {
List<DataCell> targetCategories = new ArrayList<DataCell>();
targetCategories.addAll(targetCol.getDomain().getValues());
Collections.sort(targetCategories, targetCol.getType().getComparator());
if (targetCategories.size() == modelTargetCategories.size() + 1) {
targetReferenceCategory = targetCategories.get(targetCategories.size() - 1).toString();
// the last target category is the target reference category
LOGGER.debug("The target reference category is not explicitly set in PMML. Automatically choose : " + targetReferenceCategory);
} else {
throw new InvalidSettingsException("Please set the attribute \"targetReferenceCategory\" of the" + "\"GeneralRegression\" element in the PMML file.");
}
}
modelTargetCategories.add(domainValues.get(targetReferenceCategory));
List<DataCell> toReturn = new ArrayList<DataCell>();
toReturn.addAll(modelTargetCategories);
return toReturn;
}
use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.
the class RegressionPredictorNodeDialogPane method loadSettingsFrom.
/**
* {@inheritDoc}
*/
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
RegressionPredictorSettings s = new RegressionPredictorSettings();
s.loadSettingsForDialog(settings);
m_hasCustomPredictionName.setSelected(s.getHasCustomPredictionName());
PMMLPortObjectSpec portSpec = (PMMLPortObjectSpec) specs[0];
DataTableSpec tableSpec = (DataTableSpec) specs[1];
if (s.getCustomPredictionName() != null) {
m_customPredictionName.setText(s.getCustomPredictionName());
} else {
try {
DataColumnSpec[] outSpec = RegressionPredictorCellFactory.createColumnSpec(portSpec, tableSpec, new RegressionPredictorSettings());
m_customPredictionName.setText(outSpec[outSpec.length - 1].getName());
} catch (InvalidSettingsException e) {
// Open dialog and give a chance define settings
}
}
m_includeProbs.setSelected(s.getIncludeProbabilities());
m_probColumnSuffix.setText(s.getPropColumnSuffix());
updateEnableState();
}
Aggregations