Search in sources :

Example 6 with ColorColumn

use of org.knime.base.node.viz.histogram.util.ColorColumn 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 7 with ColorColumn

use of org.knime.base.node.viz.histogram.util.ColorColumn in project knime-core by knime.

the class BinDataModel method addDataRow.

/**
 * @param id the row id
 * @param rowColor the row color
 * @param aggrCols the {@link ColorColumn} objects in the same order
 * like the aggregation values
 * @param aggrVals the aggregation value in the same order like the
 * columns
 */
public void addDataRow(final RowKey id, final Color rowColor, final Collection<ColorColumn> aggrCols, final DataCell... aggrVals) {
    // }
    if (aggrCols == null || aggrCols.size() < 1) {
        // no aggregation column selected create a dummy bar to show
        // at least the count aggregation method
        BarDataModel bar = m_bars.get(NO_AGGR_COL_COLOR);
        if (bar == null) {
            bar = createBar("", NO_AGGR_COL_COLOR);
            m_bars.put(NO_AGGR_COL_COLOR, bar);
        }
        bar.addDataRow(rowColor, id, new DoubleCell(0));
    } else {
        int i = 0;
        for (final ColorColumn column : aggrCols) {
            final DataCell cell = aggrVals[i++];
            final Color barColor = column.getColor();
            BarDataModel bar = m_bars.get(barColor);
            if (bar == null) {
                bar = createBar(column.getColumnName(), barColor);
                m_bars.put(barColor, bar);
            }
            bar.addDataRow(rowColor, id, cell);
        }
    }
    m_rowCounter++;
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) Color(java.awt.Color) DataCell(org.knime.core.data.DataCell) Point(java.awt.Point)

Example 8 with ColorColumn

use of org.knime.base.node.viz.histogram.util.ColorColumn in project knime-core by knime.

the class BinDataModel method updateBinWidth.

/**
 * @param startX new x coordinate
 * @param binWidth new bin width
 * @param barElementColors all element colors which define the order
 * the elements should be drawn
 * @param aggrColumns the current aggregation columns
 * @param baseLine the base line
 * @param calculator the hilite shape calculator
 */
public void updateBinWidth(final int startX, final int binWidth, final List<Color> barElementColors, final Collection<ColorColumn> aggrColumns, final int baseLine, final HistogramHiliteCalculator calculator) {
    if (m_binRectangle == null) {
        return;
    }
    final boolean drawBarBefore = m_presentable;
    final int yCoord = (int) m_binRectangle.getY();
    final int binHeight = (int) m_binRectangle.getHeight();
    m_binRectangle.setBounds(startX, yCoord, binWidth, binHeight);
    m_surroundingRectangle = AbstractHistogramVizModel.calculateSurroundingRectangle(m_binRectangle, baseLine, AbstractHistogramVizModel.BIN_SURROUNDING_SPACE);
    final int noOfBars = m_bars.size();
    if (noOfBars < 1) {
        return;
    }
    m_presentable = elementsFitInBin(noOfBars, binWidth);
    if (!m_presentable) {
        // }
        return;
    }
    if (!drawBarBefore) {
        // if the bar couldn't be draw before but now we have to
        // recalculate them
        setBinRectangle(m_binRectangle, baseLine, barElementColors, aggrColumns, calculator);
        return;
    }
    final int barWidth = calculateBarWidth(binWidth, noOfBars);
    int xCoord = startX;
    if (aggrColumns == null || aggrColumns.size() < 1) {
        // the user hasn't selected a aggregation column so we use the
        // dummy bar
        final BarDataModel bar = m_bars.get(NO_AGGR_COL_COLOR);
        bar.updateBarWidth(xCoord, barWidth, barElementColors, baseLine, calculator);
    } else {
        for (final ColorColumn aggrCol : aggrColumns) {
            final BarDataModel bar = m_bars.get(aggrCol.getColor());
            bar.updateBarWidth(xCoord, barWidth, barElementColors, baseLine, calculator);
            xCoord += barWidth + AbstractHistogramVizModel.SPACE_BETWEEN_BARS;
        }
    }
}
Also used : ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) Point(java.awt.Point)

