Search in sources :

Example 1 with ColorHandler

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

the class ColorExtractNodeModel method extractColorTable.

private DataTable extractColorTable(final DataTableSpec colorSpec) throws InvalidSettingsException {
    // first column has column handler (convention in ColorHandlerPO)
    ColorHandler clrHdl = colorSpec.getColumnSpec(0).getColorHandler();
    final ColorModel model = clrHdl.getColorModel();
    if (model.getClass() == ColorModelNominal.class) {
        ColorModelNominal nom = (ColorModelNominal) model;
        return extractColorTable(nom);
    } else if (model.getClass() == ColorModelRange.class) {
        ColorModelRange range = (ColorModelRange) model;
        return extractColorTable(range);
    } else {
        throw new InvalidSettingsException("Unknown ColorModel class: " + model.getClass());
    }
}
Also used : ColorModelRange(org.knime.core.data.property.ColorModelRange) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColorModel(org.knime.core.data.property.ColorHandler.ColorModel) ColorModelNominal(org.knime.core.data.property.ColorModelNominal) ColorHandler(org.knime.core.data.property.ColorHandler)

Example 2 with ColorHandler

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

the class DataColumnSpecCreator method merge.

/**
 * Merges the existing {@link DataColumnSpec} with a second
 * {@link DataColumnSpec}. If they have equal structure, the domain
 * information and properties from both DataColumnSpecs is merged,
 * Color, Shape and Size-Handlers are compared (must be equal).
 *
 * @param cspec2 the second {@link DataColumnSpec}.
 *
 * @see DataTableSpec#mergeDataTableSpecs(DataTableSpec...)
 * @throws IllegalArgumentException if the structure (type and name) does
 *             not match, if the domain cannot be merged, if the Color-,
 *             Shape- or SizeHandlers are different or the sub element
 *             names are not equal.
 */
