Skip to content

Commit

Permalink
refactor(dimsort):Refactor DICOMSorter class and add new method to ge…
Browse files Browse the repository at this point in the history
…t DICOM file paths
  • Loading branch information
jjjermiah committed Jan 21, 2024
1 parent 50916a4 commit 573978e
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/nbiatoolkit/dicomsort/dicomsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(

def generateFilePathFromDICOMAttributes(
self, dataset: pydicom.dataset.FileDataset
) -> str:
) -> str:
"""
Generate a file path for the DICOM file by formatting DICOM attributes.
"""
Expand All @@ -45,7 +45,7 @@ def generateFilePathFromDICOMAttributes(

def sortSingleDICOMFile(
self, filePath: str, option: str, overwrite: bool = False
) -> bool:
) -> bool:
assert option in ["copy", "move"], "Invalid option: symlink not implemented yet"

try:
Expand Down Expand Up @@ -77,38 +77,39 @@ def sortSingleDICOMFile(

return True

def sortDICOMFiles(
self, option: str = "copy", overwrite: bool = False
) -> bool:

def sortDICOMFiles(self, option: str = "copy", overwrite: bool = False) -> bool:

all_files = []
# Iterate over all files in the source directory
for root, dirs, files in os.walk(self.sourceDir):
for file in files:
all_files.append(os.path.join(root, file)) if file.endswith(".dcm") else None
dicom_file_paths = self._get_dicom_files()

results = [self.sortSingleDICOMFile(file, option, overwrite) for file in dicom_file_paths]

results = [self.sortSingleDICOMFile(file, option, overwrite) for file in all_files]

return all(results)


# Test case
if __name__ == "__main__":
def _get_dicom_files(self) -> list[str]:
dicom_file_paths = []
# Iterate over all files in the source directory
for root, dirs, files in os.walk(self.sourceDir):
for f in files:
dicom_file_paths.append(os.path.join(root, f)) if f.endswith(".dcm") else None

# Create an instance of DICOMSorter with the desired target pattern
sourceDir="/home/bioinf/bhklab/jermiah/projects/NBIA-toolkit/resources/rawdata/RADCURE-0281"
pattern = '%PatientName/%StudyDescription-%StudyDate/%SeriesNumber-%SeriesDescription-%SeriesInstanceUID/%InstanceNumber.dcm'
destinationDir="/home/bioinf/bhklab/jermiah/projects/NBIA-toolkit/resources/procdata"
sorter = DICOMSorter(
sourceDir = sourceDir,
destinationDir=destinationDir,
targetPattern=pattern,
truncateUID=True,
sanitizeFilename=True,
overwrite=True
)

sorter.sortDICOMFiles(option="move")
return dicom_file_paths

# Test case
# if __name__ == "__main__":

# sorter = DICOMSorter(
# sourceDir = sourceDir,
# destinationDir=destinationDir,
# targetPattern=pattern,
# truncateUID=True,
# sanitizeFilename=True,
# overwrite=True
# )

# sorter.sortDICOMFiles(option="move")



Expand Down

0 comments on commit 573978e

Please sign in to comment.