Search in sources :

Example 6 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class PMMLStringConversionTranslator method initializeFrom.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public List<Integer> initializeFrom(final DerivedField[] derivedFields) {
    if (derivedFields == null) {
        return Collections.EMPTY_LIST;
    }
    int num = derivedFields.length;
    List<Integer> consumed = new ArrayList<Integer>(num);
    for (int i = 0; i < derivedFields.length; i++) {
        DerivedField df = derivedFields[i];
        /**
         * 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.
         */
        String displayName = df.getDisplayName();
        if (!df.isSetFieldRef()) {
            // only reading field references
            continue;
        }
        DataType dataType = PMMLDataDictionaryTranslator.getKNIMEDataType(df.getDataType());
        if (dataType.isCompatible(IntValue.class)) {
            m_parseType = IntCell.TYPE;
        } else if (dataType.isCompatible(DoubleValue.class)) {
            m_parseType = DoubleCell.TYPE;
        } else if (dataType == StringCell.TYPE) {
            m_parseType = StringCell.TYPE;
        } else {
            // only processing int, double and string conversions
            continue;
        }
        FieldRef fieldRef = df.getFieldRef();
        if (displayName != null) {
            m_includeCols.add(displayName);
        } else {
            m_includeCols.add(m_mapper.getColumnName(fieldRef.getField()));
        }
        consumed.add(i);
    }
    return consumed;
}
Also used : FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) DoubleValue(org.knime.core.data.DoubleValue) ArrayList(java.util.ArrayList) DataType(org.knime.core.data.DataType) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 7 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class DataColumnSpecFilterPMMLNodeModel method createPMMLOut.

private PMMLPortObject createPMMLOut(final PMMLPortObject pmmlIn, final DataTableSpec outSpec, final FilterResult res) throws XmlException {
    StringBuffer warningBuffer = new StringBuffer();
    if (pmmlIn == null) {
        return new PMMLPortObject(createPMMLSpec(null, outSpec, res));
    } else {
        PMMLDocument pmmldoc;
        try (LockedSupplier<Document> supplier = pmmlIn.getPMMLValue().getDocumentSupplier()) {
            pmmldoc = PMMLDocument.Factory.parse(supplier.get());
        }
        // Inspect models to check if they use any excluded columns
        List<PMMLModelWrapper> models = PMMLModelWrapper.getModelListFromPMMLDocument(pmmldoc);
        for (PMMLModelWrapper model : models) {
            MiningSchema ms = model.getMiningSchema();
            for (MiningField mf : ms.getMiningFieldList()) {
                if (isExcluded(mf.getName(), res)) {
                    if (warningBuffer.length() != 0) {
                        warningBuffer.append("\n");
                    }
                    warningBuffer.append(model.getModelType().name() + " uses excluded column " + mf.getName());
                }
            }
        }
        ArrayList<String> warningFields = new ArrayList<String>();
        PMML pmml = pmmldoc.getPMML();
        // Now check the transformations if they exist
        if (pmml.getTransformationDictionary() != null) {
            for (DerivedField df : pmml.getTransformationDictionary().getDerivedFieldList()) {
                FieldRef fr = df.getFieldRef();
                if (fr != null && isExcluded(fr.getField(), res)) {
                    warningFields.add(fr.getField());
                }
                Aggregate a = df.getAggregate();
                if (a != null && isExcluded(a.getField(), res)) {
                    warningFields.add(a.getField());
                }
                Apply ap = df.getApply();
                if (ap != null) {
                    for (FieldRef fieldRef : ap.getFieldRefList()) {
                        if (isExcluded(fieldRef.getField(), res)) {
                            warningFields.add(fieldRef.getField());
                            break;
                        }
                    }
                }
                Discretize d = df.getDiscretize();
                if (d != null && isExcluded(d.getField(), res)) {
                    warningFields.add(d.getField());
                }
                MapValues mv = df.getMapValues();
                if (mv != null) {
                    for (FieldColumnPair fcp : mv.getFieldColumnPairList()) {
                        if (isExcluded(fcp.getField(), res)) {
                            warningFields.add(fcp.getField());
                        }
                    }
                }
                NormContinuous nc = df.getNormContinuous();
                if (nc != null && isExcluded(nc.getField(), res)) {
                    warningFields.add(nc.getField());
                }
                NormDiscrete nd = df.getNormDiscrete();
                if (nd != null && isExcluded(nd.getField(), res)) {
                    warningFields.add(nd.getField());
                }
            }
        }
        DataDictionary dict = pmml.getDataDictionary();
        List<DataField> fields = dict.getDataFieldList();
        // Apply filter to spec
        int numFields = 0;
        for (int i = fields.size() - 1; i >= 0; i--) {
            if (isExcluded(fields.get(i).getName(), res)) {
                dict.removeDataField(i);
            } else {
                numFields++;
            }
        }
        dict.setNumberOfFields(new BigInteger(Integer.toString(numFields)));
        pmml.setDataDictionary(dict);
        pmmldoc.setPMML(pmml);
        // generate warnings and set as warning message
        for (String s : warningFields) {
            if (warningBuffer.length() != 0) {
                warningBuffer.append("\n");
            }
            warningBuffer.append("Transformation dictionary uses excluded column " + s);
        }
        if (warningBuffer.length() > 0) {
            setWarningMessage(warningBuffer.toString().trim());
        }
        PMMLPortObject outport = null;
        try {
            outport = new PMMLPortObject(createPMMLSpec(pmmlIn.getSpec(), outSpec, res), pmmldoc);
        } catch (IllegalArgumentException e) {
            if (res.getIncludes().length == 0) {
                throw new IllegalArgumentException("Excluding all columns produces invalid PMML", e);
            } else {
                throw e;
            }
        }
        return outport;
    }
}
Also used : MiningField(org.dmg.pmml.MiningFieldDocument.MiningField) NormContinuous(org.dmg.pmml.NormContinuousDocument.NormContinuous) Apply(org.dmg.pmml.ApplyDocument.Apply) ArrayList(java.util.ArrayList) FieldColumnPair(org.dmg.pmml.FieldColumnPairDocument.FieldColumnPair) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) MapValues(org.dmg.pmml.MapValuesDocument.MapValues) Discretize(org.dmg.pmml.DiscretizeDocument.Discretize) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) DataDictionary(org.dmg.pmml.DataDictionaryDocument.DataDictionary) PMMLModelWrapper(org.knime.core.node.port.pmml.PMMLModelWrapper) NormDiscrete(org.dmg.pmml.NormDiscreteDocument.NormDiscrete) MiningSchema(org.dmg.pmml.MiningSchemaDocument.MiningSchema) DataField(org.dmg.pmml.DataFieldDocument.DataField) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMML(org.dmg.pmml.PMMLDocument.PMML) BigInteger(java.math.BigInteger) PMMLDocument(org.dmg.pmml.PMMLDocument) Aggregate(org.dmg.pmml.AggregateDocument.Aggregate) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 8 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class MissingCellHandler method createValueReplacingDerivedField.

