Skip to content

Commit

Permalink
Add UPS and BWM to qrscp and UPS to findscu (#837)
Browse files Browse the repository at this point in the history
* add support for Unified Procedure Step to findscu
* incorporated minimal support for MWL and UPS model and presentation contexts, bypassing query of database and returning empty list for MWL and UPS queries
* add sample UPS Query and sample MWL CFind Query as DICOM binary files
  • Loading branch information
sjswerdloff committed Jun 23, 2023
1 parent 78d4b95 commit a5beba7
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 42 deletions.
31 changes: 31 additions & 0 deletions docs/apps/findscu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Query Information Model Options
use patient/study only information model
``-W --worklist``
use modality worklist information model
``-U --ups``
use unified procedure step pull information model

Query Options
-------------
Expand Down Expand Up @@ -185,6 +187,35 @@ SOP Classes
+-----------------------------+-----------------------------------------------+


Transfer Syntaxes
.................

+------------------------+----------------------------------------------------+
| UID | Transfer Syntax |
+========================+====================================================+
| 1.2.840.10008.1.2 | Implicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.1 | Explicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.1.99 | Deflated Explicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.2 | Explicit VR Big Endian |
+------------------------+----------------------------------------------------+

Unified Procedure Step Service
-------------------------------


SOP Classes
...........

+-----------------------------+-----------------------------------------------+
| UID | Transfer Syntax |
+=============================+===============================================+
| 1.2.840.10008.5.1.4.34.6.3 | UPS Pull Information Model - FIND |
+-----------------------------+-----------------------------------------------+


Transfer Syntaxes
.................

Expand Down
61 changes: 61 additions & 0 deletions docs/apps/qrscp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ SOP Instances sent to the application using the Storage service have some of
their attributes added to a sqlite database that is used to manage Instances
for the Query/Retrieve service.

In addition, the ``qrscp`` application implements a Service Class Provider (SCP) for the
:dcm:`Basic Modality Worklist<part04/chapter_K.html>`, and :dcm:`Unified Procedure Step<part04/Chapter_CC>`
service classes, but currently will only return empty results (0 records)

.. warning::

In addition to the standard *pynetdicom* dependencies, the ``qrscp``
Expand Down Expand Up @@ -652,6 +656,63 @@ SOP Classes
| | Model - GET |
+----------------------------------+------------------------------------------+

Transfer Syntaxes
.................

+------------------------+----------------------------------------------------+
| UID | Transfer Syntax |
+========================+====================================================+
| 1.2.840.10008.1.2 | Implicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.1 | Explicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.1.99 | Deflated Explicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.2 | Explicit VR Big Endian |
+------------------------+----------------------------------------------------+

Basic Worklist Management Service
---------------------------------

SOP Classes
...........

+-----------------------------+-----------------------------------------------+
| UID | Transfer Syntax |
+=============================+===============================================+
| 1.2.840.10008.5.1.4.31 | Modality Worklist Information Model - FIND |
+-----------------------------+-----------------------------------------------+


Transfer Syntaxes
.................

+------------------------+----------------------------------------------------+
| UID | Transfer Syntax |
+========================+====================================================+
| 1.2.840.10008.1.2 | Implicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.1 | Explicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.1.99 | Deflated Explicit VR Little Endian |
+------------------------+----------------------------------------------------+
| 1.2.840.10008.1.2.2 | Explicit VR Big Endian |
+------------------------+----------------------------------------------------+

Unified Procedure Step Service
-------------------------------


SOP Classes
...........

+-----------------------------+-----------------------------------------------+
| UID | Transfer Syntax |
+=============================+===============================================+
| 1.2.840.10008.5.1.4.34.6.3 | UPS Pull Information Model - FIND |
+-----------------------------+-----------------------------------------------+


Transfer Syntaxes
.................

Expand Down
14 changes: 13 additions & 1 deletion pynetdicom/apps/findscu/findscu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
PYNETDICOM_IMPLEMENTATION_UID,
PYNETDICOM_IMPLEMENTATION_VERSION,
PYNETDICOM_UID_PREFIX,
UnifiedProcedurePresentationContexts,
)
from pynetdicom.apps.common import create_dataset, setup_logging
from pynetdicom._globals import DEFAULT_MAX_LENGTH
from pynetdicom.pdu_primitives import SOPClassExtendedNegotiation
from pynetdicom.sop_class import (
ModalityWorklistInformationFind,
UnifiedProcedureStepPull,
PatientRootQueryRetrieveInformationModelFind,
StudyRootQueryRetrieveInformationModelFind,
PatientStudyOnlyQueryRetrieveInformationModelFind,
Expand Down Expand Up @@ -173,6 +175,12 @@ def _setup_argparser():
help="use modality worklist information model",
action="store_true",
)
qr_model.add_argument(
"-U",
"--ups",
help="use unified procedure step information model",
action="store_true",
)

