Java Code Examples for org.dmg.pmml.FieldName#create()

The following examples show how to use org.dmg.pmml.FieldName#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void evaluateMapValues(){
	FieldName name = FieldName.create("x");

	List<List<String>> rows = Arrays.asList(
		Arrays.asList("0", "zero"),
		Arrays.asList("1", "one")
	);

	MapValues mapValues = new MapValues("data:output", null, createInlineTable(rows, Arrays.asList("data:input", "data:output")))
		.addFieldColumnPairs(new FieldColumnPair(name, "data:input"));

	assertEquals("zero", evaluate(mapValues, name, "0"));
	assertEquals("one", evaluate(mapValues, name, "1"));
	assertEquals(null, evaluate(mapValues, name, "3"));

	assertEquals(null, evaluate(mapValues, name, null));

	mapValues.setMapMissingTo("Missing");

	assertEquals("Missing", evaluate(mapValues, name, null));

	mapValues.setDefaultValue("Default");

	assertEquals("Default", evaluate(mapValues, name, "3"));
}
 
Example 2
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateCaseWhenExpression(){
	String string = "CASE WHEN x1 < 0 THEN x1 WHEN x2 > 0 THEN x2 ELSE 0 END";

	FieldRef first = new FieldRef(FieldName.create("x1"));
	FieldRef second = new FieldRef(FieldName.create("x2"));

	Constant zero = PMMLUtil.createConstant(0, DataType.DOUBLE);

	Apply expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSTHAN)
			.addExpressions(first, zero)
		)
		.addExpressions(first)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATERTHAN)
				.addExpressions(second, zero)
			)
			.addExpressions(second)
			.addExpressions(zero)
		);

	checkExpression(expected, string);
}
 
Example 3
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void evaluateApplyJavaFunction(){
	FieldName name = FieldName.create("x");

	FieldRef fieldRef = new FieldRef(name);

	Apply apply = new Apply(EchoFunction.class.getName())
		.addExpressions(fieldRef);

	try {
		evaluate(apply);

		fail();
	} catch(EvaluationException ee){
		assertEquals(fieldRef, ee.getContext());
	}

	assertEquals("Hello World!", evaluate(apply, name, "Hello World!"));
}
 
Example 4
Source File: CountVectorizerModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	CountVectorizerModel transformer = getTransformer();

	DocumentFeature documentFeature = (DocumentFeature)encoder.getOnlyFeature(transformer.getInputCol());

	ParameterField documentField = new ParameterField(FieldName.create("document"));

	ParameterField termField = new ParameterField(FieldName.create("term"));

	TextIndex textIndex = new TextIndex(documentField.getName(), new FieldRef(termField.getName()))
		.setTokenize(Boolean.TRUE)
		.setWordSeparatorCharacterRE(documentFeature.getWordSeparatorRE())
		.setLocalTermWeights(transformer.getBinary() ? TextIndex.LocalTermWeights.BINARY : null);

	Set<DocumentFeature.StopWordSet> stopWordSets = documentFeature.getStopWordSets();
	for(DocumentFeature.StopWordSet stopWordSet : stopWordSets){

		if(stopWordSet.isEmpty()){
			continue;
		}

		String tokenRE;

		String wordSeparatorRE = documentFeature.getWordSeparatorRE();
		switch(wordSeparatorRE){
			case "\\s+":
				tokenRE = "(^|\\s+)\\p{Punct}*(" + JOINER.join(stopWordSet) + ")\\p{Punct}*(\\s+|$)";
				break;
			case "\\W+":
				tokenRE = "(\\W+)(" + JOINER.join(stopWordSet) + ")(\\W+)";
				break;
			default:
				throw new IllegalArgumentException("Expected \"\\s+\" or \"\\W+\" as splitter regex pattern, got \"" + wordSeparatorRE + "\"");
		}

		Map<String, List<String>> data = new LinkedHashMap<>();
		data.put("string", Collections.singletonList(tokenRE));
		data.put("stem", Collections.singletonList(" "));
		data.put("regex", Collections.singletonList("true"));

		TextIndexNormalization textIndexNormalization = new TextIndexNormalization(null, PMMLUtil.createInlineTable(data))
			.setCaseSensitive(stopWordSet.isCaseSensitive())
			.setRecursive(Boolean.TRUE); // Handles consecutive matches. See http://stackoverflow.com/a/25085385

		textIndex.addTextIndexNormalizations(textIndexNormalization);
	}

	DefineFunction defineFunction = new DefineFunction("tf" + "@" + String.valueOf(CountVectorizerModelConverter.SEQUENCE.getAndIncrement()), OpType.CONTINUOUS, DataType.INTEGER, null, textIndex)
		.addParameterFields(documentField, termField);

	encoder.addDefineFunction(defineFunction);

	List<Feature> result = new ArrayList<>();

	String[] vocabulary = transformer.vocabulary();
	for(int i = 0; i < vocabulary.length; i++){
		String term = vocabulary[i];

		if(TermUtil.hasPunctuation(term)){
			throw new IllegalArgumentException("Punctuated vocabulary terms (" + term + ") are not supported");
		}

		result.add(new TermFeature(encoder, defineFunction, documentFeature, term));
	}

	return result;
}
 
Example 5
Source File: AppPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
public static DataDictionary buildDataDictionary(
    InputSchema schema,
    CategoricalValueEncodings categoricalValueEncodings) {
  List<String> featureNames = schema.getFeatureNames();

  List<DataField> dataFields = new ArrayList<>();
  for (int featureIndex = 0; featureIndex < featureNames.size(); featureIndex++) {
    String featureName = featureNames.get(featureIndex);
    OpType opType;
    DataType dataType;
    if (schema.isNumeric(featureName)) {
      opType = OpType.CONTINUOUS;
      dataType = DataType.DOUBLE;
    } else if (schema.isCategorical(featureName)) {
      opType = OpType.CATEGORICAL;
      dataType = DataType.STRING;
    } else {
      // Don't know
      opType = null;
      dataType = null;
    }
    DataField field = new DataField(FieldName.create(featureName), opType, dataType);
    if (schema.isCategorical(featureName)) {
      Objects.requireNonNull(categoricalValueEncodings);
      categoricalValueEncodings.getEncodingValueMap(featureIndex).entrySet().stream().
          sorted(Comparator.comparing(Map.Entry::getKey)).
          map(Map.Entry::getValue).
          forEach(value -> field.addValues(new Value(value)));
    }
    dataFields.add(field);
  }

  return new DataDictionary(dataFields).setNumberOfFields(dataFields.size());
}
 
Example 6
Source File: ContrastMatrixTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void evaluate() throws Exception {
	ModelEvaluator<?> evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("gender", "f", "educ", 19d, "jobcat", "3", "salbegin", 45000d);

	Map<FieldName, ?> results = evaluator.evaluate(arguments);

	FieldName lowField = FieldName.create("Probability_Low");
	FieldName highField = FieldName.create("Probability_High");

	// Expected values have been calculated by hand
	assertEquals(0.81956470d, (Double)getOutput(results, lowField), 1e-8);
	assertEquals(0.18043530d, (Double)getOutput(results, highField), 1e-8);
}
 
Example 7
Source File: SelectFirstUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private MiningModel encodeModel(MiningFunction miningFunction, List<Object[]> steps, Schema schema){

	if(steps.size() < 1){
		throw new IllegalArgumentException();
	}

	Label label = schema.getLabel();
	List<? extends Feature> features = schema.getFeatures();

	Segmentation segmentation = new Segmentation(Segmentation.MultipleModelMethod.SELECT_FIRST, null);

	Scope scope = new DataFrameScope(FieldName.create("X"), features);

	for(Object[] step : steps){
		String name = TupleUtil.extractElement(step, 0, String.class);
		Estimator estimator = TupleUtil.extractElement(step, 1, Estimator.class);
		String predicate = TupleUtil.extractElement(step, 2, String.class);

		if(!(miningFunction).equals(estimator.getMiningFunction())){
			throw new IllegalArgumentException();
		}

		Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope);

		Model model = estimator.encodeModel(schema);

		Segment segment = new Segment(pmmlPredicate, model)
			.setId(name);

		segmentation.addSegments(segment);
	}

	MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(label))
		.setSegmentation(segmentation);

	return miningModel;
}
 
