Skip to content

Commit

Permalink
Merge pull request #718 from yarikoptic/enh-uniqe
Browse files Browse the repository at this point in the history
ENH: give informative assertion message when multiple values are found
  • Loading branch information
yarikoptic authored Nov 20, 2023
2 parents c275b93 + 24127ff commit 5374088
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
20 changes: 13 additions & 7 deletions heudiconv/heuristics/reproin.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,14 @@ def from_series_info(name: str) -> Optional[str]:
# For scouts -- we want only dicoms
# https://github.com/nipy/heudiconv/issues/145
outtype: tuple[str, ...]
if "_Scout" in s.series_description or (
datatype == "anat"
and datatype_suffix
and datatype_suffix.startswith("scout")
) or (
s.series_description.lower() == s.protocol_name.lower() + "_setter"
if (
"_Scout" in s.series_description
or (
datatype == "anat"
and datatype_suffix
and datatype_suffix.startswith("scout")
)
or (s.series_description.lower() == s.protocol_name.lower() + "_setter")
):
outtype = ("dicom",)
else:
Expand Down Expand Up @@ -725,7 +727,11 @@ def get_unique(seqinfos: list[SeqInfo], attr: str) -> Any:
"""
values = set(getattr(si, attr) for si in seqinfos)
assert len(values) == 1
if len(values) != 1:
raise AssertionError(
f"Was expecting a single value for attribute {attr!r} "
f"but got: {', '.join(sorted(values))}"
)
return values.pop()


Expand Down
32 changes: 26 additions & 6 deletions heudiconv/heuristics/test_reproin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,29 @@
from typing import NamedTuple
from unittest.mock import patch

import pytest

from . import reproin
from .reproin import (
filter_files,
fix_canceled_runs,
fix_dbic_protocol,
fixup_subjectid,
get_dups_marked,
get_unique,
md5sum,
parse_series_spec,
sanitize_str,
)


class FakeSeqInfo(NamedTuple):
accession_number: str
study_description: str
field1: str
field2: str


def test_get_dups_marked() -> None:
no_dups: dict[tuple[str, tuple[str, ...], None], list[int]] = {
("some", ("foo",), None): [1]
Expand Down Expand Up @@ -97,12 +107,6 @@ class FakeSeqInfo(NamedTuple):


def test_fix_dbic_protocol() -> None:
class FakeSeqInfo(NamedTuple):
accession_number: str
study_description: str
field1: str
field2: str

accession_number = "A003"
seq1 = FakeSeqInfo(
accession_number,
Expand Down Expand Up @@ -235,3 +239,19 @@ def test_parse_series_spec() -> None:
"session": "01",
"dir": "AP",
}


def test_get_unique() -> None:
accession_number = "A003"
acqs = [
FakeSeqInfo(accession_number, "mystudy", "nochangeplease", "nochangeeither"),
FakeSeqInfo(accession_number, "mystudy2", "nochangeplease", "nochangeeither"),
]

assert get_unique(acqs, "accession_number") == accession_number # type: ignore[arg-type]
with pytest.raises(AssertionError) as ce:
get_unique(acqs, "study_description") # type: ignore[arg-type]
assert (
str(ce.value)
== "Was expecting a single value for attribute 'study_description' but got: mystudy, mystudy2"
)

0 comments on commit 5374088

Please sign in to comment.