Skip to content

Commit c5e3589

Browse files
committed
Apply suggestions from code review
1 parent d47bc9c commit c5e3589

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

heudiconv/bids.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,7 @@ def select_fmap_from_compatible_groups(
961961
)
962962
# differences in acquisition time (abs value):
963963
diff_fmaps_acq_times = {
964-
k: abs(strptime_bids(v) - json_acq_time)
965-
for k, v in acq_times_fmaps.items()
964+
k: abs(strptime_bids(v) - json_acq_time) for k, v in acq_times_fmaps.items()
966965
}
967966
min_diff_acq_times = sorted(diff_fmaps_acq_times.values())[0]
968967
selected_fmap_key = [

heudiconv/dicoms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
load_json,
3434
set_readonly,
3535
strptime_dcm_da_tm,
36-
strptime_dcm_dt
36+
strptime_dcm_dt,
3737
)
3838

3939
if TYPE_CHECKING:

heudiconv/tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
remove_prefix,
2424
remove_suffix,
2525
save_json,
26-
strptime_micr,
2726
strptime_bids,
2827
strptime_dcm_da_tm,
2928
strptime_dcm_dt,
29+
strptime_micr,
3030
update_json,
3131
)
3232

@@ -222,7 +222,7 @@ def test_strptime_bids(dt: str, fmt: str) -> None:
222222
"offset, offset_fmt",
223223
[
224224
("-0900", "%z"),
225-
('', ''),
225+
("", ""),
226226
],
227227
)
228228
def test_strptime_dcm_da_tm(tm: str, tm_fmt: str, offset: str, offset_fmt: str) -> None:
@@ -260,7 +260,7 @@ def test_strptime_dcm_da_tm(tm: str, tm_fmt: str, offset: str, offset_fmt: str)
260260
"offset, offset_fmt",
261261
[
262262
("-0900", "%z"),
263-
('', ''),
263+
("", ""),
264264
],
265265
)
266266
def test_strptime_dcm_dt(dt: str, dt_fmt: str, offset: str, offset_fmt: str) -> None:

heudiconv/utils.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import os
1414
import os.path as op
1515
from pathlib import Path
16-
import pydicom as dcm
17-
from pydicom.tag import TagType
1816
import re
1917
import shutil
2018
import stat
@@ -39,6 +37,9 @@
3937
)
4038
import warnings
4139

40+
import pydicom as dcm
41+
from pydicom.tag import TagType
42+
4243
lgr = logging.getLogger(__name__)
4344

4445
T = TypeVar("T")
@@ -697,21 +698,23 @@ def strptime_micr(date_string: str, fmt: str) -> datetime.datetime:
697698
return datetime.datetime.strptime(date_string, fmt)
698699

699700

700-
def datetime_utc_offset(datetime_obj: datetime.datetime, utc_offset: str) -> datetime.datetime:
701+
def datetime_utc_offset(
702+
datetime_obj: datetime.datetime, utc_offset: str
703+
) -> datetime.datetime:
701704
"""set the datetime's tzinfo by parsing an utc offset string"""
702705
# https://dicom.innolitics.com/ciods/electromyogram/sop-common/00080201
703706
extract_offset = re.match(r"([+\-]?)(\d{2})(\d{2})", utc_offset)
704707
if extract_offset is None:
705708
raise ValueError(f"utc offset {utc_offset} is not valid")
706709
sign, hours, minutes = extract_offset.groups()
707-
sign = -1 if sign == '-' else 1
710+
sign = -1 if sign == "-" else 1
708711
hours, minutes = int(hours), int(minutes)
709712
tzinfo = datetime.timezone(sign * datetime.timedelta(hours=hours, minutes=minutes))
710713
return datetime_obj.replace(tzinfo=tzinfo)
711714

712715

713716
def strptime(datetime_string: str, fmts: list[str]) -> datetime.datetime:
714-
r"""
717+
"""
715718
Try datetime.strptime on a list of formats returning the first successful attempt.
716719
717720
Parameters
@@ -731,7 +734,7 @@ def strptime(datetime_string: str, fmts: list[str]) -> datetime.datetime:
731734

732735

