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

ReproIn: give an informative assertion message when multiple values are found #718

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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"
)