ck_Combine_MRI_SliceChunks_cv.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. function ck_Combine_MRI_SliceChunks_cv(Monkey,Session)
  2. % For prf analysis that is run on LISA in separate slice chunks, this
  3. % function recombines the results in volumes
  4. % location of this script
  5. homefld = pwd;
  6. % Get the root folder path for the hared data/code
  7. cd ../../../
  8. SHARED_ROOT_FLD = pwd;
  9. cd(homefld)
  10. tool_basepath = fullfile(SHARED_ROOT_FLD,'Toolboxes');
  11. addpath(genpath(fullfile(tool_basepath, 'NIfTI')));
  12. ResFld = fullfile(SHARED_ROOT_FLD,'FitResults','MRI',Monkey,Session);
  13. cd(ResFld);
  14. fld = dir('Slices_*');
  15. for s=1:length(fld)
  16. cd(fullfile(fld(s).folder, fld(s).name));
  17. matfile = ls('*mat');
  18. fprintf(['Loading ' matfile 'Chunk ' num2str(s) '\n']);
  19. R = load(matfile(1:end-4));
  20. if s==1
  21. result = R.result;
  22. else % concat
  23. result.ang = cat(3,result.ang,R.result.ang);
  24. result.ecc = cat(3,result.ecc,R.result.ecc);
  25. if isfield(result,'expt')
  26. result.expt = cat(3,result.expt,R.result.expt);
  27. end
  28. if isfield(result,'sdratio')
  29. result.sdratio = cat(3,result.sdratio,R.result.sdratio);
  30. end
  31. if isfield(result,'normamp')
  32. result.normamp = cat(3,result.normamp,R.result.normamp);
  33. end
  34. result.rfsize = cat(3,result.rfsize,R.result.rfsize);
  35. result.R2 = cat(3,result.R2,R.result.R2);
  36. result.gain = cat(3,result.gain,R.result.gain);
  37. result.resnorms = cat(3,result.resnorms,R.result.resnorms);
  38. result.numiters = cat(3,result.numiters,R.result.numiters);
  39. result.meanvol = cat(3,result.meanvol,R.result.meanvol);
  40. end
  41. clear R
  42. end
  43. cd(ResFld);
  44. % fwhm
  45. fprintf('Calculating FWHM..\n');
  46. if isfield(result,'sdratio')
  47. result.fwhm=zeros(size(result.rfsize));
  48. for x=1:size(result.rfsize,1)
  49. for y=1:size(result.rfsize,2)
  50. for z=1:size(result.rfsize,3)
  51. for cv = 1:size(result.rfsize,4)
  52. sig1 = result.rfsize(x,y,z,cv);
  53. sig2 = result.sdratio(x,y,z,cv).*result.rfsize(x,y,z,cv);
  54. a2 = result.normamp(x,y,z,cv);
  55. p=-100:0.1:100;
  56. g = normpdf(p,0,sig1) - a2*normpdf(p,0,sig2);
  57. pp = p(g>=(max(g)/2));
  58. if ~isempty(pp)
  59. result.fwhm(x,y,z,cv) = pp(end)-pp(1);
  60. else
  61. result.fwhm(x,y,z,cv) = 0;
  62. end
  63. end
  64. end
  65. end
  66. end
  67. else
  68. for cv = 1:size(result.rfsize,4)
  69. result.fwhm(:,:,:,cv) = result.rfsize(:,:,:,cv).*(2*sqrt(2*log(2)));
  70. end
  71. end
  72. % save the result ----
  73. fprintf('Merging and saving the result: mat-file, ');
  74. save(fullfile(ResFld,['pRF_Sess-' Session]),'result','-v7.3');
  75. % also save as nifti files
  76. for cv=1:2
  77. % angle ---
  78. fprintf('Angles, ');
  79. nii = make_nii(result.ang(:,:,:,cv),[1 1 1],[],16,...
  80. 'pRF fit: Angles (deg)');
  81. nii.hdr.hist = result.hdr_ref.hist;
  82. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  83. save_nii(nii, fullfile(ResFld, ...
  84. ['Sess-' Session '_ang_' num2str(cv) '.nii']));
  85. gzip(fullfile(ResFld, ['Sess-' Session '_ang_' num2str(cv) '.nii']));
  86. delete(fullfile(ResFld, ['Sess-' Session '_ang_' num2str(cv) '.nii']));
  87. % split to real(-cos)/imag(sin) components
  88. nii = make_nii(-cosd(result.ang(:,:,:,cv)),[1 1 1],[],16,...
  89. 'pRF fit: Angles_real (deg)');
  90. nii.hdr.hist = result.hdr_ref.hist;
  91. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  92. save_nii(nii, fullfile(ResFld, ...
  93. ['Sess-' Session '_real_' num2str(cv) '.nii']));
  94. gzip(fullfile(ResFld, ['Sess-' Session '_real_' num2str(cv) '.nii']));
  95. delete(fullfile(ResFld, ['Sess-' Session '_real_' num2str(cv) '.nii']));
  96. nii = make_nii(sind(result.ang(:,:,:,cv)),[1 1 1],[],16,...
  97. 'pRF fit: Angles_imag (deg)');
  98. nii.hdr.hist = result.hdr_ref.hist;
  99. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  100. save_nii(nii, fullfile(ResFld, ...
  101. ['Sess-' Session '_imag_' num2str(cv) '.nii']));
  102. gzip(fullfile(ResFld, ['Sess-' Session '_imag_' num2str(cv) '.nii']));
  103. delete(fullfile(ResFld, ['Sess-' Session '_imag_' num2str(cv) '.nii']));
  104. % ecc ---
  105. fprintf('Ecc, ');
  106. nii = make_nii(result.ecc(:,:,:,cv),[1 1 1],[],16,...
  107. 'pRF fit: Eccentricity (pix)');
  108. nii.hdr.hist = result.hdr_ref.hist;
  109. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  110. save_nii(nii, fullfile(ResFld, ...
  111. ['Sess-' Session '_ecc_' num2str(cv) '.nii']));
  112. gzip(fullfile(ResFld, ['Sess-' Session '_ecc_' num2str(cv) '.nii']));
  113. delete(fullfile(ResFld, ['Sess-' Session '_ecc_' num2str(cv) '.nii']));
  114. % size ---
  115. fprintf('Size, ');
  116. nii = make_nii(result.rfsize(:,:,:,cv),[1 1 1],[],16,...
  117. 'pRF fit: RF size (pix)');
  118. nii.hdr.hist = result.hdr_ref.hist;
  119. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  120. save_nii(nii, fullfile(ResFld, ...
  121. ['Sess-' Session '_rfsize_' num2str(cv) '.nii']));
  122. gzip(fullfile(ResFld, ['Sess-' Session '_rfsize_' num2str(cv) '.nii']));
  123. delete(fullfile(ResFld, ['Sess-' Session '_rfsize_' num2str(cv) '.nii']));
  124. % gain ---
  125. fprintf('Gain, ');
  126. nii = make_nii(result.gain(:,:,:,cv),[1 1 1],[],16,...
  127. 'pRF fit: Gain');
  128. nii.hdr.hist = result.hdr_ref.hist;
  129. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  130. save_nii(nii, fullfile(ResFld, ...
  131. ['Sess-' Session '_gain_' num2str(cv) '.nii']));
  132. gzip(fullfile(ResFld, ['Sess-' Session '_gain_' num2str(cv) '.nii']));
  133. delete(fullfile(ResFld, ['Sess-' Session '_gain_' num2str(cv) '.nii']));
  134. % exponential ---
  135. if isfield(result,'expt')
  136. fprintf('Expt, ');
  137. nii = make_nii(result.expt(:,:,:,cv),[1 1 1],[],16,...
  138. 'pRF fit: RF size (pix)');
  139. nii.hdr.hist = result.hdr_ref.hist;
  140. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  141. save_nii(nii, fullfile(ResFld, ...
  142. ['Sess-' Session '_expt_' num2str(cv) '.nii']));
  143. gzip(fullfile(ResFld, ['Sess-' Session '_expt_' num2str(cv) '.nii']));
  144. delete(fullfile(ResFld, ['Sess-' Session '_expt_' num2str(cv) '.nii']));
  145. nii = make_nii(result.expt(:,:,:,cv).^2,[1 1 1],[],16,...
  146. 'pRF fit: RF size (pix)');
  147. nii.hdr.hist = result.hdr_ref.hist;
  148. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  149. save_nii(nii, fullfile(ResFld, ...
  150. ['Sess-' Session '_exptsq_' num2str(cv) '.nii']));
  151. gzip(fullfile(ResFld, ['Sess-' Session '_exptsq_' num2str(cv) '.nii']));
  152. delete(fullfile(ResFld, ['Sess-' Session '_exptsq_' num2str(cv) '.nii']));
  153. end
  154. % sdratio ---
  155. if isfield(result,'sdratio')
  156. fprintf('sdratio, ');
  157. nii = make_nii(result.sdratio(:,:,:,cv),[1 1 1],[],16,...
  158. 'pRF fit: sdratio');
  159. nii.hdr.hist = result.hdr_ref.hist;
  160. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  161. save_nii(nii, fullfile(ResFld, ...
  162. ['Sess-' Session '_sdratio_' num2str(cv) '.nii']));
  163. gzip(fullfile(ResFld, ['Sess-' Session '_sdratio_' num2str(cv) '.nii']));
  164. delete(fullfile(ResFld, ['Sess-' Session '_sdratio_' num2str(cv) '.nii']));
  165. fprintf('sd2, ');
  166. sd2 = result.sdratio(:,:,:,cv).*result.rfsize(:,:,:,cv);
  167. nii = make_nii(sd2,[1 1 1],[],16,...
  168. 'pRF fit: sd2');
  169. nii.hdr.hist = result.hdr_ref.hist;
  170. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  171. save_nii(nii, fullfile(ResFld, ...
  172. ['Sess-' Session '_sd2_' num2str(cv) '.nii']));
  173. gzip(fullfile(ResFld, ['Sess-' Session '_sd2_' num2str(cv) '.nii']));
  174. delete(fullfile(ResFld, ['Sess-' Session '_sd2_' num2str(cv) '.nii']));
  175. % fwhm ---
  176. fprintf('FWHM, ');
  177. nii = make_nii(result.fwhm(:,:,:,cv),[1 1 1],[],16,...
  178. 'pRF fit: FWHM (pix)');
  179. nii.hdr.hist = result.hdr_ref.hist;
  180. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  181. save_nii(nii, fullfile(ResFld, ...
  182. ['Sess-' Session '_FWHM_' num2str(cv) '.nii']));
  183. gzip(fullfile(ResFld, ['Sess-' Session '_FWHM_' num2str(cv) '.nii']));
  184. delete(fullfile(ResFld, ['Sess-' Session '_FWHM_' num2str(cv) '.nii']));
  185. else
  186. % fwhm ---
  187. fprintf('FWHM, ');
  188. nii = make_nii(result.fwhm(:,:,:,cv),[1 1 1],[],16,...
  189. 'pRF fit: FWHM (pix)');
  190. nii.hdr.hist = result.hdr_ref.hist;
  191. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  192. save_nii(nii, fullfile(ResFld, ...
  193. ['Sess-' Session '_FWHM_' num2str(cv) '.nii']));
  194. gzip(fullfile(ResFld, ['Sess-' Session '_FWHM_' num2str(cv) '.nii']));
  195. delete(fullfile(ResFld, ['Sess-' Session '_FWHM_' num2str(cv) '.nii']));
  196. end
  197. % normamp ---
  198. if isfield(result,'normamp')
  199. fprintf('normamp, ');
  200. nii = make_nii(result.normamp(:,:,:,cv),[1 1 1],[],16,...
  201. 'pRF fit: normamp');
  202. nii.hdr.hist = result.hdr_ref.hist;
  203. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  204. save_nii(nii, fullfile(ResFld, ...
  205. ['Sess-' Session '_normamp_' num2str(cv) '.nii']));
  206. gzip(fullfile(ResFld, ['Sess-' Session '_normamp_' num2str(cv) '.nii']));
  207. delete(fullfile(ResFld, ['Sess-' Session '_normamp_' num2str(cv) '.nii']));
  208. end
  209. % R^2 ---
  210. fprintf('R2 ');
  211. nii = make_nii(result.R2(:,:,:,cv),[1 1 1],[],16,...
  212. 'pRF fit: R2 Goodnes off fit');
  213. nii.hdr.hist = result.hdr_ref.hist;
  214. nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64;
  215. save_nii(nii, fullfile(ResFld, ...
  216. ['Sess-' Session '_R2_' num2str(cv) '.nii']));
  217. gzip(fullfile(ResFld, ['Sess-' Session '_R2_' num2str(cv) '.nii']));
  218. delete(fullfile(ResFld, ['Sess-' Session '_R2_' num2str(cv) '.nii']));
  219. end
  220. fprintf('>> Done!\n');
  221. cd(homefld)