Skip to content

fix(apps/nuclick): replace np.random.choice with self.R.choice in Add…#8836

Open
Zeesejo wants to merge 1 commit intoProject-MONAI:devfrom
Zeesejo:patch-1
Open

fix(apps/nuclick): replace np.random.choice with self.R.choice in Add…#8836
Zeesejo wants to merge 1 commit intoProject-MONAI:devfrom
Zeesejo:patch-1

Conversation

@Zeesejo
Copy link
Copy Markdown
Contributor

@Zeesejo Zeesejo commented Apr 20, 2026

Fixes #6888 (partial) - follow-up to #8798

Description

In AddPointGuidanceSignald.exclusion_map (monai/apps/nuclick/transforms.py), two calls used np.random.choice (global NumPy random state) instead of self.R.choice. Since AddPointGuidanceSignald already inherits from Randomizable, this is a straightforward replacement that ensures reproducibility when set_determinism() or manual seeding is used.

Before:

if np.random.choice([True, False], p=[drop_rate, 1 - drop_rate]):

After:

if self.R.choice([True, False], p=[drop_rate, 1 - drop_rate]):

PR #8798 already fixed np.random.* calls in transforms and data utilities. This PR continues that work for the two remaining calls in apps/nuclick/transforms.py mentioned in #6888.

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.

…PointGuidanceSignald

In AddPointGuidanceSignald.exclusion_map, two calls to np.random.choice were using the global NumPy random state instead of the instance's self.R (RandomState). Since AddPointGuidanceSignald already inherits from Randomizable, replacing with self.R.choice ensures reproducibility when set_determinism() or manual seeding is used.

Ref Project-MONAI#6888
Signed-off-by: Zeeshan Modi <92383127+Zeesejo@users.noreply.github.com>

Signed-off-by: Zeeshan Modi <92383127+Zeesejo@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 20, 2026 15:22
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7feb0f3e-790c-4f83-b1e0-66e4e3706568

📥 Commits

Reviewing files that changed from the base of the PR and between 7f6b7e5 and dda343e.

📒 Files selected for processing (1)
  • monai/apps/nuclick/transforms.py

📝 Walkthrough

Walkthrough

The AddPointGuidanceSignald.exclusion_map() method in monai/apps/nuclick/transforms.py switches its randomness source from NumPy's global RNG to MONAI's seeded instance RNG (self.R). Two invocations are updated: one for early drop probability assessment and one for per-region decisions. No control flow or logic is altered.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: replacing np.random.choice with self.R.choice in AddPointGuidanceSignald, which matches the changeset.
Description check ✅ Passed The description comprehensively covers the change with context, code examples, issue references, and completed the required checklist.

✏️ 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
Contributor

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

Updates NuClick’s AddPointGuidanceSignald transform to use its local MONAI Randomizable RNG (self.R) instead of NumPy’s global RNG for drop decisions, improving reproducibility when seeding is used via set_random_state() / Compose.set_random_state().

Changes:

  • Replaced two np.random.choice(...) calls with self.R.choice(...) inside AddPointGuidanceSignald.exclusion_map.

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

Comment on lines +370 to 371
if self.R.choice([True, False], p=[drop_rate, 1 - drop_rate]):
return point_mask
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

This change is intended to improve reproducibility by using the transform's local RandomState, but there’s no regression test covering deterministic behavior. Consider adding a unit test (e.g., in tests/apps/nuclick/test_nuclick_transforms.py) that sets AddPointGuidanceSignald.set_random_state(seed=...) (and/or runs under Compose.set_random_state) and asserts the produced exclusion map / output image is identical across repeated calls with the same seed and differs with a different seed.

Copilot uses AI. Check for mistakes.
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.

consistent usages of np.RandomState in the code

2 participants