Skip to content

Commit cf5790d

Browse files
ericspodpre-commit-ci[bot]KumoLiu
authored
Fix gdown fails (#8576)
Fixes #8549. ### Description This modifies `skip_if_downloading_fails` to skip tests if `gdown` downloading fails. This should be used to temporarily account for failing tests when downloads start failing for whatever reason. A more reliable solution for hosting this data should be found so that tests can reliably download correctly, otherwise tests can be skipped and silently allow regression. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] 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. --------- Signed-off-by: Eric Kerfoot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: YunLiu <[email protected]>
1 parent cf5505a commit cf5790d

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

monai/losses/perceptual.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def __init__(
209209
) -> None:
210210
super().__init__()
211211
torch.hub._validate_not_a_forked_repo = lambda a, b, c: True
212-
self.model = torch.hub.load("warvito/MedicalNet-models", model=net, verbose=verbose)
212+
self.model = torch.hub.load("warvito/MedicalNet-models", model=net, verbose=verbose, trust_repo=True)
213213
self.eval()
214214

215215
self.channel_wise = channel_wise
@@ -297,7 +297,7 @@ class RadImageNetPerceptualSimilarity(nn.Module):
297297

298298
def __init__(self, net: str = "radimagenet_resnet50", verbose: bool = False) -> None:
299299
super().__init__()
300-
self.model = torch.hub.load("Warvito/radimagenet-models", model=net, verbose=verbose)
300+
self.model = torch.hub.load("Warvito/radimagenet-models", model=net, verbose=verbose, trust_repo=True)
301301
self.eval()
302302

303303
for param in self.parameters():

tests/test_utils.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,32 @@
5656

5757
nib, _ = optional_import("nibabel")
5858
http_error, has_req = optional_import("requests", name="HTTPError")
59+
file_url_error, has_gdown = optional_import("gdown.exceptions", name="FileURLRetrievalError")
60+
5961

6062
quick_test_var = "QUICKTEST"
6163
_tf32_enabled = None
6264
_test_data_config: dict = {}
6365

6466
MODULE_PATH = Path(__file__).resolve().parents[1]
6567

68+
DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, HTTPError, ConnectionError)
69+
if has_req:
70+
DOWNLOAD_EXCEPTS += (http_error,)
71+
if has_gdown:
72+
DOWNLOAD_EXCEPTS += (file_url_error,)
73+
74+
DOWNLOAD_FAIL_MSGS = (
75+
"unexpected EOF", # incomplete download
76+
"network issue",
77+
"gdown dependency", # gdown not installed
78+
"md5 check",
79+
"limit", # HTTP Error 503: Egress is over the account limit
80+
"authenticate",
81+
"timed out", # urlopen error [Errno 110] Connection timed out
82+
"HTTPError", # HTTPError: 429 Client Error: Too Many Requests for huggingface hub
83+
)
84+
6685

6786
def testing_data_config(*keys):
6887
"""get _test_data_config[keys0][keys1]...[keysN]"""
@@ -142,29 +161,21 @@ def assert_allclose(
142161

143162
@contextmanager
144163
def skip_if_downloading_fails():
164+
"""
165+
Skips a test if downloading something raises an exception recognised to indicate a download has failed.
166+
"""
167+
145168
try:
146169
yield
147-
except (ContentTooShortError, HTTPError, ConnectionError) + (http_error,) if has_req else () as e: # noqa: B030
148-
raise unittest.SkipTest(f"error while downloading: {e}") from e
170+
except DOWNLOAD_EXCEPTS as e:
171+
raise unittest.SkipTest(f"Error while downloading: {e}") from e
149172
except ssl.SSLError as ssl_e:
150173
if "decryption failed" in str(ssl_e):
151174
raise unittest.SkipTest(f"SSL error while downloading: {ssl_e}") from ssl_e
152175
except (RuntimeError, OSError) as rt_e:
153176
err_str = str(rt_e)
154-
if any(
155-
k in err_str
156-
for k in (
157-
"unexpected EOF", # incomplete download
158-
"network issue",
159-
"gdown dependency", # gdown not installed
160-
"md5 check",
161-
"limit", # HTTP Error 503: Egress is over the account limit
162-
"authenticate",
163-
"timed out", # urlopen error [Errno 110] Connection timed out
164-
"HTTPError", # HTTPError: 429 Client Error: Too Many Requests for huggingface hub
165-
)
166-
):
167-
raise unittest.SkipTest(f"error while downloading: {rt_e}") from rt_e # incomplete download
177+
if any(k in err_str for k in DOWNLOAD_FAIL_MSGS):
178+
raise unittest.SkipTest(f"Error while downloading: {rt_e}") from rt_e # incomplete download
168179

169180
raise rt_e
170181

0 commit comments

Comments
 (0)