Skip to content

ENH: Initial implementation of a PhysicsNeMo tutorial#54

Merged
aylward merged 2 commits into
Project-MONAI:mainfrom
aylward:InitialPhysicsNeMoTutorial
May 20, 2026
Merged

ENH: Initial implementation of a PhysicsNeMo tutorial#54
aylward merged 2 commits into
Project-MONAI:mainfrom
aylward:InitialPhysicsNeMoTutorial

Conversation

@aylward
Copy link
Copy Markdown
Collaborator

@aylward aylward commented May 20, 2026

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a new tutorial demonstrating mesh displacement model training with PhysicsNeMo integration.
  • Documentation

    • Expanded API reference with new component listings for longitudinal registration experiments and pipeline modules.
  • Chores

    • Updated development configuration to support expanded codebase coverage.

Copilot AI review requested due to automatic review settings May 20, 2026 11:29
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

Warning

Rate limit exceeded

@aylward has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 45 minutes and 58 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f3a28b65-c027-444a-84f2-3d43d62f24c3

📥 Commits

Reviewing files that changed from the base of the PR and between aec6568 and d1c8a1a.

📒 Files selected for processing (2)
  • .pre-commit-config.yaml
  • tutorials/tutorial_09_physicsnemo_mesh_stage_model.py

Walkthrough

This PR adds Tutorial 9, a PhysicsNeMo-based mesh stage model that predicts per-vertex respiratory stage-dependent displacements. It includes configuration updates to enable type-checking of the tutorials directory, auto-generated API documentation for the LongitudinalRegistration experiment suite, and the full tutorial implementation with training and inference logic.

Changes

Tutorial 9 PhysicsNeMo Stage Model

Layer / File(s) Summary
Type-checking and tool configuration
.pre-commit-config.yaml, pyproject.toml
Mypy pre-commit hook and pyproject.toml overrides updated to include tutorials/ directory in type-checking scope and add physicsnemo and tutorial_09_physicsnemo_mesh_stage_model to the mypy ignore/override list.
Generated API documentation
docs/API_MAP.md
Auto-generated API index entries added for LongitudinalRegistration experiment suite including uniGradICON package, finetuning components, training modules, and corresponding test modules with documented public classes and functions.
Tutorial 9 implementation
tutorials/tutorial_09_physicsnemo_mesh_stage_model.py
New tutorial script that loads PCA-fitted meshes, trains a PhysicsNeMo FullyConnected MLP model to predict per-vertex displacements from normalized reference coordinates and respiratory stage, performs inference at target stage, and saves checkpoint, metadata, loss history, and predicted mesh per DirLab case.

Sequence Diagram

sequenceDiagram
  participant TutorialScript
  participant MeshLoader
  participant Normalizer
  participant PhysicsNeMoMLP
  participant InferenceEngine
  participant OutputWriter
  TutorialScript->>MeshLoader: Load PCA-fitted VTP meshes per case
  MeshLoader->>Normalizer: Pass loaded mesh point coordinates
  Normalizer->>PhysicsNeMoMLP: Normalized [x,y,z,stage] + displacement targets
  PhysicsNeMoMLP->>PhysicsNeMoMLP: Train FullyConnected MLP with MSE loss
  TutorialScript->>InferenceEngine: Predict displacements at TARGET_STAGE
  InferenceEngine->>InferenceEngine: Chunked inference over reference points
  InferenceEngine->>OutputWriter: Rescaled displacement predictions
  OutputWriter->>OutputWriter: Apply displacements, write .vtp mesh, checkpoint, metadata, loss history
  OutputWriter->>TutorialScript: Return per-case result paths
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A stage-aware model hops into view,
Predicting mesh displacements through and through!
PhysicsNeMo trains with focused grace,
Respiratory rhythm finds its place. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'ENH: Initial implementation of a PhysicsNeMo tutorial' clearly and specifically describes the main change: adding a new tutorial for PhysicsNeMo, matching the substantial new tutorial_09_physicsnemo_mesh_stage_model.py file and supporting configuration updates.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
tutorials/tutorial_09_physicsnemo_mesh_stage_model.py (1)

64-262: 🏗️ Heavy lift

Consider splitting run_tutorial() into focused helpers.

This function currently mixes case discovery, dataset assembly, training, inference, and artifact writing. Extracting those into small helpers would improve readability and make failures easier to isolate.

