Skip to content

oxytcmri.interface.repositories.nifti_folders_mri_exam_repository

Classes:

Name Description
FileInfo
MRIDataFactory
DTIMapFactory
AtlasSegmentationFactory
DTISegmentationFactory

Factory for creating DTI segmentation MRI data.

DefaultMRIDataFactory
NiftiFoldersMRIExamRepository

FileInfo(filepath, filename, mri_exam_id) dataclass

Attributes:

Name Type Description
filepath Path
filename str
mri_exam_id MRIExamId

filepath instance-attribute

filename instance-attribute

mri_exam_id instance-attribute

MRIDataFactory

Bases: Protocol

Methods:

Name Description
create_mri_data

create_mri_data(file_info)

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
20
21
def create_mri_data(self, file_info: FileInfo) -> MRIData:
    pass

DTIMapFactory

Methods:

Name Description
create_mri_data

create_mri_data(file_info) staticmethod

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
25
26
27
28
29
30
31
32
33
@staticmethod
def create_mri_data(file_info: FileInfo) -> DTIMap:
    metric_name = file_info.filename.split("_")[0]
    metric = DTIMetric.from_acronym(metric_name)
    return DTIMap(
        mri_exam_id=file_info.mri_exam_id,
        voxel_data=NiftiVoxelData[float](file_info.filepath),
        dti_metric=metric,
    )

AtlasSegmentationFactory(atlas_repository)

Methods:

Name Description
create_mri_data

Attributes:

Name Type Description
atlas_repository
Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
37
38
def __init__(self, atlas_repository: AtlasRepository):
    self.atlas_repository = atlas_repository

atlas_repository = atlas_repository instance-attribute

create_mri_data(file_info)

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
40
41
42
43
44
45
46
47
def create_mri_data(self, file_info: FileInfo) -> AtlasSegmentation:
    atlas_id = int(file_info.filename[5:6])
    atlas = self.atlas_repository.get_by_id(atlas_id)
    return AtlasSegmentation(
        mri_exam_id=file_info.mri_exam_id,
        voxel_data=NiftiVoxelData[int](file_info.filepath),
        atlas=atlas,
    )

DTISegmentationFactory

Factory for creating DTI segmentation MRI data.

Methods:

Name Description
create_mri_data

create_mri_data(file_info)

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
55
56
57
58
59
60
def create_mri_data(self, file_info: FileInfo) -> MRIData:
    source_dti_map = self._create_source_dti_map(file_info)
    result = DTIAbnormalValues.from_dti_map(source_dti_map)
    # modify voxel_data in order to register only what we need: the filepath
    result.voxel_data = NiftiVoxelData(nifti_path=file_info.filepath)
    return result

DefaultMRIDataFactory

Methods:

Name Description
create_mri_data

create_mri_data(file_info) staticmethod

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
73
74
75
76
77
78
79
@staticmethod
def create_mri_data(file_info: FileInfo) -> MRIData:
    return MRIData(
        mri_exam_id=file_info.mri_exam_id,
        voxel_data=NiftiVoxelData[float](file_info.filepath),
        name=file_info.filename,
    )

NiftiFoldersMRIExamRepository(base_path, atlas_repository=None)

Bases: MRIExamRepository

Methods:

Name Description
exists

Check if an MRI exam exists in the repository.

scan_nifti_folders

Scan the base path for NIfTI folders and create MRIExam objects.

get_exam_for_subject

Retrieve the MRI exam for a specific subject.

find_by_id

Find an MRIExam by its ID.

list_all

List all MRI exams in the repository.

delete
save

Attributes:

Name Type Description
base_path
atlas_repository
mri_exam_list
Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
83
84
85
86
87
88
89
90
def __init__(self, base_path: str, atlas_repository: AtlasRepository = None):
    self.base_path = Path(base_path)
    self.atlas_repository = atlas_repository

    if not self.base_path.exists():
        raise FileNotFoundError(f"path '{base_path}' does not exist.")

    self.mri_exam_list = self.scan_nifti_folders() if atlas_repository else []

