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());
}
});
}
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);
}
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");
}
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());
});
}
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());
}
Aggregations