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