base_path = Path(base_path) instance-attribute

atlas_repository = atlas_repository instance-attribute

mri_exam_list = self.scan_nifti_folders() if atlas_repository else [] instance-attribute

exists(entity)

Check if an MRI exam exists in the repository.

Parameters:

Name Type Description Default
entity Entity

The entity to check for existence

required

Returns:

Type Description
bool

True if the entity exists, False otherwise

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def exists(self, entity: Entity) -> bool:
    """Check if an MRI exam exists in the repository.

    Parameters
    ----------
    entity : Entity
        The entity to check for existence

    Returns
    -------
    bool
        True if the entity exists, False otherwise
    """
    # check if the entity is an instance of MRIExam
    if not isinstance(entity, MRIExam):
        raise TypeError(f"Expected MRIExam, got {type(entity)}")

    # check if the entity's ID exists in the list of MRI exams
    return any(mri_exam.id == entity.id for mri_exam in self.mri_exam_list)

scan_nifti_folders()

Scan the base path for NIfTI folders and create MRIExam objects.

Returns:

Type Description
list[MRIExam]

A list of MRIExam objects representing the NIfTI folders found in the base path.

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def scan_nifti_folders(self) -> list[MRIExam]:
    """
    Scan the base path for NIfTI folders and create MRIExam objects.

    Returns
    -------
    list[MRIExam]
        A list of MRIExam objects representing the NIfTI folders found in the base path.
    """
    result = []

    for mri_exam_folder in self.base_path.iterdir():
        if mri_exam_folder.is_dir():
            mri_exam = self._create_mri_exam_from_folder(mri_exam_folder)
            if mri_exam is not None:
                result.append(mri_exam)

    return result

get_exam_for_subject(subject)

Retrieve the MRI exam for a specific subject.

Parameters:

Name Type Description Default
subject Subject

The subject to retrieve the MRI exam for

required

Returns:

Type Description
MRIExam

The MRI exam for the subject

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def get_exam_for_subject(self, subject: Subject) -> MRIExam:
    """Retrieve the MRI exam for a specific subject.

    Parameters
    ----------
    subject : Subject
        The subject to retrieve the MRI exam for

    Returns
    -------
    MRIExam
        The MRI exam for the subject
    """
    for mri_exam in self.mri_exam_list:
        if mri_exam.subject_id == subject.id:
            return mri_exam

    raise LookupError(f"No MRI exam found for subject {subject}")

find_by_id(entity_id)

Find an MRIExam by its ID.

Parameters:

Name Type Description Default
entity_id MRIExamId

The ID of the MRI exam

required

Returns:

Type Description
Entity

The entity if found, otherwise None

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def find_by_id(self, entity_id: MRIExamId) -> Optional[MRIExam]:
    """Find an MRIExam by its ID.

    Parameters
    ----------
    entity_id : MRIExamId
        The ID of the MRI exam

    Returns
    -------
    Entity
        The entity if found, otherwise None
    """
    for mri_exam in self.mri_exam_list:
        if mri_exam.id == entity_id:
            return mri_exam
    return None

list_all()

List all MRI exams in the repository.

Returns:

Type Description
List[MRIExam]

A list of all MRI exams in the repository

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
214
215
216
217
218
219
220
221
222
def list_all(self) -> List[MRIExam]:
    """List all MRI exams in the repository.

    Returns
    -------
    List[MRIExam]
        A list of all MRI exams in the repository
    """
    return self.mri_exam_list

delete(entity)

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
224
225
def delete(self, entity: Entity) -> None:
    raise NotImplementedError("Deleting MRI exams is not supported in this repository.")

save(mri_exam)

Source code in oxytcmri/interface/repositories/nifti_folders_mri_exam_repository.py
227
228
def save(self, mri_exam: MRIExam) -> None:
    raise NotImplementedError("Saving MRI exams is not supported in this repository.")