Search in sources :

Example 56 with DataColumnDomainCreator

use of org.knime.core.data.DataColumnDomainCreator 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 57 with DataColumnDomainCreator

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

the class SubgroupMinerModel method createItemsetOutputSpec.

private DataTableSpec createItemsetOutputSpec() {
    /*
         * creating the ouput spec with (maxDepth + 1) String columns and the
         * first column as an int colum (the support)
         */
    DataColumnSpec[] colSpecs = new DataColumnSpec[m_maxItemSetLength.getIntValue() + 1];
    DataColumnSpecCreator colspeccreator = new DataColumnSpecCreator("Support(0-1):", DoubleCell.TYPE);
    colspeccreator.setDomain(new DataColumnDomainCreator(new DoubleCell(0), new DoubleCell(1)).createDomain());
    colSpecs[0] = colspeccreator.createSpec();
    for (int i = 1; i < m_maxItemSetLength.getIntValue() + 1; i++) {
        colSpecs[i] = new DataColumnSpecCreator("Item_" + i, StringCell.TYPE).createSpec();
    }
    return new DataTableSpec(colSpecs);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Aggregations

DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)57 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)51 DataColumnSpec (org.knime.core.data.DataColumnSpec)43 DoubleCell (org.knime.core.data.def.DoubleCell)28 DataCell (org.knime.core.data.DataCell)27 DataTableSpec (org.knime.core.data.DataTableSpec)26 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)15 ArrayList (java.util.ArrayList)14 DataColumnDomain (org.knime.core.data.DataColumnDomain)12 DataRow (org.knime.core.data.DataRow)12 DataType (org.knime.core.data.DataType)12 DoubleValue (org.knime.core.data.DoubleValue)11 StringCell (org.knime.core.data.def.StringCell)8 BufferedDataTable (org.knime.core.node.BufferedDataTable)7 LinkedHashSet (java.util.LinkedHashSet)6 Coordinate (org.knime.base.util.coordinate.Coordinate)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 LinkedHashMap (java.util.LinkedHashMap)5 NumericCoordinate (org.knime.base.util.coordinate.NumericCoordinate)5