Example 8
Source File: TransformationDictionaryTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void evaluateSimpleTable() throws Exception {
	FieldName name = FieldName.create("SimpleTable");

	assertValueEquals(null, evaluate(name, createArguments("Value", null)));

	assertValueEquals("first", evaluate(name, createArguments("Value", 1)));
	assertValueEquals("second", evaluate(name, createArguments("Value", 2)));

	assertValueEquals(null, evaluate(name, createArguments("Value", 3)));
}
 
Example 9
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateBooleanSimplePredicate(){
	FieldName flag = FieldName.create("flag");

	SimplePredicate simplePredicate = new SimplePredicate(flag, SimplePredicate.Operator.EQUAL, "true");

	assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, true));
	assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, false));

	simplePredicate.setOperator(SimplePredicate.Operator.NOT_EQUAL);

	assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, true));
	assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, false));

	simplePredicate.setValue("0.5");

	simplePredicate.setOperator(SimplePredicate.Operator.LESS_OR_EQUAL);

	assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, true));
	assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, false));

	simplePredicate.setOperator(SimplePredicate.Operator.GREATER_THAN);

	assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, true));
	assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, false));
}
 
Example 10
Source File: TensorFlowEncoder.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 4 votes vote down vote up
public DataField ensureDataField(SavedModel savedModel, NodeDef placeholder){

		if(!("Placeholder").equals(placeholder.getOp())){
			throw new IllegalArgumentException(placeholder.getName());
		}

		FieldName name = FieldName.create(placeholder.getName());

		DataField dataField = getDataField(name);
		if(dataField == null){
			Operation operation = savedModel.getOperation(placeholder.getName());

			Output output = operation.output(0);

			dataField = createDataField(name, TypeUtil.getOpType(output), TypeUtil.getDataType(output));
		}

		return dataField;
	}
 
Example 11
Source File: DataFrameMapper.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<Feature> initializeFeatures(SkLearnEncoder encoder){
	Object _default = getDefault();
	List<Object[]> rows = getFeatures();

	if(!(Boolean.FALSE).equals(_default)){
		throw new IllegalArgumentException("Attribute \'" + ClassDictUtil.formatMember(this, "default") + "\' must be set to the 'False' value");
	}

	List<Feature> result = new ArrayList<>();

	for(Object[] row : rows){
		List<Feature> rowFeatures = new ArrayList<>();

		List<String> columns = getColumnList(row);
		for(String column : columns){
			FieldName name = FieldName.create(column);

			DataField dataField = encoder.getDataField(name);
			if(dataField == null){
				dataField = encoder.createDataField(name);
			}

			rowFeatures.add(new WildcardFeature(encoder, dataField));
		}

		List<Transformer> transformers = getTransformerList(row);
		for(Transformer transformer : transformers){
			rowFeatures = transformer.updateAndEncodeFeatures(rowFeatures, encoder);
		}

		if(row.length > 2){
			Map<String, ?> options = (Map)row[2];

			String alias = (String)options.get("alias");
			if(alias != null){

				for(int i = 0; i < rowFeatures.size(); i++){
					Feature rowFeature = rowFeatures.get(i);

					encoder.renameFeature(rowFeature, rowFeatures.size() > 1 ? FieldName.create(alias + "_" + i) : FieldName.create(alias));
				}
			}
		}

		result.addAll(rowFeatures);
	}

	return result;
}
 
