Parse dicoms
parse_dicoms
#
Functions:
Name | Description |
---|---|
parse_dicom_dir |
Parse all DICOM files in a directory and return the metadata. |
SeriesMetaMap
module-attribute
#
SeriesMetaMap: typing.TypeAlias = dict[
imgtools.dicom.crawl.parse_dicoms.SeriesUID,
dict[
imgtools.dicom.crawl.parse_dicoms.SubSeriesID, dict
],
]
Datatype represents: {Series
: {SubSeries
: dict
}}
SeriesUID
module-attribute
#
Represent the SeriesInstanceUID
of a DICOM file.
SopSeriesMap
module-attribute
#
SopSeriesMap: typing.TypeAlias = dict[
imgtools.dicom.crawl.parse_dicoms.SopUID,
imgtools.dicom.crawl.parse_dicoms.SeriesUID,
]
Datatype represents: {SOPInstanceUID
: SeriesInstanceUID
}
SopUID
module-attribute
#
Represent the SOPInstanceUID
of a DICOM file.
SubSeriesID
module-attribute
#
Represent the AcquisitionNumber
of a DICOM file.
ParseDicomDirResult
#
Bases: typing.NamedTuple('ParseDicomDirResult', [('crawl_db', list[dict[str, str]]), ('index', pandas.DataFrame), ('crawl_db_raw', imgtools.dicom.crawl.parse_dicoms.SeriesMetaMap), ('crawl_db_path', pathlib.Path), ('index_csv_path', pathlib.Path), ('crawl_cache_path', pathlib.Path), ('sop_map_path', pathlib.Path)])
A NamedTuple containing the result of parsing a DICOM directory.
Attributes:
Name | Type | Description |
---|---|---|
crawl_db |
list[dict[str, str]]
|
A list of dictionaries containing the simplified crawl database. |
index |
pandas.DataFrame
|
A DataFrame from which the relationships between DICOMs in the directory can be constructed. |
crawl_db_raw |
imgtools.dicom.crawl.parse_dicoms.SeriesMetaMap
|
A mapping of each series to the corresponding subseries in the directory. |
crawl_db_path |
pathlib.Path
|
Path to the simplified crawl database JSON file. |
index_csv_path |
pathlib.path
|
Path to the index file. |
crawl_cache_path |
pathlib.Path
|
Path to the crawl cache. |
sop_map_path |
pathlib.Path
|
Path to the SOP map, which maps SOP UIDs to Series UIDs. |
construct_barebones_dict
#
construct_barebones_dict(
series_meta_raw: imgtools.dicom.crawl.parse_dicoms.SeriesMetaMap,
) -> list[dict[str, str]]
Construct a simplified dictionary from the series metadata.
Source code in src/imgtools/dicom/crawl/parse_dicoms.py
extract_metadata_wrapper
#
Wrapper for extract_metadata to avoid lambda in parallel processing.
parse_all_dicoms
#
parse_all_dicoms(
dicom_files: list[pathlib.Path],
top: pathlib.Path,
n_jobs: int = -1,
) -> tuple[
imgtools.dicom.crawl.parse_dicoms.SeriesMetaMap,
imgtools.dicom.crawl.parse_dicoms.SopSeriesMap,
]
Parse a list of DICOM files in parallel and return the metadata.
Given a list of dicom files, this function will parse the metadata of each file
in parallel and return two dictionaries:
1. series_meta_raw
: A dictionary mapping SeriesInstanceUID
to
1 or more SubSeriesID
and the metadata of each SubSeries.
where SubSeriesID
is the AcquisitionNumber
of the DICOM file.
2. sop_map
: A dictionary mapping SOPInstanceUID
to SeriesInstanceUID
.
{
<SeriesInstanceUID>: {
<SubSeriesID>: {
'Modality': <Modality>,
...
'folder': <folder>,
'instances': {
<SOPInstanceUID>: <filename>,
...
},
}
}
}
{
<SOPInstanceUID>: <SeriesInstanceUID>,
}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
list[str]
|
List of paths to DICOM files to process |
required |
|
pathlib.Path
|
Top directory path (used for relative path calculation) |
required |
|
int
|
Number of parallel jobs to run |
-1
|
Source code in src/imgtools/dicom/crawl/parse_dicoms.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 |
|
parse_dicom_dir
#
parse_dicom_dir(
dicom_dir: str | pathlib.Path,
output_dir: str | pathlib.Path,
dataset_name: str | None = None,
extension: str = "dcm",
n_jobs: int = -1,
force: bool = True,
) -> imgtools.dicom.crawl.parse_dicoms.ParseDicomDirResult
Parse all DICOM files in a directory and return the metadata.
This function searches for DICOM files within the specified directory
(including subdirectories), extracts metadata from each file, and
organizes the metadata into structured dictionaries. The results,
including a simplified crawl database, the raw series metadata,
a SOP map, and their corresponding JSON file paths, are returned
as a ParseDicomDirResult
namedtuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | pathlib.Path
|
The directory to search for DICOM files. |
required |
|
str | pathlib.Path
|
The directory to save crawl outputs. See Notes for details. |
required |
|
str | None
|
The name of the dataset. If None, the name of the top directory
will be used. This is used to create a subdirectory in the
|
None
|
|
str
|
The file extension to look for when searching for DICOM files. |
"dcm"
|
|
int
|
The number of parallel jobs to use for parsing DICOM files. If -1, all available cores will be used. |
-1
|
|
bool
|
If True, overwrite existing crawl database and SOP map JSON files. If False, load existing files if they exist. |
True
|
Returns:
Type | Description |
---|---|
imgtools.dicom.crawl.parse_dicoms.ParseDicomDirResult
|
A namedtuple containing the following fields: - crawl_db_path (pathlib.Path): Path to the simplified crawl database JSON file. - crawl_db (list[dict[str, str]]): A list of dictionaries containing the simplified crawl database. |
Notes
This function will create a directory structure for the crawl database
output_dir
├── <dataset_name>
│ ├── crawl_db.json
│ ├── crawl-cache.json
│ ├── sop_map.json
│ └── index.csv
└── ...
crawl_db.json
file contains the simplified crawl database, while
Source code in src/imgtools/dicom/crawl/parse_dicoms.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
|
remove_duplicate_entries
#
remove_duplicate_entries(
slim_db: list[dict[str, str]],
ignore_keys: typing.Optional[list[str]] = None,
) -> list[dict[str, str]]
Removes duplicate entries from a barebones dict, ignoring the keys in ignore_keys
.
Returns:
Name | Type | Description |
---|---|---|
list[dict[str, str]]
|
|
|
Note |
I tried to make this as efficient as possible, sorry if it slows down everything.
|
|
Source code in src/imgtools/dicom/crawl/parse_dicoms.py
resolve_reference_series
#
resolve_reference_series(
meta: dict,
sop_map: imgtools.dicom.crawl.parse_dicoms.SopSeriesMap,
series_meta_raw: imgtools.dicom.crawl.parse_dicoms.SeriesMetaMap,
frame_mapping: dict[str, set[str]],
) -> None
Process reference mapping for a single metadata entry.
The goal is to get the ReferencedSeriesUID
for as many series as possible.
Whereas some series directly reference the SeriesInstanceUID
of another series,
others reference one or more SOPInstanceUID
of instances in another series.
This method tries to resolve the latter case by mapping the SOPInstanceUID
to the
SeriesInstanceUID
of the series it belongs to.
Additionally, the PT
modalities might reference a CT
.
Side effects:
- as we iterate over the metadata dictionaries, we add the
ReferencedSeriesUID
field to the metadata dictionaries in the crawldb.
Notes
This mutates the metadata dictionaries in the crawldb in place.
i.e the ReferencedSeriesUID
field is added to metadata dicts in the crawldb.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict
|
Metadata entry to process |
required |
|
dict
|
Dictionary mapping SOP UIDs to Series UIDs |
required |
|
dict
|
Series metadata dictionary |
required |
|
dict
|
Frame of reference mapping |
required |
Source code in src/imgtools/dicom/crawl/parse_dicoms.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
series2modality
#
series2modality(
seriesuid: imgtools.dicom.crawl.parse_dicoms.SeriesUID,
series_meta_raw: imgtools.dicom.crawl.parse_dicoms.SeriesMetaMap,
) -> str