Search in sources :

Example 6 with LocalTransformations

use of org.dmg.pmml.LocalTransformationsDocument.LocalTransformations in project knime-core by knime.

the class PMMLMany2OneTranslator method exportToLocalTrans.

/**
 * {@inheritDoc}
 */
@Override
public LocalTransformations exportToLocalTrans() {
    final LocalTransformations localTrans = LocalTransformations.Factory.newInstance();
    localTrans.setDerivedFieldArray(new DerivedField[] { createDerivedField() });
    return localTrans;
}
Also used : LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations)

Example 7 with LocalTransformations

use of org.dmg.pmml.LocalTransformationsDocument.LocalTransformations in project knime-core by knime.

the class PMMLOne2ManyTranslator method exportToLocalTrans.

/**
 * {@inheritDoc}
 */
@Override
public LocalTransformations exportToLocalTrans() {
    LocalTransformations localTrans = LocalTransformations.Factory.newInstance();
    localTrans.setDerivedFieldArray(createDerivedFields());
    return localTrans;
}
Also used : LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations)

Example 8 with LocalTransformations

use of org.dmg.pmml.LocalTransformationsDocument.LocalTransformations in project knime-core by knime.

the class PMMLNormalizeTranslator method exportToLocalTrans.

/**
 * {@inheritDoc}
 */
@Override
public LocalTransformations exportToLocalTrans() {
    LocalTransformations localtrans = LocalTransformations.Factory.newInstance();
    localtrans.setDerivedFieldArray(createDerivedFields());
    return localtrans;
}
Also used : LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations)

Example 9 with LocalTransformations

use of org.dmg.pmml.LocalTransformationsDocument.LocalTransformations in project knime-core by knime.

the class PMMLGeneralRegressionTranslator method exportTo.

