-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into jakdam-fa-adapt-eval
- Loading branch information
Showing
28 changed files
with
201 additions
and
194 deletions.
There are no files selected for viewing
This file contains 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 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
65 changes: 65 additions & 0 deletions
65
psycop/common/feature_generation/sequences/cohort_definer_to_prediction_times.py
This file contains 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,65 @@ | ||
import datetime as dt | ||
from collections import defaultdict | ||
|
||
import polars as pl | ||
|
||
from psycop.common.cohort_definition import CohortDefiner | ||
from psycop.common.data_structures.patient import Patient | ||
from psycop.common.data_structures.prediction_time import PredictionTime | ||
|
||
|
||
class CohortToPredictionTimes: | ||
def __init__(self, cohort_definer: CohortDefiner, patient_objects: list[Patient]): | ||
self.cohort_definer = cohort_definer | ||
self.patients = patient_objects | ||
|
||
@staticmethod | ||
def _polars_dataframe_to_patient_timestamp_mapping( | ||
dataframe: pl.DataFrame, | ||
id_col_name: str, | ||
patient_timestamp_col_name: str, | ||
) -> dict[str | int, list[dt.datetime]]: | ||
timestamp_dicts = dataframe.iter_rows(named=True) | ||
|
||
patient_to_prediction_times = defaultdict(list) | ||
for prediction_time_dict in timestamp_dicts: | ||
patient_id = prediction_time_dict[id_col_name] | ||
patient_to_prediction_times[patient_id].append( | ||
prediction_time_dict[patient_timestamp_col_name], | ||
) | ||
|
||
return patient_to_prediction_times | ||
|
||
def create_prediction_times( | ||
self, | ||
lookbehind: dt.timedelta, | ||
lookahead: dt.timedelta, | ||
) -> tuple[PredictionTime]: | ||
outcome_timestamps = self._polars_dataframe_to_patient_timestamp_mapping( | ||
dataframe=self.cohort_definer.get_outcome_timestamps(), | ||
id_col_name="dw_ek_borger", | ||
patient_timestamp_col_name="timestamp", | ||
) | ||
prediction_timestamps = self._polars_dataframe_to_patient_timestamp_mapping( | ||
dataframe=self.cohort_definer.get_filtered_prediction_times_bundle().prediction_times, | ||
id_col_name="dw_ek_borger", | ||
patient_timestamp_col_name="timestamp", | ||
) | ||
|
||
prediction_times = [] | ||
for patient in self.patients: | ||
pt_outcome_timestamps = outcome_timestamps.get(patient.patient_id) | ||
|
||
if pt_outcome_timestamps is not None: | ||
outcome_timestamp = pt_outcome_timestamps[0] | ||
else: | ||
outcome_timestamp = None | ||
|
||
prediction_times += patient.to_prediction_times( | ||
lookbehind=lookbehind, | ||
lookahead=lookahead, | ||
outcome_timestamp=outcome_timestamp, | ||
prediction_timestamps=prediction_timestamps[patient.patient_id], | ||
) | ||
|
||
return tuple(prediction_times) |
53 changes: 53 additions & 0 deletions
53
psycop/common/feature_generation/sequences/test_cohort_definer_to_patients.py
This file contains 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,53 @@ | ||
import datetime as dt | ||
|
||
import polars as pl | ||
|
||
from psycop.common.cohort_definition import CohortDefiner, FilteredPredictionTimeBundle | ||
from psycop.common.data_structures.test_patient import get_test_patient | ||
from psycop.common.feature_generation.sequences.cohort_definer_to_prediction_times import ( | ||
CohortToPredictionTimes, | ||
) | ||
from psycop.common.test_utils.str_to_df import str_to_pl_df | ||
|
||
|
||
class MockCohortDefiner(CohortDefiner): | ||
@staticmethod | ||
def get_filtered_prediction_times_bundle() -> FilteredPredictionTimeBundle: | ||
df = str_to_pl_df( | ||
"""dw_ek_borger,timestamp | ||
1,2021-01-01 | ||
2,2022-01-01 | ||
""", | ||
) | ||
return FilteredPredictionTimeBundle( | ||
prediction_times=df, | ||
filter_steps=[], | ||
) | ||
|
||
@staticmethod | ||
def get_outcome_timestamps() -> pl.DataFrame: | ||
df = str_to_pl_df( | ||
"""dw_ek_borger,timestamp | ||
1,2021-01-02 | ||
""", | ||
) | ||
return df | ||
|
||
|
||
def test_polars_dataframe_to_dict(): | ||
"""Test that each prediction time is mapped to the correct patient.""" | ||
prediction_times = CohortToPredictionTimes( | ||
cohort_definer=MockCohortDefiner(), | ||
patient_objects=[ | ||
get_test_patient(patient_id=1), | ||
get_test_patient(patient_id=2), | ||
], | ||
).create_prediction_times( | ||
lookbehind=dt.timedelta(days=1), | ||
lookahead=dt.timedelta(days=1), | ||
) | ||
|
||
assert len(prediction_times) == 2 | ||
patient_1 = list(filter(lambda x: x.patient.patient_id == 1, prediction_times))[0] | ||
assert patient_1.prediction_timestamp == dt.datetime(2021, 1, 1) | ||
# The rest of the prediction time creation logic is tested in the patient object tests |
121 changes: 0 additions & 121 deletions
121
...inpatient/model_evaluation/application/pipelines/performance/confusion_matrix_pipeline.py
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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.