Repository navigation
Conversation
Test CoverageTotal: 88.77% (61481 / 69257 lines)
|
|
/review |
| scaled = embeddings / embeddings.abs().amax(dim=-1, keepdim=True) | ||
| return F.normalize(scaled, dim=-1) | ||
|
|
||
| def forward( |
There was a problem hiding this comment.
[high] forward rejects the Trainer's batch keys (patient_id)
forward(self, source_embedding, label=None) has no **kwargs. PyHealth's Trainer calls self.model(**data) with the full dataloader batch (trainer.py:324, and evaluation reads data["patient_id"] at trainer.py:433), so any dataset-backed train or eval call raises TypeError: unexpected keyword argument 'patient_id'. The model is also documented as accepting dataset= and returns loss/y_true/y_prob, which is the Trainer contract. Fix: match BaseModel.forward's convention and accept **kwargs (ignoring extra keys), and add a test that runs the model through create_sample_dataset + get_dataloader and a batch that includes patient_id.
| raise ValueError(f"{name} must be a nonempty matrix.") | ||
| if not embeddings.is_floating_point() or not torch.isfinite(embeddings).all(): | ||
| raise ValueError(f"{name} must contain finite floating-point values.") | ||
| if (embeddings == 0).all(dim=-1).any(): |
There was a problem hiding this comment.
[medium] Zero query rows from TF-IDF fail the whole batch
The check (embeddings == 0).all(dim=-1).any() rejects any zero source row, and the entire batch then raises. A TF-IDF encoder, which the example and the description use, produces a zero row for any lab name with no catalog vocabulary, a common case in real local lab names. One unseen name therefore fails a whole batch. Fix: either give zero-norm source rows a zero score (for example by clamping the norm) and document that, or document that callers must filter such rows. Add a test for a zero source row either way.
| fields = "name + specimen" if include_specimen else "name only" | ||
| print(f"{analyzer:7s} | {fields:15s} | " + " | ".join(metrics)) | ||
| if analyzer == "word": | ||
| print(f" Glucose quantitative -> {CATALOG[ranking[0, 0].item()]}") |
There was a problem hiding this comment.
[low] Demo print is inside the setting loop and unlabeled
The line print(f" Glucose quantitative -> ...") runs for both the name-only and name+specimen settings, so the same line appears twice without saying which format produced it. Move it out of the inner loop or add fields to the output so the reader can tell the two results apart.
| .. autoclass:: pyhealth.models.loinc_retriever.LOINCRetriever | ||
| :members: | ||
|
|
||
| The model reproduces the cosine retrieval step in |
There was a problem hiding this comment.
[low] Page says the model reproduces the paper's step without noting the omitted parts
"The model reproduces the cosine retrieval step in Tu et al. (2022)" reads as a full reproduction. The description says the paper's Sentence-T5 encoder, contrastive fine-tuning and MIMIC-III mappings are not included. Add one sentence saying which parts are left out.
There was a problem hiding this comment.
AI Review
suggestion: need change
decision: need change
Thank you, Deepa, for this contribution. LOINCRetriever is an encoder-agnostic cosine retrieval model for LOINC candidates, and it comes with a synthetic TF-IDF example, unit tests and an API page. Coverage is above the bar: loinc_retriever.py is at 97.83% and the whole repository is at 88.77%. The change is close, but two problems need fixing first.
To get this merged:
forwardcan't take a full dataset batch.LOINCRetriever.forward(self, source_embedding, label=None)has no**kwargs. PyHealth'sTrainercallsmodel(**batch), and dataset batches also carry fields such aspatient_id. So training or evaluation through theTraineror a dataloader would most likely raise aTypeError. Your example avoids this by passingsource_embeddingexplicitly. Please accept**kwargsand ignore the extra fields, as other PyHealth models do. Please also add a test that runs a dataset batch throughmodel(**batch). If you only intend direct tensor calls, drop thedatasetargument and say so in the docs.- A zero query vector fails the whole batch.
_validate_embeddingsrejects any all-zero row, and this check also runs on the query side. A local lab name with no terms in the TF-IDF vocabulary gives a zero row. That is a realistic case for an encoder-agnostic model, and it would make the entire batch fail. Please either handle zero queries gracefully (for example, give them a score of 0 for every candidate) or state the limitation clearly in the docstring and API docs. Please also add a test for whichever you choose.
Optional suggestions:
- In the example, the "Glucose quantitative -> ..." line prints for both query formats without saying which is which. Labelling it would help.
- The API page says the model "reproduces" the paper's retrieval step. Since the Sentence-T5 encoder and the fine-tuning aren't included, "implements the cosine ranking step" would be more precise.
Reviewed commit 55c3d0b
Title
Add cosine-similarity LOINC candidate retrieval
Contributor
Deepa Mondal (GT username dmondal31, dmondal31@gatech.edu)
Contribution type
Model
Paper
Tu et al. (2022), Automated LOINC Standardization Using Pre-trained Large Language Models
https://proceedings.mlr.press/v193/tu22a.html
Description
This PyHealth model implements the cosine retrieval step in Section 3.6. It
accepts embeddings of local lab names and a shared target catalog, then returns
ranked catalog indices. It is encoder-agnostic. The example uses TF-IDF to
exercise the ranking method with synthetic records. The implementation covers
the inference stage. The published experiment also uses Sentence-T5,
contrastive fine-tuning and MIMIC-III mappings.
File guide
validation and gradient tests.
using word and character TF-IDF on the same queries and catalog.
Verification
python -m pytest -q tests/core/test_loinc_retriever.py
python -m examples.synthetic_loinc_mapping_cosine
On the synthetic fixture, adding specimen improved top-1 from 9/16 to
16/16 with word unigrams and from 7/16 to 16/16 with character n-grams.
All four settings reached 16/16 at top-3 and top-5. The hand-authored
fixture tests specimen ambiguity under controlled conditions.