pRF_FitModel_LISA_ephys.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. function pRF_FitModel_LISA_ephys(Monkey,Session,Instance,numWorkers,modeltype,cv,resfld)
  2. % This is the script that runs the pRF fit
  3. % it should be compiled and called from run_CompiledMatlab_LISA.sh
  4. % This means it cannot use 'addpath', instead toolboxes or required
  5. % function should be added when it is compiled usin -a /toolbox etc.
  6. % compile on LISA with:
  7. % module load matlab
  8. % echo "mcc -m $HOME/PRF/Code/pRF_FitModel_LISA.m -a $HOME/PRF/Code/analyzePRF " | matlab -nodisplay
  9. % module unload matlab
  10. % The uncompiled version of this function should be in
  11. % $HOME/PRF/Code
  12. % this compiled function will be running from:
  13. % "$TMPDIR"/PRF
  14. % Data will be in
  15. % "$TMPDIR"/PRF
  16. DEBUG=false;
  17. if DEBUG
  18. Monkey = 'M03'; %#ok<*UNRCH>
  19. Session = 'mua';
  20. Instance = 1;
  21. numWorkers = 2;
  22. modeltype = 'linear_ephys';
  23. cv = 1;
  24. resfld = 'debug'
  25. fprintf('RUNNING IN DEBUG MODE! CHANGE THIS FLAG FOR PRODUCTION!\n');
  26. end
  27. %% These are fixed for this configuration =================================
  28. TR=0.5;
  29. mlroot = pwd; % this is $TMPDIR/PRF when running it on LISA (fast disks)
  30. Pix2Deg = 1/29.5;
  31. %% Prep & Load ============================================================
  32. % change characters to numbers
  33. if ischar(Instance); Instance = eval(Instance); end
  34. if ischar(numWorkers); numWorkers = eval(numWorkers); end
  35. if ischar(cv); cv = eval(cv); end
  36. InstanceLabel = num2str(Instance);
  37. % Notification of the fact that we're starting
  38. disp(['Starting script for job ' Monkey ', ' Session ', Instance ' num2str(Instance)])
  39. % make outputfolder
  40. % result_folder = [mlroot '/Results/' Monkey '/' Session '/Slices_' SliceLabel];
  41. result_folder = fullfile(mlroot, 'Results', Monkey, resfld, ['Instance_' InstanceLabel]);
  42. if ~exist(result_folder,'dir')
  43. [~,~] = mkdir(result_folder);
  44. end
  45. fprintf(['Saving results in: ' result_folder '\n']);
  46. %% MODEL PRFs /SESSION ====================================================
  47. fprintf(['=== Fitting pRF model for ses-' Session ' ===\n']);
  48. fprintf('Loading data...\n');
  49. datafld = fullfile('/home/pcklink/PRF/Data/ephys/', Monkey);
  50. % load the stimulus
  51. if strcmp(Monkey,'M03')
  52. load(fullfile(datafld,'stim','M03_20180807_B2_STIM.mat'))
  53. elseif strcmp(Monkey,'M04')
  54. load(fullfile(datafld,'stim','M04_20181004_B1_STIM.mat'))
  55. end
  56. if strcmp(Session, 'mua')
  57. % load the responses
  58. if cv == 0 % no crossvalidation
  59. if strcmp(Monkey,'M03')
  60. RESP=load(fullfile(datafld,Session,['M04_20180807_B2_array_' num2str(Instance) '_mMUA.mat']));
  61. C=RESP.C;
  62. elseif strcmp(Monkey,'M04')
  63. RESP=load(fullfile(datafld,Session,['M04_20181004_B1_array_' num2str(Instance) '_mMUA.mat']));
  64. C=RESP.C;
  65. end
  66. elseif cv == 1 % crossvalidation
  67. if strcmp(Monkey,'M03')
  68. RESP=load(fullfile(datafld,Session,['M03_20180807_B2_array_' num2str(Instance) '_mMUA_odd.mat']));
  69. C=RESP.C;
  70. RESP2=load(fullfile(datafld,Session,['M03_20180807_B2_array_' num2str(Instance) '_mMUA_even.mat']));
  71. RESP.mMUA_even=RESP2.mMUA_even;
  72. elseif strcmp(Monkey,'M04')
  73. RESP=load(fullfile(datafld,Session,['M04_20181004_B1_array_' num2str(Instance) '_mMUA_odd.mat']));
  74. C=RESP.C;
  75. RESP2=load(fullfile(datafld,Session,['M04_20181004_B1_array_' num2str(Instance) '_mMUA_even.mat']));
  76. RESP.mMUA_even=RESP2.mMUA_even;
  77. end
  78. end
  79. % concatenate -----
  80. stimulus={};ephys_data={};
  81. fprintf('Concatenating stimuli and volumes...\n');
  82. if cv == 0
  83. ephys_data{1} = [];
  84. for ch=1:128
  85. %ephys_data{1}=cat(1,ephys_data{1},...
  86. % RESP.mMUA(ch).bar - RESP.mMUA(ch).BL);
  87. ephys_data{1}=cat(1,ephys_data{1},...
  88. RESP.mMUA(ch).bar);
  89. end
  90. stimulus{1}=[];
  91. for imgnr=1:length(STIM.img)/2
  92. stimulus{1}=cat(3,stimulus{1},STIM.img{imgnr});
  93. end
  94. inv_idx = [150:-1:121 180:-1:151 210:-1:181 240:-1:211];
  95. ephys_data2{1}=[];
  96. for elec = 1:size(ephys_data{1},1)
  97. ephys_data2{1} = cat(1,ephys_data2{1},...
  98. mean([ephys_data{1}(elec,1:120);ephys_data{1}(elec,inv_idx)],1));
  99. end
  100. ephys_data_org = ephys_data;
  101. ephys_data = ephys_data2; clear ephys_data2
  102. elseif cv == 1
  103. ephys_data{1}=[];ephys_data{2}=[];
  104. for ch=1:128
  105. % ephys_data{1}=cat(1,ephys_data{1},...
  106. % RESP.mMUA_odd(ch).bar - RESP.mMUA_odd(ch).BL);
  107. % ephys_data{2}=cat(1,ephys_data{2},...
  108. % RESP.mMUA_even(ch).bar - RESP.mMUA_even(ch).BL);
  109. ephys_data{1}=cat(1,ephys_data{1},...
  110. RESP.mMUA_odd(ch).bar);
  111. ephys_data{2}=cat(1,ephys_data{2},...
  112. RESP.mMUA_even(ch).bar);
  113. end
  114. stimulus{1}=[];stimulus{2}=[];
  115. for imgnr=1:length(STIM.img)/2
  116. stimulus{1}=cat(3,stimulus{1},STIM.img{imgnr});
  117. stimulus{2}=stimulus{1};
  118. end
  119. inv_idx = [150:-1:121 180:-1:151 210:-1:181 240:-1:211];
  120. ephys_data2{1}=[]; ephys_data2{2}=[];
  121. for elec = 1:size(ephys_data{1},1)
  122. ephys_data2{1} = cat(1,ephys_data2{1},...
  123. mean([ephys_data{1}(elec,1:120);ephys_data{1}(elec,inv_idx)],1));
  124. ephys_data2{2} = cat(1,ephys_data2{2},...
  125. mean([ephys_data{2}(elec,1:120);ephys_data{2}(elec,inv_idx)],1));
  126. end
  127. ephys_data_org = ephys_data;
  128. ephys_data = ephys_data2; clear ephys_data2
  129. end
  130. % fit pRF -----
  131. options.display = 'final';
  132. % set crossvalidation option
  133. options.xvalmode = cv; % two-fold cross-validation (first half of runs; second half of runs)
  134. % no denoising
  135. options.wantglmdenoise = 0;
  136. % set typicalgain to a lower value
  137. % options.typicalgain = 10;
  138. % allow negative gain factors
  139. options.allowneggain = false;
  140. % no drift correction for ephys
  141. options.maxpolydeg = 0;
  142. % start a parallel pool of workers
  143. if ~isempty(numWorkers)
  144. parpool(numWorkers);
  145. else
  146. % if numWorkers = []
  147. % don't predefine the number of workers
  148. % let it take the max available when running
  149. end
  150. % run analyzePRF tool
  151. result = analyzePRF_modeltype(stimulus,ephys_data,TR,options,modeltype);
  152. result.Chan = C;
  153. result.Pix2Deg = Pix2Deg;
  154. % save the result ----
  155. fprintf('\nSaving the result: ');
  156. save(fullfile(result_folder,['pRF_Sess-' Session '_Inst_' num2str(Instance)]),...
  157. 'result','-v7.3');
  158. cd ..
  159. fprintf('>> Done!\n');
  160. elseif strcmp(Session,'lfp')
  161. % load the responses
  162. if cv == 0 % no crossvalidation
  163. if strcmp(Monkey,'M03')
  164. RESP=load(fullfile(datafld,Session,['M03_20180807_B2_array_' num2str(Instance) '_mLFP.mat']));
  165. C=RESP.C;
  166. elseif strcmp(Monkey,'M04')
  167. RESP=load(fullfile(datafld,Session,['M04_20181004_B1_array_' num2str(Instance) '_mLFP.mat']));
  168. C=RESP.C;
  169. end
  170. elseif cv == 1 % crossvalidation
  171. if strcmp(Monkey,'M03')
  172. RESP=load(fullfile(datafld,Session,['M03_20180807_B2_array_' num2str(Instance) '_mLFP_odd.mat']));
  173. C=RESP.C;
  174. RESP2=load(fullfile(datafld,Session,['M03_20180807_B2_array_' num2str(Instance) '_mLFP_even.mat']));
  175. RESP.mLFP_even=RESP2.mLFP_even;
  176. elseif strcmp(Monkey,'M04')
  177. RESP=load(fullfile(datafld,Session,['M04_20181004_B1_array_' num2str(Instance) '_mLFP_odd.mat']));
  178. C=RESP.C;
  179. RESP2=load(fullfile(datafld,Session,['M04_20181004_B1_array_' num2str(Instance) '_mLFP_even.mat']));
  180. RESP.mLFP_even=RESP2.mLFP_even;
  181. end
  182. end
  183. for fb=1:5 % loop over frequency bands
  184. % concatenate -----
  185. stimulus={};ephys_data={};
  186. fprintf(['Frequency band ' num2str(fb) '\n']);
  187. fprintf('Concatenating stimuli and volumes...\n');
  188. if cv == 0
  189. ephys_data{1} = [];
  190. for ch=1:128
  191. ephys_data{1}=cat(1,ephys_data{1}, ...
  192. RESP.mLFP(ch).freq(fb).bar - ...
  193. RESP.mLFP(ch).freq(fb).BL);
  194. end
  195. stimulus{1}=[];
  196. for imgnr=1:length(STIM.img)/2
  197. stimulus{1}=cat(3,stimulus{1},STIM.img{imgnr});
  198. end
  199. inv_idx = [150:-1:121 180:-1:151 210:-1:181 240:-1:211];
  200. ephys_data2{1}=[];
  201. for elec = 1:size(ephys_data{1},1)
  202. ephys_data2{1} = cat(1,ephys_data2{1},...
  203. mean([ephys_data{1}(elec,1:120);ephys_data{1}(elec,inv_idx)],1));
  204. end
  205. ephys_data_org = ephys_data;
  206. ephys_data = ephys_data2; clear ephys_data2
  207. elseif cv == 1
  208. ephys_data{1}=[];ephys_data{2}=[];
  209. for ch=1:128
  210. ephys_data{1}=cat(1,ephys_data{1}, ...
  211. RESP.mLFP_odd(ch).freq(fb).bar - ...
  212. RESP.mLFP_odd(ch).freq(fb).BL);
  213. ephys_data{2}=cat(1,ephys_data{2},...
  214. RESP.mLFP_even(ch).freq(fb).bar - ...
  215. RESP.mLFP_even(ch).freq(fb).BL);
  216. end
  217. stimulus{1}=[];stimulus{2}=[];
  218. for imgnr=1:length(STIM.img)/2
  219. stimulus{1}=cat(3,stimulus{1},STIM.img{imgnr});
  220. stimulus{2}=stimulus{1};
  221. end
  222. inv_idx = [150:-1:121 180:-1:151 210:-1:181 240:-1:211];
  223. ephys_data2{1}=[]; ephys_data2{2}=[];
  224. for elec = 1:size(ephys_data{1},1)
  225. ephys_data2{1} = cat(1,ephys_data2{1},...
  226. mean([ephys_data{1}(elec,1:120);ephys_data{1}(elec,inv_idx)],1));
  227. ephys_data2{2} = cat(1,ephys_data2{2},...
  228. mean([ephys_data{2}(elec,1:120);ephys_data{2}(elec,inv_idx)],1));
  229. end
  230. ephys_data_org = ephys_data;
  231. ephys_data = ephys_data2; clear ephys_data2
  232. end
  233. % fit pRF -----
  234. options.display = 'final';
  235. % set crossvalidation option
  236. options.xvalmode = cv; % two-fold cross-validation (first half of runs; second half of runs)
  237. % no denoising
  238. options.wantglmdenoise = 0;
  239. % set typicalgain to a lower value
  240. % options.typicalgain = 10;
  241. % allow negative gain factors
  242. options.allowneggain = false;
  243. % no drift correction for ephys
  244. options.maxpolydeg = 0;
  245. % start a parallel pool of workers
  246. if ~isempty(numWorkers)
  247. parpool(numWorkers);
  248. else
  249. % if numWorkers = []
  250. % don't predefine the number of workers
  251. % let it take the max available when running
  252. end
  253. % run analyzePRF tool
  254. result = analyzePRF_modeltype(stimulus,ephys_data,TR,options,modeltype);
  255. result.Chan = C;
  256. result.Pix2Deg = Pix2Deg;
  257. % save the result ----
  258. fprintf('\nSaving the result: ');
  259. save(fullfile(result_folder,['pRF_Sess-' Session '_fb' ...
  260. num2str(fb) '_Inst_' num2str(Instance)]),'result','-v7.3');
  261. end
  262. cd ..
  263. fprintf('>> Done!\n');
  264. end