Skip to content

Commit

Permalink
Merge branch 'dev' into issue-7980-print-package
Browse files Browse the repository at this point in the history
  • Loading branch information
ericspod authored Feb 12, 2025
2 parents deff1e9 + af9e8f9 commit 13868c6
Show file tree
Hide file tree
Showing 879 changed files with 3,303 additions and 1,589 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
if pgrep python; then pkill python; fi
shell: bash
- name: Upload coverage
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: false
files: ./coverage.xml
Expand Down Expand Up @@ -123,7 +123,7 @@ jobs:
if pgrep python; then pkill python; fi
shell: bash
- name: Upload coverage
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: false
files: ./coverage.xml
Expand Down Expand Up @@ -228,7 +228,7 @@ jobs:
if pgrep python; then pkill python; fi
shell: bash
- name: Upload coverage
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: false
files: ./coverage.xml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ jobs:
shell: bash
- name: Upload coverage
if: ${{ github.head_ref != 'dev' && github.event.pull_request.merged != true }}
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
files: ./coverage.xml
4 changes: 2 additions & 2 deletions .github/workflows/setupapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
if pgrep python; then pkill python; fi
shell: bash
- name: Upload coverage
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: false
files: ./coverage.xml
Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
BUILD_MONAI=1 ./runtests.sh --build --quick --min
coverage xml --ignore-errors
- name: Upload coverage
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: false
files: ./coverage.xml
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ tests/testing_data/nrrd_example.nrrd
# clang format tool
.clang-format-bin/

# ctags
tags

# VSCode
.vscode/
*.zip
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ repos:
)$
- repo: https://github.com/hadialqattan/pycln
rev: v2.4.0
rev: v2.5.0
hooks:
- id: pycln
args: [--config=pyproject.toml]
5 changes: 5 additions & 0 deletions docs/source/networks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ Nets
.. autoclass:: ViTAutoEnc
:members:

`MaskedAutoEncoderViT`
~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: MaskedAutoEncoderViT
:members:

`FullyConnectedNet`
~~~~~~~~~~~~~~~~~~~
.. autoclass:: FullyConnectedNet
Expand Down
24 changes: 24 additions & 0 deletions docs/source/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,18 @@ Utility
:members:
:special-members: __call__

`TorchIO`
"""""""""
.. autoclass:: TorchIO
:members:
:special-members: __call__

`RandTorchIO`
"""""""""""""
.. autoclass:: RandTorchIO
:members:
:special-members: __call__

`MapLabelValue`
"""""""""""""""
.. autoclass:: MapLabelValue
Expand Down Expand Up @@ -2253,6 +2265,18 @@ Utility (Dict)
:members:
:special-members: __call__

`TorchIOd`
""""""""""
.. autoclass:: TorchIOd
:members:
:special-members: __call__

`RandTorchIOd`
""""""""""""""
.. autoclass:: RandTorchIOd
:members:
:special-members: __call__

`MapLabelValued`
""""""""""""""""
.. autoclass:: MapLabelValued
Expand Down
1 change: 1 addition & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ channels:
dependencies:
- numpy>=1.24,<2.0
- pytorch>=1.9
- torchio
- torchvision
- pytorch-cuda>=11.6
- pip
Expand Down
39 changes: 38 additions & 1 deletion monai/apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
import logging
import os
import re
import shutil
import sys
import tarfile
Expand All @@ -30,7 +31,9 @@
from monai.config.type_definitions import PathLike
from monai.utils import look_up_option, min_version, optional_import

requests, has_requests = optional_import("requests")
gdown, has_gdown = optional_import("gdown", "4.7.3")
BeautifulSoup, has_bs4 = optional_import("bs4", name="BeautifulSoup")

if TYPE_CHECKING:
from tqdm import tqdm
Expand Down Expand Up @@ -298,6 +301,29 @@ def extractall(
)


