Repository navigation
Conversation
…ore bugs
- Remove runtime os.system pip installation from pyhealth.nlp.metrics.Scorer
and fix variable shadowing in _get_missing_modules() that raised AttributeError.
- Pass weights_only=True to torch.load() in all five generator models
(CorGAN, GPT2, HALO, MedGAN, PromptEHR) to match SECURITY.md.
- Fix F821 undefined names in sage_sampler.py (define Adj), molerec.py
(explicit module-level lazy OGB bindings), and medical_coding.py (remove
dead references to commented-out MIMIC-IV coding tasks in main()).
- Fix Trainer FileHandler leak across instances (close/detach prior handlers
and add Trainer.close()), guard empty-dataloader divisions in train/evaluate/
inference, fix inference() return type annotation, and fix load_ckpt() docstring.
- Materialize patient_ids before len() in BaseDataset._task_transform_fn() so
single-use iterables are not exhausted before batching.
- Make BASE_CACHE_PATH and module cache directories lazy with exist_ok=True
(supporting PYHEALTH_CACHE_DIR), avoiding import-time filesystem side effects.
Test CoverageWarning Tests failure; coverage may be incomplete. Total: 88.43% (58590 / 66256 lines)
|
Collaborator
|
Why this PR has To help it along:
|
…and add unit tests
Contributor
Author
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses several security and reliability bugs identified during a codebase audit:
Security & Deserialization
os.systempip install(pyhealth/nlp/metrics.py): RemovedScorer._install()and_install_all()so scoring never shells out or mutates the user's Python environment at runtime. Fixed themissingvariable shadowing bug in_get_missing_modules()that raisedAttributeError: 'tuple' object has no attribute 'extend'when a dependency was missing, and logged a warning directing users topip install pyhealth[nlp].weights_only=Truein generatortorch.load()calls: UpdatedCorGAN,GPT2,HALO,MedGAN, andPromptEHRto load checkpoints withweights_only=True, aligning withSECURITY.md.Core Bug Fixes
F821):pyhealth/sampler/sage_sampler.py: DefinedAdj(NamedTuple)(with.to()) used whenis_sparse_tensor=True.pyhealth/models/molerec.py: Replacedglobals().update(...)with explicit module-level lazy bindings (smiles2graph,AtomEncoder,BondEncoder) and ensuredGINConv,GINGraph, andgraph_batch_from_smilesload OGB on demand.pyhealth/tasks/medical_coding.py: Removed dead calls inmain()to the commented-outMIMIC4ICD9CodingandMIMIC4ICD10Codingclasses.Trainerresource leak & edge cases (pyhealth/trainer.py):set_logger()now detaches and closes any existingFileHandleron the module logger before adding a new one (preventing FD leaks and duplicate writes across multipleTrainerinstances), andTrainer.close()was added.train(),evaluate(), andinference()(raisingValueErrorininference()instead ofZeroDivisionError).inference()return type annotation (-> list) andload_ckpt()docstring.Iterableexhaustion (pyhealth/datasets/base_dataset.py): Materializedpatient_ids = list(patient_ids)beforelen(patient_ids)in_task_transform_fn()so iterators are not exhausted prior toitertools.batched(...).pyhealth/__init__.py,pyhealth/utils.py,pyhealth/datasets/utils.py,pyhealth/medcode/utils.py):os.makedirs(BASE_CACHE_PATH)and eagercreate_directory(MODULE_CACHE_PATH)calls (including the 20-directory loop inmedcode/utils.py).PYHEALTH_CACHE_DIRoverride andensure_cache_dir(), switchedcreate_directory()toos.makedirs(..., exist_ok=True), and ensureddownload_file()andsave_pickle()lazily create parent directories.Verification
ruff check --select F821,E9 pyhealth— 0 errors.test_default_cache_dir.py,test_base_dataset.py,test_corgan.py,test_medgan.py,test_gpt2.py,test_halo.py,test_promptehr.py,test_molerec.py) — 58 passed, 5 skipped.BASE_CACHE_PATHon import in an isolated subprocess,Scorer._get_missing_modules()with missing dependencies,TrainerFileHandlercleanup + empty-dataloader guards, andsage_sampler.Adj.