org.jpmml.evaluator.FieldValueUtil Java Examples

The following examples show how to use org.jpmml.evaluator.FieldValueUtil. 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: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private Function<Integer, String> createIdentifierResolver(FieldName name, Table<Integer, FieldName, FieldValue> table){
	Function<Integer, String> function = new Function<Integer, String>(){

		@Override
		public String apply(Integer row){
			FieldValue value = table.get(row, name);
			if(FieldValueUtil.isMissing(value)){
				throw new MissingValueException(name);
			}

			return value.asString();
		}
	};

	return function;
}
 
Example #2
Source File: StandardDeviationFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public FieldValue evaluate(List<FieldValue> arguments){
	checkVariableArityArguments(arguments, 1, 2);

	Double result;

	if(arguments.size() > 1){
		result = evaluate(getRequiredArgument(arguments, 0).asCollection(), getRequiredArgument(arguments, 1).asBoolean());
	} else

	{
		result = evaluate(getRequiredArgument(arguments, 0).asCollection(), Boolean.FALSE);
	}

	return FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, result);
}
 
Example #3
Source File: AbstractFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected FieldValue getRequiredArgument(List<FieldValue> arguments, int index){
	FieldValue argument = arguments.get(index);

	if(FieldValueUtil.isMissing(argument)){
		String alias = null;

		List<String> aliases = getAliases();
		if((aliases != null) && (index < aliases.size())){
			alias = aliases.get(index);
		} // End if

		if(alias != null){
			throw new FunctionException(this, "Missing " + PMMLException.formatKey(alias) + " value at position " + index);
		} else

		{
			throw new FunctionException(this, "Missing value at position " + index);
		}
	}

	return argument;
}
 
Example #4
Source File: ArithmeticFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue first, FieldValue second){

	// "If one of the input fields of a simple arithmetic function is a missing value, then the result evaluates to missing value"
	if(FieldValueUtil.isMissing(first) || FieldValueUtil.isMissing(second)){
		return FieldValues.MISSING_VALUE;
	}

	DataType dataType = TypeUtil.getCommonDataType(first.getDataType(), second.getDataType());

	Number result;

	try {
		result = evaluate(first.asNumber(), second.asNumber());
	} catch(ArithmeticException ae){
		throw new UndefinedResultException()
			.initCause(ae);
	}

	return FieldValueUtil.create(dataType, OpType.CONTINUOUS, result);
}
 
Example #5
Source File: FunctionTransformerTest.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Object evaluate(String function, Object value){
	UFunc ufunc = new UFunc("numpy.core", "_ufunc_reconstruct");
	ufunc.__init__(new String[]{"numpy", function});

	FieldName name = FieldName.create("x");

	DataType dataType;

	if(value instanceof Integer){
		dataType = DataType.INTEGER;
	} else

	if(value instanceof Float){
		dataType = DataType.FLOAT;
	} else

	{
		dataType = DataType.DOUBLE;
	}

	EvaluationContext context = new VirtualEvaluationContext();
	context.declare(name, FieldValueUtil.create(dataType, OpType.CONTINUOUS, value));

	Expression expression = UFuncUtil.encodeUFunc(ufunc, Collections.singletonList(new FieldRef(name)));

	FieldValue result = ExpressionUtil.evaluate(expression, context);

	return FieldValueUtil.getValue(result);
}
 
Example #6
Source File: PercentileFunctionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Number evaluate(List<Double> values, int quantile){
	Function percentile = new PercentileFunction();

	List<FieldValue> arguments = Arrays.asList(
		FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, values),
		FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, quantile)
	);

	return (percentile.evaluate(arguments)).asNumber();
}
 
Example #7
Source File: MeanFunctionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Number evaluate(List<Double> values){
	Function mean = new MeanFunction();

	List<FieldValue> arguments = Arrays.asList(
		FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, values)
	);

	return (mean.evaluate(arguments)).asNumber();
}
 
Example #8
Source File: StandardDeviationFunctionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Number evaluate(List<Double> values, boolean flag){
	Function standardDeviation = new StandardDeviationFunction();

	List<FieldValue> arguments = Arrays.asList(
		FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, values),
		FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, flag)
	);

	return (standardDeviation.evaluate(arguments)).asNumber();
}
 
Example #9
Source File: StandardDeviationFunctionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Number evaluate(List<Double> values){
	Function standardDeviation = new StandardDeviationFunction();

	List<FieldValue> arguments = Arrays.asList(
		FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, values)
	);

	return (standardDeviation.evaluate(arguments)).asNumber();
}
 
