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

Seriesgrouping #684

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions heudiconv/dicoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import TYPE_CHECKING, Any, Dict, List, NamedTuple, Optional, Union, overload
from unittest.mock import patch
import warnings
import hashlib

import pydicom as dcm

Expand Down Expand Up @@ -114,7 +115,7 @@ def create_seqinfo(mw: dw.Wrapper, series_files: list[str], series_id: str) -> S

def validate_dicom(
fl: str, dcmfilter: Optional[Callable[[dcm.dataset.Dataset], Any]]
) -> Optional[tuple[dw.Wrapper, tuple[int, str], Optional[str]]]:
) -> Optional[tuple[dw.Wrapper, tuple[int, str, str], Optional[str]]]:
"""
Parse DICOM attributes. Returns None if not valid.
"""
Expand All @@ -135,7 +136,7 @@ def validate_dicom(
try:
protocol_name = mw.dcm_data.ProtocolName
assert isinstance(protocol_name, str)
series_id = (int(mw.dcm_data.SeriesNumber), protocol_name)
series_id = (int(mw.dcm_data.SeriesNumber), protocol_name, mw.dcm_data.SeriesInstanceUID)
except AttributeError as e:
lgr.warning('Ignoring %s since not quite a "normal" DICOM: %s', fl, e)
return None
Expand All @@ -159,10 +160,12 @@ def validate_dicom(
class SeriesID(NamedTuple):
series_number: int
protocol_name: str
series_uid: str
file_studyUID: Optional[str] = None

def __str__(self) -> str:
s = f"{self.series_number}-{self.protocol_name}"
suid_hex = hashlib.md5(self.series_uid.encode('utf-8')).hexdigest()[:16]
yarikoptic marked this conversation as resolved.
Show resolved Hide resolved
s = f"{self.series_number}-{self.protocol_name}-{suid_hex}"
if self.file_studyUID is not None:
s += f"-{self.file_studyUID}"
return s
Expand Down Expand Up @@ -285,7 +288,7 @@ def group_dicoms_into_seqinfos(
removeidx.append(idx)
continue
mw, series_id_, file_studyUID = mwinfo
series_id = SeriesID(series_id_[0], series_id_[1])
series_id = SeriesID(series_id_[0], series_id_[1], series_id_[2])
if per_studyUID:
series_id = series_id._replace(file_studyUID=file_studyUID)

Expand Down Expand Up @@ -320,6 +323,7 @@ def group_dicoms_into_seqinfos(
series_id = SeriesID(
mwgroup[idx].dcm_data.SeriesNumber,
mwgroup[idx].dcm_data.ProtocolName,
mwgroup[idx].dcm_data.SeriesInstanceUID,
)
if per_studyUID:
series_id = series_id._replace(file_studyUID=file_studyUID)
Expand Down
14 changes: 4 additions & 10 deletions heudiconv/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,9 @@ def infotoids(
)
lgr.info("Study session for %r", study_session_info)

if study_session_info in study_sessions:
if grouping != "all":
# MG - should this blow up to mimic -d invocation?
lgr.warning(
"Existing study session with the same values (%r)."
" Skipping DICOMS %s",
study_session_info,
seqinfo.values(),
)
continue
if grouping != "all":
assert (study_session_info not in study_sessions), (
f"Existing study session {study_session_info} "
f"already in analyzed sessions {study_sessions.keys()}")
study_sessions[study_session_info] = seqinfo
return study_sessions