Search in sources :

Example 31 with DataColumnDomain

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

the class ColorManager2NodeDialogPane method loadSettingsFrom.

/**
 * Updates this dialog by refreshing all components in the color tab. Inits the column name combo box and sets the
 * values for the default selected one.
 *
 * @param settings the settings to load
 * @param specs the input table specs
 * @throws NotConfigurableException if no column found for color selection
 * @see NodeDialogPane#loadSettingsFrom(NodeSettingsRO, DataTableSpec[])
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs) throws NotConfigurableException {
    // remove all columns
    m_columns.removeItemListener(this);
    m_columns.removeAllItems();
    // reset nominal and range panel
    m_nominal.removeAllElements();
    m_range.removeAllElements();
    // index of the last column with nominal values
    int hasNominals = -1;
    // index of the last column with numeric ranges defined
    int hasRanges = -1;
    // read settings and write into the map
    String target = settings.getString(ColorManager2NodeModel.SELECTED_COLUMN, null);
    // null = not specified, true = nominal, and false = range
    Boolean nominalSelected = null;
    try {
        nominalSelected = settings.getBoolean(ColorManager2NodeModel.IS_NOMINAL);
    } catch (InvalidSettingsException ise) {
        LOGGER.debug("Nominal/Range selection flag" + " not available.");
    }
    // find last columns for nominal values and numeric ranges defined
    for (int i = 0; i < specs[0].getNumColumns(); i++) {
        DataColumnSpec cspec = specs[0].getColumnSpec(i);
        DataColumnDomain domain = cspec.getDomain();
        // nominal values defined
        if (domain.hasValues()) {
            m_nominal.add(cspec.getName(), domain.getValues());
            // select last possible nominal column
            hasNominals = i;
        }
        // numeric ranges defined
        if (cspec.getType().isCompatible(DoubleValue.class)) {
            DataCell lower = domain.getLowerBound();
            DataCell upper = domain.getUpperBound();
            // lower and upper bound can be null
            m_range.add(cspec.getName(), lower, upper);
            if (hasRanges == -1) {
                // select first range column found
                hasRanges = i;
            }
        }
    }
    // check for not configurable: no column found
    if (hasNominals == -1 && hasRanges == -1) {
        throw new NotConfigurableException("Please provide input table" + " with at least one column with either nominal and/or" + " lower and upper bounds defined.");
    }
    // have possible values defined AND is not compatible with DoubleType
    if (target == null || !specs[0].containsName(target) || (!specs[0].getColumnSpec(target).getDomain().hasValues() && !specs[0].getColumnSpec(target).getType().isCompatible(DoubleValue.class))) {
        // select first nominal column if nothing could be selected
        if (hasNominals > -1) {
            target = specs[0].getColumnSpec(hasNominals).getName();
            nominalSelected = true;
        } else {
            // otherwise the first range column
            if (hasRanges > -1) {
                target = specs[0].getColumnSpec(hasRanges).getName();
                nominalSelected = false;
            } else {
                // 
                assert false : "Both, nominal and range column are not " + "available!";
            }
        }
    } else {
        // we have a valid target column
        boolean domValues = specs[0].getColumnSpec(target).getDomain().hasValues();
        // nothing selected before
        if (nominalSelected == null) {
            // select nominal, if possible values found
            nominalSelected = domValues;
        } else {
            // nominal! but no possible values
            if (nominalSelected && !domValues) {
                // use range column
                nominalSelected = false;
            }
        }
    }
    // nominal column selected
    if (hasNominals > -1) {
        m_nominal.loadSettings(settings, target);
        if (nominalSelected) {
            m_nominal.select(target);
            m_alphaPanel.setAlpha(m_nominal.getAlpha());
        }
    } else {
        m_nominal.select(null);
    }
    // numeric range column selected
    if (hasRanges > -1) {
        m_range.loadSettings(settings, target);
        if (!nominalSelected) {
            m_range.select(target);
            m_alphaPanel.setAlpha(m_range.getAlpha());
        }
    } else {
        m_range.select(null);
    }
    // add all columns
    int cols = specs[0].getNumColumns();
    for (int i = 0; i < cols; i++) {
        DataColumnSpec cspec = specs[0].getColumnSpec(i);
        m_columns.addItem(cspec);
        if (cspec.getName().equals(target)) {
            m_columns.setSelectedIndex(i);
        }
    }
    // inform about column change
    columnChanged(target, nominalSelected);
    // register column change listener
    m_columns.addItemListener(this);
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell)

Example 32 with DataColumnDomain

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

Example 33 with DataColumnDomain

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

the class ColorManager2NodeModel method execute.

/**
 * Is invoked during the node's execution to make the color settings.
 *
 * @param data the input data array
 * @param exec the execution monitor
 * @return the same input data table whereby the RowKeys contain color info
 *         now
 * @throws CanceledExecutionException if user canceled execution
 */
