Search in sources :

Example 1 with StatisticCalculator

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);
        }
    }
}
Also used : StatisticCalculator(org.knime.base.data.statistics.StatisticCalculator) Statistic(org.knime.base.data.statistics.Statistic) ArrayList(java.util.ArrayList)

Example 2 with StatisticCalculator

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() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) StatisticCalculator(org.knime.base.data.statistics.StatisticCalculator) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) Variance(org.knime.base.data.statistics.calculation.Variance) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ExecutionContext(org.knime.core.node.ExecutionContext) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

StatisticCalculator (org.knime.base.data.statistics.StatisticCalculator)2 ArrayList (java.util.ArrayList)1 Statistic (org.knime.base.data.statistics.Statistic)1 Variance (org.knime.base.data.statistics.calculation.Variance)1 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DoubleValue (org.knime.core.data.DoubleValue)1 RowKey (org.knime.core.data.RowKey)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 ExecutionContext (org.knime.core.node.ExecutionContext)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1