Skip to content

Index a patient profile at the first entry of the requested cohort - #2599

Open
DG47 wants to merge 1 commit into
OHDSI:masterfrom
DG47:fix/person-profile-earliest-cohort-entry
Open

DG47 wants to merge 1 commit into
OHDSI:masterfrom
DG47:fix/person-profile-earliest-cohort-entry

Conversation

@DG47

@DG47 DG47 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #2598

What was wrong

When a patient profile is requested for a cohort (GET /{sourceKey}/person/{personId}?cohort={id}), PersonService.getPersonProfile uses the first row from person/sql/getCohorts.sql whose cohort id matches as the index date. That query has no ORDER BY, and cohort generation does not write a person's entries in date order. For a person with several entries in the cohort, the profile can therefore be indexed at any of them. Day 0, every record's start/end day, the observation period offsets and the age at index all follow whichever row the database returns first, and they can differ between databases or change after the cohort is regenerated.

What changed

getCohorts.sql now orders the person's cohort rows by cohort_definition_id, cohort_start_date. The existing findFirst() then picks the person's first entry in the requested cohort. The cohorts list in the response comes back in a stable order too. PersonService.java itself is unchanged.

How it was tested

Added PersonServiceCohortIndexTest, which builds a small CDM on the embedded Postgres test database. Person 1 has two entries in cohort 1 (2012-06-01 and 2018-03-01, with the 2018 row stored first) and one earlier entry in another cohort. The test checks that the profile for cohort 1 is indexed at 2012-06-01 (age at index, record start days, observation period offset).

  • Before the change: expected: 42 but was: 48 (indexed at the 2018 entry).
  • After the change: passes.
  • mvn test -DskipUnitTests=false -DskipITtests=true: Tests run: 170, Failures: 0, Errors: 0, Skipped: 5.

When a profile is opened for a cohort (GET /{sourceKey}/person/{personId}
?cohort={id}), PersonService takes the first row of getCohorts.sql that
matches the cohort id as the index date. That query has no ORDER BY, and
cohort generation does not write a person's entries in date order, so for
a person with several entries in the cohort the profile can be indexed at
any of them: day 0, every record's start/end day, the observation period
offsets and the age at index all follow whichever row the database
returned first, and can change between databases or after the cohort is
regenerated.

Order the cohort rows by cohort definition and start date so the profile
is indexed at the person's first entry in the cohort.

Adds a test that builds a profile on the embedded Postgres database for a
person whose later cohort entry is stored before the earlier one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Patient profile opened from a cohort is indexed at an arbitrary cohort entry when the person has several entries

1 participant