getIncidenceMap.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import sys
  3. import nibabel as nii
  4. import glob
  5. import numpy as np
  6. import progressbar
  7. import matplotlib.pyplot as plt
  8. def heatMap(incidenceMap, araVol, outputLocation):
  9. maxV = int(np.max(incidenceMap))
  10. fig, axes = plt.subplots(nrows=3, ncols=4)
  11. t = 1
  12. for ax in axes.flat:
  13. im = ax.imshow(np.transpose(np.round(incidenceMap[:, :, t * 16])), cmap='gnuplot', vmin=0, vmax=maxV)
  14. ax.imshow(np.transpose(araVol[:, :, t * 16]), alpha=0.55, cmap='gray')
  15. ax.axis('off')
  16. t = t + 1
  17. fig.subplots_adjust(right=0.8)
  18. cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
  19. bounds = np.linspace(0, maxV, maxV + 1)
  20. cbar = fig.colorbar(im, cax=cbar_ax, format='%1i', ticks=bounds)
  21. cbar.ax.tick_params(labelsize=14)
  22. # Save the heatmap instead of showing
  23. output_file = os.path.join(outputLocation, 'heatMap.png')
  24. plt.savefig(output_file)
  25. plt.close()
  26. def incidenceMap2(path_listInc, araTemplate, inputFile, outputLocation):
  27. araDataTemplate = nii.load(araTemplate)
  28. realAraImg = np.asanyarray(araDataTemplate.dataobj)
  29. overlaidIncidences = np.zeros_like(realAraImg)
  30. bar = progressbar.ProgressBar()
  31. for fileIndex in bar(range(len(path_listInc))):
  32. dataMRI = nii.load(path_listInc[fileIndex])
  33. volumeMRI = np.asanyarray(dataMRI.dataobj)
  34. # Adjusting the volumeMRI data
  35. volumeMRI[volumeMRI <= 0] = 0
  36. volumeMRI[volumeMRI > 0] = 1
  37. overlaidIncidences += volumeMRI
  38. overlayNII = nii.Nifti1Image(overlaidIncidences, araDataTemplate.affine)
  39. output_file = os.path.join(outputLocation, 'incMap.nii.gz')
  40. nii.save(overlayNII, output_file)
  41. heatMap(incidenceMap=overlaidIncidences, araVol=realAraImg, outputLocation=outputLocation)
  42. def findIncData(path):
  43. regMR_list = []
  44. for filename in glob.iglob(os.path.join(path,"*","*",'anat', '*IncidenceData_mask.nii.gz')):
  45. regMR_list.append(filename)
  46. return regMR_list
  47. if __name__ == "__main__":
  48. import argparse
  49. parser = argparse.ArgumentParser(description='Calculate an Incidence Map')
  50. parser.add_argument('-i', '--inputFile', help='Directory: Brain extracted input data, e.g proc_data folder', required=True)
  51. parser.add_argument('-o', '--outputLocation', help='Directory: Output location for the heat map', required=True)
  52. parser.add_argument('-a', '--allenBrainTemplate', help='File: Annotations of Allen Brain', nargs='?', type=str,
  53. default=os.path.abspath(os.path.join(os.getcwd(), os.pardir, os.pardir, 'lib', 'average_template_50.nii.gz')))
  54. args = parser.parse_args()
  55. inputFile = args.inputFile
  56. outputLocation = args.outputLocation
  57. allenBrainTemplate = args.allenBrainTemplate
  58. if not os.path.exists(inputFile):
  59. sys.exit("Error: '%s' is not an existing directory." % (inputFile,))
  60. if not os.path.exists(outputLocation):
  61. sys.exit("Error: '%s' is not an existing directory." % (outputLocation,))
  62. if not os.path.exists(allenBrainTemplate):
  63. sys.exit("Error: '%s' is not an existing file." % (allenBrainTemplate,))
  64. regInc_list = findIncData(inputFile)
  65. if len(regInc_list) < 1:
  66. sys.exit("Error: No masked strokes found in the provided directory.")
  67. print("'%i' folders are part of the incidence map." % (len(regInc_list),))
  68. incidenceMap2(regInc_list, allenBrainTemplate, inputFile, outputLocation)
  69. sys.exit(0)