Search in sources :

Example 41 with Range

use of org.hl7.fhir.r4b.model.Range in project loinc2hpo by monarch-initiative.

the class ObservationAnalysisFromQnValue method getHPOforObservation.

@Override
public HpoTermId4LoincTest getHPOforObservation() throws ReferenceNotFoundException, AmbiguousReferenceException, UnrecognizedCodeException {
    HpoTermId4LoincTest hpoTermId4LoincTest = null;
    // find applicable reference range
    List<Observation.ObservationReferenceRangeComponent> references = this.references.stream().filter(p -> withinAgeRange(p)).collect(Collectors.toList());
    if (references.size() < 1) {
        throw new ReferenceNotFoundException();
    } else if (references.size() == 1) {
        Observation.ObservationReferenceRangeComponent targetReference = references.get(0);
        double low = targetReference.hasLow() ? targetReference.getLow().getValue().doubleValue() : Double.MIN_VALUE;
        double high = targetReference.hasHigh() ? targetReference.getHigh().getValue().doubleValue() : Double.MAX_VALUE;
        double observed = valueQuantity.getValue().doubleValue();
        Loinc2HPOCodedValue result;
        if (observed < low) {
            result = Loinc2HPOCodedValue.fromCode("L");
        } else if (observed > high) {
            result = Loinc2HPOCodedValue.fromCode("H");
        } else {
            result = Loinc2HPOCodedValue.fromCode("N");
        }
        Code resultCode = Code.getNewCode().setSystem(Loinc2HPOCodedValue.CODESYSTEM).setCode(result.toCode());
        hpoTermId4LoincTest = annotationMap.get(loincId).loincInterpretationToHPO(resultCode);
    } else if (references.size() == 2) {
        // what does it mean with multiple references
        throw new AmbiguousReferenceException();
    } else if (references.size() == 3) {
    // it can happen when there is actually one range but coded in three ranges
    // e.g. normal 20-30
    // in this case, one range ([20, 30]) is sufficient;
    // however, it is written as three ranges: ( , 20) [20, 30] (30, )
    // We should handle this case
    } else {
        throw new AmbiguousReferenceException();
    }
    // if we can still not find an answer, it is probably that we did not have the annotation
    if (hpoTermId4LoincTest == null)
        throw new UnrecognizedCodeException();
    return hpoTermId4LoincTest;
}
Also used : AgeCalculator(org.monarchinitiative.loinc2hpo.util.AgeCalculator) Date(java.util.Date) Loinc2HPOCodedValue(org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue) org.monarchinitiative.loinc2hpo.exception(org.monarchinitiative.loinc2hpo.exception) Collectors(java.util.stream.Collectors) BigDecimal(java.math.BigDecimal) List(java.util.List) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) Loinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation) Year(java.time.Year) LocalDate(java.time.LocalDate) Map(java.util.Map) org.hl7.fhir.dstu3.model(org.hl7.fhir.dstu3.model) FHIRException(org.hl7.fhir.exceptions.FHIRException) Code(org.monarchinitiative.loinc2hpo.codesystems.Code) Loinc2HPOCodedValue(org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) Code(org.monarchinitiative.loinc2hpo.codesystems.Code)

Example 42 with Range

use of org.hl7.fhir.r4b.model.Range in project loinc2hpo by monarch-initiative.

the class CodeSystemInternal method init.

@Override
protected void init() {
    final Coding BELOWNORMAL = new Coding(INTERNALSYSTEM, "L", "below normal range");
    final Coding NORMAL = new Coding(INTERNALSYSTEM, "N", "within normal range");
    final Coding ABOVENORMAL = new Coding(INTERNALSYSTEM, "H", "above normal range");
    final Coding ABSENCE = new Coding(INTERNALSYSTEM, "NP", "not present");
    final Coding PRESENCE = new Coding(INTERNALSYSTEM, "P", "present");
    final Coding UNKNOWN = new Coding(INTERNALSYSTEM, "unknown", "unknown");
    codes.put(BELOWNORMAL.getCode(), BELOWNORMAL);
    codes.put(NORMAL.getCode(), NORMAL);
    codes.put(ABOVENORMAL.getCode(), ABOVENORMAL);
    codes.put(ABSENCE.getCode(), ABSENCE);
    codes.put(PRESENCE.getCode(), PRESENCE);
    codes.put(UNKNOWN.getCode(), UNKNOWN);
}
Also used : Coding(org.hl7.fhir.dstu3.model.Coding)

