forked from gacou54/pyorthanc
-
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.
Merge pull request #2 from ylemarechal/docs/improvements
Find resources with complex filters or filter on many resource levels
- Loading branch information
Showing
5 changed files
with
135 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
::: pyorthanc.filtering | ||
::: pyorthanc._filtering | ||
:docstring: | ||
:members: |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
::: pyorthanc.find | ||
::: pyorthanc._find | ||
:docstring: | ||
:members: |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
::: pyorthanc.resources.Resource | ||
::: pyorthanc._resources.Resource | ||
:docstring: | ||
:members: |
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 |
---|---|---|
@@ -1,140 +1,141 @@ | ||
# First steps | ||
|
||
## Requirements | ||
|
||
- [x] Python3.8 | ||
|
||
## Installation | ||
|
||
```bash | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install pyorthanc | ||
``` | ||
## Getting started | ||
### Import pyorthanc library | ||
```python | ||
import pyorthanc | ||
``` | ||
|
||
### Connect to Orthanc | ||
Here are some quick how to examples to use pyorthanc | ||
```python | ||
from pyorthanc import Orthanc | ||
|
||
orthanc = Orthanc(url='http://localhost:8042/', | ||
username='username', | ||
password='password') | ||
orthanc = Orthanc(url='http://localhost:8042/', username='orthanc', password='orthanc') | ||
``` | ||
|
||
### Upload DICOM files to Orthanc: | ||
|
||
```python | ||
from pyorthanc import Orthanc | ||
|
||
orthanc = Orthanc(url='http://localhost:8042/', | ||
username='username', | ||
password='password') | ||
|
||
with open('A_DICOM_INSTANCE_PATH.dcm', 'rb') as file: | ||
orthanc.post_instances(file.read()) | ||
orthanc.post_instances(file.read()) | ||
``` | ||
### Getting list of connected remote modalities: | ||
```python | ||
from pyorthanc import Orthanc | ||
modalities = orthanc.get_modalities() | ||
``` | ||
### Query (C-Find) and Retrieve (C-Move) from remote modality: | ||
|
||
orthanc = Orthanc(url='http://localhost:8042/', | ||
username='username', | ||
password='password') | ||
```python | ||
from pyorthanc import Modality | ||
|
||
orthanc.get_modalities() | ||
``` | ||
modality = Modality(orthanc, 'modality') | ||
|
||
# C-Echo | ||
assert modality.echo() # Test the connection | ||
|
||
### Find and download patients according to criteria: | ||
# Query (C-Find) on modality | ||
data = {'Level': 'Study', 'Query': {'PatientID': '*'}} | ||
query_response = modality.query(data=data) | ||
|
||
```python | ||
from pyorthanc import Orthanc, find_patients, retrieve_and_write_patients | ||
# Inspect the answer | ||
answer = modality.get_query_answers()[query_response['ID']] | ||
print(answer) | ||
|
||
orthanc = Orthanc(url='https://demo.orthanc-server.com', username='', password='') | ||
# Retrieve (C-Move) results of query on a target modality (AET) | ||
modality.move(query_response['ID'], {'TargetAet': 'target_modality'}) | ||
``` | ||
|
||
### Find and download patients according to criteria: | ||
```python | ||
from pyorthanc import find_patients, retrieve_and_write_patients | ||
|
||
patients = find_patients( | ||
client=orthanc, | ||
query={'PatientName': 'COMUNIX'} # Optional: filter with pyorthanc.Series object | ||
query={'PatientName': '*Gabriel'}, # Filter on PatientName | ||
labels=['a-label'] # Filter on patients with the 'a-label' | ||
) | ||
retrieve_and_write_patients(patients, '.') | ||
``` | ||
|
||
### Query (C-Find) and Retrieve (C-Move) from remote modality: | ||
|
||
Write the patients DICOM files locally | ||
```python | ||
from pyorthanc import RemoteModality, Orthanc | ||
|
||
orthanc = Orthanc('http://localhost:8042', 'username', 'password') | ||
retrieve_and_write_patients(patients, './patients_path') | ||
``` | ||
Or manipulates the Patient object | ||
```python | ||
patient = patients[0] | ||
|
||
modality = RemoteModality(orthanc, 'modality') | ||
patient.name | ||
patient.is_stable | ||
patient.last_update | ||
|
||
# Query (C-Find) on modality | ||
data = {'Level': 'Study', 'Query': {'PatientID': '*'}} | ||
query_response = modality.query(data=data) | ||
patient.labels | ||
patient.remove_label('a-label') | ||
patient.add_label('a-new-label') | ||
... | ||
``` | ||
|
||
answer = modality.get_query_answers()[query_response['ID']] | ||
print(answer) | ||
It is also possible to query the other resource levels | ||
```python | ||
from pyorthanc import find_studies, find_series, find_instances | ||
|
||
# Retrieve (C-Move) results of query on a target modality (AET) | ||
modality.move(query_response['ID'], {'TargetAet': 'target_modality'}) | ||
studies = find_studies(client=orthanc, query={...}, labels=[...]) | ||
series = find_series(client=orthanc, query={...}, labels=[...]) | ||
instances = find_instances(client=orthanc, query={...}, labels=[...]) | ||
``` | ||
|
||
#### Anonymize patient: | ||
#### Anonymize patient | ||
Resources (`Patient`, `Study`, `Series`, `Instance`) can be easily __anonymized__. | ||
```python | ||
from pyorthanc import Orthanc, Patient | ||
import pyorthanc | ||
|
||
orthanc = Orthanc('http://localhost:8042', 'username', 'password') | ||
orthanc_patient_id = client.get_patients()[0] | ||
patient = pyorthanc.Patient(orthanc_patient_id, client) | ||
``` | ||
Waiting for the anonymization process (this may raise a TimeOutError) | ||
```python | ||
new_patient = patient.anonymize() | ||
new_patient_with_given_patient_id = patient.anonymize( | ||
keep=['PatientName'], | ||
replace={'PatientID': 'TheNewPatientID'}, | ||
force=True # Needed when changing PatientID/StudyInstanceUID/SeriesInstanceUID/SOPInstanceUID | ||
) | ||
``` | ||
For long-running job (i.e. large patient) or to submit many anonymization jobs at the same time, use | ||
```python | ||
job = patient.anonymize_as_job() | ||
job.state # You can follow the job state | ||
|
||
job.wait_until_completion() # Or just wait on its completion | ||
new_patient = pyorthanc.Patient(job.content['ID'], client) | ||
``` | ||
|
||
patient_identifier = orthanc.get_patients()[0] | ||
#### Find resources with complex filters or filter on many resource levels | ||
The `pyorthanc.find()` function allow to find resources with filters on many levels, | ||
or with complex filter. Each filter function takes an object that correspond to the resource level | ||
and should return a boolean value. | ||
|
||
anonymized_patient = Patient(patient_identifier, orthanc).anonymize( | ||
keep=['PatientName'], # You can keep/remove/replace the DICOM tags you want | ||
replace={'PatientID': 'TheNewPatientID'}, | ||
remove=['ReferringPhysicianName'], | ||
force=True # Needed when changing PatientID/StudyInstanceUID/SeriesInstanceUID/SOPInstanceUID | ||
Note that when using the `find()` function, the state of the resources `Patient/Study/Series/Instance` | ||
are locked. | ||
```python | ||
from datetime import datetime | ||
from pyorthanc import find | ||
|
||
patients = find( | ||
orthanc, | ||
patient_filter=lambda patient: patient.last_update > datetime(year=2023, month=10, day=1), | ||
study_filter=lambda study: 'thorax' in study.description.lower(), | ||
series_filter=lambda series: series.modality == 'CT' | ||
) | ||
# Or directly with | ||
orthanc.post_patients_id_anonymize(patient_identifier) | ||
|
||
# result is: (you can retrieve DICOM file from ID) | ||
# {'ID': 'dd41f2f1-24838e1e-f01746fc-9715072f-189eb0a2', | ||
# 'Path': '/patients/dd41f2f1-24838e1e-f01746fc-9715072f-189eb0a2', | ||
# 'PatientID': 'dd41f2f1-24838e1e-f01746fc-9715072f-189eb0a2', | ||
# 'Type': 'Patient'} | ||
``` | ||
__Note__ that this function may take a while to run since each resource level is filtered. | ||
Using `find()` on large Orthanc server is not recommended. | ||
|
||
|
||
## Full basic examples | ||
|
||
Be sure that Orthanc is running. The default URL (if running locally) is `http://localhost:8042`. | ||
|
||
Here is a list of examples to helps you getting started with pyorthanc | ||
Here is a list of examples to helps you getting started with pyorthanc. | ||
|
||
### Access instance informations | ||
### Access instance information | ||
|
||
[Get instance informations](https://github.com/ylemarechal/pyorthanc-examples/tree/main/basic/access_informations) | ||
|
||
## Some useful commands | ||
|
||
### Docker commands | ||
Start Orthanc | ||
```bash | ||
docker compose up -d | ||
``` | ||
Stop Orthanc | ||
```bash | ||
docker compose stop | ||
``` | ||
Restart Orthanc | ||
```bash | ||
docker compose restart | ||
``` | ||
Delete Orthanc container | ||
```bash | ||
docker compose down | ||
``` |