org.jpmml.evaluator.FieldValue Java Examples

The following examples show how to use org.jpmml.evaluator.FieldValue. 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: 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 #2
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 #3
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public <V extends Number> Value<V> updateProduct(Value<V> product, FieldValue value){
	Matrix matrix = getMatrix();

	int row = getIndex(value);
	int column = getIndex(getCategory());
	if(row < 0 || column < 0){
		PPCell ppCell = getPPCell();

		throw new InvalidElementException(ppCell);
	}

	Number result = MatrixUtil.getElementAt(matrix, row + 1, column + 1);
	if(result == null){
		throw new InvalidElementException(matrix);
	}

	return product.multiply(result);
}
 
Example #4
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 #5
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 #6
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private BaselineStratum getBaselineStratum(BaseCumHazardTables baseCumHazardTables, FieldValue value){

	if(baseCumHazardTables instanceof MapHolder){
		MapHolder<?> mapHolder = (MapHolder<?>)baseCumHazardTables;

		return (BaselineStratum)mapHolder.get(value.getDataType(), value.getValue());
	}

	List<BaselineStratum> baselineStrata = baseCumHazardTables.getBaselineStrata();
	for(BaselineStratum baselineStratum : baselineStrata){
		Object category = baselineStratum.getValue();
		if(category == null){
			throw new MissingAttributeException(baselineStratum, PMMLAttributes.BASELINESTRATUM_VALUE);
		} // End if

		if(value.equalsValue(category)){
			return baselineStratum;
		}
	}

	return null;
}
 
Example #7
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Map<Integer, BitSet> loadInstanceFlags(NearestNeighborModelEvaluator modelEvaluator){
	Map<Integer, BitSet> result = new LinkedHashMap<>();

	Map<Integer, List<FieldValue>> valueMap = modelEvaluator.getValue(NearestNeighborModelEvaluator.instanceValueCache, createInstanceValueLoader(modelEvaluator));

	Maps.EntryTransformer<Integer, List<FieldValue>, BitSet> transformer = new Maps.EntryTransformer<Integer, List<FieldValue>, BitSet>(){

		@Override
		public BitSet transformEntry(Integer key, List<FieldValue> value){
			return MeasureUtil.toBitSet(value);
		}
	};
	result.putAll(Maps.transformEntries(valueMap, transformer));

	return result;
}
 
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: AbstractFunction.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected FieldValue getArgument(List<FieldValue> arguments, int index){

		if(this instanceof MissingValueTolerant){
			return getOptionalArgument(arguments, index);
		}

		return getRequiredArgument(arguments, index);
	}
 
Example #10
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 #11
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Number getOffset(GeneralRegressionModel generalRegressionModel, EvaluationContext context){
	FieldName offsetVariable = generalRegressionModel.getOffsetVariable();

	if(offsetVariable != null){
		FieldValue value = getVariable(offsetVariable, context);

		return value.asNumber();
	}

	return generalRegressionModel.getOffsetValue();
}
 
Example #12
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 #13
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 #14
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> ClusterAffinityDistribution<V> evaluateDistance(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<ClusteringField> clusteringFields, List<FieldValue> values){
	ClusteringModel clusteringModel = getModel();

	List<Cluster> clusters = clusteringModel.getClusters();

	Value<V> adjustment;

	MissingValueWeights missingValueWeights = clusteringModel.getMissingValueWeights();
	if(missingValueWeights != null){
		Array array = missingValueWeights.getArray();

		List<? extends Number> adjustmentValues = ArrayUtil.asNumberList(array);
		if(values.size() != adjustmentValues.size()){
			throw new InvalidElementException(missingValueWeights);
		}

		adjustment = MeasureUtil.calculateAdjustment(valueFactory, values, adjustmentValues);
	} else

	{
		adjustment = MeasureUtil.calculateAdjustment(valueFactory, values);
	}

	ClusterAffinityDistribution<V> result = createClusterAffinityDistribution(Classification.Type.DISTANCE, clusters);

	for(Cluster cluster : clusters){
		List<FieldValue> clusterValues = CacheUtil.getValue(cluster, ClusteringModelEvaluator.clusterValueCache);

		if(values.size() != clusterValues.size()){
			throw new InvalidElementException(cluster);
		}

		Value<V> distance = MeasureUtil.evaluateDistance(valueFactory, comparisonMeasure, clusteringFields, values, clusterValues, adjustment);

		result.put(cluster, distance);
	}

	return result;
}
 
