From 60c936f2072e997b697c88b3fe1c9a3862fadd32 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Date: Tue, 2 Sep 2025 00:35:53 +0100 Subject: [PATCH 1/5] Change skip_if_downloading_fails to skip gdown failures Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> --- tests/test_utils.py | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 784b25f663..ded8119c22 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -56,6 +56,8 @@ nib, _ = optional_import("nibabel") http_error, has_req = optional_import("requests", name="HTTPError") +file_url_error, has_gdown = optional_import("gdown.exceptions", name="FileURLRetrievalError") + quick_test_var = "QUICKTEST" _tf32_enabled = None @@ -63,6 +65,23 @@ MODULE_PATH = Path(__file__).resolve().parents[1] +DOWNLOAD_EXCEPTS = (ContentTooShortError, HTTPError, ConnectionError) +if has_req: + DOWNLOAD_EXCEPTS += (http_error,) +if has_gdown: + DOWNLOAD_EXCEPTS += (file_url_error,) + +DOWNLOAD_FAIL_MSGS = ( + "unexpected EOF", # incomplete download + "network issue", + "gdown dependency", # gdown not installed + "md5 check", + "limit", # HTTP Error 503: Egress is over the account limit + "authenticate", + "timed out", # urlopen error [Errno 110] Connection timed out + "HTTPError", # HTTPError: 429 Client Error: Too Many Requests for huggingface hub +) + def testing_data_config(*keys): """get _test_data_config[keys0][keys1]...[keysN]""" @@ -142,29 +161,21 @@ def assert_allclose( @contextmanager def skip_if_downloading_fails(): + """ + Skips a test if downloading something raises an exception recognised to indicate a download has failed. + """ + try: yield - except (ContentTooShortError, HTTPError, ConnectionError) + (http_error,) if has_req else () as e: # noqa: B030 - raise unittest.SkipTest(f"error while downloading: {e}") from e + except DOWNLOAD_EXCEPTS as e: # noqa: B030 + raise unittest.SkipTest(f"Error while downloading: {e}") from e except ssl.SSLError as ssl_e: if "decryption failed" in str(ssl_e): raise unittest.SkipTest(f"SSL error while downloading: {ssl_e}") from ssl_e except (RuntimeError, OSError) as rt_e: err_str = str(rt_e) - if any( - k in err_str - for k in ( - "unexpected EOF", # incomplete download - "network issue", - "gdown dependency", # gdown not installed - "md5 check", - "limit", # HTTP Error 503: Egress is over the account limit - "authenticate", - "timed out", # urlopen error [Errno 110] Connection timed out - "HTTPError", # HTTPError: 429 Client Error: Too Many Requests for huggingface hub - ) - ): - raise unittest.SkipTest(f"error while downloading: {rt_e}") from rt_e # incomplete download + if any(k in err_str for k in DOWNLOAD_FAIL_MSGS): + raise unittest.SkipTest(f"Error while downloading: {rt_e}") from rt_e # incomplete download raise rt_e From d8a7c24f5a915a14d80b06f3f896e072aede4b93 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Date: Tue, 2 Sep 2025 00:36:09 +0100 Subject: [PATCH 2/5] Slight fix for torch.hub.load use Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> --- monai/losses/perceptual.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/losses/perceptual.py b/monai/losses/perceptual.py index ee653fac9d..2ae03bc8dc 100644 --- a/monai/losses/perceptual.py +++ b/monai/losses/perceptual.py @@ -209,7 +209,7 @@ def __init__( ) -> None: super().__init__() torch.hub._validate_not_a_forked_repo = lambda a, b, c: True - self.model = torch.hub.load("warvito/MedicalNet-models", model=net, verbose=verbose) + self.model = torch.hub.load("warvito/MedicalNet-models", model=net, verbose=verbose, trust_repo=True) self.eval() self.channel_wise = channel_wise @@ -297,7 +297,7 @@ class RadImageNetPerceptualSimilarity(nn.Module): def __init__(self, net: str = "radimagenet_resnet50", verbose: bool = False) -> None: super().__init__() - self.model = torch.hub.load("Warvito/radimagenet-models", model=net, verbose=verbose) + self.model = torch.hub.load("Warvito/radimagenet-models", model=net, verbose=verbose, trust_repo=True) self.eval() for param in self.parameters(): From ad3aacb34cedc76e5c28acdb97ef17e2840e3380 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Sep 2025 11:58:35 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index ded8119c22..99c4b94583 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -167,7 +167,7 @@ def skip_if_downloading_fails(): try: yield - except DOWNLOAD_EXCEPTS as e: # noqa: B030 + except DOWNLOAD_EXCEPTS as e: raise unittest.SkipTest(f"Error while downloading: {e}") from e except ssl.SSLError as ssl_e: if "decryption failed" in str(ssl_e): From b1e7cee5a81711650ac637dd57266a2888c311ea Mon Sep 17 00:00:00 2001 From: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Date: Fri, 19 Sep 2025 16:30:44 +0100 Subject: [PATCH 4/5] Type update Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> --- tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index ded8119c22..4384e73011 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -65,7 +65,7 @@ MODULE_PATH = Path(__file__).resolve().parents[1] -DOWNLOAD_EXCEPTS = (ContentTooShortError, HTTPError, ConnectionError) +DOWNLOAD_EXCEPTS: tuple[type,...] = (ContentTooShortError, HTTPError, ConnectionError) if has_req: DOWNLOAD_EXCEPTS += (http_error,) if has_gdown: From 1527539abc2ac348c6687e5122a53d6190d669dd Mon Sep 17 00:00:00 2001 From: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Date: Fri, 19 Sep 2025 16:56:20 +0100 Subject: [PATCH 5/5] Fix Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> --- tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index cb4ed58bb1..f87b16fb71 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -65,7 +65,7 @@ MODULE_PATH = Path(__file__).resolve().parents[1] -DOWNLOAD_EXCEPTS: tuple[type,...] = (ContentTooShortError, HTTPError, ConnectionError) +DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, HTTPError, ConnectionError) if has_req: DOWNLOAD_EXCEPTS += (http_error,) if has_gdown: