Search in sources :

Example 11 with Observation

use of ca.uhn.fhir.model.dstu2.resource.Observation in project synthea by synthetichealth.

the class FHIRDSTU2ExporterTest method testSampledDataExport.

@Test
public void testSampledDataExport() throws Exception {
    Person person = new Person(0L);
    person.attributes.put(Person.GENDER, "F");
    person.attributes.put(Person.FIRST_LANGUAGE, "spanish");
    person.attributes.put(Person.RACE, "other");
    person.attributes.put(Person.ETHNICITY, "hispanic");
    person.attributes.put(Person.INCOME, Integer.parseInt(Config.get("generate.demographics.socioeconomic.income.poverty")) * 2);
    person.attributes.put(Person.OCCUPATION_LEVEL, 1.0);
    person.history = new LinkedList<>();
    Provider mock = Mockito.mock(Provider.class);
    Mockito.when(mock.getResourceID()).thenReturn("Mock-UUID");
    person.setProvider(EncounterType.AMBULATORY, mock);
    person.setProvider(EncounterType.WELLNESS, mock);
    person.setProvider(EncounterType.EMERGENCY, mock);
    person.setProvider(EncounterType.INPATIENT, mock);
    Long time = System.currentTimeMillis();
    int age = 35;
    long birthTime = time - Utilities.convertTime("years", age);
    person.attributes.put(Person.BIRTHDATE, birthTime);
    Payer.loadNoInsurance();
    for (int i = 0; i < age; i++) {
        long yearTime = time - Utilities.convertTime("years", i);
        person.coverage.setPayerAtTime(yearTime, Payer.noInsurance);
    }
    Module module = TestHelper.getFixture("observation.json");
    State encounter = module.getState("SomeEncounter");
    assertTrue(encounter.process(person, time));
    person.history.add(encounter);
    State physiology = module.getState("Simulate_CVS");
    assertTrue(physiology.process(person, time));
    person.history.add(physiology);
    State sampleObs = module.getState("SampledDataObservation");
    assertTrue(sampleObs.process(person, time));
    person.history.add(sampleObs);
    FhirContext ctx = FhirDstu2.getContext();
    IParser parser = ctx.newJsonParser().setPrettyPrint(true);
    String fhirJson = FhirDstu2.convertToFHIRJson(person, System.currentTimeMillis());
    Bundle bundle = parser.parseResource(Bundle.class, fhirJson);
    for (Entry entry : bundle.getEntry()) {
        if (entry.getResource() instanceof Observation) {
            Observation obs = (Observation) entry.getResource();
            assertTrue(obs.getValue() instanceof SampledDataDt);
            SampledDataDt data = (SampledDataDt) obs.getValue();
            // 0.01s == 10ms
            assertEquals(10, data.getPeriod().doubleValue(), 0.001);
            assertEquals(3, (int) data.getDimensions());
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Bundle(ca.uhn.fhir.model.dstu2.resource.Bundle) Provider(org.mitre.synthea.world.agents.Provider) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) SampledDataDt(ca.uhn.fhir.model.dstu2.composite.SampledDataDt) State(org.mitre.synthea.engine.State) Observation(ca.uhn.fhir.model.dstu2.resource.Observation) Module(org.mitre.synthea.engine.Module) Person(org.mitre.synthea.world.agents.Person) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.Test)

Example 12 with Observation

use of ca.uhn.fhir.model.dstu2.resource.Observation in project synthea by synthetichealth.

the class CodeResolveAndExportTest method verifyEncounterCodeDstu2.

private void verifyEncounterCodeDstu2() throws IOException {
    InputStream inputStream = new FileInputStream(dstu2OutputPath.toFile().getAbsolutePath());
    ca.uhn.fhir.model.dstu2.resource.Bundle bundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) FhirDstu2.getContext().newJsonParser().parseResource(inputStream);
    // Find encounter reason code.
    Optional<Entry> maybeEncounterEntry = bundle.getEntry().stream().filter(entry -> entry.getResource().getResourceName().equals(org.hl7.fhir.dstu2.model.ResourceType.Encounter.name())).findFirst();
    assertTrue(maybeEncounterEntry.isPresent());
    ca.uhn.fhir.model.dstu2.resource.Encounter encounterResource = (ca.uhn.fhir.model.dstu2.resource.Encounter) maybeEncounterEntry.get().getResource();
    assertEquals(encounterResource.getReason().size(), 1);
    CodeableConceptDt encounterReason = encounterResource.getReason().get(0);
    assertEquals(encounterReason.getCoding().size(), 1);
    CodingDt reasonCoding = encounterReason.getCoding().get(0);
    // Check encounter reason code.
    assertEquals(SNOMED_URI, reasonCoding.getSystem());
    assertEquals(EXPECTED_REASON_CODE, reasonCoding.getCode());
    assertEquals(EXPECTED_REASON_DISPLAY, reasonCoding.getDisplay());
    Optional<Entry> maybeObservationEntry = bundle.getEntry().stream().filter(entry -> entry.getResource().getResourceName().equals(org.hl7.fhir.dstu2.model.ResourceType.Observation.name())).findFirst();
    assertTrue(maybeObservationEntry.isPresent());
    // Find observation type code.
    ca.uhn.fhir.model.dstu2.resource.Observation observationResource = (ca.uhn.fhir.model.dstu2.resource.Observation) maybeObservationEntry.get().getResource();
    CodeableConceptDt observationType = observationResource.getCode();
    assertNotNull(observationType);
    assertEquals(observationType.getCoding().size(), 1);
    CodingDt observationTypeCoding = observationType.getCoding().get(0);
    // Check observation type code.
    assertEquals(LOINC_URI, observationTypeCoding.getSystem());
    assertEquals(OBSERVATION_CODE, observationTypeCoding.getCode());
    assertEquals(OBSERVATION_DISPLAY, observationTypeCoding.getDisplay());
    // Find observation value code.
    CodeableConceptDt observationValue = (CodeableConceptDt) observationResource.getValue();
    assertNotNull(observationValue);
    assertEquals(observationValue.getCoding().size(), 1);
    CodingDt observationValueCoding = observationValue.getCoding().get(0);
    // Check observation value code.
    assertEquals(LOINC_URI, observationValueCoding.getSystem());
    assertEquals(EXPECTED_VALUE_CODE, observationValueCoding.getCode());
    assertEquals(EXPECTED_VALUE_DISPLAY, observationValueCoding.getDisplay());
    inputStream.close();
}
Also used : TestHelper.years(org.mitre.synthea.TestHelper.years) Arrays(java.util.Arrays) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Location(org.mitre.synthea.world.geography.Location) Bundle(org.hl7.fhir.dstu3.model.Bundle) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) Coding(org.hl7.fhir.dstu3.model.Coding) CodeableConcept(org.hl7.fhir.dstu3.model.CodeableConcept) Person(org.mitre.synthea.world.agents.Person) Document(org.w3c.dom.Document) After(org.junit.After) Path(java.nio.file.Path) Encounter(org.mitre.synthea.world.concepts.HealthRecord.Encounter) RestTemplate(org.springframework.web.client.RestTemplate) RandomCodeGenerator(org.mitre.synthea.helpers.RandomCodeGenerator) List(java.util.List) SAXException(org.xml.sax.SAXException) LOINC_URI(org.mitre.synthea.TestHelper.LOINC_URI) Optional(java.util.Optional) TestHelper(org.mitre.synthea.TestHelper) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) EncounterType(org.mitre.synthea.world.concepts.HealthRecord.EncounterType) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) SimpleDateFormat(java.text.SimpleDateFormat) TestHelper.isHttpRecordingEnabled(org.mitre.synthea.TestHelper.isHttpRecordingEnabled) XPathExpression(javax.xml.xpath.XPathExpression) Generator(org.mitre.synthea.engine.Generator) CodingDt(ca.uhn.fhir.model.dstu2.composite.CodingDt) WireMock(com.github.tomakehurst.wiremock.client.WireMock) CodeableConceptDt(ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) ResourceType(org.hl7.fhir.dstu3.model.ResourceType) Node(org.w3c.dom.Node) Code(org.mitre.synthea.world.concepts.HealthRecord.Code) LOINC_OID(org.mitre.synthea.TestHelper.LOINC_OID) Before(org.junit.Before) TestHelper.wiremockOptions(org.mitre.synthea.TestHelper.wiremockOptions) Config(org.mitre.synthea.helpers.Config) NodeList(org.w3c.dom.NodeList) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) Payer(org.mitre.synthea.world.agents.Payer) File(java.io.File) Provider(org.mitre.synthea.world.agents.Provider) SNOMED_URI(org.mitre.synthea.TestHelper.SNOMED_URI) XPathFactory(javax.xml.xpath.XPathFactory) Rule(org.junit.Rule) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ProviderTest(org.mitre.synthea.world.agents.ProviderTest) Assert.assertEquals(org.junit.Assert.assertEquals) TestHelper.getTxRecordingSource(org.mitre.synthea.TestHelper.getTxRecordingSource) InputStream(java.io.InputStream) CodeableConceptDt(ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bundle(org.hl7.fhir.dstu3.model.Bundle) FileInputStream(java.io.FileInputStream) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) CodingDt(ca.uhn.fhir.model.dstu2.composite.CodingDt) Encounter(org.mitre.synthea.world.concepts.HealthRecord.Encounter)

