org.jpmml.evaluator.Evaluator Java Examples

The following examples show how to use org.jpmml.evaluator.Evaluator. 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: ModelProvider.java    From openscoring with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void writeTo(Model model, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
	Evaluator evaluator = model.getEvaluator();

	HasPMML hasPMML = (HasPMML)evaluator;

	httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8"));
	httpHeaders.putSingle(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=model.pmml.xml"); // XXX

	PMML pmml = hasPMML.getPMML();

	try {
		Result result = new StreamResult(entityStream);

		Marshaller marshaller = JAXBUtil.createMarshaller();

		marshaller.marshal(pmml, result);
	} catch(JAXBException je){
		throw new InternalServerErrorException(je);
	}
}
 
Example #2
Source File: AssociationSchemaTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private void evaluate(List<String> items, List<String> recommendations, List<String> exclusiveRecommendations, List<String> ruleAssociations) throws Exception {
	Evaluator evaluator = createModelEvaluator();

	checkTargetFields(Collections.emptyList(), evaluator);

	Map<FieldName, ?> arguments = createItemArguments(items);

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

	assertEquals(recommendations, getOutput(results, "Recommendation"));
	assertEquals(exclusiveRecommendations, getOutput(results, "Exclusive_Recommendation"));
	assertEquals(ruleAssociations, getOutput(results, "Rule_Association"));

	assertEquals(Iterables.getFirst(recommendations, null), getOutput(results, "Top Recommendation"));
	assertEquals(Iterables.getFirst(exclusiveRecommendations, null), getOutput(results, "Top Exclusive_Recommendation"));
	assertEquals(Iterables.getFirst(ruleAssociations, null), getOutput(results, "Top Rule_Association"));
}
 
Example #3
Source File: MLModelRegistryService.java    From registry with Apache License 2.0 6 votes vote down vote up
private List<MLModelField> doGetOutputFieldsForPMMLStream(String pmmlContents) throws SAXException, JAXBException {
    List<MLModelField> fieldNames = new ArrayList<>();
    PMMLManager pmmlManager = new PMMLManager(IOUtil.unmarshal(new ByteArrayInputStream(pmmlContents.getBytes())));
    Evaluator modelEvaluator = (ModelEvaluator<?>) pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());
    modelEvaluator.getPredictedFields().forEach((f) -> fieldNames.add(getModelField(modelEvaluator.getDataField(f))));

    modelEvaluator.getOutputFields().forEach((f) -> {
        OutputField outputField = modelEvaluator.getOutputField(f);
        ResultFeatureType resultFeatureType = outputField.getFeature();
        if (resultFeatureType != ResultFeatureType.PREDICTED_VALUE &&
                resultFeatureType != ResultFeatureType.PREDICTED_DISPLAY_VALUE) {
            fieldNames.add(getModelField(outputField));
        }
    });

    return fieldNames;
}
 
Example #4
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private void processMiningModel(MiningModel miningModel){
	Segmentation segmentation = miningModel.getSegmentation();

	if(segmentation != null){
		Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();

		switch(multipleModelMethod){
			case SELECT_FIRST:
			case SELECT_ALL:
			case MODEL_CHAIN:
				{
					this.targetDataTypes.push(Collections.singletonMap(Evaluator.DEFAULT_TARGET_NAME, null));

					this.dataType = null;

					return;
				}
			default:
				break;
		}
	}

	processModel(miningModel);
}
 
Example #5
Source File: MLModelRegistryService.java    From streamline with Apache License 2.0 6 votes vote down vote up
private List<MLModelField> doGetOutputFieldsForPMMLStream(String pmmlContents) throws SAXException, JAXBException, UnsupportedEncodingException {
    List<MLModelField> fieldNames = new ArrayList<>();
    PMMLManager pmmlManager = new PMMLManager(IOUtil.unmarshal(new ByteArrayInputStream(pmmlContents.getBytes("UTF-8"))));
    Evaluator modelEvaluator = (ModelEvaluator<?>) pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());
    modelEvaluator.getPredictedFields().forEach((f) -> fieldNames.add(getModelField(modelEvaluator.getDataField(f))));

    modelEvaluator.getOutputFields().forEach((f) -> {
        OutputField outputField = modelEvaluator.getOutputField(f);
        ResultFeatureType resultFeatureType = outputField.getFeature();
        if (resultFeatureType != ResultFeatureType.PREDICTED_VALUE &&
                resultFeatureType != ResultFeatureType.PREDICTED_DISPLAY_VALUE) {
            fieldNames.add(getModelField(outputField));
        }
    });

    return fieldNames;
}
 
