use of org.openscoring.common.EvaluationRequest 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;
}
use of org.openscoring.common.EvaluationRequest 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;
}
use of org.openscoring.common.EvaluationRequest 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);
}
use of org.openscoring.common.EvaluationRequest in project openscoring by openscoring.
the class ModelResourceTest method invalidate.
private static EvaluationRequest invalidate(EvaluationRequest record) {
Maps.EntryTransformer<String, Object, String> transformer = new Maps.EntryTransformer<String, Object, String>() {
@Override
public String transformEntry(String key, Object value) {
StringBuilder sb = new StringBuilder(key);
sb.reverse();
return sb.toString();
}
};
EvaluationRequest invalidRecord = new EvaluationRequest(record.getId());
invalidRecord.setArguments(Maps.transformEntries(record.getArguments(), transformer));
return invalidRecord;
}
use of org.openscoring.common.EvaluationRequest 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);
}
Aggregations