Example 12
Source File: XGBoostConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void encodeSchema(RExpEncoder encoder){
	RGenericVector booster = getObject();

	RStringVector featureNames = booster.getStringElement("feature_names", false);
	RGenericVector schema = booster.getGenericElement("schema", false);

	FeatureMap featureMap = ensureFeatureMap();

	if(featureNames != null){
		checkFeatureMap(featureMap, featureNames);
	} // End if

	if(schema != null){
		RVector<?> missing = schema.getVectorElement("missing", false);

		if(missing != null){
			featureMap.addMissingValue(ValueUtil.asString(missing.asScalar()));
		}
	}

	Learner learner = ensureLearner();

	ObjFunction obj = learner.obj();

	FieldName targetField = FieldName.create("_target");
	List<String> targetCategories = null;

	if(schema != null){
		RStringVector responseName = schema.getStringElement("response_name", false);
		RStringVector responseLevels = schema.getStringElement("response_levels", false);

		if(responseName != null){
			targetField = FieldName.create(responseName.asScalar());
		} // End if

		if(responseLevels != null){
			targetCategories = responseLevels.getValues();
		}
	}

	Label label = obj.encodeLabel(targetField, targetCategories, encoder);

	encoder.setLabel(label);

	List<Feature> features = featureMap.encodeFeatures(encoder);
	for(Feature feature : features){
		encoder.addFeature(feature);
	}
}
 
Example 13
Source File: FieldNameConverter.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldName convert(String value){
	FieldName name = FieldName.create(value);

	return name;
}
 
Example 14
Source File: BaseEstimatorTest.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateLogisticRegressionAudit() throws Exception {
	FieldName[] targetFields = {FieldName.create("Adjusted"), FieldName.create("h2o(Adjusted)")};

	evaluate("H2OLogisticRegression", "Audit", excludeFields(targetFields));
}
 
Example 15
Source File: RegressionTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateLinearRegressionAuto() throws Exception {
	FieldName[] transformFields = {FieldName.create("mpgBucket")};

	evaluate("LinearRegression", "Auto", excludeFields(transformFields));
}
 
Example 16
Source File: Openscoring.java    From openscoring with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private LoadingModelEvaluatorBuilder createLoadingModelEvaluatorBuilder(Config config){
	Config modelEvaluatorBuilderConfig = config.getConfig("modelEvaluatorBuilder");

	LoadingModelEvaluatorBuilder modelEvaluatorBuilder = new LoadingModelEvaluatorBuilder();

	Class<? extends ModelEvaluatorFactory> modelEvaluatorFactoryClazz = loadClass(ModelEvaluatorFactory.class, modelEvaluatorBuilderConfig);
	modelEvaluatorBuilder.setModelEvaluatorFactory(newInstance(modelEvaluatorFactoryClazz));

	Class<? extends ValueFactoryFactory> valueFactoryFactoryClazz = loadClass(ValueFactoryFactory.class, modelEvaluatorBuilderConfig);
	modelEvaluatorBuilder.setValueFactoryFactory(newInstance(valueFactoryFactoryClazz));

	modelEvaluatorBuilder.setOutputFilter(OutputFilters.KEEP_FINAL_RESULTS);

	// Jackson does not support the JSON serialization of <code>null</code> map keys
	ResultMapper resultMapper = new ResultMapper(){

		private FieldName defaultTargetName = FieldName.create(ModelResponse.DEFAULT_TARGET_NAME);


		@Override
		public FieldName apply(FieldName name){

			// A "phantom" default target field
			if(name == null){
				return this.defaultTargetName;
			}

			return name;
		}
	};

	modelEvaluatorBuilder.setResultMapper(resultMapper);

	boolean validate = modelEvaluatorBuilderConfig.getBoolean("validate");

	if(validate){
		Schema schema;

		try {
			schema = JAXBUtil.getSchema();
		} catch(SAXException | IOException e){
			throw new RuntimeException(e);
		}

		modelEvaluatorBuilder
			.setSchema(schema)
			.setValidationEventHandler(new SimpleValidationEventHandler());
	}

	boolean locatable = modelEvaluatorBuilderConfig.getBoolean("locatable");

	modelEvaluatorBuilder.setLocatable(locatable);

	VisitorBattery visitors = new VisitorBattery();

	List<String> visitorClassNames = modelEvaluatorBuilderConfig.getStringList("visitorClasses");
	for(String visitorClassName : visitorClassNames){
		Class<?> clazz = loadClass(Object.class, visitorClassName);

		if((Visitor.class).isAssignableFrom(clazz)){
			Class<? extends Visitor> visitorClazz = clazz.asSubclass(Visitor.class);

			visitors.add(visitorClazz);
		} else

		if((VisitorBattery.class).isAssignableFrom(clazz)){
			Class<? extends VisitorBattery> visitorBatteryClazz = clazz.asSubclass(VisitorBattery.class);

			VisitorBattery visitorBattery = newInstance(visitorBatteryClazz);

			visitors.addAll(visitorBattery);
		} else

		{
			throw new IllegalArgumentException(new ClassCastException(clazz.toString()));
		}
	}

	modelEvaluatorBuilder.setVisitors(visitors);

	return modelEvaluatorBuilder;
}
 
