Search in sources :

Example 6 with DataTableDomainCreator

use of org.knime.core.data.DataTableDomainCreator in project knime-core by knime.

the class DomainNodeModel method getDomainCreator.

private DataTableDomainCreator getDomainCreator(final DataTableSpec inputSpec) {
    final Set<String> possValCols = new HashSet<String>();
    possValCols.addAll(Arrays.asList(m_possValCols));
    int maxPoss = m_maxPossValues >= 0 ? m_maxPossValues : Integer.MAX_VALUE;
    final Set<String> minMaxCols = new HashSet<String>();
    minMaxCols.addAll(Arrays.asList(m_minMaxCols));
    DomainCreatorColumnSelection possValueSelection = new DomainCreatorColumnSelection() {

        @Override
        public boolean createDomain(final DataColumnSpec colSpec) {
            return possValCols.contains(colSpec.getName());
        }

        @Override
        public boolean dropDomain(final DataColumnSpec colSpec) {
            return possValCols.contains(colSpec.getName()) || !m_possValRetainUnselected;
        }
    };
    DomainCreatorColumnSelection minMaxSelection = new DomainCreatorColumnSelection() {

        @Override
        public boolean createDomain(final DataColumnSpec colSpec) {
            return minMaxCols.contains(colSpec.getName());
        }

        @Override
        public boolean dropDomain(final DataColumnSpec colSpec) {
            return minMaxCols.contains(colSpec.getName()) || !m_minMaxRetainUnselected;
        }
    };
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(inputSpec, possValueSelection, minMaxSelection);
    domainCreator.setMaxPossibleValues(maxPoss);
    return domainCreator;
}
Also used : DataTableDomainCreator(org.knime.core.data.DataTableDomainCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) DomainCreatorColumnSelection(org.knime.core.data.DomainCreatorColumnSelection) HashSet(java.util.HashSet)

Example 7 with DataTableDomainCreator

use of org.knime.core.data.DataTableDomainCreator in project knime-core by knime.

the class DomainNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    DataTableDomainCreator domainCreator = getDomainCreator(inData[0].getDataTableSpec());
    domainCreator.updateDomain(inData[0], exec, inData[0].getRowCount());
    return new BufferedDataTable[] { exec.createSpecReplacerTable(inData[0], domainCreator.createSpec()) };
}
Also used : DataTableDomainCreator(org.knime.core.data.DataTableDomainCreator) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 8 with DataTableDomainCreator

use of org.knime.core.data.DataTableDomainCreator in project knime-core by knime.

the class LogRegCoordinator method recalcDomainForTargetAndLearningFields.

