-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from anbhimi/dev
Python script to divide the DICOM's based on modality
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os, glob | ||
import sys | ||
import logging | ||
import pydicom as pyd | ||
import shutil | ||
|
||
def modality_split(cold_extraction_path, modality_split_path): | ||
# iterating through all the files in cold extraction | ||
for root, files in os.walk(cold_extraction_path): | ||
for file in files: | ||
if file.endswith('.dcm'): | ||
dcm_filename = root+'/'+file | ||
dcm_path = '/'.join(dcm_filename.split('/')[5:]) | ||
dcm_only_folder = '/'.join(dcm_path.split('/')[:-1]) | ||
dcm_file = pyd.dcmread(dcm_filename) | ||
dcm_modality = dcm_file.Modality | ||
|
||
print (dcm_modality, dcm_only_folder) | ||
isExist = os.path.exists(modality_split_path+str(dcm_modality)+'/'+str(dcm_only_folder)) | ||
if not isExist: | ||
os.makedirs(modality_split_path+str(dcm_modality)+'/'+str(dcm_only_folder)) | ||
shutil.copy2(src=cold_extraction_path+str(dcm_path), dst=modality_split_path+str(dcm_modality)+'/'+str(dcm_only_folder)+'/'+str(dcm_path.split('/')[-1])) | ||
|
||
if __name__ == "__main__": | ||
cold_extraction_path = sys.argv[1] | ||
modality_split_path = sys.argv[2] | ||
print ('Starting Modality Grouping') | ||
modality_split(cold_extraction_path, modality_split_path) |