Example #10
Source File: SplitFunctionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public Collection<String> evaluate(String input, String pattern){
	Function split = new SplitFunction();

	List<FieldValue> arguments = Arrays.asList(
		FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, input),
		FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, pattern)
	);

	return (List)(split.evaluate(arguments)).asCollection();
}
 
Example #11
Source File: TrigonometricFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param value Angle in radians.
 */
@Override
public FieldValue evaluate(FieldValue value){
	Double result = evaluate(value.asNumber());
	if(result.isNaN()){
		throw new NaNResultException();
	}

	return FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, result);
}
 
Example #12
Source File: LogicalFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public FieldValue evaluate(List<FieldValue> arguments){
	checkVariableArityArguments(arguments, 2);

	Boolean result = getRequiredArgument(arguments, 0).asBoolean();

	for(int i = 1; i < arguments.size(); i++){
		result = evaluate(result, getRequiredArgument(arguments, i).asBoolean());
	}

	return FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, result);
}
 
Example #13
Source File: ValueSpaceFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private FieldValue evaluate(FieldValue value, List<FieldValue> values){
	Boolean result;

	if(FieldValueUtil.isMissing(value)){
		result = evaluate(values.contains(FieldValues.MISSING_VALUE));
	} else

	{
		result = evaluate(value.isIn(values));
	}

	return FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, result);
}
 
Example #14
Source File: UnaryMathFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue value){
	DataType dataType = value.getDataType();

	Number result = evaluate(value.asNumber());

	return FieldValueUtil.create(dataType, OpType.CONTINUOUS, result);
}
 
Example #15
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public void checkValue(Object expectedValue, String sqlExpression){
	Expression expression = translateInternal("SELECT (" + sqlExpression + ") FROM __THIS__");

	Object sparkValue = expression.eval(InternalRow.empty());

	if(expectedValue instanceof String){
		assertEquals(expectedValue, sparkValue.toString());
	} else

	if(expectedValue instanceof Integer){
		assertEquals(expectedValue, ((Number)sparkValue).intValue());
	} else

	if(expectedValue instanceof Float){
		assertEquals(expectedValue, ((Number)sparkValue).floatValue());
	} else

	if(expectedValue instanceof Double){
		assertEquals(expectedValue, ((Number)sparkValue).doubleValue());
	} else

	{
		assertEquals(expectedValue, sparkValue);
	}

	org.dmg.pmml.Expression pmmlExpression = ExpressionTranslator.translate(expression);

	EvaluationContext context = new VirtualEvaluationContext();
	context.declareAll(Collections.emptyMap());

	FieldValue value = ExpressionUtil.evaluate(pmmlExpression, context);

	Object pmmlValue = FieldValueUtil.getValue(value);
	assertEquals(expectedValue, pmmlValue);
}
 
Example #16
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<FieldValue> load(Cluster cluster){
	Array array = cluster.getArray();

	List<? extends Number> values = ArrayUtil.asNumberList(array);

	return ImmutableList.copyOf(Lists.transform(values, value -> FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, value)));
}
 
Example #17
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private FieldValue getVariable(FieldName name, EvaluationContext context){
	FieldValue value = context.evaluate(name);

	if(FieldValueUtil.isMissing(value)){
		throw new MissingValueException(name);
	}

	return value;
}
 
Example #18
Source File: AggregateMathFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public FieldValue evaluate(List<FieldValue> arguments){
	StorelessUnivariateStatistic statistic = createStatistic();

	DataType dataType = null;

	for(int i = 0; i < arguments.size(); i++){
		FieldValue value = getOptionalArgument(arguments, i);

		// "Missing values in the input to an aggregate function are simply ignored"
		if(FieldValueUtil.isMissing(value)){
			continue;
		}

		statistic.increment((value.asNumber()).doubleValue());

		if(dataType != null){
			dataType = TypeUtil.getCommonDataType(dataType, value.getDataType());
		} else

		{
			dataType = value.getDataType();
		}
	}

	// "If all inputs are missing, then the result evaluates to a missing value"
	if(statistic.getN() == 0){
		return FieldValues.MISSING_VALUE;
	}

	Double result = statistic.getResult();

	return FieldValueUtil.create(getResultDataType(dataType), OpType.CONTINUOUS, result);
}
 
Example #19
Source File: TransformationDictionaryTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private void assertValueEquals(Object expected, FieldValue actual){
	assertEquals(expected, FieldValueUtil.getValue(actual));
}
 
