use of org.knime.core.data.DataTableSpec in project knime-core by knime.
the class TimePresetNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
// does input spec contain a date and time col?
DataTableSpec inSpec = inSpecs[0];
if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
throw new InvalidSettingsException("Input table must contain at least one time column!");
}
String selectedColName = m_selectedCol.getStringValue();
if (selectedColName != null && !selectedColName.isEmpty()) {
// already set -> search for column name in input table
if (!inSpec.containsName(selectedColName)) {
throw new InvalidSettingsException("Column " + selectedColName + " not found in input table!");
} else {
// check if it is of correct type
DataColumnSpec colSpec = inSpec.getColumnSpec(selectedColName);
if (!colSpec.getType().isCompatible(DateAndTimeValue.class)) {
throw new InvalidSettingsException("Selected column (" + selectedColName + ") must contain date or time!");
}
}
} else {
// not yet set -> auto-configure: choose first time column
for (DataColumnSpec colSpec : inSpec) {
if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
String colName = colSpec.getName();
m_selectedCol.setStringValue(colName);
setWarningMessage("Auto-configure: selected " + colName);
// take the first compatible column
break;
}
}
}
// values in time column are "enriched"
return inSpecs;
}
use of org.knime.core.data.DataTableSpec in project knime-core by knime.
the class JoinedTable method createSpec.
/**
* Creates a new DataTableSpec as an result of merging a <code>left</code>
* and a <code>right</code> table. Duplicate of column names are treated
* as given by the <code>duplicateMethod</code> argument, i.e.
* <ul>
* <li> {@link #METHOD_FAIL} throw exception</li>
* <li> {@link #METHOD_FILTER} ignore duplicates in the right column</li>
* <li> {@link #METHOD_APPEND_SUFFIX} append a suffix given by the
* <code>suffix</code> argument to occuring duplicates</li>
* </ul>
*
* @param left the left part of the this table
* @param right and the corresponding right part
* @param duplicateMethod the method on how to treat duplicates
* @param suffix the suffix that is used when the method is
* {@link #METHOD_APPEND_SUFFIX}. In case of another any other
* method this argument is ignored.
* @return the spec as result of merging both table specs
* @throws IllegalArgumentException in case of duplicate column names and no
* special treatment is requested
* @throws NullPointerException if either table is <code>null</code>
*/
public static final DataTableSpec createSpec(final DataTableSpec left, final DataTableSpec right, final String duplicateMethod, final String suffix) {
DataColumnSpec[] leftCols;
DataColumnSpec[] rightCols;
if (METHOD_FAIL.equals(duplicateMethod)) {
leftCols = new DataColumnSpec[left.getNumColumns()];
rightCols = new DataColumnSpec[right.getNumColumns()];
Set<String> hash = new HashSet<String>();
for (int i = 0; i < left.getNumColumns(); i++) {
leftCols[i] = left.getColumnSpec(i);
hash.add(leftCols[i].getName());
}
for (int i = 0; i < right.getNumColumns(); i++) {
rightCols[i] = right.getColumnSpec(i);
if (hash.contains(rightCols[i].getName())) {
throw new IllegalArgumentException("Duplicate column: " + rightCols[i].getName());
}
}
} else if (METHOD_FILTER.equals(duplicateMethod)) {
String[] survivers = getSurvivers(left, right);
DataTableSpec newRight = FilterColumnTable.createFilterTableSpec(right, survivers);
leftCols = new DataColumnSpec[left.getNumColumns()];
rightCols = new DataColumnSpec[newRight.getNumColumns()];
for (int i = 0; i < left.getNumColumns(); i++) {
leftCols[i] = left.getColumnSpec(i);
}
for (int i = 0; i < newRight.getNumColumns(); i++) {
rightCols[i] = newRight.getColumnSpec(i);
}
} else if (METHOD_APPEND_SUFFIX.equals(duplicateMethod)) {
final int rightColCount = right.getNumColumns();
HashSet<String> newInvented = new HashSet<String>();
DataColumnSpec[] newCols = new DataColumnSpec[rightColCount];
for (int i = 0; i < rightColCount; i++) {
DataColumnSpec col = right.getColumnSpec(i);
String name = col.getName();
boolean invented = false;
while (left.containsName(name) || newInvented.contains(name)) {
invented = true;
do {
name = name.toString() + suffix;
// we need also the keep track that we don't "invent" a
// name that is used in the right table already
} while (right.containsName(name));
}
if (invented) {
newInvented.add(name);
DataColumnSpecCreator creator = new DataColumnSpecCreator(col);
creator.setName(name);
newCols[i] = creator.createSpec();
} else {
newCols[i] = col;
}
}
DataTableSpec newRight = new DataTableSpec(newCols);
leftCols = new DataColumnSpec[left.getNumColumns()];
rightCols = new DataColumnSpec[newRight.getNumColumns()];
for (int i = 0; i < left.getNumColumns(); i++) {
leftCols[i] = left.getColumnSpec(i);
}
for (int i = 0; i < right.getNumColumns(); i++) {
rightCols[i] = newRight.getColumnSpec(i);
}
} else {
throw new IllegalArgumentException("Unknown method: " + duplicateMethod);
}
boolean isLeftContainColorHandler = false;
boolean isLeftContainSizeHandler = false;
boolean isLeftContainShapeHandler = false;
for (DataColumnSpec s : leftCols) {
isLeftContainColorHandler |= s.getColorHandler() != null;
isLeftContainSizeHandler |= s.getSizeHandler() != null;
isLeftContainShapeHandler |= s.getShapeHandler() != null;
}
for (int i = 0; i < rightCols.length; i++) {
DataColumnSpec s = rightCols[i];
boolean removeColorHandler = false;
if (s.getColorHandler() != null && isLeftContainColorHandler) {
removeColorHandler = true;
}
boolean removeSizeHandler = false;
if (s.getSizeHandler() != null && isLeftContainSizeHandler) {
removeSizeHandler = true;
}
boolean removeShapeHandler = false;
if (s.getShapeHandler() != null && isLeftContainShapeHandler) {
removeShapeHandler = true;
}
if (removeColorHandler || removeSizeHandler || removeShapeHandler) {
DataColumnSpecCreator c = new DataColumnSpecCreator(s);
if (removeColorHandler) {
c.setColorHandler(null);
}
if (removeSizeHandler) {
c.setSizeHandler(null);
}
if (removeShapeHandler) {
c.setShapeHandler(null);
}
rightCols[i] = c.createSpec();
}
}
DataColumnSpec[] sp = new DataColumnSpec[leftCols.length + rightCols.length];
System.arraycopy(leftCols, 0, sp, 0, leftCols.length);
System.arraycopy(rightCols, 0, sp, leftCols.length, rightCols.length);
return new DataTableSpec(sp);
}
use of org.knime.core.data.DataTableSpec in project knime-core by knime.
the class StatisticsTable method calculateAllMoments.
/**
* Calculates <b>all the statistical moments in one pass </b>. After the
* call of this operation, the statistical moments can be obtained very fast
* from all the other methods.
*
* @param rowCount Row count of table for progress, may be NaN if unknown.
* @param exec object to check with if user canceled the operation
* @throws CanceledExecutionException if user canceled
* @throws IllegalArgumentException if rowCount argument < 0
*/
protected void calculateAllMoments(final double rowCount, final ExecutionMonitor exec) throws CanceledExecutionException {
if (rowCount < 0.0) {
throw new IllegalArgumentException("rowCount argument must not < 0: " + rowCount);
}
DataTableSpec origSpec = m_table.getDataTableSpec();
int numOfCols = origSpec.getNumColumns();
// the number of non-missing cells in each column
int[] validCount = new int[numOfCols];
double[] sumsquare = new double[numOfCols];
final DataValueComparator[] comp = new DataValueComparator[numOfCols];
for (int i = 0; i < numOfCols; i++) {
sumsquare[i] = 0.0;
validCount[i] = 0;
comp[i] = origSpec.getColumnSpec(i).getType().getComparator();
assert comp[i] != null;
}
int nrRows = 0;
for (RowIterator rowIt = m_table.iterator(); rowIt.hasNext(); nrRows++) {
DataRow row = rowIt.next();
if (exec != null) {
double prog = Double.isNaN(rowCount) ? 0.0 : nrRows / rowCount;
exec.setProgress(prog, "Calculating statistics, processing row " + (nrRows + 1) + " (\"" + row.getKey() + "\")");
// throws exception if user canceled
exec.checkCanceled();
}
for (int c = 0; c < numOfCols; c++) {
final DataCell cell = row.getCell(c);
if (!(cell.isMissing())) {
// keep the min and max for each column
if ((m_minValues[c] == null) || (comp[c].compare(cell, m_minValues[c]) < 0)) {
m_minValues[c] = cell;
}
if ((m_maxValues[c] == null) || (comp[c].compare(m_maxValues[c], cell) < 0)) {
m_maxValues[c] = cell;
}
// for double columns we calc the sum (for the mean calc)
DataType type = origSpec.getColumnSpec(c).getType();
if (type.isCompatible(DoubleValue.class)) {
double d = ((DoubleValue) cell).getDoubleValue();
if (Double.isNaN(m_sum[c])) {
m_sum[c] = d;
} else {
m_sum[c] += d;
}
sumsquare[c] += d * d;
validCount[c]++;
}
} else {
m_missingValueCnt[c]++;
}
}
calculateMomentInSubClass(row);
}
m_nrRows = nrRows;
for (int j = 0; j < numOfCols; j++) {
// missing values
if (validCount[j] == 0 || m_minValues[j] == null) {
DataCell mc = DataType.getMissingCell();
m_minValues[j] = mc;
m_maxValues[j] = mc;
m_meanValues[j] = Double.NaN;
m_varianceValues[j] = Double.NaN;
} else {
m_meanValues[j] = m_sum[j] / validCount[j];
if (validCount[j] > 1) {
m_varianceValues[j] = (sumsquare[j] - ((m_sum[j] * m_sum[j]) / validCount[j])) / (validCount[j] - 1);
} else {
m_varianceValues[j] = 0.0;
}
// round-off errors resulting in negative variance values
if (m_varianceValues[j] < 0.0 && m_varianceValues[j] > -1.0E8) {
m_varianceValues[j] = 0.0;
}
assert m_varianceValues[j] >= 0.0 : "Variance cannot be negative (column \"" + origSpec.getColumnSpec(j).getName() + "\": " + m_varianceValues[j];
}
}
// compute resulting table spec
int nrCols = m_table.getDataTableSpec().getNumColumns();
DataColumnSpec[] cSpec = new DataColumnSpec[nrCols];
for (int c = 0; c < nrCols; c++) {
DataColumnSpec s = m_table.getDataTableSpec().getColumnSpec(c);
// we create domains with our bounds.
Set<DataCell> values = (s.getDomain() == null ? null : s.getDomain().getValues());
DataColumnDomain newDomain = new DataColumnDomainCreator(values, (m_minValues[c] == null || m_minValues[c].isMissing()) ? null : m_minValues[c], (m_maxValues[c] == null || m_maxValues[c].isMissing()) ? null : m_maxValues[c]).createDomain();
DataColumnSpecCreator creator = new DataColumnSpecCreator(s);
creator.setDomain(newDomain);
cSpec[c] = creator.createSpec();
}
m_tSpec = new DataTableSpec(cSpec);
}
use of org.knime.core.data.DataTableSpec in project knime-core by knime.
the class JoinedTableRowIterator method getLeftMissing.
/* Called when there is no corresponding left row to the right one. */
private DataRow getLeftMissing(final RowKey key) {
DataTableSpec spec = m_table.getLeftTable().getDataTableSpec();
if (m_leftMissingCells == null) {
LOGGER.debug("Creating missing values for top table on key \"" + key + "\"");
if (!m_table.isPrintedErrorOnMissing()) {
printMissingError(true);
m_table.setPrintedErrorOnMissing(true);
}
m_leftMissingCells = JoinedTable.createMissingCells(spec);
}
return new DefaultRow(key, m_leftMissingCells);
}
use of org.knime.core.data.DataTableSpec in project knime-core by knime.
the class JoinedTableRowIterator method getRightMissing.
/* Called when there is no corresponding right row to the left one. */
private DataRow getRightMissing(final RowKey key) {
DataTableSpec spec = m_table.getRightTable().getDataTableSpec();
if (m_rightMissingCells == null) {
LOGGER.debug("Creating missing values for bottom table on key \"" + key + "\"");
if (!m_table.isPrintedErrorOnMissing()) {
printMissingError(false);
m_table.setPrintedErrorOnMissing(true);
}
m_rightMissingCells = JoinedTable.createMissingCells(spec);
}
return new DefaultRow(key, m_rightMissingCells);
}
Aggregations