From 7cc1fee6ef445777e8949f70c78a839b3fdfb72f Mon Sep 17 00:00:00 2001 From: "Dr.Hari_krishna" Date: Mon, 13 Oct 2025 00:26:13 +0530 Subject: [PATCH 1/2] Add test_env.py and test_writer_error.py for error handling patch (#7980) --- test_env.py | 3 +++ test_writer_error.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test_env.py create mode 100644 test_writer_error.py diff --git a/test_env.py b/test_env.py new file mode 100644 index 0000000000..1b29bfcef0 --- /dev/null +++ b/test_env.py @@ -0,0 +1,3 @@ +import torch, monai +print("Torch version:", torch.__version__) +print("MONAI version:", monai.__version__) diff --git a/test_writer_error.py b/test_writer_error.py new file mode 100644 index 0000000000..a8a1edbd80 --- /dev/null +++ b/test_writer_error.py @@ -0,0 +1,18 @@ +from monai.data.image_writer import resolve_writer, OptionalImportError, SUPPORTED_WRITERS, EXT_WILDCARD, ITKWriter + +ext = "fakeext" + +print(f"Before clearing fallback writers: {SUPPORTED_WRITERS.get(EXT_WILDCARD)}") + +# Temporarily clear fallback writers to simulate no support +SUPPORTED_WRITERS[EXT_WILDCARD] = () + +try: + writers = resolve_writer(ext, error_if_not_found=True) +except OptionalImportError as e: + print("Caught OptionalImportError:", e) +finally: + # Restore the fallback writers to avoid side effects + SUPPORTED_WRITERS[EXT_WILDCARD] = (ITKWriter,) + +print(f"After restoring fallback writers: {SUPPORTED_WRITERS.get(EXT_WILDCARD)}") From 14ccd76924297ea0947ad926e3ef153753398282 Mon Sep 17 00:00:00 2001 From: "Dr.Hari_krishna" Date: Mon, 13 Oct 2025 01:06:36 +0530 Subject: [PATCH 2/2] Fix #7980: Add detailed error messages with installation suggestions for image writers, cleanup tests --- test_env.py | 15 ++++++++++++++- test_writer_error.py | 7 +++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/test_env.py b/test_env.py index 1b29bfcef0..a85b35fb69 100644 --- a/test_env.py +++ b/test_env.py @@ -1,3 +1,16 @@ -import torch, monai +""" +Manual environment check for versions of PyTorch and MONAI. + +This script helps verify the runtime versions of key libraries during manual testing +of writer error messages or environment troubleshooting. + +Note: +- This is intended for manual execution only. +- Consider moving to a diagnostics folder or integrating into automated tests. +""" + +import torch +import monai + print("Torch version:", torch.__version__) print("MONAI version:", monai.__version__) diff --git a/test_writer_error.py b/test_writer_error.py index a8a1edbd80..a3f413aeb1 100644 --- a/test_writer_error.py +++ b/test_writer_error.py @@ -1,18 +1,21 @@ from monai.data.image_writer import resolve_writer, OptionalImportError, SUPPORTED_WRITERS, EXT_WILDCARD, ITKWriter +# Fake extension to simulate unsupported file type ext = "fakeext" print(f"Before clearing fallback writers: {SUPPORTED_WRITERS.get(EXT_WILDCARD)}") -# Temporarily clear fallback writers to simulate no support +# Temporarily clear fallback writers to simulate no support scenario SUPPORTED_WRITERS[EXT_WILDCARD] = () try: + # Try resolving writer for fake unsupported extension with error flag writers = resolve_writer(ext, error_if_not_found=True) except OptionalImportError as e: + # Catch and print the enhanced OptionalImportError with package hints print("Caught OptionalImportError:", e) finally: - # Restore the fallback writers to avoid side effects + # Restore the fallback writers to avoid side effects on other tests SUPPORTED_WRITERS[EXT_WILDCARD] = (ITKWriter,) print(f"After restoring fallback writers: {SUPPORTED_WRITERS.get(EXT_WILDCARD)}")