Search in sources :

Example 21 with DataValue

use of org.knime.core.data.DataValue in project knime-core by knime.

the class ExtendedStatisticsNodeModel method execute.

/**
 * {@inheritDoc}
 *
 * @throws CanceledExecutionException
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException {
    double initPercent = m_enableHiLite.getBooleanValue() ? .25 : .2;
    ExecutionContext init = exec.createSubExecutionContext(initPercent);
    DataTableSpec dataSpec = inData[0].getDataTableSpec();
    List<String> includes = nominalColumns(dataSpec);
    m_statTable = new Statistics3Table(inData[0], m_computeMedian.getBooleanValue(), numOfNominalValuesOutput(), includes, init);
    if (getStatTable().getWarning() != null) {
        setWarningMessage(getStatTable().getWarning());
    }
    BufferedDataTable outTableOccurrences = exec.createBufferedDataTable(getStatTable().createNominalValueTable(includes), exec.createSubProgress(0.5));
    BufferedDataTable[] ret = new BufferedDataTable[3];
    DataTableSpec newSpec = renamedOccurrencesSpec(outTableOccurrences.getSpec());
    ret[2] = exec.createSpecReplacerTable(outTableOccurrences, newSpec);
    ExecutionContext table = exec.createSubExecutionContext(initPercent);
    ret[0] = getStatTable().createStatisticsInColumnsTable(table);
    ExecutionContext histogram = exec.createSubExecutionContext(1.0 / 2);
    final HistogramColumn histogramColumn = createHistogramColumn();
    HiLiteHandler hlHandler = getEnableHiLite().getBooleanValue() ? getInHiLiteHandler(0) : new HiLiteHandler();
    double[] mins = getStatTable().getMin(), maxes = getStatTable().getMax(), means = getStatTable().getMean();
    for (int i = 0; i < maxes.length; i++) {
        DataCell min = getStatTable().getNonInfMin(i);
        if (min.isMissing()) {
            mins[i] = Double.NaN;
        } else {
            mins[i] = ((DoubleValue) min).getDoubleValue();
        }
        DataCell max = getStatTable().getNonInfMax(i);
        if (max.isMissing()) {
            maxes[i] = Double.NaN;
        } else {
            maxes[i] = ((DoubleValue) max).getDoubleValue();
        }
    }
    Pair<BufferedDataTable, Map<Integer, ? extends HistogramModel<?>>> pair = histogramColumn.process(histogram, inData[0], hlHandler, ret[0], mins, maxes, means, numOfNominalValues(), getColumnNames());
    // final BufferedDataTable outTable =
    // histogramColumn.appendNominal(pair.getFirst(), getStatTable(), hlHandler, exec, numOfNominalValues());
    ret[0] = pair.getFirst();
    ret[1] = histogramColumn.nominalTable(getStatTable(), hlHandler, exec, numOfNominalValues());
    if (m_enableHiLite.getBooleanValue()) {
        double rest = 1 - initPercent * 2 - 1.0 / 2;
        ExecutionContext projection = exec.createSubExecutionContext(rest / 2);
        ColumnRearranger rearranger = new ColumnRearranger(dataSpec);
        Set<String> colNames = new HashSet<String>(Arrays.asList(getColumnNames()));
        for (DataColumnSpec spec : rearranger.createSpec()) {
            if ((!spec.getType().isCompatible(DoubleValue.class) && !spec.getType().isCompatible(NominalValue.class)) || !colNames.contains(spec.getName())) {
                rearranger.remove(spec.getName());
            }
        }
        ExecutionContext save = exec.createSubExecutionContext(rest / 2);
        m_subTable = new DefaultDataArray(projection.createColumnRearrangeTable(inData[0], rearranger, projection), 1, inData[0].getRowCount(), save);
        m_histograms = histogramColumn.histograms(inData[0], getInHiLiteHandler(0), mins, maxes, means, getColumnNames());
        Set<String> nominalColumns = new LinkedHashSet<String>();
        for (int i = 0; i < inData[0].getSpec().getNumColumns(); ++i) {
            Map<DataCell, Integer> nominalValues = getStatTable().getNominalValues(i);
            if (nominalValues != null) {
                nominalColumns.add(inData[0].getSpec().getColumnSpec(i).getName());
            }
        }
        final Pair<Map<Integer, Map<Integer, Set<RowKey>>>, Map<Integer, Map<DataValue, Set<RowKey>>>> bucketsAndNominals = HistogramColumn.construct(m_histograms, m_subTable, nominalColumns);
        m_buckets = bucketsAndNominals.getFirst();
        m_nominalKeys = bucketsAndNominals.getSecond();
    } else {
        m_histograms = pair.getSecond();
    }
    return ret;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) DataValue(org.knime.core.data.DataValue) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) HistogramColumn(org.knime.base.data.statistics.HistogramColumn) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HistogramModel(org.knime.base.data.statistics.HistogramModel) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) ExecutionContext(org.knime.core.node.ExecutionContext) Statistics3Table(org.knime.base.data.statistics.Statistics3Table) DataCell(org.knime.core.data.DataCell) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with DataValue

use of org.knime.core.data.DataValue in project knime-core by knime.

the class RuleEngineFilterNodeModel method execute.

/**
 * The real worker.
 * @param inData The input data as {@link RowInput}.
 * @param outputs The output tables.
 * @param rowCount The row count (if available, else {@code -1} is fine).
 * @param exec The {@link ExecutionContext}.
 * @throws ParseException Parsing of rules failed.
 * @throws CanceledExecutionException Execution cancelled.
 * @throws InterruptedException Streaming failed.
 */
