From 3bf1c84c3abed920b2cae0c06bd02331ad26e118 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 9 Nov 2025 05:04:27 +0000 Subject: [PATCH 1/2] Initial plan From 2092c8a652eb7612bf02f5d4a46afb2eb6cf6fb9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 9 Nov 2025 05:12:07 +0000 Subject: [PATCH 2/2] Improve exception handling for RadiopharmaceuticalInformationSequence Co-authored-by: carluri <3673343+carluri@users.noreply.github.com> --- pytheranostics/imaging_tools/tools.py | 45 ++++++++++++++++----------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/pytheranostics/imaging_tools/tools.py b/pytheranostics/imaging_tools/tools.py index b57dd86..0be1243 100644 --- a/pytheranostics/imaging_tools/tools.py +++ b/pytheranostics/imaging_tools/tools.py @@ -78,24 +78,33 @@ def load_metadata(dir: str, modality: str) -> ImagingMetadata: radionuclide = modality.split("_")[0] # This only applies to Q-SPECT TODO: replace for something more generic. - try: - injected_activity = ( - dicom_slices[0] - .RadiopharmaceuticalInformationSequence[0] - .RadionuclideTotalDose - ) - - # Currently we don't have a way to know the units ... so we use common sense. - if injected_activity > 20000: # Activity likely in Bq instead of MBq - injected_activity /= 1e6 - print( - f"Injected activity found in DICOM Header: {injected_activity:2.1f} MBq. Please verify." - ) - - except (AttributeError, IndexError): - print( - "Injected activity not found in DICOM header. Using default: 7400 MBq" - ) + injected_activity = None + + if hasattr(dicom_slices[0], "RadiopharmaceuticalInformationSequence"): + rp_seq = dicom_slices[0].RadiopharmaceuticalInformationSequence + if len(rp_seq) > 0: + try: + injected_activity = rp_seq[0].RadionuclideTotalDose + + # Currently we don't have a way to know the units ... so we use common sense. + if injected_activity > 20000: # Activity likely in Bq instead of MBq + injected_activity /= 1e6 + print( + f"Injected activity found in DICOM Header: {injected_activity:2.1f} MBq. Please verify." + ) + except AttributeError: + # Sequence exists but RadionuclideTotalDose attribute is missing + print( + "RadiopharmaceuticalInformationSequence found but RadionuclideTotalDose is missing." + ) + else: + # Sequence exists but is empty - this may indicate a data quality issue + print( + "Warning: RadiopharmaceuticalInformationSequence is empty. This may indicate a data quality issue." + ) + + if injected_activity is None: + print("Using default injected activity: 7400 MBq") injected_activity = 7400.0 # Global attributes. Should be the same in all slices!