use of org.knime.base.data.statistics.StatisticCalculator in project knime-core by knime.
the class MissingCellReplacingDataTable method init.
/**
* Initializes the statistics for the handlers. Only has to be called if actual replacement should take place.
* @param inTable the actual DataTable which this table wraps.
* @param exec the execution context for the iteration which calculates statistics
* @throws InvalidSettingsException if the statistics from the
* missing cell handlers are conflicting with the table specs
* @throws CanceledExecutionException when the user cancels the execution
*/
public void init(final BufferedDataTable inTable, final ExecutionContext exec) throws InvalidSettingsException, CanceledExecutionException {
m_table = inTable;
// Calculate necessary statistics
ArrayList<Statistic> statistics = new ArrayList<Statistic>();
for (int i = 0; i < m_table.getDataTableSpec().getNumColumns(); i++) {
Statistic s = m_handlers[i].getStatistic();
if (s != null) {
statistics.add(s);
}
}
// Fill the statistics retrieved from the handlers
if (statistics.size() > 0) {
StatisticCalculator calc = new StatisticCalculator(m_table.getDataTableSpec(), statistics.toArray(new Statistic[0]));
String res = calc.evaluate(m_table, exec);
if (res != null) {
addWarningMessage(res);
}
}
}
use of org.knime.base.data.statistics.StatisticCalculator in project knime-core by knime.
the class CronbachNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
final BufferedDataTable in = (BufferedDataTable) inData[0];
final DataTableSpec inSpec = in.getDataTableSpec();
ColumnRearranger filteredTableRearranger = new ColumnRearranger(inSpec);
String[] includeNames = m_columnFilterModel.applyTo(inSpec).getIncludes();
filteredTableRearranger.keepOnly(includeNames);
final BufferedDataTable filteredTable = exec.createColumnRearrangeTable(in, filteredTableRearranger, exec.createSilentSubExecutionContext(0.0));
final DataTableSpec filteredTableSpec = filteredTable.getDataTableSpec();
// step1 get variance for all columns
Variance my = new Variance(filteredTableSpec.getColumnNames());
StatisticCalculator sc = new StatisticCalculator(filteredTableSpec, my);
sc.evaluate(filteredTable, exec.createSubExecutionContext(0.5));
double[] sum = new double[filteredTable.getRowCount()];
// step2 get variance for the overall sum
ExecutionContext exec2 = exec.createSubExecutionContext(0.5);
int rowCount = filteredTable.getRowCount();
int i = 0;
for (DataRow row : filteredTable) {
sum[i] = 0;
exec2.checkCanceled();
exec2.setProgress(i * 1.0 / rowCount, "Statisics calculation row " + i + " of " + rowCount);
for (DataCell cell : row) {
if (!cell.isMissing()) {
double value = ((DoubleValue) cell).getDoubleValue();
sum[i] += value;
} else {
throw new InvalidSettingsException("Missing Values are not supported. " + "Please resolve them with the Missing Value node.");
}
}
i++;
}
exec.setMessage("Caluating Crombach over all Columns");
double cronbach = 0;
for (String s : filteredTableSpec.getColumnNames()) {
cronbach += my.getResult(s);
exec.checkCanceled();
}
org.apache.commons.math3.stat.descriptive.moment.Variance v = new org.apache.commons.math3.stat.descriptive.moment.Variance();
cronbach /= v.evaluate(sum);
double k = filteredTableSpec.getNumColumns();
cronbach = k / (k - 1) * (1.0 - cronbach);
BufferedDataContainer out = exec.createDataContainer(getDataTableSpec());
if (in.getRowCount() <= 0) {
setWarningMessage("Empty input table, no value calculated!");
}
DataRow r = new DefaultRow(new RowKey("Cronbach"), new DoubleCell(cronbach));
out.addRowToTable(r);
out.close();
return new BufferedDataTable[] { out.getTable() };
}
Aggregations