DICOM Modality-Specific Metadata Extraction#
extract_metadata
#
extract_metadata(
dicom: imgtools.dicom.DicomInput,
modality: str | None = None,
extra_tags: list[str] | None = None,
) -> dict[
str,
imgtools.dicom.dicom_metadata.extractor_base.ComputedValue,
]
Extract metadata from a DICOM file based on its modality.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
imgtools.dicom.DicomInput
|
DICOM input source (path, bytes, or pydicom Dataset) |
required |
|
str | None
|
Explicitly specify the modality to use, by default None. If None, the modality is extracted from the DICOM file. |
None
|
|
list[str] | None
|
Additional DICOM tags to extract, by default None. If None, no extra tags are extracted. |
None
|
Returns:
Type | Description |
---|---|
dict[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]
|
Dictionary of metadata fields with their values extracted from the DICOM. |
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/__init__.py
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
FallbackMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Generic fallback extractor for unsupported or uncommon DICOM modalities.
This extractor uses only the base tags defined in the superclass and defines no modality-specific tags or computed fields. It allows graceful handling of modalities not yet explicitly supported.
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
Returns an empty set since no modality-specific tags are defined.
|
|
Returns:
Type | Description |
---|---|
set[str]
|
Empty set. |
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
Returns an empty mapping since no computed fields are defined.
|
|
Returns:
Type | Description |
---|---|
typing.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]
|
Empty mapping. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
CTMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Metadata extractor for CT modality DICOM datasets.
This subclass defines modality-specific tags and computed fields relevant to CT (Computed Tomography) imaging. It extends the base metadata extractor with CT-specific acquisition and reconstruction parameters.
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
CT-specific DICOM tag names.
|
|
Returns:
Type | Description |
---|---|
set[str]
|
CT acquisition and reconstruction-related DICOM tags. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
CT-specific computed fields.
|
|
Returns:
Type | Description |
---|---|
typing.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]
|
Mapping of field names to functions that compute values from DICOM datasets. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
MRMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
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/extractors.py
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
MR-specific computed fields.
|
|
Returns:
Type | Description |
---|---|
typing.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]
|
Mapping of field names to functions that compute values from DICOM datasets. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
PTMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
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/extractors.py
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
PET-specific computed fields.
|
|
Returns:
Type | Description |
---|---|
typing.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]
|
Mapping of field names to functions that compute values from DICOM datasets. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
SEGMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
See Also
https://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.8.20.2.html
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
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/extractors.py
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
SEG-specific computed fields.
|
|
Each computed field is a function that takes a pydicom.Dataset as input
|
|
and returns a computed value. The functions are defined to extract
|
|
relevant information from the DICOM dataset.
|
|
Returns:
Type | Description |
---|---|
typing.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]
|
Mapping of field names to functions that compute values from DICOM datasets. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
RTSTRUCTMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Metadata extractor for RTSTRUCT modality DICOM datasets.
This class uses computed fields to extract ROI metadata and reference UIDs.
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
RTSTRUCT-specific direct tags (generally minimal).
|
|
Returns:
Type | Description |
---|---|
set[str]
|
A set of directly accessible RTSTRUCT tag names. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |
RTSTRUCT-specific computed fields.
|
|
Returns:
Type | Description |
---|---|
typing.Mapping[str, imgtools.dicom.dicom_metadata.extractor_base.ComputedField]
|
Field names mapped to functions that extract computed values. |
Source code in src/imgtools/dicom/dicom_metadata/extractors.py
RTDOSEMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Metadata extractor for RTDOSE modality DICOM datasets.
Extracts direct and computed reference UIDs from dose DICOM files.
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
computed_fields
#
computed_fields() -> typing.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, 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/extractors.py
RTPLANMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Metadata extractor for RTPLAN modality DICOM datasets.
Extracts basic DICOM tags and reference to an RTSTRUCT UID.
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
computed_fields
#
computed_fields() -> typing.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, 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/extractors.py
SRMetadataExtractor
#
Bases: imgtools.dicom.dicom_metadata.extractor_base.ModalityMetadataExtractor
Metadata extractor for SR (Structured Report) modality DICOM datasets.
Extracts referenced SeriesInstanceUIDs and SOPInstanceUIDs from structured reports.
Methods:
Name | Description |
---|---|
modality_tags |
A set of DICOM tags specific to the modality handled by this extractor. |
computed_fields |
A mapping of metadata field names to callables that compute their values. |
modality_tags
#
A set of DICOM tags specific to the modality handled by this extractor.
Returns:
Type | Description |
---|---|
set[str]
|
Set of DICOM tag names. |
computed_fields
#
computed_fields() -> typing.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, Callable[[pydicom.Dataset], imgtools.dicom.dicom_metadata.extractor_base.ComputedValue]]
|
Mapping of field names to computation functions. |