private BufferedDataTable recalcDomainForTargetAndLearningFields(final BufferedDataTable data, final ExecutionContext exec) throws InvalidSettingsException, CanceledExecutionException {
    final String targetCol = m_pmmlOutSpec.getTargetFields().get(0);
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(data.getDataTableSpec(), new DomainCreatorColumnSelection() {

        @Override
        public boolean dropDomain(final DataColumnSpec colSpec) {
            return false;
        }

        @Override
        public boolean createDomain(final DataColumnSpec colSpec) {
            return colSpec.getName().equals(targetCol) || (colSpec.getType().isCompatible(NominalValue.class) && m_pmmlOutSpec.getLearningFields().contains(colSpec.getName()));
        }
    }, new DomainCreatorColumnSelection() {

        @Override
        public boolean dropDomain(final DataColumnSpec colSpec) {
            // drop domain of numeric learning fields so that we can check for constant columns
            return colSpec.getType().isCompatible(DoubleValue.class) && m_pmmlOutSpec.getLearningFields().contains(colSpec.getName());
        }

        @Override
        public boolean createDomain(final DataColumnSpec colSpec) {
            return colSpec.getType().isCompatible(DoubleValue.class) && m_pmmlOutSpec.getLearningFields().contains(colSpec.getName());
        }
    });
    domainCreator.updateDomain(data, exec);
    DataTableSpec spec = domainCreator.createSpec();
    CheckUtils.checkSetting(spec.getColumnSpec(targetCol).getDomain().hasValues(), "Target column '%s' has too many" + " unique values - consider to use domain calucator node before to enforce calculation", targetCol);
    BufferedDataTable newDataTable = exec.createSpecReplacerTable(data, spec);
    // bug fix 5580 - ignore columns with too many different values
    Set<String> columnWithTooManyDomainValues = new LinkedHashSet<>();
    for (String learningField : m_pmmlOutSpec.getLearningFields()) {
        DataColumnSpec columnSpec = spec.getColumnSpec(learningField);
        if (columnSpec.getType().isCompatible(NominalValue.class) && !columnSpec.getDomain().hasValues()) {
            columnWithTooManyDomainValues.add(learningField);
        }
    }
    if (!columnWithTooManyDomainValues.isEmpty()) {
        StringBuilder warning = new StringBuilder();
        warning.append(columnWithTooManyDomainValues.size() == 1 ? "Column " : "Columns ");
        warning.append(ConvenienceMethods.getShortStringFrom(columnWithTooManyDomainValues, 5));
        warning.append(columnWithTooManyDomainValues.size() == 1 ? " has " : " have ");
        warning.append("too many different values - will be ignored during training ");
        warning.append("(enforce inclusion by using a domain calculator node before)");
        // LOGGER.warn(warning.toString());
        m_warning = (m_warning == null ? "" : m_warning + "\n") + warning.toString();
    }
    // initialize m_learner so that it has the correct DataTableSpec of the input
    init(newDataTable.getDataTableSpec(), columnWithTooManyDomainValues);
    return newDataTable;
}
Also used : DataTableDomainCreator(org.knime.core.data.DataTableDomainCreator) LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) NominalValue(org.knime.core.data.NominalValue) DomainCreatorColumnSelection(org.knime.core.data.DomainCreatorColumnSelection) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 9 with DataTableDomainCreator

use of org.knime.core.data.DataTableDomainCreator in project knime-core by knime.

the class LinReg2Learner method recalcDomainOfLearningFields.

private BufferedDataTable recalcDomainOfLearningFields(final BufferedDataTable data, final PMMLPortObjectSpec inPMMLSpec, final ExecutionContext exec) throws InvalidSettingsException, CanceledExecutionException {
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(data.getDataTableSpec(), new DomainCreatorColumnSelection() {

        @Override
        public boolean dropDomain(final DataColumnSpec colSpec) {
            return true;
        }

        @Override
        public boolean createDomain(final DataColumnSpec colSpec) {
            return colSpec.getType().isCompatible(NominalValue.class) && (m_pmmlOutSpec.getLearningFields().contains(colSpec.getName()) || m_pmmlOutSpec.getTargetFields().contains(colSpec.getName()));
        }
    }, new DomainCreatorColumnSelection() {

        @Override
        public boolean dropDomain(final DataColumnSpec colSpec) {
            return false;
        }

        @Override
        public boolean createDomain(final DataColumnSpec colSpec) {
            return false;
        }
    });
    domainCreator.updateDomain(data, exec);
    DataTableSpec spec = domainCreator.createSpec();
    BufferedDataTable newDataTable = exec.createSpecReplacerTable(data, spec);
    // bug fix 5793, similar to 5580 in LogReg2Learner - ignore columns with too many different values.
    // But because this would change behavior, we cannot drop the domain, which means that even
    // prepending a domain calculator to this node will node help when the column has too many values.
    Set<String> columnWithTooManyDomainValues = new LinkedHashSet<>();
    for (String learningField : m_pmmlOutSpec.getLearningFields()) {
        DataColumnSpec columnSpec = spec.getColumnSpec(learningField);
        if (columnSpec.getType().isCompatible(NominalValue.class) && !columnSpec.getDomain().hasValues()) {
            columnWithTooManyDomainValues.add(learningField);
        }
    }
    // initialize m_learner so that it has the correct DataTableSpec of
    // the input
    init(newDataTable.getDataTableSpec(), inPMMLSpec, columnWithTooManyDomainValues);
    if (!columnWithTooManyDomainValues.isEmpty()) {
        StringBuilder warning = new StringBuilder();
        warning.append(columnWithTooManyDomainValues.size() == 1 ? "Column " : "Columns ");
        warning.append(ConvenienceMethods.getShortStringFrom(columnWithTooManyDomainValues, 5));
        warning.append(columnWithTooManyDomainValues.size() == 1 ? " has " : " have ");
        warning.append("too many different values - will be ignored during training");
        // warning.append("(enforce inclusion by using a domain calculator node before)");
        LOGGER.warn(warning.toString());
        m_warningMessage = (m_warningMessage == null ? "" : m_warningMessage + "\n") + warning.toString();
    }
    return newDataTable;
}
Also used : DataTableDomainCreator(org.knime.core.data.DataTableDomainCreator) LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) NominalValue(org.knime.core.data.NominalValue) DomainCreatorColumnSelection(org.knime.core.data.DomainCreatorColumnSelection) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 10 with DataTableDomainCreator

