Skip to content

Commit

Permalink
Add flag for ezBIDS (#336)
Browse files Browse the repository at this point in the history
* Added flag for ezbids conversion

This will ensure that AcqusitionDate and AcqusitionTime are
included in the sidecar jsons.

* bump version create new release/tag
  • Loading branch information
bendhouseart authored Feb 6, 2025
1 parent 6d17a83 commit 0f21e8b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pypet2bids/pypet2bids/dcm2niix4pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -589,6 +591,7 @@ def run_dcm2niix(self):
check_for_missing,
dicom_header,
dicom2bids_json=metadata_dictionaries["dicom2bids"],
ezbids=self.ezbids,
**self.additional_arguments,
)

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions pypet2bids/pypet2bids/update_json_pet_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def update_json_with_dicom_value(
dicom_header,
dicom2bids_json=None,
silent=True,
ezbids=False,
**additional_arguments,
):
"""
Expand All @@ -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
"""

Expand Down Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion pypet2bids/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 0f21e8b

Please sign in to comment.