Example 9 with ColorColumn

use of org.knime.base.node.viz.histogram.util.ColorColumn in project knime-core by knime.

the class FixedHistogramDataModel method loadFromFile.

/**
 * @param directory the directory to write to
 * @param exec the {@link ExecutionMonitor} to provide progress messages; must not be <code>null</code>
 * @return the histogram data model
 * @throws InvalidSettingsException if the x column specification
 * wasn't valid
 * @throws IOException if a file exception occurs
 * @throws CanceledExecutionException if the operation is canceled
 */
public static FixedHistogramDataModel loadFromFile(final File directory, final ExecutionMonitor exec) throws InvalidSettingsException, IOException, CanceledExecutionException {
    exec.setProgress(0.0, "Start reading data from file");
    final ConfigRO config;
    final FileInputStream is;
    final GZIPInputStream inData;
    try {
        final File settingsFile = new File(directory, CFG_DATA_FILE);
        is = new FileInputStream(settingsFile);
        inData = new GZIPInputStream(is);
        config = NodeSettings.loadFromXML(inData);
    } catch (final FileNotFoundException e) {
        throw e;
    } catch (final IOException e) {
        LOGGER.error("Unable to load histogram data: " + e.getMessage());
        throw new IOException("Please reexecute the histogram node. " + "(For details see log file)");
    }
    final Config xConfig = config.getConfig(CFG_X_COL_SPEC);
    final DataColumnSpec xColSpec = DataColumnSpec.load(xConfig);
    final boolean binNominal = config.getBoolean(CFG_NOMINAL);
    AggregationMethod aggrMethod = AggregationMethod.getDefaultMethod();
    try {
        aggrMethod = AggregationMethod.getMethod4Command(config.getString(CFG_AGGR_METHOD));
    } catch (final Exception e) {
    // Take the default method
    }
    exec.setProgress(0.1, "Binning column specification loaded");
    exec.setProgress("Loading aggregation columns...");
    final Config aggrConf = config.getConfig(CFG_AGGR_COLS);
    final int aggrColCounter = aggrConf.getInt(CFG_AGGR_COL_COUNTER);
    final ArrayList<ColorColumn> aggrCols = new ArrayList<ColorColumn>(aggrColCounter);
    for (int i = 0; i < aggrColCounter; i++) {
        final Config aggrColConf = aggrConf.getConfig(CFG_COLOR_COL + i);
        aggrCols.add(ColorColumn.loadFromFile(aggrColConf, exec));
    }
    exec.setProgress(0.3, "Loading bins...");
    final ConfigRO binsConf = config.getConfig(CFG_BINS);
    final int binCounter = binsConf.getInt(CFG_BIN_COUNTER);
    final List<BinDataModel> bins = new ArrayList<BinDataModel>(binCounter);
    for (int i = 0; i < binCounter; i++) {
        final Config binConf = binsConf.getConfig(CFG_BIN + i);
        bins.add(BinDataModel.loadFromFile(binConf, exec));
    }
    final Config missingConfig = binsConf.getConfig(CFG_MISSING_BIN);
    final BinDataModel missingBin = BinDataModel.loadFromFile(missingConfig, exec);
    BinDataModel invalidBin = null;
    if (binsConf.containsKey(CFG_INVALID_BIN)) {
        final Config invalidConfig = binsConf.getConfig(CFG_INVALID_BIN);
        invalidBin = BinDataModel.loadFromFile(invalidConfig, exec);
    }
    exec.setProgress(0.9, "Loading element colors...");
    final ConfigRO colorColsConf = config.getConfig(CFG_COLOR_COLS);
    final int counter = colorColsConf.getInt(CFG_ROW_COLOR_COUNTER);
    final SortedSet<Color> rowColors = new TreeSet<Color>(HSBColorComparator.getInstance());
    for (int i = 0; i < counter; i++) {
        rowColors.add(new Color(colorColsConf.getInt(CFG_ROW_COLOR + i)));
    }
    exec.setProgress(1.0, "Histogram data model loaded ");
    // close the stream
    inData.close();
    is.close();
    return new FixedHistogramDataModel(xColSpec, aggrMethod, aggrCols, binNominal, bins, missingBin, invalidBin, rowColors);
}
Also used : AggregationMethod(org.knime.base.node.viz.aggregation.AggregationMethod) Config(org.knime.core.node.config.Config) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) Color(java.awt.Color) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GZIPInputStream(java.util.zip.GZIPInputStream) DataColumnSpec(org.knime.core.data.DataColumnSpec) TreeSet(java.util.TreeSet) ConfigRO(org.knime.core.node.config.ConfigRO) File(java.io.File)

