Search in sources :

Example 1 with ColumnFilter

use of org.knime.core.node.util.ColumnFilter in project knime-core by knime.

the class AbstractHistogramNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if (inSpecs == null || inSpecs[0] == null) {
        throw new InvalidSettingsException("No input specification available.");
    }
    final DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    if (tableSpec == null || tableSpec.getNumColumns() < 1) {
        throw new InvalidSettingsException("Input table should have at least 1 column.");
    }
    if (!tableSpec.containsName(m_xColName.getStringValue())) {
        // if the input table has only two columns where only one column
        // is numerical select these two columns as default columns
        // if both are numeric we don't know which one the user wants as
        // aggregation column and which one as x column
        final ColumnFilter xFilter = AbstractHistogramPlotter.X_COLUMN_FILTER;
        final ColumnFilter aggrFilter = AbstractHistogramPlotter.AGGREGATION_COLUMN_FILTER;
        if (tableSpec.getNumColumns() == 1) {
            final DataColumnSpec columnSpec0 = tableSpec.getColumnSpec(0);
            if (xFilter.includeColumn(columnSpec0)) {
                m_xColName.setStringValue(columnSpec0.getName());
            } else {
                throw new InvalidSettingsException("No column compatible with this node. Column needs to " + "be nominal or numeric and must contain a valid " + "domain. In order to compute the domain of a column " + "use the DomainCalculator or ColumnFilter node.");
            }
        } else if (tableSpec.getNumColumns() == 2) {
            final DataColumnSpec columnSpec0 = tableSpec.getColumnSpec(0);
            final DataColumnSpec columnSpec1 = tableSpec.getColumnSpec(1);
            final DataType type0 = columnSpec0.getType();
            final DataType type1 = columnSpec1.getType();
            if (type0.isCompatible(StringValue.class) && type1.isCompatible(DoubleValue.class) && xFilter.includeColumn(columnSpec0) && aggrFilter.includeColumn(columnSpec1)) {
                m_xColName.setStringValue(tableSpec.getColumnSpec(0).getName());
                m_aggrColName.setColorNameColumns(new ColorColumn(Color.lightGray, tableSpec.getColumnSpec(1).getName()));
            } else if (type0.isCompatible(DoubleValue.class) && type1.isCompatible(StringValue.class) && xFilter.includeColumn(columnSpec1) && aggrFilter.includeColumn(columnSpec0)) {
                m_xColName.setStringValue(tableSpec.getColumnSpec(1).getName());
                m_aggrColName.setColorNameColumns(new ColorColumn(Color.lightGray, tableSpec.getColumnSpec(0).getName()));
            } else {
                throw new InvalidSettingsException("Please define the binning column.");
            }
        } else {
            throw new InvalidSettingsException("Please define the binning column.");
        }
    }
    // check if the aggregation columns are available and valid
    final ColorColumn[] aggrColNames = m_aggrColName.getColorNameColumns();
    if (aggrColNames != null) {
        for (final ColorColumn col : aggrColNames) {
            final DataColumnSpec cSpec = tableSpec.getColumnSpec(col.getColumnName());
            if (cSpec == null) {
                throw new InvalidSettingsException("Aggregation column '" + col.getColumnName() + "' not found in input table");
            } else if (!AbstractHistogramPlotter.AGGREGATION_COLUMN_FILTER.includeColumn(cSpec)) {
                throw new InvalidSettingsException("Aggregation column '" + col.getColumnName() + "' is not numeric " + "(not double compatible) or has an invalid domain");
            }
        }
    }
    final ColumnFilter filter = NoDomainColumnFilter.getInstance();
    m_xColSpec = tableSpec.getColumnSpec(m_xColName.getStringValue());
    // check that the selected x column has a valid domain
    if (!filter.includeColumn(m_xColSpec)) {
        throw new InvalidSettingsException("Binning column has invalid or no domain");
    }
    // check if the table contains value which don't have a valid domain
    // and display a warning that they are ignored
    final int numColumns = tableSpec.getNumColumns();
    final List<DataColumnSpec> invalidCols = new ArrayList<DataColumnSpec>(numColumns);
    for (int i = 0; i < numColumns; i++) {
        final DataColumnSpec columnSpec = tableSpec.getColumnSpec(i);
        if (!filter.includeColumn(columnSpec)) {
            invalidCols.add(columnSpec);
        }
    }
    if (invalidCols.size() > 0) {
        final StringBuilder buf = new StringBuilder();
        if (invalidCols.size() == 1) {
            buf.append("Column ");
            buf.append(invalidCols.get(0).getName());
            buf.append(" contains no valid domain an will be ignored.");
        } else {
            buf.append(invalidCols.size());
            buf.append(" columns without a valid domain will be ignored.");
        }
        buf.append(" In order to calculate the domain use the" + " Nominal Values or Domain Calculator node.");
        setWarningMessage(buf.toString());
    }
    return new DataTableSpec[0];
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) ArrayList(java.util.ArrayList) ColumnFilter(org.knime.core.node.util.ColumnFilter) NoDomainColumnFilter(org.knime.base.node.viz.histogram.util.NoDomainColumnFilter) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) StringValue(org.knime.core.data.StringValue)

