Skip to content

Commit 44124bd

Browse files
committed
ENH: custom_seqinfo - provide a way for heuristics to extract/add arbitrary value
Just a draft implementation
1 parent 153d522 commit 44124bd

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

heudiconv/convert.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ def prep_conversion(
221221
dcmfilter=getattr(heuristic, "filter_dicom", None),
222222
flatten=True,
223223
custom_grouping=getattr(heuristic, "grouping", None),
224+
# callable which will be provided dcminfo and returned
225+
# structure extend seqinfo
226+
custom_seqinfo = getattr(heuristic, 'custom_seqinfo', None),
224227
)
225228
elif seqinfo is None:
226229
raise ValueError("Neither 'dicoms' nor 'seqinfo' is given")

heudiconv/dicoms.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from pathlib import Path
1010
import sys
1111
import tarfile
12-
from typing import TYPE_CHECKING, Any, Dict, List, NamedTuple, Optional, Union, overload
12+
from typing import TYPE_CHECKING, Any, Dict, Hashable, List, NamedTuple, Optional, Union, overload
13+
from typing_extensions import Protocol
1314
from unittest.mock import patch
1415
import warnings
1516

@@ -42,7 +43,16 @@
4243
compresslevel = 9
4344

4445

45-
def create_seqinfo(mw: dw.Wrapper, series_files: list[str], series_id: str) -> SeqInfo:
46+
class CustomSeqinfoT(Protocol):
47+
def __call__(self, wrapper: dw.Wrapper, series_files: list[str]) -> Hashable: ...
48+
49+
50+
def create_seqinfo(
51+
mw: dw.Wrapper,
52+
series_files: list[str],
53+
series_id: str,
54+
custom_seqinfo: CustomSeqinfoT | None = None,
55+
) -> SeqInfo:
4656
"""Generate sequence info
4757
4858
Parameters
@@ -109,6 +119,9 @@ def create_seqinfo(mw: dw.Wrapper, series_files: list[str], series_id: str) -> S
109119
date=dcminfo.get("AcquisitionDate"),
110120
series_uid=dcminfo.get("SeriesInstanceUID"),
111121
time=dcminfo.get("AcquisitionTime"),
122+
custom =
123+
custom_seqinfo(wrapper=mw, series_files=series_files)
124+
if custom_seqinfo else None,
112125
)
113126

114127

@@ -199,6 +212,7 @@ def group_dicoms_into_seqinfos(
199212
dict[SeqInfo, list[str]],
200213
]
201214
| None = None,
215+
custom_seqinfo: CustomSeqinfoT | None = None,
202216
) -> dict[SeqInfo, list[str]]:
203217
...
204218

@@ -215,6 +229,7 @@ def group_dicoms_into_seqinfos(
215229
dict[SeqInfo, list[str]],
216230
]
217231
| None = None,
232+
custom_seqinfo: CustomSeqinfoT | None = None,
218233
) -> dict[Optional[str], dict[SeqInfo, list[str]]] | dict[SeqInfo, list[str]]:
219234
"""Process list of dicoms and return seqinfo and file group
220235
`seqinfo` contains per-sequence extract of fields from DICOMs which
@@ -236,9 +251,11 @@ def group_dicoms_into_seqinfos(
236251
Creates a flattened `seqinfo` with corresponding DICOM files. True when
237252
invoked with `dicom_dir_template`.
238253
custom_grouping: str or callable, optional
239-
grouping key defined within heuristic. Can be a string of a
240-
DICOM attribute, or a method that handles more complex groupings.
241-
254+
grouping key defined within heuristic. Can be a string of a
255+
DICOM attribute, or a method that handles more complex groupings.
256+
custom_seqinfo: callable, optional
257+
A callable which will be provided MosaicWrapper giving possibility to
258+
extract any custom DICOM metadata of interest.
242259
243260
Returns
244261
-------
@@ -358,7 +375,7 @@ def group_dicoms_into_seqinfos(
358375
else:
359376
# nothing to see here, just move on
360377
continue
361-
seqinfo = create_seqinfo(mw, series_files, series_id_str)
378+
seqinfo = create_seqinfo(mw, series_files, series_id_str, custom_seqinfo)
362379

363380
key: Optional[str]
364381
if per_studyUID:

heudiconv/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from typing import (
2525
Any,
2626
AnyStr,
27+
Hashable,
2728
Mapping,
2829
NamedTuple,
2930
Optional,
@@ -69,6 +70,7 @@ class SeqInfo(NamedTuple):
6970
date: Optional[str] # 24
7071
series_uid: Optional[str] # 25
7172
time: Optional[str] # 26
73+
custom: Optional[Hashable] # 27
7274

7375

7476
class StudySessionInfo(NamedTuple):

0 commit comments

Comments
 (0)