private void execute(final RowInput inData, final RowOutput[] outputs, final long rowCount, final ExecutionContext exec) throws ParseException, CanceledExecutionException, InterruptedException {
    final List<Rule> rules = parseRules(inData.getDataTableSpec(), RuleNodeSettings.RuleFilter);
    final int matchIndex = m_includeOnMatch.getBooleanValue() ? 0 : 1;
    final int otherIndex = 1 - matchIndex;
    try {
        final long[] rowIdx = new long[] { 0L };
        final long rows = rowCount;
        final VariableProvider provider = new VariableProvider() {

            @Override
            public Object readVariable(final String name, final Class<?> type) {
                return RuleEngineFilterNodeModel.this.readVariable(name, type);
            }

            @Override
            public long getRowCountLong() {
                return rows;
            }

            @Deprecated
            @Override
            public int getRowCount() {
                return KnowsRowCountTable.checkRowCount(rows);
            }

            @Override
            @Deprecated
            public int getRowIndex() {
                return (int) rowIdx[0];
            }

            @Override
            public long getRowIndexLong() {
                return rowIdx[0];
            }
        };
        DataRow row;
        while ((row = inData.poll()) != null) {
            rowIdx[0]++;
            exec.setProgress(rowIdx[0] / (double) rows, () -> "Adding row " + rowIdx[0] + " of " + rows);
            exec.checkCanceled();
            boolean wasMatch = false;
            for (Rule r : rules) {
                if (r.getCondition().matches(row, provider).getOutcome() == MatchState.matchedAndStop) {
                    // r.getSideEffect().perform(row, provider);
                    DataValue value = r.getOutcome().getComputedResult(row, provider);
                    final int index;
                    if (value instanceof BooleanValue) {
                        final BooleanValue bv = (BooleanValue) value;
                        index = bv.getBooleanValue() ? matchIndex : otherIndex;
                    } else {
                        index = matchIndex;
                    }
                    if (index < outputs.length) {
                        outputs[index].push(row);
                    }
                    wasMatch = true;
                    break;
                }
            }
            if (!wasMatch) {
                if (otherIndex < outputs.length) {
                    outputs[otherIndex].push(row);
                }
            }
        }
    } finally {
        outputs[0].close();
        if (outputs.length > 1) {
            outputs[1].close();
        }
    }
}
Also used : DataValue(org.knime.core.data.DataValue) BooleanValue(org.knime.core.data.BooleanValue) DataRow(org.knime.core.data.DataRow)

Example 23 with DataValue

use of org.knime.core.data.DataValue in project knime-core by knime.

the class RuleEngineNodeModel method updateColSpec.

/**
 * Updates the prediction column specification if the rule outcomes are computable in advance.
 * <br/>
 * This will add all outcomes, not just the possibles.
 * <br/>
 * Sorry for the high complexity.
 *
 * @param rules The {@link Rule}s we want to analyse.
 * @param outType The output data type.
 * @param colSpecCreator The column creator.
 */