Example 17
Source File: RExpUtil.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public FieldName makeName(FieldName name){
	return FieldName.create(makeName(name.getValue()));
}
 
Example 18
Source File: FeatureConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public <T extends Transformer & HasOutputCol> FieldName formatName(T transformer){
	return FieldName.create(transformer.getOutputCol());
}
 
Example 19
Source File: ArrayListTransformerTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void transform(){
	Node node1a = new BranchNode();

	Node node2a = new BranchNode();
	Node node2b = new LeafNode();

	node1a.addNodes(node2a, node2b);

	Array array = new ComplexArray()
		.setType(Array.Type.INT)
		.setValue(Arrays.asList(-1, 1));

	Predicate predicate = new SimpleSetPredicate(FieldName.create("x"), SimpleSetPredicate.BooleanOperator.IS_IN, array);

	Node node3a = new LeafNode(null, predicate);

	node2a.addNodes(node3a);

	assertTrue(node1a.getNodes() instanceof ArrayList);
	assertTrue(node2a.getNodes() instanceof ArrayList);

	Object value = array.getValue();

	assertTrue(value instanceof ArrayList);
	assertTrue(value instanceof ComplexValue);

	TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a);

	ArrayListTransformer transformer = new ArrayListTransformer();
	transformer.applyTo(treeModel);

	assertTrue(node1a.getNodes() instanceof DoubletonList);
	assertTrue(node2a.getNodes() instanceof SingletonList);

	value = array.getValue();

	assertTrue(value instanceof ArrayList);
	assertTrue(value instanceof ComplexValue);
}
 
Example 20
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void evaluateSimplePredicate(){
	FieldName age = FieldName.create("age");

	SimplePredicate simplePredicate = new SimplePredicate(age, SimplePredicate.Operator.IS_MISSING, null);

	assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30));
	assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, null));

	simplePredicate.setOperator(SimplePredicate.Operator.IS_NOT_MISSING);

	assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30));
	assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, null));

	simplePredicate.setValue("30");

	simplePredicate.setOperator(SimplePredicate.Operator.EQUAL);

	assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30));
	assertEquals(null, evaluate(simplePredicate, age, null));

	simplePredicate.setOperator(SimplePredicate.Operator.NOT_EQUAL);

	assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30));
	assertEquals(null, evaluate(simplePredicate, age, null));

	simplePredicate.setOperator(SimplePredicate.Operator.LESS_THAN);

	assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30));

	simplePredicate.setOperator(SimplePredicate.Operator.LESS_OR_EQUAL);

	assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30));

	simplePredicate.setOperator(SimplePredicate.Operator.GREATER_OR_EQUAL);

	assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30));

	simplePredicate.setOperator(SimplePredicate.Operator.GREATER_THAN);

	assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30));
}