Search in sources :

Example 6 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class ModelResource method evaluateBatch.

@POST
@Path("{id: " + ModelRegistry.ID_REGEX + "}/batch")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public BatchEvaluationResponse evaluateBatch(@PathParam("id") String id, BatchEvaluationRequest request) {
    BatchEvaluationResponse batchResponse = new BatchEvaluationResponse(request.getId());
    List<EvaluationRequest> requests = request.getRequests();
    List<EvaluationResponse> responses = doEvaluate(id, requests, false, "evaluate.batch");
    batchResponse.setResponses(responses);
    return batchResponse;
}
Also used : BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 7 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class ModelResource method doEvaluate.

@SuppressWarnings(value = "resource")
private List<EvaluationResponse> doEvaluate(String id, List<EvaluationRequest> requests, boolean allOrNothing, String method) {
    Model model = this.modelRegistry.get(id, true);
    if (model == null) {
        throw new NotFoundException("Model \"" + id + "\" not found");
    }
    List<EvaluationResponse> responses = new ArrayList<>();
    Timer timer = this.metricRegistry.timer(createName(id, method));
    Timer.Context context = timer.time();
    try {
        Evaluator evaluator = model.getEvaluator();
        if (evaluator instanceof HasGroupFields) {
            HasGroupFields hasGroupFields = (HasGroupFields) evaluator;
            List<InputField> groupFields = hasGroupFields.getGroupFields();
            if (groupFields.size() == 1) {
                InputField groupField = groupFields.get(0);
                requests = aggregateRequests(groupField.getName(), requests);
            } else if (groupFields.size() > 1) {
                throw new EvaluationException("Too many group fields");
            }
        }
        for (EvaluationRequest request : requests) {
            EvaluationResponse response;
            try {
                response = evaluate(evaluator, request);
            } catch (Exception e) {
                if (allOrNothing) {
                    throw e;
                }
                response = new EvaluationResponse(request.getId());
                response.setMessage(e.toString());
            }
            responses.add(response);
        }
    } catch (Exception e) {
        logger.error("Failed to evaluate", e);
        throw new BadRequestException(e);
    }
    context.stop();
    Counter counter = this.metricRegistry.counter(createName(id, "records"));
    counter.inc(responses.size());
    return responses;
}
Also used : InputField(org.jpmml.evaluator.InputField) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) EvaluationException(org.jpmml.evaluator.EvaluationException) Evaluator(org.jpmml.evaluator.Evaluator) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JAXBException(javax.xml.bind.JAXBException) NotFoundException(javax.ws.rs.NotFoundException) IOException(java.io.IOException) EvaluationException(org.jpmml.evaluator.EvaluationException) HasGroupFields(org.jpmml.evaluator.HasGroupFields) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) BadRequestException(javax.ws.rs.BadRequestException)

Example 8 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class ModelResourceTest method evaluate.

private EvaluationResponse evaluate(String id, EvaluationRequest request) {
    Entity<EvaluationRequest> entity = Entity.json(request);
    Response response = target("model/" + id).request(MediaType.APPLICATION_JSON).post(entity);
    assertEquals(200, response.getStatus());
    return response.readEntity(EvaluationResponse.class);
}
Also used : EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) BatchModelResponse(org.openscoring.common.BatchModelResponse) ModelResponse(org.openscoring.common.ModelResponse) SimpleResponse(org.openscoring.common.SimpleResponse) Response(javax.ws.rs.core.Response) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) EvaluationRequest(org.openscoring.common.EvaluationRequest)

Example 9 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class ModelResourceTest method linearRegressionAuto.

@Test
public void linearRegressionAuto() throws Exception {
    String id = "LinearRegressionAuto";
    assertEquals("Auto", extractSuffix(id));
    deploy(id);
    List<EvaluationRequest> evaluationRequests = loadRecords(id);
    EvaluationRequest evaluationRequest = evaluationRequests.get(0);
    EvaluationResponse evaluationResponse = evaluate(id, evaluationRequest);
    assertEquals(evaluationRequest.getId(), evaluationResponse.getId());
    Map<String, ?> result = evaluationResponse.getResult();
    assertEquals(3, result.size());
    String report = (String) result.get("report(Predicted_mpg)");
    assertTrue(report.startsWith("<math xmlns=\"http://www.w3.org/1998/Math/MathML\">") && report.endsWith("</math>"));
    undeploy(id);
}
Also used : BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) EvaluationRequest(org.openscoring.common.EvaluationRequest) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 10 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class ModelResourceTest method decisionTreeIris.

@Test
public void decisionTreeIris() throws Exception {
    String id = "DecisionTreeIris";
    assertEquals("Iris", extractSuffix(id));
    BatchModelResponse batchModelResponse = queryBatch();
    List<ModelResponse> modelResponses = batchModelResponse.getResponses();
    assertNull(modelResponses);
    deploy(id);
    batchModelResponse = queryBatch();
    modelResponses = batchModelResponse.getResponses();
    assertEquals(1, modelResponses.size());
    download(id);
    List<EvaluationRequest> evaluationRequests = loadRecords(id);
    EvaluationRequest evaluationRequest = evaluationRequests.get(0);
    EvaluationResponse evaluationResponse = evaluate(id, evaluationRequest);
    assertEquals(evaluationRequest.getId(), evaluationResponse.getId());
    EvaluationRequest invalidEvaluationRequest = invalidate(evaluationRequests.get(50));
    evaluationRequests = Arrays.asList(evaluationRequests.get(0), invalidEvaluationRequest, evaluationRequests.get(100));
    BatchEvaluationRequest batchEvaluationRequest = new BatchEvaluationRequest();
    batchEvaluationRequest.setRequests(evaluationRequests);
    BatchEvaluationResponse batchEvaluationResponse = evaluateBatch(id, batchEvaluationRequest);
    assertEquals(batchEvaluationRequest.getId(), batchEvaluationResponse.getId());
    List<EvaluationResponse> evaluationResponses = batchEvaluationResponse.getResponses();
    assertEquals(evaluationRequests.size(), evaluationResponses.size());
    EvaluationResponse invalidEvaluationResponse = evaluationResponses.get(1);
    assertEquals(invalidEvaluationRequest.getId(), invalidEvaluationResponse.getId());
    assertNotNull(invalidEvaluationResponse.getMessage());
    undeploy(id);
}
Also used : BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchModelResponse(org.openscoring.common.BatchModelResponse) BatchModelResponse(org.openscoring.common.BatchModelResponse) ModelResponse(org.openscoring.common.ModelResponse) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

EvaluationResponse (org.openscoring.common.EvaluationResponse)10 BatchEvaluationResponse (org.openscoring.common.BatchEvaluationResponse)7 EvaluationRequest (org.openscoring.common.EvaluationRequest)7 BatchEvaluationRequest (org.openscoring.common.BatchEvaluationRequest)6 IOException (java.io.IOException)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2 Response (javax.ws.rs.core.Response)2 JAXBException (javax.xml.bind.JAXBException)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 EvaluationException (org.jpmml.evaluator.EvaluationException)2 InputField (org.jpmml.evaluator.InputField)2 Test (org.junit.Test)2 BatchModelResponse (org.openscoring.common.BatchModelResponse)2 ModelResponse (org.openscoring.common.ModelResponse)2 Counter (com.codahale.metrics.Counter)1 Timer (com.codahale.metrics.Timer)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1