
PyOrthanc
PyOrthanc is a comprehensive Python client for Orthanc, providing:
- Complete wrapping of the Orthanc REST API methods
- High-level utilities for common DICOM operations
- Asynchronous client support
- Helper functions for working with DICOM data
- Integration with the Orthanc Python plugin
Why PyOrthanc?
PyOrthanc makes it easy to work with DICOM medical images stored on Orthanc servers using Python - instead
of dealing with the DICOM protocol directly or creating complex code to interact with Orthanc's REST API.
Researchers and clinicians can make simple Python script to access and manage their medical imaging data.
Advanced users can use PyOrthanc to make Orthanc query a hospital PACS (Picture Archiving and Communication System).
This allows to find and retrieve images produced in the clinic for research or quality control purposes.
Additionally, since PyOrthanc simplifies Orthanc's anonymization operations,
an entire medical image management workflow can be implemented in Python.
Quick Install
pip install pyorthanc
pip install pyorthanc[all]
Basic Usage
Assuming an Orthanc server running locally at http://localhost:8042
:
from pyorthanc import Orthanc, upload
client = Orthanc('http://localhost:8042')
client = Orthanc('http://localhost:8042', username='orthanc', password='orthanc')
patient_ids = client.get_patients()
studies = client.get_studies()
upload(client, 'image_path.dcm')
Working with DICOM Modalities
from pyorthanc import Modality
modality = Modality(client, 'REMOTE_PACS')
if modality.echo():
print("Successfully connected to PACS")
response = modality.find({
'Level': 'Study',
'Query': {
'PatientID': '12345*',
'StudyDate': '20230101-20231231'
}
})
response['answers']
modality.move(response['ID'], {'TargetAet': 'ORTHANC'})
Finding and Processing DICOM Data
from pyorthanc import find_patients, find_studies, find_series, find_instances
patients = find_patients(
client,
query={'PatientName': '*Gabriel'},
labels=['research']
)
for patient in patients:
print(f"Patient: {patient.name} (ID: {patient.patient_id})")
print(f"Birth Date: {patient.birth_date}")
print(f"Labels: {patient.labels}")
for study in patient.studies:
print(f"\nStudy Date: {study.date}")
print(f"Description: {study.description}")
for series in study.series:
print(f"\nModality: {series.modality}")
print(f"Series Description: {series.description}")
for instance in series.instances:
ds = instance.get_pydicom()
find_studies(client, query={...})
find_series(client, query={...})
find_instances(client, query={...})
Using pyorthanc within Orthanc's Python plugin
Use the orthanc_sdk
module when using Orthanc's Python plugin.
orthanc_sdk
acts as the same as orthanc
, but it provides type hints and autocompletion.
For example:
from pyorthanc import orthanc_sdk
def handle_api(output: orthanc_sdk.RestOutput, uri: str, **request):
"""Handle REST API request"""
if request['method'] == 'GET':
output.AnswerBuffer('Hello from plugin!', 'text/plain')
else:
output.SendMethodNotAllowed('GET')
orthanc_sdk.RegisterRestCallback('/hello-world', handle_api)
def on_store(dicom: orthanc_sdk.DicomInstance, instance_id: str):
"""Process stored DICOM instances"""
print(f'Received instance {instance_id}')
print(f'Size: {dicom.GetInstanceSize()} bytes')
print(f'Transfer Syntax: {dicom.GetInstanceTransferSyntaxUid()}')
orthanc_sdk.RegisterOnStoredInstanceCallback(on_store)
Examples
Typical example can be found in these notebooks.
- This notebook shows
how a user can query image data from an Orthanc server
- This notebook shows
how a user can query and pull data from other modality (such as a CT scan or a PACS) connected to an Orthanc Server.
Notes on versioning
The Orthanc
and AsyncOrthanc
classes are generated from https://orthanc.uclouvain.be/api/.
Compatibility of versions between PyOrthanc and the Orthanc REST API are the following.
Note that recent PyOrthanc versions will likely support older Orthanc version.
PyOrthanc version | Generated from |
---|
>= 1.19.0 | Orthanc API 1.12.5 with Python Plugin 4.2 |
1.18.0 | Orthanc API 1.12.4 with Python Plugin 4.2 |
1.17.0 | Orthanc API 1.12.3 with Python Plugin 4.2 |
1.13.2 to 1.16.1 | Orthanc API 1.12.1 with Python Plugin 4.1 |
1.13.0, 1.13.1 | Orthanc API 1.12.1 with Python Plugin 4.0 |
1.12.* | Orthanc API 1.12.1 |
1.11.* | Orthanc API 1.11.3 |
0.2.* | Provided Google sheet from Orthanc maintainer |
Running tests
The tests are run in a docker image launched with docker compose.
docker compose run test
This command starts 3 containers :
- A Python image with the PyOrthanc source code and launches pytest
- An instance of Orthanc (
orthanc1
) on which the PyOrthanc client is connected - A second Orthanc instance (
orthanc2
) which acts as a modality connected to orthanc1