private static void updateColSpec(final List<Rule> rules, final DataType outType, final DataColumnSpecCreator colSpecCreator, final FlowVariableProvider nm) {
    List<DataValue> results = new ArrayList<DataValue>(rules.size());
    for (Rule rule : rules) {
        try {
            DataValue result = rule.getOutcome().getComputedResult(new DefaultRow("", new double[0]), new VariableProvider() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                @Deprecated
                public int getRowCount() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long getRowCountLong() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                @Deprecated
                public int getRowIndex() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long getRowIndexLong() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                public Object readVariable(final String arg0, final Class<?> arg1) {
                    return nm.readVariable(arg0, arg1);
                }
            });
            results.add(result);
        } catch (RuntimeException e) {
            // We stop, cannot update properly
            return;
        }
    }
    Set<DataCell> values = new LinkedHashSet<DataCell>(results.size());
    if (outType.equals(StringCell.TYPE)) {
        for (DataValue dataValue : results) {
            if (dataValue instanceof StringCell) {
                values.add((StringCell) dataValue);
            } else if (dataValue instanceof StringValue) {
                StringValue sv = (StringValue) dataValue;
                values.add(new StringCell(sv.getStringValue()));
            } else {
                values.add(new StringCell(dataValue.toString()));
            }
        }
        colSpecCreator.setDomain(new DataColumnDomainCreator(values).createDomain());
    } else if (outType.isCompatible(DoubleValue.class)) {
        DataCell min = new DoubleCell(Double.POSITIVE_INFINITY), max = new DoubleCell(Double.NEGATIVE_INFINITY);
        for (DataValue dataValue : results) {
            if (dataValue instanceof DoubleValue) {
                DoubleValue dv = (DoubleValue) dataValue;
                double d = dv.getDoubleValue();
                min = d < ((DoubleValue) min).getDoubleValue() ? (DataCell) dv : min;
                max = d > ((DoubleValue) max).getDoubleValue() ? (DataCell) dv : max;
                values.add((DataCell) dv);
            }
        }
        DataColumnDomainCreator dcdc = new DataColumnDomainCreator();
        if (min instanceof DoubleValue && max instanceof DoubleValue) {
            double mi = ((DoubleValue) min).getDoubleValue(), ma = ((DoubleValue) max).getDoubleValue();
            if (mi != Double.POSITIVE_INFINITY && ma != Double.NEGATIVE_INFINITY && !Double.isNaN(mi) && !Double.isNaN(ma)) {
                dcdc.setLowerBound(min);
                dcdc.setUpperBound(max);
            }
        }
        colSpecCreator.setDomain(dcdc.createDomain());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataValue(org.knime.core.data.DataValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) StringCell(org.knime.core.data.def.StringCell) DoubleValue(org.knime.core.data.DoubleValue) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) StringValue(org.knime.core.data.StringValue)

Example 24 with DataValue

use of org.knime.core.data.DataValue in project knime-core by knime.

the class AggregationColumnPanel method createColumnSelectionMenu.

/**
 * Adds the column selection section to the given menu.
 * This section allows the user to select all rows that are compatible
 * to the chosen data type at once.
 * @param menu the menu to append the column selection section
 */
private void createColumnSelectionMenu(final JPopupMenu menu) {
    final Collection<Class<? extends DataValue>> existingTypes = getAllPresentTypes();
    if (existingTypes.size() < 3) {
        // create no sub menu if their are to few different types
        for (final Class<? extends DataValue> type : existingTypes) {
            if (type == DataValue.class || type == DoubleValue.class) {
                // skip the general and numerical types
                continue;
            }
            final JMenuItem selectCompatible = new JMenuItem("Select " + AggregationMethods.getUserTypeLabel(type).toLowerCase() + " columns");
            selectCompatible.addActionListener(new ActionListener() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void actionPerformed(final ActionEvent e) {
                    selectCompatibleRows(type);
                }
            });
            menu.add(selectCompatible);
        }
    } else {
        // create a column selection sub menu
        final JMenu menuItem = new JMenu("Select all...");
        final JMenuItem supportedMenu = menu.add(menuItem);
        for (final Class<? extends DataValue> type : existingTypes) {
            if (type == DataValue.class || type == DoubleValue.class) {
                // skip the general and numerical types
                continue;
            }
            final JMenuItem selectCompatible = new JMenuItem(AggregationMethods.getUserTypeLabel(type).toLowerCase() + " columns");
            selectCompatible.addActionListener(new ActionListener() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void actionPerformed(final ActionEvent e) {
                    selectCompatibleRows(type);
                }
            });
            supportedMenu.add(selectCompatible);
        }
    }
    // add the select numerical columns entry if they are available
    final Collection<Integer> numericIdxs = getTableModel().getCompatibleRowIdxs(DoubleValue.class);
    if (numericIdxs != null && !numericIdxs.isEmpty()) {
        final JMenuItem selectNoneNumerical = new JMenuItem("Select all numerical columns");
        selectNoneNumerical.addActionListener(new ActionListener() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void actionPerformed(final ActionEvent e) {
                updateSelection(numericIdxs);
            }
        });
        menu.add(selectNoneNumerical);
    }
    // add the select none numerical columns entry if they are available
    final Collection<Integer> nonNumericIdxs = getTableModel().getNotCompatibleRowIdxs(DoubleValue.class);
    if (nonNumericIdxs != null && !nonNumericIdxs.isEmpty()) {
        final JMenuItem selectNoneNumerical = new JMenuItem("Select all non-numerical columns");
        selectNoneNumerical.addActionListener(new ActionListener() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void actionPerformed(final ActionEvent e) {
                updateSelection(nonNumericIdxs);
            }
        });
        menu.add(selectNoneNumerical);
    }
    // add the select all columns entry
    final JMenuItem selectAll = new JMenuItem("Select all columns");
    selectAll.addActionListener(new ActionListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void actionPerformed(final ActionEvent e) {
            getTable().selectAll();
        }
    });
    menu.add(selectAll);
}
Also used : ActionListener(java.awt.event.ActionListener) DataValue(org.knime.core.data.DataValue) DoubleValue(org.knime.core.data.DoubleValue) ActionEvent(java.awt.event.ActionEvent) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Example 25 with DataValue