Example 43 with Range

use of org.hl7.fhir.r4b.model.Range in project loinc2hpo by monarch-initiative.

the class ObservationWithValueRangeTest method highHbWithReferenceRange.

/**
 * This observation does not have an Interpretation, but we can infer the value to be high
 * based on the reference range.
 */
@Test
public void highHbWithReferenceRange() {
    Observation observation = highHemoglobinWithValueRangeObservation();
    Uberobservation uberobservation = new ObservationDtu3(observation);
    LoincId erysInBlood = new LoincId("789-8");
    Optional<LoincId> opt = uberobservation.getLoincId();
    assertTrue(opt.isPresent());
    assertEquals(erysInBlood, opt.get());
    Optional<Outcome> outcomeOpt = uberobservation.getOutcome();
    assertTrue(outcomeOpt.isPresent());
    assertEquals(Outcome.HIGH(), outcomeOpt.get());
}
Also used : ObservationDtu3(org.monarchinitiative.loinc2hpofhir.fhir2hpo.ObservationDtu3) Outcome(org.monarchinitiative.loinc2hpocore.codesystems.Outcome) Observation(org.hl7.fhir.dstu3.model.Observation) LoincId(org.monarchinitiative.loinc2hpocore.loinc.LoincId) Uberobservation(org.monarchinitiative.loinc2hpofhir.fhir2hpo.Uberobservation) Test(org.junit.jupiter.api.Test)

Example 44 with Range

use of org.hl7.fhir.r4b.model.Range in project clinical_quality_language by cqframework.

the class ElmDataRequirement method applyConditionRequirementTo.

private void applyConditionRequirementTo(ElmConditionRequirement conditionRequirement, Retrieve retrieve, ElmRequirementsContext context) {
    if (retrieve.getDataType() == null) {
        // If the retrieve has no data type, it is neither useful nor possible to apply requirements to it
        return;
    }
    if (!conditionRequirement.isTargetable()) {
        // If the comparand of the condition requirement is not targetable, requirements cannot be applied
        return;
    }
    // if the column is terminology-valued, express as a code filter
    // if the column is date-valued, express as a date filter
    // else express as an other filter
    Property property = conditionRequirement.getProperty().getProperty();
    // DataType propertyType = property.getResultType();
    // Use the comparison type due to the likelihood of conversion operators along the property
    DataType comparisonType = conditionRequirement.getComparand().getExpression().getResultType();
    if (comparisonType != null) {
        if (context.getTypeResolver().isTerminologyType(comparisonType)) {
            CodeFilterElement codeFilter = new CodeFilterElement();
            if (property instanceof Search) {
                codeFilter.setSearch(property.getPath());
            } else {
                codeFilter.setProperty(property.getPath());
            }
            switch(conditionRequirement.getElement().getClass().getSimpleName()) {
                case "Equal":
                    codeFilter.setComparator("=");
                    break;
                case "Equivalent":
                    codeFilter.setComparator("~");
                    break;
                case "In":
                case "InValueSet":
                case "AnyInValueSet":
                    codeFilter.setComparator("in");
                    break;
            }
            if (codeFilter.getComparator() != null && (conditionRequirement.isTargetable())) {
                codeFilter.setValue(conditionRequirement.getComparand().getExpression());
                if (!ComparableElmRequirement.hasCodeFilter(retrieve.getCodeFilter(), codeFilter)) {
                    retrieve.getCodeFilter().add(codeFilter);
                }
            }
        } else if (context.getTypeResolver().isDateType(comparisonType)) {
            DateFilterElement dateFilter = new DateFilterElement();
            if (property instanceof Search) {
                dateFilter.setSearch(property.getPath());
            } else {
                dateFilter.setProperty(property.getPath());
            }
            // Determine operation and appropriate range
            // If right is interval-valued
            // If the operation is equal, equivalent, same as, in, or included in, the date range is the comparand
            Expression comparand = conditionRequirement.getComparand().getExpression();
            if (conditionRequirement.isTargetable()) {
                if (context.getTypeResolver().isIntervalType(comparisonType)) {
                    switch(conditionRequirement.getElement().getClass().getSimpleName()) {
                        case "Equal":
                        case "Equivalent":
                        case "SameAs":
                        case "In":
                        case "IncludedIn":
                            dateFilter.setValue(comparand);
                            break;
                        case "Before":
                            dateFilter.setValue(new Interval().withLowClosed(true).withHigh(new Start().withOperand(comparand)).withHighClosed(false));
                            break;
                        case "SameOrBefore":
                            dateFilter.setValue(new Interval().withLowClosed(true).withHigh(new Start().withOperand(comparand)).withHighClosed(true));
                            break;
                        case "After":
                            dateFilter.setValue(new Interval().withLow(new End().withOperand(comparand)).withLowClosed(false).withHighClosed(true));
                            break;
                        case "SameOrAfter":
                            dateFilter.setValue(new Interval().withLow(new End().withOperand(comparand)).withLowClosed(true).withHighClosed(true));
                            break;
                        case "Includes":
                        case "Meets":
                        case "MeetsBefore":
                        case "MeetsAfter":
                        case "Overlaps":
                        case "OverlapsBefore":
                        case "OverlapsAfter":
                        case "Starts":
                        case "Ends":
                            // TODO: Might be better to turn these into date-based conjunctive requirements as part of condition requirement inference
                            break;
                    }
                } else {
                    switch(conditionRequirement.getElement().getClass().getSimpleName()) {
                        case "Equal":
                        case "Equivalent":
                        case "SameAs":
                            dateFilter.setValue(new Interval().withLow(comparand).withLowClosed(true).withHigh(comparand).withHighClosed(true));
                            break;
                        case "Less":
                        case "Before":
                            dateFilter.setValue(new Interval().withLowClosed(true).withHigh(comparand).withHighClosed(false));
                            break;
                        case "LessOrEqual":
                        case "SameOrBefore":
                            dateFilter.setValue(new Interval().withLowClosed(true).withHigh(comparand).withHighClosed(true));
                            break;
                        case "Greater":
                        case "After":
                            dateFilter.setValue(new Interval().withLow(comparand).withLowClosed(false).withHighClosed(true));
                            break;
                        case "GreaterOrEqual":
                        case "SameOrAfter":
                            dateFilter.setValue(new Interval().withLow(comparand).withLowClosed(true).withHighClosed(true));
                            break;
                    }
                }
            }
            if (dateFilter.getValue() != null) {
                if (!ComparableElmRequirement.hasDateFilter(retrieve.getDateFilter(), dateFilter)) {
                    retrieve.getDateFilter().add(dateFilter);
                }
            }
        } else {
        }
    }
}
Also used : DataType(org.hl7.cql.model.DataType)

