-
Notifications
You must be signed in to change notification settings - Fork 1
Features/ibis custom eras #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4b89967
implementation of custom end era logic in ibis layer
azimov 09748f2
more tests around custom era logic for parity with java
azimov 8f7c4d2
fix(execution): CustomEra window partitions on (person_id, event_id) …
azimov dd20010
fixes and improved tests;
azimov f3331bf
More fixes from PR feedback
azimov 3cc0c76
Updated test fixtures for cleaner and more comprehensive tests
azimov 43f16b9
Merge branch 'develop' into features/ibis-custom-eras
azimov c2e9aeb
Filter only drug exposures to people who have primary events
azimov cd73de4
Fixes for versioning causing actions to fail
azimov 20bf1b6
code format
azimov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import ibis | ||
|
|
||
| from ..plan.schema import PERSON_ID, START_DATE | ||
| from .end_strategy import _replace_end_date, attach_observation_bounds | ||
|
|
||
|
|
||
| def _compute_exposure_end_date(table, *, days_supply_override: int | None): | ||
| start = table["drug_exposure_start_date"].cast("date") | ||
|
|
||
| if days_supply_override is not None: | ||
| return start + ibis.interval(days=days_supply_override) | ||
|
|
||
| raw_end = ( | ||
| table["drug_exposure_end_date"].cast("date") | ||
| if "drug_exposure_end_date" in table.columns | ||
| else ibis.null().cast("date") | ||
| ) | ||
| days_supply = ( | ||
| table["days_supply"].cast("int64") if "days_supply" in table.columns else ibis.null().cast("int64") | ||
| ) | ||
| supply_end = start + days_supply.as_interval("D") | ||
|
|
||
| return ibis.coalesce(raw_end, supply_end, start + ibis.interval(days=1)) | ||
|
|
||
|
|
||
| def _compute_eras(exposures, *, gap_days: int, offset: int): | ||
| padded = exposures.mutate( | ||
| _padded_end=(exposures._exposure_end + ibis.interval(days=int(gap_days + offset))) | ||
| ) | ||
|
|
||
| ordering = [ | ||
| padded.start_date, | ||
| padded._padded_end.desc(), | ||
| padded._exposure_end.desc(), | ||
| ] | ||
|
|
||
| cumulative_window = ibis.cumulative_window(group_by=padded.person_id, order_by=ordering) | ||
| ordered_window = ibis.window(group_by=padded.person_id, order_by=ordering) | ||
|
|
||
| with_cummax = padded.mutate(_cummax_padded_end=padded._padded_end.max().over(cumulative_window)) | ||
|
|
||
| with_prev = with_cummax.mutate(_prev_max=with_cummax._cummax_padded_end.lag().over(ordered_window)) | ||
|
|
||
| marked = with_prev.mutate( | ||
| _is_new=ibis.ifelse( | ||
| with_prev._prev_max.isnull() | (with_prev._prev_max < with_prev.start_date), | ||
| ibis.literal(1, type="int64"), | ||
| ibis.literal(0, type="int64"), | ||
| ) | ||
| ) | ||
|
|
||
| group_window = ibis.cumulative_window( | ||
| group_by=marked.person_id, | ||
| order_by=[ | ||
| marked.start_date, | ||
| marked._padded_end.desc(), | ||
| marked._exposure_end.desc(), | ||
| marked._is_new.desc(), | ||
| ], | ||
| ) | ||
| era_indexed = marked.mutate(_era_id=marked._is_new.sum().over(group_window)) | ||
|
|
||
| collapsed = era_indexed.group_by(era_indexed.person_id, era_indexed._era_id).aggregate( | ||
| era_start_date=era_indexed.start_date.min(), | ||
| _max_exposure_end=era_indexed._exposure_end.max(), | ||
| ) | ||
|
|
||
| return collapsed.select( | ||
| collapsed.person_id.cast("int64").name(PERSON_ID), | ||
| collapsed.era_start_date.cast("date").name("era_start_date"), | ||
| (collapsed._max_exposure_end + ibis.interval(days=int(offset))).cast("date").name("era_end_date"), | ||
| ) | ||
|
|
||
|
|
||
| def compute_drug_eras( | ||
| ctx, | ||
| *, | ||
| drug_codeset_id: int, | ||
| gap_days: int, | ||
| offset: int, | ||
| days_supply_override: int | None, | ||
| cohort_person_ids=None, | ||
| ): | ||
| concept_ids = ctx.concept_ids_for_codeset(drug_codeset_id) | ||
|
|
||
| if not concept_ids: | ||
| de = ctx.table("drug_exposure") | ||
| return de.filter(ibis.literal(False)).select( | ||
| de.person_id.cast("int64").name(PERSON_ID), | ||
| ibis.null().cast("date").name("era_start_date"), | ||
| ibis.null().cast("date").name("era_end_date"), | ||
| ) | ||
|
|
||
| de = ctx.table("drug_exposure") | ||
| if cohort_person_ids is not None: | ||
| de = de.semi_join( | ||
| cohort_person_ids, | ||
| predicates=[de.person_id == cohort_person_ids.person_id], | ||
| ) | ||
|
|
||
| if "drug_source_concept_id" in de.columns: | ||
| filtered = de.filter( | ||
| de.drug_concept_id.isin(concept_ids) | de.drug_source_concept_id.isin(concept_ids) | ||
| ) | ||
| else: | ||
| filtered = de.filter(de.drug_concept_id.isin(concept_ids)) | ||
|
|
||
| prepared = filtered.select( | ||
| filtered.person_id.cast("int64").name("person_id"), | ||
| filtered.drug_exposure_start_date.cast("date").name("start_date"), | ||
| _compute_exposure_end_date(filtered, days_supply_override=days_supply_override).name("_exposure_end"), | ||
| ) | ||
|
|
||
| return _compute_eras(prepared, gap_days=gap_days, offset=offset) | ||
|
|
||
|
|
||
| def apply_custom_era_strategy(events, strategy, ctx): | ||
| payload = strategy.payload | ||
| drug_codeset_id = payload["drug_codeset_id"] | ||
| gap_days = payload["gap_days"] | ||
| offset = payload["offset"] | ||
| days_supply_override = payload.get("days_supply_override") | ||
|
|
||
| if drug_codeset_id is None: | ||
| with_bounds = attach_observation_bounds(events, ctx) | ||
| return _replace_end_date(events, with_bounds, with_bounds.op_end_date) | ||
|
|
||
| cohort_person_ids = events.select(events.person_id).distinct() | ||
|
|
||
| eras = compute_drug_eras( | ||
| ctx, | ||
| drug_codeset_id=drug_codeset_id, | ||
| gap_days=gap_days, | ||
| offset=offset, | ||
| days_supply_override=days_supply_override, | ||
| cohort_person_ids=cohort_person_ids, | ||
| ) | ||
|
|
||
| eras_for_join = eras.select( | ||
| eras.person_id.name("_era_person_id"), | ||
| eras.era_start_date, | ||
| eras.era_end_date, | ||
| ) | ||
|
|
||
| with_bounds = attach_observation_bounds(events, ctx) | ||
|
|
||
| joined = with_bounds.left_join( | ||
| eras_for_join, | ||
| predicates=[ | ||
| with_bounds.person_id == eras_for_join._era_person_id, | ||
| with_bounds[START_DATE] >= eras_for_join.era_start_date, | ||
| with_bounds[START_DATE] <= eras_for_join.era_end_date, | ||
| ], | ||
| ) | ||
|
|
||
| event_window = ibis.window( | ||
| group_by=[joined.person_id, joined.event_id], | ||
| order_by=[joined.era_end_date.asc()], | ||
| ) | ||
| ranked = joined.mutate(_rn=ibis.row_number().over(event_window)) | ||
| one_per_event = ranked.filter(ranked._rn == 0) | ||
|
|
||
| effective_end = ibis.coalesce( | ||
| one_per_event.era_end_date, | ||
| one_per_event.op_end_date, | ||
| ) | ||
| final_end = ibis.least(effective_end, one_per_event.op_end_date) | ||
|
|
||
| return _replace_end_date(events, one_per_event, final_end) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are filtering the whole
drug_exposuretable here for theseconcept_ids, isn't it better to first filter to people having primary events/included events. Like CIRCE-BE does:https://github.com/OHDSI/circe-be/blob/498893689a9cf4f09c2a43cc893bb01116db7184/src/main/resources/resources/cohortdefinition/sql/customEraStrategy.sql#L3-L13