nbiatoolkit.dicomsort

Submodules

Package Contents

Classes

DICOMSorter

Functions

generateFilePathFromDICOMAttributes(→ str)

Generate a file path for the DICOM file by formatting DICOM attributes.

parseDICOMKeysFromFormat(→ Tuple[str, List[str]])

Parse the target pattern to create a format string with named placeholders

sanitizeFileName(→ str)

Sanitize the file name by replacing potentially dangerous characters.

_truncateUID(→ str)

Truncate the UID to the last n characters (includes periods & underscores).

class nbiatoolkit.dicomsort.DICOMSorter(sourceDir: str, destinationDir: str, targetPattern: str = '%PatientName/%SeriesNumber-%SeriesInstanceUID/%InstanceNumber.dcm', truncateUID: bool = True, sanitizeFilename: bool = True)[source]
sortDICOMFiles(shutil_option: str = 'copy', overwrite: bool = False, n_parallel: int = 1, progressbar=True) bool[source]
nbiatoolkit.dicomsort.generateFilePathFromDICOMAttributes(dataset: pydicom.Dataset, targetPattern: str, truncateUID: bool, sanitizeFilename: bool) str[source]

Generate a file path for the DICOM file by formatting DICOM attributes.

nbiatoolkit.dicomsort.parseDICOMKeysFromFormat(targetPattern: str) Tuple[str, List[str]][source]

Parse the target pattern to create a format string with named placeholders and extract a list of DICOM keys.

The target pattern is a string with placeholders matching ‘%<DICOMKey>’. This method converts placeholders into a format string with named placeholders and creates a list of DICOM keys contained within the placeholders.

Returns:

A tuple containing the format string and a list of DICOM keys.

Return type:

Tuple[str, List[str]]

Example usage:

fmt, keys = parseDICOMKeysFromFormat(targetPattern) print(fmt) # “%(PatientID)s-%(StudyDate)s” print(keys) # [‘PatientID’, ‘StudyDate’]

nbiatoolkit.dicomsort.sanitizeFileName(fileName: str) str[source]

Sanitize the file name by replacing potentially dangerous characters.

nbiatoolkit.dicomsort._truncateUID(uid: str, lastDigits: int = 5) str[source]

Truncate the UID to the last n characters (includes periods & underscores). If the UID is shorter than n characters, the entire UID is returned.