Skip to content

Commit e4eb5e9

Browse files
committed
backwards compatibility
1 parent c5e3589 commit e4eb5e9

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

heudiconv/dicoms.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,15 @@ def get_datetime_from_dcm(dcm_data: dcm.FileDataset) -> Optional[datetime.dateti
536536
3. SeriesDate & SeriesTime (0008,0021); (0008,0031)
537537
538538
"""
539-
if "AcquisitionDate" in dcm_data and "AcquisitionTime" in dcm_data:
539+
540+
def check_tag(x):
541+
return x in dcm_data and dcm_data[x].value.strip()
542+
543+
if check_tag("AcquisitionDate") and check_tag("AcquisitionTime"):
540544
return strptime_dcm_da_tm(dcm_data, "AcquisitionDate", "AcquisitionTime")
541-
if "AcquisitionDateTime" in dcm_data:
545+
if check_tag("AcquisitionDateTime"):
542546
return strptime_dcm_dt(dcm_data, "AcquisitionDateTime")
543-
if "SeriesDate" in dcm_data and "SeriesTime" in dcm_data:
547+
if check_tag("SeriesDate") and check_tag("SeriesTime"):
544548
return strptime_dcm_da_tm(dcm_data, "SeriesDate", "SeriesTime")
545549
return None
546550

heudiconv/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ def strptime(datetime_string: str, fmts: list[str]) -> datetime.datetime:
724724
fmts: list[str]
725725
List of format strings
726726
"""
727-
datetime_str = datetime_string.strip()
727+
datetime_str = datetime_string
728728
for fmt in fmts:
729729
try:
730730
return datetime.datetime.strptime(datetime_str, fmt)
@@ -769,20 +769,20 @@ def strptime_dcm_da_tm(
769769
Dicom tag with TM value representation
770770
"""
771771
# https://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.2.html
772-
date_str = dcm_data[da_tag].value.strip()
772+
date_str = dcm_data[da_tag].value
773773
fmts = [
774774
"%Y%m%d",
775775
]
776776
date = strptime(date_str, fmts)
777777

778-
time_str = dcm_data[tm_tag].value.strip()
778+
time_str = dcm_data[tm_tag].value
779779
fmts = ["%H", "%H%M", "%H%M%S", "%H%M%S.%f"]
780780
time = strptime(time_str, fmts)
781781

782782
datetime_obj = datetime.datetime.combine(date.date(), time.time())
783783

784784
if utc_offset_dcm := dcm_data.get((0x0008, 0x0201)):
785-
utc_offset = utc_offset_dcm.value.strip()
785+
utc_offset = utc_offset_dcm.value
786786
datetime_obj = (
787787
datetime_utc_offset(datetime_obj, utc_offset)
788788
if utc_offset
@@ -804,7 +804,7 @@ def strptime_dcm_dt(dcm_data: dcm.Dataset, dt_tag: TagType) -> datetime.datetime
804804
Dicom tag with DT value representation
805805
"""
806806
# https://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.2.html
807-
datetime_str = dcm_data[dt_tag].value.strip()
807+
datetime_str = dcm_data[dt_tag].value
808808
fmts = [
809809
"%Y%z",
810810
"%Y%m%z",
@@ -824,7 +824,7 @@ def strptime_dcm_dt(dcm_data: dcm.Dataset, dt_tag: TagType) -> datetime.datetime
824824
datetime_obj = strptime(datetime_str, fmts)
825825

826826
if utc_offset_dcm := dcm_data.get((0x0008, 0x0201)):
827-
if utc_offset := utc_offset_dcm.value.strip():
827+
if utc_offset := utc_offset_dcm.value:
828828
datetime_obj2 = datetime_utc_offset(datetime_obj, utc_offset)
829829
if datetime_obj.tzinfo and datetime_obj2 != datetime_obj:
830830
lgr.warning(

0 commit comments

Comments
 (0)