Example 45 with Range

use of org.hl7.fhir.r4b.model.Range in project dpc-app by CMSgov.

the class GroupResourceUnitTest method makeTestPatients.

private Map<UUID, Patient> makeTestPatients(int count, UUID orgId) {
    if (count > 88) {
        throw new IllegalStateException("Don't support building more than 88 patients..yet (need a better mbi generator)");
    }
    final Map<UUID, Patient> patients = Maps.newHashMap();
    while (count > 0) {
        UUID id = UUID.randomUUID();
        Patient patient = FHIRPatientBuilder.newBuild().withMbi(// makes MBI in range 4S41C00AA10 -> 4S41C00AA99
        "4S41C00AA" + (count + 10)).withId(id).withTestData().build();
        patients.put(id, patient);
        count--;
    }
    return patients;
}
Also used : Patient(org.hl7.fhir.dstu3.model.Patient) UUID(java.util.UUID)

Aggregations

Test (org.junit.jupiter.api.Test)18 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)15 Quantity (org.hl7.fhir.r4.model.Quantity)14 Resource (org.hl7.fhir.r4.model.Resource)13 NotImplementedException (org.apache.commons.lang3.NotImplementedException)12 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Search (ca.uhn.fhir.rest.annotation.Search)11 Trace (com.newrelic.api.agent.Trace)10 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)10 Beneficiary (gov.cms.bfd.model.rif.Beneficiary)9 ArrayList (java.util.ArrayList)9 DateRangeParam (ca.uhn.fhir.rest.param.DateRangeParam)8 Operation (gov.cms.bfd.server.war.Operation)8 OffsetLinkBuilder (gov.cms.bfd.server.war.commons.OffsetLinkBuilder)8 Date (java.util.Date)8 Range (org.hl7.fhir.r4.model.Range)8 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)7 List (java.util.List)7 Bundle (org.hl7.fhir.r4.model.Bundle)7