Skip to content

Feature/adaptive truncation - #70

Open
RH-MikkelWerling wants to merge 18 commits into
mainfrom
feature/adaptive_truncation
Open

Feature/adaptive truncation#70
RH-MikkelWerling wants to merge 18 commits into
mainfrom
feature/adaptive_truncation

Conversation

@RH-MikkelWerling

Copy link
Copy Markdown
Collaborator

Trying to implement adaptive truncation in the pipeline

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@RH-MikkelWerling, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: dd83db35-4f98-4327-8428-ab772ce5e5a3

📥 Commits

Reviewing files that changed from the base of the PR and between dc4b531 and e5a229d.

📒 Files selected for processing (13)
  • .gitignore
  • README.md
  • configs/MEDS/default_adaptive_code_mapping.yaml
  • configs/MEDS/lymphoma_pipeline.yaml
  • configs/MEDS/lymphoma_pipeline_external_mapping.yaml
  • ehr2meds/adaptive_code_mapping.py
  • ehr2meds/io_utils.py
  • ehr2meds/meds_stages/annotate_numeric_values.py
  • ehr2meds/meds_stages/apply_adaptive_code_mapping.py
  • ehr2meds/meds_stages/bin_numeric_values_fast.py
  • ehr2meds/meds_stages/finalize_adaptive_code_metadata.py
  • ehr2meds/meds_stages/fit_adaptive_code_mapping.py
  • pyproject.toml
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/adaptive_truncation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mikkelfo mikkelfo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally looks fine, but again I'm worried about the amount of code added for these features. The more we add, the harder it is to maintain imo.

We obviously require some functionality so we cannot aimlessly minimise, but we're generally adding a high level of verbosity to these functions that we may never look at. I think it's a fine line we have to walk (@Sllambias thoughts?)

Comment thread .claude/settings.json Outdated
Comment thread ehr2meds/meds_stages/apply_adaptive_code_mapping.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm a little worried about the level of verbosity for the columns. Seems like a lot of code for an extensive level of verbosity (via different columns) that we may never look at

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

True, agree - I've made much less checks and balances now. For writing up the code though, it is much easier to get a sense of what's up and down going overboard with how verbose it is. I've made it much more manageable now, totally agree with the sentiment.

Comment thread ehr2meds/meds_stages/fit_adaptive_code_mapping.py Outdated
Comment thread ehr2meds/meds_stages/fit_adaptive_code_mapping.py
@mikkelfo
mikkelfo requested a review from Sllambias July 29, 2026 08:50
from pathlib import Path

CODE_COLUMN = DataSchema.code_name
MAPPED_CODE_COLUMN = "adaptive/mapped_code"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should just be a default in the main function calling for this, or be contained in a yaml

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, moved it.

@Sllambias Sllambias left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with mikkel on the verbosity. I think the explanations make sense, but I think they're better placed in readme's that can be directly referred to from the yaml files and then I had a few comments. Happy to look again at a later time aswell

Comment thread ehr2meds/adaptive_code_mapping.py Outdated
from MEDS_transforms.utils import PKG_PFX, resolve_pkg_path
from pathlib import Path

CODE_COLUMN = DataSchema.code_name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Preferably just referred to as DataSchema.code_name in the code

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agree, this again - opted for more DataSchema.code_name in the code instead.

Comment thread ehr2meds/adaptive_code_mapping.py Outdated
return resolve_pkg_path(filepath) if filepath.startswith(PKG_PFX) else Path(filepath)


def load_frame(filepath: str, label: str) -> pl.DataFrame:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this something that should be a more generally shared function or does it necessarily need to be nested here?
As opposed to being in a more general function file that is used by all of EHR2meds - it seems rather general maybe apart from path = resolve_resource_path(filepath)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, super good point. This actually simplified things. I've added a io_utils.py now with a few helpers, and I think that setup makes more sense.

Comment thread ehr2meds/adaptive_code_mapping.py Outdated
external_mapping_mode: str,
) -> pl.DataFrame:
"""Select a local mapping and optionally overlay or replace it externally."""
required_local = {CODE_COLUMN, MAPPED_CODE_COLUMN}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you explain the different scenarios here? I'm trying to figure out if support all of these are necessary and can't really tell

Like what is the case where

  1. required_local is not subset of local_metadata and external_mapping_filepath is given
  2. required_local is not subset of local_metadata and external_mapping_filepath is not given
  3. required_local is subset of local_metadata and external_mapping_filepath is not given
  4. required_local is subset of local_metadata and external_mapping_filepath is given

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, super good question.
So, going through this, I rewrote much of the code because some of the instances don't really make sense.
Like for instance, you can imagine having to fit on your own data and not needing external data (say for instance, when you are training PHAIR light). The behavior before was also a bit messy in that it would add metadata for codes that were not in the external data, which is why it needed both. Now this can technically happen - you could imagine having a mismatch between data sources, where an upstream model doesn't include some data source that does have a hierarchy you want to truncate. However, I figured that this would be so rare that we are better off opting for a system where you can specify the stages for the pipeline that calculates the aggregate measures and thereby bin / truncate based on your own data, or otherwise use an external source as a backup, which is then taken as ground truth and not messed with. The other behavior was a mistake on my part; I thought it was smart, but I think it's stupid.
This also means that the behavior now is simpler. It doesn't cover as many different combinations, but it's easier to understand and more aligned with what the actual use cases are.

Comment thread ehr2meds/adaptive_code_mapping.py Outdated
Comment on lines +67 to +69
if mode != "overlay":
raise ValueError("external_mapping_mode must be 'overlay' or 'replace'")
return pl.concat([external, local]).unique(CODE_COLUMN, keep="first", maintain_order=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very minor suggestion but I think this is slightly nicer

Suggested change
if mode != "overlay":
raise ValueError("external_mapping_mode must be 'overlay' or 'replace'")
return pl.concat([external, local]).unique(CODE_COLUMN, keep="first", maintain_order=True)
if mode == "overlay":
return pl.concat([external, local]).unique(CODE_COLUMN, keep="first", maintain_order=True)
else:
raise ValueError(f"external_mapping_mode must be 'overlay' or 'replace' but is {mode}")

Comment thread tests/test_adaptive_code_mapping.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is a really nice file, and fits perfectly with what I also wanted to do with the other "tests" running in the workflows.

I'm a little concerned about (my perceived) complexity of these tests and I fear they may be prone to breaking quite easily and being relatively hard to maintain for maintainers (like me) not too familiar with the exact usage. But this is just a concern and may be completely wrong.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yeah it was super useful for developing, but I think generally these files should be wiped after development or be integrated in a larger test suite. Don't think they make sense on their own, so I'll probably delete it now.

"""
cfg = OmegaConf.to_container(stage_cfg, resolve=True)
namespaces = {str(namespace): str(profile) for namespace, profile in cfg["namespaces"].items()}
hierarchies = cfg.get("hierarchies", {})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what happens when hierarchies is empty?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

made it so it errors instead of defaulting to an empty set

}


def fit_mapping(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe this function should be split into two

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

tried but didn't see a super good way of doing it. There are parts of the loop that needs previous parts of the code, so a natural split is kind of hard to make I think. Happy to take a second look if you see a path forward!

@RH-MikkelWerling
RH-MikkelWerling marked this pull request as ready for review July 30, 2026 14:08
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.

3 participants