qr_query = parser.add_argument_group("Query Options")
qr_query.add_argument(
Expand Down Expand Up @@ -303,12 +311,16 @@ def main(args=None):

# Set the Presentation Contexts we are requesting the Find SCP support
ae.requested_contexts = (
QueryRetrievePresentationContexts + BasicWorklistManagementPresentationContexts
QueryRetrievePresentationContexts
+ BasicWorklistManagementPresentationContexts
+ UnifiedProcedurePresentationContexts
)

# Query/Retrieve Information Models
if args.worklist:
query_model = ModalityWorklistInformationFind
elif args.ups:
query_model = UnifiedProcedureStepPull
elif args.study:
query_model = StudyRootQueryRetrieveInformationModelFind
elif args.psonly:
Expand Down
81 changes: 44 additions & 37 deletions pynetdicom/apps/qrscp/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,50 @@ def handle_find(event, db_path, cli_config, logger):

model = event.request.AffectedSOPClassUID

engine = create_engine(db_path)
with engine.connect() as conn:
Session = sessionmaker(bind=engine)
session = Session()
# Search database using Identifier as the query
try:
matches = search(model, event.identifier, session)
except InvalidIdentifier as exc:
session.rollback()
logger.error("Invalid C-FIND Identifier received")
logger.error(str(exc))
yield 0xA900, None
return
except Exception as exc:
session.rollback()
logger.error("Exception occurred while querying database")
logger.exception(exc)
yield 0xC320, None
return
finally:
session.close()

# Yield results
for match in matches:
if event.is_cancelled:
yield 0xFE00, None
return

try:
response = match.as_identifier(event.identifier, model)
response.RetrieveAETitle = event.assoc.ae.ae_title
except Exception as exc:
logger.error("Error creating response Identifier")
logger.exception(exc)
yield 0xC322, None

yield 0xFF00, response
if model.keyword in (
"UnifiedProcedureStepPull",
"ModalityWorklistInformationModelFind",
):
yield 0x0000, None
else:
engine = create_engine(db_path)
with engine.connect() as conn:
Session = sessionmaker(bind=engine)
session = Session()
# Search database using Identifier as the query
try:
matches = search(model, event.identifier, session)

except InvalidIdentifier as exc:
session.rollback()
logger.error("Invalid C-FIND Identifier received")
logger.error(str(exc))
yield 0xA900, None
return
except Exception as exc:
session.rollback()
logger.error("Exception occurred while querying database")
logger.exception(exc)
yield 0xC320, None
return
finally:
session.close()

# Yield results
for match in matches:
if event.is_cancelled:
yield 0xFE00, None
return

try:
response = match.as_identifier(event.identifier, model)
response.RetrieveAETitle = event.assoc.ae.ae_title
except Exception as exc:
logger.error("Error creating response Identifier")
logger.exception(exc)
yield 0xC322, None

yield 0xFF00, response


def handle_get(event, db_path, cli_config, logger):
Expand Down
14 changes: 13 additions & 1 deletion pynetdicom/apps/qrscp/qrscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
evt,
AllStoragePresentationContexts,
ALL_TRANSFER_SYNTAXES,
UnifiedProcedurePresentationContexts,
)
from pynetdicom import _config, _handlers
from pynetdicom.apps.common import setup_logging
from pynetdicom.sop_class import (
Verification,
ModalityWorklistInformationFind,
PatientRootQueryRetrieveInformationModelFind,
PatientRootQueryRetrieveInformationModelMove,
PatientRootQueryRetrieveInformationModelGet,
StudyRootQueryRetrieveInformationModelFind,
StudyRootQueryRetrieveInformationModelMove,
StudyRootQueryRetrieveInformationModelGet,
UnifiedProcedureStepPull,
)
from pynetdicom.utils import set_ae