use of org.knime.core.data.DataTableDomainCreator in project knime-core by knime.

the class DomainNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    // auto-configuration in case no user settings are available
    DataTableSpec spec = inSpecs[0];
    if (m_possValConfig == null) {
        m_possValConfig = createDCSFilterConfigurationPossVals();
        m_possValConfig.loadDefaults(DomainNodeModel.getAllCols(NominalValue.class, spec), null, EnforceOption.EnforceInclusion);
    }
    if (m_minMaxConfig == null) {
        m_minMaxConfig = createDCSFilterConfigurationMinMax();
        m_minMaxConfig.loadDefaults(DomainNodeModel.getAllCols(BoundedValue.class, spec), null, EnforceOption.EnforceInclusion);
    }
    FilterResult filter = m_possValConfig.applyTo(spec);
    String[] rmFromIncl = filter.getRemovedFromIncludes();
    if (m_possValConfig.isEnforceInclusion() && rmFromIncl.length != 0) {
        throw new InvalidSettingsException("Input table does not contain the following column(s) selected in the " + "possible value panel: " + ConvenienceMethods.getShortStringFrom(new HashSet<String>(Arrays.asList(rmFromIncl)), 3));
    }
    filter = m_minMaxConfig.applyTo(spec);
    rmFromIncl = filter.getRemovedFromIncludes();
    if (m_possValConfig.isEnforceInclusion() && rmFromIncl.length != 0) {
        throw new InvalidSettingsException("Input table does not contain the following column(s) selected in the " + "Min/Max value panel: " + ConvenienceMethods.getShortStringFrom(new HashSet<String>(Arrays.asList(rmFromIncl)), 3));
    }
    DataTableDomainCreator domainCreator = getDomainCreator(spec);
    return new DataTableSpec[] { domainCreator.createSpec() };
}
Also used : DataTableDomainCreator(org.knime.core.data.DataTableDomainCreator) DataTableSpec(org.knime.core.data.DataTableSpec) BoundedValue(org.knime.core.data.BoundedValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NominalValue(org.knime.core.data.NominalValue) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)

Aggregations

DataTableDomainCreator (org.knime.core.data.DataTableDomainCreator)10 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 DataTableSpec (org.knime.core.data.DataTableSpec)6 DomainCreatorColumnSelection (org.knime.core.data.DomainCreatorColumnSelection)6 BufferedDataTable (org.knime.core.node.BufferedDataTable)6 NominalValue (org.knime.core.data.NominalValue)5 LinkedHashSet (java.util.LinkedHashSet)4 HashSet (java.util.HashSet)2 BoundedValue (org.knime.core.data.BoundedValue)1 DataRow (org.knime.core.data.DataRow)1 RowIterator (org.knime.core.data.RowIterator)1 CloseableRowIterator (org.knime.core.data.container.CloseableRowIterator)1 DefaultRowIterator (org.knime.core.data.def.DefaultRowIterator)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 FilterResult (org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)1