Example #20
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<FieldValue> parseCategories(TypeInfo typeInfo){
	List<Object> categories = getCategories();

	return Lists.transform(categories, category -> FieldValueUtil.create(typeInfo, category));
}
 
Example #21
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private <V extends Number> V calculateContinuousTarget(ValueFactory<V> valueFactory, FieldName name, List<InstanceResult<V>> instanceResults, Table<Integer, FieldName, FieldValue> table){
	NearestNeighborModel nearestNeighborModel = getModel();

	Number threshold = nearestNeighborModel.getThreshold();
	NearestNeighborModel.ContinuousScoringMethod continuousScoringMethod = nearestNeighborModel.getContinuousScoringMethod();

	ValueAggregator<V> aggregator;

	switch(continuousScoringMethod){
		case AVERAGE:
			aggregator = new ValueAggregator.UnivariateStatistic<>(valueFactory);
			break;
		case WEIGHTED_AVERAGE:
			aggregator = new ValueAggregator.WeightedUnivariateStatistic<>(valueFactory);
			break;
		case MEDIAN:
			aggregator = new ValueAggregator.Median<>(valueFactory, instanceResults.size());
			break;
		default:
			throw new UnsupportedAttributeException(nearestNeighborModel, continuousScoringMethod);
	}

	for(InstanceResult<V> instanceResult : instanceResults){
		FieldValue value = table.get(instanceResult.getId(), name);
		if(FieldValueUtil.isMissing(value)){
			throw new MissingValueException(name);
		}

		Number targetValue = value.asNumber();

		switch(continuousScoringMethod){
			case AVERAGE:
			case MEDIAN:
				aggregator.add(targetValue);
				break;
			case WEIGHTED_AVERAGE:
				InstanceResult.Distance distance = TypeUtil.cast(InstanceResult.Distance.class, instanceResult);

				Value<V> weight = distance.getWeight(threshold);

				aggregator.add(targetValue, weight.getValue());
				break;
			default:
				throw new UnsupportedAttributeException(nearestNeighborModel, continuousScoringMethod);
		}
	}

	switch(continuousScoringMethod){
		case AVERAGE:
			return (aggregator.average()).getValue();
		case WEIGHTED_AVERAGE:
			return (aggregator.weightedAverage()).getValue();
		case MEDIAN:
			return (aggregator.median()).getValue();
		default:
			throw new UnsupportedAttributeException(nearestNeighborModel, continuousScoringMethod);
	}
}
 
Example #22
Source File: MeanFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue value){
	Double result = evaluate(value.asCollection());

	return FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, result);
}
 
Example #23
Source File: PercentileFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue first, FieldValue second){
	Double result = evaluate(first.asCollection(), second.asInteger());

	return FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, result);
}
 
Example #24
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue prepare(Object value){
	return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, value);
}
 
Example #25
Source File: UnaryStringFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue value){
	String result = evaluate(value.asString());

	return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result);
}
 
Example #26
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue prepare(Object value){
	DerivedField derivedField = getDerivedField();
	MiningField miningField = getMiningField();

	if(miningField != null){
		return InputFieldUtil.prepareInputValue(derivedField, miningField, value);
	}

	TypeInfo typeInfo = new TypeInfo(){

		@Override
		public DataType getDataType(){
			DataType dataType = derivedField.getDataType();
			if(dataType == null){
				throw new MissingAttributeException(derivedField, org.dmg.pmml.PMMLAttributes.DERIVEDFIELD_DATATYPE);
			}

			return dataType;
		}

		@Override
		public OpType getOpType(){
			OpType opType = derivedField.getOpType();
			if(opType == null){
				throw new MissingAttributeException(derivedField, org.dmg.pmml.PMMLAttributes.DERIVEDFIELD_OPTYPE);
			}

			return opType;
		}

		@Override
		public List<?> getOrdering(){
			List<?> ordering = FieldUtil.getValidValues(derivedField);

			return ordering;
		}
	};

	return FieldValueUtil.create(typeInfo, value);
}
 
Example #27
Source File: ComparisonFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue first, FieldValue second){
	Boolean result = evaluate((first).compareToValue(second));

	return FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, result);
}
 
Example #28
Source File: EqualityFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue first, FieldValue second){
	Boolean result = evaluate((first).equalsValue(second));

	return FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, result);
}
 
Example #29
Source File: UnaryBooleanFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue value){
	Boolean result = evaluate(value.asBoolean());

	return FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, result);
}
 
Example #30
Source File: RoundingFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue evaluate(FieldValue value){
	Number result = evaluate(value.asNumber());

	return FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, result);
}