Search in sources :

Example 66 with Element

use of org.hl7.fhir.r4b.elementmodel.Element 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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MeasureScoring(org.opencds.cqf.common.evaluation.MeasureScoring) ListEntryComponent(org.hl7.fhir.r4.model.ListResource.ListEntryComponent) Measure(org.hl7.fhir.r4.model.Measure) ListEntryComponent(org.hl7.fhir.r4.model.ListResource.ListEntryComponent) Reference(org.hl7.fhir.r4.model.Reference) MeasureReportBuilder(com.ibm.cohort.engine.r4.builder.MeasureReportBuilder) ListResource(org.hl7.fhir.r4.model.ListResource) Resource(org.hl7.fhir.r4.model.Resource) Patient(org.hl7.fhir.r4.model.Patient) Quantity(org.hl7.fhir.r4.model.Quantity) MeasureReport(org.hl7.fhir.r4.model.MeasureReport) IdType(org.hl7.fhir.r4.model.IdType) ListResource(org.hl7.fhir.r4.model.ListResource) HashMap(java.util.HashMap) Map(java.util.Map) MeasurePopulationType(org.opencds.cqf.common.evaluation.MeasurePopulationType)

Example 67 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project quality-measure-and-cohort-service by Alvearie.

the class ModelUtilsTest method testFHIR400Model.

@Test
public void testFHIR400Model() throws Exception {
    ModelInfo modelInfo = loadFromClasspath("org/hl7/fhir/fhir-modelinfo-4.0.0.xml");
    Map<QName, TypeNode> typeMap = ModelUtils.buildTypeMap(modelInfo);
    assertEquals(892, typeMap.size());
    TypeNode node = typeMap.get(new QName(modelInfo.getUrl(), "MoneyQuantity"));
    assertNotNull(node);
    assertEquals("Missing parent type", 1, node.getParentTypes().size());
    assertEquals("Quantity", node.getParentTypes().iterator().next().getTypeName().getLocalPart());
    node = typeMap.get(new QName(modelInfo.getUrl(), "Quantity"));
    assertNotNull(node);
    assertEquals(5, node.getChildTypes().size());
    assertEquals(1, node.getChildTypes().stream().filter(n -> n.getTypeName().getLocalPart().equals("MoneyQuantity")).collect(Collectors.counting()).longValue());
    node = typeMap.get(new QName(modelInfo.getUrl(), "Element"));
    assertNotNull(node);
    // The actual parent is System.Any, but we exclude included types from the type map
    assertEquals(0, node.getParentTypes().size());
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) Arrays(java.util.Arrays) TypeInfo(org.hl7.elm_modelinfo.r1.TypeInfo) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertThrows(org.junit.Assert.assertThrows) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) Collectors(java.util.stream.Collectors) File(java.io.File) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) Assert.assertNull(org.junit.Assert.assertNull) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) Map(java.util.Map) QName(javax.xml.namespace.QName) JAXB(javax.xml.bind.JAXB) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ModelSpecifier(org.hl7.elm_modelinfo.r1.ModelSpecifier) ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) QName(javax.xml.namespace.QName) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) Test(org.junit.Test)

Example 68 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project Gravity-SDOH-Exchange-RI by FHIR.

the class ConvertService method convertToBundle.

