Skip to content

Warn about the default dataset cache; allow requiring cache_dir - #1264

Merged
jhnwu3 merged 1 commit into
sunlabuiuc:masterfrom
solarsys:feat/cache-dir-warning
Oct 2, 2026
Merged

jhnwu3 merged 1 commit into
sunlabuiuc:masterfrom
solarsys:feat/cache-dir-warning

Conversation

@solarsys

@solarsys solarsys commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

Problem

When no cache_dir is given, a dataset writes processed copies of its source data (the event table every patient is read from, and the samples each set_task() produces) to the user cache folder: platformdirs.user_cache_dir("pyhealth"), e.g. ~/Library/Caches/pyhealth or ~/.cache/pyhealth. That was logged only at INFO level.

For identified clinical data this silently copies row-level data outside the project's controlled storage. A downstream EHR project found it only by reading the code.

Changes

  • Loud default. Without cache_dir, PyHealth logs a WARNING naming the resolved path and explaining what is written there. It warns once per path, so repeated loads don't spam.
  • Opt-in hard stop. With PYHEALTH_REQUIRE_CACHE_DIR=1 (or true/yes), a dataset created without cache_dir raises ValueError before writing anything. An environment variable protects every script and notebook in a project at once, which a per-call argument can't.
  • Both entry points. One helper, _default_cache_dir(), is used by BaseDataset and by the FHIR dataset base, which had its own copy of the default. MEDSDataset inherits it.
  • Unchanged: behaviour when cache_dir is given, and the default location itself.

Tests, docs, example

  • New tests/core/test_default_cache_dir.py:
    • the warning names the path and the env var;
    • it is logged once per path;
    • an explicit cache_dir gives no warning;
    • the env var raises for several spellings and creates nothing;
    • the env var still allows an explicit cache_dir.
  • Existing base-dataset, caching and FHIR tests pass.
  • docs/api/datasets.rst: the cache_dir bullet names the default location, and there is a new "Working with identified data" section. That section also notes that create_sample_dataset(..., in_memory=False) writes to the system temp folder.
  • New examples/require_cache_dir.py on the bundled MIMIC-III demo.
  • Full core suite: Ran 1386 tests … OK (skipped=76). tools/check_pr_rules.py passes.

🤖 Generated with Claude Code

Without cache_dir, a dataset writes processed copies of its source data
(the event table and task samples) to the user cache folder, e.g.
~/Library/Caches/pyhealth, and said so only at INFO level. For identified
clinical data that silently copies row-level data outside the project's
controlled storage.

- New _default_cache_dir() (base_dataset): logs a WARNING naming the
  path, once per path; raises ValueError if PYHEALTH_REQUIRE_CACHE_DIR is
  1/true/yes, before anything is written. Used by BaseDataset and by the
  FHIR dataset base, which had its own copy of the default.
- tests/core/test_default_cache_dir.py: warning names the path and the
  env var, warns once, no warning with an explicit cache_dir, the env var
  raises without creating anything and allows an explicit cache_dir.
- docs/api/datasets.rst: default location and a "Working with identified
  data" section.
- examples/require_cache_dir.py on the bundled MIMIC-III demo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@jhnwu3 jhnwu3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@jhnwu3
jhnwu3 merged commit 5568155 into sunlabuiuc:master Oct 2, 2026
2 checks passed
@AxelNoun AxelNoun mentioned this pull request Oct 7, 2026
5 tasks done
@solarsys
solarsys deleted the feat/cache-dir-warning branch October 9, 2026 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants