Search in sources :

Example 11 with DataTableSpecCreator

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

the class EditNumericDomainNodeModel method processDomainSettings.

private DataTableSpec processDomainSettings(final DataTableSpec dataTableSpec) throws InvalidSettingsException {
    if (m_configuration == null) {
        throw new InvalidSettingsException("Missing Configuration.");
    }
    EditNumericDomainConfiguration config = m_configuration;
    FilterResult filterResult = config.getColumnspecFilterConfig().applyTo(dataTableSpec);
    List<DataColumnSpec> newColumnSpecs = new ArrayList<DataColumnSpec>(dataTableSpec.getNumColumns());
    String[] columnNames = dataTableSpec.getColumnNames();
    Set<String> includeSet = new HashSet<String>();
    Collections.addAll(includeSet, filterResult.getIncludes());
    for (int i = 0; i < dataTableSpec.getNumColumns(); i++) {
        DataColumnSpec columnSpec = dataTableSpec.getColumnSpec(i);
        String columnName = columnNames[i];
        if (includeSet.contains(columnName)) {
            DataColumnSpecCreator columnSpecCreator = new DataColumnSpecCreator(columnSpec);
            DataColumnDomainCreator domainCreator = new // 
            DataColumnDomainCreator(// 
            createCell(columnName, columnSpec.getType(), config.getLowerBound()), createCell(columnName, columnSpec.getType(), config.getUpperBound()));
            domainCreator.setValues(columnSpec.getDomain().getValues());
            columnSpecCreator.setDomain(domainCreator.createDomain());
            newColumnSpecs.add(columnSpecCreator.createSpec());
        } else {
            newColumnSpecs.add(columnSpec);
        }
    }
    StringBuilder warnings = new StringBuilder();
    if (includeSet.isEmpty()) {
        warnings.append("No columns are included.");
    }
    if (filterResult.getRemovedFromIncludes().length > 0) {
        warnings.append("\nFollowing columns are configured but no longer exist: " + ConvenienceMethods.getShortStringFrom(Arrays.asList(filterResult.getRemovedFromIncludes()), 5));
    }
    if (warnings.length() > 0) {
        setWarningMessage(warnings.toString());
    }
    return new DataTableSpecCreator(dataTableSpec).dropAllColumns().addColumns(newColumnSpecs.toArray(new DataColumnSpec[newColumnSpecs.size()])).createSpec();
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) HashSet(java.util.HashSet)

Example 12 with DataTableSpecCreator

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

the class PMMLKNNNodeModel method createSpec.

/**
 * @param dataTableSpec
 * @return
 * @throws InvalidSettingsException when the input table contains invalid columns
 */
private PMMLPortObjectSpec createSpec(final DataTableSpec dataTableSpec) throws InvalidSettingsException {
    List<DataColumnSpec> learningColumns = new ArrayList<DataColumnSpec>();
    DataTableSpecCreator dataDictCreator = new DataTableSpecCreator();
    String[] selectedColumns = m_learningColumns.applyTo(dataTableSpec).getIncludes();
    if (selectedColumns.length == 0) {
        throw new InvalidSettingsException("No learning columns are selected.");
    }
    for (String lc : selectedColumns) {
        DataColumnSpec cs = dataTableSpec.getColumnSpec(lc);
        dataDictCreator.addColumns(cs);
        learningColumns.add(cs);
    }
    if (m_predColumnName.getStringValue() == null || !dataTableSpec.containsName(m_predColumnName.getStringValue())) {
        for (int i = 0; i < dataTableSpec.getNumColumns(); i++) {
            DataColumnSpec cspec = dataTableSpec.getColumnSpec(i);
            if (cspec.getType().isCompatible(StringValue.class)) {
                m_predColumnName.setStringValue(cspec.getName());
                setWarningMessage("No target column selected. Using \"" + cspec.getName() + "\".");
                break;
            }
        }
    }
    if (m_predColumnName.getStringValue() == null) {
        throw new InvalidSettingsException("The table does not contain a suitable target column.");
    }
    dataDictCreator.addColumns(dataTableSpec.getColumnSpec(m_predColumnName.getStringValue()));
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(dataDictCreator.createSpec());
    specCreator.setTargetCol(dataTableSpec.getColumnSpec(m_predColumnName.getStringValue()));
    specCreator.setLearningCols(learningColumns);
    return specCreator.createSpec();
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator) ArrayList(java.util.ArrayList) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 13 with DataTableSpecCreator

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

the class ColumnSelectionSearchableListPanel method update.

/**
 * Clears the current list, adds the {@link DataColumnSpec}s of the given spec and invalidates the current
 * {@link ListModifier}. The {@link DataColumnSpec}s added by this function cannot be deleted from list using the
 * {@link ListModifier}.
 *
 * @param specs containing the immutable part of the column list
 * @return a ListModifier for managing the mutable part of the column list
 */
public ListModifier update(final Iterable<DataColumnSpec> specs) {
    m_lastSearchHits.clear();
    setUnkownFilterVisible(false);
    m_columnList.update(specs);
    if (m_currentModifier != null) {
        m_currentModifier.invalidate();
    }
    DataTableSpecCreator dataTableSpecCreator = new DataTableSpecCreator();
    for (DataColumnSpec spec : specs) {
        dataTableSpecCreator.addColumns(spec);
    }
    m_currentModifier = new ModifierImpl(this, dataTableSpecCreator.createSpec());
    return m_currentModifier;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator)

Example 14 with DataTableSpecCreator

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

the class MissingCellReplacingDataTable method createSpec.

private DataTableSpec createSpec() {
    DataTableSpecCreator specCreator = new DataTableSpecCreator();
    for (MissingCellHandler handler : m_handlers) {
        DataColumnSpecCreator c = new DataColumnSpecCreator(handler.getColumnSpec());
        if (handler.getOutputDataType() != null) {
            c.setType(handler.getOutputDataType());
        }
        specCreator.addColumns(c.createSpec());
    }
    return specCreator.createSpec();
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator)

Example 15 with DataTableSpecCreator

use of org.knime.core.data.DataTableSpecCreator 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

DataTableSpecCreator (org.knime.core.data.DataTableSpecCreator)20 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 DataTableSpec (org.knime.core.data.DataTableSpec)6 DataCell (org.knime.core.data.DataCell)4 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)4 ArrayList (java.util.ArrayList)3 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 DefaultRow (org.knime.core.data.def.DefaultRow)2 DoubleCell (org.knime.core.data.def.DoubleCell)2 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 FilterResult (org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 HistogramColumn (org.knime.base.data.statistics.HistogramColumn)1 BooleanValue (org.knime.core.data.BooleanValue)1