plot_cued.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. function Stats=plot_cued(Colors, R, RewHist, bm_RD, DOI, ROI, os, bm_cue, RAW)
  2. %which neurons are plotted
  3. IncludedNeurons=ChooseNs(R,DOI,ROI);
  4. %subselect sessions of interest
  5. for i=1:length(RAW)
  6. name=char(RAW(i).Ninfo(1,1));
  7. sessrat(i,1)=str2num(name(2:3));
  8. sessday(i,1)=str2num(name(8:9));
  9. end
  10. %decide which sessions to include
  11. inc_sessions = (ismember(sessrat,ROI) & ismember(sessday,DOI));
  12. RAW=RAW(inc_sessions);
  13. xaxis=[-2 5];
  14. %event names
  15. RD1NP=strcmp('Cue3RD1', R.Erefnames);
  16. RD1PR=strcmp('Cue1RD', R.Erefnames);
  17. RD2NP=strcmp('Cue3RD2', R.Erefnames);
  18. RD2PR=strcmp('Cue2RD', R.Erefnames);
  19. Cue1=strcmp('Cue1', R.Erefnames);
  20. Cue2=strcmp('Cue2', R.Erefnames);
  21. Cue31=strcmp('Cue31', R.Erefnames);
  22. Cue32=strcmp('Cue32', R.Erefnames);
  23. %when the model data is only on subselected neurons, make the logicals in terms of all neurons
  24. NNumber=[1:length(R.Ninfo)]';
  25. index=NNumber(IncludedNeurons);
  26. histmasks=zeros(length(R.Ninfo),3);
  27. histmasks(index,1)=bm_RD.mask_base'|bm_RD.mask_base_cue';
  28. histmasks(index,2)=bm_RD.mask_curr'|bm_RD.mask_curr_cue';
  29. histmasks(index,3)=bm_RD.mask_mean'|bm_RD.mask_mean_cue';
  30. cuemasks=zeros(length(R.Ninfo),2);
  31. cuemasks(index,1)=bm_RD.mask_base_cue'|bm_RD.mask_curr_cue'|bm_RD.mask_mean_cue';
  32. cuemasks(index,2)=bm_RD.mask_base'|bm_RD.mask_curr'|bm_RD.mask_mean';
  33. %analysis windows
  34. HistWindow = [0.75 1.95];
  35. CueWindow = [0 0.75];
  36. figure;
  37. %% bar graph for history effects
  38. stacked_data = zeros(2,3);
  39. for subset=1:3
  40. stacked_data(1,subset)=sum(IncludedNeurons&histmasks(:,subset)&cuemasks(:,2));
  41. stacked_data(2,subset)=sum(IncludedNeurons&histmasks(:,subset)&cuemasks(:,1));
  42. end
  43. subplot(3,6,1)
  44. hold on;
  45. b = bar([1 2],stacked_data./sum(IncludedNeurons),'stacked','edgecolor','none');
  46. b(3).FaceColor = 'Flat';
  47. b(3).CData = Colors('mean');
  48. b(2).FaceColor = 'Flat';
  49. b(2).CData = Colors('current');
  50. b(1).FaceColor = 'Flat';
  51. b(1).CData = Colors('rpe');
  52. axis([0 7 0 1])
  53. legend('RPE','Current','Unmod.');
  54. set(gca,'xtick',[]);
  55. yticks([0,.2,.4,.6,.8,1])
  56. ylabel('Fraction of population')
  57. %chi2 for distribution of cue effects among history effects
  58. for i = 1:length(histmasks)
  59. histcategory(i,1)=sum(find(histmasks(i,:)));
  60. cuecategory(i,1)=sum(find(cuemasks(i,:)));
  61. end
  62. [table,Stats.HistCueDistX2,Stats.HistCueDistpval]=crosstab(histcategory(IncludedNeurons),cuecategory(IncludedNeurons));
  63. %% mean activity
  64. Sel=IncludedNeurons;
  65. ymax=5;
  66. alph=0.3;
  67. Xaxis=[-2 4];
  68. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  69. time1=R.Param.Tm(Ishow);
  70. %plot suc after non-specific cue
  71. psth1=nanmean(R.Ev(RD1NP).PSTHz(Sel,Ishow),1);
  72. sem1=nanste(R.Ev(RD1NP).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  73. up1=psth1+sem1;
  74. down1=psth1-sem1;
  75. %plot suc after sucrose cue
  76. psth2=nanmean(R.Ev(RD1PR).PSTHz(Sel,Ishow),1);
  77. sem2=nanste(R.Ev(RD1PR).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  78. up2=psth2+sem2;
  79. down2=psth2-sem2;
  80. %plot malt after non-specific cue
  81. psth3=nanmean(R.Ev(RD2NP).PSTHz(Sel,Ishow),1);
  82. sem3=nanste(R.Ev(RD2NP).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  83. up3=psth3+sem3;
  84. down3=psth3-sem3;
  85. %plot malt after maltodextrin cue
  86. psth4=nanmean(R.Ev(RD2PR).PSTHz(Sel,Ishow),1);
  87. sem4=nanste(R.Ev(RD2PR).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  88. up4=psth4+sem4;
  89. down4=psth4-sem4;
  90. %make the plot
  91. subplot(3,3,4);
  92. hold on;
  93. title(['All neurons (n=' num2str(sum(Sel)) ')']);
  94. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  95. a=plot(time1,psth1,'Color',Colors('sucrose'),'linewidth',1);
  96. b=plot(time1,psth2,'Color',Colors('blockrose'),'linewidth',1);
  97. c=plot(time1,psth3,'Color',Colors('maltodextrin'),'linewidth',1);
  98. d=plot(time1,psth4,'Color',Colors('blockrodextrin'),'linewidth',1);
  99. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrose'),'EdgeColor','none');alpha(alph);
  100. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],Colors('blockrose'),'EdgeColor','none');alpha(alph);
  101. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],Colors('maltodextrin'),'EdgeColor','none');alpha(alph);
  102. patch([time1,time1(end:-1:1)],[up4,down4(end:-1:1)],Colors('blockrodextrin'),'EdgeColor','none');alpha(alph);
  103. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  104. plot([0 0],[-3 ymax],'color','k','linewidth',0.5);
  105. text(0.1,ymax-0.5,'RD','color','k');
  106. axis([Xaxis(1) Xaxis(2) -0.5 ymax]);
  107. ylabel('Mean firing (z-score)');
  108. xlabel('Seconds from RD');
  109. legend([a b c d],'Non-pr suc','Pred suc','Non-pr mal','Pred mal','location','northeast');
  110. disp('Mean activity');
  111. %% reward history plots
  112. %broken down by previous trial
  113. ymax=10;
  114. alph=0.3;
  115. trialsback=10;
  116. linecolor{1}=Colors('rpe');
  117. linecolor{2}=Colors('current');
  118. linecolor{3}=Colors('mean');
  119. titles={'RPE neurons (n=';'Current outcome neurons (n=';'Mean neurons (n='};
  120. for subset=1:3
  121. Sel=IncludedNeurons&histmasks(:,subset)&cuemasks(:,2);
  122. %calculate model based on all reward selective neurons
  123. allRDHz=[];
  124. allpreds=[];
  125. selindex=find(Sel);
  126. for i=1:sum(Sel)
  127. allRDHz=cat(1,allRDHz,RewHist.RDHz{selindex(i),1});
  128. allpreds=cat(1,allpreds,RewHist.Predictors{selindex(i),1}(:,1:trialsback+1));
  129. end
  130. allVPmdl=fitlm(allpreds,allRDHz);
  131. Stats.trials.subsets{subset,1}=allVPmdl.Coefficients.pValue(2:12);
  132. %plot coefficients
  133. subplot(3,6,2);
  134. hold on;
  135. errorbar(0:trialsback,allVPmdl.Coefficients.Estimate(2:trialsback+2),allVPmdl.Coefficients.SE(2:trialsback+2),'color',linecolor{subset},'linewidth',1.5);
  136. end
  137. axis([-1 trialsback+1 -1.5 4]);
  138. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  139. xlabel('Trials back');
  140. ylabel('Coefficient weight');
  141. title('Trial history regression, subsets');
  142. legend('RPE','Current','Mean');
  143. disp('Rew History Responses')
  144. %% scatterplot of cue values for effects on RD
  145. subplot(3,6,5);
  146. sz=25;
  147. hold on;
  148. sucvalues=nan(sum(IncludedNeurons),1);
  149. malvalues=nan(sum(IncludedNeurons),1);
  150. %mean
  151. Sel=bm_RD.mask_mean_cue;
  152. index=find(Sel);
  153. vsuc = getParameters_ott(os, 'RD', 'mean_cue', 'vsuc');
  154. vmal = getParameters_ott(os, 'RD', 'mean_cue', 'vmal');
  155. scatter(-vsuc(Sel),-vmal(Sel),sz,Colors('mean'));
  156. sucvalues(index)=-vsuc(Sel)';
  157. malvalues(index)=-vmal(Sel)';
  158. %current
  159. Sel=bm_RD.mask_curr_cue;
  160. index=find(Sel);
  161. vsuc = getParameters_ott(os, 'RD', 'curr_cue', 'vsuc');
  162. vmal = getParameters_ott(os, 'RD', 'curr_cue', 'vmal');
  163. scatter(-vsuc(Sel),-vmal(Sel),sz,Colors('current'));
  164. sucvalues(index)=-vsuc(Sel)';
  165. malvalues(index)=-vmal(Sel)';
  166. %base
  167. Sel=bm_RD.mask_base_cue;
  168. index=find(Sel);
  169. vsuc = getParameters_ott(os, 'RD', 'base_cue', 'vsuc');
  170. vmal = getParameters_ott(os, 'RD', 'base_cue', 'vmal');
  171. scatter(-vsuc(Sel),-vmal(Sel),sz,Colors('rpe'));
  172. sucvalues(index)=-vsuc(Sel)';
  173. malvalues(index)=-vmal(Sel)';
  174. axis([-1 1 -1 1]);
  175. xlabel('Sucrose value');
  176. ylabel('Maltodextrin value');
  177. plot([0 0],[-1 1],'color','k');
  178. plot([-1 1],[0 0],'color','k');
  179. quadrant=[];
  180. for i=1:length(sucvalues)
  181. if sucvalues(i)<0 & malvalues(i)>0
  182. quadrant(i)=1;
  183. elseif sucvalues(i)>0 & malvalues(i)>0
  184. quadrant(i)=2;
  185. elseif sucvalues(i)<0 & malvalues(i)<0
  186. quadrant(i)=3;
  187. elseif sucvalues(i)>0 & malvalues(i)<0
  188. quadrant(i)=4;
  189. end
  190. end
  191. quadrant=quadrant(find(quadrant));
  192. text(-0.9,0.8,[num2str(round(100*sum(quadrant==1)/length(quadrant),1)) '%']);
  193. text(0.6,0.8,[num2str(round(100*sum(quadrant==2)/length(quadrant),1)) '%']);
  194. text(-0.9,-0.8,[num2str(round(100*sum(quadrant==3)/length(quadrant),1)) '%']);
  195. text(0.6,-0.8,[num2str(round(100*sum(quadrant==4)/length(quadrant),1)) '%']);
  196. [h,Stats.RDpval,Stats.RDX2] = chi2gof(quadrant,'Ctrs',[1 2 3 4],'expected',[length(quadrant)/4 length(quadrant)/4 length(quadrant)/4 length(quadrant)/4]);
  197. if h
  198. text(0.5,-0.8,'*');
  199. end
  200. %% effect of cue plots
  201. AllSel=zeros(length(IncludedNeurons),1);
  202. index=find(IncludedNeurons);
  203. Sel=sucvalues>0 & malvalues<0;
  204. AllSel(index)=Sel;
  205. Sel=logical(AllSel);
  206. ymax=7;
  207. alph=0.3;
  208. trialsback=10;
  209. Xaxis=[-0.5 2.5];
  210. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  211. time1=R.Param.Tm(Ishow);
  212. %plot sucrose after non-specific cue
  213. psth1=nanmean(R.Ev(RD1NP).PSTHz(Sel,Ishow),1);
  214. sem1=nanste(R.Ev(RD1NP).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  215. up1=psth1+sem1;
  216. down1=psth1-sem1;
  217. %plot sucrose after sucrose cue
  218. psth2=nanmean(R.Ev(RD1PR).PSTHz(Sel,Ishow),1);
  219. sem2=nanste(R.Ev(RD1PR).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  220. up2=psth2+sem2;
  221. down2=psth2-sem2;
  222. %plot malt after non-specific cue
  223. psth3=nanmean(R.Ev(RD2NP).PSTHz(Sel,Ishow),1);
  224. sem3=nanste(R.Ev(RD2NP).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  225. up3=psth3+sem3;
  226. down3=psth3-sem3;
  227. %plot malt after maltodextrin cue
  228. psth4=nanmean(R.Ev(RD2PR).PSTHz(Sel,Ishow),1);
  229. sem4=nanste(R.Ev(RD2PR).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  230. up4=psth4+sem4;
  231. down4=psth4-sem4;
  232. %make the plot
  233. subplot(3,6,6);
  234. hold on;
  235. title(['Suc>0, Mal<0 (n=' num2str(sum(Sel)) ')']);
  236. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  237. a=plot(time1,psth1,'Color',Colors('sucrose'),'linewidth',1);
  238. b=plot(time1,psth2,'Color',Colors('blockrose'),'linewidth',1);
  239. c=plot(time1,psth3,'Color',Colors('maltodextrin'),'linewidth',1);
  240. d=plot(time1,psth4,'Color',Colors('blockrodextrin'),'linewidth',1);
  241. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrose'),'EdgeColor','none');alpha(alph);
  242. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],Colors('blockrose'),'EdgeColor','none');alpha(alph);
  243. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],Colors('maltodextrin'),'EdgeColor','none');alpha(alph);
  244. patch([time1,time1(end:-1:1)],[up4,down4(end:-1:1)],Colors('blockrodextrin'),'EdgeColor','none');alpha(alph);
  245. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  246. plot([0 0],[-3 ymax],'color','k','linewidth',0.5);
  247. text(0.1,ymax-0.5,'RD','color','k');
  248. axis([Xaxis(1) Xaxis(2) -2 ymax]);
  249. ylabel('Mean firing (z-score)');
  250. xlabel('Seconds from RD');
  251. legend([a b c d],'Non-pr suc','Pred suc','Non-pr mal','Pred mal','location','northeast');
  252. disp('RD cue impact');
  253. %% bar graph for cue effects on cue
  254. %when the model data is only on subselected neurons
  255. NNumber=[1:length(R.Ninfo)]';
  256. index=NNumber(IncludedNeurons);
  257. cuemasksc=zeros(length(R.Ninfo),2);
  258. cuemasksc(index,1)=bm_cue.mask_base_cue'|bm_cue.mask_mean_cue';
  259. cuemasksc(index,2)=bm_cue.mask_base'|bm_cue.mask_mean';
  260. stacked_data = zeros(2,2);
  261. for subset=1:2
  262. stacked_data(1,subset)=sum(IncludedNeurons&cuemasksc(:,subset));
  263. end
  264. subplot(3,6,10)
  265. hold on;
  266. b = bar(stacked_data./sum(stacked_data,2),'stacked','edgecolor','none');
  267. b(2).FaceColor = 'Flat';
  268. b(2).CData = [0 0 0];
  269. b(1).FaceColor = 'Flat';
  270. b(1).CData = Colors('cueeffect');
  271. axis([0 7 0 1])
  272. text(1.5,0.7,'No cue effect','color',[0 0 0]);
  273. %text(1.5,0.15,'Info','color',[0.3 0 0.8]);
  274. text(1.5,0.2,'Cue effect','color',Colors('cueeffect'));
  275. set(gca,'xtick',[]);
  276. yticks([0,.2,.4,.6,.8,1])
  277. ylabel('Fraction of population')
  278. %% scatterplot of cue responses
  279. subplot(3,6,11);
  280. sz=25;
  281. hold on;
  282. sucvalues=nan(sum(IncludedNeurons),1);
  283. malvalues=nan(sum(IncludedNeurons),1);
  284. %mean
  285. Sel=bm_cue.mask_mean_cue;
  286. index=find(Sel);
  287. vsuc = getParameters_ott(os, 'cue', 'mean_cue', 'vsuc');
  288. vmal = getParameters_ott(os, 'cue', 'mean_cue', 'vmal');
  289. scatter(vsuc(Sel),vmal(Sel),sz,Colors('cueeffect'));
  290. sucvalues(index)=vsuc(Sel)';
  291. malvalues(index)=vmal(Sel)';
  292. %base
  293. Sel=bm_cue.mask_base_cue;
  294. index=find(Sel);
  295. vsuc = getParameters_ott(os, 'cue', 'base_cue', 'vsuc');
  296. vmal = getParameters_ott(os, 'cue', 'base_cue', 'vmal');
  297. scatter(vsuc(Sel),vmal(Sel),sz,Colors('cueeffect'));
  298. sucvalues(index)=vsuc(Sel)';
  299. malvalues(index)=vmal(Sel)';
  300. axis([-1 1 -1 1]);
  301. xlabel('Sucrose value');
  302. ylabel('Maltodextrin value');
  303. plot([0 0],[-1 1],'color','k');
  304. plot([-1 1],[0 0],'color','k');
  305. quadrant=[];
  306. for i=1:length(sucvalues)
  307. if sucvalues(i)<0 & malvalues(i)>0
  308. quadrant(i)=1;
  309. elseif sucvalues(i)>0 & malvalues(i)>0
  310. quadrant(i)=2;
  311. elseif sucvalues(i)<0 & malvalues(i)<0
  312. quadrant(i)=3;
  313. elseif sucvalues(i)>0 & malvalues(i)<0
  314. quadrant(i)=4;
  315. end
  316. end
  317. quadrant=quadrant(find(quadrant));
  318. text(-0.9,0.8,[num2str(round(100*sum(quadrant==1)/length(quadrant),1)) '%']);
  319. text(0.6,0.8,[num2str(round(100*sum(quadrant==2)/length(quadrant),1)) '%']);
  320. text(-0.9,-0.8,[num2str(round(100*sum(quadrant==3)/length(quadrant),1)) '%']);
  321. text(0.6,-0.8,[num2str(round(100*sum(quadrant==4)/length(quadrant),1)) '%']);
  322. [h,Stats.Cuepval,Stats.CueX2] = chi2gof(quadrant,'Ctrs',[1 2 3 4],'expected',[length(quadrant)/4 length(quadrant)/4 length(quadrant)/4 length(quadrant)/4]);
  323. if h
  324. text(0.5,-0.8,'*');
  325. end
  326. %% effect of cue plots
  327. AllSel=zeros(length(IncludedNeurons),1);
  328. index=find(IncludedNeurons);
  329. Sel=sucvalues>0 & malvalues<0;
  330. AllSel(index)=Sel;
  331. Sel=logical(AllSel);
  332. ymax=8;
  333. alph=0.3;
  334. trialsback=10;
  335. Xaxis=[-0.5 1.5];
  336. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  337. time1=R.Param.Tm(Ishow);
  338. %plot non-specific cue, sucrose trials
  339. psth1=nanmean(R.Ev(Cue31).PSTHz(Sel,Ishow),1);
  340. sem1=nanste(R.Ev(Cue31).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  341. up1=psth1+sem1;
  342. down1=psth1-sem1;
  343. %plot sucrose cue
  344. psth2=nanmean(R.Ev(Cue1).PSTHz(Sel,Ishow),1);
  345. sem2=nanste(R.Ev(Cue1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  346. up2=psth2+sem2;
  347. down2=psth2-sem2;
  348. %plot non-specific cue, maltodextrin trials
  349. psth3=nanmean(R.Ev(Cue32).PSTHz(Sel,Ishow),1);
  350. sem3=nanste(R.Ev(Cue32).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  351. up3=psth3+sem3;
  352. down3=psth3-sem3;
  353. %plot maltodextrin cue
  354. psth4=nanmean(R.Ev(Cue2).PSTHz(Sel,Ishow),1);
  355. sem4=nanste(R.Ev(Cue2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  356. up4=psth4+sem4;
  357. down4=psth4-sem4;
  358. %make the plot
  359. subplot(3,6,12);
  360. hold on;
  361. title(['Suc>0, Mal<0 (n=' num2str(sum(Sel)) ')']);
  362. patch([CueWindow(1) CueWindow(1) CueWindow(2) CueWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  363. a=plot(time1,psth1,'Color',Colors('sucrose'),'linewidth',1);
  364. b=plot(time1,psth2,'Color',Colors('blockrose'),'linewidth',1);
  365. c=plot(time1,psth3,'Color',Colors('maltodextrin'),'linewidth',1);
  366. d=plot(time1,psth4,'Color',Colors('blockrodextrin'),'linewidth',1);
  367. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrose'),'EdgeColor','none');alpha(alph);
  368. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],Colors('blockrose'),'EdgeColor','none');alpha(alph);
  369. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],Colors('maltodextrin'),'EdgeColor','none');alpha(alph);
  370. patch([time1,time1(end:-1:1)],[up4,down4(end:-1:1)],Colors('blockrodextrin'),'EdgeColor','none');alpha(alph);
  371. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  372. plot([0 0],[-3 ymax],'color','r','linewidth',0.5);
  373. text(Xaxis(1)+0.1,ymax-0.5,'Cue','color','r');
  374. axis([Xaxis(1) Xaxis(2) -2 ymax]);
  375. ylabel('Mean firing (z-score)');
  376. xlabel('Seconds from cue');
  377. legend([a b c d],'Non-pr suc','Pred suc','Non-pr mal','Pred mal','location','northeast');
  378. disp('Cue cue impact')
  379. %% RPE neurons based on different RPE values
  380. bins=[-3 -1.2;-1.2 -0.7;-0.7 -0.4;-0.4 0;0 0.4;0.4 0.7;0.7 1.2;1.2 3];
  381. Sel=bm_RD.mask_base';
  382. mintrials=1;
  383. global Dura Tm BSIZE
  384. BSIZE=0.01; %Do not change
  385. Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2); %should match whatever was used to create R for simplicity
  386. Baseline=[-11 -1]; %for PSTHs, should match whatever is in R
  387. %smoothing filter for whole PSTH
  388. PSTHsmoothbins=50; %number of previous bins used to smooth
  389. halfnormal=makedist('HalfNormal','mu',0,'sigma',20); %std=3.98
  390. PSTHfilterweights=pdf(halfnormal,0:PSTHsmoothbins);
  391. %smoothing filter for individual trials
  392. trialsmoothbins=25; %number of previous bins used to smooth
  393. halfnormal=makedist('HalfNormal','mu',0,'sigma',10); %std=3.98
  394. trialfilterweights=pdf(halfnormal,0:trialsmoothbins);
  395. sessions=unique(R.Ninfo(:,1));
  396. index=find(Sel);
  397. RPEs=[];
  398. for i=1:sum(Sel)
  399. %collect all RPE values for all cells of interest
  400. RPEs=cat(1,RPEs,os(index(i)).mod_RD.base.RPEs);
  401. neuroninfo = R.Ninfo(IncludedNeurons,:);
  402. session=neuroninfo(index(i),1); %what's the name of session
  403. sessnum=ismember(sessions,session); %which session of all sessions
  404. neuronnum=ismember(R.Ninfo(strcmp(session,R.Ninfo(:,1)),2),neuroninfo(index(i),2)); %which number neuron in the session
  405. RD=strmatch('RD',RAW(sessnum).Einfo(:,2),'exact'); %get index for reward delivery times
  406. Cue=strmatch('Cues',RAW(sessnum).Einfo(:,2),'exact'); %get index for reward delivery times
  407. %get mean baseline firing for all reward trials
  408. [Bcell1,B1n]=MakePSR04(RAW(sessnum).Nrast(neuronnum),RAW(sessnum).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
  409. basespk=[];
  410. for y= 1:B1n
  411. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  412. end
  413. FiringRate=basespk/(Baseline(1,2)-Baseline(1,1));
  414. Bmean=nanmean(FiringRate);
  415. Bstd=nanstd(FiringRate);
  416. %make PSTHs around reward delivery
  417. trialtimes=RAW(sessnum).Erast{RD,1}(:,1); %reward delivery times
  418. for j=1:length(bins)
  419. RPEs=os(index(i)).mod_RD.base.RPEs;
  420. RPEsZ = (RPEs - mean(RPEs))/std(RPEs);
  421. toi=trialtimes(RPEsZ>=bins(j,1) & RPEsZ<=bins(j,2)); %which trials, binned by model-produced RPE
  422. if length(toi)>=mintrials
  423. %make PSTHs
  424. [PSR1,~]=MakePSR04(RAW(sessnum).Nrast(neuronnum),toi,Dura,{2});% makes collpased rasters. PSR1 is a cell(neurons)
  425. smoothedtrials=[];
  426. for trial=1:length(PSR1)
  427. binned=histcounts(PSR1{trial},[Tm Tm(end)+(Tm(end)-Tm(end-1))]);
  428. for l=1:length(Tm)
  429. smoothedtrials(trial,l)=sum(binned(1,l-min([l-1 trialsmoothbins]):l).*fliplr(trialfilterweights(1:min([l trialsmoothbins+1]))))/sum(trialfilterweights(1:min([l trialsmoothbins+1])));
  430. end
  431. end
  432. PTH1=mean(smoothedtrials,1)/BSIZE;
  433. PTH1smooth=[];
  434. for l=1:length(Tm)
  435. PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 PSTHsmoothbins]):l).*fliplr(PSTHfilterweights(1:min([l PSTHsmoothbins+1]))))/sum(PSTHfilterweights(1:min([l PSTHsmoothbins+1])));
  436. end
  437. RPEPSTHs{j,1}.PSTHraw(i,:)=PTH1smooth;
  438. %normalize already smoothed activity
  439. for l=1:length(PTH1smooth)
  440. RPEPSTHs{j,1}.PSTHz(i,l)=(PTH1smooth(l)-Bmean)/Bstd;
  441. end
  442. else
  443. RPEPSTHs{j,1}.PSTHraw(i,1:length(Tm))=NaN;
  444. RPEPSTHs{j,1}.PSTHz(i,1:length(Tm))=NaN;
  445. end
  446. end
  447. end
  448. %plotting the histograms
  449. ymax=8;
  450. Xaxis=[-1.5 4];
  451. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  452. time1=R.Param.Tm(Ishow);
  453. alph=0.2;
  454. psthcolor=[0 0.9 1;...
  455. 0 0.7 0.75;...
  456. 0 0.5 0.5;...
  457. 0 0.3 0.25;...
  458. 0.25 0.3 0;...
  459. 0.5 0.5 0;...
  460. 0.75 0.7 0;...
  461. 1 0.9 0];
  462. subplot(3,3,7);
  463. hold on;
  464. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-4 ymax ymax -4],Colors('gray'),'edgecolor','none');alpha(alph);
  465. a=[];
  466. for i=1:length(RPEPSTHs) %for each bin
  467. %plot activity for each bin
  468. psth1=nanmean(RPEPSTHs{i,1}.PSTHz(:,Ishow),1);
  469. sem1=nanste(RPEPSTHs{i,1}.PSTHz(:,Ishow),1); %calculate standard error of the mean
  470. up1=psth1+sem1;
  471. down1=psth1-sem1;
  472. %make the plot
  473. a{i}=plot(time1,psth1,'Color',psthcolor(i,:),'linewidth',1.5);
  474. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],psthcolor(i,:),'EdgeColor','none');alpha(alph);
  475. labels{i}=[num2str(bins(i,1)) ' < RPE < ' num2str(bins(i,2))];
  476. end
  477. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  478. plot([0 0],[-5 ymax],'color','k','linewidth',0.5);
  479. axis([Xaxis(1) Xaxis(2) -1 ymax]);
  480. ylabel('Mean firing (z-score)');
  481. xlabel('Seconds from RD');
  482. colormap(psthcolor);
  483. h=colorbar;
  484. set(h,'YTick',[0:0.125:1]);
  485. set(h,'yticklabel',[-3;-1.2;-.7;-.4;0;0.4;0.7;1.2;3])
  486. set(get(h,'title'),'string','RPE');
  487. %legend([a{:}],labels,'location','northeast');
  488. disp('RPE responses')