/**
 * {@inheritDoc}
 */
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
    m_nameMapper = new DerivedFieldMapper(pmmlDoc);
    GeneralRegressionModel reg = pmmlDoc.getPMML().addNewGeneralRegressionModel();
    final JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    if (!m_content.getVectorLengths().isEmpty()) {
        LocalTransformations localTransformations = reg.addNewLocalTransformations();
        for (final Entry<? extends String, ? extends Integer> entry : m_content.getVectorLengths().entrySet()) {
            DataColumnSpec columnSpec = spec.getDataTableSpec().getColumnSpec(entry.getKey());
            if (columnSpec != null) {
                final DataType type = columnSpec.getType();
                final DataColumnProperties props = columnSpec.getProperties();
                final boolean bitVector = type.isCompatible(BitVectorValue.class) || (type.isCompatible(StringValue.class) && props.containsProperty("realType") && "BitVector".equals(props.getProperty("realType")));
                final boolean byteVector = type.isCompatible(ByteVectorValue.class) || (type.isCompatible(StringValue.class) && props.containsProperty("realType") && "ByteVector".equals(props.getProperty("realType")));
                final String lengthAsString;
                final int width;
                if (byteVector) {
                    lengthAsString = "3";
                    width = 4;
                } else if (bitVector) {
                    lengthAsString = "1";
                    width = 1;
                } else {
                    throw new UnsupportedOperationException("Not supported type: " + type + " for column: " + columnSpec);
                }
                for (int i = 0; i < entry.getValue().intValue(); ++i) {
                    final DerivedField derivedField = localTransformations.addNewDerivedField();
                    derivedField.setOptype(OPTYPE.CONTINUOUS);
                    derivedField.setDataType(DATATYPE.INTEGER);
                    derivedField.setName(entry.getKey() + "[" + i + "]");
                    Apply apply = derivedField.addNewApply();
                    apply.setFunction("substring");
                    apply.addNewFieldRef().setField(entry.getKey());
                    Constant from = apply.addNewConstant();
                    from.setDataType(DATATYPE.INTEGER);
                    from.setStringValue(bitVector ? Long.toString(entry.getValue().longValue() - i) : Long.toString(i * width + 1L));
                    Constant length = apply.addNewConstant();
                    length.setDataType(DATATYPE.INTEGER);
                    length.setStringValue(lengthAsString);
                }
            }
            jsonBuilder.add(entry.getKey(), entry.getValue().intValue());
        }
    }
    // PMMLPortObjectSpecCreator newSpecCreator = new PMMLPortObjectSpecCreator(spec);
    // newSpecCreator.addPreprocColNames(m_content.getVectorLengths().entrySet().stream()
    // .flatMap(
    // e -> IntStream.iterate(0, o -> o + 1).limit(e.getValue()).mapToObj(i -> e.getKey() + "[" + i + "]"))
    // .collect(Collectors.toList()));
    PMMLMiningSchemaTranslator.writeMiningSchema(spec, reg);
    // if (!m_content.getVectorLengths().isEmpty()) {
    // Extension miningExtension = reg.getMiningSchema().addNewExtension();
    // miningExtension.setExtender(EXTENDER);
    // miningExtension.setName(VECTOR_COLUMNS_WITH_LENGTH);
    // miningExtension.setValue(jsonBuilder.build().toString());
    // }
    reg.setModelType(getPMMLRegModelType(m_content.getModelType()));
    reg.setFunctionName(getPMMLMiningFunction(m_content.getFunctionName()));
    String algorithmName = m_content.getAlgorithmName();
    if (algorithmName != null && !algorithmName.isEmpty()) {
        reg.setAlgorithmName(algorithmName);
    }
    String modelName = m_content.getModelName();
    if (modelName != null && !modelName.isEmpty()) {
        reg.setModelName(modelName);
    }
    String targetReferenceCategory = m_content.getTargetReferenceCategory();
    if (targetReferenceCategory != null && !targetReferenceCategory.isEmpty()) {
        reg.setTargetReferenceCategory(targetReferenceCategory);
    }
    if (m_content.getOffsetValue() != null) {
        reg.setOffsetValue(m_content.getOffsetValue());
    }
    // add parameter list
    ParameterList paramList = reg.addNewParameterList();
    for (PMMLParameter p : m_content.getParameterList()) {
        Parameter param = paramList.addNewParameter();
        param.setName(p.getName());
        String label = p.getLabel();
        if (label != null) {
            param.setLabel(m_nameMapper.getDerivedFieldName(label));
        }
    }
    // add factor list
    FactorList factorList = reg.addNewFactorList();
    for (PMMLPredictor p : m_content.getFactorList()) {
        Predictor predictor = factorList.addNewPredictor();
        predictor.setName(m_nameMapper.getDerivedFieldName(p.getName()));
    }
    // add covariate list
    CovariateList covariateList = reg.addNewCovariateList();
    for (PMMLPredictor p : m_content.getCovariateList()) {
        Predictor predictor = covariateList.addNewPredictor();
        predictor.setName(m_nameMapper.getDerivedFieldName(p.getName()));
    }
    // add PPMatrix
    PPMatrix ppMatrix = reg.addNewPPMatrix();
    for (PMMLPPCell p : m_content.getPPMatrix()) {
        PPCell cell = ppMatrix.addNewPPCell();
        cell.setValue(p.getValue());
        cell.setPredictorName(m_nameMapper.getDerivedFieldName(p.getPredictorName()));
        cell.setParameterName(p.getParameterName());
        String targetCategory = p.getTargetCategory();
        if (targetCategory != null && !targetCategory.isEmpty()) {
            cell.setTargetCategory(targetCategory);
        }
    }
    // add CovMatrix
    if (m_content.getPCovMatrix().length > 0) {
        PCovMatrix pCovMatrix = reg.addNewPCovMatrix();
        for (PMMLPCovCell p : m_content.getPCovMatrix()) {
            PCovCell covCell = pCovMatrix.addNewPCovCell();
            covCell.setPRow(p.getPRow());
            covCell.setPCol(p.getPCol());
            String tCol = p.getTCol();
            String tRow = p.getTRow();
            if (tRow != null || tCol != null) {
                covCell.setTRow(tRow);
                covCell.setTCol(tCol);
            }
            covCell.setValue(p.getValue());
            String targetCategory = p.getTargetCategory();
            if (targetCategory != null && !targetCategory.isEmpty()) {
                covCell.setTargetCategory(targetCategory);
            }
        }
    }
    // add ParamMatrix
    ParamMatrix paramMatrix = reg.addNewParamMatrix();
    for (PMMLPCell p : m_content.getParamMatrix()) {
        PCell pCell = paramMatrix.addNewPCell();
        String targetCategory = p.getTargetCategory();
        if (targetCategory != null) {
            pCell.setTargetCategory(targetCategory);
        }
        pCell.setParameterName(p.getParameterName());
        pCell.setBeta(p.getBeta());
        Integer df = p.getDf();
        if (df != null) {
            pCell.setDf(BigInteger.valueOf(df));
        }
    }
    return GeneralRegressionModel.type;
}
Also used : Predictor(org.dmg.pmml.PredictorDocument.Predictor) Apply(org.dmg.pmml.ApplyDocument.Apply) Constant(org.dmg.pmml.ConstantDocument.Constant) PPCell(org.dmg.pmml.PPCellDocument.PPCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) DataColumnSpec(org.knime.core.data.DataColumnSpec) FactorList(org.dmg.pmml.FactorListDocument.FactorList) PPCell(org.dmg.pmml.PPCellDocument.PPCell) PCell(org.dmg.pmml.PCellDocument.PCell) DataType(org.knime.core.data.DataType) JsonObjectBuilder(javax.json.JsonObjectBuilder) DataColumnProperties(org.knime.core.data.DataColumnProperties) ParamMatrix(org.dmg.pmml.ParamMatrixDocument.ParamMatrix) PPMatrix(org.dmg.pmml.PPMatrixDocument.PPMatrix) CovariateList(org.dmg.pmml.CovariateListDocument.CovariateList) PCovMatrix(org.dmg.pmml.PCovMatrixDocument.PCovMatrix) BigInteger(java.math.BigInteger) LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations) PCovCell(org.dmg.pmml.PCovCellDocument.PCovCell) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) ParameterList(org.dmg.pmml.ParameterListDocument.ParameterList) Parameter(org.dmg.pmml.ParameterDocument.Parameter) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 10 with LocalTransformations

