Skip to content

Filtering

pyorthanc.find(orthanc, patient_filter=None, study_filter=None, series_filter=None, instance_filter=None)

Find desired patients/Study/Series/Instance in an Orthanc server

This function builds a series of tree structure. Each tree correspond to a patient. The layers in the tree correspond to:

Patient -> Studies -> Series -> Instances

Parameters:

Name Type Description Default
orthanc Union[Orthanc, AsyncOrthanc]

Orthanc object.

required
patient_filter Optional[Callable]

Patient filter (e.g. lambda patient: patient.id_ == '03HDQ99*')

None
study_filter Optional[Callable]

Study filter (e.g. lambda study: study.study_id == 'pros')

None
series_filter Optional[Callable]

Series filter (e.g. lambda series: series.modality == 'SR')

None
instance_filter Optional[Callable]

Instance filter (e.g. lambda instance: instance.SOPInstance == '...')

None

Returns:

Type Description
List[Patient]

List of patients that respect .

Source code in pyorthanc/_filtering.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def find(orthanc: Union[Orthanc, AsyncOrthanc],
         patient_filter: Optional[Callable] = None,
         study_filter: Optional[Callable] = None,
         series_filter: Optional[Callable] = None,
         instance_filter: Optional[Callable] = None) -> List[Patient]:
    """Find desired patients/Study/Series/Instance in an Orthanc server

    This function builds a series of tree structure.
    Each tree correspond to a patient. The layers in the
    tree correspond to:

    `Patient -> Studies -> Series -> Instances`

    Parameters
    ----------
    orthanc
        Orthanc object.
    patient_filter
        Patient filter (e.g. lambda patient: patient.id_ == '03HDQ99*')
    study_filter
        Study filter (e.g. lambda study: study.study_id == '*pros*')
    series_filter
        Series filter (e.g. lambda series: series.modality == 'SR')
    instance_filter
        Instance filter (e.g. lambda instance: instance.SOPInstance == '...')

    Returns
    -------
    List[Patient]
        List of patients that respect .
    """
    # In this function, client that return raw responses are not supported.
    orthanc = util.ensure_non_raw_response(orthanc)

    if isinstance(orthanc, AsyncOrthanc):
        return asyncio.run(_async_find(
            async_orthanc=orthanc,
            patient_filter=patient_filter,
            study_filter=study_filter,
            series_filter=series_filter,
            instance_filter=instance_filter
        ))

    patients = [Patient(i, orthanc, lock=True) for i in orthanc.get_patients()]
    if patient_filter is not None:
        patients = [i for i in patients if patient_filter(i)]

    for patient in patients:
        if study_filter is not None:
            patient._child_resources = [i for i in patient.studies if study_filter(i)]

        for study in patient.studies:
            if series_filter is not None:
                study._child_resources = [i for i in study.series if series_filter(i)]

            for series in study.series:
                if instance_filter is not None:
                    series._child_resources = [i for i in series.instances if instance_filter(i)]

    return trim_patients(patients)

pyorthanc.trim_patients(patients)

Trim Patient forest (list of patients)

Parameters:

Name Type Description Default
patients List[Patient]

Patient forest.

required

Returns:

Type Description
List[Patient]

Pruned patient forest.

Source code in pyorthanc/_filtering.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def trim_patients(patients: List[Patient]) -> List[Patient]:
    """Trim Patient forest (list of patients)

    Parameters
    ----------
    patients
        Patient forest.

    Returns
    -------
    List[Patient]
        Pruned patient forest.
    """
    for patient in patients:
        patient.remove_empty_studies()

    patients = [p for p in patients if p.studies != []]

    return patients