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 };
}
use of org.knime.core.data.property.ColorHandler 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 };
}
use of org.knime.core.data.property.ColorHandler in project knime-core by knime.
the class ColorAppender2NodeModel 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 color information in input");
}
DataColumnSpec col = modelSpec.getColumnSpec(0);
ColorHandler colorHandler = col.getColorHandler();
if (col.getColorHandler() == null) {
throw new InvalidSettingsException("No color information in input");
}
String column = m_column.getStringValue();
if (column == null) {
// suitable column (while setting a warning message)
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 = ColorManager2NodeModel.getOutSpec(dataSpec, column, colorHandler);
return spec;
}
Aggregations