use of org.knime.core.data.DataTableSpecCreator in project knime-core by knime.
the class EditNumericDomainNodeModel method processDomainSettings.
private DataTableSpec processDomainSettings(final DataTableSpec dataTableSpec) throws InvalidSettingsException {
if (m_configuration == null) {
throw new InvalidSettingsException("Missing Configuration.");
}
EditNumericDomainConfiguration config = m_configuration;
FilterResult filterResult = config.getColumnspecFilterConfig().applyTo(dataTableSpec);
List<DataColumnSpec> newColumnSpecs = new ArrayList<DataColumnSpec>(dataTableSpec.getNumColumns());
String[] columnNames = dataTableSpec.getColumnNames();
Set<String> includeSet = new HashSet<String>();
Collections.addAll(includeSet, filterResult.getIncludes());
for (int i = 0; i < dataTableSpec.getNumColumns(); i++) {
DataColumnSpec columnSpec = dataTableSpec.getColumnSpec(i);
String columnName = columnNames[i];
if (includeSet.contains(columnName)) {
DataColumnSpecCreator columnSpecCreator = new DataColumnSpecCreator(columnSpec);
DataColumnDomainCreator domainCreator = new //
DataColumnDomainCreator(//
createCell(columnName, columnSpec.getType(), config.getLowerBound()), createCell(columnName, columnSpec.getType(), config.getUpperBound()));
domainCreator.setValues(columnSpec.getDomain().getValues());
columnSpecCreator.setDomain(domainCreator.createDomain());
newColumnSpecs.add(columnSpecCreator.createSpec());
} else {
newColumnSpecs.add(columnSpec);
}
}
StringBuilder warnings = new StringBuilder();
if (includeSet.isEmpty()) {
warnings.append("No columns are included.");
}
if (filterResult.getRemovedFromIncludes().length > 0) {
warnings.append("\nFollowing columns are configured but no longer exist: " + ConvenienceMethods.getShortStringFrom(Arrays.asList(filterResult.getRemovedFromIncludes()), 5));
}
if (warnings.length() > 0) {
setWarningMessage(warnings.toString());
}
return new DataTableSpecCreator(dataTableSpec).dropAllColumns().addColumns(newColumnSpecs.toArray(new DataColumnSpec[newColumnSpecs.size()])).createSpec();
}
use of org.knime.core.data.DataTableSpecCreator in project knime-core by knime.
the class PMMLKNNNodeModel method createSpec.
/**
* @param dataTableSpec
* @return
* @throws InvalidSettingsException when the input table contains invalid columns
*/
private PMMLPortObjectSpec createSpec(final DataTableSpec dataTableSpec) throws InvalidSettingsException {
List<DataColumnSpec> learningColumns = new ArrayList<DataColumnSpec>();
DataTableSpecCreator dataDictCreator = new DataTableSpecCreator();
String[] selectedColumns = m_learningColumns.applyTo(dataTableSpec).getIncludes();
if (selectedColumns.length == 0) {
throw new InvalidSettingsException("No learning columns are selected.");
}
for (String lc : selectedColumns) {
DataColumnSpec cs = dataTableSpec.getColumnSpec(lc);
dataDictCreator.addColumns(cs);
learningColumns.add(cs);
}
if (m_predColumnName.getStringValue() == null || !dataTableSpec.containsName(m_predColumnName.getStringValue())) {
for (int i = 0; i < dataTableSpec.getNumColumns(); i++) {
DataColumnSpec cspec = dataTableSpec.getColumnSpec(i);
if (cspec.getType().isCompatible(StringValue.class)) {
m_predColumnName.setStringValue(cspec.getName());
setWarningMessage("No target column selected. Using \"" + cspec.getName() + "\".");
break;
}
}
}
if (m_predColumnName.getStringValue() == null) {
throw new InvalidSettingsException("The table does not contain a suitable target column.");
}
dataDictCreator.addColumns(dataTableSpec.getColumnSpec(m_predColumnName.getStringValue()));
PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(dataDictCreator.createSpec());
specCreator.setTargetCol(dataTableSpec.getColumnSpec(m_predColumnName.getStringValue()));
specCreator.setLearningCols(learningColumns);
return specCreator.createSpec();
}
use of org.knime.core.data.DataTableSpecCreator in project knime-core by knime.
the class ColumnSelectionSearchableListPanel method update.
/**
* Clears the current list, adds the {@link DataColumnSpec}s of the given spec and invalidates the current
* {@link ListModifier}. The {@link DataColumnSpec}s added by this function cannot be deleted from list using the
* {@link ListModifier}.
*
* @param specs containing the immutable part of the column list
* @return a ListModifier for managing the mutable part of the column list
*/
public ListModifier update(final Iterable<DataColumnSpec> specs) {
m_lastSearchHits.clear();
setUnkownFilterVisible(false);
m_columnList.update(specs);
if (m_currentModifier != null) {
m_currentModifier.invalidate();
}
DataTableSpecCreator dataTableSpecCreator = new DataTableSpecCreator();
for (DataColumnSpec spec : specs) {
dataTableSpecCreator.addColumns(spec);
}
m_currentModifier = new ModifierImpl(this, dataTableSpecCreator.createSpec());
return m_currentModifier;
}
use of org.knime.core.data.DataTableSpecCreator in project knime-core by knime.
the class MissingCellReplacingDataTable method createSpec.
private DataTableSpec createSpec() {
DataTableSpecCreator specCreator = new DataTableSpecCreator();
for (MissingCellHandler handler : m_handlers) {
DataColumnSpecCreator c = new DataColumnSpecCreator(handler.getColumnSpec());
if (handler.getOutputDataType() != null) {
c.setType(handler.getOutputDataType());
}
specCreator.addColumns(c.createSpec());
}
return specCreator.createSpec();
}
use of org.knime.core.data.DataTableSpecCreator in project knime-core by knime.
the class EditNumericDomainNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
//
final DataTableSpec originalTableSpec = inData[0].getDataTableSpec();
DataTableSpec editedTableSpec = processDomainSettings(inData[0].getDataTableSpec());
FilterResult filterResult = m_configuration.getColumnspecFilterConfig().applyTo(originalTableSpec);
List<String> includedColumnNames = Arrays.asList(filterResult.getIncludes());
long currentRowIndex = 0;
long size = inData[0].size();
String[] tableNames = originalTableSpec.getColumnNames();
Map<Integer, DataColumnSpec> map = new HashMap<Integer, DataColumnSpec>();
for (int i = 0; i < originalTableSpec.getNumColumns(); i++) {
if (includedColumnNames.contains(tableNames[i])) {
map.put(i, editedTableSpec.getColumnSpec(i));
} else {
map.put(i, originalTableSpec.getColumnSpec(i));
}
}
CloseableRowIterator rowIterator = inData[0].iterator();
long rowIndex = 0;
try {
while (rowIterator.hasNext()) {
DataRow currentRow = rowIterator.next();
//
exec.setProgress(//
currentRowIndex++ / (double) size, "checking domains of row: " + currentRow.getKey().getString());
for (String colName : includedColumnNames) {
int currIndex = originalTableSpec.findColumnIndex(colName);
DataCell currCell = currentRow.getCell(currIndex);
if (!currCell.isMissing() && outOfDomain(currCell, map.get(currIndex))) {
switch(m_configuration.getDomainOverflowPolicy()) {
case CALCULATE_BOUNDS:
map.put(currIndex, calculateAndCreateBoundedColumnSpec(currCell, map.get(currIndex)));
break;
case USE_EXISTING_BOUNDS:
map.put(currIndex, originalTableSpec.getColumnSpec(currIndex));
break;
default:
throw new EditNumericDomainOverflowException(tableNames[currIndex], ((DoubleValue) currCell).getDoubleValue(), m_configuration.getLowerBound(), m_configuration.getUpperBound(), rowIndex, currentRow.getKey());
}
}
}
exec.checkCanceled();
rowIndex++;
}
} finally {
rowIterator.close();
}
DataTableSpecCreator newTableSpecCreator = new DataTableSpecCreator().setName(originalTableSpec.getName()).putProperties(originalTableSpec.getProperties());
for (int i = 0; i < originalTableSpec.getNumColumns(); i++) {
newTableSpecCreator.addColumns(map.get(i));
}
return new BufferedDataTable[] { exec.createSpecReplacerTable(inData[0], newTableSpecCreator.createSpec()) };
}
Aggregations