public void merge(final DataColumnSpec cspec2) {
    if (!cspec2.getName().equals(m_name) || !cspec2.getType().equals(m_type)) {
        throw new IllegalArgumentException("Structures of DataColumnSpecs" + " do not match.");
    }
    DataColumnDomain domain2 = cspec2.getDomain();
    boolean hasDomainChanged = false;
    final Set<DataCell> myValues = m_domain.getValues();
    final Set<DataCell> oValues = domain2.getValues();
    Set<DataCell> newValues;
    if (myValues == null || oValues == null) {
        newValues = null;
        hasDomainChanged |= myValues != null;
    } else if (myValues.equals(oValues)) {
        newValues = myValues;
    } else {
        newValues = new LinkedHashSet<DataCell>(myValues);
        newValues.addAll(oValues);
        hasDomainChanged = true;
    }
    DataValueComparator comparator = m_type.getComparator();
    final DataCell myLower = m_domain.getLowerBound();
    final DataCell oLower = domain2.getLowerBound();
    DataCell newLower;
    if (myLower == null || oLower == null) {
        newLower = null;
        hasDomainChanged |= myLower != null;
    } else if (myLower.equals(oLower)) {
        newLower = myLower;
    } else if (comparator.compare(myLower, oLower) > 0) {
        newLower = oLower;
        hasDomainChanged = true;
    } else {
        newLower = myLower;
    }
    final DataCell myUpper = m_domain.getUpperBound();
    final DataCell oUpper = domain2.getUpperBound();
    DataCell newUpper;
    if (myUpper == null || oUpper == null) {
        newUpper = null;
        hasDomainChanged |= myUpper != null;
    } else if (myUpper.equals(oUpper)) {
        newUpper = myUpper;
    } else if (comparator.compare(myUpper, oUpper) < 0) {
        newUpper = oUpper;
        hasDomainChanged = true;
    } else {
        newUpper = myUpper;
    }
    if (hasDomainChanged) {
        setDomain(new DataColumnDomain(newLower, newUpper, newValues));
    }
    // check for redundant color handler
    ColorHandler colorHandler2 = cspec2.getColorHandler();
    if (!Objects.equals(m_colorHandler, colorHandler2)) {
        LOGGER.warn("Column has already a color handler attached, ignoring new handler.");
    }
    // check for redundant shape handler
    ShapeHandler shapeHandler2 = cspec2.getShapeHandler();
    if (!Objects.equals(m_shapeHandler, shapeHandler2)) {
        LOGGER.warn("Column has already a shape handler attached, ignoring new handler.");
    }
    // check for redundant size handler
    SizeHandler sizeHandler2 = cspec2.getSizeHandler();
    if (!Objects.equals(m_sizeHandler, sizeHandler2)) {
        LOGGER.warn("Column has already a size handler attached, ignoring new handler.");
    }
    // check for redundant filter handler
    FilterHandler filterHandler = cspec2.getFilterHandler().orElse(null);
    if (!Objects.equals(m_filterHandler, filterHandler)) {
        LOGGER.warn("Column has already a filter handler attached, ignoring new handler.");
    }
    // merge properties, take intersection
    DataColumnProperties prop2 = cspec2.getProperties();
    Map<String, String> mergedProps = new HashMap<String, String>();
    Enumeration<String> e = m_properties.properties();
    while (e.hasMoreElements()) {
        String key = e.nextElement();
        String value = m_properties.getProperty(key);
        if (prop2.getProperty(key) != null && prop2.getProperty(key).equals(value)) {
            mergedProps.put(key, value);
        }
    }
    if (mergedProps.size() != m_properties.size()) {
        setProperties(new DataColumnProperties(mergedProps));
    }
    List<String> elNames2 = cspec2.getElementNames();
    String[] elNames2Array = elNames2.toArray(new String[elNames2.size()]);
    String[] elNamesArray = m_elementNames == null ? new String[] { m_name } : m_elementNames;
    if (!Arrays.deepEquals(elNamesArray, elNames2Array)) {
        throw new IllegalArgumentException("Element names are not equal");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ShapeHandler(org.knime.core.data.property.ShapeHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler) SizeHandler(org.knime.core.data.property.SizeHandler) ColorHandler(org.knime.core.data.property.ColorHandler)

Example 3 with ColorHandler

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

the class ColorManager2NodeModel method createRangeColorHandler.

private static final ColorHandler createRangeColorHandler(final DataCell lower, final DataCell upper, final Map<DataCell, ColorAttr> map) {
    assert map.size() == 2;
    Color c0 = map.get(MIN_VALUE).getColor();
    Color c1 = map.get(MAX_VALUE).getColor();
    double d0 = Double.NaN;
    if (lower != null && !lower.isMissing() && lower.getType().isCompatible(DoubleValue.class)) {
        d0 = ((DoubleValue) lower).getDoubleValue();
    }
    double d1 = Double.NaN;
    if (upper != null && !upper.isMissing() && upper.getType().isCompatible(DoubleValue.class)) {
        d1 = ((DoubleValue) upper).getDoubleValue();
    }
    return new ColorHandler(new ColorModelRange(d0, c0, d1, c1));
}
Also used : ColorModelRange(org.knime.core.data.property.ColorModelRange) DoubleValue(org.knime.core.data.DoubleValue) Color(java.awt.Color) ColorHandler(org.knime.core.data.property.ColorHandler)

Example 4 with ColorHandler

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

the class DataColumnSpec method load.

/**
 * Reads name, type, domain, and properties from the given
 * <code>ConfigRO</code> and - if available - size, shape, and color
 * handler. Returns a new <code>DataColumnSpec</code> object initialized
 * with the information read.
 *
 * @param config to read properties from
 * @return a new column spec object
 * @throws InvalidSettingsException if one of the non-optional properties is
 *             not available or can't be initialized
 * @throws NullPointerException if the config object is <code>null</code>
 */
public static DataColumnSpec load(final ConfigRO config) throws InvalidSettingsException {
    String name = config.getString(CFG_COLUMN_NAME);
    String[] elNames = config.getStringArray(CFG_ELEMENT_NAMES, (String[]) null);
    if (elNames == null) {
        elNames = new String[] { name };
    }
    DataType type = DataType.load(config.getConfig(CFG_COLUMN_TYPE));
    DataColumnDomain domain = DataColumnDomain.load(config.getConfig(CFG_COLUMN_DOMAIN));
    DataColumnProperties properties = DataColumnProperties.load(config.getConfig(CFG_COLUMN_PROPS));
    ColorHandler color = null;
    if (config.containsKey(CFG_COLORS)) {
        color = ColorHandler.load(config.getConfig(CFG_COLORS));
    }
    SizeHandler size = null;
    if (config.containsKey(CFG_SIZES)) {
        size = SizeHandler.load(config.getConfig(CFG_SIZES));
    }
    ShapeHandler shape = null;
    if (config.containsKey(CFG_SHAPES)) {
        shape = ShapeHandler.load(config.getConfig(CFG_SHAPES));
    }
    FilterHandler filter = null;
    if (config.containsKey(CFG_FILTER)) {
        filter = FilterHandler.load(config.getConfig(CFG_FILTER));
    }
    final DataColumnMetaDataManager metaDataManager;
    if (config.containsKey(CFG_META_DATA)) {
        metaDataManager = DataColumnMetaDataManager.load(config.getConfig(CFG_META_DATA));
    } else {
        // create an empty meta data object to avoid issues with NPEs
        metaDataManager = DataColumnMetaDataManager.EMPTY;
    }
    return new DataColumnSpec(name, elNames, type, domain, properties, size, color, shape, filter, metaDataManager);
}
Also used : SizeHandler(org.knime.core.data.property.SizeHandler) ShapeHandler(org.knime.core.data.property.ShapeHandler) ColorHandler(org.knime.core.data.property.ColorHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler)

Example 5 with ColorHandler

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

the class ColorManager2NodeModel method configure.

/**
 * @param inSpecs the input specs passed to the output port
 * @return the same as the input spec
 *
 * @throws InvalidSettingsException if a column is not available
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec spec = (DataTableSpec) inSpecs[INPORT];
    if (spec == null) {
        throw new InvalidSettingsException("No input");
    }
    // check null column
    if (m_column == null) {
        // find first nominal column with possible values
        String column = DataTableSpec.guessNominalClassColumn(spec, false);
        if (column == null) {
            throw new InvalidSettingsException("No column selected and no categorical column available.");
        }
        m_columnGuess = column;
        m_isNominalGuess = true;
        Set<DataCell> set = spec.getColumnSpec(column).getDomain().getValues();
        m_mapGuess.clear();
        m_mapGuess.putAll(ColorManager2DialogNominal.createColorMapping(set));
        ColorHandler colorHandler = createNominalColorHandler(m_mapGuess);
        DataTableSpec dataSpec = getOutSpec(spec, column, colorHandler);
        DataTableSpec modelSpec = new DataTableSpec(dataSpec.getColumnSpec(column));
        super.setWarningMessage("Selected column \"" + column + "\" with default nominal color mapping.");
        return new DataTableSpec[] { dataSpec, modelSpec };
    }
    // check column in spec
    if (!spec.containsName(m_column)) {
        throw new InvalidSettingsException("Column \"" + m_column + "\" not found.");
    }
    // get domain
    DataColumnDomain domain = spec.getColumnSpec(m_column).getDomain();
    // either set colors by ranges or discrete values
    if (m_isNominal) {
        // check if all values set are in the domain of the column spec
        Set<DataCell> list = domain.getValues();
        if (list == null) {
            throw new InvalidSettingsException("Column \"" + m_column + "\"" + " has no nominal values set: " + "execute predecessor or add Binner.");
        }
        // check if the mapping values and the possible values match
        if (!m_map.keySet().containsAll(list)) {
            throw new InvalidSettingsException("Color mapping does not match possible values.");
        }
    } else {
        // check if double column is selected
        if (!spec.getColumnSpec(m_column).getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Column is not valid for" + " range color settings: " + spec.getColumnSpec(m_column).getType());
        }
        // check map
        if (m_map.size() != 2) {
            throw new InvalidSettingsException("Color settings not yet available.");
        }
    }
    // temp color handler
    ColorHandler colorHandler;
    // create new column spec based on color settings
    DataColumnSpec cspec = spec.getColumnSpec(m_column);
    if (m_isNominal) {
        colorHandler = createNominalColorHandler(m_map);
    } else {
        DataColumnDomain dom = cspec.getDomain();
        DataCell lower = null;
        DataCell upper = null;
        if (dom.hasBounds()) {
            lower = dom.getLowerBound();
            upper = dom.getUpperBound();
        }
        colorHandler = createRangeColorHandler(lower, upper, m_map);
    }
    DataTableSpec dataSpec = getOutSpec(spec, m_column, colorHandler);
    DataTableSpec modelSpec = new DataTableSpec(dataSpec.getColumnSpec(m_column));
    return new DataTableSpec[] { dataSpec, modelSpec };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) ColorHandler(org.knime.core.data.property.ColorHandler)

Aggregations

ColorHandler (org.knime.core.data.property.ColorHandler)8 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 DataCell (org.knime.core.data.DataCell)3 DataColumnDomain (org.knime.core.data.DataColumnDomain)3 ShapeHandler (org.knime.core.data.property.ShapeHandler)3 SizeHandler (org.knime.core.data.property.SizeHandler)3 FilterHandler (org.knime.core.data.property.filter.FilterHandler)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 DoubleValue (org.knime.core.data.DoubleValue)2 ColorModelRange (org.knime.core.data.property.ColorModelRange)2 Color (java.awt.Color)1 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 Statistics3Table (org.knime.base.data.statistics.Statistics3Table)1