/**
 * Helper method for creating a derived field that replaces a field's value with a fixed value.
 * @param dataType the data type of the field.
 * @param value the replacement value for the field
 * @return the derived field
 */
protected DerivedField createValueReplacingDerivedField(final DATATYPE.Enum dataType, final String value) {
    DerivedField field = DerivedField.Factory.newInstance();
    if (dataType == org.dmg.pmml.DATATYPE.STRING || dataType == org.dmg.pmml.DATATYPE.BOOLEAN) {
        field.setOptype(org.dmg.pmml.OPTYPE.CATEGORICAL);
    } else {
        field.setOptype(org.dmg.pmml.OPTYPE.CONTINUOUS);
    }
    /*
         * Create the PMML equivalent of: "if fieldVal is missing then x else fieldVal"
         * <Apply function="if">
         *    <Apply function="isMissing">
         *        <FieldRef field="fieldVal"/>
         *    </Apply>
         *    <Constant dataType="___" value="x"/>
         *    <FieldRef field="fieldVal"/>
         * </Apply>
         */
    Apply ifApply = field.addNewApply();
    ifApply.setFunction(IF_FUNCTION_NAME);
    Apply isMissingApply = Apply.Factory.newInstance();
    FieldRef fieldRef = FieldRef.Factory.newInstance();
    fieldRef.setField(m_col.getName());
    isMissingApply.setFieldRefArray(new FieldRef[] { fieldRef });
    isMissingApply.setFunction(IS_MISSING_FUNCTION_NAME);
    ifApply.setApplyArray(new Apply[] { isMissingApply });
    Constant replacement = Constant.Factory.newInstance();
    replacement.setDataType(dataType);
    replacement.setStringValue(value);
    ifApply.setConstantArray(new Constant[] { replacement });
    ifApply.setFieldRefArray(new FieldRef[] { fieldRef });
    field.setDataType(dataType);
    field.setName(m_col.getName());
    field.setDisplayName(m_col.getName());
    return field;
}
Also used : FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) Apply(org.dmg.pmml.ApplyDocument.Apply) Constant(org.dmg.pmml.ConstantDocument.Constant) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 9 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class PMMLStringConversionTranslator method createDerivedFields.

