Search in sources :

Example 1 with Generator

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

the class PersonTest method testSerializationAndDeserialization.

@Test
public void testSerializationAndDeserialization() throws Exception {
    // serialization
    if (Boolean.valueOf(Config.get("physiology.generators.enabled", "false"))) {
        System.out.println("Skipping test PersonTest.testSerializationAndDeserialization");
        System.out.println("Set config physiology.generators.enabled=false to enable this test");
        return;
    }
    // Generate a filled-out patient record to test on
    Generator.GeneratorOptions opts = new Generator.GeneratorOptions();
    opts.population = 1;
    opts.minAge = 50;
    opts.maxAge = 100;
    Generator generator = new Generator(opts);
    int personSeed = 0;
    Random randomForDemographics = new Random(personSeed);
    Map<String, Object> demoAttributes = generator.randomDemographics(randomForDemographics);
    Person original = generator.createPerson(0, demoAttributes);
    Person rehydrated = serializeAndDeserialize(original);
    // Compare the original to the serialized+deserialized version
    assertEquals(original.randInt(), rehydrated.randInt());
    assertEquals(original.seed, rehydrated.seed);
    assertEquals(original.populationSeed, rehydrated.populationSeed);
    assertEquals(original.symptoms.keySet(), rehydrated.symptoms.keySet());
    assertEquals(original.getOnsetConditionRecord().getSources().keySet(), rehydrated.getOnsetConditionRecord().getSources().keySet());
    assertEquals(original.hasMultipleRecords, rehydrated.hasMultipleRecords);
    assertEquals(original.attributes.keySet(), rehydrated.attributes.keySet());
    assertEquals(original.vitalSigns.keySet(), rehydrated.vitalSigns.keySet());
    assertEquals(original.chronicMedications.keySet(), rehydrated.chronicMedications.keySet());
    assertEquals(original.hasMultipleRecords, rehydrated.hasMultipleRecords);
    if (original.hasMultipleRecords) {
        assertEquals(original.records.keySet(), rehydrated.records.keySet());
    }
    assertEquals(original.coverage.getPlanHistory().size(), rehydrated.coverage.getPlanHistory().size());
}
Also used : GeneratorOptions(org.mitre.synthea.engine.Generator.GeneratorOptions) GeneratorOptions(org.mitre.synthea.engine.Generator.GeneratorOptions) Random(java.util.Random) Generator(org.mitre.synthea.engine.Generator) Test(org.junit.Test)

Example 2 with Generator

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

the class PersonTest method testPersonCcdaRecreation.

@Test()
public void testPersonCcdaRecreation() throws Exception {
    TestHelper.loadTestProperties();
    TestHelper.exportOff();
    Generator.DEFAULT_STATE = Config.get("test_state.default", "Massachusetts");
    Config.set("exporter.ccda.export", "true");
    SimpleDateFormat format = new SimpleDateFormat("YYYYMMDD");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    GeneratorOptions options = new GeneratorOptions();
    options.clinicianSeed = 9L;
    options.seed = 9L;
    options.referenceTime = format.parse("20200704").getTime();
    options.overflow = false;
    Generator generator = new Generator(options);
    List<List<String>> fileContents = new ArrayList<>();
    // the file names should be identical
    for (int i = 0; i < 2; i++) {
        File tempOutputFolder = tempFolder.newFolder();
        Config.set("exporter.baseDirectory", tempOutputFolder.toString());
        generator.generatePerson(0, 42L);
        // Check that the output files exist
        File expectedExportFolder = tempOutputFolder.toPath().resolve("ccda").toFile();
        assertTrue(expectedExportFolder.exists() && expectedExportFolder.isDirectory());
        for (File txtFile : expectedExportFolder.listFiles()) {
            if (!txtFile.getName().endsWith(".xml")) {
                continue;
            }
            fileContents.add(Files.readAllLines(txtFile.toPath()));
        }
    }
    // Check that there are exactly two files
    assertEquals("Expected 2 files, found " + fileContents.size(), 2, fileContents.size());
    // Check that the two files are identical
    for (int i = 0; i < fileContents.get(0).size(); i++) {
        assertEquals(fileContents.get(0).get(i), fileContents.get(1).get(i));
    }
}
Also used : GeneratorOptions(org.mitre.synthea.engine.Generator.GeneratorOptions) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Generator(org.mitre.synthea.engine.Generator) Test(org.junit.Test)

Example 3 with Generator

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

the class SymptomTextExporterTest method testSymptomTextExport.

@Test
public void testSymptomTextExport() throws Exception {
    TestHelper.loadTestProperties();
    Generator.DEFAULT_STATE = Config.get("test_state.default", "Massachusetts");
    File tempOutputFolder = tempFolder.newFolder();
    Config.set("exporter.baseDirectory", tempOutputFolder.toString());
    int numberOfPeople = 10;
    Generator generator = new Generator(numberOfPeople);
    generator.options.overflow = false;
    for (int i = 0; i < numberOfPeople; i++) {
        TestHelper.exportOff();
        Person person = generator.generatePerson(i);
        Config.set("exporter.symptoms.text.export", "true");
        Exporter.export(person, System.currentTimeMillis());
    }
    // if we get here we at least had no exceptions
    File expectedExportFolder = tempOutputFolder.toPath().resolve("symptoms/text").toFile();
    assertTrue(expectedExportFolder.exists() && expectedExportFolder.isDirectory());
    int count = 0;
    for (File txtFile : expectedExportFolder.listFiles()) {
        if (!txtFile.getName().endsWith(".txt")) {
            continue;
        }
        count++;
    }
    assertEquals("Expected " + numberOfPeople + " files in the output directory, found " + count, numberOfPeople, count);
}
Also used : File(java.io.File) Person(org.mitre.synthea.world.agents.Person) Generator(org.mitre.synthea.engine.Generator) Test(org.junit.Test)

