use of org.knime.core.data.DataColumnDomainCreator in project knime-core by knime.
the class RegressionPredictorCellFactory method createColumnSpec.
/**
* Creates the spec of the output if possible.
*
* @param portSpec the spec of the pmml input port
* @param tableSpec the spec of the data input port
* @param settings settings for the predictor node
* @return The spec of the output or null
* @throws InvalidSettingsException when tableSpec and portSpec do not match
*/
public static DataColumnSpec[] createColumnSpec(final PMMLPortObjectSpec portSpec, final DataTableSpec tableSpec, final RegressionPredictorSettings settings) throws InvalidSettingsException {
// Assertions
if (portSpec.getTargetCols().isEmpty()) {
throw new InvalidSettingsException("The general regression model" + " does not specify a target column.");
}
for (DataColumnSpec learningColSpec : portSpec.getLearningCols()) {
String learningCol = learningColSpec.getName();
if (tableSpec.containsName(learningCol)) {
DataColumnSpec colSpec = tableSpec.getColumnSpec(learningCol);
if (learningColSpec.getType().isCompatible(NominalValue.class)) {
if (!colSpec.getType().isCompatible(BitVectorValue.class) && !colSpec.getType().isCompatible(ByteVectorValue.class) && !colSpec.getType().isCompatible(NominalValue.class)) {
throw new InvalidSettingsException("The column \"" + learningCol + "\" in the table of prediction " + "is expected to be compatible with " + "\"NominalValue\".");
}
} else if (learningColSpec.getType().isCompatible(DoubleValue.class) && !colSpec.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("The column \"" + learningCol + "\" in the table of prediction " + "is expected to be numeric.");
}
} else {
throw new InvalidSettingsException("The table for prediction " + "does not contain the column \"" + learningCol + "\".");
}
}
// The list of added columns
List<DataColumnSpec> newColsSpec = new ArrayList<DataColumnSpec>();
String targetCol = portSpec.getTargetFields().get(0);
DataColumnSpec targetColSpec = portSpec.getDataTableSpec().getColumnSpec(targetCol);
if (settings.getIncludeProbabilities() && targetColSpec.getType().isCompatible(NominalValue.class)) {
if (!targetColSpec.getDomain().hasValues()) {
return null;
}
List<DataCell> targetCategories = new ArrayList<DataCell>();
targetCategories.addAll(targetColSpec.getDomain().getValues());
for (DataCell value : targetCategories) {
String name = "P (" + targetCol + "=" + value.toString() + ")" + settings.getPropColumnSuffix();
String newColName = DataTableSpec.getUniqueColumnName(tableSpec, name);
DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, DoubleCell.TYPE);
DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(new DoubleCell(0.0), new DoubleCell(1.0));
colSpecCreator.setDomain(domainCreator.createDomain());
newColsSpec.add(colSpecCreator.createSpec());
}
}
String targetColName = settings.getHasCustomPredictionName() ? settings.getCustomPredictionName() : "Prediction (" + targetCol + ")";
String uniqueTargetColName = DataTableSpec.getUniqueColumnName(tableSpec, targetColName);
DataType targetType = targetColSpec.getType().isCompatible(NominalValue.class) ? targetColSpec.getType() : DoubleCell.TYPE;
DataColumnSpecCreator targetColSpecCreator = new DataColumnSpecCreator(uniqueTargetColName, targetType);
if (targetColSpec.getType().isCompatible(NominalValue.class)) {
DataColumnDomainCreator targetDomainCreator = new DataColumnDomainCreator(targetColSpec.getDomain());
targetColSpecCreator.setDomain(targetDomainCreator.createDomain());
}
newColsSpec.add(targetColSpecCreator.createSpec());
return newColsSpec.toArray(new DataColumnSpec[0]);
}
use of org.knime.core.data.DataColumnDomainCreator in project knime-core by knime.
the class ClusterNodeModel method createAppendedSpec.
private DataTableSpec createAppendedSpec(final DataTableSpec originalSpec) {
// determine the possible values of the appended column
DataCell[] possibleValues = new DataCell[m_nrOfClusters.getIntValue()];
for (int i = 0; i < m_nrOfClusters.getIntValue(); i++) {
DataCell key = new StringCell(CLUSTER + i);
possibleValues[i] = key;
}
// create the domain
// 1) guess an unused name for the new column (fixes bug #1022)
String colNameGuess = "Cluster";
int uniqueNr = 0;
while (originalSpec.getColumnSpec(colNameGuess) != null) {
uniqueNr++;
colNameGuess = "Cluster_" + uniqueNr;
}
// 2) create spec
DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(possibleValues);
DataColumnSpecCreator creator = new DataColumnSpecCreator(colNameGuess, StringCell.TYPE);
creator.setDomain(domainCreator.createDomain());
// create the appended column spec
DataColumnSpec labelColSpec = creator.createSpec();
return new DataTableSpec(originalSpec, new DataTableSpec(labelColSpec));
}
use of org.knime.core.data.DataColumnDomainCreator in project knime-core by knime.
the class AbstractMany2OneCellFactory method getColumnSpecs.
/**
* {@inheritDoc}
*/
@Override
public DataColumnSpec[] getColumnSpecs() {
// new column
DataColumnSpecCreator appendedColumnCreator = new DataColumnSpecCreator(m_appendedColumnName, StringCell.TYPE);
// possible values depend on allow multi occurrences
DataColumnDomainCreator possibleValuesCreator = new DataColumnDomainCreator(m_columnNames);
appendedColumnCreator.setDomain(possibleValuesCreator.createDomain());
return new DataColumnSpec[] { appendedColumnCreator.createSpec() };
}
use of org.knime.core.data.DataColumnDomainCreator in project knime-core by knime.
the class BinModelPlotter method updatePaintModel.
/**
* {@inheritDoc}
*/
@Override
public synchronized void updatePaintModel() {
if (m_discretizationModel == null) {
return;
}
// clear the drawing pane
((BinModelDrawingPane) getDrawingPane()).setBinningSchemes(null);
// get the first columns
if (m_selectedColumns == null) {
m_selectedColumns = new LinkedHashSet<String>();
String[] binnedColumnNames = m_discretizationModel.getIncludedColumnNames();
for (int i = 0; i < binnedColumnNames.length; i++) {
// add them to the selected columns
m_selectedColumns.add(binnedColumnNames[i]);
}
((MultiColumnPlotterProperties) getProperties()).updateColumnSelection(m_binnedColumnsSpec, m_selectedColumns);
}
if (m_selectedColumns.size() == 0) {
getDrawingPane().repaint();
return;
}
Set<DataCell> selectedColumnCells = new LinkedHashSet<DataCell>();
m_coordinates = new ArrayList<Coordinate>();
List<Integer> columnIndices = new ArrayList<Integer>();
for (String name : m_selectedColumns) {
int idx = m_binnedColumnsSpec.findColumnIndex(name);
if (idx >= 0) {
selectedColumnCells.add(new StringCell(name));
DataColumnSpec colSpec = m_binnedColumnsSpec.getColumnSpec(idx);
columnIndices.add(idx);
Coordinate coordinate = Coordinate.createCoordinate(colSpec);
m_coordinates.add(coordinate);
}
}
// get the binning schemes for the selected columns
DiscretizationScheme[] selectedSchemes = getSelectedSchemes();
String[] selectedColumnNames = getSelectedColumnNames();
// calculate the display coordinates for the drawing pane
BinRuler[] binRulers = new BinRuler[selectedSchemes.length];
// determine the width available for a bin ruler
int rulerWidth = getDrawingPaneDimension().width - 2 * m_hMargin;
for (int i = 0; i < selectedSchemes.length; i++) {
double[] bounds = selectedSchemes[i].getBounds();
double min = bounds[0];
double max = bounds[bounds.length - 1];
// first create a colum spec from the schemes
DataColumnSpecCreator columnSpecCreator = new DataColumnSpecCreator("", DoubleCell.TYPE);
columnSpecCreator.setDomain(new DataColumnDomainCreator(new DoubleCell(min), new DoubleCell(max)).createDomain());
DoubleCoordinate coordinate = (DoubleCoordinate) Coordinate.createCoordinate(columnSpecCreator.createSpec());
Point leftStart = new Point(m_hMargin, m_vMargin + (i + 1) * m_columnDisplayHeight);
int[] binPositions = new int[bounds.length];
String[] binLabels = new String[bounds.length];
int count = 0;
for (double bound : bounds) {
binPositions[count] = (int) coordinate.calculateMappedValue(new DoubleCell(bound), rulerWidth, true);
binLabels[count] = coordinate.formatNumber(bounds[count]);
count++;
}
binRulers[i] = new BinRuler(leftStart, rulerWidth, binPositions, binLabels, selectedColumnNames[i]);
}
((BinModelDrawingPane) getDrawingPane()).setBinningSchemes(binRulers);
m_hMargin = 10;
m_vMargin = 10;
((BinModelDrawingPane) getDrawingPane()).setHorizontalMargin(m_hMargin);
setHeight(binRulers[binRulers.length - 1].getLeftStartPoint().y + 40);
}
use of org.knime.core.data.DataColumnDomainCreator in project knime-core by knime.
the class EditNominalDomainNodeModel method sortPossibleValues.
private DataTableSpec sortPossibleValues(final DataTableSpec orgSpec) throws InvalidSettingsException {
if (m_configuration == null) {
throw new InvalidSettingsException("Missing Configuration.");
}
Set<String> configuredColumns = new HashSet<String>(m_configuration.getConfiguredColumns());
String[] columnNames = orgSpec.getColumnNames();
DataTableSpecCreator creator = new DataTableSpecCreator(orgSpec).dropAllColumns();
for (int i = 0; i < orgSpec.getNumColumns(); i++) {
String name = columnNames[i];
if (configuredColumns.remove(name)) {
DataColumnSpec orgDataSpec = orgSpec.getColumnSpec(i);
if (!StringCell.TYPE.equals(orgDataSpec.getType())) {
CheckUtils.checkSetting(m_configuration.isIgnoreWrongTypes(), "Column '%s' must be of type '%s' \nbut was of type: '%s'", name, StringCell.TYPE, orgDataSpec.getType());
creator.addColumns(orgDataSpec);
} else {
DataColumnDomain domain = orgDataSpec.getDomain();
DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(orgDataSpec);
DataColumnDomainCreator yetAnotherCreator = new DataColumnDomainCreator(domain.getLowerBound(), domain.getUpperBound());
List<DataCell> sorting = new ArrayList<DataCell>(m_configuration.getSorting(name));
Set<DataCell> difference = diff(domain.getValues(), sorting);
yetAnotherCreator.setValues(resolveNewValues(sorting, difference));
dataColumnSpecCreator.setDomain(yetAnotherCreator.createDomain());
creator.addColumns(dataColumnSpecCreator.createSpec());
}
} else {
creator.addColumns(orgSpec.getColumnSpec(i));
}
}
if (!configuredColumns.isEmpty()) {
String missingColumnsString = "Following columns are configured but no longer exist: \n" + ConvenienceMethods.getShortStringFrom(configuredColumns, 5);
CheckUtils.checkSetting(m_configuration.isIgnoreNotExistingColumns(), missingColumnsString);
setWarningMessage(missingColumnsString);
}
return creator.createSpec();
}
Aggregations