use of org.knime.core.data.DataRow in project knime-core by knime.
the class DateShiftConfigure method getColumnbasedCellFactory.
/**
* @param spec the output column spec
* @param col1Idx the column index of the numerical column to add
* @param g the time field to modify (as defined by calendar constants)
* @param conf the configuration object
* @param col2Idx the time column
* @return the cell factory
*/
public static SingleCellFactory getColumnbasedCellFactory(final DataColumnSpec spec, final int col1Idx, final int col2Idx, final int g, final DateShiftConfigure conf) {
return new SingleCellFactory(spec) {
/**
* Value for the new column is based on the values of two column of the row (first and second date column),
* the selected granularity, and the fraction digits for rounding.
*
* @param row the current row
* @return the difference between the two date values with the given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
final int value;
DataCell cell2 = row.getCell(col2Idx);
if (cell2.isMissing()) {
return DataType.getMissingCell();
}
String typeofshift = conf.gettypeofshift().getStringValue();
if (typeofshift.equals(DateShiftNodeDialog.CFG_COLUMN_SHIFT)) {
DataCell cell1 = row.getCell(col1Idx);
if ((cell1.isMissing())) {
return DataType.getMissingCell();
}
value = ((IntValue) cell1).getIntValue();
} else {
value = conf.getvalueofshift().getIntValue();
}
Calendar c = ((DateAndTimeValue) cell2).getUTCCalendarClone();
c.add(g, value);
return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
}
};
}
use of org.knime.core.data.DataRow 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.DataRow in project knime-core by knime.
the class InMemoryIterator method next.
/**
* {@inheritDoc}
*/
@Override
public DataRow next() {
if (m_nextRow == null) {
if (getNextMatch() == null) {
throw new NoSuchElementException("No more rows");
}
}
DataRow row;
if (m_inverted) {
row = new JoinedRow(m_nextRow[1], m_nextRow[0]);
} else {
row = new JoinedRow(m_nextRow[0], m_nextRow[1]);
}
m_nextRow = null;
return row;
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class JoinedTableRowIterator method next.
/**
* {@inheritDoc}
*/
@Override
public DataRow next() {
if (m_leftIt.hasNext()) {
final DataRow left = m_leftIt.next();
final RowKey leftID = left.getKey();
assert (!leftID.equals(m_lastRightID));
DataRow right;
RowKey rightID;
boolean cont = true;
do {
right = null;
rightID = null;
if (!m_rightIt.hasNext() && initNewRightIterator()) {
break;
}
right = nextRight();
rightID = right.getKey();
boolean madeWholeLoop = (m_lastRightID == null && !m_rightIt.hasNext()) || rightID.equals(m_lastRightID);
cont = !madeWholeLoop && !rightID.equals(leftID);
if (cont) {
if (!m_hasSkippedRight) {
if (!m_table.isPrintedErrorOnSorting()) {
LOGGER.warn("Either both tables don't have all rows in " + "common or they are sorted differently.");
LOGGER.warn("(Iteration may have quadratic complexity " + "to ensure that all matching rows are " + "found.");
LOGGER.warn("I'll suppress further warnings.");
m_table.setPrintedErrorOnSorting(true);
}
m_hasSkippedRight = true;
}
}
} while (cont);
// no matching right row found
if (!leftID.equals(rightID)) {
right = getRightMissing(leftID);
} else {
m_lastRightID = rightID;
assert (rightID.equals(leftID));
m_rightSet.set(m_rightItCounter);
}
if (!m_leftIt.hasNext()) {
// (according to the bit set m_rightSet)
if (m_hasSkippedRight) {
initNewRightIterator();
}
m_lastRightID = null;
m_nextRightRow = findNextRightRow();
}
return new JoinedRow(left, right);
} else {
// in a perfect world, you don't come here...
DataRow maybeNext = findNextRightRow();
DataRow left = getLeftMissing(m_nextRightRow.getKey());
DataRow merged = new JoinedRow(left, m_nextRightRow);
m_nextRightRow = maybeNext;
return merged;
}
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class ReadPNGFromURLNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec in, final AtomicLong failCounter) throws InvalidSettingsException {
String colName = m_config.getUrlColName();
if (colName == null) {
// throws ISE
m_config.guessDefaults(in);
colName = m_config.getUrlColName();
setWarningMessage("Auto-configuration: Guessing column \"" + colName + "\" to contain locations");
}
final int colIndex = in.findColumnIndex(colName);
if (colIndex < 0) {
throw new InvalidSettingsException("No such column in input: " + colName);
}
DataColumnSpec colSpec = in.getColumnSpec(colIndex);
if (!colSpec.getType().isCompatible(StringValue.class)) {
throw new InvalidSettingsException("Selected column \"" + colName + "\" is not string-compatible");
}
final String newColName = m_config.getNewColumnName();
DataColumnSpecCreator colSpecCreator;
if (newColName != null) {
String newName = DataTableSpec.getUniqueColumnName(in, newColName);
colSpecCreator = new DataColumnSpecCreator(newName, PNGImageContent.TYPE);
} else {
colSpecCreator = new DataColumnSpecCreator(colSpec);
colSpecCreator.setType(PNGImageContent.TYPE);
colSpecCreator.removeAllHandlers();
colSpecCreator.setDomain(null);
}
DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
ColumnRearranger rearranger = new ColumnRearranger(in);
CellFactory fac = new SingleCellFactory(outColumnSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing()) {
return DataType.getMissingCell();
} else {
String url = ((StringValue) cell).getStringValue();
try {
return toPNGCell(url);
} catch (Exception e) {
if (m_config.isFailOnInvalid()) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e.getMessage(), e);
}
} else {
String message = "Failed to read png content from " + "\"" + url + "\": " + e.getMessage();
LOGGER.warn(message, e);
failCounter.incrementAndGet();
return DataType.getMissingCell();
}
}
}
}
};
if (newColName == null) {
rearranger.replace(fac, colIndex);
} else {
rearranger.append(fac);
}
return rearranger;
}
Aggregations