Search in sources :

Example 1 with State

use of org.mitre.synthea.engine.State in project synthea by synthetichealth.

the class FHIRR4ExporterTest method testObservationAttachment.

@Test
public void testObservationAttachment() 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.attributes.put("Pulmonary Resistance", 0.1552);
    person.attributes.put("BMI Multiplier", 0.055);
    person.setVitalSign(VitalSign.BMI, 21.0);
    person.history = new LinkedList<>();
    Provider mock = Mockito.mock(Provider.class);
    Mockito.when(mock.getResourceID()).thenReturn("Mock-Provider");
    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 physiology = module.getState("Simulate_CVS");
    assertTrue(physiology.process(person, time));
    person.history.add(physiology);
    State encounter = module.getState("SomeEncounter");
    assertTrue(encounter.process(person, time));
    person.history.add(encounter);
    State chartState = module.getState("ChartObservation");
    assertTrue(chartState.process(person, time));
    person.history.add(chartState);
    State urlState = module.getState("UrlObservation");
    assertTrue(urlState.process(person, time));
    person.history.add(urlState);
    FhirContext ctx = FhirR4.getContext();
    IParser parser = ctx.newJsonParser().setPrettyPrint(true);
    String fhirJson = FhirR4.convertToFHIRJson(person, System.currentTimeMillis());
    Bundle bundle = parser.parseResource(Bundle.class, fhirJson);
    for (BundleEntryComponent entry : bundle.getEntry()) {
        if (entry.getResource() instanceof Media) {
            Media media = (Media) entry.getResource();
            if (media.getContent().getData() != null) {
                assertEquals(400, media.getWidth());
                assertEquals(200, media.getHeight());
                assertEquals("Invasive arterial pressure", media.getReasonCode().get(0).getText());
                assertTrue(Base64.isBase64(media.getContent().getDataElement().getValueAsString()));
            } else if (media.getContent().getUrl() != null) {
                assertEquals("https://example.com/image/12498596132", media.getContent().getUrl());
                assertEquals("en-US", media.getContent().getLanguage());
                assertTrue(media.getContent().getSize() > 0);
            } else {
                fail("Invalid Media element in output JSON");
            }
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Bundle(org.hl7.fhir.r4.model.Bundle) Media(org.hl7.fhir.r4.model.Media) Provider(org.mitre.synthea.world.agents.Provider) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) State(org.mitre.synthea.engine.State) Module(org.mitre.synthea.engine.Module) Person(org.mitre.synthea.world.agents.Person) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.Test)

Example 2 with State

use of org.mitre.synthea.engine.State in project synthea by synthetichealth.

the class FHIRR4ExporterTest 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.attributes.put(Person.CONTACT_EMAIL, "test@test.test");
    person.attributes.put(Person.CONTACT_GIVEN_NAME, "John");
    person.attributes.put(Person.CONTACT_FAMILY_NAME, "Appleseed");
    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 = FhirR4.getContext();
    IParser parser = ctx.newJsonParser().setPrettyPrint(true);
    String fhirJson = FhirR4.convertToFHIRJson(person, System.currentTimeMillis());
    Bundle bundle = parser.parseResource(Bundle.class, fhirJson);
    for (BundleEntryComponent entry : bundle.getEntry()) {
        if (entry.getResource() instanceof Observation) {
            Observation obs = (Observation) entry.getResource();
            assertTrue(obs.getValue() instanceof SampledData);
            SampledData data = (SampledData) 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(org.hl7.fhir.r4.model.Bundle) Provider(org.mitre.synthea.world.agents.Provider) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) State(org.mitre.synthea.engine.State) Observation(org.hl7.fhir.r4.model.Observation) SampledData(org.hl7.fhir.r4.model.SampledData) Module(org.mitre.synthea.engine.Module) Person(org.mitre.synthea.world.agents.Person) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.Test)

Example 3 with State

use of org.mitre.synthea.engine.State 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 4 with State

use of org.mitre.synthea.engine.State in project synthea by synthetichealth.

the class TransitionMetrics method printStats.

/**
 * Print the statistics that have been gathered.
 *
 * @param totalPopulation
 *          The total population that was simulated.
 * @param modules
 *          The collection of modules to display stats for
 */
