sqlmodel_data_gateway
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. |
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
31 32 33 34 |
|
to_entity()
abstractmethod
¶
Convert the DTO to a domain entity.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
36 37 38 |
|
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
49 50 51 |
|
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
53 54 |
|
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
65 66 67 |
|
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
69 70 |
|
MRIDataType
¶
Bases: str
, Enum
Enum for mapping MRIData subtypes.
Attributes:
Name | Type | Description |
---|---|---|
GENERIC |
|
|
ATLAS_SEGMENTATION |
|
|
DTI_MAP |
|
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
¶
from_entity(entity)
classmethod
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
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
164 165 166 |
|
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
168 169 170 171 172 |
|
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
182 183 184 |
|
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
186 187 |
|
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
230 231 232 233 234 235 236 237 238 239 |
|
to_entity()
¶
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
241 242 243 244 245 246 247 |
|
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[SQLModel]]
|
|
type_converters |
|
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
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, DTIMap: MRIDataDTO, NormativeValue: NormativeValuesDTO}
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
253 254 |
|
find_by_id(entity_type, id_value)
¶
Retrieve an entity by its ID.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
find_by_filters(entity_type, filters)
¶
Retrieve an entity by filters.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
find_all(entity_type)
¶
Retrieve all entities of the given type.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
409 410 411 412 413 414 415 416 417 418 419 |
|
save(entity)
¶
Save an entity (create or update).
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
421 422 423 |
|
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
425 426 427 428 429 430 431 432 433 434 |
|
delete(entity)
¶
Delete an entity.
Source code in oxytcmri/infrastructure/gateways/sqlmodel_data_gateway.py
436 437 438 439 440 441 442 443 444 445 446 |
|