Search in sources :

Example 16 with MiningField

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

the class KiePMMLUtilTest method populateMissingPredictedOutputFieldTarget.

@Test
public void populateMissingPredictedOutputFieldTarget() throws Exception {
    final InputStream inputStream = getFileInputStream(NO_OUTPUT_FIELD_TARGET_NAME_SAMPLE);
    final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
    final Model toPopulate = pmml.getModels().get(0);
    final OutputField outputField = toPopulate.getOutput().getOutputFields().get(0);
    assertEquals(ResultFeature.PREDICTED_VALUE, outputField.getResultFeature());
    assertNull(outputField.getTargetField());
    KiePMMLUtil.populateMissingPredictedOutputFieldTarget(toPopulate);
    final MiningField targetField = getMiningTargetFields(toPopulate.getMiningSchema().getMiningFields()).get(0);
    assertNotNull(outputField.getTargetField());
    assertEquals(targetField.getName(), outputField.getTargetField());
}
Also used : MiningField(org.dmg.pmml.MiningField) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) InputStream(java.io.InputStream) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) OutputField(org.dmg.pmml.OutputField) Test(org.junit.Test)

Example 17 with MiningField

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

the class KiePMMLUtilTest method populateMissingTargetFieldInSegment.

@Test
public void populateMissingTargetFieldInSegment() throws Exception {
    final InputStream inputStream = getFileInputStream(NO_MODELNAME_NO_SEGMENT_ID_NOSEGMENT_TARGET_FIELD_SAMPLE);
    final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
    final Model retrieved = pmml.getModels().get(0);
    assertTrue(retrieved instanceof MiningModel);
    MiningModel miningModel = (MiningModel) retrieved;
    Model toPopulate = miningModel.getSegmentation().getSegments().get(0).getModel();
    assertTrue(getMiningTargetFields(toPopulate.getMiningSchema()).isEmpty());
    KiePMMLUtil.populateMissingTargetFieldInSegment(retrieved.getMiningSchema(), toPopulate);
    List<MiningField> childrenTargetFields = getMiningTargetFields(toPopulate.getMiningSchema());
    assertFalse(childrenTargetFields.isEmpty());
    getMiningTargetFields(miningModel.getMiningSchema()).forEach(parentTargetField -> assertTrue(childrenTargetFields.contains(parentTargetField)));
}
Also used : MiningField(org.dmg.pmml.MiningField) MiningModel(org.dmg.pmml.mining.MiningModel) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) InputStream(java.io.InputStream) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) Test(org.junit.Test)

Example 18 with MiningField

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

the class KiePMMLUtilTest method correctTargetFields.

@Test
public void correctTargetFields() {
    final MiningField miningField = new MiningField(FieldName.create("FIELD_NAME"));
    final Targets targets = new Targets();
    final Target namedTarget = new Target();
    String targetName = "TARGET_NAME";
    namedTarget.setField(FieldName.create(targetName));
    final Target unnamedTarget = new Target();
    targets.addTargets(namedTarget, unnamedTarget);
    KiePMMLUtil.correctTargetFields(miningField, targets);
    assertEquals(targetName, namedTarget.getField().getValue());
    assertEquals(miningField.getName(), unnamedTarget.getField());
}
Also used : MiningField(org.dmg.pmml.MiningField) Target(org.dmg.pmml.Target) Targets(org.dmg.pmml.Targets) Test(org.junit.Test)

Example 19 with MiningField

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

the class KiePMMLUtilTest method getTargetMiningField.

@Test
public void getTargetMiningField() {
    final DataField dataField = new DataField();
    dataField.setName(FieldName.create("FIELD_NAME"));
    final MiningField retrieved = KiePMMLUtil.getTargetMiningField(dataField);
    assertEquals(dataField.getName().getValue(), retrieved.getName().getValue());
    assertEquals(MiningField.UsageType.TARGET, retrieved.getUsageType());
}
Also used : MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) Test(org.junit.Test)

Example 20 with MiningField

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

the class KiePMMLClassificationTableFactoryTest method getClassificationTableBuilders.

