oxytcmri.infrastructure.gateways.sqlmodel_data_gateway
¶
A module using SQLModel ORM, with SQLite as the database engine.
Classes:
| Name | Description |
|---|---|
BaseDTO |
Base class for all Data Transfer Objects (DTO) with entity conversion methods. |
SubjectDTO |
DTO for Subject entity with database mapping. |
AtlasDTO |
DTO for Atlas entity with database mapping. |
MRIDataType |
Enum for mapping MRIData subtypes. |
MRIDataDTO |
DTO for MRIData entity with database mapping. |
MRIExamDTO |
DTO for MRIExam entity with database mapping. |
CenterDTO |
DTO for Center entity with database mapping. |
NormativeValuesDTO |
DTO for NormativeValues entity with database mapping. |
RegionOfInterestDTO |
DTO for RegionOfInterest entity with database mapping. |
BrainLesionsVolumeDTO |
DTO for BrainLesionsVolume entity with database mapping. |
SQLModelSQLiteDataGateway |
SQLModel implementation of DataBaseGateway for SQLite. |
Attributes:
| Name | Type | Description |
|---|---|---|
logger |
|
|
EntityType |
|
logger = logging.getLogger(__name__)
module-attribute
¶
EntityType = TypeVar('EntityType')
module-attribute
¶
BaseDTO
¶
Bases: SQLModel, Generic[EntityType], ABC
Base class for all Data Transfer Objects (DTO) with entity conversion methods.
Methods:
| Name | Description |
|---|---|
from_entity |
Create a DTO from a domain entity. |
to_entity |
Convert the DTO to a domain entity. |
from_entity(entity)
abstractmethod
classmethod
¶
Create a DTO from a domain entity.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
33 34 35 36 | |
to_entity()
abstractmethod
¶
Convert the DTO to a domain entity.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
38 39 40 | |
SubjectDTO
¶
DTO for Subject entity with database mapping.
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
|
subject_type |
str
|
|
center_id |
int
|
|
id = Field(primary_key=True)
class-attribute
instance-attribute
¶
subject_type
instance-attribute
¶
center_id
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
51 52 53 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
55 56 | |
AtlasDTO
¶
DTO for Atlas entity with database mapping.
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
|
name |
str
|
|
labels |
List[int]
|
|
id = Field(primary_key=True)
class-attribute
instance-attribute
¶
name
instance-attribute
¶
labels = Field(sa_type=JSON)
class-attribute
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
67 68 69 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
71 72 | |
MRIDataType
¶
Bases: str, Enum
Enum for mapping MRIData subtypes.
Attributes:
| Name | Type | Description |
|---|---|---|
GENERIC |
|
|
ATLAS_SEGMENTATION |
|
|
DTI_MAP |
|
|
DTI_ABNORMAL_VALUES |
|
MRIDataDTO
¶
DTO for MRIData entity with database mapping.
Warnings
This class assumes that the voxel data is of type NiftiVoxelData.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier (primary key) of the MRI data |
mri_exam_id |
str
|
Unique identifier of the MRI exam (foreign key) |
name |
str
|
Name of the MRI data |
nifti_data_path |
str
|
Path to the Nifti file |
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
id = Field(primary_key=True)
class-attribute
instance-attribute
¶
mri_exam_id = Field(foreign_key='mri_exams.id')
class-attribute
instance-attribute
¶
mri_exam = Relationship(back_populates='mri_data')
class-attribute
instance-attribute
¶
name
instance-attribute
¶
nifti_data_path
instance-attribute
¶
data_type = Field(default=(MRIDataType.GENERIC))
class-attribute
instance-attribute
¶
atlas_id = Field(default=None, foreign_key='atlases.id')
class-attribute
instance-attribute
¶
atlas = Relationship()
class-attribute
instance-attribute
¶
dti_metric = Field(default=None)
class-attribute
instance-attribute
¶
source_dti_path = Field(default=None)
class-attribute
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
MRIExamDTO
¶
DTO for MRIExam entity with database mapping.
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
|
subject_id |
str
|
|
mri_data |
list[MRIDataDTO]
|
|
id = Field(primary_key=True)
class-attribute
instance-attribute
¶
subject_id = Field(foreign_key='subjects.id')
class-attribute
instance-attribute
¶
mri_data = Relationship(back_populates='mri_exam')
class-attribute
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
184 185 186 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
188 189 190 191 192 | |
CenterDTO
¶
DTO for Center entity with database mapping.
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
|
name |
str
|
|
id = Field(primary_key=True)
class-attribute
instance-attribute
¶
name
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
202 203 204 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
206 207 | |
NormativeValuesDTO
¶
Bases: BaseDTO[NormativeValue]
DTO for NormativeValues entity with database mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
Unique identifier (primary key) of the normative value |
value |
float
|
Value of the normative value |
center_id |
int
|
Unique identifier of the center (foreign key) |
center |
CenterDTO
|
Center of the normative value |
dti_metric |
DTIMetric
|
DTI metric of the normative value |
atlas_id |
int
|
Unique identifier of the atlas (foreign key) |
atlas_label |
int
|
Label of the atlas |
atlas |
AtlasDTO
|
Atlas of the normative value |
statistic_strategy |
StatisticsStrategies
|
Statistic strategy of the normative value |
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
id = Field(default=None, primary_key=True)
class-attribute
instance-attribute
¶
value
instance-attribute
¶
center_id = Field(foreign_key='centers.id')
class-attribute
instance-attribute
¶
center = Relationship()
class-attribute
instance-attribute
¶
dti_metric
instance-attribute
¶
atlas_id = Field(foreign_key='atlases.id')
class-attribute
instance-attribute
¶
atlas_label
instance-attribute
¶
atlas = Relationship()
class-attribute
instance-attribute
¶
statistic_strategy
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
250 251 252 253 254 255 256 257 258 259 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
261 262 263 264 265 266 267 | |
RegionOfInterestDTO
¶
Bases: BaseDTO[RegionOfInterest]
DTO for RegionOfInterest entity with database mapping.
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
|
atlas_id |
int
|
|
atlas |
AtlasDTO
|
|
atlas_labels |
List[int]
|
|
name = Field(primary_key=True)
class-attribute
instance-attribute
¶
atlas_id = Field(foreign_key='atlases.id')
class-attribute
instance-attribute
¶
atlas = Relationship()
class-attribute
instance-attribute
¶
atlas_labels = Field(sa_type=JSON, default=[])
class-attribute
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
279 280 281 282 283 284 285 286 287 288 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
290 291 292 293 294 295 | |
BrainLesionsVolumeDTO
¶
Bases: BaseDTO[BrainLesionsVolume]
DTO for BrainLesionsVolume entity with database mapping.
Methods:
| Name | Description |
|---|---|
from_entity |
|
to_entity |
|
Attributes:
| Name | Type | Description |
|---|---|---|
WHOLE_BRAIN_ROI_NAME |
str
|
|
mri_exam_id |
str
|
|
mri_exam |
MRIExamDTO
|
|
dti_metric |
str
|
|
region_of_interest_name |
Optional[str]
|
|
region_of_interest |
RegionOfInterestDTO
|
|
abnormal_value_type |
str
|
|
value_ml |
float
|
|
WHOLE_BRAIN_ROI_NAME = 'Whole Brain'
class-attribute
¶
mri_exam_id = Field(foreign_key='mri_exams.id', primary_key=True)
class-attribute
instance-attribute
¶
mri_exam = Relationship()
class-attribute
instance-attribute
¶
dti_metric = Field(primary_key=True)
class-attribute
instance-attribute
¶
region_of_interest_name = Field(default=WHOLE_BRAIN_ROI_NAME, foreign_key='regions_of_interest.name', primary_key=True)
class-attribute
instance-attribute
¶
region_of_interest = Relationship()
class-attribute
instance-attribute
¶
abnormal_value_type = Field(primary_key=True)
class-attribute
instance-attribute
¶
value_ml
instance-attribute
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
311 312 313 314 315 316 317 318 319 | |
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
321 322 323 324 325 326 327 328 329 330 331 332 333 | |
SQLModelSQLiteDataGateway(database_path)
¶
Bases: DataBaseGateway[EntityType]
SQLModel implementation of DataBaseGateway for SQLite.
Initialize the gateway with the path to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database_path
|
str
|
Path to the SQLite file |
required |
Methods:
| Name | Description |
|---|---|
update |
|
find_by_id |
Retrieve an entity by its ID. |
find_by_filters |
Retrieve an entity by filters. |
find_all |
Retrieve all entities of the given type. |
save |
Save an entity (create or update). |
save_list |
Save a list of entities to the database in a single transaction. |
delete |
Delete an entity. |
Attributes:
| Name | Type | Description |
|---|---|---|
database_path |
|
|
engine |
|
|
entity_to_model_map |
Dict[Type, Type[BaseDTO]]
|
|
type_converters |
|
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
database_path = Path(database_path)
instance-attribute
¶
engine = create_engine(f'sqlite:///{database_path}', echo=False)
instance-attribute
¶
entity_to_model_map = {Center: CenterDTO, Subject: SubjectDTO, Atlas: AtlasDTO, MRIExam: MRIExamDTO, MRIData: MRIDataDTO, AtlasSegmentation: MRIDataDTO, DTIAbnormalValues: MRIDataDTO, DTIMap: MRIDataDTO, NormativeValue: NormativeValuesDTO, RegionOfInterest: RegionOfInterestDTO, BrainLesionsVolume: BrainLesionsVolumeDTO}
instance-attribute
¶
type_converters = {SubjectId: lambda x: str(x), MRIExamId: lambda x: str(x), DTIMetric: lambda x: str(x), StatisticStrategy: lambda x: x.name}
instance-attribute
¶
update(entity)
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
339 340 | |
find_by_id(entity_type, id_value)
¶
Retrieve an entity by its ID.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
find_by_filters(entity_type, filters)
¶
Retrieve an entity by filters.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
find_all(entity_type)
¶
Retrieve all entities of the given type.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
502 503 504 505 506 507 508 509 510 511 512 | |
save(entity)
¶
Save an entity (create or update).
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
514 515 516 | |
save_list(entities)
¶
Save a list of entities to the database in a single transaction.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
delete(entity)
¶
Delete an entity.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
535 536 537 538 539 540 541 542 543 544 545 | |