Search in sources :

Example 1 with Range

use of org.hl7.fhir.r4.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 2 with Range

use of org.hl7.fhir.r4.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 3 with Range

use of org.hl7.fhir.r4.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 4 with Range

use of org.hl7.fhir.r4.model.Range in project beneficiary-fhir-data by CMSgov.

the class ExplanationOfBenefitResourceProvider method findByPatient.

/**
 * Adds support for the FHIR "search" operation for {@link ExplanationOfBenefit}s, allowing users
 * to search by {@link ExplanationOfBenefit#getPatient()}.
 *
 * <p>The {@link Search} annotation indicates that this method supports the search operation.
 * There may be many different methods annotated with this {@link Search} annotation, to support
 * many different search criteria.
 *
 * @param patient a {@link ReferenceParam} for the {@link ExplanationOfBenefit#getPatient()} to
 *     try and find matches for {@link ExplanationOfBenefit}s
 * @param type a list of {@link ClaimType} to include in the result. Defaults to all types.
 * @param startIndex an {@link OptionalParam} for the startIndex (or offset) used to determine
 *     pagination
 * @param excludeSamhsa an {@link OptionalParam} that, if <code>"true"</code>, will use {@link
 *     Stu3EobSamhsaMatcher} to filter out all SAMHSA-related claims from the results
 * @param lastUpdated an {@link OptionalParam} that specifies a date range for the lastUpdated
 *     field.
 * @param serviceDate an {@link OptionalParam} that specifies a date range for {@link
 *     ExplanationOfBenefit}s that completed
 * @param requestDetails a {@link RequestDetails} containing the details of the request URL, used
 *     to parse out pagination values
 * @return Returns a {@link Bundle} of {@link ExplanationOfBenefit}s, which may contain multiple
 *     matching resources, or may also be empty.
 */