private String convertToBundle(JSONObject questionnaireResponse, String mapUri) {
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        Element element = validationEngine.transform(questionnaireResponse.toString().getBytes(), Manager.FhirFormat.JSON, mapUri);
        new JsonParser(validationEngine.getContext()).compose(element, byteArrayOutputStream, org.hl7.fhir.r5.formats.IParser.OutputStyle.PRETTY, null);
        return byteArrayOutputStream.toString();
    } catch (IOException e) {
        throw new IllegalStateException("QuestionnaireResponse with id cannot be parsed.", e.getCause());
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JsonParser(org.hl7.fhir.r5.elementmodel.JsonParser)

Example 69 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project org.hl7.fhir.core by hapifhir.

the class SpecDifferenceEvaluator method analyseTypes.

private void analyseTypes(JsonObject element, ElementDefinition rev, ElementDefinition orig) {
    JsonArray oa = new JsonArray();
    JsonArray ra = new JsonArray();
    if (rev.getType().size() == 1 && orig.getType().size() == 1) {
        String r = describeType(rev.getType().get(0));
        if (Utilities.noString(r) && Utilities.existsInList(rev.getId(), "Element.id", "Extension.url"))
            r = "string";
        String o = describeType(orig.getType().get(0));
        if (Utilities.noString(o) && Utilities.existsInList(orig.getId(), "Element.id", "Extension.url"))
            o = "string";
        if (!o.equals(r)) {
            oa.add(new JsonPrimitive(o));
            ra.add(new JsonPrimitive(r));
        }
    } else {
        for (TypeRefComponent tr : orig.getType()) {
            if (!hasType(rev.getType(), tr))
                oa.add(new JsonPrimitive(describeType(tr)));
        }
        for (TypeRefComponent tr : rev.getType()) {
            if (!hasType(orig.getType(), tr) && !isAbstractType(tr.getWorkingCode()))
                ra.add(new JsonPrimitive(describeType(tr)));
        }
        for (TypeRefComponent tr : rev.getType()) {
            TypeRefComponent tm = getType(rev.getType(), tr);
            if (tm != null) {
                compareParameters(element, tr, tm);
            }
        }
    }
    if (oa.size() > 0)
        element.add("removed-types", oa);
    if (ra.size() > 0)
        element.add("added-types", ra);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)

Example 70 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project org.hl7.fhir.core by hapifhir.

the class SpecDifferenceEvaluator method compareBindings.

private boolean compareBindings(JsonObject element, ElementDefinitionBindingComponent rev, ElementDefinitionBindingComponent orig) {
    boolean res = false;
    if (rev.getStrength() != orig.getStrength()) {
        element.addProperty("binding-strength-changed", true);
        res = true;
    }
    if (!Base.compareDeep(rev.getValueSet(), orig.getValueSet(), false)) {
        element.addProperty("binding-valueset-changed", true);
        res = true;
    }
    if (!maxValueSetsMatch(rev, orig)) {
        element.addProperty("max-valueset-changed", true);
        res = true;
    }
    if (rev.getStrength() == BindingStrength.REQUIRED && orig.getStrength() == BindingStrength.REQUIRED) {
        JsonArray oa = new JsonArray();
        JsonArray ra = new JsonArray();
        ValueSet vrev = getValueSet(rev.getValueSet(), revision.getExpansions());
        ValueSet vorig = getValueSet(rev.getValueSet(), original.getExpansions());
        if (vrev != null && vorig != null) {
            for (ValueSetExpansionContainsComponent cc : vorig.getExpansion().getContains()) {
                if (!hasCode(vrev, cc))
                    oa.add(new JsonPrimitive(cc.getCode()));
            }
            for (ValueSetExpansionContainsComponent cc : vrev.getExpansion().getContains()) {
                if (!hasCode(vorig, cc))
                    ra.add(new JsonPrimitive(cc.getCode()));
            }
        }
        if (oa.size() > 0 || ra.size() > 0) {
            element.addProperty("binding-codes-changed", true);
            res = true;
        }
        if (oa.size() > 0)
            element.add("removed-codes", oa);
        if (ra.size() > 0)
            element.add("added-codes", ra);
    }
    return res;
}
Also used : JsonArray(com.google.gson.JsonArray) ValueSetExpansionContainsComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent) JsonPrimitive(com.google.gson.JsonPrimitive)

Aggregations

Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)659 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)488 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)486 ArrayList (java.util.ArrayList)240 FHIRException (org.hl7.fhir.exceptions.FHIRException)162 Element (org.hl7.fhir.r5.elementmodel.Element)98 IOException (java.io.IOException)97 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)91 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)84 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)84 Element (org.w3c.dom.Element)74 JsonElement (com.google.gson.JsonElement)62 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)61 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)60 HashSet (java.util.HashSet)53 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)49 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)48 Cell (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)47 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)46 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)43