figure_neurobar_prepare.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. function figure_neurobar_prepare(fld,monkey,limit)
  2. fprintf('\n======================================================\n');
  3. fprintf(['-- Running figure_neurobar_prepare for ' monkey ' --\n']);
  4. fprintf('======================================================\n');
  5. % settings
  6. snrthres = 2.5;
  7. daythres = 3;
  8. do_cleantraces = true; % if true we throw out artefact traces
  9. %% find which data to include
  10. datainfo = session_info();
  11. if strcmp(monkey,'M1')
  12. info = datainfo(1);
  13. else
  14. info = datainfo(2);
  15. end
  16. sessions = info.dates;
  17. MP = get_mp(fld,monkey,sessions);
  18. %% set SNR threshold and find channels and sessions
  19. % limit MP to snr threshold
  20. incl = MP.snr>snrthres;
  21. MP_ = MP; %#ok<*NASGU> % copy for backup
  22. % only include channels that have at least 5 sessions
  23. MP = MP(incl,:);
  24. [chid, ch_list] = findgroups(MP.chan_id);
  25. session_per_chan = splitapply(@length,MP.session_id,chid);
  26. % make a list of channels that we will include
  27. chans = ch_list(session_per_chan>daythres);
  28. %% load and concatenate the data we need
  29. datadir = fld.procdatadir;
  30. % first loop all channels
  31. numtrls = [];
  32. traces = [];
  33. tracesLUT = [];
  34. c = 1;
  35. grandtraces = [];
  36. grandtracesLUT = [];
  37. chnum=1;
  38. for ch = chans'
  39. disp(['analysing channel ' num2str(ch) ', ' num2str(chnum) ...
  40. '/' num2str(size(chans,1))]);
  41. chidx = find(chans==ch);
  42. chnum=chnum+1;
  43. incl_sessions = MP.chan_id==ch & MP.snr>snrthres;
  44. cur_mp = MP(incl_sessions,:);
  45. numtrls(chidx) = sum(cellfun(@sum,cur_mp.goodtrials)); %#ok<*FNDSB,*AGROW>
  46. % look at each session for each channel individually
  47. env = []; clut = [];
  48. for sid = 1:size(cur_mp,1)
  49. % session info
  50. cdate = cur_mp.date{sid};
  51. block = cur_mp.block(sid);
  52. % load the data
  53. session_id = [monkey '_' cdate '_' num2str(block)];
  54. sessiondir = fullfile(datadir, monkey, session_id);
  55. channeldir = fullfile(sessiondir, 'individual_channels');
  56. chanfile = fullfile(channeldir, ...
  57. ['Env_preprocessed_ch' num2str(ch) '_' session_id '.mat']);
  58. if ~exist(chanfile,'file')
  59. continue
  60. end
  61. % load the data for this session for this channel
  62. load(chanfile); %#ok<*LOAD> % loads 'data'
  63. load(fullfile(sessiondir, [session_id '_LUT.mat'])); % loads 'lut'
  64. load(fullfile(sessiondir, [session_id '.mat']),'env_t');
  65. % this is overwritten every time but it's always the same so who cares
  66. if do_cleantraces % throw out artefact traces
  67. prewin = env_t > -0.1 & env_t < 0;
  68. postwin = env_t > 0 & env_t < 0.1;
  69. trace = mean(data');
  70. sd_pre = std(trace(prewin));
  71. m_post = mean(trace(postwin));
  72. if m_post < 3*sd_pre
  73. data = nan(size(data));
  74. end
  75. end
  76. env = [env; data'];
  77. clut = [clut; lut];
  78. end
  79. lut = clut;
  80. % we now have three independent variables, with 3 or 2 levels each:
  81. % - choice_identity:
  82. % - target (correct)
  83. % - nontarget
  84. % - distractor
  85. % - stimulus_in_RF:
  86. % - target
  87. % - nontarget
  88. % - distractor
  89. % - choice_in_rf:
  90. % - yes
  91. % - no
  92. % choice_identity
  93. ls = lut.Status;
  94. choice_identity = [strcmp(ls,'Target'), strcmp(ls,'Nontarget'), ...
  95. strcmp(ls,'Distractor')];
  96. choice_label = {'Target','NDist','SDist'};
  97. % get distances between chosen and target or salient distractor
  98. chdistT = [lut.TarPos lut.TarChoice abs(lut.TarChoice-lut.TarPos)];
  99. chdistSD = [lut.DistPos lut.TarChoice abs(lut.TarChoice-lut.DistPos)];
  100. chdistT(chdistT(:,3)<0,3)=chdistT(chdistT(:,3)<0,3)+6;
  101. chdistT(chdistT(:,3)>3,3) = 6-chdistT(chdistT(:,3)>3,3);
  102. chdistSD(chdistSD(:,3)<0,3)=chdistSD(chdistSD(:,3)<0,3)+6;
  103. chdistSD(chdistSD(:,3)>3,3) = 6-chdistSD(chdistSD(:,3)>3,3);
  104. for tr = 1:size(chdistT,1)
  105. if chdistT(tr,3)>3
  106. chdistT(tr,3) = mod(chdistT(tr,3),3);
  107. end
  108. if chdistSD(tr,3)>3
  109. chdistSD(tr,3) = mod(chdistSD(tr,3),3);
  110. end
  111. end
  112. % get RT
  113. RT = lut.reactTime2;
  114. % 30% fast/slow
  115. fastRT = RT<prctile(RT,limit.ReactimeTimePerc);
  116. slowRT = RT>prctile(RT,100-limit.ReactimeTimePerc);
  117. %% plot distance from target/dist on error trials
  118. if limit.PlotBehavior && chidx==1
  119. figure; set(gcf,'Position',[100 100 1000 1000])
  120. subplot(3,3,1);
  121. histogram(chdistT(choice_identity(:,2),3),0.5:1:3.5,...
  122. 'Normalization','probability');
  123. title('Choice ND: dist. T ALL-RT');
  124. xlabel('#Stim from T');ylabel('Proportion');
  125. set(gca, 'ylim',[0 1]);
  126. subplot(3,3,2);
  127. histogram(chdistT(choice_identity(:,3),3),0.5:1:3.5,...
  128. 'Normalization','probability');
  129. title('Choice SD: dist. T ALL-RT');
  130. xlabel('#Stim from T');ylabel('Proportion');
  131. set(gca, 'ylim',[0 1]);
  132. subplot(3,3,3);
  133. histogram(chdistSD(choice_identity(:,2),3),0.5:1:3.5,...
  134. 'Normalization','probability');
  135. title('Choice ND: dist. SD ALL-RT');
  136. xlabel('#Stim from T');ylabel('Proportion');
  137. set(gca, 'ylim',[0 1]);
  138. subplot(3,3,4);
  139. histogram(chdistT(choice_identity(:,2) & fastRT,3),0.5:1:3.5,...
  140. 'Normalization','probability');
  141. title('Choice ND: dist. T FAST-RT');
  142. xlabel('#Stim from T');ylabel('Proportion');
  143. set(gca, 'ylim',[0 1]);
  144. subplot(3,3,5);
  145. histogram(chdistT(choice_identity(:,3) & fastRT,3),0.5:1:3.5,...
  146. 'Normalization','probability');
  147. title('Choice SD: dist. T FAST-RT');
  148. xlabel('#Stim from T');ylabel('Proportion');
  149. set(gca, 'ylim',[0 1]);
  150. subplot(3,3,6);
  151. histogram(chdistSD(choice_identity(:,2) & fastRT,3),0.5:1:3.5,...
  152. 'Normalization','probability');
  153. title('Choice ND: dist. SD FAST-RT');
  154. xlabel('#Stim from T');ylabel('Proportion');
  155. set(gca, 'ylim',[0 1]);
  156. subplot(3,3,7);
  157. histogram(chdistT(choice_identity(:,2) & slowRT,3),0.5:1:3.5,...
  158. 'Normalization','probability');
  159. title('Choice ND: dist. T SLOW-RT');
  160. xlabel('#Stim from T');ylabel('Proportion');
  161. set(gca, 'ylim',[0 1]);
  162. subplot(3,3,8);
  163. histogram(chdistT(choice_identity(:,3) & slowRT,3),0.5:1:3.5,...
  164. 'Normalization','probability');
  165. title('Choice SD: dist. T SLOW-RT');
  166. xlabel('#Stim from T');ylabel('Proportion');
  167. set(gca, 'ylim',[0 1]);
  168. subplot(3,3,9);
  169. histogram(chdistSD(choice_identity(:,2) & slowRT,3),0.5:1:3.5,...
  170. 'Normalization','probability');
  171. title('Choice ND: dist. SD SLOW-RT');
  172. xlabel('#Stim from T');ylabel('Proportion');
  173. set(gca, 'ylim',[0 1]);
  174. suptitle(['Subj: ' monkey ', ERRORS']);
  175. end
  176. % stimulus_in_RF
  177. targetInRF = lut.TarPos==MP.rfpos(ch);
  178. nontargInRF = lut.TarPos~=MP.rfpos(ch) & lut.DistPos~=MP.rfpos(ch);
  179. distInRF = lut.DistPos==MP.rfpos(ch);
  180. stimulus_in_RF = [targetInRF, nontargInRF, distInRF];
  181. stim_rf_label = {'Target','NDist','SDist'};
  182. % choice_in_RF; to make it easier, let's define three
  183. rfchosen = lut.TarChoice==MP.rfpos(ch);
  184. choice_in_rf = [rfchosen ~rfchosen ones(length(rfchosen),1)];
  185. choice_rf_label = {'In','Out','Both'};
  186. % additional criteria that are not independent variables
  187. goodtrials = [cur_mp.sample_wise_goodtrials{:}]';
  188. inclRT = ones(size(goodtrials));
  189. if limit.ReactionTime~=0
  190. if limit.ReactionTime==-1
  191. pp = prctile(lut.reactTime2,limit.ReactimeTimePerc);
  192. inclRT = lut.reactTime2<pp;
  193. else
  194. pp = prctile(lut.reactTime2,100-limit.ReactimeTimePerc);
  195. inclRT = lut.reactTime2>pp;
  196. end
  197. end
  198. inclD_C2T = ones(size(goodtrials));
  199. if limit.DistChoiceToTarget
  200. inclD_C2T = (chdistT(:,3) >= limit.DistChoiceToTarget_range(1) & ...
  201. chdistT(:,3) <= limit.DistChoiceToTarget_range(2));
  202. end
  203. inclD_C2SD = ones(size(goodtrials));
  204. if limit.DistChoiceToSD
  205. inclD_C2SD = chdistSD(:,3) >= limit.DistChoiceToSD_range(1) & ...
  206. chdistSD(:,3) <= limit.DistChoiceToSD_range(2);
  207. end
  208. for choice_level = 1:3
  209. for stimulus_level = 1:3
  210. for choice_rf_level = 1:3
  211. incl = choice_identity(:,choice_level) & ...
  212. stimulus_in_RF(:,stimulus_level) & ...
  213. choice_in_rf(:,choice_rf_level) & ...
  214. goodtrials & inclRT & inclD_C2T & inclD_C2SD;
  215. mn = mean(env(incl,:),'omitnan');
  216. traces(c,:) = mn;
  217. tracesLUT.ch(c,1) = ch;
  218. tracesLUT.choice_id{c,1} = choice_label{choice_level};
  219. tracesLUT.stimulus_in_rf{c,1} = stim_rf_label{stimulus_level};
  220. tracesLUT.choice_in_rf{c,1} = choice_rf_label{choice_rf_level};
  221. tracesLUT.numtrls(c,1) = sum(incl);
  222. c = c + 1;
  223. end
  224. end
  225. end
  226. end
  227. tracesLUT = struct2table(tracesLUT);
  228. %% save output to make a combined plot of all channels
  229. savedir = fullfile(fld.basedir,'results','figure_neurobar');
  230. [~,~]=mkdir(savedir);
  231. if limit.ReactionTime==0
  232. limitRT = '_allRT';
  233. else
  234. limitRT = ['_limitRT_' num2str(limit.ReactionTime*limit.ReactimeTimePerc)];
  235. end
  236. if limit.DistChoiceToTarget
  237. C2T = ['_C2T-range_' num2str(limit.DistChoiceToTarget_range(1)) ...
  238. '-' num2str(limit.DistChoiceToTarget_range(2))];
  239. else
  240. C2T = '_allC2T';
  241. end
  242. if limit.DistChoiceToSD
  243. C2SD = ['_C2SD-range_' num2str(limit.DistChoiceToSD_range(1)) ...
  244. '-' num2str(limit.DistChoiceToSD_range(2))];
  245. else
  246. C2SD = '_allC2SD';
  247. end
  248. save(fullfile(savedir, [monkey '_averages_snr' num2str(snrthres) ...
  249. '_mindays' num2str(daythres) limitRT C2T C2SD '.mat']));