@Search
@Trace
public Bundle findByPatient(@RequiredParam(name = ExplanationOfBenefit.SP_PATIENT) @Description(shortDefinition = "The patient identifier to search for") ReferenceParam patient, @OptionalParam(name = "type") @Description(shortDefinition = "A list of claim types to include") TokenAndListParam type, @OptionalParam(name = "startIndex") @Description(shortDefinition = "The offset used for result pagination") String startIndex, @OptionalParam(name = "excludeSAMHSA") @Description(shortDefinition = "If true, exclude all SAMHSA-related resources") String excludeSamhsa, @OptionalParam(name = "_lastUpdated") @Description(shortDefinition = "Include resources last updated in the given range") DateRangeParam lastUpdated, @OptionalParam(name = "service-date") @Description(shortDefinition = "Include resources that completed in the given range") DateRangeParam serviceDate, RequestDetails requestDetails) {
    /*
     * startIndex is an optional parameter here because it must be declared in the
     * event it is passed in. However, it is not being used here because it is also
     * contained within requestDetails and parsed out along with other parameters
     * later.
     */
    String beneficiaryId = patient.getIdPart();
    Set<ClaimType> claimTypes = parseTypeParam(type);
    Boolean includeTaxNumbers = returnIncludeTaxNumbers(requestDetails);
    OffsetLinkBuilder paging = new OffsetLinkBuilder(requestDetails, "/ExplanationOfBenefit?");
    Operation operation = new Operation(Operation.Endpoint.V1_EOB);
    operation.setOption("by", "patient");
    operation.setOption("types", (claimTypes.size() == ClaimType.values().length) ? "*" : claimTypes.stream().sorted(Comparator.comparing(ClaimType::name)).collect(Collectors.toList()).toString());
    operation.setOption("IncludeTaxNumbers", "" + includeTaxNumbers);
    operation.setOption("pageSize", paging.isPagingRequested() ? "" + paging.getPageSize() : "*");
    operation.setOption("_lastUpdated", Boolean.toString(lastUpdated != null && !lastUpdated.isEmpty()));
    operation.setOption("service-date", Boolean.toString(serviceDate != null && !serviceDate.isEmpty()));
    operation.publishOperationName();
    List<IBaseResource> eobs = new ArrayList<IBaseResource>();
    // Optimize when the lastUpdated parameter is specified and result set is empty
    if (loadedFilterManager.isResultSetEmpty(beneficiaryId, lastUpdated)) {
        return TransformerUtils.createBundle(paging, eobs, loadedFilterManager.getTransactionTime());
    }
    /*
     * The way our JPA/SQL schema is setup, we have to run a separate search for
     * each claim type, then combine the results. It's not super efficient, but it's
     * also not so inefficient that it's worth fixing.
     */
    if (claimTypes.contains(ClaimType.CARRIER))
        eobs.addAll(transformToEobs(ClaimType.CARRIER, findClaimTypeByPatient(ClaimType.CARRIER, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.DME))
        eobs.addAll(transformToEobs(ClaimType.DME, findClaimTypeByPatient(ClaimType.DME, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.HHA))
        eobs.addAll(transformToEobs(ClaimType.HHA, findClaimTypeByPatient(ClaimType.HHA, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.HOSPICE))
        eobs.addAll(transformToEobs(ClaimType.HOSPICE, findClaimTypeByPatient(ClaimType.HOSPICE, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.INPATIENT))
        eobs.addAll(transformToEobs(ClaimType.INPATIENT, findClaimTypeByPatient(ClaimType.INPATIENT, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.OUTPATIENT))
        eobs.addAll(transformToEobs(ClaimType.OUTPATIENT, findClaimTypeByPatient(ClaimType.OUTPATIENT, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.PDE))
        eobs.addAll(transformToEobs(ClaimType.PDE, findClaimTypeByPatient(ClaimType.PDE, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (claimTypes.contains(ClaimType.SNF))
        eobs.addAll(transformToEobs(ClaimType.SNF, findClaimTypeByPatient(ClaimType.SNF, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
    if (Boolean.parseBoolean(excludeSamhsa))
        filterSamhsa(eobs);
    eobs.sort(ExplanationOfBenefitResourceProvider::compareByClaimIdThenClaimType);
    // Add bene_id to MDC logs
    TransformerUtils.logBeneIdToMdc(Arrays.asList(beneficiaryId));
    return TransformerUtils.createBundle(paging, eobs, loadedFilterManager.getTransactionTime());
}
Also used : OffsetLinkBuilder(gov.cms.bfd.server.war.commons.OffsetLinkBuilder) ArrayList(java.util.ArrayList) Operation(gov.cms.bfd.server.war.Operation) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Trace(com.newrelic.api.agent.Trace) Search(ca.uhn.fhir.rest.annotation.Search)

Example 5 with Range

use of org.hl7.fhir.r4.model.Range in project beneficiary-fhir-data by CMSgov.

the class AbstractR4ResourceProvider method createBundleFor.

/**
 * Creates a Bundle of resources for the given data using the given {@link ResourceTypeV2}.
 *
 * @param resourceTypes The {@link ResourceTypeV2} data to retrieve.
 * @param mbi The mbi to look up associated data for.
 * @param isHashed Denotes if the given mbi is hashed.
 * @param lastUpdated Date range of desired lastUpdate values to retrieve data for.
 * @param serviceDate Date range of the desired service date to retrieve data for.
 * @return A Bundle with data found using the provided parameters.
 */
@VisibleForTesting
Bundle createBundleFor(Set<ResourceTypeV2<T>> resourceTypes, String mbi, boolean isHashed, boolean excludeSamhsa, DateRangeParam lastUpdated, DateRangeParam serviceDate) {
    List<T> resources = new ArrayList<>();
    for (ResourceTypeV2<T> type : resourceTypes) {
        List<?> entities;
        entities = claimDao.findAllByMbiAttribute(type.getEntityClass(), type.getEntityMbiRecordAttribute(), mbi, isHashed, lastUpdated, serviceDate, type.getEntityEndDateAttribute());
        resources.addAll(entities.stream().filter(e -> !excludeSamhsa || hasNoSamhsaData(metricRegistry, e)).map(e -> type.getTransformer().transform(metricRegistry, e)).collect(Collectors.toList()));
    }
    Bundle bundle = new Bundle();
    resources.forEach(c -> {
        Bundle.BundleEntryComponent entry = bundle.addEntry();
        entry.setResource((Resource) c);
    });
    return bundle;
}
Also used : IdParam(ca.uhn.fhir.rest.annotation.IdParam) ClaimDao(gov.cms.bfd.server.war.r4.providers.preadj.common.ClaimDao) Trace(com.newrelic.api.agent.Trace) SpringConfiguration(gov.cms.bfd.server.war.SpringConfiguration) Description(ca.uhn.fhir.model.api.annotation.Description) NoResultException(javax.persistence.NoResultException) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RequiredParam(ca.uhn.fhir.rest.annotation.RequiredParam) PreAdjMcsClaim(gov.cms.bfd.model.rda.PreAdjMcsClaim) Inject(javax.inject.Inject) TransformerUtilsV2(gov.cms.bfd.server.war.r4.providers.TransformerUtilsV2) Matcher(java.util.regex.Matcher) RequestDetails(ca.uhn.fhir.rest.api.server.RequestDetails) DateRangeParam(ca.uhn.fhir.rest.param.DateRangeParam) Search(ca.uhn.fhir.rest.annotation.Search) IResourceProvider(ca.uhn.fhir.rest.server.IResourceProvider) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ReferenceParam(ca.uhn.fhir.rest.param.ReferenceParam) Map(java.util.Map) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) Nonnull(javax.annotation.Nonnull) Read(ca.uhn.fhir.rest.annotation.Read) PreAdjFissClaim(gov.cms.bfd.model.rda.PreAdjFissClaim) IdDt(ca.uhn.fhir.model.primitive.IdDt) MetricRegistry(com.codahale.metrics.MetricRegistry) Set(java.util.Set) Resource(org.hl7.fhir.r4.model.Resource) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Collectors(java.util.stream.Collectors) ClaimResponse(org.hl7.fhir.r4.model.ClaimResponse) IdType(org.hl7.fhir.r4.model.IdType) TokenParam(ca.uhn.fhir.rest.param.TokenParam) Objects(java.util.Objects) List(java.util.List) ResourceTypeV2(gov.cms.bfd.server.war.r4.providers.preadj.common.ResourceTypeV2) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Bundle(org.hl7.fhir.r4.model.Bundle) OptionalParam(ca.uhn.fhir.rest.annotation.OptionalParam) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) Claim(org.hl7.fhir.r4.model.Claim) TokenAndListParam(ca.uhn.fhir.rest.param.TokenAndListParam) Bundle(org.hl7.fhir.r4.model.Bundle) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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