Expand Down Expand Up @@ -57,7 +60,7 @@ def _dont_log(event):
_handlers._recv_c_store_rsp = _dont_log


__version__ = "1.0.1"
__version__ = "1.1.0"


def _log_config(config, logger):
Expand Down Expand Up @@ -386,6 +389,15 @@ def main(args=None):
ae.add_supported_context(StudyRootQueryRetrieveInformationModelMove)
ae.add_supported_context(StudyRootQueryRetrieveInformationModelGet)

# Unified Procedure Step SCP
for cx in UnifiedProcedurePresentationContexts:
ae.add_supported_context(
cx.abstract_syntax, ALL_TRANSFER_SYNTAXES, scp_role=True, scu_role=False
)

# Modality Worklist SCP
ae.add_supported_context(ModalityWorklistInformationFind)

# Set our handler bindings
handlers = [
(evt.EVT_C_ECHO, handle_echo, [args, APP_LOGGER]),
Expand Down
38 changes: 37 additions & 1 deletion pynetdicom/apps/tests/test_findscu.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
DEFAULT_TRANSFER_SYNTAXES,
QueryRetrievePresentationContexts,
BasicWorklistManagementPresentationContexts,
UnifiedProcedurePresentationContexts,
)
from pynetdicom.sop_class import (
Verification,
PatientRootQueryRetrieveInformationModelFind,
StudyRootQueryRetrieveInformationModelFind,
PatientStudyOnlyQueryRetrieveInformationModelFind,
ModalityWorklistInformationFind,
UnifiedProcedureStepPull,
)


Expand Down Expand Up @@ -89,6 +91,7 @@ def handle_release(event):
ae.supported_contexts = (
QueryRetrievePresentationContexts
+ BasicWorklistManagementPresentationContexts
+ UnifiedProcedurePresentationContexts
)
scp = ae.start_server(("localhost", 11112), block=False, evt_handlers=handlers)

Expand All @@ -111,7 +114,7 @@ def handle_release(event):
assert {} == requestor.sop_class_extended
assert requestor.user_identity == None
cxs = requestor.primitive.presentation_context_definition_list
assert len(cxs) == 13
assert len(cxs) == 18
cxs = {cx.abstract_syntax: cx for cx in cxs}
assert PatientRootQueryRetrieveInformationModelFind in cxs
cx = cxs[PatientRootQueryRetrieveInformationModelFind]
Expand Down Expand Up @@ -521,6 +524,39 @@ def handle_find(event):
cx = events[0].context
assert cx.abstract_syntax == (PatientStudyOnlyQueryRetrieveInformationModelFind)

def test_flag_ups(self):
"""Test the -U flag."""
events = []

def handle_find(event):
events.append(event)
yield 0x0000, None

handlers = [
(evt.EVT_C_FIND, handle_find),
]

self.ae = ae = AE()
ae.acse_timeout = 5
ae.dimse_timeout = 5
ae.network_timeout = 5
ae.supported_contexts = (
QueryRetrievePresentationContexts
+ BasicWorklistManagementPresentationContexts
+ UnifiedProcedurePresentationContexts
)
scp = ae.start_server(("localhost", 11112), block=False, evt_handlers=handlers)

p = self.func(["-U", "-k", "PatientName="])
p.wait()
assert p.returncode == 0

scp.shutdown()

assert events[0].event == evt.EVT_C_FIND
cx = events[0].context
assert cx.abstract_syntax == UnifiedProcedureStepPull

def test_flag_worklist(self):
"""Test the -W flag."""
events = []
Expand Down
3 changes: 1 addition & 2 deletions pynetdicom/apps/tests/test_storescu.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def test_no_peer(self, capfd):
p = self.func([DATASET_FILE])
p.wait()
assert p.returncode == 1

out, err = capfd.readouterr()
assert "Association request failed: unable to connect to remote" in err
assert "TCP Initialisation Error" in err
Expand Down Expand Up @@ -566,7 +565,7 @@ def handle_store(event):
ae.add_supported_context(cx.abstract_syntax, ALL_TRANSFER_SYNTAXES)
scp = ae.start_server(("localhost", 11112), block=False, evt_handlers=handlers)

p = self.func([LIB_DIR, "--recurse", "-cx"])
p = self.func([DATA_DIR, "--recurse", "-cx"])
p.wait()
assert p.returncode == 0

Expand Down

0 comments on commit a5beba7

Please sign in to comment.