Example 4 with Generator

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

the class ExportBreakerTest method runUntilBreaking.

@Test
public void runUntilBreaking() throws Exception {
    int numberOfPeople = 10000;
    Generator generator = new Generator(numberOfPeople);
    generator.options.overflow = false;
    TestHelper.loadTestProperties();
    Generator.DEFAULT_STATE = Config.get("test_state.default", "Massachusetts");
    Config.set("exporter.baseDirectory", tempFolder.newFolder().toString());
    // This is currently set up for CCDA testing, but it should be easy to remove this and do FHIR
    // testing or whatever else needs to be run.
    CDAUtil.loadPackages();
    TestHelper.exportOff();
    Config.set("exporter.ccda.export", "true");
    // I picked 6 because it allows me to run tests on my 8 core machine, but have it still be
    // usable for other things.
    ExecutorService service = Executors.newFixedThreadPool(6);
    for (int i = 0; i < numberOfPeople; i++) {
        final int personIndex = i;
        service.submit(() -> {
            try {
                Person p = generator.generatePerson(personIndex);
                // Export work goes here
                String ccdaXml = CCDAExporter.export(p, System.currentTimeMillis());
                InputStream inputStream = IOUtils.toInputStream(ccdaXml, "UTF-8");
                // Validation work goes here
                CDAUtil.load(inputStream, new BasicValidationHandler() {

                    public void handleError(Diagnostic diagnostic) {
                        System.out.println("ERROR: " + diagnostic.getMessage());
                        System.out.println(ccdaXml);
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
    service.shutdown();
    service.awaitTermination(1, TimeUnit.HOURS);
}
Also used : InputStream(java.io.InputStream) ExecutorService(java.util.concurrent.ExecutorService) Diagnostic(org.eclipse.emf.common.util.Diagnostic) Person(org.mitre.synthea.world.agents.Person) BasicValidationHandler(org.eclipse.mdht.uml.cda.util.BasicValidationHandler) Generator(org.mitre.synthea.engine.Generator) Test(org.junit.Test)

Example 5 with Generator

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

the class HospitalExporterTestR4 method testFHIRExport.

@Test
public void testFHIRExport() throws Exception {
    FhirContext ctx = FhirR4.getContext();
    FhirValidator validator = ctx.newValidator();
    validator.setValidateAgainstStandardSchema(true);
    validator.setValidateAgainstStandardSchematron(true);
    File tempOutputFolder = tempFolder.newFolder();
    Config.set("exporter.baseDirectory", tempOutputFolder.toString());
    Config.set("exporter.hospital.fhir.export", "true");
    Config.set("exporter.fhir.transaction_bundle", "true");
    // set this manually, in case it has already been loaded.
    FhirR4.TRANSACTION_BUNDLE = true;
    TestHelper.loadTestProperties();
    Generator.DEFAULT_STATE = Config.get("test_state.default", "Massachusetts");
    Location location = new Location(Generator.DEFAULT_STATE, null);
    Provider.clear();
    Provider.loadProviders(location, 1L);
    assertNotNull(Provider.getProviderList());
    assertFalse(Provider.getProviderList().isEmpty());
    Provider.getProviderList().get(0).incrementEncounters(EncounterType.WELLNESS, 0);
    HospitalExporterR4.export(new Generator(), 0L);
    File expectedExportFolder = tempOutputFolder.toPath().resolve("fhir").toFile();
    assertTrue(expectedExportFolder.exists() && expectedExportFolder.isDirectory());
    File expectedExportFile = expectedExportFolder.toPath().resolve("hospitalInformation0.json").toFile();
    assertTrue(expectedExportFile.exists() && expectedExportFile.isFile());
    FileReader fileReader = new FileReader(expectedExportFile.getPath());
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuilder fhirJson = new StringBuilder();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        fhirJson.append(line);
    }
    bufferedReader.close();
    IBaseResource resource = ctx.newJsonParser().parseResource(fhirJson.toString());
    ValidationResult result = validator.validateWithResult(resource);
    if (result.isSuccessful() == false) {
        for (SingleValidationMessage message : result.getMessages()) {
            System.out.println(message.getSeverity().toString() + ": " + message.getMessage());
        }
    }
    assertTrue(result.isSuccessful());
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SingleValidationMessage(ca.uhn.fhir.validation.SingleValidationMessage) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FhirValidator(ca.uhn.fhir.validation.FhirValidator) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ValidationResult(ca.uhn.fhir.validation.ValidationResult) File(java.io.File) Location(org.mitre.synthea.world.geography.Location) Generator(org.mitre.synthea.engine.Generator) Test(org.junit.Test)

Aggregations

Generator (org.mitre.synthea.engine.Generator)23 Test (org.junit.Test)20 File (java.io.File)19 GeneratorOptions (org.mitre.synthea.engine.Generator.GeneratorOptions)11 SimpleDateFormat (java.text.SimpleDateFormat)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Location (org.mitre.synthea.world.geography.Location)5 Person (org.mitre.synthea.world.agents.Person)4 ExporterRuntimeOptions (org.mitre.synthea.export.Exporter.ExporterRuntimeOptions)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 LinkedHashMap (java.util.LinkedHashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 RandomNumberGenerator (org.mitre.synthea.helpers.RandomNumberGenerator)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 FhirValidator (ca.uhn.fhir.validation.FhirValidator)1 SingleValidationMessage (ca.uhn.fhir.validation.SingleValidationMessage)1 ValidationResult (ca.uhn.fhir.validation.ValidationResult)1 Gson (com.google.gson.Gson)1