use of org.hl7.fhir.r5.model.Measure in project quality-measure-and-cohort-service by Alvearie.
the class MeasureEvaluation method evaluatePatientMeasure.
public MeasureReport evaluatePatientMeasure(Measure measure, Context context, String patientId, boolean includeEvaluatedResources) {
logger.info("Generating individual report");
if (patientId == null) {
throw new IllegalArgumentException("Must provide patient id");
}
Iterable<Object> patientRetrieve = provider.retrieve(PATIENT, "id", patientId, PATIENT, null, null, null, null, null, null, null, null);
Patient patient = null;
if (patientRetrieve.iterator().hasNext()) {
patient = (Patient) patientRetrieve.iterator().next();
}
boolean isSingle = true;
return evaluate(measure, context, patient == null ? Collections.emptyList() : Collections.singletonList(patient), MeasureReport.MeasureReportType.INDIVIDUAL, isSingle, includeEvaluatedResources);
}
use of org.hl7.fhir.r5.model.Measure in project quality-measure-and-cohort-service by Alvearie.
the class MeasureEvaluation method evaluate.
protected MeasureReport evaluate(Measure measure, Context context, List<Patient> patients, MeasureReport.MeasureReportType type, boolean isSingle, boolean includeEvaluatedResources) {
MeasureReportBuilder reportBuilder = new MeasureReportBuilder();
reportBuilder.buildStatus("complete");
reportBuilder.buildType(type);
reportBuilder.buildMeasureReference(measure.getIdElement().getResourceType() + "/" + measure.getIdElement().getIdPart());
if (type == MeasureReport.MeasureReportType.INDIVIDUAL && CollectionUtils.isNotEmpty(patients)) {
IdType patientId = patients.get(0).getIdElement();
reportBuilder.buildPatientReference(patientId.getResourceType() + "/" + patientId.getIdPart());
}
reportBuilder.buildPeriod(measurementPeriod);
MeasureReport report = reportBuilder.build();
Map<String, Resource> resources = new HashMap<>();
Map<String, Set<String>> codeToResourceMap = new HashMap<>();
MeasureScoring measureScoring = MeasureScoring.fromCode(measure.getScoring().getCodingFirstRep().getCode());
if (measureScoring == null) {
throw new RuntimeException("Measure scoring is required in order to calculate.");
}
List<Measure.MeasureSupplementalDataComponent> sde = measure.getSupplementalData();
Map<String, Map<String, Integer>> sdeAccumulators = new HashMap<>();
for (Measure.MeasureGroupComponent group : measure.getGroup()) {
MeasureReport.MeasureReportGroupComponent reportGroup = new MeasureReport.MeasureReportGroupComponent();
reportGroup.setId(group.getId());
report.getGroup().add(reportGroup);
// Declare variables to avoid a hash lookup on every patient
// TODO: Isn't quite right, there may be multiple initial populations for a
// ratio measure...
Measure.MeasureGroupPopulationComponent initialPopulationCriteria = null;
Measure.MeasureGroupPopulationComponent numeratorCriteria = null;
Measure.MeasureGroupPopulationComponent numeratorExclusionCriteria = null;
Measure.MeasureGroupPopulationComponent denominatorCriteria = null;
Measure.MeasureGroupPopulationComponent denominatorExclusionCriteria = null;
Measure.MeasureGroupPopulationComponent denominatorExceptionCriteria = null;
Map<String, Resource> initialPopulation = null;
Map<String, Resource> numerator = null;
Map<String, Resource> numeratorExclusion = null;
Map<String, Resource> denominator = null;
Map<String, Resource> denominatorExclusion = null;
Map<String, Resource> denominatorException = null;
Map<String, Patient> initialPopulationPatients = null;
Map<String, Patient> numeratorPatients = null;
Map<String, Patient> numeratorExclusionPatients = null;
Map<String, Patient> denominatorPatients = null;
Map<String, Patient> denominatorExclusionPatients = null;
Map<String, Patient> denominatorExceptionPatients = null;
for (Measure.MeasureGroupPopulationComponent pop : group.getPopulation()) {
MeasurePopulationType populationType = MeasurePopulationType.fromCode(pop.getCode().getCodingFirstRep().getCode());
if (populationType != null) {
switch(populationType) {
case INITIALPOPULATION:
initialPopulationCriteria = pop;
initialPopulation = new HashMap<>();
if (type == MeasureReport.MeasureReportType.SUBJECTLIST) {
initialPopulationPatients = new HashMap<>();
}
break;
case NUMERATOR:
numeratorCriteria = pop;
numerator = new HashMap<>();
if (type == MeasureReport.MeasureReportType.SUBJECTLIST) {
numeratorPatients = new HashMap<>();
}
break;
case NUMERATOREXCLUSION:
numeratorExclusionCriteria = pop;
numeratorExclusion = new HashMap<>();
if (type == MeasureReport.MeasureReportType.SUBJECTLIST) {
numeratorExclusionPatients = new HashMap<>();
}
break;
case DENOMINATOR:
denominatorCriteria = pop;
denominator = new HashMap<>();
if (type == MeasureReport.MeasureReportType.SUBJECTLIST) {
denominatorPatients = new HashMap<>();
}
break;
case DENOMINATOREXCLUSION:
denominatorExclusionCriteria = pop;
denominatorExclusion = new HashMap<>();
if (type == MeasureReport.MeasureReportType.SUBJECTLIST) {
denominatorExclusionPatients = new HashMap<>();
}
break;
case DENOMINATOREXCEPTION:
denominatorExceptionCriteria = pop;
denominatorException = new HashMap<>();
if (type == MeasureReport.MeasureReportType.SUBJECTLIST) {
denominatorExceptionPatients = new HashMap<>();
}
break;
default:
throw new UnsupportedOperationException("Measure population, observation and measure population exclusion are used for continuous-variable scoring measures which are not supported");
}
}
}
switch(measureScoring) {
case PROPORTION:
case RATIO:
{
// For each patient in the initial population
for (Patient patient : patients) {
// Are they in the initial population?
boolean inInitialPopulation = evaluatePopulationCriteria(context, patient, initialPopulationCriteria, initialPopulation, initialPopulationPatients, null, null, null);
populateResourceMap(context, MeasurePopulationType.INITIALPOPULATION, resources, codeToResourceMap, includeEvaluatedResources);
if (inInitialPopulation) {
// Are they in the denominator?
boolean inDenominator = evaluatePopulationCriteria(context, patient, denominatorCriteria, denominator, denominatorPatients, denominatorExclusionCriteria, denominatorExclusion, denominatorExclusionPatients);
populateResourceMap(context, MeasurePopulationType.DENOMINATOR, resources, codeToResourceMap, includeEvaluatedResources);
if (inDenominator) {
// Are they in the numerator?
boolean inNumerator = evaluatePopulationCriteria(context, patient, numeratorCriteria, numerator, numeratorPatients, numeratorExclusionCriteria, numeratorExclusion, numeratorExclusionPatients);
populateResourceMap(context, MeasurePopulationType.NUMERATOR, resources, codeToResourceMap, includeEvaluatedResources);
if (!inNumerator && inDenominator && (denominatorExceptionCriteria != null)) {
// Are they in the denominator exception?
boolean inException = false;
for (Resource resource : evaluateCriteria(context, patient, denominatorExceptionCriteria)) {
inException = true;
denominatorException.put(resource.getIdElement().getIdPart(), resource);
denominator.remove(resource.getIdElement().getIdPart());
populateResourceMap(context, MeasurePopulationType.DENOMINATOREXCEPTION, resources, codeToResourceMap, includeEvaluatedResources);
}
if (inException) {
if (denominatorExceptionPatients != null) {
denominatorExceptionPatients.put(patient.getIdElement().getIdPart(), patient);
}
if (denominatorPatients != null) {
denominatorPatients.remove(patient.getIdElement().getIdPart());
}
}
}
}
}
MeasureSupplementalDataEvaluation.populateSDEAccumulators(context, patient, sdeAccumulators, sde);
}
// Calculate actual measure score, Count(numerator) / Count(denominator)
if (numerator != null && MapUtils.isNotEmpty(denominator)) {
reportGroup.setMeasureScore(new Quantity(numerator.size() / (double) denominator.size()));
}
break;
}
case COHORT:
{
// For each patient in the patient list
for (Patient patient : patients) {
evaluatePopulationCriteria(context, patient, initialPopulationCriteria, initialPopulation, initialPopulationPatients, null, null, null);
populateResourceMap(context, MeasurePopulationType.INITIALPOPULATION, resources, codeToResourceMap, includeEvaluatedResources);
MeasureSupplementalDataEvaluation.populateSDEAccumulators(context, patient, sdeAccumulators, sde);
}
break;
}
case CONTINUOUSVARIABLE:
throw new UnsupportedOperationException("Scoring type CONTINUOUSVARIABLE is not supported");
}
// Add population reports for each group
addPopulationCriteriaReport(report, reportGroup, initialPopulationCriteria, initialPopulation != null ? initialPopulation.size() : 0, initialPopulationPatients != null ? initialPopulationPatients.values() : null);
addPopulationCriteriaReport(report, reportGroup, numeratorCriteria, numerator != null ? numerator.size() : 0, numeratorPatients != null ? numeratorPatients.values() : null);
addPopulationCriteriaReport(report, reportGroup, numeratorExclusionCriteria, numeratorExclusion != null ? numeratorExclusion.size() : 0, numeratorExclusionPatients != null ? numeratorExclusionPatients.values() : null);
addPopulationCriteriaReport(report, reportGroup, denominatorCriteria, denominator != null ? denominator.size() : 0, denominatorPatients != null ? denominatorPatients.values() : null);
addPopulationCriteriaReport(report, reportGroup, denominatorExclusionCriteria, denominatorExclusion != null ? denominatorExclusion.size() : 0, denominatorExclusionPatients != null ? denominatorExclusionPatients.values() : null);
addPopulationCriteriaReport(report, reportGroup, denominatorExceptionCriteria, denominatorException != null ? denominatorException.size() : 0, denominatorExceptionPatients != null ? denominatorExceptionPatients.values() : null);
}
for (Entry<String, Set<String>> entry : codeToResourceMap.entrySet()) {
ListResource list = new ListResource();
for (String element : entry.getValue()) {
ListResource.ListEntryComponent comp = new ListEntryComponent();
comp.setItem(new Reference('#' + element));
list.addEntry(comp);
}
if (!list.isEmpty()) {
list.setId(UUID.randomUUID().toString());
list.setTitle(entry.getKey());
resources.put(list.getId(), list);
}
}
if (MapUtils.isNotEmpty(resources)) {
List<Reference> evaluatedResourceIds = new ArrayList<>();
resources.forEach((key, resource) -> {
evaluatedResourceIds.add(new Reference(resource.getId()));
});
report.setEvaluatedResource(evaluatedResourceIds);
}
if (MapUtils.isNotEmpty(sdeAccumulators)) {
report = MeasureSupplementalDataEvaluation.processAccumulators(report, sdeAccumulators, isSingle, patients);
}
return report;
}
use of org.hl7.fhir.r5.model.Measure in project quality-measure-and-cohort-service by Alvearie.
the class MeasureSupplementalDataEvaluation method processAccumulators.
public static MeasureReport processAccumulators(MeasureReport report, Map<String, Map<String, Integer>> sdeAccumulators, boolean isSingle, List<Patient> patients) {
List<Reference> newRefList = new ArrayList<>();
sdeAccumulators.forEach((sdeKey, sdeAccumulator) -> {
sdeAccumulator.forEach((sdeAccumulatorKey, sdeAccumulatorValue) -> {
Observation obs = new Observation();
obs.setStatus(Observation.ObservationStatus.FINAL);
obs.setId(UUID.randomUUID().toString());
Coding valueCoding = new Coding();
if (sdeKey.equalsIgnoreCase(SDE_SEX)) {
valueCoding.setCode(sdeAccumulatorKey);
} else {
String coreCategory = sdeKey.substring(sdeKey.lastIndexOf('-'));
patients.forEach((pt) -> {
pt.getExtension().forEach((ptExt) -> {
if (ptExt.getUrl().contains(coreCategory)) {
String code = ((Coding) ptExt.getExtension().get(0).getValue()).getCode();
if (code.equalsIgnoreCase(sdeAccumulatorKey)) {
valueCoding.setSystem(((Coding) ptExt.getExtension().get(0).getValue()).getSystem());
valueCoding.setCode(code);
valueCoding.setDisplay(((Coding) ptExt.getExtension().get(0).getValue()).getDisplay());
}
}
});
});
}
CodeableConcept obsCodeableConcept = new CodeableConcept();
Extension obsExtension = new Extension().setUrl(CQF_MEASUREINFO_URL);
Extension extExtMeasure = new Extension().setUrl(MEASURE).setValue(new CanonicalType(CQFMEASURES_URL + report.getMeasure()));
obsExtension.addExtension(extExtMeasure);
Extension extExtPop = new Extension().setUrl(POPULATION_ID).setValue(new StringType(sdeKey));
obsExtension.addExtension(extExtPop);
obs.addExtension(obsExtension);
obs.setValue(new IntegerType(sdeAccumulatorValue));
if (!isSingle) {
valueCoding.setCode(sdeAccumulatorKey);
obsCodeableConcept.setCoding(Collections.singletonList(valueCoding));
obs.setCode(obsCodeableConcept);
} else {
obs.setCode(new CodeableConcept().setText(sdeKey));
obsCodeableConcept.setCoding(Collections.singletonList(valueCoding));
obs.setValue(obsCodeableConcept);
}
newRefList.add(new Reference("#" + obs.getId()));
report.addContained(obs);
});
});
newRefList.addAll(report.getEvaluatedResource());
report.setEvaluatedResource(newRefList);
return report;
}
use of org.hl7.fhir.r5.model.Measure in project quality-measure-and-cohort-service by Alvearie.
the class DefaultVT method testMeasureEvaluationByMeasureIdentifier.
// to tag a specific test to be part of DVT (deployment verification test)
@Category(DVT.class)
@Test
public /**
* Test a successful measure evaluation using identifier and version as the lookup key
*/
void testMeasureEvaluationByMeasureIdentifier() throws Exception {
// You want -Denabled.dark.features=all in your Liberty jvm.options
Assume.assumeTrue(isServiceDarkFeatureEnabled(CohortEngineRestConstants.DARK_LAUNCHED_MEASURE_EVALUATION));
final String RESOURCE = getUrlBase() + CohortServiceAPISpec.CREATE_DELETE_EVALUATION_PATH;
FhirContext fhirContext = FhirContext.forR4();
IParser parser = fhirContext.newJsonParser().setPrettyPrint(true);
Library library = TestHelper.getTemplateLibrary();
Identifier identifier = new Identifier().setValue("measure-identifier").setSystem("http://ibm.com/health/test");
Measure measure = TestHelper.getTemplateMeasure(library);
measure.setIdentifier(Arrays.asList(identifier));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TestHelper.createMeasureArtifact(baos, parser, measure, library);
// Files.write( baos.toByteArray(), new File("target/test_measure_v1_0_0.zip"));
Map<String, Parameter> parameterOverrides = new HashMap<>();
parameterOverrides.put("Measurement Period", new IntervalParameter(new DateParameter("2019-07-04"), true, new DateParameter("2020-07-04"), true));
MeasureEvaluation requestData = new MeasureEvaluation();
requestData.setDataServerConfig(dataServerConfig);
requestData.setTerminologyServerConfig(termServerConfig);
// This is a patient ID that is assumed to exist in the target FHIR server
requestData.setPatientId(VALID_PATIENT_ID);
requestData.setMeasureContext(new MeasureContext(null, parameterOverrides, new com.ibm.cohort.engine.measure.Identifier(identifier.getSystem(), identifier.getValue()), measure.getVersion()));
requestData.setEvidenceOptions(new MeasureEvidenceOptions(false, MeasureEvidenceOptions.DefineReturnOptions.NONE));
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(requestData));
RequestSpecification request = buildBaseRequest(new Headers()).queryParam(CohortEngineRestHandler.VERSION, ServiceBuildConstants.DATE).multiPart(CohortEngineRestHandler.REQUEST_DATA_PART, requestData, "application/json").multiPart(CohortEngineRestHandler.MEASURE_PART, "test_measure_v1_0_0.zip", new ByteArrayInputStream(baos.toByteArray()));
ValidatableResponse response = request.post(RESOURCE, getServiceVersion()).then();
ValidatableResponse vr = runSuccessValidation(response, ContentType.JSON, HttpStatus.SC_OK);
String expected = getJsonFromFile(ServiceAPIGlobalSpec.EXP_FOLDER_TYPE, "measure_evaluation_exp.json");
String actual = vr.extract().asString();
assertMeasureReportEquals(parser, expected, actual, false);
}
use of org.hl7.fhir.r5.model.Measure in project quality-measure-and-cohort-service by Alvearie.
the class CDMMeasureEvaluation method getParameterExtensions.
protected static List<Extension> getParameterExtensions(Measure measure, Context context, Map<String, Parameter> parameterMap) {
Set<String> parameterNames = new HashSet<>();
// Check for special parameters we handle elsewhere
if (context.resolveParameterRef(null, CDMConstants.MEASUREMENT_PERIOD) != null) {
parameterNames.add(CDMConstants.MEASUREMENT_PERIOD);
}
if (context.resolveParameterRef(null, CDMConstants.PRODUCT_LINE) != null) {
parameterNames.add(CDMConstants.PRODUCT_LINE);
}
if (parameterMap != null) {
parameterNames.addAll(parameterMap.keySet());
}
List<Extension> parameterExtensions = measure.getExtensionsByUrl(CDMConstants.MEASURE_PARAMETER_URL);
for (Extension e : parameterExtensions) {
ParameterDefinition parameterDefinition = (ParameterDefinition) e.getValue();
parameterNames.add(parameterDefinition.getName());
}
return parameterNames.stream().map(x -> createParameterExtension(context, x)).filter(Objects::nonNull).collect(Collectors.toList());
}
Aggregations