use of org.dmg.pmml.LocalTransformationsDocument.LocalTransformations in project knime-core by knime.

the class PMMLPortObject method moveDerivedFields.

/**
 * Moves the content of the transformation dictionary to local
 * transformations.
 * @param type the type of model to move the derived fields to
 * @return the {@link LocalTransformations} element containing the moved
 *      derived fields or an empty local transformation object if nothing
 *      has to be moved
 */
private LocalTransformations moveDerivedFields(final SchemaType type) {
    PMML pmml = m_pmmlDoc.getPMML();
    TransformationDictionary transDict = pmml.getTransformationDictionary();
    LocalTransformations localTrans = LocalTransformations.Factory.newInstance();
    if (transDict == null) {
        // nothing to be moved
        return localTrans;
    }
    localTrans.setDerivedFieldArray(transDict.getDerivedFieldArray());
    localTrans.setExtensionArray(transDict.getExtensionArray());
    /*
         * Unfortunately the PMML models have no common base class. Therefore a
         * cast to the specific type is necessary for being able to add the
         * mining schema.
         */
    boolean known = true;
    if (AssociationModel.type.equals(type)) {
        AssociationModel model = pmml.getAssociationModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (ClusteringModel.type.equals(type)) {
        ClusteringModel model = pmml.getClusteringModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (GeneralRegressionModel.type.equals(type)) {
        GeneralRegressionModel model = pmml.getGeneralRegressionModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (MiningModel.type.equals(type)) {
        MiningModel model = pmml.getMiningModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (NaiveBayesModel.type.equals(type)) {
        NaiveBayesModel model = pmml.getNaiveBayesModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (NeuralNetwork.type.equals(type)) {
        NeuralNetwork model = pmml.getNeuralNetworkArray(0);
        model.setLocalTransformations(localTrans);
    } else if (RegressionModel.type.equals(type)) {
        RegressionModel model = pmml.getRegressionModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (RuleSetModel.type.equals(type)) {
        RuleSetModel model = pmml.getRuleSetModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (SequenceModel.type.equals(type)) {
        SequenceModel model = pmml.getSequenceModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (SupportVectorMachineModel.type.equals(type)) {
        SupportVectorMachineModel model = pmml.getSupportVectorMachineModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (TextModel.type.equals(type)) {
        TextModel model = pmml.getTextModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (TimeSeriesModel.type.equals(type)) {
        TimeSeriesModel model = pmml.getTimeSeriesModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (TreeModel.type.equals(type)) {
        TreeModel model = pmml.getTreeModelArray(0);
        model.setLocalTransformations(localTrans);
    } else {
        if (type != null) {
            LOGGER.error("Could not move TransformationDictionary to " + "unsupported model of type \"" + type + "\".");
        }
        known = false;
    }
    if (known) {
        // remove derived fields from TransformationDictionary
        transDict.setDerivedFieldArray(new DerivedField[0]);
        transDict.setExtensionArray(new ExtensionDocument.Extension[0]);
    }
    return localTrans;
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) SequenceModel(org.dmg.pmml.SequenceModelDocument.SequenceModel) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) TextModel(org.dmg.pmml.TextModelDocument.TextModel) ExtensionDocument(org.dmg.pmml.ExtensionDocument) NaiveBayesModel(org.dmg.pmml.NaiveBayesModelDocument.NaiveBayesModel) TimeSeriesModel(org.dmg.pmml.TimeSeriesModelDocument.TimeSeriesModel) NeuralNetwork(org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) RegressionModel(org.dmg.pmml.RegressionModelDocument.RegressionModel) TreeModel(org.dmg.pmml.TreeModelDocument.TreeModel) LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations) MiningModel(org.dmg.pmml.MiningModelDocument.MiningModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) PMML(org.dmg.pmml.PMMLDocument.PMML) SupportVectorMachineModel(org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel) AssociationModel(org.dmg.pmml.AssociationModelDocument.AssociationModel) ClusteringModel(org.dmg.pmml.ClusteringModelDocument.ClusteringModel)

Aggregations

LocalTransformations (org.dmg.pmml.LocalTransformationsDocument.LocalTransformations)15 DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)4 GeneralRegressionModel (org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel)3 TransformationDictionary (org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary)3 ArrayList (java.util.ArrayList)2 ClusteringModel (org.dmg.pmml.ClusteringModelDocument.ClusteringModel)2 NeuralNetwork (org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork)2 PMML (org.dmg.pmml.PMMLDocument.PMML)2 RegressionModel (org.dmg.pmml.RegressionModelDocument.RegressionModel)2 RuleSetModel (org.dmg.pmml.RuleSetModelDocument.RuleSetModel)2 SupportVectorMachineModel (org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel)2 TreeModel (org.dmg.pmml.TreeModelDocument.TreeModel)2 BigInteger (java.math.BigInteger)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 SchemaType (org.apache.xmlbeans.SchemaType)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlObject (org.apache.xmlbeans.XmlObject)1 Apply (org.dmg.pmml.ApplyDocument.Apply)1