Example #6
Source File: Model.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
public Model(Evaluator evaluator){
	setEvaluator(evaluator);

	Map<String, Object> properties = new LinkedHashMap<>();
	properties.put(Model.PROPERTY_CREATED_TIMESTAMP, new Date());
	properties.put(Model.PROPERTY_ACCESSED_TIMESTAMP, null);

	setProperties(properties);

	setSchema(ModelUtil.encodeSchema(evaluator));
}
 
Example #7
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Appends all target fields.
 * </p>
 *
 * @see Evaluator#getTargetFields()
 */
public TransformerBuilder withTargetCols(){
	Evaluator evaluator = getEvaluator();

	List<TargetField> targetFields = evaluator.getTargetFields();
	for(TargetField targetField : targetFields){
		this.columnProducers.add(new TargetColumnProducer(targetField, null));
	}

	return this;
}
 
Example #8
Source File: PMMLThreadPool.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private InferenceWorker(int id, @NonNull BlockingQueue inputQueue, boolean rootDevice, @NonNull ModelLoader<Evaluator> modelLoader) {
    this.inputQueue = inputQueue;
    this.pmmlModelLoader = modelLoader;
    this.rootDevice = rootDevice;
    this.setDaemon(true);
    this.setName("InferenceThread-" + id);

}
 
Example #9
Source File: PmmlModelLoader.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public Evaluator loadModel() throws Exception {
    PMML pmml;
    try (InputStream is = new FileInputStream(pmmlFile)) {
        pmml = PMMLUtil.unmarshal(is);
    }


    Evaluator evaluator = modelEvaluatorFactory.newModelEvaluator(pmml);
    return evaluator;
}
 
Example #10
Source File: PmmlInferenceExecutioner.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ModelLoader<Evaluator> model, ParallelInferenceConfig config) {
    this.inference = new PMMLThreadPool.Builder(model)
            .batchLimit(config.getBatchLimit())
            .queueLimit(config.getQueueLimit())
            .inferenceMode(config.getInferenceMode())
            .workers(config.getWorkers())
            .build();
    this.modelLoader = model;


}
 
Example #11
Source File: TargetColumnProducer.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public StructField init(Evaluator evaluator){
	TargetField field = getField();

	DataType dataType = field.getDataType();

	return DataTypes.createStructField(getColumnName(), SchemaUtil.translateDataType(dataType), false);
}
 
Example #12
Source File: EvaluatorUtil.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @see LoadingModelEvaluatorBuilder#load(InputStream)
 * @see LoadingModelEvaluatorBuilder#load(InputStream, String)
 */
@Deprecated
static
public Evaluator createEvaluator(InputStream is) throws SAXException, JAXBException {
	EvaluatorBuilder evaluatorBuilder = new LoadingModelEvaluatorBuilder()
		.load(is);

	Evaluator evaluator = evaluatorBuilder.build();

	// Perform self-testing
	evaluator.verify();

	return evaluator;
}
 
Example #13
Source File: EvaluatorUtil.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @see LoadingModelEvaluatorBuilder#load(File)
 * @see LoadingModelEvaluatorBuilder#load(File, String)
 */
@Deprecated
static
public Evaluator createEvaluator(File file) throws IOException, SAXException, JAXBException {

	try(InputStream is = new FileInputStream(file)){
		return createEvaluator(is);
	}
}
 
Example #14
Source File: MLModelRegistryService.java    From registry with Apache License 2.0 5 votes vote down vote up
private List<MLModelField> doGetInputFieldsFromPMMLStream(String pmmlContents) throws SAXException, JAXBException {
    final List<MLModelField> fieldNames = new ArrayList<>();
    PMMLManager pmmlManager = new PMMLManager(IOUtil.unmarshal(new ByteArrayInputStream(pmmlContents.getBytes())));
    Evaluator modelEvaluator = (ModelEvaluator<?>) pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());
    for (FieldName predictedField : modelEvaluator.getActiveFields()) {
        fieldNames.add(getModelField(modelEvaluator.getDataField(predictedField)));
    }
    return fieldNames;
}
 
Example #15
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Appends all output fields.
 * </p>
 *
 * @see Evaluator#getOutputFields()
 */
public TransformerBuilder withOutputCols(){
	Evaluator evaluator = getEvaluator();

	List<OutputField> outputFields = evaluator.getOutputFields();
	for(OutputField outputField : outputFields){
		this.columnProducers.add(new OutputColumnProducer(outputField, null));
	}

	return this;
}
 