private DerivedField[] createDerivedFields() {
    DATATYPE.Enum dataType = PMMLDataDictionaryTranslator.getPMMLDataType(m_parseType);
    OPTYPE.Enum optype = PMMLDataDictionaryTranslator.getOptype(m_parseType);
    int num = m_includeCols.size();
    DerivedField[] derivedFields = new DerivedField[num];
    for (int i = 0; i < num; i++) {
        DerivedField df = DerivedField.Factory.newInstance();
        String name = m_includeCols.get(i);
        df.setDisplayName(name);
        /* The field name must be retrieved before creating a new derived
             * name for this derived field as the map only contains the
             * current mapping. */
        String fieldName = m_mapper.getDerivedFieldName(name);
        df.setName(m_mapper.createDerivedFieldName(name));
        df.setDataType(dataType);
        df.setOptype(optype);
        FieldRef fieldRef = df.addNewFieldRef();
        fieldRef.setField(fieldName);
        derivedFields[i] = df;
    }
    return derivedFields;
}
Also used : DATATYPE(org.dmg.pmml.DATATYPE) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) OPTYPE(org.dmg.pmml.OPTYPE) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 10 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class PMMLStringConversionTranslator method initializeFrom.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public List<Integer> initializeFrom(final DerivedField[] derivedFields) {
    if (derivedFields == null) {
        return Collections.EMPTY_LIST;
    }
    int num = derivedFields.length;
    List<Integer> consumed = new ArrayList<Integer>(num);
    for (int i = 0; i < derivedFields.length; i++) {
        DerivedField df = derivedFields[i];
        /**
         * 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.
         */
        String displayName = df.getDisplayName();
        if (!df.isSetFieldRef()) {
            // only reading field references
            continue;
        }
        DataType dataType = PMMLDataDictionaryTranslator.getKNIMEDataType(df.getDataType());
        if (dataType.isCompatible(IntValue.class)) {
            m_parseType = IntCell.TYPE;
        } else if (dataType.isCompatible(DoubleValue.class)) {
            m_parseType = DoubleCell.TYPE;
        } else if (dataType == StringCell.TYPE) {
            m_parseType = StringCell.TYPE;
        } else {
            // only processing int, double and string conversions
            continue;
        }
        FieldRef fieldRef = df.getFieldRef();
        if (displayName != null) {
            m_includeCols.add(displayName);
        } else {
            m_includeCols.add(m_mapper.getColumnName(fieldRef.getField()));
        }
        consumed.add(i);
    }
    return consumed;
}
Also used : FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) DoubleValue(org.knime.core.data.DoubleValue) ArrayList(java.util.ArrayList) DataType(org.knime.core.data.DataType) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Aggregations

DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)11 FieldRef (org.dmg.pmml.FieldRefDocument.FieldRef)11 BigInteger (java.math.BigInteger)6 NeuralLayer (org.dmg.pmml.NeuralLayerDocument.NeuralLayer)4 HiddenLayer (org.knime.base.data.neural.HiddenLayer)4 InputLayer (org.knime.base.data.neural.InputLayer)4 InputPerceptron (org.knime.base.data.neural.InputPerceptron)4 Layer (org.knime.base.data.neural.Layer)4 MultiLayerPerceptron (org.knime.base.data.neural.MultiLayerPerceptron)4 Perceptron (org.knime.base.data.neural.Perceptron)4 SigmoidPerceptron (org.knime.base.data.neural.SigmoidPerceptron)4 ArrayList (java.util.ArrayList)3 NormDiscrete (org.dmg.pmml.NormDiscreteDocument.NormDiscrete)3 Apply (org.dmg.pmml.ApplyDocument.Apply)2 DATATYPE (org.dmg.pmml.DATATYPE)2 NeuralInput (org.dmg.pmml.NeuralInputDocument.NeuralInput)2 NeuralInputs (org.dmg.pmml.NeuralInputsDocument.NeuralInputs)2 NeuralOutput (org.dmg.pmml.NeuralOutputDocument.NeuralOutput)2 NeuralOutputs (org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs)2 OPTYPE (org.dmg.pmml.OPTYPE)2