Example 10 with ColorColumn

use of org.knime.base.node.viz.histogram.util.ColorColumn in project knime-core by knime.

the class AbstractHistogramPlotter method getAggregationColSpec.

/**
 * @return the <code>DataColumnSpec</code> of the aggregation column
 */
public DataColumnSpec getAggregationColSpec() {
    final AbstractHistogramVizModel vizModel = getHistogramVizModel();
    if (vizModel == null) {
        LOGGER.debug("VizModel was null");
        throw new IllegalStateException("Exception in getAggregationColSpec: " + "Viz model must not be null");
    }
    double lowerBound = vizModel.getMinAggregationValue();
    double upperBound = vizModel.getMaxAggregationValue();
    // coordinate
    if (lowerBound > 0) {
        lowerBound = 0;
    } else if (upperBound < 0) {
        upperBound = 0;
    }
    final AggregationMethod aggrMethod = vizModel.getAggregationMethod();
    // set the column type depending on the aggregation method and type of
    // the aggregation column. If the method is count set it to integer. If
    // the aggregation method is summary and the data type of the
    // aggregation column is integer the result must be an integer itself
    DataType type = DoubleCell.TYPE;
    final Collection<? extends ColorColumn> columnNames = vizModel.getAggrColumns();
    if (AggregationMethod.COUNT.equals(aggrMethod) || AggregationMethod.VALUE_COUNT.equals(aggrMethod)) {
        type = IntCell.TYPE;
    }
    if (AggregationMethod.SUM.equals(aggrMethod) && columnNames != null) {
        // if the aggregation method is summary and ...
        boolean allInteger = true;
        for (final ColorColumn column : columnNames) {
            final DataColumnSpec colSpec = m_tableSpec.getColumnSpec(column.getColumnName());
            if (colSpec == null || !colSpec.getType().isCompatible(IntValue.class)) {
                allInteger = false;
                break;
            }
        }
        // ... all columns of the int type we can set the column type to int
        if (allInteger) {
            type = IntCell.TYPE;
        }
    }
    final String displayColumnName = createAggregationColumnName(columnNames, aggrMethod);
    final DataColumnSpec spec = createColumnSpec(displayColumnName, type, lowerBound, upperBound, null);
    return spec;
}
Also used : AggregationMethod(org.knime.base.node.viz.aggregation.AggregationMethod) DataColumnSpec(org.knime.core.data.DataColumnSpec) AbstractHistogramVizModel(org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) DataType(org.knime.core.data.DataType)

Aggregations

ColorColumn (org.knime.base.node.viz.histogram.util.ColorColumn)17 Color (java.awt.Color)7 Point (java.awt.Point)5 DataCell (org.knime.core.data.DataCell)5 DataColumnSpec (org.knime.core.data.DataColumnSpec)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 AggregationMethod (org.knime.base.node.viz.aggregation.AggregationMethod)4 AbstractHistogramVizModel (org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel)4 ArrayList (java.util.ArrayList)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 HistogramLayout (org.knime.base.node.viz.histogram.HistogramLayout)2 HistogramHiliteCalculator (org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel.HistogramHiliteCalculator)2 BinDataModel (org.knime.base.node.viz.histogram.datamodel.BinDataModel)2 InteractiveHistogramVizModel (org.knime.base.node.viz.histogram.datamodel.InteractiveHistogramVizModel)2 AbstractHistogramProperties (org.knime.base.node.viz.histogram.impl.AbstractHistogramProperties)2