@Test public void getScoreWithNoFeaturesReturns0() throws ComponentLookupException { Mockito.doReturn(Collections.emptySet()).when(this.patient).getFeatures(); double score = this.mocker.getComponentUnderTest().getScore(this.patient); Assert.assertEquals(0.0, score, 0.0); }
@Override public double getScore(Patient patient) { String key = getCacheKey(patient); PatientSpecificity specificity = this.cache.get(key); if (specificity != null) { return specificity.getScore(); } if (patient.getFeatures().isEmpty()) { this.cache.set(key, new PatientSpecificity(0, now(), SCORER_NAME)); return 0; } CloseableHttpResponse response = null; try { JSONObject data = new JSONObject(); JSONArray features = new JSONArray(); for (Feature f : patient.getFeatures()) { if (StringUtils.isNotEmpty(f.getId())) { JSONObject featureObj = new JSONObject(Collections.singletonMap("id", f.getId())); if (!f.isPresent()) { featureObj.put("isPresent", false); } features.put(featureObj); } } data.put("features", features); HttpPost method = new HttpPost(this.scorerURL); method.setEntity(new StringEntity("annotation_profile=" + URLEncoder.encode(data.toString(), "UTF-8"), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))); RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(1000).build(); method.setConfig(config); response = this.client.execute(method); JSONObject score = new JSONObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); specificity = new PatientSpecificity(score.getDouble("scaled_score"), now(), SCORER_NAME); this.cache.set(key, specificity); return specificity.getScore(); } catch (Exception ex) { this.logger.error("Failed to compute specificity score for patient [{}] using the monarch server [{}]: {}", patient.getDocumentReference(), this.scorerURL, ex.getMessage()); } finally { if (response != null) { try { EntityUtils.consumeQuietly(response.getEntity()); response.close(); } catch (IOException ex) { } } } return -1; }
MonarchPatientScorer implements PatientScorer, Initializable { @Override public double getScore(Patient patient) { String key = getCacheKey(patient); PatientSpecificity specificity = this.cache.get(key); if (specificity != null) { return specificity.getScore(); } if (patient.getFeatures().isEmpty()) { this.cache.set(key, new PatientSpecificity(0, now(), SCORER_NAME)); return 0; } CloseableHttpResponse response = null; try { JSONObject data = new JSONObject(); JSONArray features = new JSONArray(); for (Feature f : patient.getFeatures()) { if (StringUtils.isNotEmpty(f.getId())) { JSONObject featureObj = new JSONObject(Collections.singletonMap("id", f.getId())); if (!f.isPresent()) { featureObj.put("isPresent", false); } features.put(featureObj); } } data.put("features", features); HttpPost method = new HttpPost(this.scorerURL); method.setEntity(new StringEntity("annotation_profile=" + URLEncoder.encode(data.toString(), "UTF-8"), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))); RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(1000).build(); method.setConfig(config); response = this.client.execute(method); JSONObject score = new JSONObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); specificity = new PatientSpecificity(score.getDouble("scaled_score"), now(), SCORER_NAME); this.cache.set(key, specificity); return specificity.getScore(); } catch (Exception ex) { this.logger.error("Failed to compute specificity score for patient [{}] using the monarch server [{}]: {}", patient.getDocumentReference(), this.scorerURL, ex.getMessage()); } finally { if (response != null) { try { EntityUtils.consumeQuietly(response.getEntity()); response.close(); } catch (IOException ex) { } } } return -1; } }
MonarchPatientScorer implements PatientScorer, Initializable { @Override public double getScore(Patient patient) { String key = getCacheKey(patient); PatientSpecificity specificity = this.cache.get(key); if (specificity != null) { return specificity.getScore(); } if (patient.getFeatures().isEmpty()) { this.cache.set(key, new PatientSpecificity(0, now(), SCORER_NAME)); return 0; } CloseableHttpResponse response = null; try { JSONObject data = new JSONObject(); JSONArray features = new JSONArray(); for (Feature f : patient.getFeatures()) { if (StringUtils.isNotEmpty(f.getId())) { JSONObject featureObj = new JSONObject(Collections.singletonMap("id", f.getId())); if (!f.isPresent()) { featureObj.put("isPresent", false); } features.put(featureObj); } } data.put("features", features); HttpPost method = new HttpPost(this.scorerURL); method.setEntity(new StringEntity("annotation_profile=" + URLEncoder.encode(data.toString(), "UTF-8"), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))); RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(1000).build(); method.setConfig(config); response = this.client.execute(method); JSONObject score = new JSONObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); specificity = new PatientSpecificity(score.getDouble("scaled_score"), now(), SCORER_NAME); this.cache.set(key, specificity); return specificity.getScore(); } catch (Exception ex) { this.logger.error("Failed to compute specificity score for patient [{}] using the monarch server [{}]: {}", patient.getDocumentReference(), this.scorerURL, ex.getMessage()); } finally { if (response != null) { try { EntityUtils.consumeQuietly(response.getEntity()); response.close(); } catch (IOException ex) { } } } return -1; }  }
MonarchPatientScorer implements PatientScorer, Initializable { @Override public double getScore(Patient patient) { String key = getCacheKey(patient); PatientSpecificity specificity = this.cache.get(key); if (specificity != null) { return specificity.getScore(); } if (patient.getFeatures().isEmpty()) { this.cache.set(key, new PatientSpecificity(0, now(), SCORER_NAME)); return 0; } CloseableHttpResponse response = null; try { JSONObject data = new JSONObject(); JSONArray features = new JSONArray(); for (Feature f : patient.getFeatures()) { if (StringUtils.isNotEmpty(f.getId())) { JSONObject featureObj = new JSONObject(Collections.singletonMap("id", f.getId())); if (!f.isPresent()) { featureObj.put("isPresent", false); } features.put(featureObj); } } data.put("features", features); HttpPost method = new HttpPost(this.scorerURL); method.setEntity(new StringEntity("annotation_profile=" + URLEncoder.encode(data.toString(), "UTF-8"), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))); RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(1000).build(); method.setConfig(config); response = this.client.execute(method); JSONObject score = new JSONObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); specificity = new PatientSpecificity(score.getDouble("scaled_score"), now(), SCORER_NAME); this.cache.set(key, specificity); return specificity.getScore(); } catch (Exception ex) { this.logger.error("Failed to compute specificity score for patient [{}] using the monarch server [{}]: {}", patient.getDocumentReference(), this.scorerURL, ex.getMessage()); } finally { if (response != null) { try { EntityUtils.consumeQuietly(response.getEntity()); response.close(); } catch (IOException ex) { } } } return -1; }  @Override void initialize(); @Override PatientSpecificity getSpecificity(Patient patient); @Override double getScore(Patient patient); }
MonarchPatientScorer implements PatientScorer, Initializable { @Override public double getScore(Patient patient) { String key = getCacheKey(patient); PatientSpecificity specificity = this.cache.get(key); if (specificity != null) { return specificity.getScore(); } if (patient.getFeatures().isEmpty()) { this.cache.set(key, new PatientSpecificity(0, now(), SCORER_NAME)); return 0; } CloseableHttpResponse response = null; try { JSONObject data = new JSONObject(); JSONArray features = new JSONArray(); for (Feature f : patient.getFeatures()) { if (StringUtils.isNotEmpty(f.getId())) { JSONObject featureObj = new JSONObject(Collections.singletonMap("id", f.getId())); if (!f.isPresent()) { featureObj.put("isPresent", false); } features.put(featureObj); } } data.put("features", features); HttpPost method = new HttpPost(this.scorerURL); method.setEntity(new StringEntity("annotation_profile=" + URLEncoder.encode(data.toString(), "UTF-8"), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))); RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(1000).build(); method.setConfig(config); response = this.client.execute(method); JSONObject score = new JSONObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); specificity = new PatientSpecificity(score.getDouble("scaled_score"), now(), SCORER_NAME); this.cache.set(key, specificity); return specificity.getScore(); } catch (Exception ex) { this.logger.error("Failed to compute specificity score for patient [{}] using the monarch server [{}]: {}", patient.getDocumentReference(), this.scorerURL, ex.getMessage()); } finally { if (response != null) { try { EntityUtils.consumeQuietly(response.getEntity()); response.close(); } catch (IOException ex) { } } } return -1; }  @Override void initialize(); @Override PatientSpecificity getSpecificity(Patient patient); @Override double getScore(Patient patient);  }