batchProc.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. Created on 18/11/2020
  3. @author: Leon Scharwächter
  4. AG Neuroimaging and Neuroengineering of Experimental Stroke
  5. Department of Neurology, University Hospital Cologne
  6. This script runs every needed script for all (pre-)processing and registration
  7. steps. The data needs to be ordered like after Bruker2NIfTI conversion:
  8. project_folder/days/groups/subjects/.
  9. For the script to work, it needs to be placed within the /bin folder of AIDAmri.
  10. Example:
  11. python batchProc.py -f /Volumes/Desktop/MRI/proc_data -g Treatment_C3a Treatment_PBS -d Baseline P7 P14 P28 P42 P56 -t fMRI DTI
  12. """
  13. import glob
  14. import os
  15. import fnmatch
  16. def findData(projectPath, days, groups):
  17. # This function screens all existing paths based on the specified days
  18. # and groups. Within these paths, this function collects all subject
  19. # folders, which are all folders that are not named 'Physio'.
  20. fullPath_list = []
  21. path_list = [os.path.join(projectPath, days[d], groups[g]) for d in range(len(days)) for g in range(len(groups))]
  22. for checkPath in path_list:
  23. list_subfolders = [f.name for f in os.scandir(checkPath) if f.is_dir() and f.name.lower() != 'physio']
  24. for subject in list_subfolders:
  25. fullPath_list.append(os.path.join(checkPath, subject))
  26. return(fullPath_list)
  27. def executeScripts(fullPath, dataTypeInput):
  28. # For every datatype (T2w, fMRI, DTI), go in all days/group/subjects folders
  29. # and execute the respective (pre-)processing/registration-scripts.
  30. # If a certain file does not exist, a note will be created in the errorList.
  31. # cwd should contain the path of the /bin folder (the user needs to navigate to the /bin folder before executing this script)
  32. errorList = [];
  33. message = '';
  34. cwd = str(os.getcwd())
  35. for dataFormat in dataTypeInput:
  36. for currentPath in fullPath:
  37. currentPath_wData = os.path.join(currentPath,dataFormat)
  38. # currentPath_wData = projectfolder/day/group/subject/dataFormat (e.g. T2w, fMRI, DTI)
  39. if os.path.isdir(currentPath_wData):
  40. if dataFormat == 'T2w':
  41. os.chdir(cwd + '/2.1_T2PreProcessing')
  42. currentFile = find("*1.nii.gz", currentPath_wData)
  43. if len(currentFile)>0:
  44. print('Run python 2.1_T2PreProcessing/preProcessing_T2.py -i '+currentFile[0])
  45. os.system('python preProcessing_T2.py -i '+currentFile[0])
  46. else:
  47. message = 'Could not find *1.nii.gz in '+currentPath_wData;
  48. print(message)
  49. errorList.append(message)
  50. currentFile = find("*BiasBet.nii.gz", currentPath_wData)
  51. if len(currentFile)>0:
  52. print('Run python 2.1_T2PreProcessing/registration_T2.py -i '+currentFile[0])
  53. os.system('python registration_T2.py -i '+currentFile[0])
  54. else:
  55. message = 'Could not find *BiasBet.nii.gz in '+currentPath_wData;
  56. print(message)
  57. errorList.append(message)
  58. print('Run python 3.1_T2Processing/getIncidenceSize_par.py -i '+currentPath_wData)
  59. os.chdir(cwd + '/3.1_T2Processing')
  60. os.system('python getIncidenceSize_par.py -i '+currentPath_wData)
  61. print('Run python 3.1_T2Processing/getIncidenceSize.py -i '+currentPath_wData)
  62. os.system('python getIncidenceSize.py -i '+currentPath_wData)
  63. os.chdir(cwd)
  64. elif dataFormat == 'fMRI':
  65. os.chdir(cwd + '/2.3_fMRIPreProcessing')
  66. currentFile = find("*1.nii.gz", currentPath_wData)
  67. if len(currentFile)>0:
  68. print('Run python 2.3_fMRIPreProcessing/preProcessing_fMRI.py -i '+currentFile[0])
  69. os.system('python preProcessing_fMRI.py -i '+currentFile[0])
  70. else:
  71. message = 'Could not find *1.nii.gz in '+currentPath_wData;
  72. print(message)
  73. errorList.append(message)
  74. currentFile = find("*SmoothBet.nii.gz", currentPath_wData)
  75. if len(currentFile)>0:
  76. print('Run python 2.3_fMRIPreProcessing/registration_rsfMRI.py -i '+currentFile[0])
  77. os.system('python registration_rsfMRI.py -i '+currentFile[0])
  78. else:
  79. message = 'Could not find *SmoothBet.nii.gz in '+currentPath_wData;
  80. print(message)
  81. errorList.append(message)
  82. currentFile = find("*1.nii.gz", currentPath_wData)
  83. if len(currentFile)>0:
  84. print('Run python 3.3_fMRIActivity/process_fMRI.py -i '+ currentFile[0])
  85. os.chdir(cwd + '/3.3_fMRIActivity')
  86. os.system('python process_fMRI.py -i '+ currentFile[0])
  87. os.chdir(cwd)
  88. elif dataFormat == 'DTI':
  89. os.chdir(cwd + '/2.2_DTIPreProcessing')
  90. currentFile = find("*1.nii.gz", currentPath_wData)
  91. if len(currentFile)>0:
  92. print('Run python 2.2_DTIPreProcessing/preProcessing_DTI.py -i '+currentFile[0])
  93. os.system('python preProcessing_DTI.py -i '+currentFile[0])
  94. else:
  95. message = 'Could not find *1.nii.gz in '+currentPath_wData;
  96. print(message)
  97. errorList.append(message)
  98. currentFile = find("*SmoothMicoBet.nii.gz", currentPath_wData)
  99. if len(currentFile)>0:
  100. print('Run python 2.2_DTIPreProcessing/registration_DTI.py -i '+currentFile[0])
  101. os.system('python registration_DTI.py -i '+currentFile[0])
  102. else:
  103. message = 'Could not find *SmoothMicoBet.nii.gz in '+currentPath_wData;
  104. print(message)
  105. errorList.append(message)
  106. currentFile = find("*1.nii.gz", currentPath_wData)
  107. if len(currentFile)>0:
  108. print('Run python 3.2_DTIConnectivity/dsi_main.py -i '+ currentFile[0])
  109. os.chdir(cwd + '/3.2_DTIConnectivity')
  110. os.system('python dsi_main.py -i '+ currentFile[0])
  111. os.chdir(cwd)
  112. else:
  113. message = 'The data folders'' names do not match T2w, fMRI or DTI';
  114. print(message);
  115. errorList.append(message)
  116. else:
  117. message = 'The folder '+dataFormat+' does not exist in '+currentPath
  118. print(message)
  119. errorList.append(message)
  120. print('')
  121. print('Errors:')
  122. print(errorList)
  123. def find(pattern, path):
  124. # This function finds all files with a specified fragment within
  125. # the given path
  126. result = []
  127. for root, dirs, files in os.walk(path):
  128. for name in files:
  129. if fnmatch.fnmatch(name, pattern):
  130. result.append(os.path.join(root, name))
  131. return result
  132. if __name__ == "__main__":
  133. import argparse
  134. parser = argparse.ArgumentParser(description='Batch processing of all data. This script runs every needed script for all registration and processing steps. The data needs to be ordered like after Bruker2NIfTI conversion: project_folder/days/groups/subjects/. For the script to work, it needs to be placed within the /bin folder of AIDAmri. Example: python batchProc.py -f /Volumes/Desktop/MRI/proc_data -g Treatment_C3a Treatment_PBS -d Baseline P7 P14 P28 P42 P56 -t T2w fMRI DTI')
  135. requiredNamed = parser.add_argument_group('required arguments')
  136. requiredNamed.add_argument('-f', '--folder', required=True,
  137. help='Path to the parent project folder of the dataset, e.g. proc_data')
  138. requiredNamed.add_argument('-g', '--groups', required=True, nargs='+', help='Group names as in the Bruker2NIfTI processed project folder')
  139. requiredNamed.add_argument('-d', '--days', required=True, nargs='+', help='Day names as in the Bruker2NIfTI processed project folder')
  140. requiredNamed.add_argument('-t', '--dataTypes', required=True, nargs='+', help='Data types to be processed e.g. T2w, DTI and/or fMRI. Multiple specifications are possible.')
  141. args = parser.parse_args()
  142. pathToData = args.folder
  143. groupNames = args.groups
  144. dayNames = args.days
  145. dataTypes = args.dataTypes
  146. print('Entered information:')
  147. print(groupNames)
  148. print(dayNames)
  149. print(dataTypes)
  150. listMr = findData(pathToData, dayNames, groupNames)
  151. executeScripts(listMr, dataTypes)