-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement matching of DICOM tag values
- Loading branch information
1 parent
c2f31db
commit d651296
Showing
10 changed files
with
255 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import re | ||
from collections.abc import Sequence | ||
|
||
from serie.dicom_series_metadata import DicomSeriesMetadata | ||
from serie.models import DicomSeriesMatcher | ||
from serie.series_file_pair import DicomSeriesFilePair | ||
|
||
|
||
def is_match( | ||
series: DicomSeriesFilePair, conditions: Sequence[DicomSeriesMatcher] | ||
) -> bool: | ||
""" | ||
:return: True if the series matches the conditions | ||
""" | ||
series_dict = series.to_dict() | ||
return all(_matches(cond, series_dict) for cond in conditions) | ||
|
||
|
||
def _matches(condition: DicomSeriesMatcher, series_dict: DicomSeriesMetadata) -> bool: | ||
if condition.tag.value not in series_dict: | ||
return False | ||
value = series_dict[condition.tag.value] | ||
flag = re.IGNORECASE if condition.case_sensitive else re.NOFLAG | ||
return condition.regex.fullmatch(value) is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import dataclasses | ||
|
||
import aiochris.models | ||
|
||
from serie.dicom_series_metadata import DicomSeriesMetadata, DicomSeriesMetadataName | ||
from serie.models import OxidicomCustomMetadata | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class DicomSeriesFilePair: | ||
""" | ||
A product type which can provide all the fields listed in :class:`DicomSeriesMetadataName`. | ||
""" | ||
|
||
ocm: OxidicomCustomMetadata | ||
pacs_file: aiochris.models.PACSFile | ||
|
||
@property | ||
def series_dir(self) -> str: | ||
return self.ocm.series_dir | ||
|
||
def to_dict(self) -> DicomSeriesMetadata: | ||
""" | ||
Create a dict with all the keys from the variants of :class:`DicomSeriesMetadataName`. | ||
""" | ||
values = {} | ||
for name in DicomSeriesMetadataName: | ||
values[name.value] = getattr(self.pacs_file, name.value, None) or getattr( | ||
self.ocm, name.value | ||
) | ||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
import pydicom | ||
import pynetdicom | ||
import pytest | ||
import requests | ||
|
||
import tests.e2e_config as config | ||
|
||
|
||
def download_and_send_dicom(url: str, ae_title: str): | ||
_send_dicom(_get_sample_dicom(url), ae_title) | ||
|
||
|
||
def _get_sample_dicom(url: str) -> Path: | ||
tmp_dicom = Path(__file__).parent.parent / ".test_data" / _basename(url) | ||
if tmp_dicom.exists(): | ||
return tmp_dicom | ||
tmp_dicom.parent.mkdir(parents=True, exist_ok=True) | ||
with requests.get(url, stream=True) as r: | ||
r.raise_for_status() | ||
with tmp_dicom.open("wb") as f: | ||
for chunk in r.iter_content(chunk_size=8192): | ||
f.write(chunk) | ||
return tmp_dicom | ||
|
||
|
||
def _send_dicom(dicom_file: str | os.PathLike, ae_title: str): | ||
""" | ||
See https://pydicom.github.io/pynetdicom/stable/examples/storage.html#storage-scu | ||
""" | ||
ds = pydicom.dcmread(dicom_file) | ||
ae = pynetdicom.AE(ae_title=ae_title) | ||
ae.add_requested_context(ds.file_meta.MediaStorageSOPClassUID) | ||
assoc = ae.associate( | ||
config.OXIDICOM_HOST, config.OXIDICOM_PORT, ae_title=config.OXIDICOM_AET | ||
) | ||
if not assoc.is_established: | ||
raise pytest.fail("Could not establish association with oxidicom") | ||
status = assoc.send_c_store(ds) | ||
if not status: | ||
raise pytest.fail( | ||
"Failed to send data to oxidicom: " | ||
"connection timed out, was aborted or received invalid response" | ||
) | ||
assoc.release() | ||
|
||
|
||
def _basename(s: str) -> str: | ||
split = s.rsplit("/", maxsplit=1) | ||
return s if len(split) == 1 else split[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.