use of org.knime.core.data.DataValue in project knime-core by knime.

the class AggregationColumnPanel method createColumnSelectionMenu.

/**
 * Adds the column selection section to the given menu.
 * This section allows the user to select all rows that are compatible
 * to the chosen data type at once.
 * @param menu the menu to append the column selection section
 */
private void createColumnSelectionMenu(final JPopupMenu menu) {
    final Collection<Class<? extends DataValue>> existingTypes = getAllPresentTypes();
    if (existingTypes.size() < 3) {
        // create no sub menu if their are to few different types
        for (final Class<? extends DataValue> type : existingTypes) {
            if (type == DataValue.class || type == DoubleValue.class) {
                // skip the general and numerical types
                continue;
            }
            final JMenuItem selectCompatible = new JMenuItem("Select " + AggregationMethods.getUserTypeLabel(type).toLowerCase() + " columns");
            selectCompatible.addActionListener(new ActionListener() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void actionPerformed(final ActionEvent e) {
                    selectCompatibleRows(type);
                }
            });
            menu.add(selectCompatible);
        }
    } else {
        // create a column selection sub menu
        final JMenu menuItem = new JMenu("Select all...");
        final JMenuItem supportedMenu = menu.add(menuItem);
        for (final Class<? extends DataValue> type : existingTypes) {
            if (type == DataValue.class || type == DoubleValue.class) {
                // skip the general and numerical types
                continue;
            }
            final JMenuItem selectCompatible = new JMenuItem(AggregationMethods.getUserTypeLabel(type).toLowerCase() + " columns");
            selectCompatible.addActionListener(new ActionListener() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void actionPerformed(final ActionEvent e) {
                    selectCompatibleRows(type);
                }
            });
            supportedMenu.add(selectCompatible);
        }
    }
    // add the select numerical columns entry if they are available
    final Collection<Integer> numericIdxs = getTableModel().getCompatibleRowIdxs(DoubleValue.class);
    if (numericIdxs != null && !numericIdxs.isEmpty()) {
        final JMenuItem selectNoneNumerical = new JMenuItem("Select all numerical columns");
        selectNoneNumerical.addActionListener(new ActionListener() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void actionPerformed(final ActionEvent e) {
                updateSelection(numericIdxs);
            }
        });
        menu.add(selectNoneNumerical);
    }
    // add the select none numerical columns entry if they are available
    final Collection<Integer> nonNumericIdxs = getTableModel().getNotCompatibleRowIdxs(DoubleValue.class);
    if (nonNumericIdxs != null && !nonNumericIdxs.isEmpty()) {
        final JMenuItem selectNoneNumerical = new JMenuItem("Select all non-numerical columns");
        selectNoneNumerical.addActionListener(new ActionListener() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void actionPerformed(final ActionEvent e) {
                updateSelection(nonNumericIdxs);
            }
        });
        menu.add(selectNoneNumerical);
    }
    // add the select all columns entry
    final JMenuItem selectAll = new JMenuItem("Select all columns");
    selectAll.addActionListener(new ActionListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void actionPerformed(final ActionEvent e) {
            getTable().selectAll();
        }
    });
    menu.add(selectAll);
}
Also used : ActionListener(java.awt.event.ActionListener) DataValue(org.knime.core.data.DataValue) DoubleValue(org.knime.core.data.DoubleValue) ActionEvent(java.awt.event.ActionEvent) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Aggregations

DataValue (org.knime.core.data.DataValue)28 DataCell (org.knime.core.data.DataCell)9 DoubleValue (org.knime.core.data.DoubleValue)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 DataType (org.knime.core.data.DataType)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 StringCell (org.knime.core.data.def.StringCell)5 LinkedHashMap (java.util.LinkedHashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 Set (java.util.Set)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 DataRow (org.knime.core.data.DataRow)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 FileInputStream (java.io.FileInputStream)2 JMenu (javax.swing.JMenu)2