public void printStats(int totalPopulation, Collection<Module> modules) {
    for (Module m : modules) {
        if (!m.getClass().equals(Module.class)) {
            // java module, not GMF. no states to show
            continue;
        }
        System.out.println(m.name.toUpperCase());
        Map<String, Metric> moduleMetrics = metrics.row(m.name);
        List<String> keys = new ArrayList<String>(moduleMetrics.keySet());
        Collections.sort(keys);
        for (String stateName : keys) {
            Metric stats = getMetric(m.name, stateName);
            int entered = stats.entered.get();
            int population = stats.population.get();
            long duration = stats.duration.get();
            int current = stats.current.get();
            System.out.println(stateName + ":");
            System.out.println(" Total times entered: " + stats.entered);
            System.out.println(" Population that ever hit this state: " + stats.population + " (" + decimal(population, totalPopulation) + "%)");
            System.out.println(" Average # of hits per total population: " + decimal(entered, totalPopulation));
            System.out.println(" Average # of hits per person that ever hit state: " + decimal(entered, population));
            System.out.println(" Population currently in state: " + stats.current + " (" + decimal(current, totalPopulation) + "%)");
            State state = m.getState(stateName);
            if (state instanceof State.Guard || state instanceof State.Delay) {
                System.out.println(" Total duration: " + durationOf(duration));
                System.out.println(" Average duration per time entered: " + durationOf(duration / entered));
                System.out.println(" Average duration per person that ever entered state: " + durationOf(duration / population));
            } else if (state instanceof State.Encounter && ((State.Encounter) state).isWellness()) {
                System.out.println(" (duration metrics for wellness encounter omitted)");
            }
            if (!stats.destinations.isEmpty()) {
                System.out.println(" Transitioned to:");
                long total = stats.destinations.values().stream().mapToLong(ai -> ai.longValue()).sum();
                stats.destinations.forEach((toState, count) -> System.out.println(" --> " + toState + " : " + count + " = " + decimal(count.get(), total) + "%"));
            }
            System.out.println();
        }
        List<String> unreached = new ArrayList<>(m.getStateNames());
        Collections.sort(unreached);
        // moduleMetrics only includes states actually hit
        unreached.removeAll(moduleMetrics.keySet());
        unreached.forEach(state -> System.out.println(state + ": \n Never reached \n\n"));
        System.out.println();
        System.out.println();
    }
}
Also used : Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) State(org.mitre.synthea.engine.State) HashBasedTable(com.google.common.collect.HashBasedTable) ArrayList(java.util.ArrayList) Module(org.mitre.synthea.engine.Module) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tables(com.google.common.collect.Tables) List(java.util.List) Person(org.mitre.synthea.world.agents.Person) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DurationFormatUtils(org.apache.commons.lang3.time.DurationFormatUtils) Map(java.util.Map) Table(com.google.common.collect.Table) Collections(java.util.Collections) ArrayList(java.util.ArrayList) State(org.mitre.synthea.engine.State) Module(org.mitre.synthea.engine.Module)

Example 5 with State

use of org.mitre.synthea.engine.State in project synthea by synthetichealth.

the class ValueSetCodeResolverTest method resolveCodesInImagingStudy.

@Test
public void resolveCodesInImagingStudy() throws Exception {
    // We load the imaging study from a module fixture, as there doesn't seem to be
    // a way to
    // instantiate it programmatically.
    Module module = TestHelper.getFixture("imaging_study_with_valueset.json");
    person.history = new ArrayList<>();
    State encounterState = module.getState("ED_Visit");
    assertTrue(encounterState.process(person, time));
    person.history.add(encounterState);
    State mri = module.getState("Knee_MRI");
    assertTrue(mri.process(person, time));
    ValueSetCodeResolver valueSetCodeResolver = new ValueSetCodeResolver(person);
    Person resolvedPerson = valueSetCodeResolver.resolve();
    // assertEquals(2, resolvedPerson.record.encounters.size());
    Encounter resolvedEncounter = resolvedPerson.record.encounters.get(resolvedPerson.record.encounters.size() - 1);
    assertEquals(1, resolvedEncounter.imagingStudies.size());
    ImagingStudy resolvedImagingStudy = resolvedEncounter.imagingStudies.get(0);
    assertEquals(1, resolvedImagingStudy.series.size());
    Series series = resolvedImagingStudy.series.get(0);
    assertEquals(SNOMED_URI, series.bodySite.system);
    assertEquals("762879008", series.bodySite.code);
    assertEquals("Structure of right common peroneal nerve in popliteal region", series.bodySite.display);
// Modality and SOP class are not really good candidates for ValueSet-based
// selection, so we do
// not currently have a sensible test case for these.
}
Also used : Series(org.mitre.synthea.world.concepts.HealthRecord.ImagingStudy.Series) State(org.mitre.synthea.engine.State) Encounter(org.mitre.synthea.world.concepts.HealthRecord.Encounter) ImagingStudy(org.mitre.synthea.world.concepts.HealthRecord.ImagingStudy) Module(org.mitre.synthea.engine.Module) Person(org.mitre.synthea.world.agents.Person) Test(org.junit.Test)

Aggregations

Module (org.mitre.synthea.engine.Module)10 State (org.mitre.synthea.engine.State)10 Test (org.junit.Test)8 Person (org.mitre.synthea.world.agents.Person)8 FhirContext (ca.uhn.fhir.context.FhirContext)6 IParser (ca.uhn.fhir.parser.IParser)6 Provider (org.mitre.synthea.world.agents.Provider)6 Bundle (ca.uhn.fhir.model.dstu2.resource.Bundle)2 Entry (ca.uhn.fhir.model.dstu2.resource.Bundle.Entry)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Bundle (org.hl7.fhir.dstu3.model.Bundle)2 BundleEntryComponent (org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent)2 Bundle (org.hl7.fhir.r4.model.Bundle)2 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)2 Encounter (org.mitre.synthea.world.concepts.HealthRecord.Encounter)2 SampledDataDt (ca.uhn.fhir.model.dstu2.composite.SampledDataDt)1 Media (ca.uhn.fhir.model.dstu2.resource.Media)1 Observation (ca.uhn.fhir.model.dstu2.resource.Observation)1 HashBasedTable (com.google.common.collect.HashBasedTable)1