Example 13 with Observation

use of ca.uhn.fhir.model.dstu2.resource.Observation in project synthea by synthetichealth.

the class FhirDstu2 method caregoal.

/**
 * Map the JsonObject into a FHIR Goal resource, and add it to the given Bundle.
 *
 * @param rand Source of randomness to use when generating ids etc
 * @param bundle The Bundle to add to
 * @param goalStatus The GoalStatus
 * @param goal The JsonObject
 * @return The added Entry
 */
private static Entry caregoal(RandomNumberGenerator rand, Bundle bundle, GoalStatusEnum goalStatus, JsonObject goal) {
    ca.uhn.fhir.model.dstu2.resource.Goal goalResource = new ca.uhn.fhir.model.dstu2.resource.Goal();
    goalResource.setStatus(goalStatus);
    if (goal.has("text")) {
        goalResource.setDescription(goal.get("text").getAsString());
    } else if (goal.has("codes")) {
        JsonObject code = goal.get("codes").getAsJsonArray().get(0).getAsJsonObject();
        goalResource.setDescription(code.get("display").getAsString());
    } else if (goal.has("observation")) {
        // build up our own text from the observation condition, similar to the graphviz logic
        JsonObject logic = goal.get("observation").getAsJsonObject();
        String[] text = { logic.get("codes").getAsJsonArray().get(0).getAsJsonObject().get("display").getAsString(), logic.get("operator").getAsString(), logic.get("value").getAsString() };
        goalResource.setDescription(String.join(" ", text));
    }
    if (goal.has("addresses")) {
        for (JsonElement reasonElement : goal.get("addresses").getAsJsonArray()) {
            if (reasonElement instanceof JsonObject) {
                JsonObject reasonObject = reasonElement.getAsJsonObject();
                String reasonCode = reasonObject.get("codes").getAsJsonObject().get("SNOMED-CT").getAsJsonArray().get(0).getAsString();
                for (Entry entry : bundle.getEntry()) {
                    if (entry.getResource().getResourceName().equals("Condition")) {
                        Condition condition = (Condition) entry.getResource();
                        // Only one element in list
                        CodingDt coding = condition.getCode().getCoding().get(0);
                        if (reasonCode.equals(coding.getCode())) {
                            goalResource.addAddresses().setReference(entry.getFullUrl());
                        }
                    }
                }
            }
        }
    }
    return newEntry(rand, bundle, goalResource);
}
Also used : Condition(ca.uhn.fhir.model.dstu2.resource.Condition) JsonObject(com.google.gson.JsonObject) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) JsonElement(com.google.gson.JsonElement) CodingDt(ca.uhn.fhir.model.dstu2.composite.CodingDt)

Example 14 with Observation

use of ca.uhn.fhir.model.dstu2.resource.Observation in project synthea by synthetichealth.

the class FhirDstu2 method mapValueToFHIRType.

static IDatatype mapValueToFHIRType(Object value, String unit) {
    if (value == null) {
        return null;
    } else if (value instanceof Condition) {
        Code conditionCode = ((HealthRecord.Entry) value).codes.get(0);
        return mapCodeToCodeableConcept(conditionCode, SNOMED_URI);
    } else if (value instanceof Code) {
        return mapCodeToCodeableConcept((Code) value, SNOMED_URI);
    } else if (value instanceof String) {
        return new StringDt((String) value);
    } else if (value instanceof Number) {
        double dblVal = ((Number) value).doubleValue();
        PlainBigDecimal bigVal = new PlainBigDecimal(dblVal);
        return new QuantityDt().setValue(bigVal).setCode(unit).setSystem(UNITSOFMEASURE_URI).setUnit(unit);
    } else if (value instanceof Components.SampledData) {
        return mapValueToSampledData((Components.SampledData) value, unit);
    } else {
        throw new IllegalArgumentException("unexpected observation value class: " + value.getClass().toString() + "; " + value);
    }
}
Also used : Condition(ca.uhn.fhir.model.dstu2.resource.Condition) SimpleQuantityDt(ca.uhn.fhir.model.dstu2.composite.SimpleQuantityDt) QuantityDt(ca.uhn.fhir.model.dstu2.composite.QuantityDt) Code(org.mitre.synthea.world.concepts.HealthRecord.Code) Components(org.mitre.synthea.engine.Components) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) StringDt(ca.uhn.fhir.model.primitive.StringDt)

