Search in sources :

Example 6 with SizeHandler

use of org.knime.core.data.property.SizeHandler in project knime-core by knime.

the class SizeAppenderNodeModel method createOutputSpec.

private DataTableSpec createOutputSpec(final DataTableSpec modelSpec, final DataTableSpec dataSpec) throws InvalidSettingsException {
    if (modelSpec == null || dataSpec == null) {
        throw new InvalidSettingsException("Invalid input.");
    }
    if (modelSpec.getNumColumns() < 1) {
        throw new InvalidSettingsException("No size information in input");
    }
    DataColumnSpec col = modelSpec.getColumnSpec(0);
    SizeHandler sizeHandler = col.getSizeHandler();
    if (col.getSizeHandler() == null) {
        throw new InvalidSettingsException("No size information in input");
    }
    String column = m_column.getStringValue();
    if (column == null) {
        // auto-configuration/guessing
        if (dataSpec.containsName(col.getName())) {
            column = col.getName();
        }
    }
    if (column == null) {
        throw new InvalidSettingsException("Not configured.");
    }
    if (!dataSpec.containsName(column)) {
        throw new InvalidSettingsException("Column \"" + column + "\" not available.");
    }
    DataTableSpec spec = SizeManager2NodeModel.appendSizeHandler(dataSpec, column, sizeHandler);
    return spec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SizeHandler(org.knime.core.data.property.SizeHandler) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 7 with SizeHandler

use of org.knime.core.data.property.SizeHandler in project knime-core by knime.

the class DataTableSpecExtractor method extract.

/**
 * Creates and returns a data table containing the meta information of the given spec. The meta information is
 * referred to as the table data specification and contains information such as column names, types, domain values
 * (list of possible values for categorical columns) and lower and upper bounds. It also contains the information
 * which of the columns have a view handler associated, as well the possible values, if specified. Each column in
 * the given table spec is represented as a row in the returned table.
 *
 * @param spec The spec to extract the meta information from.
 * @return The data table containing the meta information of the given spec.
 * @since 4.2
 */
public CloseableTable extract(final DataTableSpec spec) {
    List<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
    if (m_extractColumnNameAsColumn) {
        colSpecs.add(new DataColumnSpecCreator("Column Name", StringCell.TYPE).createSpec());
    }
    colSpecs.add(new DataColumnSpecCreator("Column Type", StringCell.TYPE).createSpec());
    colSpecs.add(new DataColumnSpecCreator("Column Index", IntCell.TYPE).createSpec());
    switch(m_propertyHandlerOutputFormat) {
        case Hide:
            break;
        case Boolean:
            colSpecs.add(new DataColumnSpecCreator("Color Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Size Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Shape Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Filter Handler", BooleanCell.TYPE).createSpec());
            break;
        default:
            colSpecs.add(new DataColumnSpecCreator("Color Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Size Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Shape Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Filter Handler", StringCell.TYPE).createSpec());
    }
    // likely number (important for sorting)
    DataType lowerBoundColType = null;
    DataType upperBoundColType = null;
    for (DataColumnSpec c : spec) {
        DataColumnDomain domain = c.getDomain();
        if (domain.hasLowerBound()) {
            DataType curLowerType = domain.getLowerBound().getType();
            if (lowerBoundColType == null) {
                lowerBoundColType = curLowerType;
            } else {
                lowerBoundColType = DataType.getCommonSuperType(lowerBoundColType, curLowerType);
            }
        }
        if (domain.hasUpperBound()) {
            DataType curUpperType = domain.getUpperBound().getType();
            if (upperBoundColType == null) {
                upperBoundColType = curUpperType;
            } else {
                upperBoundColType = DataType.getCommonSuperType(upperBoundColType, curUpperType);
            }
        }
    }
    lowerBoundColType = lowerBoundColType == null ? GENERIC_DATA_TYPE : lowerBoundColType;
    upperBoundColType = upperBoundColType == null ? GENERIC_DATA_TYPE : upperBoundColType;
    colSpecs.add(new DataColumnSpecCreator("Lower Bound", lowerBoundColType).createSpec());
    colSpecs.add(new DataColumnSpecCreator("Upper Bound", upperBoundColType).createSpec());
    int maxPossValues = 0;
    switch(m_possibleValueOutputFormat) {
        case Hide:
            break;
        case Collection:
            colSpecs.add(new DataColumnSpecCreator("Possible Values", SetCell.getCollectionType(GENERIC_DATA_TYPE)).createSpec());
            break;
        default:
            for (DataColumnSpec c : spec) {
                DataColumnDomain domain = c.getDomain();
                if (domain.hasValues()) {
                    maxPossValues = Math.max(domain.getValues().size(), maxPossValues);
                }
            }
            for (int i = 0; i < maxPossValues; i++) {
                colSpecs.add(new DataColumnSpecCreator("Value " + i, GENERIC_DATA_TYPE).createSpec());
            }
    }
    /* fill it */
    DataContainer dc = new DataContainer(new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()])));
    for (int i = 0; i < spec.getNumColumns(); i++) {
        DataColumnSpec colSpec = spec.getColumnSpec(i);
        List<DataCell> cells = new ArrayList<DataCell>();
        if (m_extractColumnNameAsColumn) {
            cells.add(new StringCell(colSpec.getName()));
        }
        cells.add(new StringCell(colSpec.getType().toString()));
        cells.add(new IntCell(i));
        ColorHandler colorHandler = colSpec.getColorHandler();
        SizeHandler sizeHandler = colSpec.getSizeHandler();
        ShapeHandler shapeHandler = colSpec.getShapeHandler();
        Optional<FilterHandler> filterHandler = colSpec.getFilterHandler();
        switch(m_propertyHandlerOutputFormat) {
            case Hide:
                break;
            case Boolean:
                cells.add(BooleanCellFactory.create(colorHandler != null));
                cells.add(BooleanCellFactory.create(sizeHandler != null));
                cells.add(BooleanCellFactory.create(shapeHandler != null));
                cells.add(BooleanCellFactory.create(filterHandler.isPresent()));
                break;
            default:
                cells.add(new StringCell(colorHandler == null ? "" : colorHandler.toString()));
                cells.add(new StringCell(sizeHandler == null ? "" : sizeHandler.toString()));
                cells.add(new StringCell(shapeHandler == null ? "" : shapeHandler.toString()));
                cells.add(new StringCell(filterHandler.map(f -> f.toString()).orElse("")));
        }
        DataColumnDomain domain = colSpec.getDomain();
        DataCell lb = domain.getLowerBound();
        if (lb != null) {
            cells.add(lb);
        } else {
            cells.add(DataType.getMissingCell());
        }
        DataCell ub = domain.getUpperBound();
        if (ub != null) {
            cells.add(ub);
        } else {
            cells.add(DataType.getMissingCell());
        }
        switch(m_possibleValueOutputFormat) {
            case Hide:
                break;
            case Collection:
                if (domain.hasValues()) {
                    cells.add(CollectionCellFactory.createSetCell(domain.getValues()));
                } else {
                    cells.add(DataType.getMissingCell());
                }
                break;
            default:
                Set<DataCell> set = domain.hasValues() ? domain.getValues() : Collections.EMPTY_SET;
                int nrColsToWrite = maxPossValues;
                for (DataCell c : set) {
                    cells.add(c);
                    nrColsToWrite -= 1;
                }
                while (nrColsToWrite > 0) {
                    cells.add(DataType.getMissingCell());
                    nrColsToWrite -= 1;
                }
        }
        dc.addRowToTable(new DefaultRow(new RowKey(colSpec.getName()), cells));
    }
    dc.close();
    return dc.getCloseableTable();
}
Also used : FilterHandler(org.knime.core.data.property.filter.FilterHandler) RowKey(org.knime.core.data.RowKey) DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataCell(org.knime.core.data.DataCell) ContainerTable(org.knime.core.data.container.ContainerTable) ColorHandler(org.knime.core.data.property.ColorHandler) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) ShapeHandler(org.knime.core.data.property.ShapeHandler) SetCell(org.knime.core.data.collection.SetCell) Set(java.util.Set) SizeHandler(org.knime.core.data.property.SizeHandler) CollectionCellFactory(org.knime.core.data.collection.CollectionCellFactory) DataColumnDomain(org.knime.core.data.DataColumnDomain) CloseableTable(org.knime.core.data.container.CloseableTable) List(java.util.List) BooleanCell(org.knime.core.data.def.BooleanCell) BooleanCellFactory(org.knime.core.data.def.BooleanCell.BooleanCellFactory) Optional(java.util.Optional) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) Collections(java.util.Collections) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) ArrayList(java.util.ArrayList) ShapeHandler(org.knime.core.data.property.ShapeHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler) IntCell(org.knime.core.data.def.IntCell) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) StringCell(org.knime.core.data.def.StringCell) SizeHandler(org.knime.core.data.property.SizeHandler) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) ColorHandler(org.knime.core.data.property.ColorHandler)

Aggregations

SizeHandler (org.knime.core.data.property.SizeHandler)7 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 ColorHandler (org.knime.core.data.property.ColorHandler)3 ShapeHandler (org.knime.core.data.property.ShapeHandler)3 FilterHandler (org.knime.core.data.property.filter.FilterHandler)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 DataCell (org.knime.core.data.DataCell)1 DataColumnDomain (org.knime.core.data.DataColumnDomain)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataType (org.knime.core.data.DataType)1 DoubleValue (org.knime.core.data.DoubleValue)1