Fix kg_emb broken import: SampleBaseDataset -> SampleDataset, add pan… - #1192
Fix kg_emb broken import: SampleBaseDataset -> SampleDataset, add pan…#1192userjuma wants to merge 4 commits into
Conversation
…darallel dependency
|
Thanks for this. The I think one step is missing though. The module imports, but Two smaller notes:
Worth saying that #952 asked for the rename across eight files and your PR does I've opened #1202 going after the root cause, and credited you in the |
- Add >>> usage examples to docstrings of SampleKGDataset, split(), KGEBaseModel, TransE, RotatE, DistMult, ComplEx - Fix a separate, pre-existing bug found while verifying the examples: SampleKGDataset called SampleDataset.__init__ with the old in-memory list API, but SampleDataset is now litdata-backed and expects a path to a directory built by SampleBuilder.save(). Set base attributes directly instead and added __len__ (previously inherited, now needed since we no longer call the incompatible super().__init__()) - Document the kg_emb classes in docs/api/medcode.rst - Add a runnable KG embedding example to examples/medcode.py
…l instantiation tests Per AxelNoun's review on this PR: - KGEBaseModel and its 4 subclasses (TransE, RotatE, DistMult, ComplEx), plus split(), typed their dataset parameter as SampleDataset, but they actually require SampleKGDataset-specific attributes (entity_num, relation_num). Fixed the type hints across all 6 locations. - tests/core/test_kg_emb.py previously only checked imports and a type annotation, never actually instantiating SampleKGDataset or any model — exactly the gap that let the SampleDataset.__init__ incompatibility (fixed in the previous commit) go undetected. Added tests that build a real SampleKGDataset, run split() on it, and construct all 5 model classes from it, checking embedding shapes. splitter.py uses a relative import (from .sample_kg_dataset import SampleKGDataset) rather than importing through the package __init__, since kg_emb/datasets/__init__.py imports split from splitter.py — importing back through the package would be circular.
userjuma
left a comment
There was a problem hiding this comment.
Thanks for the thorough review and for taking the time to reproduce the issue directly.
We reached the same root cause independently: SampleKGDataset was calling SampleDataset.__init__ using the deprecated in-memory list interface, whereas SampleDataset is now backed by litdata and expects paths generated via SampleBuilder.save(). A recent commit addresses this by bypassing super().__init__(), assigning the required base attributes directly, and implementing a dedicated __len__ method.
The accompanying feedback has also been addressed:
- Corrected dataset type annotations across
KGEBaseModel,TransE,RotatE,DistMult,ComplEx, andsplit()to explicitly requireSampleKGDataset, which supplies the necessaryentity_numandrelation_numattributes. - Expanded the test suite beyond basic import and annotation checks to instantiate
SampleKGDataset, executesplit(), and initialize all five model classes end-to-end.
3.Clarified the dependency requirement forpandarallel; while the import inbase_kg_dataset.pywas unused, the dependency remains declared inpyproject.tomlto support the active call insideumls.pyduringinitialize().
Since #1202 addresses the same underlying issue, this PR can be closed in favor of yours if that branch is further along, or we can move forward with whichever implementation is cleaner.
|
Thanks for jumping on the constructor fix and tests so fast, that definitely closed the gap I flagged. I think I'll take you up on your offer (if you're still cool with it). It really just came down to scope: #1202 completely drops the SampleDataset inheritance. Since kg_emb doesn't actually use the streaming contract, this decouples it so it won't break when that layer changes. I put the full context in the PR description so we don't have to rehash it here. Since this all started from your initial diagnosis, would you mind giving #1202 a quick review when you have a sec? I'd love to get your eyes on the redesign before we ping a maintainer. Please be brutal if anything looks off, I'd way rather catch it now than after merge! |
|
Update, and I need to walk something back: please don't close this in favor of #1202. After I'd already asked you to close, I checked the target architecture with John Wu, and he wants I'd like to offer you the fixes from #1202 that don't depend on the hierarchy question Worth noting: I branched these off master and four of my tests fail on |
|
Good call checking with John before committing to the redesig definitely better to confirm the direction upfront than rework things later. Glad #1192 can land as filed. Good to know the As for the other four bugs you spotted, let’s handle those in a dedicated follow-up PR against master once this one merges, rather than continuing to pile onto this branch. Between the initial rename, the docstring examples, the init fix, and the type hints, #1192 has already grown past its original scope. Keeping your four fixes as their own focused PR ensures they get reviewed on their own merits instead of getting buried inside an unrelated diff. Ping me when its up |
|
It's very good, congratulations! I just have two tiny, non-blocking nits whenever you have a second:
@jhnwu3 It seems to me that everything is good! |
Per AxelNoun's review: linear-attention-transformer picked up an extra 2-space indent when pandarallel was added nearby, and examples/medcode.py and tests/core/test_kg_emb.py were both missing a trailing newline.
Closes #952
Fixes the broken import in
kg_embcaused bySampleBaseDatasetbeing renamed toSampleDatasetin PyHealth 2.0. Updated across all 7 affected files (2 inkg_emb/datasets, 5 inkg_emb/models).Also adds
pandarallel(>=1.6.5) topyproject.toml. It is used inkg_emb/datasets/base_kg_dataset.pybut was previously missing from declared dependencies, which caused aModuleNotFoundErrorimmediately after the dataset rename was resolved.Added
tests/core/test_kg_emb.pycovering:1.Clean import of the
kg_embmodule2. Importability of all five
kg_embmodel classes3.A regression check ensuring
TransE's dataset parameter is explicitly typed asSampleDatasetManual verification:
Reproduced the original crash via
python -c "import pyhealth.medcode.pretrained_embeddings"and confirmed it now resolves cleanly end-to-end.