FeatureCheck.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. import numpy as np
  2. import os
  3. import pandas as pd
  4. import glob
  5. import matplotlib.backends.backend_pdf
  6. import nibabel as nib
  7. import nibabel as nii
  8. import matplotlib.pyplot as plt
  9. import pv_conv2Nifti as pr
  10. import alive_progress as ap
  11. import pv_parser as par
  12. from QC import *
  13. #%% Feature calculation of the pipeline. Core Unit of the Pipeline
  14. def CheckingRawFeatures(Path):
  15. #Path=r"C:\Users\Erfan\Downloads\Compressed\proc_data\P5"
  16. Abook = []
  17. Names =[]
  18. for file in glob.glob(os.path.join(Path, '*addreses*.csv')) :
  19. if "anat" in file :
  20. t2w_path= file
  21. t2w_addreses= pd.read_csv(t2w_path)
  22. Abook.append(t2w_addreses)
  23. Names.append("anat")
  24. elif "diff" in file:
  25. dti_path= file
  26. dti_addreses= pd.read_csv(dti_path)
  27. Abook.append(dti_addreses)
  28. Names.append("diff")
  29. elif "func" in file :
  30. fmri_path= file
  31. fmri_addreses= pd.read_csv(fmri_path)
  32. Abook.append(fmri_addreses)
  33. Names.append("func")
  34. ErorrList = []
  35. saving_path = Path
  36. C = np.array([not Check.empty for Check in Abook])
  37. Names = np.array(Names)[C].tolist()
  38. Names.append('ErrorData')
  39. Abook2 = [a.values.tolist() for a in Abook]
  40. #% Calculate all the SNR for all the data that was found
  41. # in the last step and saving it into a vector
  42. # Load Bruker data from each address
  43. kk =0
  44. for ii,N in enumerate(Names):
  45. if N != 'ErrorData':
  46. if kk > 0:
  47. print(str(kk) + ' 1 faulty file(s) were found. Note: these files will be listed in CanNotProcessTheseFiles.csv \n')
  48. print(N+' processing... \n')
  49. text_files0 = Abook2[ii]
  50. text_files = [i[0] for i in text_files0]
  51. dd = 1
  52. snrCh_vec =[]
  53. tsnr_vec = []
  54. snr_normal_vec = []
  55. SpatRes_vec = []
  56. MI_vec_all = []
  57. LMV_all = []
  58. GMV_all = []
  59. Max_mov_between_all = []
  60. text_files_new = []
  61. img_names_new = []
  62. keys = []
  63. GMetric_vec = []
  64. kk = 0
  65. i=1
  66. with ap.alive_bar(len(text_files),spinner='wait') as bar:
  67. for tf in text_files:
  68. tf = str(tf)
  69. tf = os.path.normpath(tf)
  70. path_split = tf.split(os.sep)
  71. procno = str(1)
  72. expno =path_split[-1]
  73. study = path_split[-2]
  74. raw_folder = os.sep.join(path_split[:-2])
  75. proc_folder = os.path.join(raw_folder,'proc_data') #Here still adjustment is needed
  76. pv = pr.Bruker2Nifti(study, expno, procno, raw_folder, proc_folder, ftype='NIFTI_GZ')
  77. CP_v = os.path.join(tf ,'pdata','1','visu_pars') # Check Parameter: Visu_pars
  78. CP_a = os.path.join(tf , 'acqp') # Check Parameter: acqp
  79. if os.path.isfile(CP_v) and os.path.isfile(CP_a):
  80. try:
  81. pv.read_2dseq(map_raw=False, pv6=False)
  82. input_file = nii.squeeze_image(pv.nim)
  83. except ValueError:
  84. ErorrList.append(tf+"_Value Error")
  85. print(tf)
  86. print("Value Error: catched")
  87. continue
  88. except SystemError:
  89. ErorrList.append(tf+"_System Error")
  90. print(tf)
  91. print("System Error: catched")
  92. continue
  93. except KeyError:
  94. ErorrList.append(tf+"_KeyError")
  95. print(tf)
  96. print("KeyError: catched")
  97. continue
  98. except FileNotFoundError:
  99. ErorrList.append(tf+"_FileNotFoundError")
  100. print(tf)
  101. print("System Error: catched")
  102. continue
  103. else:
  104. ErorrList.append(tf+"No_Visu_pars_file")
  105. print("No Visu_pars file found")
  106. kk = kk+1
  107. continue
  108. # determine sequence name
  109. NameTemp = par.read_param_file(CP_a)
  110. MN = NameTemp[1]["ACQ_method"].upper() #Here we check what the name of the sequence is
  111. MN2 = NameTemp[1]["ACQ_protocol_name"].upper()
  112. KEY = MN + MN2
  113. keys.append(KEY)
  114. # =============================================================================
  115. # Size = ((input_file.get_fdata()).nbytes/(1024*1024))
  116. # if Size < 3:
  117. # ErorrList.append(tf)
  118. # continue
  119. # =============================================================================
  120. ########### Slice extraction
  121. selected_img = Image_Selection(input_file)
  122. qc_path = os.path.join(saving_path,"manual_slice_inspection")
  123. if not os.path.isdir(qc_path):
  124. os.mkdir(qc_path)
  125. img_name = str.split(tf,os.sep)[-2]
  126. full_img_name = str(N)+"_" + img_name+"_"+ str(dd)+".png".replace(".nii","").replace(".gz","")
  127. img_names_new.append(full_img_name)
  128. #plt.figure()
  129. plt.axis('off')
  130. plt.imshow(selected_img,cmap='gray')
  131. #svg_path = os.path.join(qc_path,+ img_name+".png").replace(".nii","").replace(".gz","")
  132. svg_path = os.path.join(qc_path,str(N)+"_"+ img_name+"_"+ str(dd)+".png").replace(".nii","").replace(".gz","")
  133. dd = dd +1
  134. plt.savefig(svg_path)
  135. ########### Slice extraction
  136. # other Features
  137. SpatRes = ResCalculator(input_file)
  138. GMetric = GoastCheck(input_file)
  139. if N == 'anat':
  140. # Signal 2 noise ratio
  141. snrCh = snrCalclualtor_chang(input_file)
  142. snr_normal = snrCalclualtor_normal(input_file)
  143. LMV_all = np.nan
  144. GMV_all = np.nan
  145. Max_mov_between_all = np.nan
  146. snr_normal_vec.append(snr_normal)
  147. snrCh_vec.append(snrCh)
  148. elif N == 'diff':
  149. # Signal 2 noise ratio
  150. #print(tf)
  151. snrCh = snrCalclualtor_chang(input_file)
  152. snr_normal = snrCalclualtor_normal(input_file)
  153. Final,Max_mov_between,GMV,LMV = Ismotion(input_file)
  154. GMV_all.append(GMV)
  155. LMV_all.append(LMV)
  156. MI_vec_all.append(Final)
  157. Max_mov_between_all.append(Max_mov_between)
  158. snr_normal_vec.append(snr_normal)
  159. snrCh_vec.append(snrCh)
  160. elif N == 'func':
  161. #temporal signal 2 noise ratio
  162. #print(tf)
  163. tSNR = TsnrCalclualtor(input_file)
  164. Final,Max_mov_between,GMV,LMV = Ismotion(input_file)
  165. Max_mov_between_all.append(Max_mov_between)
  166. GMV_all.append(GMV)
  167. LMV_all.append(LMV)
  168. MI_vec_all.append(Final)
  169. Max_mov_between_all.append(Max_mov_between)
  170. tsnr_vec.append(tSNR)
  171. i=i+1
  172. bar()
  173. text_files_new.append(tf)
  174. SpatRes_vec.append(SpatRes)
  175. GMetric_vec.append(GMetric)
  176. df = pd.DataFrame()
  177. df['FileAddress'] = text_files_new
  178. df['sequence name'] = keys
  179. df["corresponding_img"] = img_names_new
  180. df['SpatRx'] = np.array(SpatRes_vec)[:,0]
  181. df['SpatRy'] = np.array(SpatRes_vec)[:,1]
  182. df['Slicethick'] = np.array(SpatRes_vec)[:,2]
  183. df['Goasting'] = np.array(GMetric_vec)
  184. if N == 'anat':
  185. df['SNR Chang'] = np.array(snrCh_vec)
  186. df['SNR Normal'] = np.array(snr_normal_vec)
  187. elif N == 'diff':
  188. df['SNR Chang'] = np.array(snrCh_vec)
  189. df['SNR Normal'] = np.array(snr_normal_vec)
  190. df['Displacement factor (std of Mutual information)']=np.array(LMV_all)
  191. #df['Maximal displacement']=AR[4]
  192. elif N == "func":
  193. df['tSNR (Averaged Brain ROI)'] = np.array(tsnr_vec)
  194. df['Displacement factor (std of Mutual information)']=np.array(LMV_all)
  195. #df['Maximal displacement']=AR[4]
  196. if N=="anat":
  197. t2w_result= os.path.join(Path,"caculated_features_anat.csv")
  198. df.to_csv( t2w_result)
  199. elif N=="diff":
  200. dti_result= os.path.join(Path,"caculated_features_diff.csv")
  201. df.to_csv( dti_result)
  202. elif N=="func":
  203. fmri_result= os.path.join(Path,"caculated_features_func.csv")
  204. df.to_csv(fmri_result)
  205. if ErorrList:
  206. dfNewError = pd.DataFrame(ErorrList)
  207. new_file_path = os.path.join(saving_path, "CanNotProcessTheseFiles.csv")
  208. dfNewError.to_csv(new_file_path)
  209. print('\n\noutput files were created:' + str(Path))
  210. print('\n\n%%%%%%%%%%%%% End of the stage 2 %%%%%%%%%%%%%%%\n\n'.upper())
  211. #%% exact above function but this time for nifti format
  212. def CheckingNiftiFeatures(Path):
  213. Abook = []
  214. Names =[]
  215. for file in glob.glob(os.path.join(Path, '*addreses*.csv')) :
  216. if "anat" in file :
  217. t2w_path= file
  218. t2w_addreses= pd.read_csv(t2w_path)
  219. Abook.append(t2w_addreses)
  220. Names.append("anat")
  221. elif "diff" in file:
  222. dti_path= file
  223. dti_addreses= pd.read_csv(dti_path)
  224. Abook.append(dti_addreses)
  225. Names.append("diff")
  226. elif "func" in file :
  227. fmri_path= file
  228. fmri_addreses= pd.read_csv(fmri_path)
  229. Abook.append(fmri_addreses)
  230. Names.append("func")
  231. ErorrList = []
  232. saving_path = os.path.dirname(Path)
  233. C = np.array([not Check.empty for Check in Abook])
  234. Names = np.array(Names)[C].tolist()
  235. Names.append('ErrorData')
  236. Abook2 = [a.values.tolist() for a in Abook]
  237. #% Calculate all the SNR for all the data that was found
  238. # in the last step and saving it into a vector
  239. # Load Bruker data from each address
  240. kk =0
  241. for ii,N in enumerate(Names):
  242. if N != 'ErrorData':
  243. if kk > 0:
  244. print(str(kk) + 'faulty files were found: All faulty files are available in the Errorlist tab in the Excel outputs\n')
  245. print(N+' processing... \n')
  246. text_files0 = Abook2[ii]
  247. text_files = [i[0] for i in text_files0]
  248. dd = 1
  249. snrCh_vec =[]
  250. tsnr_vec = []
  251. snr_normal_vec = []
  252. SpatRes_vec = []
  253. MI_vec_all = []
  254. LMV_all = []
  255. GMV_all = []
  256. Max_mov_between_all = []
  257. text_files_new = []
  258. img_names_new = []
  259. GMetric_vec = []
  260. kk = 0
  261. i=1
  262. with ap.alive_bar(len(text_files),spinner='wait') as bar:
  263. for tf in text_files:
  264. tf = str(tf)
  265. print(tf)
  266. """
  267. if "DTI".upper() in tf.upper() :
  268. N= "DTI"
  269. if "T2w".upper() in tf.upper():
  270. N="T2w"
  271. if "fMRI".upper() in tf.upper():
  272. N="rsfMRI"
  273. """
  274. tf = os.path.normpath(tf)
  275. try:
  276. input_file= nib.load(tf)
  277. except nib.loadsave.ImageFileError:
  278. print("could not load the following file (check the size of the file):")
  279. print(tf)
  280. ErorrList.append(tf)
  281. continue
  282. ########### Slice extraction
  283. selected_img = Image_Selection(input_file)
  284. qc_path = os.path.join(Path,"manual_slice_inspection")
  285. if not os.path.isdir(qc_path):
  286. os.mkdir(qc_path)
  287. img_name = str.split(tf,os.sep)[-1]
  288. folder_name = str.split(tf,os.sep)[-2]
  289. full_img_name = (str(N)+"_"+folder_name+"_"+img_name+"_"+str(dd)+".png").replace(".nii","").replace(".gz","")
  290. img_names_new.append(full_img_name)
  291. #plt.figure()
  292. plt.axis('off')
  293. plt.imshow(selected_img,cmap='gray')
  294. svg_path = os.path.join(qc_path,str(N)+"_"+folder_name+"_"+img_name+"_"+str(dd)+".png").replace(".nii","").replace(".gz","")
  295. dd = dd +1
  296. plt.savefig(svg_path)
  297. ########### Slice extraction
  298. # other Features
  299. SpatRes = ResCalculator(input_file)
  300. GMetric = GoastCheck(input_file)
  301. if N == "anat":
  302. # Signal 2 noise ratio
  303. snrCh = snrCalclualtor_chang(input_file)
  304. snr_normal = snrCalclualtor_normal(input_file)
  305. LMV_all = np.nan
  306. GMV_all = np.nan
  307. Max_mov_between_all = np.nan
  308. snr_normal_vec.append(snr_normal)
  309. snrCh_vec.append(snrCh)
  310. elif N == "diff":
  311. # Signal 2 noise ratio
  312. snrCh = snrCalclualtor_chang(input_file)
  313. snr_normal = snrCalclualtor_normal(input_file)
  314. Final,Max_mov_between,GMV,LMV = Ismotion(input_file)
  315. GMV_all.append(GMV)
  316. LMV_all.append(LMV)
  317. MI_vec_all.append(Final)
  318. Max_mov_between_all.append(Max_mov_between)
  319. snr_normal_vec.append(snr_normal)
  320. snrCh_vec.append(snrCh)
  321. elif N == "func":
  322. #temporal signal 2 noise ratio
  323. tSNR = TsnrCalclualtor(input_file)
  324. Final,Max_mov_between,GMV,LMV = Ismotion(input_file)
  325. Max_mov_between_all.append(Max_mov_between)
  326. GMV_all.append(GMV)
  327. LMV_all.append(LMV)
  328. MI_vec_all.append(Final)
  329. Max_mov_between_all.append(Max_mov_between)
  330. tsnr_vec.append(tSNR)
  331. i=i+1
  332. bar()
  333. text_files_new.append(tf)
  334. SpatRes_vec.append(SpatRes)
  335. GMetric_vec.append(GMetric)
  336. # Saving parsed files to excel sheets
  337. #AR = [text_files_new,np.array(SpatRes_vec),np.array(snrCh_vec),np.array(LMV_all),np.array(GMV_all),np.array(snr_normal_vec)]
  338. # using the savetxt
  339. # from the numpy module
  340. df = pd.DataFrame()
  341. df['FileAddress'] = text_files_new
  342. df["corresponding_img"] = img_names_new
  343. df['SpatRx'] = np.array(SpatRes_vec)[:,0]
  344. df['SpatRy'] = np.array(SpatRes_vec)[:,1]
  345. df['Slicethick'] = np.array(SpatRes_vec)[:,2]
  346. df['Goasting'] = np.array(GMetric_vec)
  347. if N == "anat":
  348. df['SNR Chang'] = np.array(snrCh_vec)
  349. df['SNR Normal'] = np.array(snr_normal_vec)
  350. elif N == "diff":
  351. df['SNR Chang'] = np.array(snrCh_vec)
  352. df['SNR Normal'] = np.array(snr_normal_vec)
  353. df['Displacement factor (std of Mutual information)']=np.array(LMV_all)
  354. #df['Maximal displacement']=AR[4]
  355. elif N == "func":
  356. df['tSNR (Averaged Brain ROI)'] = np.array(tsnr_vec)
  357. df['Displacement factor (std of Mutual information)']=np.array(LMV_all)
  358. #df['Maximal displacement']=AR[4]
  359. if N=="anat":
  360. t2w_result= os.path.join(Path,"caculated_features_anat.csv")
  361. df.to_csv( t2w_result)
  362. elif N=="diff":
  363. dti_result= os.path.join(Path,"caculated_features_diff.csv")
  364. df.to_csv( dti_result)
  365. elif N=="func":
  366. fmri_result= os.path.join(Path,"caculated_features_func.csv")
  367. df.to_csv(fmri_result)
  368. if ErorrList:
  369. dfNewError = pd.DataFrame(ErorrList)
  370. new_file_path = os.path.join(saving_path, "CanNotProcessTheseFiles.csv")
  371. dfNewError.to_csv(new_file_path)
  372. print('\n\noutput file was created:' + str(Path))
  373. print('\n\n%%%%%%%%%%%%% End of the stage 2 %%%%%%%%%%%%%%%\n\n'.upper())
  374. #%%