custom_filter_measurements
A polars-to-polars transformation function for filtering subjects by sequence length.
filter_measurements_fntr(stage_cfg, code_metadata, code_modifiers=None)
Returns a function that filters subject events to only encompass those with a set of permissible codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
The input DataFrame. |
required | |
stage_cfg |
DictConfig
|
The configuration for the code filtering stage. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[LazyFrame], LazyFrame]
|
The processed DataFrame. |
Examples:
>>> code_metadata_df = pl.DataFrame({
... "code": ["A", "A", "B", "C"],
... "modifier1": [1, 2, 1, 2],
... "code/n_subjects": [2, 1, 3, 2],
... "code/n_occurrences": [4, 5, 3, 2],
... })
>>> data = pl.DataFrame({
... "subject_id": [1, 1, 2, 2],
... "code": ["A", "B", "A", "C"],
... "modifier1": [1, 1, 2, 2],
... }).lazy()
>>> stage_cfg = DictConfig({"min_subjects_per_code": 2, "min_occurrences_per_code": 3})
>>> fn = filter_measurements_fntr(stage_cfg, code_metadata_df, ["modifier1"])
>>> fn(data).collect()
shape: (2, 3)
┌────────────┬──────┬───────────┐
│ subject_id ┆ code ┆ modifier1 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞════════════╪══════╪═══════════╡
│ 1 ┆ A ┆ 1 │
│ 1 ┆ B ┆ 1 │
└────────────┴──────┴───────────┘
>>> stage_cfg = DictConfig({"min_subjects_per_code": 1, "min_occurrences_per_code": 4})
>>> fn = filter_measurements_fntr(stage_cfg, code_metadata_df, ["modifier1"])
>>> fn(data).collect()
shape: (2, 3)
┌────────────┬──────┬───────────┐
│ subject_id ┆ code ┆ modifier1 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞════════════╪══════╪═══════════╡
│ 1 ┆ A ┆ 1 │
│ 2 ┆ A ┆ 2 │
└────────────┴──────┴───────────┘
>>> stage_cfg = DictConfig({"min_subjects_per_code": 1})
>>> fn = filter_measurements_fntr(stage_cfg, code_metadata_df, ["modifier1"])
>>> fn(data).collect()
shape: (4, 3)
┌────────────┬──────┬───────────┐
│ subject_id ┆ code ┆ modifier1 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞════════════╪══════╪═══════════╡
│ 1 ┆ A ┆ 1 │
│ 1 ┆ B ┆ 1 │
│ 2 ┆ A ┆ 2 │
│ 2 ┆ C ┆ 2 │
└────────────┴──────┴───────────┘
>>> stage_cfg = DictConfig({"min_subjects_per_code": None, "min_occurrences_per_code": None})
>>> fn = filter_measurements_fntr(stage_cfg, code_metadata_df, ["modifier1"])
>>> fn(data).collect()
shape: (4, 3)
┌────────────┬──────┬───────────┐
│ subject_id ┆ code ┆ modifier1 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞════════════╪══════╪═══════════╡
│ 1 ┆ A ┆ 1 │
│ 1 ┆ B ┆ 1 │
│ 2 ┆ A ┆ 2 │
│ 2 ┆ C ┆ 2 │
└────────────┴──────┴───────────┘
>>> stage_cfg = DictConfig({"min_occurrences_per_code": 5})
>>> fn = filter_measurements_fntr(stage_cfg, code_metadata_df, ["modifier1"])
>>> fn(data).collect()
shape: (1, 3)
┌────────────┬──────┬───────────┐
│ subject_id ┆ code ┆ modifier1 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞════════════╪══════╪═══════════╡
│ 2 ┆ A ┆ 2 │
└────────────┴──────┴───────────┘
>>> data = pl.DataFrame({
... "subject_id": [1, 1, 2, 2],
... "code": ["BIRTH", "VISIT//PULMONARY", "BIRTH", "BIRTH//VISIT"],
... "modifier1": [1, 1, 2, 2],
... }).lazy()
>>> code_metadata_df = pl.DataFrame({
... "code": ["BIRTH", "VISIT//PULMONARY", "BIRTH", "BIRTH//VISIT"],
... "modifier1": [1, 1, 2, 2],
... "code/n_subjects": [2, 1, 3, 2],
... "code/n_occurrences": [4, 5, 3, 2],
... })
>>> stage_cfg = DictConfig({"additional_codes": ["^BIRTH$", "^VISIT.*$"]})
>>> fn = filter_measurements_fntr(stage_cfg, code_metadata_df, ["modifier1"])
>>> fn(data).collect()
shape: (3, 3)
┌────────────┬──────────────────┬───────────┐
│ subject_id ┆ code ┆ modifier1 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞════════════╪══════════════════╪═══════════╡
│ 1 ┆ BIRTH ┆ 1 │
│ 1 ┆ VISIT//PULMONARY ┆ 1 │
│ 2 ┆ BIRTH ┆ 2 │
└────────────┴──────────────────┴───────────┘
This stage works even if the default row index column exists
code_metadata_df = pl.DataFrame({ … “code”: [“A”, “A”, “B”, “C”], … “modifier1”: [1, 2, 1, 2], … “code/n_subjects”: [2, 1, 3, 2], … “code/n_occurrences”: [4, 5, 3, 2], … }) data = pl.DataFrame({ … “subject_id”: [1, 1, 2, 2], … “code”: [“A”, “B”, “A”, “C”], … “modifier1”: [1, 1, 2, 2], … “_row_idx”: [1, 1, 1, 1], … }).lazy() stage_cfg = DictConfig({“min_subjects_per_code”: 2, “min_occurrences_per_code”: 3}) fn = filter_measurements_fntr(stage_cfg, code_metadata_df, [“modifier1”]) fn(data).collect() shape: (2, 4) ┌────────────┬──────┬───────────┬──────────┐ │ subject_id ┆ code ┆ modifier1 ┆ _row_idx │ │ — ┆ — ┆ — ┆ — │ │ i64 ┆ str ┆ i64 ┆ i64 │ ╞════════════╪══════╪═══════════╪══════════╡ │ 1 ┆ A ┆ 1 ┆ 1 │ │ 1 ┆ B ┆ 1 ┆ 1 │ └────────────┴──────┴───────────┴──────────┘
Source code in meds_torch/utils/custom_filter_measurements.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
main(cfg)
TODO.