Example 2 with ColumnFilter

use of org.knime.core.node.util.ColumnFilter in project knime-core by knime.

the class ColumnHeaderExtractorNodeModel method createOutputSpecInternal.

private DataTableSpec createOutputSpecInternal(final DataTableSpec in, final boolean includeIgnoredCols) throws InvalidSettingsException {
    ColType colType = getColType();
    ColumnFilter filter = colType.getFilter();
    final String namePrefix = m_unifyHeaderPrefix.getStringValue();
    HashSet<String> usedNames = new HashSet<String>();
    for (DataColumnSpec c : in) {
        if (!filter.includeColumn(c)) {
            // only add remaining columns
            usedNames.add(c.getName());
        }
    }
    List<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
    // re-use index in loop - prevent repeated adds to the hash set - fixes bug 5920
    int index = 0;
    for (DataColumnSpec c : in) {
        if (filter.includeColumn(c)) {
            String newName;
            do {
                newName = namePrefix + (index++);
            } while (!usedNames.add(newName));
            DataColumnSpecCreator newSpecCreator = new DataColumnSpecCreator(c);
            newSpecCreator.setName(newName);
            colSpecs.add(newSpecCreator.createSpec());
        } else if (includeIgnoredCols) {
            colSpecs.add(c);
        }
    }
    return new DataTableSpec(in.getName(), colSpecs.toArray(new DataColumnSpec[colSpecs.size()]));
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) ColumnFilter(org.knime.core.node.util.ColumnFilter) DataValueColumnFilter(org.knime.core.node.util.DataValueColumnFilter) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) HashSet(java.util.HashSet)

Example 3 with ColumnFilter

use of org.knime.core.node.util.ColumnFilter in project knime-core by knime.

the class ColumnHeaderExtractorNodeModel method createOutSpecPort0.

private DataTableSpec createOutSpecPort0(final DataTableSpec spec) throws InvalidSettingsException {
    DataTableSpec temp;
    if (m_replaceColHeader.getBooleanValue()) {
        temp = createOutputSpecInternal(spec, false);
    } else {
        ColumnFilter filter = getColType().getFilter();
        List<String> includes = new ArrayList<String>();
        for (DataColumnSpec colSpec : spec) {
            if (filter.includeColumn(colSpec)) {
                includes.add(colSpec.getName());
            }
        }
        temp = FilterColumnTable.createFilterTableSpec(spec, includes.toArray(new String[includes.size()]));
    }
    DataColumnSpec[] cols = new DataColumnSpec[temp.getNumColumns()];
    for (int i = 0; i < cols.length; i++) {
        // don't use input as basis here, throw away handlers etc.
        DataColumnSpecCreator creator = new DataColumnSpecCreator(temp.getColumnSpec(i).getName(), StringCell.TYPE);
        cols[i] = creator.createSpec();
    }
    return new DataTableSpec("Column Headers", cols);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) ColumnFilter(org.knime.core.node.util.ColumnFilter) DataValueColumnFilter(org.knime.core.node.util.DataValueColumnFilter) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 4 with ColumnFilter

use of org.knime.core.node.util.ColumnFilter in project knime-core by knime.

the class ColumnHeaderExtractorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    DataTableSpec inSpec = inData[0].getDataTableSpec();
    DataTableSpec spec0 = createOutSpecPort0(inSpec);
    DataTableSpec spec1 = createOutSpecPort1(inSpec);
    BufferedDataContainer cont = exec.createDataContainer(spec0);
    List<String> origColNames = new ArrayList<String>();
    ColumnFilter filter = getColType().getFilter();
    for (DataColumnSpec c : inSpec) {
        if (filter.includeColumn(c)) {
            origColNames.add(c.getName());
        }
    }
    DefaultRow row = new DefaultRow("Column Header", origColNames.toArray(new String[origColNames.size()]));
    cont.addRowToTable(row);
    cont.close();
    BufferedDataTable table0 = cont.getTable();
    BufferedDataTable table1 = exec.createSpecReplacerTable(inData[0], spec1);
    return new BufferedDataTable[] { table0, table1 };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ArrayList(java.util.ArrayList) BufferedDataTable(org.knime.core.node.BufferedDataTable) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ColumnFilter(org.knime.core.node.util.ColumnFilter) DataValueColumnFilter(org.knime.core.node.util.DataValueColumnFilter) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

ArrayList (java.util.ArrayList)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 ColumnFilter (org.knime.core.node.util.ColumnFilter)4 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 DataValueColumnFilter (org.knime.core.node.util.DataValueColumnFilter)3 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 HashSet (java.util.HashSet)1 ColorColumn (org.knime.base.node.viz.histogram.util.ColorColumn)1 NoDomainColumnFilter (org.knime.base.node.viz.histogram.util.NoDomainColumnFilter)1 DataType (org.knime.core.data.DataType)1 DoubleValue (org.knime.core.data.DoubleValue)1 StringValue (org.knime.core.data.StringValue)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1