Example #16
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Appends the probability distribution associated with the sole target field of a classification model.
 * </p>
 *
 * @param columnName The name of the probability column.
 * @param labels The ordering of class label elements in the vector.
 */
public TransformerBuilder withProbabilityCol(String columnName, List<String> labels){
	Evaluator evaluator = getEvaluator();

	TargetField targetField = getTargetField(evaluator);

	List<OutputField> probabilityOutputFields = getProbabilityFields(evaluator, targetField);

	List<String> targetCategories = probabilityOutputFields.stream()
		.map(probabilityOutputField -> {
			org.dmg.pmml.OutputField pmmlOutputField = probabilityOutputField.getField();

			String value = pmmlOutputField.getValue();
			if(value == null){
				throw new MissingAttributeException(pmmlOutputField, PMMLAttributes.OUTPUTFIELD_VALUE);
			}

			return value;
		})
		.collect(Collectors.toList());

	if((labels != null) && (labels.size() != targetCategories.size() || !labels.containsAll(targetCategories))){
		throw new IllegalArgumentException("Model has an incompatible set of probability-type output fields (expected " + labels + ", got " + targetCategories + ")");
	}

	this.columnProducers.add(new ProbabilityColumnProducer(targetField, columnName, labels != null ? labels : targetCategories));

	return this;
}
 
Example #17
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private TargetField getTargetField(Evaluator evaluator){
	List<TargetField> targetFields = evaluator.getTargetFields();

	if(targetFields.size() < 1){
		throw new IllegalArgumentException("Model does not have a target field");
	} else

	if(targetFields.size() > 1){
		throw new IllegalArgumentException("Model has multiple target fields (" + targetFields + ")");
	}

	return targetFields.get(0);
}
 
Example #18
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private List<OutputField> getProbabilityFields(Evaluator evaluator, TargetField targetField){
	List<OutputField> outputFields = evaluator.getOutputFields();

	Predicate<OutputField> predicate = new Predicate<OutputField>(){

		@Override
		public boolean test(OutputField outputField){
			org.dmg.pmml.OutputField pmmlOutputField = outputField.getField();

			ResultFeature resultFeature = pmmlOutputField.getResultFeature();
			switch(resultFeature){
				case PROBABILITY:
					FieldName targetFieldName = pmmlOutputField.getTargetField();

					return Objects.equals(targetFieldName, null) || Objects.equals(targetFieldName, targetField.getName());
				default:
					return false;
			}
		}
	};

	List<OutputField> probabilityOutputFields = outputFields.stream()
		.filter(predicate)
		.collect(Collectors.toList());

	if(probabilityOutputFields.size() < 1){
		throw new IllegalArgumentException("Model does not have probability-type output fields");
	}

	return probabilityOutputFields;
}
 
Example #19
Source File: OutputColumnProducer.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public StructField init(Evaluator evaluator){
	OutputField field = getField();

	DataType dataType = field.getDataType();
	if(dataType == null){
		dataType = DataType.STRING;

		this.formatString = true;
	}

	return DataTypes.createStructField(getColumnName(), SchemaUtil.translateDataType(dataType), false);
}
 
Example #20
Source File: MLModelRegistryService.java    From streamline with Apache License 2.0 5 votes vote down vote up
private List<MLModelField> doGetInputFieldsFromPMMLStream(String pmmlContents) throws SAXException, JAXBException, UnsupportedEncodingException {
    final List<MLModelField> fieldNames = new ArrayList<>();
    PMMLManager pmmlManager = new PMMLManager(IOUtil.unmarshal(new ByteArrayInputStream(pmmlContents.getBytes("UTF-8"))));
    Evaluator modelEvaluator = (ModelEvaluator<?>) pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());
    for (FieldName predictedField: modelEvaluator.getActiveFields()) {
        fieldNames.add(getModelField(modelEvaluator.getDataField(predictedField)));
    }
    return fieldNames;
}
 
Example #21
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public FieldName getTargetName(){
	List<TargetField> targetFields = super.getTargetFields();

	if(targetFields.isEmpty()){
		return Evaluator.DEFAULT_TARGET_NAME;
	}

	return super.getTargetName();
}
 
