check_quali.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. BIDS_path = "F:\Projects\StrokePT_tDCS_rsfmri\subjects";
  2. cd (BIDS_path)
  3. addpath("..\scripts\toolbox\DVARS-master\")
  4. addpath("..\scripts\toolbox\DVARS-master\Nifti_Util\")
  5. addpath("..\scripts")
  6. addpath("..\scripts\toolbox\spm12")
  7. addpath("..\scripts\toolbox\fMRI-Quality-Checker-master")
  8. addpath("..\scripts\toolbox\spm12\matlabbatch")
  9. addpath("..\scripts\toolbox\nifti_utils-master\")
  10. %% Preps
  11. % px = dir('sub*');
  12. timeX = {'tpA','tpB','tpC','tpD'};
  13. % master = zeros300,5};
  14. % noPhysio = zeros(300);
  15. master_tag = {'subject','group','timepoint','dataset','physio','volumes','bad_volumes','meanDeltaDvar'};
  16. tick = 1;
  17. tack = 1;
  18. % Check for registration quality (user input)?
  19. quali = "on";
  20. %% level1: group folders
  21. cd (BIDS_path)
  22. gx = dir('sub*');
  23. for g = 21:length(gx)
  24. subject = gx(g).name;
  25. cd (fullfile(gx(g).folder,gx(g).name))
  26. %% level2: timepoints
  27. tx = dir('ses*');
  28. for t = 1:length(tx)
  29. time = tx(t).name;
  30. cd (fullfile(tx(t).folder,tx(t).name))
  31. folder_to = pwd;
  32. %%
  33. % a) get structural
  34. if exist(fullfile(folder_to,"anat/"))==7 && exist (fullfile(folder_to,"func/"))==7
  35. cd (fullfile(folder_to,"anat/"))
  36. struct_orig = dir("*.1.nii.gz");
  37. struct_name = struct_orig(length(struct_orig)).name;
  38. struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  39. struct_reg = dir("*BiasBet_AnnorsfMRI.nii.gz");
  40. if ~isempty(struct_reg)
  41. structReg_fullfile = fullfile(struct_reg.folder,struct_reg.name);
  42. struct_BET = dir("*BiasBet.nii.gz");
  43. structBET_fullfile = fullfile(struct_BET.folder,struct_BET.name);
  44. end
  45. % copyfile(struct_fullfile,fullfile(folder_to,"anat/",strcat(struct_name(1:end-7),'_T2w.nii.gz')))
  46. struct_orig = dir("*_T2w.nii.gz");
  47. struct_name = struct_orig(length(struct_orig)).name;
  48. struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  49. % b) get fMRI
  50. cd (fullfile(folder_to,"func/"))
  51. fmri_orig = dir("*.1.nii.gz");
  52. clear ('fx_rep')
  53. for jx = 1:length(fmri_orig)
  54. fx_scan = niftiinfo(fmri_orig(jx).name);
  55. fx_rep(jx) = fx_scan.ImageSize(4);
  56. end
  57. fx_id = find(fx_rep>100);
  58. fx = length(fx_id);
  59. fMRI_fullfile = fullfile(fmri_orig(fx_id(1)).folder,fmri_orig(fx_id(1)).name);
  60. fMRI_file = fmri_orig(fx_id(1));
  61. fMRI_name = fMRI_file.name;
  62. % copyfile(fMRI_fullfile,fullfile(fMRI_file.folder,strcat(fMRI_name(1:end-7),'_bold.nii.gz')))
  63. info = niftiinfo(fMRI_fullfile);
  64. master{tick,6} = info.ImageSize(4);
  65. fmri_smooth = dir("*SmoothBet.nii.gz");
  66. if ~isempty(fmri_smooth)
  67. fmriSmooth_fullfile = fullfile(fmri_smooth.folder,fmri_smooth.name);
  68. end
  69. % c) get Regression File
  70. cd (fullfile(folder_to,"func/"))
  71. ABAreg_orig = dir("*SmoothBet_AnnoSplit_rsfMRI.nii.gz");
  72. if ~isempty(ABAreg_orig)
  73. ABAreg_fullfile = fullfile(ABAreg_orig.folder,ABAreg_orig.name);
  74. end
  75. if exist("regr","dir")
  76. cd regr\
  77. SFRGR_orig = dir("*_SFRGR.nii.gz");
  78. if ~isempty(SFRGR_orig)
  79. SFRGR_fullfile = fullfile(SFRGR_orig.folder,SFRGR_orig.name);
  80. else
  81. SFRGR_fullfile = [];
  82. end
  83. end
  84. %% plot raw images to check registration
  85. if ~exist("quali",'var')
  86. quali = "off"
  87. end
  88. if quali == "on"
  89. fMRI_imag = niftiread(fmriSmooth_fullfile);
  90. fMRI_imag = imrotate(fMRI_imag,-90);
  91. if ~isempty(ABAreg_orig)
  92. fMRI_Reg = niftiread(ABAreg_fullfile);
  93. fMRI_Reg = imrotate(fMRI_Reg,-90);
  94. else
  95. fMRI_Reg = zeros(96,96,16);
  96. end
  97. T2raw = niftiread(structBET_fullfile);
  98. T2raw = imrotate(T2raw,-90);
  99. if ~isempty(struct_reg)
  100. T2Reg = niftiread(structReg_fullfile);
  101. T2Reg = imrotate(T2Reg,-90);
  102. else
  103. T2Reg = zeros(256,256,28);
  104. end
  105. Loc = [8 10];
  106. Loc2 = [4 6];
  107. figure('units','normalized','outerposition',[0 0 1 1])
  108. for ix = 1:2
  109. % Raw T2w
  110. subplot(2,4,1+(4*(ix-1)))
  111. imagesc(fMRI_imag(:,:,Loc2(ix)))
  112. subplot(2,4,2+(4*(ix-1)))
  113. imagesc(fMRI_Reg(:,:,Loc2(ix)))
  114. % % subplot(2,4,1+(4*(ix-1)))
  115. % % imagesc(T2raw(:,:,floor(Loc(ix)/16*48)))
  116. colormap("hot")
  117. % % % T2 ARA Reg
  118. % % subplot(2,4,2+(4*(ix-1)))
  119. % % imagesc(T2Reg(:,:,floor(Loc(ix)/16*48)))
  120. % % % colormap("hot")
  121. % Raw fMRI
  122. subplot(2,4,3+(4*(ix-1)))
  123. imagesc(fMRI_imag(:,:,Loc(ix)))
  124. % colormap("gray")
  125. % fMRI ARA Reg
  126. subplot(2,4,4+(4*(ix-1)))
  127. imagesc(fMRI_Reg(:,:,Loc(ix)))
  128. % colormap("hot")
  129. end
  130. title(strcat(subject,'-',time))
  131. pause
  132. close
  133. %% user input to evaluate quality
  134. RegQuali{tick,1}=subject;
  135. RegQuali{tick,2}=time;
  136. % get input of image quality
  137. checkT2 = input("is T2 ok? Y/N [Y]");
  138. if isempty(checkT2)
  139. checkT2 = "Y";
  140. else
  141. checkT2 = "N";
  142. end
  143. RegQuali{tick,3} = checkT2;
  144. checkfMRI = input("is fMRI ok? Y/N [Y]");
  145. if isempty(checkfMRI)
  146. checkfMRI = "Y";
  147. else
  148. checkfMRI = "N";
  149. end
  150. disp('')
  151. RegQuali{tick,4} = checkfMRI;
  152. end
  153. %%
  154. tick=tick +1;
  155. end
  156. end
  157. end
  158. % % % a) get structural
  159. % % if exist(fullfile(parent_path,subject,"MRI",time,"T2w/"))==7 && exist (fullfile(parent_path,subject,"MRI",time,"fMRI/"))==7
  160. % % cd (fullfile(parent_path,subject,"MRI",time,"T2w/"))
  161. % % struct_orig = dir("*.1.nii.gz");
  162. % % struct_name = struct_orig(length(struct_orig)).name;
  163. % % struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  164. % %
  165. % % struct_reg = dir("*BiasBet_AnnorsfMRI.nii.gz");
  166. % % if ~isempty(struct_reg)
  167. % % structReg_fullfile = fullfile(struct_reg.folder,struct_reg.name);
  168. % % end
  169. % % struct_BET = dir("*BiasBet.nii.gz");
  170. % % structBET_fullfile = fullfile(struct_BET.folder,struct_BET.name);
  171. % %
  172. % %
  173. % % % b) get fMRI
  174. % % cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  175. % % fmri_orig = dir("*.1.nii.gz");
  176. % % fmri_name = fmri_orig(length(fmri_orig)).name;
  177. % % fMRI_fullfile = fullfile(fmri_orig(length(fmri_orig)).folder,fmri_orig(length(fmri_orig)).name);
  178. % % fmri_smooth = dir("*SmoothBet.nii.gz");
  179. % % fmriSmooth_fullfile = fullfile(fmri_smooth.folder,fmri_smooth.name);
  180. % %
  181. % % if ~exist("analyses","dir")
  182. % % mkdir("analyses")
  183. % % end
  184. % % cd ("analyses")
  185. % %
  186. % % % c) get Regression File
  187. % % cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  188. % % ABAreg_orig = dir("*SmoothBet_AnnoSplit_rsfMRI.nii.gz");
  189. % % if ~isempty(ABAreg_orig)
  190. % % ABAreg_fullfile = fullfile(ABAreg_orig.folder,ABAreg_orig.name);
  191. % % end
  192. % % cd regr\
  193. % % SFRGR_orig = dir("*_SFRGR.nii.gz");
  194. % % if ~isempty(SFRGR_orig)
  195. % % SFRGR_fullfile = fullfile(SFRGR_orig.folder,SFRGR_orig.name);
  196. % % else
  197. % % SFRGR_fullfile = [];
  198. % % end
  199. % % % plot raw images to check registration
  200. % %
  201. % %
  202. % % fMRI_imag = niftiread(fmriSmooth_fullfile);
  203. % % fMRI_imag = imrotate(fMRI_imag,-90);
  204. % %
  205. % % if ~isempty(ABAreg_orig)
  206. % % fMRI_Reg = niftiread(ABAreg_fullfile);
  207. % % fMRI_Reg = imrotate(fMRI_Reg,-90);
  208. % % else
  209. % % fMRI_Reg = zeros(96,96,16);
  210. % % end
  211. % %
  212. % % T2raw = niftiread(structBET_fullfile);
  213. % % T2raw = imrotate(T2raw,-90);
  214. % % if ~isempty(struct_reg)
  215. % % T2Reg = niftiread(structReg_fullfile);
  216. % % T2Reg = imrotate(T2Reg,-90);
  217. % % else
  218. % % T2Reg = zeros(256,256,28);
  219. % % end
  220. % %
  221. % % Loc = [8 13];
  222. % % %%
  223. % % figure('units','normalized','outerposition',[0 0 1 1])
  224. % % for ix = 1:2
  225. % % % Raw T2w
  226. % % subplot(2,4,1+(4*(ix-1)))
  227. % % imagesc(T2raw(:,:,floor(Loc(ix)/16*28)))
  228. % % colormap("hot")
  229. % % % T2 ARA Reg
  230. % % subplot(2,4,2+(4*(ix-1)))
  231. % % imagesc(T2Reg(:,:,floor(Loc(ix)/16*28)))
  232. % % % colormap("hot")
  233. % % % Raw fMRI
  234. % % subplot(2,4,3+(4*(ix-1)))
  235. % % imagesc(fMRI_imag(:,:,Loc(ix)))
  236. % % % colormap("gray")
  237. % % % fMRI ARA Reg
  238. % % subplot(2,4,4+(4*(ix-1)))
  239. % % imagesc(fMRI_Reg(:,:,Loc(ix)))
  240. % % % colormap("hot")
  241. % % end
  242. % % title(strcat(subject,'-',time))
  243. % % pause
  244. % % close
  245. % % %%
  246. % % RegQuali{tick,1}=subject;
  247. % % RegQuali{tick,2}=time;
  248. % %
  249. % % % get input of image quality
  250. % % checkT2 = input("is T2 ok? Y/N [Y]");
  251. % % if isempty(checkT2)
  252. % % checkT2 = "Y";
  253. % % else
  254. % % checkT2 = "N";
  255. % % end
  256. % % RegQuali{tick,3} = checkT2;
  257. % %
  258. % % checkfMRI = input("is fMRI ok? Y/N [Y]");
  259. % % if isempty(checkfMRI)
  260. % % checkfMRI = "Y";
  261. % % else
  262. % % checkfMRI = "N";
  263. % % end
  264. % % disp('')
  265. % % RegQuali{tick,4} = checkfMRI;
  266. % %
  267. % % tick = tick + 1;
  268. %
  269. % xlswrite('Reg_Quali.xls',RegQuali)