Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
113 changes: 113 additions & 0 deletions examples/mortality_prediction/README_unified_embedding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Unified-embedding experiments: A/B, sweeps, ablations

`unified_embedding_experiments.py` drives the existing runner
(`unified_embedding_e2e_mimic4.py::run`) one arm at a time. Each arm
gets its own directory under `--output-root`; together they write
`summary.csv` and `summary.md` (arm, the one differing knob, seconds
per phase, frozen-text cache counters, test scores).

Driver options come first, then `--`, then the runner arguments
(passed through to every arm unchanged):

```
# A/B: two arms, one flag differs (booleans need no --values)
python examples/mortality_prediction/unified_embedding_experiments.py \
--output-root ./output/xp_ab \
ab --flag cache-frozen-text -- \
--ehr-root /data/mimic-iv/2.2 --note-root /data/mimic-iv/note \
--task notes_labs --model transformer --epochs 3 --device cuda:0 --seed 2

# Sweep: N values of one flag (--flag window-hours is an alias)
python examples/mortality_prediction/unified_embedding_experiments.py \
--output-root ./output/xp_win \
sweep --flag window-hours --values 0 24 48 -- \
--ehr-root /data/mimic-iv/2.2 --task labs --model mlp --epochs 1 --seed 2

# Ablate: reference plus one arm per dropped modality / switched-off component
python examples/mortality_prediction/unified_embedding_experiments.py \
--output-root ./output/xp_abl \
ablate --modalities notes labs --off cache-frozen-text -- \
--ehr-root /data/mimic-iv/2.2 --note-root /data/mimic-iv/note \
--task notes_labs --model transformer --epochs 3 --device cuda:0 --seed 2
```

`ablate --modalities` drops each modality at the model stage through the
runner's `--drop-modality`: same task, same samples, same split, same
seed. `labs` drops the lab values and their observation mask, `notes`
drops the admission-note field; the cohort keeps every patient either
way. Dropping everything is refused, as is dropping a modality the
task does not have.

Rules the driver enforces (it exits 2 instead of running): unknown
`--flag`; a flag inert for the chosen backbone or task (e.g.
`--hidden-dim` with `--model transformer`, the text-cache flags with
`--task labs` — backbone reachability comes from the runner's own
wiring map, so the two cannot disagree); arms that would differ in more
than the declared knob; duplicate values; `ablate` with nothing to drop
or nothing left after the drop; `ablate` with a passed-through
`--drop-modality` (the arms set it themselves, so a passed value would
be silently replaced). Sweeping `--seed` is allowed but
changes the split by design. Seed, split sizes, and sample identity are
checked across arms and reported in `summary.md` — identical when they
match, naming the arms that differ when they do not. The identity check
compares unique inference-split patient ids; train/val splits and labels
are not compared, and an empty predictions file reports not recorded.
Pass `--output-root`, not `--output-dir` (each arm gets its own
`--output-dir` underneath it).

## Equivalent Python API

```python
from examples.mortality_prediction import unified_embedding_experiments as xp

runner = xp._load_runner()
base = runner.parse_args(["--ehr-root", "/data/mimic-iv/2.2",
"--task", "labs", "--model", "mlp",
"--epochs", "1", "--seed", "2"])
arms = xp.build_arms("sweep", base, flag="window-hours",
values=["0", "24", "48"])
xp.main(["--output-root", "./output/xp_win", "sweep",
"--flag", "window-hours", "--values", "0", "24", "48",
"--", "--ehr-root", "/data/mimic-iv/2.2",
"--task", "labs", "--model", "mlp",
"--epochs", "1", "--seed", "2"])
```

## Small utilities you will actually use

- Cache counters: every `metrics.json` carries
`frozen_text_cache: {hits, misses, size}`; the summary rolls them
up per arm. Compare the on/off arms of
`ab --flag cache-frozen-text` to see the timing effect.
`UnifiedMultimodalEmbeddingModel.reset_frozen_text_cache_stats()`
zeroes them mid-session.
- DataLoader workers: `--num-workers 1` (below 1 is refused when the
runner builds the dataset itself: a zero-worker Dask cluster never
computes, the local path divides by the worker count in
`set_task`, and a negative count dies inside loader construction;
a prebuilt sample dataset may still load with 0).
`--no-dask-distributed` builds the cache with the local Dask
scheduler instead of starting a cluster. `--pin-memory`,
`--persistent-workers` (needs workers > 0), `--prefetch-factor`
(needs workers > 0). On CPU, workers 1 with `OMP_NUM_THREADS`
pinned is the stable setting.
- Cache dir: share one `--cache-dir` across arms of a sweep; the
task cache key already includes task + window, so arms reuse each
other's dataset builds. A killed run can leave a stale
`tasks/<hash>/build.lock`; delete the task dir and rerun.

