DICOMSorter
imgtools.dicom.sort
Sorting DICOM Files by Specific Tags and Patterns.
This module provides functionality to organize DICOM files into structured directories based on customizable target patterns.
The target patterns allow metadata-driven file organization using placeholders for DICOM tags, enabling flexible and systematic storage.
Extended Summary
Target patterns define directory structures using placeholders, such as
%<DICOMKey>
and {DICOMKey}
, which are resolved to their corresponding
metadata values in the DICOM file.
This approach ensures that files are organized based on their metadata, while retaining their original basenames. Files with identical metadata fields are placed in separate directories to preserve unique identifiers.
Examples of target patterns:
- `%PatientID/%StudyID/{SeriesID}/`
- `path/to_destination/%PatientID/images/%Modality/%SeriesInstanceUID/`
Important: Only the directory structure is modified during the sorting process. The basename of each file remains unchanged.
Notes
The module ensures that:
- Target patterns are resolved accurately based on the metadata in DICOM files.
- Files are placed in directories that reflect their resolved metadata fields.
- Original basenames are preserved to prevent unintended overwrites!
Examples:
Source file:
Target directory pattern:
would result in the following structure for each file:
data/
└── dicoms/
└── {PatientID}/
└── Study-{StudyInstanceUID}/
└── Series-{SeriesInstanceUID}/
└── {Modality}/
└── 1-1.dcm
And so the resolved path for the file would be:
Here, the file is relocated into the resolved directory structure:
while the basename 1-1.dcm
remains unchanged.
imgtools.dicom.sort.DICOMSorter
DICOMSorter(source_directory: Path, target_pattern: str, pattern_parser: Pattern = DEFAULT_PATTERN_PARSER)
A specialized implementation of the SorterBase
for sorting DICOM files by metadata.
This class resolves paths for DICOM files based on specified target patterns, using metadata extracted from the files. The filename of each source file is preserved during this process.
Attributes:
Name | Type | Description |
---|---|---|
source_directory |
Path
|
The directory containing the files to be sorted. |
logger |
Logger
|
The instance logger bound with the source directory context. |
dicom_files |
list of Path
|
The list of DICOM files found in the |
format |
str
|
The parsed format string with placeholders for DICOM tags. |
keys |
Set[str]
|
DICOM tags extracted from the target pattern. |
invalid_keys |
Set[str]
|
DICOM tags from the pattern that are invalid. |
Methods:
Name | Description |
---|---|
execute |
Execute the file action on DICOM files. |
print_tree |
Display the pattern structure as a tree visualization. |
validate_keys |
Validate extracted keys. Subclasses should implement this method |
Source code in src/imgtools/dicom/sort/dicomsorter.py
invalid_keys
property
Get the set of invalid keys.
Essentially, this will check pydicom.dictionary_has_tag
for each key
in the pattern and return the set of keys that are invalid.
Returns:
Type | Description |
---|---|
Set[str]
|
The set of invalid keys. |
pattern_preview
property
execute
execute(action: FileAction = MOVE, overwrite: bool = False, dry_run: bool = False, num_workers: int = 1) -> None
Execute the file action on DICOM files.
Users are encouraged to use FileAction.HARDLINK for efficient storage and performance for large dataset, as well as protection against lost data.
Using hard links can save disk space and improve performance by creating multiple directory entries (links) for a single file instead of duplicating the file content. This is particularly useful when working with large datasets, such as DICOM files, where storage efficiency is crucial.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
FileAction
|
|
FileAction.MOVE
|
|
bool
|
|
False
|
|
bool
|
|
False
|
|
int
|
|
1
|
Source code in src/imgtools/dicom/sort/dicomsorter.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
print_tree
Display the pattern structure as a tree visualization.
Notes
This only prints the target pattern, parsed and formatted. Performing a dry-run execute will display more information.
Source code in src/imgtools/dicom/sort/sorter_base.py
validate_keys
Validate extracted keys. Subclasses should implement this method to perform specific validations based on their context.
Validate the DICOM keys in the target pattern.
If any invalid keys are found, it suggests similar valid keys and raises an error.