Example 15 with Observation

use of ca.uhn.fhir.model.dstu2.resource.Observation in project synthea by synthetichealth.

the class FhirDstu2 method observation.

/**
 * Map the given Observation into a FHIR Observation resource, and add it to the given Bundle.
 *
 * @param rand
 *          Source of randomness to use when generating ids etc
 * @param personEntry
 *          The Person Entry
 * @param bundle
 *          The Bundle to add to
 * @param encounterEntry
 *          The current Encounter entry
 * @param observation
 *          The Observation
 * @return The added Entry
 */
private static Entry observation(RandomNumberGenerator rand, Entry personEntry, Bundle bundle, Entry encounterEntry, Observation observation) {
    ca.uhn.fhir.model.dstu2.resource.Observation observationResource = new ca.uhn.fhir.model.dstu2.resource.Observation();
    observationResource.setSubject(new ResourceReferenceDt(personEntry.getFullUrl()));
    observationResource.setEncounter(new ResourceReferenceDt(encounterEntry.getFullUrl()));
    observationResource.setStatus(ObservationStatusEnum.FINAL);
    Code code = observation.codes.get(0);
    observationResource.setCode(mapCodeToCodeableConcept(code, LOINC_URI));
    Code category = new Code("http://hl7.org/fhir/observation-category", observation.category, observation.category);
    observationResource.setCategory(mapCodeToCodeableConcept(category, "http://hl7.org/fhir/observation-category"));
    if (observation.value != null) {
        IDatatype value = mapValueToFHIRType(observation.value, observation.unit);
        observationResource.setValue(value);
    } else if (observation.observations != null && !observation.observations.isEmpty()) {
        // multi-observation (ex blood pressure)
        for (Observation subObs : observation.observations) {
            Component comp = new Component();
            comp.setCode(mapCodeToCodeableConcept(subObs.codes.get(0), LOINC_URI));
            IDatatype value = mapValueToFHIRType(subObs.value, subObs.unit);
            comp.setValue(value);
            observationResource.addComponent(comp);
        }
    }
    observationResource.setEffective(convertFhirDateTime(observation.start, true));
    observationResource.setIssued(new InstantDt(new Date(observation.start)));
    Entry entry = newEntry(rand, bundle, observationResource);
    observation.fullUrl = entry.getFullUrl();
    return entry;
}
Also used : IDatatype(ca.uhn.fhir.model.api.IDatatype) ResourceReferenceDt(ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt) Code(org.mitre.synthea.world.concepts.HealthRecord.Code) Date(java.util.Date) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) Observation(org.mitre.synthea.world.concepts.HealthRecord.Observation) Component(ca.uhn.fhir.model.dstu2.resource.Observation.Component) InstantDt(ca.uhn.fhir.model.primitive.InstantDt)

Aggregations

Entry (ca.uhn.fhir.model.dstu2.resource.Bundle.Entry)12 Observation (ca.uhn.fhir.model.dstu2.resource.Observation)9 Bundle (ca.uhn.fhir.model.dstu2.resource.Bundle)8 CodeableConceptDt (ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt)7 ArrayList (java.util.ArrayList)7 Condition (ca.uhn.fhir.model.dstu2.resource.Condition)6 CodingDt (ca.uhn.fhir.model.dstu2.composite.CodingDt)5 ResourceReferenceDt (ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt)5 DiagnosticReport (ca.uhn.fhir.model.dstu2.resource.DiagnosticReport)5 List (java.util.List)5 Encounter (org.mitre.synthea.world.concepts.HealthRecord.Encounter)4 FhirContext (ca.uhn.fhir.context.FhirContext)3 DiagnosticOrder (ca.uhn.fhir.model.dstu2.resource.DiagnosticOrder)3 Encounter (ca.uhn.fhir.model.dstu2.resource.Encounter)3 Organization (ca.uhn.fhir.model.dstu2.resource.Organization)3 Patient (ca.uhn.fhir.model.dstu2.resource.Patient)3 Practitioner (ca.uhn.fhir.model.dstu2.resource.Practitioner)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)3 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)3 File (java.io.File)3