Extractor base
extractor_base
#
Base class for extracting modality-specific DICOM metadata.
This module defines an extensible interface for building metadata extractors that handle both simple DICOM tags and complex computed metadata fields, such as references embedded in nested sequences.
ComputedField
module-attribute
#
ComputedField = typing.Callable[
[pydicom.Dataset],
imgtools.dicom.dicom_metadata.extractor_base.ComputedValue,
]
Function that extracts a value from a DICOM dataset
ComputedValue
module-attribute
#
Single value or list of values extracted from a DICOM dataset.
ExtractedFields
module-attribute
#
ExtractedFields = dict[
str,
imgtools.dicom.dicom_metadata.extractor_base.ComputedValue,
]
Collection of computed values keyed by field name
ModalityMetadataExtractor
#
Bases: abc.ABC
Abstract base class for modality-specific DICOM metadata extractors.
This class supports both standard DICOM tag retrieval and more complex field computations based on the full DICOM dataset. Subclasses must specify the modality they support and define additional metadata fields either as DICOM tag names or as custom computation functions.
Attributes:
Name | Type | Description |
---|---|---|
base_tags |
typing.ClassVar[set[str]]
|
Standard DICOM tags to always extract, regardless of modality. |
modality_tags |
typing.ClassVar[set[str]]
|
Tags specific to the subclass's modality. These are merged with |
computed_fields |
typing.ClassVar[collections.abc.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]]
|
A mapping of metadata field names to callables that compute values from
the loaded |
Methods:
Name | Description |
---|---|
metadata_keys |
Returns a predictable, sorted list of all metadata field names produced by this extractor. |
extract |
Extracts metadata tags and computed fields from a DICOM dataset. Returns a dictionary mapping metadata field names to values. |
Notes
Subclasses MUST implement the following abstract methods/properties:
- modality() -> str
: A class method that returns the DICOM modality string handled
(e.g., "CT", "MR", "RTDOSE").
- modality_tags -> set[str]
: A class property that defines the set of DICOM attribute
names (tags) specific to the modality.
- computed_fields -> Mapping[str, Callable[[pydicom.Dataset], ComputedValue]]
: A class property
that defines a mapping of metadata field names to callables which compute their values.
Examples:
>>> class CTExtractor(ModalityMetadataExtractor):
>>> @classmethod
>>> def modality(cls) -> str:
>>> return "CT"
>>> @classproperty
>>> def modality_tags(cls) -> set[str]:
>>> return {"KVP", "ReconstructionAlgorithm"}
>>> @classproperty
>>> def computed_fields(cls) -> Mapping[str, ComputedField]:
>>> return {
>>> "CustomValue": lambda ds: str(float(ds.SliceThickness) * 2),
>>> "DoublePatientAge": lambda ds: str(ds.PatientAge * 2)
>>> }
>>> # Using the extractor
>>> metadata = CTExtractor.extract("file.dcm")
>>> # Returns: {'PatientID': '123', 'KVP': '120', 'CustomValue': '5.0', ...}
computed_fields
abstractmethod
#
computed_fields() -> collections.abc.Mapping[
str,
imgtools.dicom.dicom_metadata.extractor_base.ComputedField,
]
A mapping of metadata field names to callables that compute their values.
The callable should accept a pydicom Dataset and return a value.
Returns:
Type | Description |
---|---|
dict[str, typing.Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
Source code in src/imgtools/dicom/dicom_metadata/extractor_base.py
extract
classmethod
#
extract(
dicom: imgtools.dicom.DicomInput,
extra_tags: list[str] | None = None,
) -> (
imgtools.dicom.dicom_metadata.extractor_base.ExtractedFields
)
Extract metadata tags and computed fields from a DICOM dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
imgtools.dicom.DicomInput
|
A path, byte stream, or pydicom FileDataset. |
required |
|
list[str] | None
|
Additional DICOM tags to extract, by default None |
None
|
Returns:
Type | Description |
---|---|
dict[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]
|
A dictionary mapping metadata field names to values. Values may be strings, numbers, dictionaries, or lists of these types. Missing tags or errors during computation will result in an empty string. |
Notes
Be aware that using extra_tags may lead to unexpected results if the extra tags are not compatible with the modality or if they are not present in the DICOM file. The extractor will not validate the extra tags against the modality, so it's the user's responsibility to ensure that the extra tags are relevant and valid for the given DICOM file.
Source code in src/imgtools/dicom/dicom_metadata/extractor_base.py
metadata_keys
classmethod
#
Return a predictable, sorted list of metadata field names.
This includes both direct DICOM tag names and any computed metadata keys.
Returns:
Type | Description |
---|---|
list[str]
|
All metadata keys produced by this extractor. |
Source code in src/imgtools/dicom/dicom_metadata/extractor_base.py
modality
abstractmethod
classmethod
#
The DICOM modality handled by this extractor (e.g., "CT", "MR").
Returns:
Type | Description |
---|---|
str
|
Modality name. |
modality_tags
abstractmethod
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
Source code in src/imgtools/dicom/dicom_metadata/extractor_base.py
classproperty
#
Bases: property
A decorator that behaves like @property, but on the class rather than instance.
Useful for exposing computed class-level constants or derived metadata.