Skip to content

Commit e332430

Browse files
committed
Fix guard both OptionalImportError raises with raise_on_missing_reader flag
Both raise statements in LoadImage.__init__ now use the same guarding mechanism via the raise_on_missing_reader flag. This ensures consistent behavior when dealing with missing readers whether they are unrecognized names or missing optional dependencies. Also applied code formatting fixes. Signed-off-by: Sarah <[email protected]>
1 parent dae4264 commit e332430

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

monai/transforms/io/array.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,16 @@ def __init__(
214214
the_reader = look_up_option(_r.lower(), SUPPORTED_READERS)
215215
except ValueError:
216216
# If the reader name is not recognized at all, raise OptionalImportError
217-
raise OptionalImportError(
218-
f"Cannot find reader '{_r}'. It may not be installed or recognized."
219-
)
217+
if self.raise_on_missing_reader:
218+
raise OptionalImportError(
219+
f"Cannot find reader '{_r}'. It may not be installed or recognized."
220+
)
221+
else:
222+
warnings.warn(
223+
f"Cannot find reader '{_r}'. It may not be installed or recognized. "
224+
f"Will use fallback readers if available."
225+
)
226+
continue
220227
try:
221228
self.register(the_reader(*args, **kwargs))
222229
except OptionalImportError as e:

0 commit comments

Comments
 (0)