Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor create_seqinfo tiny bit to avoid duplication and add logging; and in tests to reuse list of dicom paths #785

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions heudiconv/dicoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,19 @@ def create_seqinfo(
series_desc = get_typed_attr(dcminfo, "SeriesDescription", str, "")
protocol_name = get_typed_attr(dcminfo, "ProtocolName", str, "")

if dcminfo.get([0x18, 0x24]):
# GE and Philips
sequence_name = dcminfo[0x18, 0x24].value
elif dcminfo.get([0x19, 0x109C]):
# Siemens
sequence_name = dcminfo[0x19, 0x109C].value
elif dcminfo.get([0x18, 0x9005]):
# Siemens XA
sequence_name = dcminfo[0x18, 0x9005].value
for k, m in (
([0x18, 0x24], "GE and Philips"),
([0x19, 0x109C], "Siemens"),
([0x18, 0x9005], "Siemens XA"),
):
if v := dcminfo.get(k):
sequence_name = v.value
lgr.debug(
"Identified sequence name as %s coming from the %r family of MR scanners",
sequence_name,
m,
)
break
else:
sequence_name = ""

Expand Down
25 changes: 8 additions & 17 deletions heudiconv/tests/test_dicoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
parse_private_csa_header,
)

from .utils import TESTS_DATA_PATH
from .utils import TEST_DICOM_PATHS, TESTS_DATA_PATH

# Public: Private DICOM tags
DICOM_FIELDS_TO_TEST = {"ProtocolName": "tProtocolName"}
Expand Down Expand Up @@ -180,26 +180,17 @@ def test_get_datetime_from_dcm_wo_dt() -> None:
assert get_datetime_from_dcm(XA30_enhanced_dcm) is None


dicom_test_data = [
(dw.wrapper_from_file(d_file), [d_file], op.basename(d_file))
for d_file in glob(op.join(TESTS_DATA_PATH, "*.dcm"))
]


@pytest.mark.parametrize("mw,series_files,series_id", dicom_test_data)
@pytest.mark.parametrize("dcmfile", TEST_DICOM_PATHS)
def test_create_seqinfo(
mw: dw.Wrapper,
series_files: list[str],
series_id: str,
dcmfile,
yarikoptic marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
seqinfo = create_seqinfo(mw, series_files, series_id)
assert seqinfo.sequence_name != ""
pass

mw = dw.wrapper_from_file(dcmfile)
seqinfo = create_seqinfo(mw, [dcmfile], op.basename(dcmfile))
assert seqinfo.sequence_name

def test_get_reproducible_int() -> None:
dcmfile = op.join(TESTS_DATA_PATH, "phantom.dcm")

@pytest.mark.parametrize("dcmfile", TEST_DICOM_PATHS)
def test_get_reproducible_int(dcmfile) -> None:
yarikoptic marked this conversation as resolved.
Show resolved Hide resolved
assert type(get_reproducible_int([dcmfile])) is int


Expand Down
9 changes: 9 additions & 0 deletions heudiconv/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from glob import glob
import logging
import os.path as op
from pathlib import Path
Expand All @@ -9,6 +10,14 @@

HEURISTICS_PATH = op.join(heudiconv.heuristics.__path__[0])
TESTS_DATA_PATH = op.join(op.dirname(__file__), "data")
# Do relative to curdir to shorten in a typical application,
# and side-effect test that tests do not change curdir.
TEST_DICOM_PATHS = [
op.relpath(x)
for x in glob(op.join(TESTS_DATA_PATH, "**/*.dcm"), recursive=True)
# exclude PhoenixDocuments
if "PhoenixDocument" not in x
]

lgr = logging.getLogger(__name__)

Expand Down
Loading