imaging_api.services.download

Attributes

XNAT_URL

BASE_IMAGES_DOWNLOAD_DIR

Functions

download_and_unzip_images(→ str)

Downloads XNAT experiment image data corresponding to a given accession ID and encrypted central hub project id.

format_download_url(→ str)

Formats the XNAT API URL to download experiment scan images.

download_file(→ str)

Downloads a file from the given URL using an XNAT auth headers.

unzip_file(→ str)

Extracts a ZIP file and renames the directory.

Module Contents

imaging_api.services.download.XNAT_URL
imaging_api.services.download.BASE_IMAGES_DOWNLOAD_DIR
async imaging_api.services.download.download_and_unzip_images(central_hub_project_id: str, accession_id: str, net_id: str, assessor_type: str, resource_type: str, headers: dict[str, str]) str

Downloads XNAT experiment image data corresponding to a given accession ID and encrypted central hub project id.

If data exists for the experiment, all image scan resources will be downloaded and the location returned.

All data is downloaded in a single .zip file. Once the file download is complete, this method will then attempt to unzip the folder and delete the existing .zip file.

Parameters:
  • central_hub_project_id (str) – Central Hub project ID. Corresponds to XNAT secondary ID.

  • accession_id (str) – The unique value for a study stored in PACS. Corresponds to XNAT experiment label.

  • net_id (str) – The ID of the FL net that will run the training.

  • assessor_type (str) – The type of assessor to use for the download (“scan” or “assessor”).

  • resource_type (str) – XNAT resource type e.g DICOM/NIFTI. ALL will download all resources. Custom value is

  • scans. (allowed if researcher has added their own custom XNAT resource type into)

  • headers (dict[str, str]) – XNAT authentication headers.

Returns:

The path to the unzipped folder containing the downloaded images.

Return type:

str

Raises:
  • imaging_api.utils.exceptions.NotFoundError – If the project with the given ID is not found, if no experiments are

  • found for the given accession ID, if no data is found at the download URL, or if the ZIP file is not found after

  • download.

  • ValueError – If the net ID attempts path traversal outside the base images directory.

  • Exception – If there is an error during any of the requests to XNAT, during the download process, or during the

  • unzipping process.

imaging_api.services.download.format_download_url(project_id: str, subject_id: str, experiment_id_or_label: str, assessor_type: str = 'scan', resource_type: str = 'NIFTI') str

Formats the XNAT API URL to download experiment scan images.

Parameters:
  • project_id (str) – XNAT project ID.

  • subject_id (str) – XNAT subject ID.

  • experiment_id_or_label (str) – XNAT experiment ID or its label.

  • assessor_type (str) – Type of assessor (scan or assessor).

  • resource_type (str) – Resource type (e.g. NIFTI, DICOM, etc.).

Returns:

Formatted URL for downloading images.

Return type:

str

imaging_api.services.download.download_file(url: str, destination_path: str, headers: dict[str, str]) str

Downloads a file from the given URL using an XNAT auth headers.

Parameters:
  • url (str) – URL to download the file from.

  • destination_path (str) – Path to save the downloaded file.

  • headers (dict[str, str]) – XNAT authentication headers.

Returns:

Path to the downloaded file.

Return type:

str

Raises:
imaging_api.services.download.unzip_file(zip_path: str, extract_dir: str, new_name: str) str

Extracts a ZIP file and renames the directory.

Parameters:
  • zip_path (str) – Path to the ZIP file.

  • extract_dir (str) – Directory to extract the contents.

  • new_name (str) – New name for the extracted directory.

Returns:

Path to the renamed directory.

Return type:

str

Raises:
  • FileNotFoundError – If the ZIP file does not exist.

  • ValueError – If the ZIP file contains path traversal entries (zip slip), or if new_name attempts path traversal outside extract_dir.