Example #15
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 #16
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 #17
Source File: StreamlineJPMMLModelRunner.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, List<Object>> scoredTuplePerStream(Tuple input) {
    final Map<FieldName, Object> rawInputs = extractRawInputs(input);
    final Map<FieldName, FieldValue> preProcInputs = preProcessInputs(rawInputs);
    final Map<FieldName, ?> predScores = predictScores(preProcInputs);

    return toStreamLineEvents(predScores, input);
}
 
Example #18
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Integer getTrials(GeneralRegressionModel generalRegressionModel, EvaluationContext context){
	FieldName trialsVariable = generalRegressionModel.getTrialsVariable();

	if(trialsVariable != null){
		FieldValue value = getVariable(trialsVariable, context);

		return value.asInteger();
	}

	return generalRegressionModel.getTrialsValue();
}
 
Example #19
Source File: NaiveBayesModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private TargetValueCounts getTargetValueCounts(BayesInput bayesInput, FieldValue value){

	if(bayesInput instanceof MapHolder){
		MapHolder<?> mapHolder = (MapHolder<?>)bayesInput;

		return (TargetValueCounts)mapHolder.get(value.getDataType(), value.getValue());
	}

	List<PairCounts> pairCounts = bayesInput.getPairCounts();
	for(PairCounts pairCount : pairCounts){
		Object category = pairCount.getValue();
		if(category == null){
			throw new MissingAttributeException(pairCount, PMMLAttributes.PAIRCOUNTS_VALUE);
		} // End if

		if(value.equalsValue(category)){
			TargetValueCounts targetValueCounts = pairCount.getTargetValueCounts();
			if(targetValueCounts == null){
				throw new MissingElementException(pairCount, PMMLElements.PAIRCOUNTS_TARGETVALUECOUNTS);
			}

			return targetValueCounts;
		}
	}

	return null;
}
 
Example #20
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> List<InstanceResult<V>> evaluateInstanceRows(ValueFactory<V> valueFactory, EvaluationContext context){
	NearestNeighborModel nearestNeighborModel = getModel();

	ComparisonMeasure comparisonMeasure = nearestNeighborModel.getComparisonMeasure();

	List<FieldValue> values = new ArrayList<>();

	KNNInputs knnInputs = nearestNeighborModel.getKNNInputs();
	for(KNNInput knnInput : knnInputs){
		FieldName name = knnInput.getField();
		if(name == null){
			throw new MissingAttributeException(knnInput, PMMLAttributes.KNNINPUT_FIELD);
		}

		FieldValue value = context.evaluate(name);

		values.add(value);
	}

	Measure measure = MeasureUtil.ensureMeasure(comparisonMeasure);

	if(measure instanceof Similarity){
		return evaluateSimilarity(valueFactory, comparisonMeasure, knnInputs.getKNNInputs(), values);
	} else

	if(measure instanceof Distance){
		return evaluateDistance(valueFactory, comparisonMeasure, knnInputs.getKNNInputs(), values);
	} else

	{
		throw new UnsupportedElementException(measure);
	}
}
 
Example #21
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private Table<Integer, FieldName, FieldValue> getTrainingInstances(){

		if(this.trainingInstances == null){
			this.trainingInstances = getValue(NearestNeighborModelEvaluator.trainingInstanceCache, createTrainingInstanceLoader(this));
		}

		return this.trainingInstances;
	}
 
Example #22
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Callable<Table<Integer, FieldName, FieldValue>> createTrainingInstanceLoader(NearestNeighborModelEvaluator modelEvaluator){
	return new Callable<Table<Integer, FieldName, FieldValue>>(){

		@Override
		public Table<Integer, FieldName, FieldValue> call(){
			return parseTrainingInstances(modelEvaluator);
		}
	};
}
 
Example #23
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private Map<Integer, List<FieldValue>> getInstanceValues(){

		if(this.instanceValues == null){
			this.instanceValues = getValue(NearestNeighborModelEvaluator.instanceValueCache, createInstanceValueLoader(this));
		}

		return this.instanceValues;
	}
 
Example #24
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Callable<Map<Integer, List<FieldValue>>> createInstanceValueLoader(NearestNeighborModelEvaluator modelEvaluator){
	return new Callable<Map<Integer, List<FieldValue>>>(){

		@Override
		public Map<Integer, List<FieldValue>> call(){
			return loadInstanceValues(modelEvaluator);
		}
	};
}
 
Example #25
Source File: TransformationDictionaryTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private FieldValue evaluate(FieldName name, Map<FieldName, ?> arguments) throws Exception {
	ModelEvaluator<?> evaluator = createModelEvaluator();

	ModelEvaluationContext context = evaluator.createEvaluationContext();
	context.setArguments(arguments);

	return context.evaluate(name);
}
 
Example #26
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 #27
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 #28
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 #29
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 #30
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);
}