figure_behavior.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. function RTs = figure_behavior(fld)
  2. fprintf('\n======================================================\n');
  3. fprintf('-- Creating Figure for behavior --\n');
  4. fprintf('======================================================\n');
  5. %% Get sessions ---------------------------------------------------------
  6. datainfo = session_info();
  7. datadir = fld.procdatadir;
  8. mkdir(fullfile(fld.basedir,'results'));
  9. %% Settings -------------------------------------------------------------
  10. green = [23, 105, 13]./255;
  11. red = [201, 0, 34]./255;
  12. cols = [green; 0.3 0.3 0.3; red];
  13. %% Plots ----------------------------------------------------------------
  14. % load traces
  15. monkeys = {'M1','M2'};
  16. f1 = figure;
  17. f2 = figure;
  18. f3 = figure;
  19. for mi = 1:2
  20. monkey = monkeys{mi};
  21. info = datainfo(mi);
  22. sessions = info.dates;
  23. clut = [];
  24. for sid = 1:length(sessions)
  25. session = sessions{sid};
  26. % session info
  27. q = strsplit(session,'_');
  28. cdate = q{2};
  29. block = q{3}(4);
  30. % load the data
  31. session_id = [monkey '_' cdate '_' num2str(block)];
  32. sessiondir = fullfile(datadir, monkey, session_id);
  33. % load the data for this session for this channel
  34. load(fullfile(sessiondir, [session_id '_LUT.mat'])); % loads 'lut'
  35. clut = [clut; lut];
  36. end
  37. lut = clut;
  38. disp(['for monkey ' monkey ' we recorded ' num2str(size(lut,1)) ...
  39. ' trials in ' num2str(length(sessions)) ' days']);
  40. % calculate concatenated means
  41. choices = {'Target','Nontarget','Distractor'};
  42. q = cellfun(@(x) sum(strcmp(lut.Status,x)),choices);
  43. response_ratio = q./sum(q);
  44. RT = cellfun(@(x) mean(lut.reactTime2(strcmp(lut.Status,x))),choices);
  45. for ic=1:3
  46. collectRT{mi,ic} = lut.reactTime2(strcmp(lut.Status,choices{ic}));
  47. end
  48. RTstd = cellfun(@(x) std(lut.reactTime2(strcmp(lut.Status,x))),choices);
  49. RTntrls = cellfun(@(x) sum(strcmp(lut.Status,x)),choices);
  50. RTsem = RTstd./sqrt(RTntrls);
  51. % calculate response ratio per session to provide SEM estimate
  52. incl_sessions = unique(lut.session);
  53. all_ratios = [];
  54. for sid = 1:length(incl_sessions)
  55. clut = lut(strcmp(lut.session,incl_sessions{sid}),:);
  56. q = cellfun(@(x) sum(strcmp(clut.Status,x)),choices);
  57. all_ratios(sid,:) = q./sum(q);
  58. end
  59. sem = std(all_ratios);%./sqrt(size(all_ratios,1));
  60. % NB! SEMs are so small, plot SD over sessions instead
  61. % collect choice proportions for stats
  62. choiceprop{mi} = all_ratios;
  63. % behavioral responses
  64. figure(f1);
  65. subplot(2,2,mi);
  66. mn = response_ratio;
  67. b = bar(mn); hold all
  68. b.FaceColor = 'flat';
  69. b.CData = cols;
  70. errorbar(mn,sem,'k.');
  71. ylim([0 1]);
  72. title(monkey);
  73. if mi==1
  74. ylabel('Ratio');
  75. end
  76. set(gca,'XTickLabel',{'T','ND','SD'});
  77. % same, corrected for different chance levels
  78. figure(f3);
  79. subplot(2,2,mi);
  80. mn = response_ratio;
  81. mn(2) = mn(2)/4;
  82. sem(2) = sem(2)/4;
  83. b = bar(mn); hold all
  84. b.FaceColor = 'flat';
  85. b.CData = cols;
  86. errorbar(mn,sem,'k.');
  87. ylim([0 1]);
  88. title([monkey ', corrected for chance level']);
  89. if mi==1
  90. ylabel('Ratio');
  91. end
  92. set(gca,'XTickLabel',{'T','ND','SD'});
  93. % reaction time
  94. figure(f1)
  95. subplot(2,2,mi+2);
  96. b2 = bar(RT); hold all
  97. b2.FaceColor = 'flat';
  98. b2.CData = cols;
  99. errorbar(RT,RTsem,'k.');
  100. ylim([190 290])
  101. if mi==1
  102. ylabel('Reaction time (ms)');
  103. end
  104. xlabel('Chosen target');
  105. set(gca,'XTickLabel',{'T','ND','SD'});
  106. fprintf(['Mean RTs for ' monkey ': T ' num2str(RT(1)) ...
  107. 'ms, ND ' num2str(RT(2)) , 'ms , SD ' num2str(RT(3)) 'ms\n'])
  108. RTs{mi}=RT;
  109. %% RT distribution
  110. figure(f2);
  111. subplot(2,2,mi);
  112. histogram(lut.reactTime2);
  113. xlabel('Reaction Time (ms)');
  114. xlim([0 500]);
  115. title(monkey);
  116. subplot(2,2,mi+2); hold off
  117. for c = 3:-1:1
  118. rts = lut.reactTime2(strcmp(lut.Status,choices{c}));
  119. rts(rts>500) = [];
  120. histogram(rts,70,'FaceColor',cols(c,:),'Normalization',...
  121. 'probability'); hold all
  122. end
  123. xlim([100 400]);
  124. %% RT distribution collect
  125. for c = 3:-1:1
  126. RTcoll{mi,c} = lut.reactTime2(strcmp(lut.Status,choices{c}));
  127. end
  128. end
  129. % save all RTs
  130. save(fullfile(fld.basedir, 'results','figure_behavior','RTcoll.mat'),'RTcoll');
  131. % Generate some plots
  132. RT_SlidingWindow;
  133. % RT_Distributions;
  134. % add the pooled average RT
  135. RTs{mi+1} = [mean([collectRT{1,1};collectRT{2,1}]) ...
  136. mean([collectRT{1,2};collectRT{2,2}]) ...
  137. mean([collectRT{1,3};collectRT{2,3}]) ];
  138. fprintf(['Mean pooled RTs : T ' num2str(RTs{3}(1)) ...
  139. 'ms, D ' num2str(RTs{3}(2)) , 'ms , SD ' num2str(RTs{3}(3)) 'ms\n'])
  140. % add the pooled choice proportions
  141. choiceprop{3} = [choiceprop{1}; choiceprop{2}];
  142. %% Stats ----------------------------------------------------------------
  143. fprintf ('===============================================\n');
  144. fprintf (' Statistics CHOICE PROPORTIONS\n');
  145. fprintf ('===============================================\n');
  146. for mi=1:2
  147. % choices
  148. cp = choiceprop{mi};
  149. % against chance
  150. [h,p,~,stats]=ttest(cp(:,1),1/6,'tail','right');
  151. fprintf([monkeys{mi} ' pTARG > chance: p = ' num2str(p) ...
  152. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  153. [h,p,~,stats]=ttest(cp(:,2),4/6,'tail','left');
  154. fprintf([monkeys{mi} ' pNDIST < chance: p = ' num2str(p) ...
  155. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  156. [h,p,~,stats]=ttest(cp(:,3),1/6,'tail','left');
  157. fprintf([monkeys{mi} ' pSDIST < chance: p = ' num2str(p) ...
  158. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  159. % pSD<pND
  160. [h,p,~,stats]=ttest(cp(:,3),cp(:,2),'tail','left');
  161. fprintf([monkeys{mi} ' pSD < pND: p = ' num2str(p) ...
  162. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  163. end
  164. % choices
  165. cp = choiceprop{3};
  166. % against chance
  167. [h,p,~,stats]=ttest(cp(:,1),1/6,'tail','right');
  168. fprintf(['POOLED pTARG > chance: p = ' num2str(p) ...
  169. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  170. [h,p,~,stats]=ttest(cp(:,2),4/6,'tail','left');
  171. fprintf(['POOLED pNDIST < chance: p = ' num2str(p) ...
  172. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  173. [h,p,~,stats]=ttest(cp(:,3),1/6,'tail','left');
  174. fprintf(['POOLED pSDIST < chance: p = ' num2str(p) ...
  175. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  176. % pSD<pND
  177. [h,p,~,stats]=ttest(cp(:,3),cp(:,2),'tail','left');
  178. fprintf(['POOLED pSD < pND: p = ' num2str(p) ...
  179. ', t = ' num2str(stats.tstat) ', df = ' num2str(stats.df) '\n']);
  180. fprintf ('===============================================\n');
  181. fprintf (' Statistics REACTION TIME\n');
  182. fprintf ('===============================================\n');
  183. for mi=1:2
  184. % choices
  185. fprintf(['Stats for ' monkeys{mi} '\n']);
  186. rt = [collectRT{mi,1};collectRT{mi,2};collectRT{mi,3}];
  187. s = categorical([ones(size(collectRT{mi,1}));...
  188. 2*ones(size(collectRT{mi,2})); ...
  189. 3*ones(size(collectRT{mi,3}))]);
  190. [p,tbl,stats] = kruskalwallis(rt,s);
  191. fprintf(['Kruskal Wallis -- H = ' num2str(tbl{2,5}(1)) ...
  192. ', df = ' num2str(tbl{2,3}(1)) ', p = ' num2str(p) '\n']);
  193. [c,m,h,gnames] = multcompare(stats);
  194. fprintf('Tukey HSD -----\n')
  195. fprintf('T=1, ND=2, SD=3 \n')
  196. for i=1:size(c,1)
  197. fprintf([gnames{c(i,1)} ' vs ' gnames{c(i,2)} ...
  198. ', p = ' num2str(c(i,6)) '\n'])
  199. end
  200. end
  201. %% Save figures ---------------------------------------------------------
  202. figure(f1)
  203. savefig(fullfile(fld.basedir,'results','figure_behavior',...
  204. 'figure_behavior.fig'));
  205. figure(f2)
  206. savefig(fullfile(fld.basedir, 'results', 'figure_behavior',...
  207. 'figure_rt_distribution.fig'));
  208. %% Save RTs -------------------------------------------------------------
  209. save(fullfile(fld.basedir, 'results','figure_behavior','RTs.mat'),'RTs');