/Modules/Python/StochasticTractographyServerMP/autoTracto2regions.py

https://github.com/LinjieChen/Slicer3
Python | 76 lines | 48 code | 22 blank | 6 comment | 12 complexity | 5ca82468da547d280635400565afa876 MD5 | raw file
  1. import os, sys, glob, time, shutil
  2. if __name__ == '__main__':
  3. rootA = os.path.abspath(os.path.dirname(sys.argv[0]))
  4. print 'Input : ', rootA
  5. cont = os.walk('.')
  6. d = cont.next()
  7. dirs = d[1]
  8. print 'Folders to visit : ', dirs
  9. for i in range(len(dirs)):
  10. print 'Go into folder : ', dirs[i]
  11. os.chdir(dirs[i])
  12. contRM = os.walk('.')
  13. dRM = contRM.next()
  14. dirsRM = dRM[1]
  15. if len(dirsRM)>0:
  16. for s in range(len(dirsRM)):
  17. os.system('rm -rf ' + dirsRM[s])
  18. os.chdir('..')
  19. print 'Cleaning done'
  20. for i in range(len(dirs)):
  21. print 'Go into folder : ', dirs[i]
  22. os.chdir(dirs[i])
  23. fullDir = os.getcwd()
  24. pars = glob.glob('*.in')
  25. dwis = glob.glob('*.dwi')
  26. rois = glob.glob('*.roi')
  27. wmas = glob.glob('*.wm')
  28. #try:
  29. # ret = os.system('python ' + rootA + '/TractoCell0/ModifyHeader.py ' + pars[0] + ' stepSize 0.9' )
  30. # ret = os.system('python ' + rootA + '/TractoCell0/ModifyHeader.py ' + pars[0] + ' totalTracts 40' )
  31. # ret = os.system('python ' + rootA + '/TractoCell0/ModifyHeader.py ' + pars[0] + ' maxLength 200')
  32. #except:
  33. # print "Exception: continue anyway!"
  34. if len(pars)==1 and len(dwis)==1:
  35. print 'ROIS : ', rois
  36. if len(rois)!=2:
  37. print 'No consistent number of ROIs'
  38. else:
  39. try:
  40. for s in range(len(rois)/2):
  41. tmpD = rois[s].split('.')[0] + '_' + rois[s+1].split('.')[0]
  42. isDir = os.access(tmpD, os.F_OK)
  43. if not isDir:
  44. os.mkdir(tmpD)
  45. tmpF = './' + tmpD + '/'
  46. os.chdir(tmpF)
  47. print sys.executable + ' ' + rootA + '/' + 'StochasticTractoGraphyClusterFiles.py ' + fullDir + '/' + pars[0] + ' ' + fullDir + '/' + dwis[0] + ' ' + fullDir + '/' + rois[s] + ' ' + fullDir + '/' + rois[s+1]
  48. ret = os.system(sys.executable + ' ' + rootA + '/' + 'StochasticTractoGraphyClusterFiles.py ' + fullDir + '/' + pars[0] + ' ' + fullDir + '/' + dwis[0] + ' ' + fullDir + '/' + rois[s] + ' ' + fullDir + '/' + rois[s+1])
  49. os.chdir('..')
  50. except:
  51. print "Exception: continue anyway!"
  52. os.chdir('..')
  53. time.sleep(4) # breathe
  54. print 'Completed'