@Test
public void getClassificationTableBuilders() {
    RegressionTable regressionTableProf = getRegressionTable(3.5, "professional");
    RegressionTable regressionTableCler = getRegressionTable(27.4, "clerical");
    OutputField outputFieldCat = getOutputField("CAT-1", ResultFeature.PROBABILITY, "CatPred-1");
    OutputField outputFieldNum = getOutputField("NUM-1", ResultFeature.PROBABILITY, "NumPred-0");
    OutputField outputFieldPrev = getOutputField("PREV", ResultFeature.PREDICTED_VALUE, null);
    String targetField = "targetField";
    DataField dataField = new DataField();
    dataField.setName(FieldName.create(targetField));
    dataField.setOpType(OpType.CATEGORICAL);
    DataDictionary dataDictionary = new DataDictionary();
    dataDictionary.addDataFields(dataField);
    RegressionModel regressionModel = new RegressionModel();
    regressionModel.setNormalizationMethod(RegressionModel.NormalizationMethod.CAUCHIT);
    regressionModel.addRegressionTables(regressionTableProf, regressionTableCler);
    regressionModel.setModelName(getGeneratedClassName("RegressionModel"));
    Output output = new Output();
    output.addOutputFields(outputFieldCat, outputFieldNum, outputFieldPrev);
    regressionModel.setOutput(output);
    MiningField miningField = new MiningField();
    miningField.setUsageType(MiningField.UsageType.TARGET);
    miningField.setName(dataField.getName());
    MiningSchema miningSchema = new MiningSchema();
    miningSchema.addMiningFields(miningField);
    regressionModel.setMiningSchema(miningSchema);
    PMML pmml = new PMML();
    pmml.setDataDictionary(dataDictionary);
    pmml.addModels(regressionModel);
    final CommonCompilationDTO<RegressionModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, regressionModel, new HasClassLoaderMock());
    final RegressionCompilationDTO compilationDTO = RegressionCompilationDTO.fromCompilationDTORegressionTablesAndNormalizationMethod(source, regressionModel.getRegressionTables(), regressionModel.getNormalizationMethod());
    Map<String, KiePMMLTableSourceCategory> retrieved = KiePMMLClassificationTableFactory.getClassificationTableBuilders(compilationDTO);
    assertNotNull(retrieved);
    assertEquals(3, retrieved.size());
    retrieved.values().forEach(kiePMMLTableSourceCategory -> commonValidateKiePMMLRegressionTable(kiePMMLTableSourceCategory.getSource()));
    Map<String, String> sources = retrieved.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, stringKiePMMLTableSourceCategoryEntry -> stringKiePMMLTableSourceCategoryEntry.getValue().getSource()));
    commonValidateCompilation(sources);
}
Also used : GETKIEPMML_TABLE(org.kie.pmml.models.regression.compiler.factories.KiePMMLClassificationTableFactory.GETKIEPMML_TABLE) BeforeClass(org.junit.BeforeClass) OutputField(org.dmg.pmml.OutputField) JavaParserUtils.getFromFileName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromFileName) KIE_PMML_CLASSIFICATION_TABLE_TEMPLATE(org.kie.pmml.models.regression.compiler.factories.KiePMMLClassificationTableFactory.KIE_PMML_CLASSIFICATION_TABLE_TEMPLATE) ResultFeature(org.dmg.pmml.ResultFeature) MiningSchema(org.dmg.pmml.MiningSchema) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) Output(org.dmg.pmml.Output) LinkedHashMap(java.util.LinkedHashMap) FieldName(org.dmg.pmml.FieldName) OpType(org.dmg.pmml.OpType) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) Assert.fail(org.junit.Assert.fail) CompilationUnit(com.github.javaparser.ast.CompilationUnit) MiningField(org.dmg.pmml.MiningField) RegressionCompilationDTO(org.kie.pmml.models.regression.compiler.dto.RegressionCompilationDTO) SUPPORTED_NORMALIZATION_METHODS(org.kie.pmml.models.regression.compiler.factories.KiePMMLClassificationTableFactory.SUPPORTED_NORMALIZATION_METHODS) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) PMML(org.dmg.pmml.PMML) RegressionModel(org.dmg.pmml.regression.RegressionModel) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) Assert.assertTrue(org.junit.Assert.assertTrue) KIE_PMML_CLASSIFICATION_TABLE_TEMPLATE_JAVA(org.kie.pmml.models.regression.compiler.factories.KiePMMLClassificationTableFactory.KIE_PMML_CLASSIFICATION_TABLE_TEMPLATE_JAVA) IOException(java.io.IOException) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) CodegenTestUtils.commonValidateCompilation(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilation) KiePMMLClassificationTable(org.kie.pmml.models.regression.model.KiePMMLClassificationTable) RegressionTable(org.dmg.pmml.regression.RegressionTable) Collectors(java.util.stream.Collectors) KiePMMLModelUtils.getGeneratedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getGeneratedClassName) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) DataField(org.dmg.pmml.DataField) KiePMMLTableSourceCategory(org.kie.pmml.models.regression.model.tuples.KiePMMLTableSourceCategory) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) UNSUPPORTED_NORMALIZATION_METHODS(org.kie.pmml.models.regression.compiler.factories.KiePMMLClassificationTableFactory.UNSUPPORTED_NORMALIZATION_METHODS) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) MiningField(org.dmg.pmml.MiningField) DataDictionary(org.dmg.pmml.DataDictionary) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) RegressionTable(org.dmg.pmml.regression.RegressionTable) RegressionModel(org.dmg.pmml.regression.RegressionModel) DataField(org.dmg.pmml.DataField) MiningSchema(org.dmg.pmml.MiningSchema) Output(org.dmg.pmml.Output) KiePMMLTableSourceCategory(org.kie.pmml.models.regression.model.tuples.KiePMMLTableSourceCategory) OutputField(org.dmg.pmml.OutputField) PMML(org.dmg.pmml.PMML) RegressionCompilationDTO(org.kie.pmml.models.regression.compiler.dto.RegressionCompilationDTO) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

MiningField (org.dmg.pmml.MiningField)59 DataField (org.dmg.pmml.DataField)40 Test (org.junit.Test)39 MiningSchema (org.dmg.pmml.MiningSchema)33 DataDictionary (org.dmg.pmml.DataDictionary)25 RegressionModel (org.dmg.pmml.regression.RegressionModel)24 Model (org.dmg.pmml.Model)22 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)20 PMML (org.dmg.pmml.PMML)18 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)18 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)17 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)16 OutputField (org.dmg.pmml.OutputField)15 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)15 FieldName (org.dmg.pmml.FieldName)12 Target (org.dmg.pmml.Target)11 Targets (org.dmg.pmml.Targets)11 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)11 HashMap (java.util.HashMap)10 List (java.util.List)10