## Real output (public demo fixture, toy magnitudes, no scientific claim)

`sweep --flag window-hours --values 0 24` on
`test-resources/core/mimic4demo`, labs, mlp, 1 epoch, CPU:

```
arm,differs,seed,train_s,eval_s,infer_s,cache_hits,cache_misses,cache_size,score_accuracy,score_f1,score_loss,score_pr_auc,score_roc_auc
sweep_observation_window_hours_0.0,observation_window_hours=0.0,2,16.934,8.491,8.727,0,0,0,0.5,0.0,0.6831296682357788,1.0,1.0
sweep_observation_window_hours_24.0,observation_window_hours=24.0,2,16.913,8.432,8.743,0,0,0,0.5,0.0,0.6831296682357788,1.0,1.0
```

Both arms agree to all digits: the 13-patient demo stays are short
enough that a 24 h window never binds. On real MIMIC-IV the same
command separates the arms; treat every number above as a timing
smoke test, not a result.
155 changes: 146 additions & 9 deletions examples/mortality_prediction/unified_embedding_e2e_mimic4.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ def finish(self) -> None:
self._run.finish()


def _require_build_workers(
args: argparse.Namespace, sample_dataset: Any = None
) -> None:
"""Refuse ``--num-workers < 1`` when the runner builds the dataset itself.

A zero-worker distributed cluster never computes (no workers run the
tasks), so the build hangs; the local-scheduler path avoids the hang
but ``set_task`` then divides the patient list by the worker count.
A negative count fails even earlier inside the Dask/torch loader
construction, with an error that never names the flag. Either way
fewer than one worker cannot build. Zero is still fine for loading
a prebuilt ``sample_dataset`` (plain main-process torch loading).

Example:
>>> _require_build_workers(
... argparse.Namespace(num_workers=1), sample_dataset=None)
>>> _require_build_workers(
... argparse.Namespace(num_workers=0), sample_dataset=object())
"""
if sample_dataset is None and args.num_workers < 1:
raise RuntimeError(
f"--num-workers {args.num_workers} cannot build the dataset: "
"the distributed Dask cluster would start with no workers and "
"never compute, and the local-scheduler path "
"(--no-dask-distributed) still divides by the worker count in "
"set_task. Pass --num-workers 1 or more when the runner builds "
"the dataset itself."
)


def _build_base_dataset(args: argparse.Namespace) -> MIMIC4Dataset:
ehr_tables = ["labevents"]
note_tables = None
Expand Down Expand Up @@ -201,14 +231,69 @@ def _nondefault_inert_flags(args: argparse.Namespace) -> List[str]:
return set_flags


# Modality -> task input fields removed at the model stage. Siblings drop
# together: labs goes with its observation mask, notes with the single
# admission-note field that carries both text and note times.
_DROP_MODALITY_FIELDS = {
"labs": ("labs", "labs_mask"),
"notes": ("admission_note_times",),
}


def _check_drop_modality(args: argparse.Namespace) -> None:
"""Refuse a ``--drop-modality`` combination that drops nothing or all.

Needs only ``task`` and ``drop_modality``, so the experiment driver
uses it to refuse bad ablations before running anything. Raises
``ValueError``; the CLI turns it into a parse error.

Example:
>>> _check_drop_modality(
... argparse.Namespace(task="notes_labs", drop_modality=["notes"]))
>>> _check_drop_modality(
... argparse.Namespace(task="labs", drop_modality=["labs"]))
Traceback (most recent call last):
...
ValueError: --drop-modality ['labs'] leaves no input modality: ...
"""
drop = args.drop_modality or []
if len(set(drop)) != len(drop):
raise ValueError(f"--drop-modality contains duplicates: {drop}.")
available = {
"labs": {"labs", "labs_mask"},
"notes_labs": {"admission_note_times", "labs", "labs_mask"},
}[args.task]
dropped = {f for m in drop for f in _DROP_MODALITY_FIELDS[m]}
if dropped - available:
raise ValueError(
f"--drop-modality {drop} has no fields to remove for --task "
f"{args.task}: it only has {sorted(available)}."
)
if not (set(available) - dropped) & {"labs", "admission_note_times"}:
raise ValueError(
f"--drop-modality {drop} leaves no input modality: dropping "
"everything is refused."
)


