figure_neurobar.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. function figure_neurobar(fld, limit)
  2. fprintf('\n======================================================\n');
  3. fprintf('-- Creating Figure neurobar --\n');
  4. fprintf('======================================================\n');
  5. %% settings
  6. snrthres = 2.5; mindays = 3;
  7. % do_cleantraces = true; % if true we throw out artefact traces
  8. % NOTE: there is an embedded function in the bottom that has some more
  9. % settings.
  10. if limit.ReactionTime==0
  11. limitRT = '_allRT';
  12. else
  13. limitRT = ['_limitRT_' num2str(limit.ReactionTime*limit.ReactimeTimePerc)];
  14. end
  15. if limit.DistChoiceToTarget
  16. C2T = ['_C2T-range_' num2str(limit.DistChoiceToTarget_range(1)) ...
  17. '-' num2str(limit.DistChoiceToTarget_range(2))];
  18. else
  19. C2T = '_allC2T';
  20. end
  21. if limit.DistChoiceToSD
  22. C2SD = ['_C2SD-range_' num2str(limit.DistChoiceToSD_range(1)) ...
  23. '-' num2str(limit.DistChoiceToSD_range(2))];
  24. else
  25. C2SD = '_allC2SD';
  26. end
  27. %% plots
  28. % load traces
  29. monkeys = {'M1','M2'};
  30. alltraces = [];
  31. alltracesLUT = [];
  32. figure; set(gcf,'Position',[ 100 100 1000 1200]);
  33. for mi = 1:2
  34. monkey = monkeys{mi};
  35. savedir = fullfile(fld.basedir,'results','figure_neurobar');
  36. [~,~]=mkdir(savedir);
  37. load(fullfile(savedir, [monkey '_averages_snr' num2str(snrthres) ...
  38. '_mindays' num2str(mindays) limitRT C2T C2SD '.mat']),...
  39. 'traces','tracesLUT','env_t');
  40. subj = monkey;
  41. bar_data = make_bar_plot(subj,traces,tracesLUT,env_t,3,mi);
  42. %% do some statistics
  43. % plot 1
  44. D = bar_data{1}; % from the first plot, correct trials
  45. [~,p,~,st] = ttest(D(:,1),D(:,2));
  46. fprintf('\n');
  47. disp('--- correct trials ---');
  48. disp([monkey ', target vs non-target, p=' num2str(p) ...
  49. ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  50. [~,p,~,st] = ttest(D(:,3),D(:,2));
  51. disp([monkey ', distractor vs non-target, p=' num2str(p) ...
  52. ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  53. disp(' ');
  54. disp('--- monkey chose non-target ---');
  55. D = bar_data{2};
  56. [~,p,~,st] = ttest(D(:,1),D(:,3));
  57. disp([monkey ', chosen non-target vs target, p=' num2str(p) ...
  58. ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  59. [~,p,~,st] = ttest(D(:,4),D(:,3));
  60. disp([monkey ', chosen non-target vs distractor, p=' num2str(p) ...
  61. ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  62. [~,p,~,st] = ttest(D(:,2),D(:,3));
  63. disp([monkey ', chosen non-target vs non-chosen non-target, p=' ...
  64. num2str(p) ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  65. disp(' ');
  66. [~,p,~,st] = ttest(D(:,1),D(:,4));
  67. disp([monkey ', target vs distractor, p=' num2str(p) ', t=' ...
  68. num2str(st.tstat) ', df=' num2str(st.df)]);
  69. [~,p,~,st] = ttest(D(:,1),D(:,2));
  70. disp([monkey ', target vs non-chosen non-target, p=' num2str(p) ...
  71. ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  72. [~,p,~,st] = ttest(D(:,4),D(:,2));
  73. disp([monkey ', distractor vs non-chosen non-target, p=' num2str(p) ...
  74. ', t=' num2str(st.tstat) ', df=' num2str(st.df)]);
  75. disp(' ');
  76. disp('--- monkey chose distractor ---');
  77. D = bar_data{3};
  78. [~,p,~,st] = ttest(D(:,1),D(:,2));
  79. disp([monkey ', target vs non-target, p=' num2str(p) ', t=' ...
  80. num2str(st.tstat) ', df=' num2str(st.df)]);
  81. [~,p,~,st] = ttest(D(:,3),D(:,2));
  82. disp([monkey ', distractor vs non-target, p=' num2str(p) ', t=' ...
  83. num2str(st.tstat) ', df=' num2str(st.df)]);
  84. [~,p,~,st] = ttest(D(:,1),D(:,3));
  85. disp([monkey ', target vs distractor, p=' num2str(p) ', t=' ...
  86. num2str(st.tstat) ', df=' num2str(st.df)]);
  87. %% concatenate traces for combined plot
  88. alltraces = [alltraces; traces]; %#ok<*AGROW>
  89. alltracesLUT = [alltracesLUT; tracesLUT];
  90. end
  91. subj = 'BOTH';
  92. make_bar_plot(subj,alltraces,alltracesLUT,env_t,3,3);
  93. st=suptitle(['SELECT: ' limitRT C2T C2SD ]);
  94. set(st,'interpreter','none');
  95. % save figure
  96. savefig(gcf,fullfile(savedir, ['figure_neurobar' limitRT C2T C2SD]));
  97. print(gcf,fullfile(savedir, ['figure_neurobar' limitRT C2T C2SD]),'-dpng');
  98. end
  99. function bar_data = make_bar_plot(subj,traces,tracesLUT,env_t,nrows,rownr)
  100. %% settings
  101. green = [23, 105, 13]./255;
  102. red = [201, 0, 34]./255;
  103. cols = [green; 0.3 0.3 0.3; red];
  104. smoothfact = 10;
  105. plot_type = 2; % 1 for default (bar), 2 for scatter (showing all channels)
  106. %% make include matrix
  107. tl = tracesLUT;
  108. choices = unique(tracesLUT.choice_id,'stable');
  109. rfstims = unique(tracesLUT.stimulus_in_rf,'stable');
  110. include = cell(0);
  111. % for first figure, vary the stimulus in the RF
  112. for i = 1:3
  113. include{1}(:,i) = strcmp(tl.choice_id,'Target') & ...
  114. strcmp(tl.stimulus_in_rf,rfstims{i}) & strcmp(tl.choice_in_rf,'Both');
  115. end
  116. % for second figure, we split the case when a nontarget is chosen
  117. include{2}(:,1) = strcmp(tl.choice_id,'NDist') & ...
  118. strcmp(tl.stimulus_in_rf,'Target') & strcmp(tl.choice_in_rf,'Both');
  119. include{2}(:,2) = strcmp(tl.choice_id,'NDist') & ...
  120. strcmp(tl.stimulus_in_rf,'NDist') & strcmp(tl.choice_in_rf,'Out');
  121. include{2}(:,3) = strcmp(tl.choice_id,'NDist') & ...
  122. strcmp(tl.stimulus_in_rf,'NDist') & strcmp(tl.choice_in_rf,'In');
  123. include{2}(:,4) = strcmp(tl.choice_id,'NDist') & ...
  124. strcmp(tl.stimulus_in_rf,'SDist') & strcmp(tl.choice_in_rf,'Both');
  125. for i = 1:3
  126. include{3}(:,i) = strcmp(tl.choice_id,'SDist') & ...
  127. strcmp(tl.stimulus_in_rf,rfstims{i}) & strcmp(tl.choice_in_rf,'Both');
  128. end
  129. %% plot figures in a loop
  130. use_colors{1} = cols;
  131. use_colors{2} = [green; 0.5 0.5 0.5; 0.1 0.1 0.1; red];
  132. use_colors{3} = cols;
  133. tick_labels{1} = {'T','NT','D'};
  134. tick_labels{2} = {'T','NT-','NT+','D'};
  135. tick_labels{3} = {'T','NT','D'};
  136. %figure; set(gcf,'Position',[ 92 621 1096 371]);
  137. bar_data = cell(0);
  138. for plt = 1:3
  139. % subplot(1,3,plt);
  140. subplot(nrows,3,(3*(rownr-1))+plt);
  141. % make window_means
  142. window_means = [];
  143. ntrls = [];
  144. for l = 1:size(include{plt},2) % loop l for level
  145. incl = include{plt}(:,l);
  146. ntrls(l) = sum(tl.numtrls(incl));
  147. % get average within window
  148. win_idx = find(env_t>0.15 & env_t<0.20);
  149. get_traces = traces(incl,:);
  150. s_traces = [];
  151. for t = 1:size(get_traces,1)
  152. s_traces(t,:) = smooth(get_traces(t,:),smoothfact);
  153. end
  154. window_means(:,l) = mean(s_traces(:,win_idx),2);
  155. end
  156. % store window_means in bar_data
  157. bar_data{plt} = window_means;
  158. % plot
  159. mn = mean(window_means);
  160. sem = std(window_means)./sqrt(size(window_means,1));
  161. % make a bar plot (DEFAULT)
  162. b = bar(mean(window_means)); hold all;
  163. b.FaceColor = 'flat';
  164. b.CData = use_colors{plt};
  165. errorbar(mn,sem,'k+');
  166. ylim([0.4 0.7]);
  167. if plot_type==2
  168. % to get info per channel, do a scatter or line plot
  169. b.FaceAlpha = 0.5;
  170. x = ones(size(window_means,1),1);
  171. x = x + (randi(10,length(x),1)-5)/30;
  172. cx = [];
  173. for l=1:size(window_means,2)
  174. cx(:,l) = x + 1*(l-1);
  175. end
  176. % find lines that are increasing, only in third plot
  177. if plt==3
  178. q = window_means;
  179. % find increasing lines, i.e. lines that are in line with
  180. % what we expect
  181. makebold = q(:,3)>q(:,1);
  182. plot(cx(makebold,:)',q(makebold,:)','Color',[1 .5 .5]); hold all;
  183. plot(cx(~makebold,:)',q(~makebold,:)','Color',[.8 .8 .8]);
  184. else
  185. % plot lines
  186. plot(cx',window_means','Color',[.8 .8 .8]); hold all;
  187. end
  188. for l=1:size(window_means,2)
  189. scatter(cx(:,l),window_means(:,l),20,use_colors{plt}(l,:),'f');
  190. end
  191. ylim([0 1]);
  192. end
  193. set(gca,'XTickLabel',tick_labels{plt});
  194. title([subj ': ' choices{plt}]);
  195. if plot_type==2
  196. txtheight = ones(size(include{plt},2),1)*0.1;
  197. else
  198. txtheight = mns+0.01;
  199. end
  200. t = text([1:size(include{plt},2)],txtheight,strsplit(num2str(ntrls),' '),...
  201. 'FontSize',8,'HorizontalAlignment','center');
  202. if plt==2
  203. xlabel('Stimulus in RF');
  204. end
  205. if plt==1
  206. ylabel('Response magnitude');
  207. end
  208. end
  209. end