Skip to content
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

Adds a multi-stage integration test for pre-processing. #167

Merged
merged 18 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9bb19bf
Separated input data preparation and renamed it to make tests cleaner…
mmcdermott Aug 14, 2024
9cf0bc4
Making stage-name always be passed via the command line if it is used.
mmcdermott Aug 14, 2024
d5b0782
Updated test output syntax to support data and metadata checking and …
mmcdermott Aug 14, 2024
138eb1e
Added a multi-stage test which currently, appropriately, fails due to…
mmcdermott Aug 14, 2024
74d3ce1
Corrected #161. This makes the normalization error disappear, but mor…
mmcdermott Aug 14, 2024
5911c90
Added a graceful exit mode for cases where shards are empty. Should s…
mmcdermott Aug 14, 2024
ff46645
Using more default parameters.
mmcdermott Aug 14, 2024
9304bf7
Tests re-failing as I haven't added all the NRT tests, just the fully…
mmcdermott Aug 14, 2024
c7d56a9
Starting to track some of the intermediate outputs to identify the ta…
mmcdermott Aug 14, 2024
210a9ce
Added capability to test intermediate files and started validating fu…
mmcdermott Aug 14, 2024
747ce11
Tested outputs up to occlude outliers.
mmcdermott Aug 14, 2024
8736279
Removed broken assert_no_other_outcomes
mmcdermott Aug 14, 2024
6114c42
Validated fit normalization stage as well.
mmcdermott Aug 15, 2024
3c5c7d9
Added test for final metadata stage
mmcdermott Aug 15, 2024
4a466c0
Added normalization shard test.
mmcdermott Aug 15, 2024
2a58ec1
Added tokenization schemas test.
mmcdermott Aug 15, 2024
aeedb9e
Added tokenized output validations.
mmcdermott Aug 15, 2024
80b31fd
Tested tensorized outputs.
mmcdermott Aug 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
fit_vocabulary_indices:
is_metadata: true
ordering_method: "lexicographic"
output_dir: "${cohort_dir}"
15 changes: 12 additions & 3 deletions src/MEDS_transforms/transforms/tensorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import hydra
import polars as pl
from loguru import logger
from nested_ragged_tensors.ragged_numpy import JointNestedRaggedTensorDict
from omegaconf import DictConfig

Expand Down Expand Up @@ -87,9 +88,17 @@ def convert_to_NRT(df: pl.LazyFrame) -> JointNestedRaggedTensorDict:

time_delta_col = time_delta_cols[0]

return JointNestedRaggedTensorDict(
df.select(time_delta_col, "code", "numeric_value").collect().to_dict(as_series=False)
)
tensors_dict = df.select(time_delta_col, "code", "numeric_value").collect().to_dict(as_series=False)

if all((not v) for v in tensors_dict.values()):
logger.warning("All columns are empty. Returning an empty tensor dict.")
return JointNestedRaggedTensorDict({})

for k, v in tensors_dict.items():
if not v:
raise ValueError(f"Column {k} is empty")

return JointNestedRaggedTensorDict(tensors_dict)


