Skip to content

Perf(test): load inverse-collation fixtures once at class level - #9040

Open
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:perf/inverse-collation-fixtures-classlevel
Open

Perf(test): load inverse-collation fixtures once at class level#9040
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:perf/inverse-collation-fixtures-classlevel

Conversation

@aymuos15

@aymuos15 aymuos15 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Worktree: .claude/worktrees/inverse-collation-fixtures on perf/inverse-collation-fixtures-classlevel

Description

Fixtures for the inverse-collation tests were rebuilt in setUp on every test, re-generating the synthetic NIfTI volumes 32 times per run. The build now happens once in setUpClass, with each test taking a shallow copy of the loaded dicts. The transforms never mutate their inputs and every case re-seeds determinism, so behaviour is unchanged.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

…ect-MONAI#9040)

Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
@aymuos15
aymuos15 force-pushed the perf/inverse-collation-fixtures-classlevel branch from 9af7cce to 5d3d1ac Compare August 1, 2026 17:15
@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The inverse collation tests now check NIfTI availability and load reusable 2D and 3D datasets during class initialization. Each test reseeds deterministic behavior and receives shallow copies of the required datasets.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the performance change to load inverse-collation fixtures once at class level.
Description check ✅ Passed The description explains the change, its performance benefit, and behavior preservation, and it includes the required change-type checklist.
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.
✨ 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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/transforms/test_inverse_collation.py (2)

93-94: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add Google-style docstrings to both setup hooks.

Line 94 defines setUpClass without a docstring. Line 105 defines setUp without a docstring. Document the shared and per-test fixture attributes. Add unittest.SkipTest to the Raises section for setUpClass.

As per path instructions, docstrings must be present for all definitions and use Google-style sections.

Also applies to: 105-105

🤖 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 `@tests/transforms/test_inverse_collation.py` around lines 93 - 94, Add
Google-style docstrings to the setUpClass and setUp methods in the test fixture,
documenting the shared and per-test attributes they initialize. Include a Raises
section in setUpClass documenting unittest.SkipTest, and ensure both docstrings
use the required Google-style sections.

Source: Path instructions


107-108: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Protect the shallow-copy fixture contract.

Lines 107-108 copy only the outer dictionaries. All entries share the same loaded tensor and metadata objects. CacheDataset passes source items directly to its deterministic cache stage, so an in-place transform or metadata update could contaminate later entries or tests. (raw.githubusercontent.com)

Add an isolation regression check for the base fixtures, or deep-copy values when a transform requires it. Verify this contract for every transform in TESTS_2D and TESTS_3D.

🤖 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 `@tests/transforms/test_inverse_collation.py` around lines 107 - 108, Update
the fixture setup around data_3d and data_2d so each generated entry has
isolated tensor and metadata values rather than sharing nested objects from
base_3d/base_2d. Add regression coverage that checks this isolation for every
transform listed in TESTS_2D and TESTS_3D, preserving the existing fixture
counts and transform behavior.
🤖 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 `@tests/transforms/test_inverse_collation.py`:
- Around line 100-103: Update the fixture setup around make_nifti_image and the
class initialization to retain all generated NIfTI paths in a shared temporary
directory and register class-level cleanup before creating the fixtures. Ensure
cleanup covers both files and directories, including partial failures during
base_3d or base_2d setup, while preserving the existing load_ims inputs.

---

Nitpick comments:
In `@tests/transforms/test_inverse_collation.py`:
- Around line 93-94: Add Google-style docstrings to the setUpClass and setUp
methods in the test fixture, documenting the shared and per-test attributes they
initialize. Include a Raises section in setUpClass documenting
unittest.SkipTest, and ensure both docstrings use the required Google-style
sections.
- Around line 107-108: Update the fixture setup around data_3d and data_2d so
each generated entry has isolated tensor and metadata values rather than sharing
nested objects from base_3d/base_2d. Add regression coverage that checks this
isolation for every transform listed in TESTS_2D and TESTS_3D, preserving the
existing fixture counts and transform behavior.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 59e45b6f-f18a-4024-b487-13f60181ad2b

📥 Commits

Reviewing files that changed from the base of the PR and between 8690ae7 and 43d84c1.

📒 Files selected for processing (1)
  • tests/transforms/test_inverse_collation.py

Comment on lines +100 to +103
im_fname, seg_fname = (make_nifti_image(i) for i in create_test_image_3d(101, 100, 107))
cls.base_3d = load_ims({"image": im_fname, "label": seg_fname})
im_fname, seg_fname = (make_nifti_image(i) for i in create_test_image_2d(62, 37, rad_max=10))
load_ims = Compose([LoadImaged(KEYS), EnsureChannelFirstd(KEYS, channel_dim="no_channel")])
self.data_2d = [load_ims({"image": im_fname, "label": seg_fname}) for _ in range(b_size)]
cls.base_2d = load_ims({"image": im_fname, "label": seg_fname})

@coderabbitai coderabbitai Bot Aug 1, 2026

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.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Clean up the temporary NIfTI files.

Lines 100 and 102 create temporary files and directories, but the returned paths are discarded. make_nifti_image assigns deletion to the caller, so each run leaves four files and directories behind. (raw.githubusercontent.com)

Retain the files in one temporary directory or register class cleanup before creating the fixtures. Cover partial setup failures too.

🤖 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 `@tests/transforms/test_inverse_collation.py` around lines 100 - 103, Update
the fixture setup around make_nifti_image and the class initialization to retain
all generated NIfTI paths in a shared temporary directory and register
class-level cleanup before creating the fixtures. Ensure cleanup covers both
files and directories, including partial failures during base_3d or base_2d
setup, while preserving the existing load_ims inputs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is out of scope since this is pre-existing. Being tracked here now: #9041

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.

@aymuos15 Acknowledged. The cleanup issue is tracked in #9041, so no change is required in this pull request.

You are interacting with an AI system.

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.

1 participant