diff --git a/pypet2bids/pypet2bids/dcm2niix4pet.py b/pypet2bids/pypet2bids/dcm2niix4pet.py index 40021c8..bc21e60 100644 --- a/pypet2bids/pypet2bids/dcm2niix4pet.py +++ b/pypet2bids/pypet2bids/dcm2niix4pet.py @@ -175,6 +175,7 @@ def __init__( file_format="%p_%i_%t_%s", silent=False, tempdir_location=None, + ezbids=False ): """ This class is a simple wrapper for dcm2niix and contains methods to do the following in order: @@ -215,6 +216,7 @@ def __init__( self.blood_json = None self.blood_tsv = None self.telemetry_data = {} + self.ezbids=ezbids self.dcm2niix_path = self.check_for_dcm2niix() if not self.dcm2niix_path: logger.error( @@ -589,6 +591,7 @@ def run_dcm2niix(self): check_for_missing, dicom_header, dicom2bids_json=metadata_dictionaries["dicom2bids"], + ezbids=self.ezbids, **self.additional_arguments, ) @@ -1162,6 +1165,13 @@ def cli(): "This information helps to improve PET2BIDS and provides an indicator of real world " "usage crucial for obtaining funding.", ) + parser.add_argument( + "--ezbids", + action="store_true", + default=False, + help="Add fields to json output that are useful for ezBIDS or other conversion software. This will de-anonymize" + " pet2bids output and add AcquisitionDate an AcquisitionTime into the output json." + ) return parser @@ -1328,6 +1338,7 @@ def main(): additional_arguments=cli_args.kwargs, tempdir_location=cli_args.tempdir, silent=cli_args.silent, + ezbids=cli_args.ezbids, ) if cli_args.trc: diff --git a/pypet2bids/pypet2bids/update_json_pet_file.py b/pypet2bids/pypet2bids/update_json_pet_file.py index f32d7b0..ce1c6be 100644 --- a/pypet2bids/pypet2bids/update_json_pet_file.py +++ b/pypet2bids/pypet2bids/update_json_pet_file.py @@ -149,6 +149,7 @@ def update_json_with_dicom_value( dicom_header, dicom2bids_json=None, silent=True, + ezbids=False, **additional_arguments, ): """ @@ -160,6 +161,9 @@ def update_json_with_dicom_value( :param missing_values: dictionary output from check_json indicating missing fields and/or values :param dicom_header: the dicom or dicoms that may contain information not picked up by dcm2niix :param dicom2bids_json: a json file that maps dicom header entities to their corresponding BIDS entities + :param silent: run silently without error, status, or warning messages + :param ezbids: boolean to supply additional data that ezbids or other software requires, defaults to false. When + true the sidecar json will be updated with AcquisitionDate, AcquisitionTime, and AcquisitionDateTime :return: a dictionary of successfully updated (written to the json file) fields and values """ @@ -297,6 +301,20 @@ def update_json_with_dicom_value( # remove scandate if it exists json_updater.remove("ScanDate") + # lastly if ezbids is true update the sidecar with acquisition data + if ezbids: + acquisition_date = parser.parse(dicom_header.get("AcquisitionDate", "")) + acquisition_time = parser.parse(dicom_header.get("AcquisitionTime", "")) + if acquisition_time and acquisition_date: + acquisition_datetime = datetime.datetime.combine(acquisition_date.date(), acquisition_time.time()) + else: + acquisition_datetime = "0000-00-00T00:00:00" + json_updater.update( + {"AcquisitionDate": f"{acquisition_date.date()}", + "AcquisitionTime": f"{acquisition_time.time()}", + "AcquisitionDateTime": f"{acquisition_datetime.isoformat()}" + }) + # after updating raise warnings to user if values in json don't match values in dicom headers, only warn! updated_values = json.load(open(path_to_json, "r")) for key, value in paired_fields.items(): diff --git a/pypet2bids/pyproject.toml b/pypet2bids/pyproject.toml index ef5d984..d656cb4 100644 --- a/pypet2bids/pyproject.toml +++ b/pypet2bids/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pypet2bids" -version = "1.3.17" +version = "1.4.0" description = "A python library for converting PET imaging and blood data to BIDS." authors = ["anthony galassi <28850131+bendhouseart@users.noreply.github.com>"] license = "MIT"