Search in sources :

Example 6 with Expression

use of org.dmg.pmml.Expression in project jpmml-r by jpmml.

the class FormulaUtil method createFormula.

public static Formula createFormula(RExp terms, FormulaContext context, RExpEncoder encoder) {
    Formula formula = new Formula(encoder);
    RIntegerVector factors = (RIntegerVector) terms.getAttributeValue("factors");
    RStringVector dataClasses = (RStringVector) terms.getAttributeValue("dataClasses");
    RStringVector variableRows = factors.dimnames(0);
    RStringVector termColumns = factors.dimnames(1);
    VariableMap expressionFields = new VariableMap();
    for (int i = 0; i < variableRows.size(); i++) {
        String variable = variableRows.getDequotedValue(i);
        FieldName name = FieldName.create(variable);
        OpType opType = OpType.CONTINUOUS;
        DataType dataType = RExpUtil.getDataType(dataClasses.getValue(variable));
        List<String> categories = context.getCategories(variable);
        if (categories != null && categories.size() > 0) {
            opType = OpType.CATEGORICAL;
        }
        Expression expression = null;
        FieldName shortName = name;
        expression: if (variable.indexOf('(') > -1 && variable.indexOf(')') > -1) {
            FunctionExpression functionExpression;
            try {
                functionExpression = (FunctionExpression) ExpressionTranslator.translateExpression(variable);
            } catch (Exception e) {
                break expression;
            }
            if (functionExpression.hasId("base", "cut")) {
                expression = encodeCutExpression(functionExpression, categories, expressionFields, encoder);
            } else if (functionExpression.hasId("base", "I")) {
                expression = encodeIdentityExpression(functionExpression, expressionFields, encoder);
            } else if (functionExpression.hasId("base", "ifelse")) {
                expression = encodeIfElseExpression(functionExpression, expressionFields, encoder);
            } else if (functionExpression.hasId("plyr", "mapvalues")) {
                expression = encodeMapValuesExpression(functionExpression, categories, expressionFields, encoder);
            } else if (functionExpression.hasId("plyr", "revalue")) {
                expression = encodeReValueExpression(functionExpression, categories, expressionFields, encoder);
            } else {
                break expression;
            }
            FunctionExpression.Argument xArgument = functionExpression.getArgument("x", 0);
            String value = (xArgument.formatExpression()).trim();
            shortName = FieldName.create(functionExpression.hasId("base", "I") ? value : (functionExpression.getFunction() + "(" + value + ")"));
        }
        if (expression != null) {
            DerivedField derivedField = encoder.createDerivedField(name, opType, dataType, expression).addExtensions(createExtension(variable));
            if (categories != null && categories.size() > 0) {
                formula.addField(derivedField, categories);
            } else {
                formula.addField(derivedField);
            }
            if (!(name).equals(shortName)) {
                encoder.renameField(name, shortName);
            }
        } else {
            if ((DataType.BOOLEAN).equals(dataType)) {
                categories = Arrays.asList("false", "true");
            }
            if (categories != null && categories.size() > 0) {
                DataField dataField = encoder.createDataField(name, OpType.CATEGORICAL, dataType, categories);
                List<String> categoryNames;
                List<String> categoryValues;
                switch(dataType) {
                    case BOOLEAN:
                        categoryNames = Arrays.asList("FALSE", "TRUE");
                        categoryValues = Arrays.asList("false", "true");
                        break;
                    default:
                        categoryNames = categories;
                        categoryValues = categories;
                        break;
                }
                formula.addField(dataField, categoryNames, categoryValues);
            } else {
                DataField dataField = encoder.createDataField(name, OpType.CONTINUOUS, dataType);
                formula.addField(dataField);
            }
        }
    }
    Collection<Map.Entry<FieldName, List<String>>> entries = expressionFields.entrySet();
    for (Map.Entry<FieldName, List<String>> entry : entries) {
        FieldName name = entry.getKey();
        List<String> categories = entry.getValue();
        DataField dataField = encoder.getDataField(name);
        if (dataField == null) {
            OpType opType = OpType.CONTINUOUS;
            DataType dataType = DataType.DOUBLE;
            if (categories != null && categories.size() > 0) {
                opType = OpType.CATEGORICAL;
            }
            RGenericVector data = context.getData();
            if (data != null && data.hasValue(name.getValue())) {
                RVector<?> column = (RVector<?>) data.getValue(name.getValue());
                dataType = column.getDataType();
            }
            dataField = encoder.createDataField(name, opType, dataType, categories);
        }
    }
    return formula;
}
Also used : DataField(org.dmg.pmml.DataField) Expression(org.dmg.pmml.Expression) DataType(org.dmg.pmml.DataType) OpType(org.dmg.pmml.OpType) ArrayList(java.util.ArrayList) List(java.util.List) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with Expression

use of org.dmg.pmml.Expression 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);
            }
        }
    }
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply)

Example 8 with Expression

use of org.dmg.pmml.Expression 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);
                }
            }
        }
    }
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply)

Example 9 with Expression

use of org.dmg.pmml.Expression 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);
}
Also used : PowerFeature(org.jpmml.converter.PowerFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Feature(org.jpmml.converter.Feature) PowerFeature(org.jpmml.converter.PowerFeature) BinaryFeature(org.jpmml.converter.BinaryFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) BooleanFeature(org.jpmml.converter.BooleanFeature) InteractionFeature(org.jpmml.converter.InteractionFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) DerivedField(org.dmg.pmml.DerivedField)

Example 10 with Expression

use of org.dmg.pmml.Expression in project jpmml-r by jpmml.

the class Formula method checkApply.

private static boolean checkApply(Apply apply, String function, Class<? extends Expression>... expressionClazzes) {
    if ((function).equals(apply.getFunction())) {
        List<Expression> expressions = apply.getExpressions();
        if (expressionClazzes.length == expressions.size()) {
            for (int i = 0; i < expressionClazzes.length; i++) {
                Class<? extends Expression> expressionClazz = expressionClazzes[i];
                Expression expression = expressions.get(i);
                if (!(expressionClazz).isInstance(expression)) {
                    return false;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : Expression(org.dmg.pmml.Expression)

Aggregations

Expression (org.dmg.pmml.Expression)22 Apply (org.dmg.pmml.Apply)11 Test (org.junit.Test)8 DerivedField (org.dmg.pmml.DerivedField)7 ArrayList (java.util.ArrayList)6 ContinuousFeature (org.jpmml.converter.ContinuousFeature)6 Feature (org.jpmml.converter.Feature)6 FieldRef (org.dmg.pmml.FieldRef)5 Constant (org.dmg.pmml.Constant)4 Vector (org.apache.spark.ml.linalg.Vector)3 DataType (org.dmg.pmml.DataType)2 FieldName (org.dmg.pmml.FieldName)2 OpType (org.dmg.pmml.OpType)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 MaxAbsScalerModel (org.apache.spark.ml.feature.MaxAbsScalerModel)1 MinMaxScalerModel (org.apache.spark.ml.feature.MinMaxScalerModel)1 PCAModel (org.apache.spark.ml.feature.PCAModel)1