Java Code Examples for org.jpmml.evaluator.FieldValueUtil#isMissing()

The following examples show how to use org.jpmml.evaluator.FieldValueUtil#isMissing() . 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: 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 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: 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 5
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 6
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 7
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);
	}
}