733736
def strptime_bids(datetime_string: str) -> datetime.datetime:
734-
r"""
737+
"""
735738
Create a datetime object from a bids datetime string.
736739
737740
Parameters
@@ -740,13 +743,20 @@ def strptime_bids(datetime_string: str) -> datetime.datetime:
740743
Datetime string to parse
741744
"""
742745
# https://bids-specification.readthedocs.io/en/stable/common-principles.html#units
743-
fmts = ["%Y-%m-%dT%H:%M:%S.%f%z", "%Y-%m-%dT%H:%M:%S%z", "%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S"]
746+
fmts = [
747+
"%Y-%m-%dT%H:%M:%S.%f%z",
748+
"%Y-%m-%dT%H:%M:%S%z",
749+
"%Y-%m-%dT%H:%M:%S.%f",
750+
"%Y-%m-%dT%H:%M:%S",
751+
]
744752
datetime_obj = strptime(datetime_string, fmts)
745753
return datetime_obj
746754

747755

748-
def strptime_dcm_da_tm(dcm_data: dcm.Dataset, da_tag: TagType, tm_tag: TagType) -> datetime.datetime:
749-
r"""
756+
def strptime_dcm_da_tm(
757+
dcm_data: dcm.Dataset, da_tag: TagType, tm_tag: TagType
758+
) -> datetime.datetime:
759+
"""
750760
Create a datetime object from a dicom DA tag and TM tag.
751761
752762
Parameters
@@ -760,7 +770,9 @@ def strptime_dcm_da_tm(dcm_data: dcm.Dataset, da_tag: TagType, tm_tag: TagType)
760770
"""
761771
# https://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.2.html
762772
date_str = dcm_data[da_tag].value.strip()
763-
fmts = ["%Y%m%d",]
773+
fmts = [
774+
"%Y%m%d",
775+
]
764776
date = strptime(date_str, fmts)
765777

766778
time_str = dcm_data[tm_tag].value.strip()
@@ -771,12 +783,16 @@ def strptime_dcm_da_tm(dcm_data: dcm.Dataset, da_tag: TagType, tm_tag: TagType)
771783

772784
if utc_offset_dcm := dcm_data.get((0x0008, 0x0201)):
773785
utc_offset = utc_offset_dcm.value.strip()
774-
datetime_obj = datetime_utc_offset(datetime_obj, utc_offset) if utc_offset else datetime_obj
786+
datetime_obj = (
787+
datetime_utc_offset(datetime_obj, utc_offset)
788+
if utc_offset
789+
else datetime_obj
790+
)
775791
return datetime_obj
776792

777793

778794
def strptime_dcm_dt(dcm_data: dcm.Dataset, dt_tag: TagType) -> datetime.datetime:
779-
r"""
795+
"""
780796
Create a datetime object from a dicom DT tag.
781797
782798
Parameters
@@ -789,15 +805,33 @@ def strptime_dcm_dt(dcm_data: dcm.Dataset, dt_tag: TagType) -> datetime.datetime
789805
"""
790806
# https://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.2.html
791807
datetime_str = dcm_data[dt_tag].value.strip()
792-
fmts = ["%Y%z", "%Y%m%z", "%Y%m%d%z", "%Y%m%d%H%z", "%Y%m%d%H%M%z", "%Y%m%d%H%M%S%z", "%Y%m%d%H%M%S.%f%z",
793-
"%Y", "%Y%m", "%Y%m%d", "%Y%m%d%H", "%Y%m%d%H%M", "%Y%m%d%H%M%S", "%Y%m%d%H%M%S.%f"]
808+
fmts = [
809+
"%Y%z",
810+
"%Y%m%z",
811+
"%Y%m%d%z",
812+
"%Y%m%d%H%z",
813+
"%Y%m%d%H%M%z",
814+
"%Y%m%d%H%M%S%z",
815+
"%Y%m%d%H%M%S.%f%z",
816+
"%Y",
817+
"%Y%m",
818+
"%Y%m%d",
819+
"%Y%m%d%H",
820+
"%Y%m%d%H%M",
821+
"%Y%m%d%H%M%S",
822+
"%Y%m%d%H%M%S.%f",
823+
]
794824
datetime_obj = strptime(datetime_str, fmts)
795825

796826
if utc_offset_dcm := dcm_data.get((0x0008, 0x0201)):
797827
if utc_offset := utc_offset_dcm.value.strip():
798828
datetime_obj2 = datetime_utc_offset(datetime_obj, utc_offset)
799829
if datetime_obj.tzinfo and datetime_obj2 != datetime_obj:
800-
lgr.warning("Unexpectedly previously parsed datetime %s contains zoneinfo which is different from the one obtained from DICOMs UTFOffset field: %s", datetime_obj, datetime_obj2)
830+
lgr.warning(
831+
"Unexpectedly previously parsed datetime %s contains zoneinfo which is different from the one obtained from DICOMs UTFOffset field: %s",
832+
datetime_obj,
833+
datetime_obj2,
834+
)
801835
else:
802836
datetime_obj = datetime_obj2
803837
return datetime_obj

0 commit comments

Comments
 (0)