Search in sources :

Example 6 with Interval

use of org.dmg.pmml.IntervalDocument.Interval in project knime-core by knime.

the class PMMLDataDictionaryTranslator method exportTo.

/**
 * Adds a data dictionary to the PMML document based on the
 * {@link DataTableSpec}.
 *
 * @param pmmlDoc the PMML document to export to
 * @param dts the data table spec
 * @return the schema type of the exported schema if applicable, otherwise
 *         null
 * @see #exportTo(PMMLDocument, PMMLPortObjectSpec)
 */
public SchemaType exportTo(final PMMLDocument pmmlDoc, final DataTableSpec dts) {
    DataDictionary dict = DataDictionary.Factory.newInstance();
    dict.setNumberOfFields(BigInteger.valueOf(dts.getNumColumns()));
    DataField dataField;
    for (DataColumnSpec colSpec : dts) {
        dataField = dict.addNewDataField();
        dataField.setName(colSpec.getName());
        DataType dataType = colSpec.getType();
        dataField.setOptype(getOptype(dataType));
        dataField.setDataType(getPMMLDataType(dataType));
        // Value
        if (colSpec.getType().isCompatible(NominalValue.class) && colSpec.getDomain().hasValues()) {
            for (DataCell possVal : colSpec.getDomain().getValues()) {
                Value value = dataField.addNewValue();
                value.setValue(possVal.toString());
            }
        } else if (colSpec.getType().isCompatible(DoubleValue.class) && colSpec.getDomain().hasBounds()) {
            Interval interval = dataField.addNewInterval();
            interval.setClosure(Interval.Closure.CLOSED_CLOSED);
            interval.setLeftMargin(((DoubleValue) colSpec.getDomain().getLowerBound()).getDoubleValue());
            interval.setRightMargin(((DoubleValue) colSpec.getDomain().getUpperBound()).getDoubleValue());
        }
    }
    pmmlDoc.getPMML().setDataDictionary(dict);
    // no schematype available yet
    return null;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataField(org.dmg.pmml.DataFieldDocument.DataField) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) NominalValue(org.knime.core.data.NominalValue) BooleanValue(org.knime.core.data.BooleanValue) IntValue(org.knime.core.data.IntValue) Value(org.dmg.pmml.ValueDocument.Value) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DataDictionary(org.dmg.pmml.DataDictionaryDocument.DataDictionary) Interval(org.dmg.pmml.IntervalDocument.Interval)

Example 7 with Interval

use of org.dmg.pmml.IntervalDocument.Interval in project knime-core by knime.

the class PMMLBinningTranslator method initializeFrom.

@Override
public List<Integer> initializeFrom(final DerivedField[] derivedFields) {
    m_mapper = new DerivedFieldMapper(derivedFields);
    final List<Integer> consumed = new ArrayList<>(derivedFields.length);
    for (int i = 0; i < derivedFields.length; i++) {
        final DerivedField df = derivedFields[i];
        if (!df.isSetDiscretize()) {
            // only reading discretize entries other entries are skipped
            continue;
        }
        consumed.add(i);
        final Discretize discretize = df.getDiscretize();
        @SuppressWarnings("deprecation") final DiscretizeBin[] pmmlBins = discretize.getDiscretizeBinArray();
        final NumericBin[] knimeBins = new NumericBin[pmmlBins.length];
        for (int j = 0; j < pmmlBins.length; j++) {
            final DiscretizeBin bin = pmmlBins[j];
            final String binName = bin.getBinValue();
            final Interval interval = bin.getInterval();
            final double leftValue = interval.getLeftMargin();
            final double rightValue = interval.getRightMargin();
            final Closure.Enum closure = interval.getClosure();
            boolean leftOpen = true;
            boolean rightOpen = true;
            if (Closure.OPEN_CLOSED == closure) {
                rightOpen = false;
            } else if (Closure.CLOSED_OPEN == closure) {
                leftOpen = false;
            } else if (Closure.CLOSED_CLOSED == closure) {
                leftOpen = false;
                rightOpen = false;
            }
            knimeBins[j] = new NumericBin(binName, leftOpen, leftValue, rightOpen, rightValue);
        }
        /**
         * This field contains the name of the column in KNIME that corresponds to the derived field in PMML. This
         * is necessary if derived fields are defined on other derived fields and the columns in KNIME are replaced
         * with the preprocessed values. In this case KNIME has to know the original names (e.g. A) while PMML
         * references to A', A'' etc.
         */
        final String displayName = df.getDisplayName();
        if (displayName != null) {
            m_columnToBins.put(displayName, knimeBins);
            m_columnToAppend.put(displayName, null);
        } else if (df.getName() != null) {
            final String field = m_mapper.getColumnName(discretize.getField());
            m_columnToBins.put(field, knimeBins);
            m_columnToAppend.put(field, df.getName());
        }
    }
    return consumed;
}
Also used : Closure(org.dmg.pmml.IntervalDocument.Interval.Closure) ArrayList(java.util.ArrayList) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) Discretize(org.dmg.pmml.DiscretizeDocument.Discretize) DiscretizeBin(org.dmg.pmml.DiscretizeBinDocument.DiscretizeBin) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) Interval(org.dmg.pmml.IntervalDocument.Interval)

Aggregations

Interval (org.dmg.pmml.IntervalDocument.Interval)7 DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)5 DiscretizeBin (org.dmg.pmml.DiscretizeBinDocument.DiscretizeBin)5 ArrayList (java.util.ArrayList)4 Discretize (org.dmg.pmml.DiscretizeDocument.Discretize)4 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 DataDictionary (org.dmg.pmml.DataDictionaryDocument.DataDictionary)2 DataField (org.dmg.pmml.DataFieldDocument.DataField)2 Closure (org.dmg.pmml.IntervalDocument.Interval.Closure)2 Value (org.dmg.pmml.ValueDocument.Value)2 BooleanValue (org.knime.core.data.BooleanValue)2 DataCell (org.knime.core.data.DataCell)2 DataType (org.knime.core.data.DataType)2 DoubleValue (org.knime.core.data.DoubleValue)2 IntValue (org.knime.core.data.IntValue)2 NominalValue (org.knime.core.data.NominalValue)2 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1