def _build_task(args: argparse.Namespace):
if args.task == "notes_labs":
return NotesLabsMIMIC4(
task = NotesLabsMIMIC4(
window_hours=args.observation_window_hours,
)
if args.task == "labs":
return LabsMIMIC4(window_hours=args.observation_window_hours)
raise ValueError(f"Unknown task: {args.task}")
elif args.task == "labs":
task = LabsMIMIC4(window_hours=args.observation_window_hours)
else:
raise ValueError(f"Unknown task: {args.task}")
drop = getattr(args, "drop_modality", None) or []
if drop:
dropped = {f for m in drop for f in _DROP_MODALITY_FIELDS[m]}
task.input_schema = {
key: value
for key, value in task.input_schema.items()
if key not in dropped
}
return task


def _is_mortality_task(task: str) -> bool:
Expand Down Expand Up @@ -492,6 +577,7 @@ def run(args: argparse.Namespace, sample_dataset: Any = None) -> Path:
args.task,
)
set_seed(args.seed)
_require_build_workers(args, sample_dataset)

if sample_dataset is None:
base_dataset = _build_base_dataset(args)
Expand Down Expand Up @@ -548,13 +634,15 @@ def run(args: argparse.Namespace, sample_dataset: Any = None) -> Path:
# suffixes they would collide with the full run.
dev_suffix = f"_dev{args.dev}" if args.dev else ""
window_suffix = _window_suffix(args.observation_window_hours)
drop = getattr(args, "drop_modality", None) or []
drop_suffix = "".join(f"_no{m}" for m in sorted(drop))
# The flag marks the config, not the outcome: a run that splits
# cleanly by patient keeps the suffix, so the opt-in stays visible
# in the directory name.
leaky_suffix = "_allow_leaky" if args.allow_leaky_split else ""
exp_name = (
f"{args.task}_{args.model}_seed{args.seed}"
f"{dev_suffix}{window_suffix}{leaky_suffix}"
f"{dev_suffix}{window_suffix}{drop_suffix}{leaky_suffix}"
)
output_dir = Path(args.output_dir)

Expand Down Expand Up @@ -763,7 +851,18 @@ def parse_args(argv: Optional[list] = None) -> argparse.Namespace:
default=1,
help="Gradient accumulation steps (default 1, no accumulation).",
)
parser.add_argument("--num-workers", type=int, default=1)
parser.add_argument(
"--num-workers",
type=int,
default=1,
help=(
"Dask build workers and loader workers. Below 1 is refused "
"when the runner builds the dataset itself (a zero-worker "
"build hangs or divides by zero; a negative count dies "
"inside loader construction); a prebuilt sample dataset may "
"still load with 0."
),
)
parser.add_argument(
"--no-dask-distributed",
dest="use_dask_distributed",
Expand Down Expand Up @@ -809,9 +908,11 @@ def parse_args(argv: Optional[list] = None) -> argparse.Namespace:
const=1000,
default=0,
help=(
"Dev mode: limit dataset to N patients for fast iteration. "
"--dev (no value) defaults to 1000 patients. "
"--dev 5000 limits to 5000. Omit for full dataset."
"Dev mode: cap the cohort at 1000 patients for fast iteration "
"(bare --dev means 1000). Only 0 (off, full dataset) and 1000 "
"are accepted: the dataset treats dev as a flag and always "
"caps at 1000, so any other value would silently behave as "
"1000. Omit for the full dataset."
),
)
parser.add_argument(
Expand All @@ -823,6 +924,23 @@ def parse_args(argv: Optional[list] = None) -> argparse.Namespace:
"Default 0 means no cap: full stay through discharge."
),
)
parser.add_argument(
"--drop-modality",
action="append",
choices=["labs", "notes"],
default=None,
help=(
"Drop a modality's input fields at the model stage; repeat for "
"both (but dropping everything is refused). labs removes labs "
"and labs_mask, notes removes admission_note_times. Same task, "
"same samples, same split, same seed: the cohort keeps every "
"patient, and a patient with nothing left in the remaining "
"modality stays as an empty sequence. Cohort selection, time "
"origin, and splits are unchanged -- the task still reads every "
"table -- so dropping notes still needs --note-root for --task "
"notes_labs, and discharge timing still bounds the windows."
),
)
parser.add_argument(
"--freeze-encoder",
action="store_true",
Expand Down Expand Up @@ -926,6 +1044,25 @@ def parse_args(argv: Optional[list] = None) -> argparse.Namespace:
if args.amp_dtype is None:
args.amp_dtype = "bf16"

# The dataset treats dev as a flag and hardcodes the 1000-patient cap,
# so any other value would silently behave as --dev 1000. Refuse
# rather than mislead; the dataset itself is left alone.
if args.dev not in (0, 1000):
parser.error(
f"--dev {args.dev} is not supported. Only 0 (off, full "
"dataset) and 1000 are accepted: the dataset caps at 1000 "
"patients whatever positive value it gets, so any other value "
"would silently behave as --dev 1000."
)

# A modality ablation must keep at least one input modality and may
# only drop fields the task has. Checked here so a bad combination
# fails before any dataset build.
try:
_check_drop_modality(args)
except ValueError as exc:
parser.error(str(exc))

return args


Expand Down
Loading
Loading