cobra_db.model

Module Contents

Classes

BaseObj

Defines the interface to load the object from dict and how to export it to dict.

Embedded

Base class for an embedded doc (that doesn't get its own _id)

Metadata

Stores information of when the db doc was created, modified, and the data model

Entity

Base class for all entities that are stored in the db with an _id, i.e. that

Source

Base class for a pointer to more information.

DicomEntity

Base class for all entities that contain dicom_tags.

EntitySource

Pointer to another Entity instance in the database.

FileSource

Pointer to a file in the filesystem.

Patient

Represents a unique person that has gone through at least one study.

RadiologicalStudy

Aggregation of multiple dicom instances by the combination of patient and date.

RadiologicalSeries

Base class for all entities that contain dicom_tags.

ImageMetadata

Represents one DICOM image i.e. a DICOM instance with PixelData.

Annotation

Abstract class for any annotation that is stored in cobra_db. Should be subclassed

Annotator

Base class for all entities that are stored in the db with an _id, i.e. that

class cobra_db.model.BaseObj[source]

Defines the interface to load the object from dict and how to export it to dict.

static expand(value)[source]
_to_dict()[source]
to_dict()[source]

expand the nested objects and remove all keys with None value

abstract classmethod from_dict(d: dict)[source]
class cobra_db.model.Embedded[source]

Bases: BaseObj

Base class for an embedded doc (that doesn’t get its own _id)

classmethod from_dict(d: dict)[source]
class cobra_db.model.Metadata[source]

Bases: Embedded

Stores information of when the db doc was created, modified, and the data model version. Should be used in any doc with _id that is stored in the database.

model_version: str
created: datetime.datetime
modified: datetime.datetime
project_name: str
classmethod create()[source]
class cobra_db.model.Entity[source]

Bases: BaseObj

Base class for all entities that are stored in the db with an _id, i.e. that require a Dao. Usually parent of a @dataclass class

_id: bson.ObjectId
_metadata: Metadata
__repr__()[source]

Return repr(self).

__post_init__()[source]

Automatically add db_metadata if missing

classmethod from_dict(obj_dict: dict)[source]
class cobra_db.model.Source[source]

Base class for a pointer to more information.

class cobra_db.model.DicomEntity[source]

Bases: Entity

Base class for all entities that contain dicom_tags.

dicom_tags: dict
static optional(ds: pydicom.dataset.Dataset, tag_name: str, parser: Callable = None, default=None)[source]

Get tag value from dataset, if missing return default value.

static choose_first(ds, *tag_names, parser, default)[source]

Choose first tag that does not return None,

abstract classmethod from_dataset(ds: pydicom.dataset.Dataset)[source]

Abstract method. How to create an instance from a pydicom Dataset

get_tag(keyword: str, default=None)[source]
get_acquisition_datetime()[source]
class cobra_db.model.EntitySource[source]

Bases: Embedded, Source

Pointer to another Entity instance in the database. Should only be used when there’s ambiguity about the referenced class/collection.

name: str
description: str
_entity_type: Union[Type[Entity], str]
_id: bson.ObjectId
__post_init__()[source]
to_dict()[source]

expand the nested objects and remove all keys with None value

class cobra_db.model.FileSource[source]

Bases: Embedded, Source

Pointer to a file in the filesystem. rel_path is relative to the place where drive_name is mounted. This is because drive_name can be mounted in different paths according to the machine and we don’t have another way of referencing a drive. filename is automatically stored for easy indexing and querying without having to manipulate the rel_path to extract the filename.

drive_name: str
rel_path: str
filename: str
get_local_filepath(mount_paths: dict) str[source]

Returns the filepath according to mount_paths

__post_init__()[source]
classmethod from_mount_paths(filepath: str, mount_paths: dict)[source]

create an instance from the filepath and the mount_paths.

Parameters:
  • filepath – _description_

  • mount_paths – _description_

class cobra_db.model.Patient[source]

Bases: Entity

Represents a unique person that has gone through at least one study. https://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.2.2.html https://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.2.3.html

anon_id: str
hidden: bool
birth_date: datetime.datetime
__repr__()[source]

Return repr(self).

classmethod from_dataset(ds: pydicom.dataset.Dataset)[source]

Create an instance of the entity from a DICOM dataset

__eq__(other: Patient) bool[source]

Return self==value.

class cobra_db.model.RadiologicalStudy[source]

Bases: DicomEntity

Aggregation of multiple dicom instances by the combination of patient and date.

study_uid: List[str]
date: datetime.datetime
series_count: int
patient_id: bson.ObjectId
sop_class: List[str]
modality: List[cobra_db.enums.Modality]
description: str
accession_number: List[str]
patient_age: int
patient_weight: float
patient_height: float
patient_pregnancy_status: cobra_db.enums.PregnancyStatus
manufacturer: List[str]
manufacturer_model_name: List[str]
detector_id: List[str]
detector_type: List[str]
device_serial_number: List[str]
software_versions: List[str]
date_of_last_detector_calibration: List[str]
breast_implant_present: List[str]
__repr__()[source]

Return repr(self).

classmethod from_dataset(ds: pydicom.dataset.Dataset)[source]

Create an instance of the entity from a DICOM dataset

classmethod from_dict(d: dict)[source]

Convert a mongodb dict into a RadiologicalStudy object

__eq__(other: RadiologicalStudy) bool[source]

Return self==value.

class cobra_db.model.RadiologicalSeries[source]

Bases: DicomEntity

Base class for all entities that contain dicom_tags.

study_id: bson.ObjectId
date: datetime.datetime
series_uid: str
description: str
image_count: int
image_shape: Tuple[int, int]
protocol_name: str
__repr__()[source]

Return repr(self).

classmethod from_dataset(ds: pydicom.dataset.Dataset)[source]

Create an instance of the entity from a DICOM dataset

classmethod from_images_metadata(images_metadata: List[ImageMetadata])[source]
__eq__(other: RadiologicalSeries) bool[source]

Return self==value.

class cobra_db.model.ImageMetadata[source]

Bases: DicomEntity

Represents one DICOM image i.e. a DICOM instance with PixelData. Only the dicom headers are stored. While the pixel data is not stored.

dicom_tags: dict
file_source: FileSource
study_id: bson.ObjectId
series_id: bson.ObjectId
aka_file_sources: List[FileSource]
classmethod from_dataset(ds: pydicom.dataset.Dataset, mount_paths: dict)[source]

Create one instance from a pydicom Dataset.

Parameters:
  • ds – pydicom Dataset

  • mount_paths – dict that maps the drive_names to the mount path of the drives that are in use.

to_dataset() pydicom.dataset.Dataset[source]

Creates a pydicom Dataset with only the metadata in dicom_tags. Useful to use pydicom interface when required.

get_checksum(mount_paths: dict) str[source]

Obtain the md5 hexdigest hash of the original file. This helps finding duplicated images. Warning: md5 is not cryptographycally secure.

Parameters:

mount_paths – a dictionary mapping the drive_name to the mount path in the local computer.

Returns:

string hexdigest with the hash of the file.

get_local_filepath(mount_paths: dict) str[source]

Returns the filepath of the file_source according to mount_paths.

Parameters:

mount_paths – a dictionary mapping the drive_name to the mount path in the local computer.

Returns:

string hexdigest with the hash of the file.

get_pixel_data(mount_paths) numpy.ndarray[source]

Load the file and extract the pixel_array from the pydicom dataset.

Parameters:

mount_paths – a dictionary mapping the drive_name to the mount path in the local computer.

Returns:

the content of pydicom.Dataset.pixel_array

get_file_size(mount_paths) int[source]

Get the size in bytes of the file

__repr__()[source]

Return repr(self).

class cobra_db.model.Annotation[source]

Bases: Entity

Abstract class for any annotation that is stored in cobra_db. Should be subclassed

annotation_type: List[Literal[text, semantic_segmentation, binary, categorical]]
annotator_email: str
date: datetime.datetime
possible_labels: List[str]
file_format: str
aggregation_level: Literal[patient, study, series, image]
referenced_entity_uid: str
patient_anon_id: str
study_date: datetime.datetime
file_source: FileSource
project_name: str
class cobra_db.model.Annotator[source]

Bases: Entity

Base class for all entities that are stored in the db with an _id, i.e. that require a Dao. Usually parent of a @dataclass class

email: str
specialization: str
first_name: str
last_name: str