@hydra.main(
Expand Down
22 changes: 11 additions & 11 deletions src/MEDS_transforms/transforms/tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,27 @@ def extract_seq_of_patient_events(df: pl.LazyFrame) -> pl.LazyFrame:
... None, datetime(2021, 1, 1), datetime(2021, 1, 1), datetime(2021, 1, 13),
... None, datetime(2021, 1, 2), datetime(2021, 1, 2)],
... "code": [100, 101, 102, 103, 200, 201, 202],
... "numeric_value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
... "numeric_value": pl.Series([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], dtype=pl.Float32)
... }).lazy()
>>> extract_seq_of_patient_events(df).collect()
shape: (2, 4)
┌────────────┬─────────────────┬───────────────────────────┬─────────────────────┐
│ patient_id ┆ time_delta_days ┆ code ┆ numeric_value │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ list[f64] ┆ list[list[f64]] ┆ list[list[f64]] │
╞════════════╪═════════════════╪═══════════════════════════╪═════════════════════╡
│ 1 ┆ [NaN, 12.0] ┆ [[101.0, 102.0], [103.0]] ┆ [[2.0, 3.0], [4.0]] │
│ 2 ┆ [NaN] ┆ [[201.0, 202.0]] ┆ [[6.0, 7.0]] │
└────────────┴─────────────────┴───────────────────────────┴─────────────────────┘
┌────────────┬─────────────────┬─────────────────────┬─────────────────────┐
│ patient_id ┆ time_delta_days ┆ code ┆ numeric_value │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ list[f32] ┆ list[list[i64]] ┆ list[list[f32]] │
╞════════════╪═════════════════╪═════════════════════╪═════════════════════╡
│ 1 ┆ [NaN, 12.0] ┆ [[101, 102], [103]] ┆ [[2.0, 3.0], [4.0]] │
│ 2 ┆ [NaN] ┆ [[201, 202]] ┆ [[6.0, 7.0]] │
└────────────┴─────────────────┴─────────────────────┴─────────────────────┘
"""

_, dynamic = split_static_and_dynamic(df)

time_delta_days_expr = (pl.col("time").diff().dt.total_seconds() / SECONDS_PER_DAY).cast(pl.Float64)
time_delta_days_expr = (pl.col("time").diff().dt.total_seconds() / SECONDS_PER_DAY).cast(pl.Float32)

return (
dynamic.group_by("patient_id", "time", maintain_order=True)
.agg(fill_to_nans("code").name.keep(), fill_to_nans("numeric_value").name.keep())
.agg(pl.col("code").name.keep(), fill_to_nans("numeric_value").name.keep())
.group_by("patient_id", maintain_order=True)
.agg(
fill_to_nans(time_delta_days_expr).alias("time_delta_days"),
Expand Down
4 changes: 2 additions & 2 deletions src/MEDS_transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def populate_stage(
'reducer_output_dir': '/c/d/metadata'}
>>> populate_stage("stage6", *args) # doctest: +NORMALIZE_WHITESPACE
{'is_metadata': False, 'data_input_dir': '/c/d/stage4',
'metadata_input_dir': '/c/d/stage5', 'output_dir': '/c/d/data', 'reducer_output_dir': None}
'metadata_input_dir': '/c/d/metadata', 'output_dir': '/c/d/data', 'reducer_output_dir': None}
>>> populate_stage("stage7", *args) # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
Expand Down Expand Up @@ -304,7 +304,7 @@ def populate_stage(
if is_first_metadata_stage:
default_metadata_input_dir = pipeline_input_metadata_dir
else:
default_metadata_input_dir = prior_metadata_stage["output_dir"]
default_metadata_input_dir = prior_metadata_stage["reducer_output_dir"]

# Now, we need to set output directories. The output directory for the stage will either be a stage
# specific output directory, or, for the last data or metadata stages, respectively, will be the global
Expand Down
2 changes: 1 addition & 1 deletion tests/test_add_time_derived_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,5 @@ def test_add_time_derived_measurements():
transform_script=ADD_TIME_DERIVED_MEASUREMENTS_SCRIPT,
stage_name="add_time_derived_measurements",
transform_stage_kwargs={"age": {"DOB_code": "DOB"}},
want_outputs=WANT_SHARDS,
want_data=WANT_SHARDS,
)
4 changes: 2 additions & 2 deletions tests/test_aggregate_code_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_aggregate_code_metadata():
transform_script=AGGREGATE_CODE_METADATA_SCRIPT,
stage_name="aggregate_code_metadata",
transform_stage_kwargs={"aggregations": AGGREGATIONS, "do_summarize_over_all_codes": True},
want_outputs=WANT_OUTPUT_CODE_METADATA_FILE,
code_metadata=MEDS_CODE_METADATA_FILE,
want_metadata=WANT_OUTPUT_CODE_METADATA_FILE,
input_code_metadata=MEDS_CODE_METADATA_FILE,
do_use_config_yaml=True,
)
4 changes: 2 additions & 2 deletions tests/test_filter_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_filter_measurements():
transform_script=FILTER_MEASUREMENTS_SCRIPT,
stage_name="filter_measurements",
transform_stage_kwargs={"min_patients_per_code": 2},
want_outputs=WANT_SHARDS,
want_data=WANT_SHARDS,
)


Expand Down Expand Up @@ -223,6 +223,6 @@ def test_match_revise_filter_measurements():
{"_matcher": {"code": "EYE_COLOR//HAZEL"}, "min_patients_per_code": 4},
],
},
want_outputs=MR_WANT_SHARDS,
want_data=MR_WANT_SHARDS,
do_use_config_yaml=True,
)
2 changes: 1 addition & 1 deletion tests/test_filter_patients.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ def test_filter_patients():
transform_script=FILTER_PATIENTS_SCRIPT,
stage_name="filter_patients",
transform_stage_kwargs={"min_events_per_patient": 5},
want_outputs=WANT_SHARDS,
want_data=WANT_SHARDS,
)
2 changes: 1 addition & 1 deletion tests/test_fit_vocabulary_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def test_fit_vocabulary_indices_with_default_stage_config():
transform_script=FIT_VOCABULARY_INDICES_SCRIPT,
stage_name="fit_vocabulary_indices",
transform_stage_kwargs=None,
want_outputs=parse_code_metadata_csv(WANT_CSV),
want_metadata=parse_code_metadata_csv(WANT_CSV),
)
Loading
Loading