@Override
protected PortObject[] execute(final PortObject[] data, final ExecutionContext exec) throws CanceledExecutionException {
    assert (data != null && data.length == 1 && data[INPORT] != null);
    BufferedDataTable in = (BufferedDataTable) data[0];
    DataTableSpec inSpec = in.getDataTableSpec();
    ColorHandler colorHandler;
    // if no column has been selected, guess first nominal column
    if (m_column == null) {
        // find first nominal column with possible values
        String column = DataTableSpec.guessNominalClassColumn(inSpec, false);
        m_columnGuess = column;
        m_isNominalGuess = true;
        super.setWarningMessage("Selected column \"" + column + "\" with default nominal color mapping.");
        Set<DataCell> set = inSpec.getColumnSpec(column).getDomain().getValues();
        m_mapGuess.clear();
        m_mapGuess.putAll(ColorManager2DialogNominal.createColorMapping(set));
        colorHandler = createNominalColorHandler(m_mapGuess);
        DataTableSpec newSpec = getOutSpec(inSpec, column, colorHandler);
        BufferedDataTable changedSpecTable = exec.createSpecReplacerTable(in, newSpec);
        DataTableSpec modelSpec = new DataTableSpec(newSpec.getColumnSpec(column));
        ColorHandlerPortObject viewModel = new ColorHandlerPortObject(modelSpec, colorHandler.toString() + " based on column \"" + m_column + "\"");
        return new PortObject[] { changedSpecTable, viewModel };
    }
    // find column index
    int columnIndex = inSpec.findColumnIndex(m_column);
    // create new column spec based on color settings
    DataColumnSpec cspec = inSpec.getColumnSpec(m_column);
    if (m_isNominal) {
        colorHandler = createNominalColorHandler(m_map);
    } else {
        DataColumnDomain dom = cspec.getDomain();
        DataCell lower, upper;
        if (dom.hasBounds()) {
            lower = dom.getLowerBound();
            upper = dom.getUpperBound();
        } else {
            Statistics3Table stat = new Statistics3Table(in, false, 0, Collections.<String>emptyList(), exec);
            lower = stat.getMinCells()[columnIndex];
            upper = stat.getMaxCells()[columnIndex];
        }
        colorHandler = createRangeColorHandler(lower, upper, m_map);
    }
    DataTableSpec newSpec = getOutSpec(inSpec, m_column, colorHandler);
    DataTableSpec modelSpec = new DataTableSpec(newSpec.getColumnSpec(m_column));
    BufferedDataTable changedSpecTable = exec.createSpecReplacerTable(in, newSpec);
    ColorHandlerPortObject viewModel = new ColorHandlerPortObject(modelSpec, "Coloring on \"" + m_column + "\"");
    return new PortObject[] { changedSpecTable, viewModel };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) BufferedDataTable(org.knime.core.node.BufferedDataTable) Statistics3Table(org.knime.base.data.statistics.Statistics3Table) DataCell(org.knime.core.data.DataCell) ColorHandlerPortObject(org.knime.core.node.port.viewproperty.ColorHandlerPortObject) PortObject(org.knime.core.node.port.PortObject) ColorHandler(org.knime.core.data.property.ColorHandler) ColorHandlerPortObject(org.knime.core.node.port.viewproperty.ColorHandlerPortObject)

Example 34 with DataColumnDomain

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

the class BinningUtil method calculateIntegerMaxNoOfBins.

/**
 * Calculates the maximum number of bins for the given column spec if it
 * is an integer column or returns the given number of bins.
 * @param noOfBins the current number of bins
 * @param xColSpec to calculate the range for
 * @return the maximum number of bins
 */
public static int calculateIntegerMaxNoOfBins(final int noOfBins, final DataColumnSpec xColSpec) {
    int result = noOfBins;
    if (xColSpec != null && xColSpec.getType().isCompatible(LongValue.class)) {
        final DataColumnDomain domain = xColSpec.getDomain();
        if (domain != null) {
            final LongValue lowerBound = (LongValue) domain.getLowerBound();
            final LongValue upperBound = (LongValue) domain.getUpperBound();
            double range = (double) upperBound.getLongValue() - (double) lowerBound.getLongValue() + 1.0;
            if (range > Integer.MAX_VALUE) {
                range = Integer.MAX_VALUE;
            }
            if (range <= 0) {
                // we need at least one bin
                range = 1;
            }
            if (result > range) {
                result = (int) range;
            }
        }
    }
    return result;
}
Also used : DataColumnDomain(org.knime.core.data.DataColumnDomain) LongValue(org.knime.core.data.LongValue)

Example 35 with DataColumnDomain

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

the class BinningUtil method getBinDataCells.

private static Set<DataCell> getBinDataCells(final DataColumnSpec colSpec) {
    final DataColumnDomain domain = colSpec.getDomain();
    final Set<DataCell> values;
    // check if we have the values
    if (domain.getValues() == null) {
        throw new IllegalArgumentException("No domain values defined for nominal binning column. " + "Please use DomainCalculator or ColumnFilter node " + "to set the domain values.");
    }
    values = colSpec.getDomain().getValues();
    return values;
}
Also used : DataColumnDomain(org.knime.core.data.DataColumnDomain) DataCell(org.knime.core.data.DataCell)

Aggregations

DataColumnDomain (org.knime.core.data.DataColumnDomain)46 DataColumnSpec (org.knime.core.data.DataColumnSpec)34 DataCell (org.knime.core.data.DataCell)32 DataTableSpec (org.knime.core.data.DataTableSpec)20 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 DoubleValue (org.knime.core.data.DoubleValue)13 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)12 DataType (org.knime.core.data.DataType)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)7 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 DoubleCell (org.knime.core.data.def.DoubleCell)5 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 DataRow (org.knime.core.data.DataRow)3