From 93713f3b7920afa82439683681184f20bbe8d3af Mon Sep 17 00:00:00 2001 From: gacou54 Date: Wed, 30 Aug 2023 15:55:24 -0400 Subject: [PATCH] Refactoring for a cleaner api interface --- pyorthanc/__init__.py | 13 +++++++----- pyorthanc/{filtering.py => _filtering.py} | 8 ++++---- pyorthanc/{find.py => _find.py} | 10 +++++----- pyorthanc/{remote.py => _modality.py} | 20 +++++++++---------- .../{resources => _resources}/__init__.py | 0 .../{resources => _resources}/instance.py | 0 .../{resources => _resources}/patient.py | 0 .../{resources => _resources}/resource.py | 0 pyorthanc/{resources => _resources}/series.py | 0 pyorthanc/{resources => _resources}/study.py | 0 pyorthanc/retrieve.py | 8 ++++---- tests/conftest.py | 4 ++-- tests/test_filtering.py | 4 ++-- tests/test_find.py | 2 +- ...st_remote_modality.py => test_modality.py} | 0 15 files changed, 36 insertions(+), 33 deletions(-) rename pyorthanc/{filtering.py => _filtering.py} (97%) rename pyorthanc/{find.py => _find.py} (97%) rename pyorthanc/{remote.py => _modality.py} (87%) rename pyorthanc/{resources => _resources}/__init__.py (100%) rename pyorthanc/{resources => _resources}/instance.py (100%) rename pyorthanc/{resources => _resources}/patient.py (100%) rename pyorthanc/{resources => _resources}/resource.py (100%) rename pyorthanc/{resources => _resources}/series.py (100%) rename pyorthanc/{resources => _resources}/study.py (100%) rename tests/{test_remote_modality.py => test_modality.py} (100%) diff --git a/pyorthanc/__init__.py b/pyorthanc/__init__.py index 2bdcfb5..840c7a5 100644 --- a/pyorthanc/__init__.py +++ b/pyorthanc/__init__.py @@ -1,16 +1,17 @@ +from . import errors, util +from ._filtering import build_patient_forest, find, trim_patients +from ._find import find_instances, find_patients, find_series, find_studies, query_orthanc +from ._modality import Modality, RemoteModality +from ._resources import Instance, Patient, Series, Study from .async_client import AsyncOrthanc from .client import Orthanc -from .filtering import build_patient_forest, find, trim_patients -from .find import find_instances, find_patients, find_series, find_studies, query_orthanc -from .remote import RemoteModality -from .resources import Instance, Patient, Series, Study from .retrieve import retrieve_and_write_instance, retrieve_and_write_patient, retrieve_and_write_patients, \ retrieve_and_write_series, retrieve_and_write_study - __all__ = [ 'AsyncOrthanc', 'Orthanc', + 'Modality', 'RemoteModality', 'Patient', 'Study', @@ -29,4 +30,6 @@ 'retrieve_and_write_study', 'retrieve_and_write_series', 'retrieve_and_write_instance', + 'util', + 'errors', ] diff --git a/pyorthanc/filtering.py b/pyorthanc/_filtering.py similarity index 97% rename from pyorthanc/filtering.py rename to pyorthanc/_filtering.py index 99f7cd7..d5b2289 100644 --- a/pyorthanc/filtering.py +++ b/pyorthanc/_filtering.py @@ -4,10 +4,10 @@ from .async_client import AsyncOrthanc from .client import Orthanc -from .resources.instance import Instance -from .resources.patient import Patient -from .resources.series import Series -from .resources.study import Study +from ._resources.instance import Instance +from ._resources.patient import Patient +from ._resources.series import Series +from ._resources.study import Study from .util import async_to_sync diff --git a/pyorthanc/find.py b/pyorthanc/_find.py similarity index 97% rename from pyorthanc/find.py rename to pyorthanc/_find.py index 0ee0d01..024d3f2 100644 --- a/pyorthanc/find.py +++ b/pyorthanc/_find.py @@ -1,11 +1,11 @@ from typing import Dict, List, Union from .client import Orthanc -from .resources.instance import Instance -from .resources.patient import Patient -from .resources.resource import Resource -from .resources.series import Series -from .resources.study import Study +from ._resources.instance import Instance +from ._resources.patient import Patient +from ._resources.resource import Resource +from ._resources.series import Series +from ._resources.study import Study DEFAULT_RESOURCES_LIMIT = 1_000 diff --git a/pyorthanc/remote.py b/pyorthanc/_modality.py similarity index 87% rename from pyorthanc/remote.py rename to pyorthanc/_modality.py index 0eb687c..1d84b97 100644 --- a/pyorthanc/remote.py +++ b/pyorthanc/_modality.py @@ -5,9 +5,8 @@ from .client import Orthanc -class RemoteModality: - """Wrapper around Orthanc API when dealing with a (remote) modality. - """ +class Modality: + """Wrapper around Orthanc API when dealing with a modality.""" def __init__(self, client: Orthanc, modality: str) -> None: """Constructor @@ -23,7 +22,7 @@ def __init__(self, client: Orthanc, modality: str) -> None: self.modality = modality def echo(self) -> bool: - """C-Echo to remote modality + """C-Echo to modality Returns ------- @@ -60,12 +59,12 @@ def query(self, data: Dict) -> Dict: ... } ... } - >>> remote_modality = RemoteModality( + >>> modality = Modality( ... client=Orthanc('http://localhost:8042'), ... modality='sample' ... ) - >>> remote_modality.query(data) + >>> modality.query(data) """ return dict(self.client.post_modalities_id_query(self.modality, json=data)) @@ -88,13 +87,13 @@ def move(self, query_identifier: str, cmove_data: Dict) -> Dict: Examples -------- - >>> remote_modality = RemoteModality(Orthanc('http://localhost:8042'), 'modality') - >>> query_id = remote_modality.query( + >>> modality = Modality(Orthanc('http://localhost:8042'), 'modality') + >>> query_id = modality.query( ... data={'Level': 'Series', ... 'Query': {'PatientID': '', ... 'Modality':'SR'}}) - >>> remote_modality.move( + >>> modality.move( ... query_identifier=query_id['ID'], ... cmove_data={'TargetAet': 'TARGETAET'} ... ) @@ -103,7 +102,7 @@ def move(self, query_identifier: str, cmove_data: Dict) -> Dict: return dict(self.client.post_queries_id_retrieve(query_identifier, json=cmove_data)) def store(self, instance_or_series_id: str) -> Dict: - """Store series or instance to remote modality. + """Store series or instance to modality. Parameters ---------- @@ -130,3 +129,4 @@ def get_query_answers(self) -> Dict: return answers +RemoteModality = Modality diff --git a/pyorthanc/resources/__init__.py b/pyorthanc/_resources/__init__.py similarity index 100% rename from pyorthanc/resources/__init__.py rename to pyorthanc/_resources/__init__.py diff --git a/pyorthanc/resources/instance.py b/pyorthanc/_resources/instance.py similarity index 100% rename from pyorthanc/resources/instance.py rename to pyorthanc/_resources/instance.py diff --git a/pyorthanc/resources/patient.py b/pyorthanc/_resources/patient.py similarity index 100% rename from pyorthanc/resources/patient.py rename to pyorthanc/_resources/patient.py diff --git a/pyorthanc/resources/resource.py b/pyorthanc/_resources/resource.py similarity index 100% rename from pyorthanc/resources/resource.py rename to pyorthanc/_resources/resource.py diff --git a/pyorthanc/resources/series.py b/pyorthanc/_resources/series.py similarity index 100% rename from pyorthanc/resources/series.py rename to pyorthanc/_resources/series.py diff --git a/pyorthanc/resources/study.py b/pyorthanc/_resources/study.py similarity index 100% rename from pyorthanc/resources/study.py rename to pyorthanc/_resources/study.py diff --git a/pyorthanc/retrieve.py b/pyorthanc/retrieve.py index 7864d21..0ce6ab7 100644 --- a/pyorthanc/retrieve.py +++ b/pyorthanc/retrieve.py @@ -1,10 +1,10 @@ import os from typing import List -from .resources.instance import Instance -from .resources.patient import Patient -from .resources.series import Series -from .resources.study import Study +from ._resources.instance import Instance +from ._resources.patient import Patient +from ._resources.series import Series +from ._resources.study import Study def retrieve_and_write_patients(patients: List[Patient], path: str) -> None: diff --git a/tests/conftest.py b/tests/conftest.py index 9fef1af..9d678bd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,6 @@ import pytest -from pyorthanc import AsyncOrthanc, Instance, Orthanc, Patient, RemoteModality, Series, Study +from pyorthanc import AsyncOrthanc, Instance, Modality, Orthanc, Patient, Series, Study from .data import a_patient, a_series, a_study, an_instance from .setup_server import ORTHANC_1, ORTHANC_2, add_modality, clear_data, setup_data @@ -68,7 +68,7 @@ def modality(client, second_client): add_modality(ORTHANC_1, ORTHANC_2.AeT, 'orthanc2', 4242) add_modality(ORTHANC_2, ORTHANC_1.AeT, 'orthanc1', 4242) - return RemoteModality(client, ORTHANC_2.AeT) + return Modality(client, ORTHANC_2.AeT) @pytest.fixture diff --git a/tests/test_filtering.py b/tests/test_filtering.py index 31e873c..a657306 100644 --- a/tests/test_filtering.py +++ b/tests/test_filtering.py @@ -1,6 +1,6 @@ import pytest -from pyorthanc import Instance, Patient, Series, Study, filtering +from pyorthanc import Instance, Patient, Series, Study, find from pyorthanc.util import make_datetime_from_dicom_date from tests.data import a_patient, a_series, a_study, an_instance @@ -29,7 +29,7 @@ ]) def test_find(client_fixture, patient_filter, study_filter, series_filter, instance_filter, expected_nbr_of_patient, expected_nbr_of_series, request): - patients = filtering.find( + patients = find( orthanc=request.getfixturevalue(client_fixture), patient_filter=patient_filter, study_filter=study_filter, diff --git a/tests/test_find.py b/tests/test_find.py index 1d8d2db..52e433b 100644 --- a/tests/test_find.py +++ b/tests/test_find.py @@ -1,7 +1,7 @@ import pytest from pyorthanc import find_instances, find_patients, find_series, find_studies, query_orthanc -from pyorthanc.find import DEFAULT_RESOURCES_LIMIT +from pyorthanc._find import DEFAULT_RESOURCES_LIMIT from .conftest import LABEL_INSTANCE, LABEL_PATIENT, LABEL_SERIES, LABEL_STUDY from .data import a_patient, a_series, a_study, an_instance diff --git a/tests/test_remote_modality.py b/tests/test_modality.py similarity index 100% rename from tests/test_remote_modality.py rename to tests/test_modality.py