Search in sources :

Example 96 with InvalidSettingsException

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)) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec)

Example 97 with InvalidSettingsException

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() };
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 98 with InvalidSettingsException

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;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList)

Example 99 with InvalidSettingsException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PMMLPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell) HashMap(java.util.HashMap) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell)

Example 100 with InvalidSettingsException

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();
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Aggregations

InvalidSettingsException (org.knime.core.node.InvalidSettingsException)818 DataTableSpec (org.knime.core.data.DataTableSpec)278 DataColumnSpec (org.knime.core.data.DataColumnSpec)211 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)153 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)121 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)113 IOException (java.io.IOException)109 DataCell (org.knime.core.data.DataCell)99 ArrayList (java.util.ArrayList)96 DataType (org.knime.core.data.DataType)89 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)82 File (java.io.File)72 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)69 DataRow (org.knime.core.data.DataRow)66 DoubleValue (org.knime.core.data.DoubleValue)58 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)48 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)47 FileInputStream (java.io.FileInputStream)43 LinkedHashMap (java.util.LinkedHashMap)42 NotConfigurableException (org.knime.core.node.NotConfigurableException)41