Skip to content

Commit fdf44d7

Browse files
author
zyu49
committed
fix: Replace HashMap with ordered collections to fix indeterministic PersonTest
- Use LinkedHashSet in ClinicalNoteExporter for deterministic clinical note ordering - Use ConcurrentSkipListMap for Person.chronicMedications - Use LinkedHashMap in HealthRecord and Demographics
2 parents c3da5c6 + 7cf0e42 commit fdf44d7

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/main/java/org/mitre/synthea/export/ClinicalNoteExporter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.StringWriter;
88
import java.util.HashSet;
9+
import java.util.LinkedHashSet;
910
import java.util.Set;
1011

1112
import org.mitre.synthea.modules.LifecycleModule;
@@ -67,10 +68,10 @@ public static String export(Person person) {
6768
public static String export(Person person, Encounter encounter) {
6869
// The export templates fill in the record by accessing the attributes
6970
// of the Person, so we add a few attributes just for the purposes of export.
70-
Set<String> activeAllergies = new HashSet<String>();
71-
Set<String> activeConditions = new HashSet<String>();
72-
Set<String> activeMedications = new HashSet<String>();
73-
Set<String> activeProcedures = new HashSet<String>();
71+
Set<String> activeAllergies = new LinkedHashSet<String>();
72+
Set<String> activeConditions = new LinkedHashSet<String>();
73+
Set<String> activeMedications = new LinkedHashSet<String>();
74+
Set<String> activeProcedures = new LinkedHashSet<String>();
7475

7576
// need to loop through record until THIS encounter
7677
// to get previous data, since "present" is what is present

src/main/java/org/mitre/synthea/world/agents/Person.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Set;
1818
import java.util.UUID;
1919
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.concurrent.ConcurrentSkipListMap;
2021
import java.util.stream.Collectors;
2122

2223
import org.apache.commons.lang3.tuple.Pair;
@@ -254,10 +255,10 @@ public Person(long seed) {
254255
/* initialized the onsetConditions field */
255256
onsetConditionRecord = new ExpressedConditionRecord(this);
256257
/* Chronic Medications which will be renewed at each Wellness Encounter */
257-
chronicMedications = new ConcurrentHashMap<String, HealthRecord.Medication>();
258+
chronicMedications = new ConcurrentSkipListMap<String, HealthRecord.Medication>();
258259
hasMultipleRecords = Config.getAsBoolean("exporter.split_records", false);
259260
if (hasMultipleRecords) {
260-
records = new ConcurrentHashMap<String, HealthRecord>();
261+
records = new ConcurrentSkipListMap<String, HealthRecord>();
261262
}
262263
this.initializeDefaultHealthRecords();
263264
coverage = new CoverageRecord(this);

src/main/java/org/mitre/synthea/world/concepts/HealthRecord.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.ArrayList;
1616
import java.util.Date;
1717
import java.util.HashMap;
18+
import java.util.LinkedHashMap;
1819
import java.util.HashSet;
1920
import java.util.Iterator;
2021
import java.util.LinkedHashSet;
@@ -1054,7 +1055,7 @@ public Allergy(long start, String type) {
10541055
public HealthRecord(Person person) {
10551056
this.person = person;
10561057
encounters = new ArrayList<Encounter>();
1057-
present = new HashMap<String, Entry>();
1058+
present = new LinkedHashMap<String, Entry>();
10581059
if (person.attributes.get(Person.HOUSEHOLD) != null) {
10591060
this.demographicsAtRecordCreation = new HashMap<String,Object>(person.attributes);
10601061
}

src/main/java/org/mitre/synthea/world/geography/Demographics.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.Serializable;
88
import java.util.Arrays;
99
import java.util.HashMap;
10+
import java.util.LinkedHashMap;
1011
import java.util.List;
1112
import java.util.Map;
1213

@@ -266,7 +267,7 @@ public String languageFromRaceAndEthnicity(String race, String ethnicity,
266267
public int pickIncome(RandomNumberGenerator random) {
267268
// lazy-load in case this randomcollection isn't necessary
268269
if (incomeDistribution == null) {
269-
Map<String, Double> tempIncome = new HashMap<>(income);
270+
Map<String, Double> tempIncome = new LinkedHashMap<>(income);
270271
tempIncome.remove("mean");
271272
tempIncome.remove("median");
272273
incomeDistribution = buildRandomCollectionFromMap(tempIncome);
@@ -486,7 +487,7 @@ private static Demographics csvLineToDemographics(Map<String,String> line) {
486487
d.state = line.get("STNAME");
487488
d.county = line.get("CTYNAME");
488489

489-
d.ages = new HashMap<String, Double>();
490+
d.ages = new LinkedHashMap<String, Double>();
490491

491492
int i = 1;
492493
for (String ageGroup : CSV_AGE_GROUPS) {
@@ -497,13 +498,13 @@ private static Demographics csvLineToDemographics(Map<String,String> line) {
497498
}
498499
nonZeroDefaults(d.ages);
499500

500-
d.gender = new HashMap<String, Double>();
501+
d.gender = new LinkedHashMap<String, Double>();
501502
d.gender.put("male", Double.parseDouble(line.get("TOT_MALE")));
502503
d.gender.put("female", Double.parseDouble(line.get("TOT_FEMALE")));
503504
nonZeroDefaults(d.gender);
504505

505506
double percentageTotal = 0;
506-
d.race = new HashMap<String, Double>();
507+
d.race = new LinkedHashMap<String, Double>();
507508
for (String race : CSV_RACES) {
508509
double percentage = Double.parseDouble(line.get(race));
509510
d.race.put(race.toLowerCase(), percentage);
@@ -524,7 +525,7 @@ private static Demographics csvLineToDemographics(Map<String,String> line) {
524525
}
525526
nonZeroDefaults(d.race);
526527

527-
d.income = new HashMap<String, Double>();
528+
d.income = new LinkedHashMap<String, Double>();
528529
for (String income : CSV_INCOMES) {
529530
String incomeString = line.get(income);
530531
if (incomeString.isEmpty()) {
@@ -536,7 +537,7 @@ private static Demographics csvLineToDemographics(Map<String,String> line) {
536537
}
537538
nonZeroDefaults(d.income);
538539

539-
d.education = new HashMap<String, Double>();
540+
d.education = new LinkedHashMap<String, Double>();
540541
for (String education : CSV_EDUCATIONS) {
541542
String educationString = line.get(education);
542543
if (educationString.isEmpty()) {

0 commit comments

Comments
 (0)