Search in sources :

Example 6 with Target

use of org.dmg.pmml.Target in project drools by kiegroup.

the class KiePMMLUtil method populateMissingOutputFieldDataType.

/**
 * Method to populate the <b>dataType</b> property of <code>OutputField</code>s.
 * Such property was optional until 4.4.1 spec
 * @param toPopulate
 * @param miningFields
 * @param dataFields
 */
static void populateMissingOutputFieldDataType(List<OutputField> toPopulate, List<MiningField> miningFields, List<DataField> dataFields) {
    // partial implementation to fix missing "dataType" inside OutputField; "dataType" became mandatory only in 4.4.1 version
    List<MiningField> targetFields = getMiningTargetFields(miningFields);
    toPopulate.stream().filter(outputField -> outputField.getDataType() == null).forEach(outputField -> {
        MiningField referencedField = null;
        if (outputField.getTargetField() != null) {
            referencedField = targetFields.stream().filter(targetField -> outputField.getTargetField().equals(targetField.getName())).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a target field for OutputField " + outputField.getName().getValue()));
        }
        if (referencedField == null && (outputField.getResultFeature() == null || outputField.getResultFeature().equals(ResultFeature.PREDICTED_VALUE))) {
            // default predictedValue
            referencedField = targetFields.stream().findFirst().orElse(// It is allowed to not have any "target" field inside MiningSchema
            null);
        }
        if (referencedField == null && ResultFeature.PROBABILITY.equals(outputField.getResultFeature())) {
            // we set the "dataType" to "double" because outputField is a "probability", we may return
            outputField.setDataType(DataType.DOUBLE);
            return;
        }
        if (referencedField != null) {
            FieldName targetFieldName = referencedField.getName();
            DataField dataField = dataFields.stream().filter(df -> df.getName().equals(targetFieldName)).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a DataField field for " + "MiningField " + targetFieldName.toString()));
            outputField.setDataType(dataField.getDataType());
        }
    });
}
Also used : PMML(org.dmg.pmml.PMML) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) Targets(org.dmg.pmml.Targets) DataType(org.dmg.pmml.DataType) ResultFeature(org.dmg.pmml.ResultFeature) MiningSchema(org.dmg.pmml.MiningSchema) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Target(org.dmg.pmml.Target) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) OpType(org.dmg.pmml.OpType) List(java.util.List) Segment(org.dmg.pmml.mining.Segment) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MiningFunction(org.dmg.pmml.MiningFunction) MiningField(org.dmg.pmml.MiningField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MathContext(org.dmg.pmml.MathContext) InputStream(java.io.InputStream) MiningModel(org.dmg.pmml.mining.MiningModel) MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) FieldName(org.dmg.pmml.FieldName)

Example 7 with Target

use of org.dmg.pmml.Target in project drools by kiegroup.

the class KiePMMLTargetInstanceFactoryTest method getKiePMMLTarget.

@Test
public void getKiePMMLTarget() {
    final Target toConvert = getRandomTarget();
    KiePMMLTarget retrieved = KiePMMLTargetInstanceFactory.getKiePMMLTarget(toConvert);
    commonVerifyKiePMMLTarget(retrieved, toConvert);
}
Also used : KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) InstanceFactoriesTestCommon.commonVerifyKiePMMLTarget(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLTarget) Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) InstanceFactoriesTestCommon.commonVerifyKiePMMLTarget(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLTarget) Test(org.junit.Test)

Example 8 with Target

use of org.dmg.pmml.Target in project drools by kiegroup.

the class ModelUtilsTest method getOpTypeByTargetsNotFound.

@Test(expected = KiePMMLInternalException.class)
public void getOpTypeByTargetsNotFound() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    final Targets targets = new Targets();
    IntStream.range(0, 3).forEach(i -> {
        String fieldName = "field" + i;
        final DataField dataField = getRandomDataField();
        dataField.setName(FieldName.create(fieldName));
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getRandomMiningField();
        miningField.setName(dataField.getName());
        miningSchema.addMiningFields(miningField);
        final Target targetField = getRandomTarget();
        targetField.setField(dataField.getName());
        targets.addTargets(targetField);
    });
    model.setMiningSchema(miningSchema);
    model.setTargets(targets);
    ModelUtils.getOpType(getFieldsFromDataDictionary(dataDictionary), model, "NOT_EXISTING");
}
Also used : PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) MiningSchema(org.dmg.pmml.MiningSchema) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) Targets(org.dmg.pmml.Targets) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Example 9 with Target

use of org.dmg.pmml.Target in project drools by kiegroup.

the class ModelUtilsTest method getOpTypeFromTargets.

@Test
public void getOpTypeFromTargets() {
    Optional<OP_TYPE> opType = ModelUtils.getOpTypeFromTargets(null, "vsd");
    assertNotNull(opType);
    assertFalse(opType.isPresent());
    final Targets targets = new Targets();
    opType = ModelUtils.getOpTypeFromTargets(targets, "vsd");
    assertNotNull(opType);
    assertFalse(opType.isPresent());
    IntStream.range(0, 3).forEach(i -> {
        final Target target = getRandomTarget();
        targets.addTargets(target);
    });
    targets.getTargets().forEach(target -> {
        Optional<OP_TYPE> retrieved = ModelUtils.getOpTypeFromTargets(targets, target.getField().getValue());
        assertNotNull(retrieved);
        assertTrue(retrieved.isPresent());
        OP_TYPE expected = OP_TYPE.byName(target.getOpType().value());
        assertEquals(expected, retrieved.get());
    });
}
Also used : Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) Targets(org.dmg.pmml.Targets) Test(org.junit.Test)

Example 10 with Target

use of org.dmg.pmml.Target in project drools by kiegroup.

the class ModelUtilsTest method getTargetFieldsWithoutTargetFieldsWithTargets.

@Test
public void getTargetFieldsWithoutTargetFieldsWithTargets() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    final Targets targets = new Targets();
    IntStream.range(0, 3).forEach(i -> {
        final DataField dataField = getRandomDataField();
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getMiningField(dataField.getName().getValue(), MiningField.UsageType.ACTIVE);
        miningSchema.addMiningFields(miningField);
        final Target targetField = getTarget(dataField.getName().getValue(), null);
        targets.addTargets(targetField);
    });
    model.setMiningSchema(miningSchema);
    model.setTargets(targets);
    Map<String, DATA_TYPE> retrieved = ModelUtils.getTargetFieldsTypeMap(getFieldsFromDataDictionary(dataDictionary), model);
    assertNotNull(retrieved);
    assertTrue(retrieved.isEmpty());
}
Also used : PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) MiningSchema(org.dmg.pmml.MiningSchema) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) Targets(org.dmg.pmml.Targets) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Aggregations

Target (org.dmg.pmml.Target)15 Targets (org.dmg.pmml.Targets)11 Test (org.junit.Test)8 FieldName (org.dmg.pmml.FieldName)7 MiningField (org.dmg.pmml.MiningField)7 ArrayList (java.util.ArrayList)6 DataField (org.dmg.pmml.DataField)6 MiningSchema (org.dmg.pmml.MiningSchema)6 Model (org.dmg.pmml.Model)6 PMMLModelTestUtils.getRandomTarget (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget)6 DataDictionary (org.dmg.pmml.DataDictionary)5 RegressionModel (org.dmg.pmml.regression.RegressionModel)5 PMMLModelTestUtils.getTarget (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget)5 TargetValue (org.dmg.pmml.TargetValue)4 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)4 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)4 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)4 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)4 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)4 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)4