From 83a807ab23270b3941a3d86512cf52848f223bb9 Mon Sep 17 00:00:00 2001 From: Anthony Galassi <28850131+bendhouseart@users.noreply.github.com> Date: Fri, 20 Dec 2024 13:37:23 -0600 Subject: [PATCH] Bug fixes for Requests not Found; Enhancment for Non-PET modalities (#328) * bump version, add requests, create new lock file * added feature to ignore non-pet files --- pypet2bids/poetry.lock | 4 +-- pypet2bids/pypet2bids/dcm2niix4pet.py | 35 +++++++++++++++++++++++ pypet2bids/pypet2bids/helper_functions.py | 29 ++++++++++--------- pypet2bids/pyproject.toml | 3 +- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/pypet2bids/poetry.lock b/pypet2bids/poetry.lock index ae2937f..ead9ea0 100644 --- a/pypet2bids/poetry.lock +++ b/pypet2bids/poetry.lock @@ -566,8 +566,8 @@ files = [ [package.dependencies] numpy = [ {version = ">=1.20.3", markers = "python_version < \"3.10\""}, - {version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, {version = ">=1.23.2", markers = "python_version >= \"3.11\""}, + {version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, ] python-dateutil = ">=2.8.2" pytz = ">=2020.1" @@ -1201,4 +1201,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = ">=3.8,<=3.12" -content-hash = "b02777dd04e01a7b11ee992b28767525b36d9ae5b29ff37241bff88871cdc483" +content-hash = "7b5718711588d124b8768e4c368908df2c3151de9b56d01c89b3ff8e4da86e51" diff --git a/pypet2bids/pypet2bids/dcm2niix4pet.py b/pypet2bids/pypet2bids/dcm2niix4pet.py index 991484b..f0cbbd4 100644 --- a/pypet2bids/pypet2bids/dcm2niix4pet.py +++ b/pypet2bids/pypet2bids/dcm2niix4pet.py @@ -301,6 +301,15 @@ def __init__( # we consider values stored in a default JSON file to be additional arguments, we load those # values first and then overwrite them with any user supplied values + # check to make sure the dicom is a PET dicom + modalities = [dicom.Modality for dicom in self.dicom_headers.values()] + non_pet_dicom = [modality for modality in modalities if modality != "PT"] + + if len(non_pet_dicom) > 0: + logger.warning( + f"Non PET dicoms found in {self.image_folder}. Modalities f{non_pet_dicom} detected" + ) + # load config file default_json_path = helper_functions.check_pet2bids_config( "DEFAULT_METADATA_JSON" @@ -500,6 +509,32 @@ def run_dcm2niix(self): join(tempdir_pathlike, file) for file in listdir(tempdir_pathlike) ] + # check to see if there are any modalities present that aren't PET + remove_these_non_pet_files = [] + for json_file in [f for f in files_created_by_dcm2niix if ".json" in f]: + with open(json_file, "r") as infile: + json_data = json.load(infile) + if json_data.get("Modality") != "PT": + logger.warning( + f"Non PET modality found in {json_file}, skipping. Files relating to this json and it's" + f"nifti will not be included at the destination folder {self.destination_folder}" + ) + remove_these_non_pet_files.append(json_file) + remove_these_non_pet_files.append( + json_file.replace(".json", ".nii.gz") + ) + remove_these_non_pet_files.append( + json_file.replace(".json", ".nii") + ) + + # if there are non PET files remove them from files_created_by_dcm2niix + if remove_these_non_pet_files: + for file in remove_these_non_pet_files: + try: + files_created_by_dcm2niix.remove(file) + except ValueError: + pass + # make sure destination path exists if not try creating it. try: if self.destination_path.exists(): diff --git a/pypet2bids/pypet2bids/helper_functions.py b/pypet2bids/pypet2bids/helper_functions.py index 4edb0a3..f8f28f8 100644 --- a/pypet2bids/pypet2bids/helper_functions.py +++ b/pypet2bids/pypet2bids/helper_functions.py @@ -125,19 +125,22 @@ def flatten_series(series): def collect_spreadsheets(folder_path: pathlib.Path): spreadsheet_files = [] - all_files = [ - folder_path / pathlib.Path(file) - for file in os.listdir(folder_path) - if os.path.isfile(os.path.join(folder_path, file)) - ] - for file in all_files: - if ( - file.suffix == ".xlsx" - or file.suffix == ".csv" - or file.suffix == ".xls" - or file.suffix == ".tsv" - ): - spreadsheet_files.append(file) + if folder_path.is_file(): + spreadsheet_files.append(folder_path) + else: + all_files = [ + folder_path / pathlib.Path(file) + for file in os.listdir(folder_path) + if os.path.isfile(os.path.join(folder_path, file)) + ] + for file in all_files: + if ( + file.suffix == ".xlsx" + or file.suffix == ".csv" + or file.suffix == ".xls" + or file.suffix == ".tsv" + ): + spreadsheet_files.append(file) return spreadsheet_files diff --git a/pypet2bids/pyproject.toml b/pypet2bids/pyproject.toml index ab015b0..a8211dd 100644 --- a/pypet2bids/pyproject.toml +++ b/pypet2bids/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pypet2bids" -version = "1.3.15" +version = "1.3.16" description = "A python library for converting PET imaging and blood data to BIDS." authors = ["anthony galassi <28850131+bendhouseart@users.noreply.github.com>"] license = "MIT" @@ -30,6 +30,7 @@ pandas = ">=1.4.4" pyxlsb = "^1.0.9" joblib = "^1.2.0" toml = ">=0.10.2" +requests = "^2.32.3" [tool.poetry.dev-dependencies]