Skip to content

repositories

oxytcmri.domain.ports.repositories

This module defines repositories for the application, storing and retrieving entities.

Classes:

Name Description
EntityNotFoundException

Exception raised when an entity is not found in the repository.

Repository

Abstract base class for repositories.

SubjectRepository
MRIExamRepository

Abstract base class for MRI repository.

AtlasRepository

Abstract base class for Atlas repository.

CenterRepository

Abstract base class for Center repository.

RepositoriesRegistry

Abstract base class for a registry responsible for managing the repositories in the application.

Attributes:

Name Type Description
Entity
EntityIdentifier

Entity = TypeVar('Entity') module-attribute

EntityIdentifier = TypeVar('EntityIdentifier') module-attribute

EntityNotFoundException(entity_id, repository)

Bases: Exception

Exception raised when an entity is not found in the repository.

Source code in oxytcmri/domain/ports/repositories.py
22
23
def __init__(self, entity_id: EntityIdentifier, repository: Repository):
    super().__init__(f"Entity id {entity_id} not found in repository {repository}")

Repository

Bases: ABC, Generic[Entity, EntityIdentifier]

Abstract base class for repositories. Defines the interface for all repositories in the application.

Methods:

Name Description
find_by_id

Retrieve an entity by its ID.

get_by_id

Retrieve an entity by its ID.

list_all

List all entities in the repository.

save

Save an entity to the repository.

save_list

Save a list of entities to the repository.

delete

Delete an entity from the repository.

find_by_id(entity_id) abstractmethod

Retrieve an entity by its ID.

Parameters:

Name Type Description Default
entity_id EntityIdentifier

The ID of the entity

required

Returns:

Type Description
Entity

The entity object if found, otherwise None

Source code in oxytcmri/domain/ports/repositories.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@abstractmethod
def find_by_id(self, entity_id: EntityIdentifier) -> Optional[Entity]:
    """
    Retrieve an entity by its ID.

    Parameters
    ----------
    entity_id : EntityIdentifier
        The ID of the entity

    Returns
    -------
    Entity
        The entity object if found, otherwise None
    """

get_by_id(entity_id)

Retrieve an entity by its ID.

Parameters:

Name Type Description Default
entity_id EntityIdentifier

The ID of the entity

required

Returns:

Type Description
Entity

The entity object

Raises:

Type Description
EntityNotFoundException

If the entity with the given ID does not exist

Source code in oxytcmri/domain/ports/repositories.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def get_by_id(self, entity_id: EntityIdentifier) -> Entity:
    """
    Retrieve an entity by its ID.

    Parameters
    ----------
    entity_id : EntityIdentifier
        The ID of the entity

    Returns
    -------
    Entity
        The entity object

    Raises
    -------
    EntityNotFoundException
        If the entity with the given ID does not exist
    """
    entity = self.find_by_id(entity_id)
    if entity is None:
        raise EntityNotFoundException(entity_id, self)
    return entity

list_all() abstractmethod

List all entities in the repository.

Returns:

Type Description
List[Entity]

List of all entities

Source code in oxytcmri/domain/ports/repositories.py
72
73
74
75
76
77
78
79
80
81
@abstractmethod
def list_all(self) -> List[Entity]:
    """
    List all entities in the repository.

    Returns
    -------
    List[Entity]
        List of all entities
    """

save(entity) abstractmethod

Save an entity to the repository.

Parameters:

Name Type Description Default
entity Entity

The entity to save

required
Source code in oxytcmri/domain/ports/repositories.py
83
84
85
86
87
88
89
90
91
92
@abstractmethod
def save(self, entity: Entity) -> None:
    """
    Save an entity to the repository.

    Parameters
    ----------
    entity : Entity
        The entity to save
    """

save_list(entity_list)

Save a list of entities to the repository.

Parameters:

Name Type Description Default
entity_list List[Entity]

The list of entities to save

required
Source code in oxytcmri/domain/ports/repositories.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def save_list(self, entity_list: List[Entity]) -> None:
    """
    Save a list of entities to the repository.

    Parameters
    ----------
    entity_list : List[Entity]
        The list of entities to save
    """
    for entity in entity_list:
        self.save(entity)

delete(entity) abstractmethod

Delete an entity from the repository.

Parameters:

Name Type Description Default
entity Entity

The entity to delete

required
Source code in oxytcmri/domain/ports/repositories.py
106
107
108
109
110
111
112
113
114
115
@abstractmethod
def delete(self, entity: Entity) -> None:
    """
    Delete an entity from the repository.

    Parameters
    ----------
    entity : Entity
        The entity to delete
    """

SubjectRepository

Bases: Repository[Subject, SubjectId]

Methods:

Name Description
find_subjects_by_center

Find subjects for a given center, optionally filtered by type.

find_subjects_by_center(center, subject_type=None) abstractmethod

Find subjects for a given center, optionally filtered by type.

Parameters:

Name Type Description Default
center Center

The center to find subjects for

required
subject_type Optional[SubjectType]

If provided, only return subjects of this type

None

Returns:

Type Description
List[Subject]

List of matching subjects

Source code in oxytcmri/domain/ports/repositories.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@abstractmethod
def find_subjects_by_center(
        self, center: Center, subject_type: Optional[SubjectType] = None
) -> List[Subject]:
    """
    Find subjects for a given center, optionally filtered by type.

    Parameters
    ----------
    center : Center
        The center to find subjects for
    subject_type : Optional[SubjectType], default=None
        If provided, only return subjects of this type

    Returns
    -------
    List[Subject]
        List of matching subjects
    """

MRIExamRepository

Bases: Repository[MRIExam, MRIExamId]

Abstract base class for MRI repository. Defines the interface for retrieving MRI exam data.

Methods:

Name Description
get_exam_for_subject

Retrieve the MRI exam for a specific subject.

get_exam_for_subject(subject_id) abstractmethod

Retrieve the MRI exam for a specific subject.

Parameters:

Name Type Description Default
subject_id SubjectId

The identifier of the subject

required

Returns:

Type Description
MRIExam

The MRI exam for the subject

Source code in oxytcmri/domain/ports/repositories.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@abstractmethod
def get_exam_for_subject(self, subject_id: SubjectId) -> MRIExam:
    """
    Retrieve the MRI exam for a specific subject.

    Parameters
    ----------
    subject_id : SubjectId
        The identifier of the subject

    Returns
    -------
    MRIExam
        The MRI exam for the subject
    """

AtlasRepository

Bases: Repository[Atlas, int], ABC

Abstract base class for Atlas repository. Defines the interface for retrieving atlas data.

CenterRepository

Bases: Repository[Center, int], ABC

Abstract base class for Center repository.

RepositoriesRegistry

Bases: ABC

Abstract base class for a registry responsible for managing the repositories in the application.

Methods:

Name Description
get_repository

Return the repository corresponding to the given entity type.

list_all_repositories

Return a list of all repositories in the registry.

get_repository(entity_type) abstractmethod

Return the repository corresponding to the given entity type.

Source code in oxytcmri/domain/ports/repositories.py
178
179
180
@abstractmethod
def get_repository(self, entity_type: Type[Entity]) -> Repository[Entity, EntityIdentifier]:
    """Return the repository corresponding to the given entity type."""

list_all_repositories() abstractmethod

Return a list of all repositories in the registry.

Source code in oxytcmri/domain/ports/repositories.py
182
183
184
@abstractmethod
def list_all_repositories(self) -> List[Repository[Entity, EntityIdentifier]]:
    """Return a list of all repositories in the registry."""