plot_blocks.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. function R=plot_blocks(Colors, R, events, trial_info, plot_sub_figures,bm_RD)
  2. %PLOT_PAPER_F3 Plots paper figure 3 for the blocks project. This is the
  3. %evolution through the trial figure
  4. xaxis=[-2 4];
  5. num_neurons = size(events{1,2},1);
  6. interspersed = [trial_info.blocks]' == 0;
  7. blocks1 = [trial_info.blocks]' == 1;
  8. blocks2 = [trial_info.blocks]' == 2;
  9. area_vectors = [strcmp({trial_info.area},'NA')',strcmp({trial_info.area},'VP')'];
  10. suc_delivery=strcmp('RD1', R.Erefnames);
  11. malt_delivery=strcmp('RD2', R.Erefnames);
  12. suc_cue=strcmp('CueR1', R.Erefnames);
  13. malt_cue=strcmp('CueR2', R.Erefnames);
  14. time_indicies=find(R.Param.Tm>=xaxis(1) & R.Param.Tm<=xaxis(end)); %returns the indicies where these are both true
  15. activity_xaxis=R.Param.Tm(time_indicies);
  16. masks=cat(2,bm_RD.mask_base',bm_RD.mask_curr',bm_RD.mask_mean');
  17. region = strcmp(R.Region,'VP');
  18. HistWindow=[0.75 1.95];
  19. color = {Colors('sucrose'),Colors('maltodextrin'),Colors('total'),Colors('blockrose'),Colors('blockrodextrin'),Colors('total')};
  20. figure;
  21. %% bar graphs
  22. stacked_data = zeros(2,3);
  23. for session_type=1:2
  24. for subset=1:3
  25. stacked_data(session_type,subset)=sum(region&masks(:,subset)&R.Blocks==(session_type-1));
  26. end
  27. end
  28. subplot(5,4,1)
  29. hold on;
  30. b = bar(stacked_data./sum(stacked_data,2),'stacked','edgecolor','none');
  31. b(3).FaceColor = 'Flat';
  32. b(3).CData = Colors('mean');
  33. b(2).FaceColor = 'Flat';
  34. b(2).CData = Colors('current');
  35. b(1).FaceColor = 'Flat';
  36. b(1).CData = Colors('rpe');
  37. axis([0 5 0 1])
  38. xticks([1 2]);
  39. xticklabels({'Interspersed','Blocked'});
  40. yticks([0,.2,.4,.6,.8,1])
  41. ylabel('Fraction of population')
  42. view(-90,90);
  43. legend('RPE','Current','Mean','location','northwest');
  44. %chisquared
  45. %all categories
  46. category=sum(masks.*[1:3],2);
  47. category=category(region);
  48. task=R.Blocks(region);
  49. [~,x2,pval]=crosstab(category,task);
  50. R.Stats.MLEX2(1).chi2=x2;
  51. R.Stats.MLEX2(1).pval=pval;
  52. %RPE cells
  53. category=masks(region,1);
  54. task=R.Blocks(region);
  55. [~,x2,pval]=crosstab(category,task);
  56. R.Stats.MLEX2(2).chi2=x2;
  57. R.Stats.MLEX2(2).pval=pval;
  58. %% activity plots
  59. ymax=11;
  60. for session_type=1:2
  61. if session_type==1 Sel=region & R.Blocks==0 & bm_RD.mask_base'; end
  62. if session_type==2 Sel=region & R.Blocks & bm_RD.mask_base'; end
  63. %plot sucrose preferring neurons to sucrose
  64. psth1=nanmean(R.Ev(suc_delivery).PSTHz(Sel,time_indicies),1);
  65. sem1=nanste(R.Ev(suc_delivery).PSTHz(Sel,time_indicies),1); %calculate standard error of the mean
  66. up1=psth1+sem1;
  67. down1=psth1-sem1;
  68. %plot sucrose preferring neurons to malt
  69. psth2=nanmean(R.Ev(malt_delivery).PSTHz(Sel,time_indicies),1);
  70. sem2=nanste(R.Ev(malt_delivery).PSTHz(Sel,time_indicies),1); %calculate standard error of the mean
  71. up2=psth2+sem2;
  72. down2=psth2-sem2;
  73. subplot(5,8,[5:8]-8*(session_type-2))
  74. hold on
  75. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  76. a=plot(activity_xaxis,psth1,'Color',color{1+3*(session_type-1)},'linewidth',1);
  77. b=plot(activity_xaxis,psth2,'Color',color{2+3*(session_type-1)},'linewidth',1);
  78. patch([activity_xaxis,activity_xaxis(end:-1:1)],[up1,down1(end:-1:1)],color{1+3*(session_type-1)},'EdgeColor','none');alpha(0.5);
  79. patch([activity_xaxis,activity_xaxis(end:-1:1)],[up2,down2(end:-1:1)],color{2+3*(session_type-1)},'EdgeColor','none');alpha(0.5);
  80. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  81. plot([0 0],[-2 ymax],'color','k','linewidth',0.5);
  82. plot([-0.5 -0.5],[-2 ymax],'color','b','linewidth',0.5);
  83. axis([-2 4 -1 ymax]);
  84. text(-1.75,ymax+0.5,['RPE (n=' num2str(sum(Sel)) ' of ' num2str(sum(region&R.Blocks==(session_type-1))) ')'],'FontSize',8)
  85. legend([a b],'Suc trials','Mal trials');
  86. text(-1.3,ymax-0.5,'PE','color','b');
  87. text(0.1,ymax-0.5,'RD','color','k');
  88. xlabel('Seconds from RD');
  89. set(gca,'ytick',[])
  90. %plot sucrose preferring neurons to sucrose cue
  91. psth1=nanmean(R.Ev(suc_cue).PSTHz(Sel,time_indicies),1);
  92. sem1=nanste(R.Ev(suc_cue).PSTHz(Sel,time_indicies),1); %calculate standard error of the mean
  93. up1=psth1+sem1;
  94. down1=psth1-sem1;
  95. %plot sucrose preferring neurons to malt cue
  96. psth2=nanmean(R.Ev(malt_cue).PSTHz(Sel,time_indicies),1);
  97. sem2=nanste(R.Ev(malt_cue).PSTHz(Sel,time_indicies),1); %calculate standard error of the mean
  98. up2=psth2+sem2;
  99. down2=psth2-sem2;
  100. subplot(5,8,[3:4]-8*(session_type-2))
  101. hold on
  102. plot(activity_xaxis,psth1,'Color',color{1+3*(session_type-1)},'linewidth',1);
  103. plot(activity_xaxis,psth2,'Color',color{2+3*(session_type-1)},'linewidth',1);
  104. patch([activity_xaxis,activity_xaxis(end:-1:1)],[up1,down1(end:-1:1)],color{1+3*(session_type-1)},'EdgeColor','none');alpha(0.5);
  105. patch([activity_xaxis,activity_xaxis(end:-1:1)],[up2,down2(end:-1:1)],color{2+3*(session_type-1)},'EdgeColor','none');alpha(0.5);
  106. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  107. axis([-1 2 -1 ymax]);
  108. ylabel('Mean firing (z-score)');
  109. xlabel('Seconds from cue onset')
  110. plot([0 0],[-1 ymax],'color','r','linewidth',0.5);
  111. text(-0.6,ymax-0.5,'Cue','color','r');
  112. title('Event-evoked firing');
  113. end
  114. disp('Event firing')
  115. %% firing across session
  116. session_types = {interspersed,blocks1,blocks2};
  117. event=3; %RD
  118. area=2; %VP
  119. SessType={'RPE','Current only','Mean'};
  120. % Visualize (equal axis)
  121. for subset=1:3
  122. for i = 1:3 % Each session type (interspersed, blocks1, blocks2)
  123. subplot(5,3,6+i+3*(subset-1));
  124. hold on
  125. alpha(0.15)
  126. data_suc = events{event,5}(session_types{i}&area_vectors(:,area)&masks(:,subset),:);
  127. error_suc = std(data_suc)/sqrt(size(data_suc,1));
  128. data_means = mean(data_suc);
  129. error_bars = error_suc;
  130. if i == 1
  131. segments = [1 3 5 7 9;2 4 6 8 10]; % Change to [1:6] to make all connected
  132. elseif i == 2
  133. segments = [1:5;6:10];
  134. else
  135. segments = [6:10;1:5];
  136. end
  137. segcolors={Colors('sucrose'),Colors('maltodextrin'),Colors('blockrose'),Colors('blockrodextrin'),Colors('blockrose'),Colors('blockrodextrin')};
  138. segnum=0;
  139. for segment = segments'
  140. segnum=segnum+1;
  141. if i==1
  142. l{segnum}=errorbar(1:5,data_means(1,segment),error_bars(1,segment),'Color',segcolors{2*(i-1)+segnum},'linewidth',1.5);
  143. axis([0 6 -1 11])
  144. else
  145. l{segnum}=errorbar(segment,data_means(1,segment),error_bars(1,segment),'Color',segcolors{2*(i-1)+segnum},'linewidth',1.5);
  146. axis([0 11 -1 11])
  147. end
  148. end
  149. % title([session_titles{i} ': ' events{event,1}])
  150. if i < 3
  151. ylabel('Mean firing (z-score)')
  152. legend([l{1} l{2}],'Suc trials','Mal trials','AutoUpdate','off');
  153. end
  154. plot([0 11],[0 0],':','color','k');
  155. set(gca,'xtick',[]);
  156. xlabel('Session progress');
  157. title({[SessType{subset} ' (n=' num2str(length(data_suc)) ' of ' num2str(sum(session_types{i}&area_vectors(:,area))) ')']}');
  158. end
  159. end
  160. disp('Evolution')
  161. if plot_sub_figures
  162. % Make the Linear Model
  163. linear_models = cell(num_neurons,size(events,1));
  164. linear_models_shuffled = cell(num_neurons,size(events,1));
  165. model_output = cell(3,1);
  166. model_output_shuffled = cell(3,1);
  167. for task = 1:3
  168. for class=1:3
  169. Sel=session_types{task} & masks(:,class) & region;
  170. SelIndex=find(Sel);
  171. data_table_all{class,task}=table;
  172. for event = 3
  173. for neuron = 1:length(SelIndex)
  174. firing_rate = events{event, 2}(SelIndex(neuron),:)';
  175. firing_rate = firing_rate(~isnan(firing_rate));
  176. if event == 3
  177. reward = trial_info(SelIndex(neuron)).current_reward;
  178. progress = (1:length(firing_rate))'./length(firing_rate);
  179. order = ones(length(reward),1)*trial_info(SelIndex(neuron)).blocks;
  180. neuronnum = categorical(ones(length(reward),1)*SelIndex(neuron));
  181. end
  182. data_table = table(progress,reward,order,neuronnum,firing_rate);
  183. data_table = data_table(~any(ismissing(data_table),2),:); % Removes any NaN values from Cues played at the beginning without any previous reward (should be very few, since I already removed the very first trial)
  184. data_table_all{class,task}=cat(1,data_table_all{class,task},data_table);
  185. end
  186. end
  187. R.sucrose_mdl{class,task} = fitlm(data_table_all{class,task}.progress(data_table_all{class,task}.reward==1),data_table_all{class,task}.firing_rate(data_table_all{class,task}.reward==1));
  188. R.maltodextrin_mdl{class,task} = fitlm(data_table_all{class,task}.progress(data_table_all{class,task}.reward==0),data_table_all{class,task}.firing_rate(data_table_all{class,task}.reward==0));
  189. R.lmdl{class,task}=fitlm(data_table_all{class,task},'firing_rate~progress+reward+progress:reward');
  190. end
  191. end
  192. R.lmdl_blocksRPE=fitlme(cat(1,data_table_all{1,2},data_table_all{1,3}),'firing_rate~progress+reward+progress:reward+progress:reward:order+(1|neuronnum)');
  193. R.lmdl_blocksCurrent=fitlme(cat(1,data_table_all{2,2},data_table_all{2,3}),'firing_rate~progress+reward+progress:reward+progress:reward:order+(1|neuronnum)');
  194. disp('Linear Models')
  195. end