From d22b66968a8e306de04e360d446fde37592fd1b2 Mon Sep 17 00:00:00 2001 From: Anthony Galassi <28850131+bendhouseart@users.noreply.github.com> Date: Fri, 20 Dec 2024 13:26:46 -0600 Subject: [PATCH] added feature to ignore non-pet files --- pypet2bids/pypet2bids/dcm2niix4pet.py | 35 +++++++++++++++++++++++ pypet2bids/pypet2bids/helper_functions.py | 29 ++++++++++--------- pypet2bids/pyproject.toml | 3 +- 3 files changed, 53 insertions(+), 14 deletions(-) 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]