imaging_api.services.upload

Attributes

XNAT_URL

BASE_IMAGES_DOWNLOAD_DIR

router

Functions

upload_data_to_xnat(→ list[str])

Uploads image files to XNAT under a specific project, experiment, and scan.

create_xnat_scan(→ None)

Creates a scan in XNAT if it does not exist.

create_xnat_resource(→ None)

Creates a resource folder under a scan in XNAT if it does not exist.

check_file_exists_in_xnat(→ bool)

Checks if a file already exists in XNAT.

upload_file_to_xnat(→ str)

Uploads a file to the specified resource in XNAT.

Module Contents

imaging_api.services.upload.XNAT_URL
imaging_api.services.upload.BASE_IMAGES_DOWNLOAD_DIR
imaging_api.services.upload.router
async imaging_api.services.upload.upload_data_to_xnat(central_hub_project_id: str, accession_id: str, net_id: str, scan_id: str, resource_id: str, files_relative_paths_to_upload: list[str], exist_ok: bool, headers: dict[str, str]) list[str]

Uploads image files to XNAT under a specific project, experiment, and scan.

Parameters:
  • central_hub_project_id (str) – The central hub project ID in which the experiment belongs to. Corresponds to

  • ID. (XNAT secondary)

  • 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.

  • scan_id (str) – The ID of the scan in XNAT. Will be created if it does not exist.

  • resource_id (str) – XNAT resource type e.g DICOM/NIFTI. Custom value is allowed. Will be created if it does not

  • exist.

  • files_relative_paths_to_upload (list[str]) – List of relative file paths to be uploaded.

  • exist_ok (bool) – Whether to overwrite the file if it already exists.

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

Returns:

List of URLs of the uploaded files.

Return type:

list[str]

Raises:
  • FileNotFoundError – If any of the files to be uploaded are not found.

  • ValueError – If the net ID or any file path attempts path traversal outside the upload directory.

imaging_api.services.upload.create_xnat_scan(project_id: str, subject_id: str, experiment_id_or_label: str, scan_id: str, headers: dict[str, str]) None

Creates a scan in XNAT if it does not exist.

Normally when we upload a file to XNAT, the scan will already exist. TODO reassess if this function is necessary – could require the scan to exist

In order to create a scan, you must specify the xsiType of the scan. See https://wiki.xnat.org/xnat-api/image-session-scans-api#ImageSessionScansAPI-AddScanToAnImageSession TODO Note this is hardcoded to MR scans below (xsiType=xnat:mrScanData), but it is not really used for anything.

Note this function will not complain if the scan already exists.

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

  • subject_id (str) – The ID of the subject in XNAT.

  • experiment_id_or_label (str) – The ID or label of the experiment in XNAT.

  • scan_id (str) – The ID of the scan in XNAT.

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

Returns:

None

Raises:

Exception – If there is an error during the creation of the scan.

imaging_api.services.upload.create_xnat_resource(project_id: str, subject_id: str, experiment_id_or_label: str, scan_id: str, resource_id: str, headers: dict[str, str]) None

Creates a resource folder under a scan in XNAT if it does not exist. A resource can have any name, but it is typically “DICOM” or “NIFTI”.

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

  • subject_id (str) – The ID of the subject in XNAT.

  • experiment_id_or_label (str) – The ID or label of the experiment in XNAT.

  • scan_id (str) – The ID of the scan in XNAT.

  • resource_id (str) – The ID of the resource in XNAT.

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

Returns:

None

Raises:
  • AlreadyExistsError – If the resource already exists in XNAT.

  • Exception – If there is an error during the creation of the resource.

imaging_api.services.upload.check_file_exists_in_xnat(check_url: str, headers: dict[str, str]) bool

Checks if a file already exists in XNAT.

Parameters:
  • check_url (str) – The URL to check for the file.

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

Returns:

True if the file exists, False otherwise.

Return type:

bool

imaging_api.services.upload.upload_file_to_xnat(project_id: str, subject_id: str, experiment_id_or_label: str, scan_id: str, resource_id: str, file_path: str, exist_ok: bool, headers: dict[str, str]) str

Uploads a file to the specified resource in XNAT.

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

  • subject_id (str) – The ID of the subject in XNAT.

  • experiment_id_or_label (str) – The ID or label of the experiment in XNAT.

  • scan_id (str) – The ID of the scan in XNAT.

  • resource_id (str) – The ID of the resource in XNAT, for example “DICOM” or “NIFTI”.

  • file_path (str) – The path to the file to be uploaded.

  • exist_ok (bool) – Whether to overwrite the file if it already exists.

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

Returns:

The URL of the uploaded file.

Return type:

str

Raises:
  • AlreadyExistsError – If exist_ok is False and a file with the same name already exists in the specified XNAT

  • resource.

  • Exception – If there is an error during the upload process.