-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Raise exception for uninstalled LoadImage reader and print writer pkg… #8915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,9 +29,28 @@ def test_load_image(self): | |||||||||||||||||||||||||||||||
| self.assertIsInstance(instance1, LoadImage) | ||||||||||||||||||||||||||||||||
| self.assertIsInstance(instance2, LoadImage) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| from monai.utils import optional_import, OptionalImportError | ||||||||||||||||||||||||||||||||
| pkg_map = { | ||||||||||||||||||||||||||||||||
| "NibabelReader": "nibabel", | ||||||||||||||||||||||||||||||||
| "PILReader": "PIL", | ||||||||||||||||||||||||||||||||
| "ITKReader": "itk", | ||||||||||||||||||||||||||||||||
| "NrrdReader": "nrrd", | ||||||||||||||||||||||||||||||||
| "NumpyReader": "numpy", | ||||||||||||||||||||||||||||||||
| "PydicomReader": "pydicom", | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| for r in ["NibabelReader", "PILReader", "ITKReader", "NumpyReader", "NrrdReader", "PydicomReader", None]: | ||||||||||||||||||||||||||||||||
| inst = LoadImaged("image", reader=r) | ||||||||||||||||||||||||||||||||
| self.assertIsInstance(inst, LoadImaged) | ||||||||||||||||||||||||||||||||
| if r is None: | ||||||||||||||||||||||||||||||||
| inst = LoadImaged("image", reader=r) | ||||||||||||||||||||||||||||||||
| self.assertIsInstance(inst, LoadImaged) | ||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _, has_pkg = optional_import(pkg_map[r]) | ||||||||||||||||||||||||||||||||
| if has_pkg: | ||||||||||||||||||||||||||||||||
| inst = LoadImaged("image", reader=r) | ||||||||||||||||||||||||||||||||
| self.assertIsInstance(inst, LoadImaged) | ||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||
| with self.assertRaises(OptionalImportError): | ||||||||||||||||||||||||||||||||
| LoadImaged("image", reader=r) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
rg -n --type=py 'optional_import\("itk"' -C2Repository: Project-MONAI/MONAI Length of output: 5327 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the exact code under review
sed -n '1,140p' tests/data/test_init_reader.py | nl -ba | sed -n '35,90p'
# Find the pkg_map definition / ITKReader mapping and the exact optional_import call site
rg -n "pkg_map|ITKReader|LoadImaged|test_load_image|optional_import" tests/data/test_init_reader.py -n
# Inspect optional_import signature/docs in repo
optional_import_file="$(rg -n --type=py "def optional_import" -S . | head -n 1 | cut -d: -f1)"
echo "Found optional_import definition in: $optional_import_file"
sed -n '1,200p' "$optional_import_file" | nl -ba | sed -n '1,200p'Repository: Project-MONAI/MONAI Length of output: 106 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Show reviewed section with line numbers (no `nl` in this sandbox)
sed -n '1,120p' tests/data/test_init_reader.py | cat -n | sed -n '35,90p'
# Locate the exact test and the ITK probe line
rg -n "TestInitLoadImage|test_load_image|pkg_map|optional_import|ITKReader|LoadImaged" tests/data/test_init_reader.py
# Find optional_import implementation and show its signature/defaults
opt_file="$(rg -n --type=py "def optional_import" -S . | head -n 1 | cut -d: -f1)"
echo "optional_import definition: $opt_file"
sed -n '1,220p' "$opt_file" | cat -n | sed -n '1,220p'Repository: Project-MONAI/MONAI Length of output: 13955 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Get line number of optional_import definition
opt_def_line="$(rg -n --type=py "^def optional_import\\b" monai/utils/module.py | head -n 1 | cut -d: -f1)"
echo "optional_import def line: $opt_def_line"
# Print a window around the function start (signature + docstring header)
start=$((opt_def_line-20))
end=$((opt_def_line+120))
sed -n "${start},${end}p" monai/utils/module.py | cat -n
# Also find any occurrences of allow_namespace_pkg inside the function/file
rg -n "allow_namespace_pkg" monai/utils/module.py | head -n 50Repository: Project-MONAI/MONAI Length of output: 7091 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Find where ITKReader is resolved/used during LoadImaged/LoadImage
rg -n "ITKReader" monai -S
rg -n "LoadImaged\\(|LoadImage\\(" monai/transforms -S
rg -n "reader=.*ITKReader|resolve_reader|resolve_reader\\(|image_reader" monai -S
# Inspect relevant reader resolution code (likely monai/data/image_reader.py and monai/data/__init__.py / transforms loader)
sed -n '1,220p' monai/data/image_reader.py | cat -n
# Also inspect image_writer if needed (for writer mapping consistency)
sed -n '1,140p' monai/data/image_writer.py | cat -nRepository: Project-MONAI/MONAI Length of output: 21170 🏁 Script executed: #!/bin/bash
set -euo pipefail
rg -n --type=py "^def require_pkg\\b" monai/utils/module.py
req_file="monai/utils/module.py"
req_line="$(rg -n --type=py "^def require_pkg\\b" "$req_file" | head -n 1 | cut -d: -f1)"
echo "require_pkg def line: $req_line"
start=$((req_line-20))
end=$((req_line+220))
sed -n "${start},${end}p" "$req_file" | cat -n
# See whether allow_namespace_pkg is referenced inside require_pkg
rg -n "allow_namespace_pkg" "$req_file" | head -n 50Repository: Project-MONAI/MONAI Length of output: 10172 🏁 Script executed: #!/bin/bash
set -euo pipefail
rg -n "has_itk|\\bitk\\b" monai/data/image_reader.pyRepository: Project-MONAI/MONAI Length of output: 1442 Handle ITK as a namespace package in
Suggested change- _, has_pkg = optional_import(pkg_map[r])
+ import_kwargs = {"allow_namespace_pkg": True} if pkg_map[r] == "itk" else {}
+ _, has_pkg = optional_import(pkg_map[r], **import_kwargs)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @SkipIfNoModule("nibabel") | ||||||||||||||||||||||||||||||||
| @SkipIfNoModule("cupy") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Project-MONAI/MONAI
Length of output: 2624
🏁 Script executed:
Repository: Project-MONAI/MONAI
Length of output: 5311
Add
stacklevel=2to the warning inLoadImage.__init__.monai/transforms/io/array.pyline 217 useswarnings.warn(...)withoutstacklevel, so the warning location points at MONAI internals instead of the user call site.Suggested change
🧰 Tools
🪛 Ruff (0.15.15)
[warning] 217-217: No explicit
stacklevelkeyword argument foundSet
stacklevel=2(B028)
🤖 Prompt for AI Agents
Source: Linters/SAST tools