Skip to content

Commit

Permalink
Merge pull request #1385 from synthetichealth/export-filter-active-re…
Browse files Browse the repository at this point in the history
…peats

Fix export filter keeping old entries when there is an active entry of the same type
  • Loading branch information
jawalonoski authored Nov 10, 2023
2 parents 11a7411 + 574152d commit c88eb6b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/main/java/org/mitre/synthea/export/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,25 +598,15 @@ private static HealthRecord filterForExport(HealthRecord record, int yearsToKeep

long cutoffDate = endTime - Utilities.convertTime("years", yearsToKeep);
Predicate<HealthRecord.Entry> notFutureDated = e -> e.start <= endTime;
Predicate<HealthRecord.Entry> entryIsActive = e -> e.stop == 0L || e.stop > cutoffDate;

for (Encounter encounter : record.encounters) {
List<Claim.ClaimEntry> claimItems = encounter.claim.items;
// keep conditions if still active, regardless of start date
Predicate<HealthRecord.Entry> conditionActive = c -> record.conditionActive(c.type);
// or if the condition was active at any point since the cutoff date
Predicate<HealthRecord.Entry> activeWithinCutoff = c -> c.stop != 0L && c.stop > cutoffDate;
Predicate<HealthRecord.Entry> keepCondition = conditionActive.or(activeWithinCutoff);
filterEntries(encounter.conditions, claimItems, cutoffDate, endTime, keepCondition);
// keep a condition if it was active at any point since the cutoff date
filterEntries(encounter.conditions, claimItems, cutoffDate, endTime, entryIsActive);

// allergies are essentially the same as conditions
// But we need to redefine all of the predicates, because we are talking about Allergies as
// opposed to Entries... You would think that it would work... but generics are hard
Predicate<HealthRecord.Allergy> allergyActive = c -> record.allergyActive(c.type);
// or if the condition was active at any point since the cutoff date
Predicate<HealthRecord.Allergy> allergyActiveWithinCutoff =
c -> c.stop != 0L && c.stop > cutoffDate;
Predicate<HealthRecord.Allergy> keepAllergy = allergyActive.or(allergyActiveWithinCutoff);
filterEntries(encounter.allergies, claimItems, cutoffDate, endTime, keepAllergy);
filterEntries(encounter.allergies, claimItems, cutoffDate, endTime, entryIsActive);

// some of the "future death" logic could potentially add a future-dated death certificate
Predicate<Observation> isCauseOfDeath =
Expand All @@ -633,14 +623,12 @@ private static HealthRecord filterForExport(HealthRecord record, int yearsToKeep
filterEntries(encounter.procedures, claimItems, cutoffDate, endTime, null);

// keep medications if still active, regardless of start date
filterEntries(encounter.medications, claimItems, cutoffDate, endTime,
med -> record.medicationActive(med.type));
filterEntries(encounter.medications, claimItems, cutoffDate, endTime, entryIsActive);

filterEntries(encounter.immunizations, claimItems, cutoffDate, endTime, null);

// keep careplans if they are still active, regardless of start date
filterEntries(encounter.careplans, claimItems, cutoffDate, endTime,
cp -> record.careplanActive(cp.type));
filterEntries(encounter.careplans, claimItems, cutoffDate, endTime, entryIsActive);
}

// if ANY of these are not empty, the encounter is not empty
Expand Down Expand Up @@ -675,7 +663,7 @@ private static HealthRecord filterForExport(HealthRecord record, int yearsToKeep
*/
private static <E extends HealthRecord.Entry> void filterEntries(List<E> entries,
List<Claim.ClaimEntry> claimItems, long cutoffDate,
long endTime, Predicate<E> keepFunction) {
long endTime, Predicate<? super E> keepFunction) {

Iterator<E> iterator = entries.iterator();
// iterator allows us to use the remove() method
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/mitre/synthea/export/ExporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mitre.synthea.TestHelper;
import org.mitre.synthea.engine.Generator;
import org.mitre.synthea.helpers.Config;
import org.mitre.synthea.helpers.Utilities;
import org.mitre.synthea.modules.DeathModule;
import org.mitre.synthea.world.agents.PayerManager;
import org.mitre.synthea.world.agents.Person;
Expand Down Expand Up @@ -257,4 +258,24 @@ public void testExportFilterShouldFilterClaimItems() {
assertEquals("something_permanent", record.encounters.get(0).claim.items.get(0).entry.type);
}

@Test
public void testExportFilterShouldNotKeepOldStuffWhenActiveOfSameType() {
// create old encounters with the same repeated condition, ended
long oneWeek = Utilities.convertTime("weeks", 1);
for (int i = 0; i < 3; i++) {
record.encounterStart(time - years(10 - i), EncounterType.EMERGENCY);
record.conditionStart(time - years(10 - i), "viral_sinusitis");
record.conditionEnd(time - years(10 - i) + oneWeek, "viral_sinusitis");
}

// create a recent encounter with the same condition, still active
record.encounterStart(time - oneWeek, EncounterType.EMERGENCY);
record.conditionStart(time - oneWeek, "viral_sinusitis");

Person filtered = Exporter.filterForExport(patient, yearsToKeep, endTime);

assertEquals(1, filtered.record.encounters.size());
assertEquals(1, filtered.record.encounters.get(0).conditions.size());
assertEquals("viral_sinusitis", filtered.record.encounters.get(0).conditions.get(0).type);
}
}

0 comments on commit c88eb6b

Please sign in to comment.