use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class BoxplotCalculator method calculateMultiple.
/**
* Calculates the necessary statistics for a non-conditional boxplot.
* @param table the input data
* @param numCol array of names of numeric columns to plot
* @param exec Execution context to report progress to
* @return LinkedHashMap with the column name as key and statistics as value
* @throws CanceledExecutionException when the user cancels the execution
*/
public LinkedHashMap<String, BoxplotStatistics> calculateMultiple(final BufferedDataTable table, final String[] numCol, final ExecutionContext exec) throws CanceledExecutionException {
DataTableSpec spec = table.getSpec();
int[] numColIdxs = new int[numCol.length];
for (int i = 0; i < numCol.length; i++) {
numColIdxs[i] = spec.findColumnIndex(numCol[i]);
}
LinkedHashMap<String, DataContainer> containers = new LinkedHashMap<String, DataContainer>();
for (int i = 0; i < numCol.length; i++) {
containers.put(numCol[i], exec.createDataContainer(new DataTableSpec(new String[] { "col" }, new DataType[] { DoubleCell.TYPE })));
}
ExecutionContext subExec = exec.createSilentSubExecutionContext(0.7);
long[] numMissValPerCol = new long[numCol.length];
int count = 0;
for (DataRow row : table) {
exec.checkCanceled();
subExec.setProgress((double) count++ / table.size());
for (int i = 0; i < numCol.length; i++) {
DataCell cell = row.getCell(numColIdxs[i]);
if (!cell.isMissing()) {
containers.get(numCol[i]).addRowToTable(new DefaultRow(row.getKey(), cell));
} else {
numMissValPerCol[i]++;
}
}
}
LinkedHashMap<String, BoxplotStatistics> statsMap = new LinkedHashMap<>();
ExecutionContext subExec2 = exec.createSilentSubExecutionContext(1.0);
count = 0;
List<String> excludedDataColList = new ArrayList<String>();
for (Entry<String, DataContainer> entry : containers.entrySet()) {
exec.checkCanceled();
subExec2.setProgress((double) count++ / containers.size());
Set<Outlier> extremeOutliers = new HashSet<Outlier>();
Set<Outlier> mildOutliers = new HashSet<Outlier>();
entry.getValue().close();
BufferedDataTable catTable = (BufferedDataTable) entry.getValue().getTable();
if (catTable.size() == 0) {
excludedDataColList.add(entry.getKey());
continue;
}
SortedTable st = new SortedTable(catTable, new Comparator<DataRow>() {
@Override
public int compare(final DataRow o1, final DataRow o2) {
DataCell c1 = o1.getCell(0);
DataCell c2 = o2.getCell(0);
double d1 = ((DoubleValue) c1).getDoubleValue();
double d2 = ((DoubleValue) c2).getDoubleValue();
if (d1 == d2) {
return 0;
} else {
return d1 < d2 ? -1 : 1;
}
}
}, false, exec);
double min = 0, max = 0, q1 = 0, q3 = 0, median = 0;
boolean dq1 = catTable.size() % 4 == 0;
long q1Idx = catTable.size() / 4;
boolean dq3 = 3 * catTable.size() % 4 == 0;
long q3Idx = 3 * catTable.size() / 4;
boolean dMedian = catTable.size() % 2 == 0;
long medianIdx = catTable.size() / 2;
int counter = 0;
for (DataRow row : st) {
double val = ((DoubleValue) row.getCell(0)).getDoubleValue();
if (counter == 0) {
min = val;
}
if (counter == catTable.size() - 1) {
max = val;
}
if (counter == q1Idx - 1 && dq1) {
q1 = val;
}
if (counter == q1Idx || (counter == 0 && st.size() <= 3)) {
if (dq1) {
q1 = (q1 + val) / 2.0;
} else {
q1 = val;
}
}
if (counter == medianIdx - 1 && dMedian) {
median = val;
}
if (counter == medianIdx) {
if (dMedian) {
median = (median + val) / 2;
} else {
median = val;
}
}
if (counter == q3Idx - 1 && dq3) {
q3 = val;
}
if (counter == q3Idx || (counter == st.size() - 1 && st.size() <= 3)) {
if (dq3) {
q3 = (q3 + val) / 2.0;
} else {
q3 = val;
}
}
counter++;
}
double iqr = q3 - q1;
double lowerWhisker = min;
double upperWhisker = max;
double upperWhiskerFence = q3 + (1.5 * iqr);
double lowerWhiskerFence = q1 - (1.5 * iqr);
double lowerFence = q1 - (3 * iqr);
double upperFence = q3 + (3 * iqr);
for (DataRow row : st) {
double value = ((DoubleValue) row.getCell(0)).getDoubleValue();
String rowKey = row.getKey().getString();
if (value < lowerFence) {
extremeOutliers.add(new Outlier(value, rowKey));
} else if (value < lowerWhiskerFence) {
mildOutliers.add(new Outlier(value, rowKey));
} else if (lowerWhisker < lowerWhiskerFence && value >= lowerWhiskerFence) {
lowerWhisker = value;
} else if (value <= upperWhiskerFence) {
upperWhisker = value;
} else if (value > upperFence) {
extremeOutliers.add(new Outlier(value, rowKey));
} else if (value > upperWhiskerFence) {
mildOutliers.add(new Outlier(value, rowKey));
}
}
statsMap.put(entry.getKey(), new BoxplotStatistics(mildOutliers, extremeOutliers, min, max, lowerWhisker, q1, median, q3, upperWhisker));
}
// missing values part
m_excludedDataCols = excludedDataColList.toArray(new String[excludedDataColList.size()]);
m_numMissValPerCol = new LinkedHashMap<String, Long>();
for (int i = 0; i < numCol.length; i++) {
if (numMissValPerCol[i] > 0 && !excludedDataColList.contains(numCol[i])) {
m_numMissValPerCol.put(numCol[i], numMissValPerCol[i]);
}
}
return statsMap;
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class MemoryGroupByTable method createResultTable.
private BufferedDataTable createResultTable(final ExecutionContext exec, final DataTableSpec resultSpec) throws CanceledExecutionException {
final BufferedDataContainer dc = exec.createDataContainer(resultSpec);
int groupCounter = 0;
final int size = m_vals.size();
for (final Entry<GroupKey, ColumnAggregator[]> entry : m_vals.entrySet()) {
exec.checkCanceled();
exec.setProgress(groupCounter / (double) size, "Writing group " + groupCounter + " of " + size);
final GroupKey groupVals = entry.getKey();
final ColumnAggregator[] colAggregators = entry.getValue();
final RowKey rowKey = RowKey.createRowKey(groupCounter++);
final DataCell[] rowVals = new DataCell[groupVals.size() + colAggregators.length];
// add the group values first
int valIdx = 0;
for (final DataCell groupCell : groupVals.getGroupVals()) {
rowVals[valIdx++] = groupCell;
}
// add the aggregation values
for (final ColumnAggregator colAggr : colAggregators) {
final AggregationOperator operator = colAggr.getOperator(getGlobalSettings());
rowVals[valIdx++] = operator.getResult();
if (operator.isSkipped()) {
// add skipped groups and the column that causes the skipping
// into the skipped groups map
addSkippedGroup(colAggr.getOriginalColName(), operator.getSkipMessage(), groupVals.getGroupVals());
}
// reset the operator for the next group
operator.reset();
}
final DataRow newRow = new DefaultRow(rowKey, rowVals);
dc.addRowToTable(newRow);
// add hilite mappings if enabled
if (isEnableHilite()) {
final Set<RowKey> oldKeys = m_rowKeys.get(groupVals);
addHiliteMapping(rowKey, oldKeys);
}
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class Pivot2NodeModel method write.
private void write(final BufferedDataContainer buf, final DataCell[] outcells) {
for (int j = 0; j < outcells.length; j++) {
if (outcells[j] == null) {
outcells[j] = DataType.getMissingCell();
}
}
final RowKey key = RowKey.createRowKey(buf.size());
final DefaultRow outrow = new DefaultRow(key, outcells);
buf.addRowToTable(outrow);
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class NextValidValueStatisticTB method afterEvaluation.
/**
* {@inheritDoc}
*/
@Override
protected String afterEvaluation() {
// All remaining enqueued cells have no next value and stay missing
for (int i = 0; i < m_numMissing; i++) {
m_nextCells.addRowToTable(new DefaultRow(new RowKey(Integer.toString(m_counter++)), DataType.getMissingCell()));
}
m_nextCells.close();
m_table = m_nextCells.getTable();
return super.afterEvaluation();
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class PMCCPortObjectAndSpec method createCorrelationMatrix.
private DataTable createCorrelationMatrix(final DataContainer cont, final ExecutionMonitor mon) throws CanceledExecutionException {
if (!hasData()) {
throw new IllegalStateException("No data available");
}
final int l = m_colNames.length;
for (int i = 0; i < l; i++) {
RowKey key = new RowKey(m_colNames[i]);
DataCell[] cells = new DataCell[l];
for (int j = 0; j < l; j++) {
if (i == j) {
cells[i] = MAX_VALUE_CELL;
} else {
double corr = m_correlations.get(i, j);
if (Double.isNaN(corr)) {
cells[j] = DataType.getMissingCell();
} else {
cells[j] = new DoubleCell(corr);
}
}
}
mon.checkCanceled();
cont.addRowToTable(new DefaultRow(key, cells));
mon.setProgress(i / (double) l, "Added row " + i);
}
cont.close();
return cont.getTable();
}
Aggregations