Skip to content

Commit

Permalink
Fix dictifying of query dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack authored Aug 14, 2023
1 parent 70411a5 commit fc9726b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
11 changes: 6 additions & 5 deletions adit/core/utils/dicom_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ def StudyDescription(self) -> str:
@property
def ModalitiesInStudy(self) -> list[str]:
modalities = self._ds.ModalitiesInStudy
if not modalities:
return []
# Cave, in Python string are also iterable, so we test it first
if isinstance(modalities, str):
return [modalities]
if isinstance(modalities, Iterable):
return [str(modality) for modality in modalities]
return [str(modalities)]
return []

@property
def NumberOfStudyRelatedSeries(self) -> int:
Expand Down Expand Up @@ -190,7 +191,7 @@ def from_dict(cls, query: dict[str, str], **additional_attributes) -> "QueryData

return QueryDataset(ds)

def to_dicomweb(self) -> dict[str, str]:
def dictify(self) -> dict[str, str]:
"""Create DICOMweb compatible query dict.
Convert a query dataset to a DICOMweb compatible query dict.
Expand All @@ -213,7 +214,7 @@ def to_dicomweb(self) -> dict[str, str]:

if val in ("*", "?", None):
query[key] = ""
elif isinstance(val, Iterable):
elif not isinstance(val, str) and isinstance(val, Iterable):
query[key] = ",".join(val)
else:
# VR of DA, TM, DT are already in string format
Expand Down
22 changes: 11 additions & 11 deletions adit/core/utils/dicom_web_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def send_qido_rs(
) -> list[ResultDataset]:
logger.debug("Sending QIDO-RS with query: %s", query)

dw_query = query.to_dicomweb()
query_dict = query.dictify()

level = dw_query.pop("QueryRetrieveLevel", "")
level = query_dict.pop("QueryRetrieveLevel", "")
if not level:
raise ValueError("Missing QueryRetrieveLevel.")

Expand All @@ -95,17 +95,17 @@ def send_qido_rs(
try:
if level == "STUDY":
results = self.dicomweb_client.search_for_studies(
search_filters=dw_query, limit=limit_results
search_filters=query_dict, limit=limit_results
)
elif level == "SERIES":
study_uid = dw_query.pop("StudyInstanceUID", "")
study_uid = query_dict.pop("StudyInstanceUID", "")
if study_uid:
results = self.dicomweb_client.search_for_series(
study_uid, search_filters=dw_query, limit=limit_results
study_uid, search_filters=query_dict, limit=limit_results
)
else:
results = self.dicomweb_client.search_for_series(
search_filters=dw_query, limit=limit_results
search_filters=query_dict, limit=limit_results
)
else:
raise ValueError(f"Invalid QueryRetrieveLevel: {level}")
Expand All @@ -119,9 +119,9 @@ def send_wado_rs(self, query: QueryDataset) -> list[Dataset]:
logger.debug("Sending WADO-RS with query: %s", query)

# TODO: Do we really need the conversion?
dw_query = query.to_dicomweb()
query_dict = query.dictify()

level = dw_query.pop("QueryRetrieveLevel", "")
level = query_dict.pop("QueryRetrieveLevel", "")
if not level:
raise ValueError("Missing QueryRetrieveLevel.")

Expand All @@ -133,13 +133,13 @@ def send_wado_rs(self, query: QueryDataset) -> list[Dataset]:

try:
if level == "STUDY":
study_uid = dw_query.pop("StudyInstanceUID", "")
study_uid = query_dict.pop("StudyInstanceUID", "")
if not study_uid:
raise ValueError("Missing StudyInstanceUID for WADO-RS on study level.")
return self.dicomweb_client.retrieve_study(study_uid)
elif level == "SERIES":
study_uid = dw_query.pop("StudyInstanceUID", "")
series_uid = dw_query.pop("SeriesInstanceUID", "")
study_uid = query_dict.pop("StudyInstanceUID", "")
series_uid = query_dict.pop("SeriesInstanceUID", "")
if not study_uid:
raise ValueError("Missing StudyInstanceUID for WADO-RS on series level.")
if not series_uid:
Expand Down

0 comments on commit fc9726b

Please sign in to comment.