As per coding guidelines, "Keep functions focused and small in Python code."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tutorials/tutorial_09_physicsnemo_mesh_stage_model.py` around lines 64 - 262,
run_tutorial() is doing too many things (case discovery, dataset assembly,
training, inference, and artifact writing); split it into focused helper
functions to improve readability and testability. Create helpers such as
discover_cases() (returns case_numbers and case_prefix),
build_case_dataset(case_prefix, tutorial_08_output_dir, points_per_mesh)
(returns reference_mesh, point_indices, coordinate_mean/scale, inputs_array,
targets_array, mesh_files), train_stage_model(inputs_tensor, targets_tensor,
device, learning_rate, epochs) (constructs FullyConnected, optimizer, trains and
returns model and losses and displacement_scale), predict_mesh_for_stage(model,
reference_mesh, coordinate_mean, coordinate_scale, displacement_scale,
target_stage, point_indices, device) (returns predicted_mesh), and
write_case_artifacts(case_output_dir, model, metadata, losses, predicted_mesh,
mesh_files) (saves checkpoint, metadata, losses, mesh). Replace the big loop
inside run_tutorial() with calls to these helpers, ensure each helper has clear
return values (e.g., model, losses, predicted_mesh, checkpoint path) and that
existing names like FullyConnected, run_tutorial, inputs_tensor, targets_tensor,
and tutorial_outputs are preserved and used to assemble final tutorial_outputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tutorials/tutorial_09_physicsnemo_mesh_stage_model.py`:
- Line 100: Replace the ad-hoc print calls that use case_prefix (e.g.,
print(f"Skipping {case_prefix}: fewer than two Tutorial 8 meshes found")) and
the other print(s) around the mesh-processing block with structured logging
calls; import and use the module-level logger (logging.info(...)) instead of
print so messages respect configured verbosity and handlers—locate the prints by
searching for the string "Skipping {case_prefix}" and the prints in the
mesh-processing section (around the same function/loop that references
case_prefix) and change them to logging.info(...) with the same formatted
message.
- Around line 89-97: Validate CASE bounds before indexing DIRLAB_CASE_PREFIXES:
ensure any value in case_numbers is between 1 and len(DIRLAB_CASE_PREFIXES)
(inclusive) and raise a clear ValueError if not. Specifically, where
case_numbers is derived (the CASE variable or range(1, 11)), add a quick check
before using case_prefix = DIRLAB_CASE_PREFIXES[case_number - 1] (or validate
inside the loop) to fail fast with an informative message mentioning the invalid
CASE value and the accepted range; reference variables/keys: case, case_numbers,
DIRLAB_CASE_PREFIXES, case_number, case_prefix, mesh_dir,
tutorial_08_output_dir.

---

Nitpick comments:
In `@tutorials/tutorial_09_physicsnemo_mesh_stage_model.py`:
- Around line 64-262: run_tutorial() is doing too many things (case discovery,
dataset assembly, training, inference, and artifact writing); split it into
focused helper functions to improve readability and testability. Create helpers
such as discover_cases() (returns case_numbers and case_prefix),
build_case_dataset(case_prefix, tutorial_08_output_dir, points_per_mesh)
(returns reference_mesh, point_indices, coordinate_mean/scale, inputs_array,
targets_array, mesh_files), train_stage_model(inputs_tensor, targets_tensor,
device, learning_rate, epochs) (constructs FullyConnected, optimizer, trains and
returns model and losses and displacement_scale), predict_mesh_for_stage(model,
reference_mesh, coordinate_mean, coordinate_scale, displacement_scale,
target_stage, point_indices, device) (returns predicted_mesh), and
write_case_artifacts(case_output_dir, model, metadata, losses, predicted_mesh,
mesh_files) (saves checkpoint, metadata, losses, mesh). Replace the big loop
inside run_tutorial() with calls to these helpers, ensure each helper has clear
return values (e.g., model, losses, predicted_mesh, checkpoint path) and that
existing names like FullyConnected, run_tutorial, inputs_tensor, targets_tensor,
and tutorial_outputs are preserved and used to assemble final tutorial_outputs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 517b7b8a-440b-4950-908b-8c12d6306367

📥 Commits

Reviewing files that changed from the base of the PR and between 481dd76 and aec6568.

📒 Files selected for processing (5)
  • .pre-commit-config.yaml
  • docs/API_MAP.md
  • pyproject.toml
  • tutorials/__init__.py
  • tutorials/tutorial_09_physicsnemo_mesh_stage_model.py

Comment thread tutorials/tutorial_09_physicsnemo_mesh_stage_model.py
Comment thread tutorials/tutorial_09_physicsnemo_mesh_stage_model.py Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new tutorial script demonstrating how to train a PhysicsNeMo MLP to predict DirLab mesh displacements as a function of normalized respiratory stage, using meshes generated by Tutorial 8.

Changes:

  • Add tutorial_09_physicsnemo_mesh_stage_model.py to train per-case PhysicsNeMo models and write predicted meshes/checkpoints/metadata.
  • Update mypy configuration and pre-commit mypy hook to include PhysicsNeMo imports and type-check the tutorials/ directory.
  • Regenerate docs/API_MAP.md to reflect the updated public API map.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tutorials/tutorial_09_physicsnemo_mesh_stage_model.py New end-to-end tutorial for training a PhysicsNeMo stage-conditioned displacement model and exporting predicted meshes.
tutorials/init.py Tutorials package marker (as included in PR file list).
pyproject.toml Mypy override updates to tolerate PhysicsNeMo imports and tutorial module typing configuration.
docs/API_MAP.md Regenerated API map output.
.pre-commit-config.yaml Run mypy against both src/ and tutorials/ in pre-commit.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# tutorials also matches the style of tutorial_01.
if __name__ == "__main__":
# %%
REPO_ROOT = Path(__file__).resolve().parent.parent
Comment on lines +98 to +100
mesh_files = sorted(mesh_dir.glob("*_pca_fit.vtp"))
if len(mesh_files) < 2:
print(f"Skipping {case_prefix}: fewer than two Tutorial 8 meshes found")
Comment on lines +218 to +224
"model_state_dict": model.state_dict(),
"coordinate_mean": coordinate_mean.tolist(),
"coordinate_scale": coordinate_scale.tolist(),
"displacement_scale": displacement_scale,
"target_stage": target_stage,
"mesh_files": [str(mesh_file) for mesh_file in mesh_files],
},
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 30.62%. Comparing base (481dd76) to head (d1c8a1a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #54      +/-   ##
==========================================
+ Coverage   30.61%   30.62%   +0.01%     
==========================================
  Files          49       49              
  Lines        6808     6808              
==========================================
+ Hits         2084     2085       +1     
+ Misses       4724     4723       -1     
Flag Coverage Δ
integration-tests 30.50% <ø> (?)
unittests 30.61% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aylward aylward merged commit 248db10 into Project-MONAI:main May 20, 2026
11 of 12 checks passed
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