Search in sources :

Example 41 with FilterResult

use of org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult in project knime-core by knime.

the class LowVarFilter2NodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException {
    if (m_conf == null) {
        // auto-guess
        m_conf = createColFilterConf();
    }
    final FilterResult filter = m_conf.applyTo(inData[0].getDataTableSpec());
    String[] includedColumns = filter.getIncludes();
    Statistics3Table statTable = new Statistics3Table(inData[0], false, 0, Collections.<String>emptyList(), exec);
    ArrayList<String> includes = new ArrayList<String>();
    DataTableSpec s = inData[0].getDataTableSpec();
    int colCount = s.getNumColumns();
    double threshold = m_varianceThreshold;
    HashSet<String> includesHash = new HashSet<String>(Arrays.asList(includedColumns));
    for (int i = 0; i < colCount; i++) {
        DataColumnSpec cs = s.getColumnSpec(i);
        if (!includesHash.contains(cs.getName()) || !cs.getType().isCompatible(DoubleValue.class) || statTable.getVariance(i) > threshold) {
            includes.add(cs.getName());
        }
    }
    int filteredOutCount = s.getNumColumns() - includes.size();
    LOGGER.info("Filtered out " + filteredOutCount + " column(s)");
    if (filteredOutCount == 0) {
        setWarningMessage("No columns were filtered out.");
    }
    ColumnRearranger rearranger = new ColumnRearranger(s);
    rearranger.keepOnly(includes.toArray(new String[includes.size()]));
    BufferedDataTable t = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
    return new BufferedDataTable[] { t };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DoubleValue(org.knime.core.data.DoubleValue) Statistics3Table(org.knime.base.data.statistics.Statistics3Table) BufferedDataTable(org.knime.core.node.BufferedDataTable) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 42 with FilterResult

use of org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult 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)

Example 43 with FilterResult

use of org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult in project knime-core by knime.

the class CategoryToNumberNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inSpec = (DataTableSpec) inSpecs[0];
    List<String> inputCols = new ArrayList<String>();
    for (DataColumnSpec column : inSpec) {
        if (column.getType().isCompatible(StringValue.class)) {
            inputCols.add(column.getName());
        }
    }
    FilterResult filter = m_settings.getFilterConfiguration().applyTo(inSpec);
    String[] rmFromIncl = filter.getRemovedFromIncludes();
    if (m_settings.getFilterConfiguration().isEnforceInclusion() && rmFromIncl.length != 0) {
        throw new InvalidSettingsException("Input table does not contain the following selected column(s): " + ConvenienceMethods.getShortStringFrom(new HashSet<String>(Arrays.asList(rmFromIncl)), 3));
    }
    m_included = filter.getIncludes();
    if (m_included.length == 0) {
        setWarningMessage("No columns selected.");
    }
    ColumnRearranger rearranger = createRearranger(inSpec);
    PMMLPortObjectSpec pmmlSpec = m_pmmlInEnabled ? (PMMLPortObjectSpec) inSpecs[1] : null;
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inSpec);
    pmmlSpecCreator.addPreprocColNames(inputCols);
    return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ArrayList(java.util.ArrayList) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 44 with FilterResult

use of org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult in project knime-core by knime.

the class ColumnAggregatorNodeDialog method getSuperType.

/**
 * @return the {@link DataType} the selected columns are compatible with
 * or <code>null</code> if none is available or no table exists
 */
private DataType getSuperType() {
    final FilterResult filterResult = m_aggregationCols.applyTo(m_spec);
    final List<String> includeList = Arrays.asList(filterResult.getIncludes());
    if (includeList == null || includeList.isEmpty() || m_spec == null) {
        return null;
    }
    final Set<DataType> types = new HashSet<DataType>(includeList.size());
    final HashSet<String> inclCols = new HashSet<String>(includeList);
    for (final DataColumnSpec colSpec : m_spec) {
        if (inclCols.contains(colSpec.getName())) {
            types.add(colSpec.getType());
        }
    }
    final DataType superType = CollectionCellFactory.getElementType(types.toArray(new DataType[0]));
    return superType;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) HashSet(java.util.HashSet)

Example 45 with FilterResult

use of org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult in project knime-core by knime.

the class EditNumericDomainNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // 
    final DataTableSpec originalTableSpec = inData[0].getDataTableSpec();
    DataTableSpec editedTableSpec = processDomainSettings(inData[0].getDataTableSpec());
    FilterResult filterResult = m_configuration.getColumnspecFilterConfig().applyTo(originalTableSpec);
    List<String> includedColumnNames = Arrays.asList(filterResult.getIncludes());
    long currentRowIndex = 0;
    long size = inData[0].size();
    String[] tableNames = originalTableSpec.getColumnNames();
    Map<Integer, DataColumnSpec> map = new HashMap<Integer, DataColumnSpec>();
    for (int i = 0; i < originalTableSpec.getNumColumns(); i++) {
        if (includedColumnNames.contains(tableNames[i])) {
            map.put(i, editedTableSpec.getColumnSpec(i));
        } else {
            map.put(i, originalTableSpec.getColumnSpec(i));
        }
    }
    CloseableRowIterator rowIterator = inData[0].iterator();
    long rowIndex = 0;
    try {
        while (rowIterator.hasNext()) {
            DataRow currentRow = rowIterator.next();
            // 
            exec.setProgress(// 
            currentRowIndex++ / (double) size, "checking domains of row: " + currentRow.getKey().getString());
            for (String colName : includedColumnNames) {
                int currIndex = originalTableSpec.findColumnIndex(colName);
                DataCell currCell = currentRow.getCell(currIndex);
                if (!currCell.isMissing() && outOfDomain(currCell, map.get(currIndex))) {
                    switch(m_configuration.getDomainOverflowPolicy()) {
                        case CALCULATE_BOUNDS:
                            map.put(currIndex, calculateAndCreateBoundedColumnSpec(currCell, map.get(currIndex)));
                            break;
                        case USE_EXISTING_BOUNDS:
                            map.put(currIndex, originalTableSpec.getColumnSpec(currIndex));
                            break;
                        default:
                            throw new EditNumericDomainOverflowException(tableNames[currIndex], ((DoubleValue) currCell).getDoubleValue(), m_configuration.getLowerBound(), m_configuration.getUpperBound(), rowIndex, currentRow.getKey());
                    }
                }
            }
            exec.checkCanceled();
            rowIndex++;
        }
    } finally {
        rowIterator.close();
    }
    DataTableSpecCreator newTableSpecCreator = new DataTableSpecCreator().setName(originalTableSpec.getName()).putProperties(originalTableSpec.getProperties());
    for (int i = 0; i < originalTableSpec.getNumColumns(); i++) {
        newTableSpecCreator.addColumns(map.get(i));
    }
    return new BufferedDataTable[] { exec.createSpecReplacerTable(inData[0], newTableSpecCreator.createSpec()) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashMap(java.util.HashMap) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)

Aggregations

FilterResult (org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)54 DataTableSpec (org.knime.core.data.DataTableSpec)29 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)29 DataColumnSpec (org.knime.core.data.DataColumnSpec)19 ArrayList (java.util.ArrayList)14 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)13 DataType (org.knime.core.data.DataType)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)10 BufferedDataTable (org.knime.core.node.BufferedDataTable)9 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)6 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)6 HashSet (java.util.HashSet)5 DataCell (org.knime.core.data.DataCell)5 DoubleValue (org.knime.core.data.DoubleValue)5 HashMap (java.util.HashMap)3 DataColumnProperties (org.knime.core.data.DataColumnProperties)3 DataRow (org.knime.core.data.DataRow)3 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)3 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2