use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class FormulaUtil method encodeIfElseExpression.
private static Expression encodeIfElseExpression(FunctionExpression functionExpression, VariableMap expressionFields, RExpEncoder encoder) {
FunctionExpression.Argument testArgument = functionExpression.getArgument("test", 0);
expressionFields.putAll(testArgument);
FunctionExpression.Argument yesArgument = functionExpression.getArgument("yes", 1);
FunctionExpression.Argument noArgument = functionExpression.getArgument("no", 2);
expressionFields.putAll(yesArgument);
expressionFields.putAll(noArgument);
// XXX: "Missing values in test give missing values in the result"
Apply apply = PMMLUtil.createApply("if").addExpressions(prepareExpression(testArgument, expressionFields, encoder)).addExpressions(prepareExpression(yesArgument, expressionFields, encoder), prepareExpression(noArgument, expressionFields, encoder));
return apply;
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class EarthConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector earth = getObject();
RDoubleVector dirs = (RDoubleVector) earth.getValue("dirs");
RDoubleVector cuts = (RDoubleVector) earth.getValue("cuts");
RDoubleVector selectedTerms = (RDoubleVector) earth.getValue("selected.terms");
RDoubleVector coefficients = (RDoubleVector) earth.getValue("coefficients");
RExp terms = earth.getValue("terms");
final RGenericVector xlevels;
try {
xlevels = (RGenericVector) earth.getValue("xlevels");
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("No variable levels information. Please initialize the \'xlevels\' element", iae);
}
RStringVector dirsRows = dirs.dimnames(0);
RStringVector dirsColumns = dirs.dimnames(1);
RStringVector cutsRows = cuts.dimnames(0);
RStringVector cutsColumns = cuts.dimnames(1);
if (!(dirsRows.getValues()).equals(cutsRows.getValues()) || !(dirsColumns.getValues()).equals(cutsColumns.getValues())) {
throw new IllegalArgumentException();
}
int rows = dirsRows.size();
int columns = dirsColumns.size();
List<String> predictorNames = dirsColumns.getValues();
FormulaContext context = new FormulaContext() {
@Override
public List<String> getCategories(String variable) {
if (xlevels.hasValue(variable)) {
RStringVector levels = (RStringVector) xlevels.getValue(variable);
return levels.getValues();
}
return null;
}
@Override
public RGenericVector getData() {
return null;
}
};
Formula formula = FormulaUtil.createFormula(terms, context, encoder);
// Dependent variable
{
RStringVector yNames = coefficients.dimnames(1);
FieldName name = FieldName.create(yNames.asScalar());
DataField dataField = (DataField) encoder.getField(name);
encoder.setLabel(dataField);
}
// Independent variables
for (int i = 1; i < selectedTerms.size(); i++) {
int termIndex = ValueUtil.asInt(selectedTerms.getValue(i)) - 1;
List<Double> dirsRow = FortranMatrixUtil.getRow(dirs.getValues(), rows, columns, termIndex);
List<Double> cutsRow = FortranMatrixUtil.getRow(cuts.getValues(), rows, columns, termIndex);
List<Feature> features = new ArrayList<>();
predictors: for (int j = 0; j < predictorNames.size(); j++) {
String predictorName = predictorNames.get(j);
int dir = ValueUtil.asInt(dirsRow.get(j));
double cut = cutsRow.get(j);
if (dir == 0) {
continue predictors;
}
Feature feature = formula.resolveFeature(predictorName);
switch(dir) {
case -1:
case 1:
{
feature = feature.toContinuousFeature();
FieldName name = FieldName.create(formatHingeFunction(dir, feature, cut));
DerivedField derivedField = encoder.getDerivedField(name);
if (derivedField == null) {
Apply apply = createHingeFunction(dir, feature, cut);
derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);
}
feature = new ContinuousFeature(encoder, derivedField);
}
break;
case 2:
break;
default:
throw new IllegalArgumentException();
}
features.add(feature);
}
Feature feature;
if (features.size() == 1) {
feature = features.get(0);
} else if (features.size() > 1) {
feature = new InteractionFeature(encoder, FieldName.create(dirsRows.getValue(i)), DataType.DOUBLE, features);
} else {
throw new IllegalArgumentException();
}
encoder.addFeature(feature);
}
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class ExpressionCompactor method negateExpression.
private static void negateExpression(Apply apply) {
List<Expression> expressions = apply.getExpressions();
if (expressions.size() != 1) {
throw new IllegalArgumentException();
}
ListIterator<Expression> expressionIt = expressions.listIterator();
Expression expression = expressionIt.next();
if (expression instanceof Apply) {
Apply nestedApply = (Apply) expression;
String negatedFunction = negate(nestedApply.getFunction());
if (negatedFunction != null) {
apply.setFunction(negatedFunction);
expressionIt.remove();
List<Expression> nestedExpressions = nestedApply.getExpressions();
for (Expression nestedExpression : nestedExpressions) {
expressionIt.add(nestedExpression);
}
}
}
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class ExpressionCompactor method inlineLogicalExpressions.
private static void inlineLogicalExpressions(Apply apply) {
String function = apply.getFunction();
List<Expression> expressions = apply.getExpressions();
for (ListIterator<Expression> expressionIt = expressions.listIterator(); expressionIt.hasNext(); ) {
Expression expression = expressionIt.next();
if (expression instanceof Apply) {
Apply nestedApply = (Apply) expression;
if ((function).equals(nestedApply.getFunction())) {
expressionIt.remove();
// Deep inline
inlineLogicalExpressions(nestedApply);
List<Expression> nestedExpressions = nestedApply.getExpressions();
for (Expression nestedExpression : nestedExpressions) {
expressionIt.add(nestedExpression);
}
}
}
}
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class Formula method addField.
public void addField(Field<?> field) {
RExpEncoder encoder = getEncoder();
Feature feature = new ContinuousFeature(encoder, field);
if (field instanceof DerivedField) {
DerivedField derivedField = (DerivedField) field;
Expression expression = derivedField.getExpression();
if (expression instanceof Apply) {
Apply apply = (Apply) expression;
if (checkApply(apply, "pow", FieldRef.class, Constant.class)) {
List<Expression> expressions = apply.getExpressions();
FieldRef fieldRef = (FieldRef) expressions.get(0);
Constant constant = (Constant) expressions.get(1);
try {
int power = Integer.parseInt(constant.getValue());
feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
} catch (NumberFormatException nfe) {
// Ignored
}
}
}
}
putFeature(field.getName(), feature);
this.fields.add(field);
}
Aggregations