Example #22
Source File: ModelNestingTest.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();

	List<InputField> inputFields = evaluator.getInputFields();
	List<TargetField> targetFields = evaluator.getTargetFields();
	List<OutputField> outputFields = evaluator.getOutputFields();

	assertEquals(1, inputFields.size());
	assertEquals(0, targetFields.size());
	assertEquals(2, outputFields.size());

	try {
		evaluator.getTargetField();

		fail();
	} catch(EvaluationException ee){
		// Ignored
	}

	assertEquals(Evaluator.DEFAULT_TARGET_NAME, evaluator.getTargetName());

	Map<FieldName, ?> arguments = createArguments("input", 2d);

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

	assertEquals(3, results.size());

	assertEquals(2d * 2d, (Double)getTarget(results, Evaluator.DEFAULT_TARGET_NAME), 1e-8d);

	assertNotNull((Double)getOutput(results, "output"));
	assertNull(getOutput(results, "report(output)"));
}
 
Example #23
Source File: TestingExample.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Batch createBatch(Evaluator evaluator, List<? extends Map<FieldName, ?>> input, List<? extends Map<FieldName, ?>> output, Predicate<ResultField> predicate, Equivalence<Object> equivalence){
	Batch batch = new Batch(){

		@Override
		public Evaluator getEvaluator(){
			return evaluator;
		}

		@Override
		public List<? extends Map<FieldName, ?>> getInput(){
			return input;
		}

		@Override
		public List<? extends Map<FieldName, ?>> getOutput(){
			return output;
		}

		@Override
		public Predicate<ResultField> getPredicate(){
			return predicate;
		}

		@Override
		public Equivalence<Object> getEquivalence(){
			return equivalence;
		}

		@Override
		public void close(){
		}
	};

	return batch;
}
 
Example #24
Source File: ModelUtil.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Map<String, List<Field>> encodeSchema(Evaluator evaluator){
	Map<String, List<Field>> result = new LinkedHashMap<>();

	if(evaluator instanceof HasInputFields){
		HasInputFields hasInputFields = (HasInputFields)evaluator;

		List<InputField> inputFields = hasInputFields.getInputFields();
		if(!inputFields.isEmpty()){
			result.put("inputFields", encodeModelFields(inputFields));
		}
	} // End if

	if(evaluator instanceof HasGroupFields){
		HasGroupFields hasGroupFields = (HasGroupFields)evaluator;

		List<InputField> groupFields = hasGroupFields.getGroupFields();
		if(!groupFields.isEmpty()){
			result.put("groupFields", encodeModelFields(groupFields));
		}
	} // End if

	if(evaluator instanceof HasResultFields){
		HasResultFields hasResultFields = (HasResultFields)evaluator;

		List<TargetField> targetFields = hasResultFields.getTargetFields();
		if(!targetFields.isEmpty()){
			result.put("targetFields", encodeModelFields(targetFields));
		}

		List<OutputField> outputFields = hasResultFields.getOutputFields();
		if(!outputFields.isEmpty()){
			result.put("outputFields", encodeModelFields(outputFields));
		}
	}

	return result;
}
 
Example #25
Source File: PmmlInferenceExecutioner.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public Evaluator model() {
    try {
        return modelLoader.loadModel();
    } catch (Exception e) {
        log.error("Unable to load model in model() call for pmml inference executioner", e);
        return null;
    }
}
 
Example #26
Source File: FieldScopeTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluate() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("input", null);

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

	assertEquals(1000d, getTarget(results, "prediction"));

	arguments = createArguments("input", 1d);

	results = evaluator.evaluate(arguments);

	assertEquals(1d, getTarget(results, "prediction"));
}
 
Example #27
Source File: Model.java    From openscoring with GNU Affero General Public License v3.0 4 votes vote down vote up
public String getSummary(){
	Evaluator evaluator = getEvaluator();

	return evaluator.getSummary();
}
 
Example #28
Source File: PMMLThreadPool.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public Builder(@NonNull ModelLoader<Evaluator> pmmlModelLoader) {
    this.pmmlModelLoader = pmmlModelLoader;
}
 
Example #29
Source File: SelectAllTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluate() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("sepal_length", 5.1d, "sepal_width", 3.5d, "petal_length", 1.4d, "petal_width", 0.2d);

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

	assertEquals(1, results.size());

	Collection<?> species = (Collection<?>)results.get(FieldName.create("species"));

	assertEquals(5, species.size());

	for(Object value : species){
		assertTrue((value instanceof Computable) & (value instanceof HasEntityId));
	}

	assertEquals(Arrays.asList("setosa", "setosa", "setosa", "setosa", "versicolor"), EvaluatorUtil.decode(species));
}
 
Example #30
Source File: FilterBatch.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Evaluator getEvaluator() throws Exception {
	Batch batch = getBatch();

	return batch.getEvaluator();
}