Search in sources :

Example 91 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class ListFilesNodeDialog method saveSettingsTo.

/**
 * {@inheritDoc}
 */
@Override
protected void saveSettingsTo(final NodeSettingsWO settings) throws InvalidSettingsException {
    // check if all entered Locations are valid
    String location = m_locations.getEditor().getItem().toString();
    if (location.trim().isEmpty()) {
        throw new InvalidSettingsException("Please select a file!");
    }
    String[] files = location.split(";");
    for (int i = 0; i < files.length; i++) {
        File currentFile = new File(files[i]);
        if (!currentFile.isDirectory()) {
            // check if it was an URL;
            String s = files[i];
            try {
                if (s.startsWith("file:")) {
                    s = s.substring(5);
                }
                currentFile = new File(URIUtil.decode(s));
            } catch (URIException ex) {
                throw new InvalidSettingsException("\"" + s + "\" does not exist or is not a directory", ex);
            }
            if (!currentFile.isDirectory()) {
                throw new InvalidSettingsException("\"" + s + "\" does not exist or is not a directory");
            }
        }
    }
    ListFilesSettings set = new ListFilesSettings();
    set.setLocationString(location);
    set.setRecursive(m_recursive.isSelected());
    set.setCaseSensitive(m_caseSensitive.isSelected());
    String extensions = m_extensionField.getEditor().getItem().toString();
    set.setExtensionsString(extensions);
    // save the selected radio-Button
    Filter filter;
    if (m_filterALLRadio.isSelected()) {
        filter = Filter.None;
    } else if (m_filterExtensionsRadio.isSelected()) {
        filter = Filter.Extensions;
    } else if (m_filterRegExpRadio.isSelected()) {
        if (extensions.trim().isEmpty()) {
            throw new InvalidSettingsException("Enter valid regular expressin pattern");
        }
        try {
            String pattern = extensions;
            Pattern.compile(pattern);
        } catch (PatternSyntaxException pse) {
            throw new InvalidSettingsException("Error in pattern: ('" + pse.getMessage(), pse);
        }
        filter = Filter.RegExp;
    } else if (m_filterWildCardsRadio.isSelected()) {
        if ((extensions).length() <= 0) {
            throw new InvalidSettingsException("Enter valid wildcard pattern");
        }
        try {
            String pattern = extensions;
            pattern = WildcardMatcher.wildcardToRegex(pattern);
            Pattern.compile(pattern);
        } catch (PatternSyntaxException pse) {
            throw new InvalidSettingsException("Error in pattern: '" + pse.getMessage(), pse);
        }
        filter = Filter.Wildcards;
    } else {
        // one button must be selected though
        filter = Filter.None;
    }
    set.setFilter(filter);
    set.saveSettingsTo(settings);
}
Also used : URIException(org.apache.commons.httpclient.URIException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Filter(org.knime.base.node.io.listfiles.ListFiles.Filter) File(java.io.File) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 92 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class ColumnListLoopStartNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    if ((m_settings.iterateOverColumns().size() < 1) && !m_settings.iterateAllColumns()) {
        throw new InvalidSettingsException("No columns to iterate over selected");
    }
    if (!m_settings.iterateAllColumns()) {
        for (String col : m_settings.iterateOverColumns()) {
            if (!inSpecs[0].containsName(col)) {
                throw new IllegalArgumentException("Column '" + col + "' does not exist in input table");
            }
        }
    }
    assert m_iteration == 0;
    pushFlowVariableInt("currentIteration", m_iteration);
    ColumnRearranger crea = createRearranger(inSpecs[0]);
    return new DataTableSpec[] { crea.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 93 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class AttributeModel method loadModel.

/**
 * @param config the <code>Config</code> object to read from
 * @return the attribute model for the given <code>Config</code> object
 * @throws InvalidSettingsException if the settings are invalid
 */
static AttributeModel loadModel(final Config config) throws InvalidSettingsException {
    final String attrName = config.getString(ATTRIBUTE_NAME);
    final String modelType = config.getString(MODEL_TYPE);
    final boolean skipMissing = config.getBoolean(SKIP_MISSING_VALUES);
    final int noOfMissingVals = config.getInt(NO_OF_MISSING_VALUES);
    final String invalidCause = config.getString(INVALID_CAUSE);
    final Config modelConfig = config.getConfig(MODEL_DATA_SECTION);
    final AttributeModel model;
    if (NominalAttributeModel.MODEL_TYPE.equals(modelType)) {
        model = new NominalAttributeModel(attrName, noOfMissingVals, skipMissing, modelConfig);
    } else if (NumericalAttributeModel.MODEL_TYPE.equals(modelType)) {
        model = new NumericalAttributeModel(attrName, skipMissing, noOfMissingVals, modelConfig);
    } else if (ClassAttributeModel.MODEL_TYPE.equals(modelType)) {
        model = new ClassAttributeModel(attrName, noOfMissingVals, skipMissing, modelConfig);
    } else if (BitVectorAttributeModel.MODEL_TYPE.equals(modelType)) {
        model = new BitVectorAttributeModel(attrName, skipMissing, noOfMissingVals, modelConfig);
    } else {
        throw new InvalidSettingsException("Invalid model type: " + modelType);
    }
    model.setInvalidCause(invalidCause);
    return model;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Config(org.knime.core.node.config.Config)

Example 94 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class NaiveBayesModel method updateModel.

/**
 * Updates the current {@link NaiveBayesModel} with the values from the
 * given {@link DataRow}.
 * @param row DataRow with values for update
 * @param tableSpec underlying DataTableSpec
 * @param classColIdx the index of the class column
 * @throws InvalidSettingsException if missing values occur in class column
 * or an attribute has too many values.
 */
public void updateModel(final DataRow row, final DataTableSpec tableSpec, final int classColIdx) throws InvalidSettingsException {
    if (row == null) {
        throw new NullPointerException("Row must not be null");
    }
    if (tableSpec == null) {
        throw new NullPointerException("TableSpec must not be null");
    }
    final DataCell classCell = row.getCell(classColIdx);
    if (classCell.isMissing()) {
        if (m_skipMissingVals) {
            return;
        }
        // check if the class value is missing
        throw new InvalidSettingsException("Missing class value found in row " + row.getKey() + " to skip missing values tick the box in the dialog");
    }
    final String classVal = classCell.toString();
    final int numColumns = tableSpec.getNumColumns();
    for (int i = 0; i < numColumns; i++) {
        final AttributeModel model = m_modelByAttrName.get(tableSpec.getColumnSpec(i).getName());
        if (model != null) {
            final DataCell cell = row.getCell(i);
            try {
                model.addValue(classVal, cell);
            } catch (final TooManyValuesException e) {
                if (model instanceof ClassAttributeModel) {
                    throw new InvalidSettingsException("Class attribute has too many unique values. " + "To avoid this exception increase the " + "maximum number of allowed nominal " + "values in the node dialog");
                }
                // delete the model if it contains too many unique values
                m_modelByAttrName.remove(model.getAttributeName());
                model.setInvalidCause("Too many values");
                m_skippedAttributes.add(model);
            }
        }
    }
    m_noOfRecs++;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) TooManyValuesException(org.knime.base.node.mine.bayes.naivebayes.datamodel2.TooManyValuesException)

Example 95 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException 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;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec)

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