def get_filename_from_url(data_url: str) -> str:
"""
Get the filename from the URL link.
"""
try:
response = requests.head(data_url, allow_redirects=True)
content_disposition = response.headers.get("Content-Disposition")
if content_disposition:
filename = re.findall('filename="?([^";]+)"?', content_disposition)
if filename:
return str(filename[0])
if "drive.google.com" in data_url:
response = requests.get(data_url)
if "text/html" in response.headers.get("Content-Type", ""):
soup = BeautifulSoup(response.text, "html.parser")
filename_div = soup.find("span", {"class": "uc-name-size"})
if filename_div:
return str(filename_div.find("a").text)
return _basename(data_url)
except Exception as e:
raise Exception(f"Error processing URL: {e}") from e


def download_and_extract(
url: str,
filepath: PathLike = "",
Expand Down Expand Up @@ -327,7 +353,18 @@ def download_and_extract(
be False.
progress: whether to display progress bar.
"""
url_filename_ext = "".join(Path(get_filename_from_url(url)).suffixes)
filepath_ext = "".join(Path(_basename(filepath)).suffixes)
if filepath not in ["", "."]:
if filepath_ext == "":
new_filepath = Path(filepath).with_suffix(url_filename_ext)
logger.warning(
f"filepath={filepath}, which missing file extension. Auto-appending extension to: {new_filepath}"
)
filepath = new_filepath
if filepath_ext and filepath_ext != url_filename_ext:
raise ValueError(f"File extension mismatch: expected extension {url_filename_ext}, but get {filepath_ext}")
with tempfile.TemporaryDirectory() as tmp_dir:
filename = filepath or Path(tmp_dir, _basename(url)).resolve()
filename = filepath or Path(tmp_dir, get_filename_from_url(url)).resolve()
download_url(url=url, filepath=filename, hash_val=hash_val, hash_type=hash_type, progress=progress)
extractall(filepath=filename, output_dir=output_dir, file_type=file_type, has_base=has_base)
2 changes: 1 addition & 1 deletion monai/bundle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
MACRO_KEY,
load_bundle_config,
)
from .workflows import BundleWorkflow, ConfigWorkflow
from .workflows import BundleWorkflow, ConfigWorkflow, PythonicWorkflow
25 changes: 20 additions & 5 deletions monai/bundle/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _get_git_release_url(repo_owner: str, repo_name: str, tag_name: str, filenam


def _get_ngc_bundle_url(model_name: str, version: str) -> str:
return f"{NGC_BASE_URL}/{model_name.lower()}/versions/{version}/zip"
return f"{NGC_BASE_URL}/{model_name.lower()}/versions/{version}/files"


def _get_ngc_private_base_url(repo: str) -> str:
Expand Down Expand Up @@ -218,6 +218,21 @@ def _remove_ngc_prefix(name: str, prefix: str = "monai_") -> str:
return name


def _get_all_download_files(request_url: str, headers: dict | None = None) -> list[str]:
if not has_requests:
raise ValueError("requests package is required, please install it.")
headers = {} if headers is None else headers
response = requests_get(request_url, headers=headers)
response.raise_for_status()
model_info = json.loads(response.text)

if not isinstance(model_info, dict) or "modelFiles" not in model_info:
raise ValueError("The data is not a dictionary or it does not have the key 'modelFiles'.")

model_files = model_info["modelFiles"]
return [f["path"] for f in model_files]


def _download_from_ngc(
download_path: Path,
filename: str,
Expand All @@ -229,12 +244,12 @@ def _download_from_ngc(
# ensure prefix is contained
filename = _add_ngc_prefix(filename, prefix=prefix)
url = _get_ngc_bundle_url(model_name=filename, version=version)
filepath = download_path / f"{filename}_v{version}.zip"
if remove_prefix:
filename = _remove_ngc_prefix(filename, prefix=remove_prefix)
extract_path = download_path / f"{filename}"
download_url(url=url, filepath=filepath, hash_val=None, progress=progress)
extractall(filepath=filepath, output_dir=extract_path, has_base=True)
filepath = download_path / filename
filepath.mkdir(parents=True, exist_ok=True)
for file in _get_all_download_files(url):
download_url(url=f"{url}/{file}", filepath=f"{filepath}/{file}", hash_val=None, progress=progress)


def _download_from_ngc_private(
Expand Down
Loading

0 comments on commit 13868c6

Please sign in to comment.