Search in sources :

Example 6 with OPTYPE

use of org.dmg.pmml.OPTYPE in project shifu by ShifuML.

the class CsvUtil method prepare.

public static FieldValue prepare(InputField inputField, Object value) {
    if (value instanceof Collection) {
        Collection<?> rawValues = (Collection<?>) value;
        DataType dataType = null;
        OpType opType = null;
        Collection<Object> preparedValues = createCollection(rawValues);
        for (Object rawValue : rawValues) {
            FieldValue preparedValue = inputField.prepare(rawValue);
            if (preparedValue != null) {
                if (dataType == null) {
                    dataType = preparedValue.getDataType();
                }
                if (opType == null) {
                    opType = preparedValue.getOpType();
                }
            }
            preparedValues.add(FieldValueUtil.getValue(preparedValue));
        }
        return FieldValueUtil.create(dataType, opType, preparedValues);
    }
    return inputField.prepare(value);
}
Also used : Collection(java.util.Collection) DataType(org.dmg.pmml.DataType) OpType(org.dmg.pmml.OpType) FieldValue(org.jpmml.evaluator.FieldValue)

Example 7 with OPTYPE

use of org.dmg.pmml.OPTYPE 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 = terms.getIntegerAttribute("factors");
    RStringVector dataClasses = terms.getStringAttribute("dataClasses", false);
    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);
        String name = variable;
        OpType opType = OpType.CONTINUOUS;
        DataType dataType;
        if (dataClasses != null) {
            dataType = RExpUtil.getDataType(dataClasses.getElement(variable));
        } else {
            RVector<?> data = context.getData(name);
            if (data != null) {
                dataType = data.getDataType();
            } else {
                throw new IllegalArgumentException();
            }
        }
        List<String> categories = context.getCategories(variable);
        if (categories != null && categories.size() > 0) {
            opType = OpType.CATEGORICAL;
        }
        Expression expression = null;
        String shortName = name;
        expression: if ((variable.indexOf('(') > -1 && variable.indexOf(')') > -1) || (variable.indexOf(' ') > -1)) {
            try {
                expression = ExpressionTranslator.translateExpression(variable);
            } catch (Exception e) {
                break expression;
            }
            FunctionExpression functionExpression;
            if (expression instanceof FunctionExpression) {
                functionExpression = (FunctionExpression) expression;
            } else {
                FunctionExpression.Argument xArgument = new FunctionExpression.Argument("x", expression) {

                    @Override
                    public String formatExpression() {
                        return variable;
                    }
                };
                functionExpression = new FunctionExpression("base", "I", Collections.singletonList(xArgument));
            }
            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 {
                expression = null;
                break expression;
            }
            FunctionExpression.Argument xArgument = functionExpression.getArgument("x", 0);
            String value = (xArgument.formatExpression()).trim();
            shortName = (functionExpression.hasId("base", "I") ? value : (functionExpression.getFunction() + "(" + value + ")"));
        }
        List<String> categoryNames;
        List<?> categoryValues;
        if (dataType == DataType.BOOLEAN) {
            opType = OpType.CATEGORICAL;
            categoryNames = Arrays.asList("FALSE", "TRUE");
            categoryValues = Arrays.asList(Boolean.FALSE, Boolean.TRUE);
        } else {
            categoryNames = categories;
            categoryValues = categories;
        }
        if (expression != null) {
            DerivedField derivedField = encoder.createDerivedField(name, opType, dataType, expression).addExtensions(PMMLUtil.createExtension("variable", (Object) variable));
            if (categoryNames != null && categoryNames.size() > 0) {
                formula.addField(derivedField, categoryNames, categoryValues);
            } else {
                formula.addField(derivedField);
            }
            if (!(name).equals(shortName)) {
                encoder.renameField(name, shortName);
            }
        } else {
            if (categoryNames != null && categoryNames.size() > 0) {
                DataField dataField = encoder.createDataField(name, OpType.CATEGORICAL, dataType, categories);
                formula.addField(dataField, categoryNames, categoryValues);
            } else {
                DataField dataField = encoder.createDataField(name, OpType.CONTINUOUS, dataType);
                formula.addField(dataField);
            }
        }
    }
    Collection<Map.Entry<String, List<String>>> entries = expressionFields.entrySet();
    for (Map.Entry<String, List<String>> entry : entries) {
        String 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;
            }
            RVector<?> data = context.getData(name);
            if (data != null) {
                dataType = data.getDataType();
            }
            dataField = encoder.createDataField(name, opType, dataType, categories);
        }
    }
    return formula;
}
Also used : DataType(org.dmg.pmml.DataType) ArrayList(java.util.ArrayList) List(java.util.List) DataField(org.dmg.pmml.DataField) Expression(org.dmg.pmml.Expression) OpType(org.dmg.pmml.OpType) DerivedField(org.dmg.pmml.DerivedField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with OPTYPE

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

the class RExpEncoder method addFeature.

public void addFeature(Field<?> field) {
    Feature feature;
    OpType opType = field.requireOpType();
    switch(opType) {
        case CATEGORICAL:
            feature = new CategoricalFeature(this, (DataField) field);
            break;
        case CONTINUOUS:
            feature = new ContinuousFeature(this, field);
            break;
        default:
            throw new IllegalArgumentException();
    }
    addFeature(feature);
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) OpType(org.dmg.pmml.OpType) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature)

Example 9 with OPTYPE

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

the class RExpEncoder method setLabel.

public void setLabel(DataField dataField) {
    Label label;
    OpType opType = dataField.requireOpType();
    switch(opType) {
        case CATEGORICAL:
            label = new CategoricalLabel(dataField);
            break;
        case CONTINUOUS:
            label = new ContinuousLabel(dataField);
            break;
        default:
            throw new IllegalArgumentException();
    }
    setLabel(label);
}
Also used : CategoricalLabel(org.jpmml.converter.CategoricalLabel) CategoricalLabel(org.jpmml.converter.CategoricalLabel) ContinuousLabel(org.jpmml.converter.ContinuousLabel) Label(org.jpmml.converter.Label) OpType(org.dmg.pmml.OpType) ContinuousLabel(org.jpmml.converter.ContinuousLabel)

Aggregations

OpType (org.dmg.pmml.OpType)7 DataField (org.dmg.pmml.DataField)4 DataType (org.dmg.pmml.DataType)3 DATATYPE (org.dmg.pmml.DATATYPE)2 DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)2 FieldRef (org.dmg.pmml.FieldRefDocument.FieldRef)2 OPTYPE (org.dmg.pmml.OPTYPE)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 DerivedField (org.dmg.pmml.DerivedField)1 Expression (org.dmg.pmml.Expression)1 MiningFunction (org.dmg.pmml.MiningFunction)1 CategoricalFeature (org.jpmml.converter.CategoricalFeature)1 CategoricalLabel (org.jpmml.converter.CategoricalLabel)1 ContinuousFeature (org.jpmml.converter.ContinuousFeature)1 ContinuousLabel (org.jpmml.converter.ContinuousLabel)1 Feature (org.jpmml.converter.Feature)1