use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class RegressionPredictorNodeModel method adjustSpecOfRegressionPredictorTable.
private BufferedDataTable adjustSpecOfRegressionPredictorTable(final BufferedDataTable table, final PortObject[] inData, final ExecutionContext exec) throws InvalidSettingsException {
String predColumn = determinPredictedColumName(inData);
if (predColumn != null) {
DataColumnSpec[] colSpecs = getColumnSpecs(table.getSpec());
colSpecs[colSpecs.length - 1] = replaceNameOf(colSpecs[colSpecs.length - 1], predColumn);
return exec.createSpecReplacerTable(table, new DataTableSpec(colSpecs));
} else {
return table;
}
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class BitVectorGeneratorNodeModel method configure.
/**
* Assume to get numeric data only. Output is one column of type BitVector.
*
* {@inheritDoc}
*/
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec spec = inSpecs[0];
if (!m_fromString) {
// check if there is at least one numeric column selected
if (m_includedColumns.isEnabled() && m_includedColumns.getIncludeList().isEmpty()) {
// the includeColumns model cannot be empty
// through the dialog (see #validateSettings)
// only case where !m_fromString and includeColumns evaluates
// to true is for old workflows.
// For backward compatiblity include all numeric columns
// which was the behavior before 2.0
List<String> allNumericColumns = new ArrayList<String>();
for (DataColumnSpec colSpec : spec) {
if (colSpec.getType().isCompatible(DoubleValue.class)) {
allNumericColumns.add(colSpec.getName());
}
}
m_includedColumns.setIncludeList(allNumericColumns);
m_loadedSettingsDontHaveIncludeColumns = false;
}
for (String inclColName : m_includedColumns.getIncludeList()) {
DataColumnSpec colSpec = spec.getColumnSpec(inclColName);
if (colSpec == null) {
throw new InvalidSettingsException("Column " + inclColName + " not found in input table. " + "Please re-configure the node.");
}
if (!colSpec.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("Column " + inclColName + " is not a numeric column");
}
}
} else {
// parse from string column
if (m_stringColumn.getStringValue() == null) {
throw new InvalidSettingsException("No string column selected. " + "Please (re-)configure the node.");
}
// -> check if selected column is a string column
if (!spec.containsName(m_stringColumn.getStringValue()) || !(spec.getColumnSpec(m_stringColumn.getStringValue()).getType().isCompatible(StringValue.class))) {
throw new InvalidSettingsException("Selected string column " + m_stringColumn.getStringValue() + " not in the input table");
}
}
if (m_fromString) {
int stringColIdx = inSpecs[0].findColumnIndex(m_stringColumn.getStringValue());
ColumnRearranger c = createColumnRearranger(inSpecs[0], stringColIdx);
return new DataTableSpec[] { c.createSpec() };
} else {
// numeric input
DataTableSpec newSpec;
DataColumnSpec newColSpec = createNumericOutputSpec(spec);
if (m_replace) {
ColumnRearranger colR = new ColumnRearranger(spec);
colR.remove(m_includedColumns.getIncludeList().toArray(new String[m_includedColumns.getIncludeList().size()]));
newSpec = new DataTableSpec(colR.createSpec(), new DataTableSpec(newColSpec));
} else {
newSpec = new DataTableSpec(spec, new DataTableSpec(newColSpec));
}
return new DataTableSpec[] { newSpec };
}
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class BitVectorGeneratorNodeModel method createBitVectorsFromNumericData.
private BufferedDataTable[] createBitVectorsFromNumericData(final BufferedDataTable data, final ExecutionContext exec) throws CanceledExecutionException {
DataColumnSpec colSpec = createNumericOutputSpec(data.getDataTableSpec());
// get the indices for included columns
List<Integer> colIndices = new ArrayList<Integer>();
for (String colName : m_includedColumns.getIncludeList()) {
int index = data.getDataTableSpec().findColumnIndex(colName);
if (index < 0) {
throw new IllegalArgumentException("Column " + colName + " is not available in " + "current data. Please re-configure the node.");
}
colIndices.add(index);
}
// calculate bits from numeric data
if (m_useMean) {
// either from a percentage of the mean
double[] meanValues = new double[0];
double meanFactor = m_meanPercentage / 100.0;
meanValues = calculateMeanValues(data);
m_factory = new Numeric2BitVectorMeanCellFactory(colSpec, meanValues, meanFactor, colIndices);
} else {
// or dependend on fixed threshold
m_factory = new Numeric2BitVectorThresholdCellFactory(colSpec, m_threshold, colIndices);
}
ColumnRearranger c = new ColumnRearranger(data.getDataTableSpec());
c.append(m_factory);
if (m_replace) {
List<String> includeList = m_includedColumns.getIncludeList();
c.remove(includeList.toArray(new String[includeList.size()]));
}
BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class AutoBinner method calcDomainBoundsIfNeccessary.
/**
* Determines the per column min/max values of the given data if not already
* present in the domain.
* @param data the data
* @param exec the execution context
* @param recalcValuesFor The columns
* @return The data with extended domain information
* @throws InvalidSettingsException
* @throws CanceledExecutionException
*/
public BufferedDataTable calcDomainBoundsIfNeccessary(final BufferedDataTable data, final ExecutionContext exec, final List<String> recalcValuesFor) throws InvalidSettingsException, CanceledExecutionException {
if (null == recalcValuesFor || recalcValuesFor.isEmpty()) {
return data;
}
List<Integer> valuesI = new ArrayList<Integer>();
for (String colName : recalcValuesFor) {
DataColumnSpec colSpec = data.getDataTableSpec().getColumnSpec(colName);
if (!colSpec.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("Can only process numeric " + "data. The column \"" + colSpec.getName() + "\" is not numeric.");
}
if (recalcValuesFor.contains(colName) && !colSpec.getDomain().hasBounds()) {
valuesI.add(data.getDataTableSpec().findColumnIndex(colName));
}
}
if (valuesI.isEmpty()) {
return data;
}
Map<Integer, Double> min = new HashMap<Integer, Double>();
Map<Integer, Double> max = new HashMap<Integer, Double>();
for (int col : valuesI) {
min.put(col, Double.MAX_VALUE);
max.put(col, Double.MIN_VALUE);
}
int c = 0;
for (DataRow row : data) {
c++;
exec.checkCanceled();
exec.setProgress(c / (double) data.getRowCount());
for (int col : valuesI) {
double val = ((DoubleValue) row.getCell(col)).getDoubleValue();
if (min.get(col) > val) {
min.put(col, val);
}
if (max.get(col) < val) {
min.put(col, val);
}
}
}
List<DataColumnSpec> newColSpecList = new ArrayList<DataColumnSpec>();
int cc = 0;
for (DataColumnSpec columnSpec : data.getDataTableSpec()) {
if (recalcValuesFor.contains(columnSpec.getName())) {
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(columnSpec);
DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(new DoubleCell(min.get(cc)), new DoubleCell(max.get(cc)));
specCreator.setDomain(domainCreator.createDomain());
DataColumnSpec newColSpec = specCreator.createSpec();
newColSpecList.add(newColSpec);
} else {
newColSpecList.add(columnSpec);
}
cc++;
}
DataTableSpec spec = new DataTableSpec(newColSpecList.toArray(new DataColumnSpec[0]));
BufferedDataTable newDataTable = exec.createSpecReplacerTable(data, spec);
return newDataTable;
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class ColCombineNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
ColumnRearranger result = new ColumnRearranger(spec);
DataColumnSpec append = new DataColumnSpecCreator(m_newColName, StringCell.TYPE).createSpec();
final int[] indices = new int[m_columns.length];
List<String> colNames = Arrays.asList(m_columns);
int j = 0;
for (int k = 0; k < spec.getNumColumns(); k++) {
DataColumnSpec cs = spec.getColumnSpec(k);
if (colNames.contains(cs.getName())) {
indices[j] = k;
j++;
}
}
// ", " -> ","
// " " -> " " (do not let the resulting string be empty)
// " bla bla " -> "bla bla"
final String delimTrim = trimDelimString(m_delimString);
result.append(new SingleCellFactory(append) {
@Override
public DataCell getCell(final DataRow row) {
String[] cellContents = new String[indices.length];
for (int i = 0; i < indices.length; i++) {
DataCell c = row.getCell(indices[i]);
String s = c instanceof StringValue ? ((StringValue) c).getStringValue() : c.toString();
cellContents[i] = s;
}
return new StringCell(handleContent(cellContents, delimTrim));
}
});
return result;
}
Aggregations