use of org.dmg.pmml.DiscretizeBin in project drools by kiegroup.
the class KiePMMLDiscretizeBinFactoryTest method getDiscretizeBinVariableDeclaration.
@Test
public void getDiscretizeBinVariableDeclaration() throws IOException {
String variableName = "variableName";
double leftMargin = 45.32;
Interval interval = new Interval();
interval.setLeftMargin(leftMargin);
interval.setRightMargin(null);
interval.setClosure(Interval.Closure.CLOSED_OPEN);
String binValue = "binValue";
DiscretizeBin discretizeBin = new DiscretizeBin();
discretizeBin.setBinValue(binValue);
discretizeBin.setInterval(interval);
BlockStmt retrieved = KiePMMLDiscretizeBinFactory.getDiscretizeBinVariableDeclaration(variableName, discretizeBin);
String closureString = CLOSURE.class.getName() + "." + CLOSURE.byName(interval.getClosure().value()).name();
String text = getFileContent(TEST_01_SOURCE);
Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, leftMargin, closureString, binValue));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(Collections.class, KiePMMLDiscretizeBin.class, KiePMMLInterval.class);
commonValidateCompilationWithImports(retrieved, imports);
}
use of org.dmg.pmml.DiscretizeBin in project drools by kiegroup.
the class KiePMMLDiscretizeFactory method getDiscretizeVariableDeclaration.
static BlockStmt getDiscretizeVariableDeclaration(final String variableName, final Discretize discretize) {
final MethodDeclaration methodDeclaration = DISCRETIZE_TEMPLATE.getMethodsByName(GETKIEPMMLDISCRETIZE).get(0).clone();
final BlockStmt discretizeBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(discretizeBody, DISCRETIZE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DISCRETIZE, discretizeBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (DiscretizeBin discretizeBin : discretize.getDiscretizeBins()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getDiscretizeBinVariableDeclaration(nestedVariableName, discretizeBin);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DISCRETIZE, toReturn))).asObjectCreationExpr();
final Expression nameExpr = new StringLiteralExpr(discretize.getField().getValue());
final Expression mapMissingToExpr = getExpressionForObject(discretize.getMapMissingTo());
final Expression defaultValueExpr = getExpressionForObject(discretize.getDefaultValue());
final Expression dataTypeExpression = getExpressionForDataType(discretize.getDataType());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
objectCreationExpr.getArguments().set(3, mapMissingToExpr);
objectCreationExpr.getArguments().set(4, defaultValueExpr);
objectCreationExpr.getArguments().set(5, dataTypeExpression);
discretizeBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.dmg.pmml.DiscretizeBin in project drools by kiegroup.
the class PMMLModelTestUtils method getRandomDiscretizeBin.
public static DiscretizeBin getRandomDiscretizeBin() {
Interval interval = getRandomInterval();
DiscretizeBin toReturn = new DiscretizeBin();
toReturn.setInterval(interval);
toReturn.setBinValue(RandomStringUtils.random(6, true, false));
return toReturn;
}
use of org.dmg.pmml.DiscretizeBin in project shifu by ShifuML.
the class WoeLocalTransformCreator method createNumericalDerivedField.
/**
* Create @DerivedField for numerical variable
*
* @param config
* - ColumnConfig for numerical variable
* @param cutoff
* - cutoff of normalization
* @param normType
* - the normalization method that is used to generate DerivedField
* @return DerivedField for variable
*/
@Override
protected List<DerivedField> createNumericalDerivedField(ColumnConfig config, double cutoff, ModelNormalizeConf.NormType normType) {
List<Double> binWoeList = (normType.equals(ModelNormalizeConf.NormType.WOE) ? config.getBinCountWoe() : config.getBinWeightedWoe());
List<Double> binBoundaryList = config.getBinBoundary();
List<DiscretizeBin> discretizeBinList = new ArrayList<DiscretizeBin>();
for (int i = 0; i < binBoundaryList.size(); i++) {
DiscretizeBin discretizeBin = new DiscretizeBin();
Interval interval = new Interval();
if (i == 0) {
if (binBoundaryList.size() == 1) {
interval.setClosure(Interval.Closure.OPEN_OPEN).setLeftMargin(Double.NEGATIVE_INFINITY).setRightMargin(Double.POSITIVE_INFINITY);
} else {
interval.setClosure(Interval.Closure.OPEN_OPEN).setRightMargin(binBoundaryList.get(i + 1));
}
} else if (i == binBoundaryList.size() - 1) {
interval.setClosure(Interval.Closure.CLOSED_OPEN).setLeftMargin(binBoundaryList.get(i));
} else {
interval.setClosure(Interval.Closure.CLOSED_OPEN).setLeftMargin(binBoundaryList.get(i)).setRightMargin(binBoundaryList.get(i + 1));
}
discretizeBin.setInterval(interval).setBinValue(Double.toString(binWoeList.get(i)));
discretizeBinList.add(discretizeBin);
}
Discretize discretize = new Discretize();
discretize.setDataType(DataType.DOUBLE).setField(FieldName.create(NormalUtils.getSimpleColumnName(config, columnConfigList, segmentExpansions, datasetHeaders))).setMapMissingTo(Normalizer.normalize(config, null, cutoff, normType).get(0).toString()).setDefaultValue(Normalizer.normalize(config, null, cutoff, normType).get(0).toString()).addDiscretizeBins(discretizeBinList.toArray(new DiscretizeBin[discretizeBinList.size()]));
// derived field name is consisted of FieldName and "_zscl"
List<DerivedField> derivedFields = new ArrayList<DerivedField>();
derivedFields.add(new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE).setName(FieldName.create(genPmmlColumnName(NormalUtils.getSimpleColumnName(config.getColumnName()), normType))).setExpression(discretize));
return derivedFields;
}
use of org.dmg.pmml.DiscretizeBin in project jpmml-r by jpmml.
the class FormulaUtil method createDiscretize.
private static Discretize createDiscretize(String name, List<String> categories) {
Discretize discretize = new Discretize(name);
for (String category : categories) {
Interval interval = ExpressionTranslator.translateInterval(category);
DiscretizeBin discretizeBin = new DiscretizeBin(category, interval);
discretize.addDiscretizeBins(discretizeBin);
}
return discretize;
}
Aggregations