plot_int.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. function stats=plot_int(Colors, R, RewHist, bm_RD, RAW, os, bm_cue, plot_value, bm_RD_BIC)
  2. color = {Colors('sucrose'),Colors('maltodextrin'),Colors('total'),Colors('blockrose'),Colors('blockrodextrin'),Colors('total')};
  3. xaxis=[-2 5];
  4. time_indicies=find(R.Param.Tm>=xaxis(1) & R.Param.Tm<=xaxis(end)); %returns the indicies where these are both true
  5. activity_xaxis=R.Param.Tm(time_indicies);
  6. suc_delivery=strcmp('RD1', R.Erefnames);
  7. malt_delivery=strcmp('RD2', R.Erefnames);
  8. suc_cue=strcmp('CueP1', R.Erefnames);
  9. mal_cue=strcmp('CueP2', R.Erefnames);
  10. RD1P1=strcmp('RD1P1', R.Erefnames);
  11. RD1P2=strcmp('RD1P2', R.Erefnames);
  12. RD2P1=strcmp('RD2P1', R.Erefnames);
  13. RD2P2=strcmp('RD2P2', R.Erefnames);
  14. %masks=cat(2,(bm_RD.mask_base' & bm_RD_BIC.mask_base'==0),bm_RD.mask_curr',bm_RD.mask_mean');
  15. masks=cat(2,bm_RD.mask_base',bm_RD.mask_curr',bm_RD.mask_mean');
  16. %masks=cat(2,bm_RD_BIC.mask_base',bm_RD_BIC.mask_curr',bm_RD_BIC.mask_mean');
  17. region = strcmp(R.Region,'VP');
  18. task = R.Blocks==0;
  19. HistWindow=RewHist.RDWindow;
  20. CueWindow=RewHist.CueWindow;
  21. figure;
  22. %% activity plots
  23. ymax=4;
  24. alph=0.3;
  25. Sel=region&task;
  26. %plot sucrose preferring neurons to sucrose
  27. psth1=nanmean(R.Ev(suc_delivery).PSTHz(Sel,time_indicies),1);
  28. sem1=nanste(R.Ev(suc_delivery).PSTHz(Sel,time_indicies),1); %calculate standard error of the mean
  29. up1=psth1+sem1;
  30. down1=psth1-sem1;
  31. %plot sucrose preferring neurons to malt
  32. psth2=nanmean(R.Ev(malt_delivery).PSTHz(Sel,time_indicies),1);
  33. sem2=nanste(R.Ev(malt_delivery).PSTHz(Sel,time_indicies),1); %calculate standard error of the mean
  34. up2=psth2+sem2;
  35. down2=psth2-sem2;
  36. subplot(3,2,1)
  37. hold on
  38. plot(activity_xaxis,psth1,'Color',Colors('sucrose'),'linewidth',1);
  39. plot(activity_xaxis,psth2,'Color',Colors('maltodextrin'),'linewidth',1);
  40. patch([activity_xaxis,activity_xaxis(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrose'),'EdgeColor','none');alpha(0.5);
  41. patch([activity_xaxis,activity_xaxis(end:-1:1)],[up2,down2(end:-1:1)],Colors('maltodextrin'),'EdgeColor','none');alpha(0.5);
  42. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  43. plot([0 0],[-2 ymax],'color','k','linewidth',0.5);
  44. plot([-0.5 -0.5],[-2 ymax],'color','b','linewidth',0.5);
  45. axis([-2 4 -1 ymax]);
  46. title(['Reward response, all neurons (n=' num2str(sum(Sel)) ')']);
  47. legend('Suc trials','Mal trials');
  48. text(-1.3,ymax-0.5,'PE','color','b');
  49. xlabel('Seconds from reward delivery');
  50. %broken down by previous trial
  51. ymax=5;
  52. Xaxis=[0 2.5];
  53. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  54. time1=R.Param.Tm(Ishow);
  55. %plot suc after suc
  56. psth1=nanmean(R.Ev(RD1P1).PSTHz(Sel,Ishow),1);
  57. sem1=nanste(R.Ev(RD1P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  58. up1=psth1+sem1;
  59. down1=psth1-sem1;
  60. %plot suc after malt
  61. psth2=nanmean(R.Ev(RD1P2).PSTHz(Sel,Ishow),1);
  62. sem2=nanste(R.Ev(RD1P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  63. up2=psth2+sem2;
  64. down2=psth2-sem2;
  65. %plot malt after suc
  66. psth3=nanmean(R.Ev(RD2P1).PSTHz(Sel,Ishow),1);
  67. sem3=nanste(R.Ev(RD2P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  68. up3=psth3+sem3;
  69. down3=psth3-sem3;
  70. %plot malt after malt
  71. psth4=nanmean(R.Ev(RD2P2).PSTHz(Sel,Ishow),1);
  72. sem4=nanste(R.Ev(RD2P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  73. up4=psth4+sem4;
  74. down4=psth4-sem4;
  75. %make the plot
  76. subplot(3,3,3);
  77. hold on;
  78. title(['Current/previous reward'])
  79. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-5 10 10 -5],Colors('gray'),'EdgeColor','none');
  80. a=plot(time1,psth2,'Color',Colors('sucrosemnp'),'linewidth',1);
  81. b= plot(time1,psth1,'Color',Colors('sucrosesnp'),'linewidth',1);
  82. c=plot(time1,psth4,'Color',Colors('maltodextrinmnp'),'linewidth',1);
  83. d=plot(time1,psth3,'Color',Colors('maltodextrinsnp'),'linewidth',1);
  84. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],Colors('sucrosemnp'),'EdgeColor','none');alpha(alph);
  85. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrosesnp'),'EdgeColor','none');alpha(alph);
  86. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],Colors('maltodextrinsnp'),'EdgeColor','none');alpha(alph);
  87. patch([time1,time1(end:-1:1)],[up4,down4(end:-1:1)],Colors('maltodextrinmnp'),'EdgeColor','none');alpha(alph);
  88. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  89. %plot([0 0],[-2 ymax],'color','k','linewidth',0.5);
  90. % plot([HistWindow(1) HistWindow(1)],[-5 10],'--','color','k','linewidth',0.85);
  91. % plot([HistWindow(2) HistWindow(2)],[-5 10],'--','color','k','linewidth',0.85);
  92. %text(0.1,ymax-0.5,'RD','color','k');
  93. axis([Xaxis(1) Xaxis(2) -1 ymax]);
  94. ylabel('Mean firing (z-score)');
  95. xlabel('Seconds from RD');
  96. legend([a b c d],'Suc after mal','Suc after suc','Mal after mal','Mal after suc','location','northeast');
  97. disp('Interspersed event responses')
  98. %% fixed window linear models
  99. trialsback=10;
  100. %all neurons
  101. Sel=region>=0; %allneurons
  102. Sel=Sel(region&task); %only the neurons from VP interspersed
  103. %calculate model based on all reward selective neurons
  104. allRDHz=[];
  105. allpreds=[];
  106. selindex=find(Sel);
  107. for i=1:sum(Sel)
  108. allRDHz=cat(1,allRDHz,RewHist.RDHz{selindex(i),1});
  109. allpreds=cat(1,allpreds,RewHist.Predictors{selindex(i),1}(:,1:trialsback+1));
  110. end
  111. allVPmdl=fitlm(allpreds,allRDHz);
  112. stats.trials.all=allVPmdl.Coefficients.pValue(2:12);
  113. %plot coefficients
  114. subplot(3,3,4);
  115. hold on;
  116. errorbar(0:trialsback,allVPmdl.Coefficients.Estimate(2:trialsback+2),allVPmdl.Coefficients.SE(2:trialsback+2),'linewidth',1.5);
  117. axis([-1 trialsback+1 -1 2]);
  118. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  119. xlabel('Trials back');
  120. ylabel('Coefficient weight');
  121. title('Trial history regression, all neurons');
  122. linecolor{1}=Colors('rpe');
  123. linecolor{2}=Colors('current');
  124. linecolor{3}=Colors('mean');
  125. for i=1:3
  126. Sel=masks(:,i);
  127. Sel=Sel(region&task); %only the neurons from VP interspersed
  128. %calculate model based on all reward selective neurons
  129. allRDHz=[];
  130. allpreds=[];
  131. selindex=find(Sel);
  132. for j=1:sum(Sel)
  133. allRDHz=cat(1,allRDHz,RewHist.RDHz{selindex(j),1});
  134. allpreds=cat(1,allpreds,RewHist.Predictors{selindex(j),1}(:,1:trialsback+1));
  135. end
  136. allVPmdl=fitlm(allpreds,allRDHz);
  137. stats.trials.subsets{i,1}=allVPmdl.Coefficients.pValue(2:12);
  138. %plot coefficients
  139. subplot(3,3,6);
  140. hold on;
  141. errorbar(0:trialsback,allVPmdl.Coefficients.Estimate(2:trialsback+2),allVPmdl.Coefficients.SE(2:trialsback+2),'color',linecolor{i},'linewidth',1.5);
  142. end
  143. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  144. xlabel('Trials back');
  145. ylabel('Coefficient weight');
  146. title('Trial history regression, subsets');
  147. legend('RPE','Current','Unmod.');
  148. axis([-1 trialsback+1 -3 5]); %AIC
  149. %axis([-1 trialsback+1 -3 6]); %BIC
  150. disp('B&G regressions')
  151. %% bar graphs
  152. stacked_data = zeros(2,3);
  153. for subset=1:3
  154. stacked_data(1,subset)=sum(region&masks(:,subset)&task);
  155. end
  156. subplot(3,3,5)
  157. hold on;
  158. b = bar(stacked_data./sum(stacked_data,2),'stacked','edgecolor','none');
  159. b(3).FaceColor = 'Flat';
  160. b(3).CData = Colors('mean');
  161. b(2).FaceColor = 'Flat';
  162. b(2).CData = Colors('current');
  163. b(1).FaceColor = 'Flat';
  164. b(1).CData = Colors('rpe');
  165. axis([0 5 0 1])
  166. % text(1.5,0.7,'Mean','color',[0.6 0.6 0.6]);
  167. % text(1.5,0.3,'Current only','color',[0.6 0 0.6]);
  168. % text(1.5,0.1,'RPE','color',[0 0.8 0.6]);
  169. set(gca,'xtick',[]);
  170. yticks([0,.2,.4,.6,.8,1])
  171. ylabel('Fraction of population')
  172. legend('RPE','Current','Unmod.','location','northwest');
  173. %% activity plots for subsets
  174. ymax=11;
  175. %ymax=5;
  176. Xaxis=[-0.5 3];
  177. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  178. time1=R.Param.Tm(Ishow);
  179. titles={'RPE neurons (n=';'Current outcome neurons (n=';'Unmodulated neurons (n='};
  180. for i=1:3
  181. if i==1 Sel=masks(:,1); end %base neurons
  182. if i==2 Sel=masks(:,2); end %current outcome neurons
  183. if i==3 Sel=masks(:,3); end %mean neurons
  184. Sel=Sel&region&task; %only the neurons from VP interspersed
  185. %plot suc after suc
  186. psth1=nanmean(R.Ev(RD1P1).PSTHz(Sel,Ishow),1);
  187. sem1=nanste(R.Ev(RD1P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  188. up1=psth1+sem1;
  189. down1=psth1-sem1;
  190. %plot suc after malt
  191. psth2=nanmean(R.Ev(RD1P2).PSTHz(Sel,Ishow),1);
  192. sem2=nanste(R.Ev(RD1P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  193. up2=psth2+sem2;
  194. down2=psth2-sem2;
  195. %plot malt after suc
  196. psth3=nanmean(R.Ev(RD2P1).PSTHz(Sel,Ishow),1);
  197. sem3=nanste(R.Ev(RD2P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  198. up3=psth3+sem3;
  199. down3=psth3-sem3;
  200. %plot malt after malt
  201. psth4=nanmean(R.Ev(RD2P2).PSTHz(Sel,Ishow),1);
  202. sem4=nanste(R.Ev(RD2P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  203. up4=psth4+sem4;
  204. down4=psth4-sem4;
  205. %make the plot
  206. subplot(3,3,6+i);
  207. hold on;
  208. title([titles{i} num2str(sum(Sel)) ')'])
  209. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  210. a=plot(time1,psth2,'Color',Colors('sucrosemnp'),'linewidth',1);
  211. b=plot(time1,psth1,'Color',Colors('sucrosesnp'),'linewidth',1);
  212. c=plot(time1,psth4,'Color',Colors('maltodextrinmnp'),'linewidth',1);
  213. d=plot(time1,psth3,'Color',Colors('maltodextrinsnp'),'linewidth',1);
  214. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],Colors('sucrosemnp'),'EdgeColor','none');alpha(alph);
  215. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrosesnp'),'EdgeColor','none');alpha(alph);
  216. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],Colors('maltodextrinsnp'),'EdgeColor','none');alpha(alph);
  217. patch([time1,time1(end:-1:1)],[up4,down4(end:-1:1)],Colors('maltodextrinmnp'),'EdgeColor','none');alpha(alph);
  218. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  219. plot([0 0],[-2 ymax],'color','k','linewidth',0.5);
  220. axis([Xaxis(1) Xaxis(2) -2 ymax]);
  221. ylabel('Mean firing (z-score)');
  222. xlabel('Seconds from RD');
  223. if i==3 legend([a b c d],'Suc after mal','Suc after suc','Mal after mal','Mal after suc','location','northeast'); end
  224. end
  225. disp('Subsets event responses')
  226. %% evaluation of RPE models
  227. figure;
  228. alph=0.3;
  229. %raster
  230. window=[0 3];
  231. sample_neuron_number=795; %794-798, 829, 1101
  232. subplot(1,4,1);
  233. hold on;
  234. sessions=unique(R.Ninfo(:,1));
  235. session=R.Ninfo(sample_neuron_number,1);
  236. sample_neuron_session=strcmp(session,sessions);
  237. neurons=R.Ninfo(strcmp(session,R.Ninfo(:,1)),2);
  238. sample_neuron_session_number=strcmp(R.Ninfo(sample_neuron_number,2),neurons);
  239. RD=strcmp('RD', RAW(sample_neuron_session).Einfo(:,2));
  240. RDtimes=RAW(sample_neuron_session).Erast{RD,1};
  241. [~,order]=sort(os(sample_neuron_number).mod_RD.base.RPEs);
  242. RDtimes=RDtimes(order);
  243. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[0 length(RDtimes)*11 length(RDtimes)*11 0],Colors('gray'),'edgecolor','none');alpha(alph);
  244. PlotRaster(RAW(sample_neuron_session).Nrast{sample_neuron_session_number,1},RDtimes,window);
  245. ylabel('Trials, sorted by RPE (low to high)');
  246. yticks([]);
  247. xticks(0:3);
  248. xticklabels(0:3);
  249. xlabel('Seconds from reward delivery');
  250. xlim(window);
  251. disp('Raster')
  252. %% RPE neurons based on different RPE values
  253. plot_RPEs=1;
  254. if plot_RPEs
  255. bins=[-3 -1.2;-1.2 -0.7;-0.7 -0.3;-0.3 0;0 0.3;0.3 0.7;0.7 1.2;1.2 3];
  256. Sel=masks(:,1) & region & task;
  257. mintrials=1;
  258. global Dura Tm BSIZE
  259. BSIZE=0.01; %Do not change
  260. Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2); %should match whatever was used to create R for simplicity
  261. Baseline=[-11 -1]; %for PSTHs, should match whatever is in R
  262. %smoothing filter for whole PSTH
  263. PSTHsmoothbins=50; %number of previous bins used to smooth
  264. halfnormal=makedist('HalfNormal','mu',0,'sigma',20); %std=3.98
  265. PSTHfilterweights=pdf(halfnormal,0:PSTHsmoothbins);
  266. %smoothing filter for individual trials
  267. trialsmoothbins=25; %number of previous bins used to smooth
  268. halfnormal=makedist('HalfNormal','mu',0,'sigma',10); %std=3.98
  269. trialfilterweights=pdf(halfnormal,0:trialsmoothbins);
  270. sessions=unique(R.Ninfo(:,1));
  271. index=find(Sel);
  272. for i=1:sum(Sel)
  273. session=R.Ninfo(index(i),1); %what's the name of session
  274. sessnum=ismember(sessions,session); %which session of all sessions
  275. neuronnum=ismember(R.Ninfo(strcmp(session,R.Ninfo(:,1)),2),R.Ninfo(index(i),2)); %which number neuron in the session
  276. RD=strmatch('RD',RAW(sessnum).Einfo(:,2),'exact'); %get index for reward delivery times
  277. Cue=strmatch('Cue',RAW(sessnum).Einfo(:,2),'exact'); %get index for reward delivery times
  278. %get mean baseline firing for all reward trials
  279. [Bcell1,B1n]=MakePSR04(RAW(sessnum).Nrast(neuronnum),RAW(sessnum).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
  280. basespk=[];
  281. for y= 1:B1n
  282. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  283. end
  284. FiringRate=basespk/(Baseline(1,2)-Baseline(1,1));
  285. Bmean=nanmean(FiringRate);
  286. Bstd=nanstd(FiringRate);
  287. %make PSTHs around reward delivery
  288. trialtimes=RAW(sessnum).Erast{RD,1}(:,1); %reward delivery times
  289. for j=1:length(bins)
  290. RPEs=os(index(i)).mod_RD.base.RPEs;
  291. RPEsZ = (RPEs - mean(RPEs))/std(RPEs);
  292. toi=trialtimes(RPEsZ>=bins(j,1) & RPEsZ<=bins(j,2)); %which trials, binned by model-produced RPE
  293. if length(toi)>=mintrials
  294. %make PSTHs
  295. [PSR1,~]=MakePSR04(RAW(sessnum).Nrast(neuronnum),toi,Dura,{2});% makes collpased rasters. PSR1 is a cell(neurons)
  296. smoothedtrials=[];
  297. for trial=1:length(PSR1)
  298. binned=histcounts(PSR1{trial},[Tm Tm(end)+(Tm(end)-Tm(end-1))]);
  299. for l=1:length(Tm)
  300. 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])));
  301. end
  302. end
  303. PTH1=mean(smoothedtrials,1)/BSIZE;
  304. PTH1smooth=[];
  305. for l=1:length(Tm)
  306. 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])));
  307. end
  308. RPEPSTHs{j,1}.PSTHraw(i,:)=PTH1smooth;
  309. %normalize already smoothed activity
  310. for l=1:length(PTH1smooth)
  311. RPEPSTHs{j,1}.PSTHz(i,l)=(PTH1smooth(l)-Bmean)/Bstd;
  312. end
  313. else
  314. RPEPSTHs{j,1}.PSTHraw(i,1:length(Tm))=NaN;
  315. RPEPSTHs{j,1}.PSTHz(i,1:length(Tm))=NaN;
  316. end
  317. end
  318. end
  319. %plotting the histograms
  320. ymax=10; %AIC
  321. %ymax=12; %BIC
  322. Xaxis=[-1.5 4];
  323. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  324. time1=R.Param.Tm(Ishow);
  325. alph=0.2;
  326. psthcolor=[0 0.9 1;...
  327. 0 0.7 0.75;...
  328. 0 0.5 0.5;...
  329. 0 0.3 0.25;...
  330. 0.25 0.3 0;...
  331. 0.5 0.5 0;...
  332. 0.75 0.7 0;...
  333. 1 0.9 0];
  334. subplot(2,4,2:3);
  335. hold on;
  336. patch([HistWindow(1) HistWindow(1) HistWindow(2) HistWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  337. a={};
  338. for i=1:length(RPEPSTHs) %for each bin
  339. %plot activity for each bin
  340. psth1=nanmean(RPEPSTHs{i,1}.PSTHz(:,Ishow),1);
  341. sem1=nanste(RPEPSTHs{i,1}.PSTHz(:,Ishow),1); %calculate standard error of the mean
  342. up1=psth1+sem1;
  343. down1=psth1-sem1;
  344. %make the plot
  345. a{i}=plot(time1,psth1,'Color',psthcolor(i,:),'linewidth',1.5);
  346. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],psthcolor(i,:),'EdgeColor','none');alpha(alph);
  347. labels{i}=[num2str(bins(i,1)) ' < RPE < ' num2str(bins(i,2))];
  348. end
  349. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  350. plot([0 0],[-3 ymax],'color','k','linewidth',0.5);
  351. axis([Xaxis(1) Xaxis(2) -2 ymax]);
  352. ylabel('Mean firing (z-score)');
  353. xlabel('Seconds from RD');
  354. colormap(psthcolor);
  355. h=colorbar;
  356. set(h,'YTick',[0:0.125:1]);
  357. set(h,'yticklabel',[-3;-1.2;-.7;-.3;0;0.3;0.7;1.2;3])
  358. set(get(h,'title'),'string','RPE');
  359. %legend([a{:}],labels,'location','northeast');
  360. end
  361. %% bar graphs comparing regions
  362. stacked_data = zeros(2,3);
  363. for reg=1:2
  364. if reg==1 regsel = strcmp(R.Region,'VP'); end
  365. if reg==2 regsel = strcmp(R.Region,'NA'); end
  366. for subset=1:3
  367. stacked_data(reg,subset)=sum(regsel&masks(:,subset)&task);
  368. end
  369. end
  370. subplot(2,4,7)
  371. hold on;
  372. b = bar(stacked_data./sum(stacked_data,2),'stacked','edgecolor','none');
  373. b(3).FaceColor = 'Flat';
  374. b(3).CData = Colors('mean');
  375. b(2).FaceColor = 'Flat';
  376. b(2).CData = Colors('current');
  377. b(1).FaceColor = 'Flat';
  378. b(1).CData = Colors('rpe');
  379. axis([0 4 0 1])
  380. % text(1.5,0.7,'Mean','color',[0.6 0.6 0.6]);
  381. % text(1.5,0.3,'Current only','color',[0.6 0 0.6]);
  382. % text(1.5,0.1,'RPE','color',[0 0.8 0.6]);
  383. set(gca,'xtick',[]);
  384. yticks([0,.2,.4,.6,.8,1])
  385. ylabel('Fraction of population')
  386. legend('RPE','Current','Unmod.','location','northwest');
  387. if plot_value
  388. %% cue responses
  389. masks=cat(2,bm_cue.mask_base',bm_cue.mask_mean');
  390. figure;
  391. %% bar graphs
  392. stacked_data = zeros(2,3);
  393. for subset=1:2
  394. stacked_data(1,subset)=sum(region&masks(:,subset)&task);
  395. end
  396. subplot(2,3,1)
  397. hold on;
  398. b = bar(stacked_data./sum(stacked_data,2),'stacked','edgecolor','none');
  399. b(2).FaceColor = 'Flat';
  400. b(2).CData = Colors('mean');
  401. b(1).FaceColor = 'Flat';
  402. b(1).CData = Colors('rpe');
  403. axis([0 5 0 1])
  404. set(gca,'xtick',[]);
  405. yticks([0,.2,.4,.6,.8,1])
  406. ylabel('Fraction of population')
  407. legend('Value','Unmod.','location','northwest');
  408. %% activity plots for subsets
  409. ymax=10;
  410. %ymax=5;
  411. Xaxis=[-1 2];
  412. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  413. time1=R.Param.Tm(Ishow);
  414. titles={'Value neurons (n=';'Unmodulated neurons (n='};
  415. for i=1:2
  416. if i==1 Sel=bm_cue.mask_base'; end %base neurons
  417. if i==2 Sel=bm_cue.mask_mean'; end %mean neurons
  418. Sel=Sel&region&task; %only the neurons from VP interspersed
  419. %plot cue after suc
  420. psth1=nanmean(R.Ev(suc_cue).PSTHz(Sel,Ishow),1);
  421. sem1=nanste(R.Ev(suc_cue).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  422. up1=psth1+sem1;
  423. down1=psth1-sem1;
  424. %plot cue after malt
  425. psth2=nanmean(R.Ev(mal_cue).PSTHz(Sel,Ishow),1);
  426. sem2=nanste(R.Ev(mal_cue).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  427. up2=psth2+sem2;
  428. down2=psth2-sem2;
  429. %make the plot
  430. subplot(2,3,1+i);
  431. hold on;
  432. title([titles{i} num2str(sum(Sel)) ')'])
  433. patch([CueWindow(1) CueWindow(1) CueWindow(2) CueWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  434. a=plot(time1,psth2,'Color',Colors('maltodextrin'),'linewidth',1);
  435. b=plot(time1,psth1,'Color',Colors('sucrose'),'linewidth',1);
  436. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],Colors('maltodextrin'),'EdgeColor','none');alpha(alph);
  437. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],Colors('sucrose'),'EdgeColor','none');alpha(alph);
  438. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  439. plot([0 0],[-2 ymax],'color','k','linewidth',0.5);
  440. axis([Xaxis(1) Xaxis(2) -2 ymax]);
  441. ylabel('Mean firing (z-score)');
  442. xlabel('Seconds from cue');
  443. if i==2 legend([b a],'After suc','After mal','location','northeast'); end
  444. end
  445. disp('Subsets event responses')
  446. %% B&G for cue subsets
  447. linecolor{1}=Colors('rpe');
  448. linecolor{2}=Colors('mean');
  449. for i=1:2
  450. if i==1 Sel=bm_cue.mask_base'; end %base neurons
  451. if i==2 Sel=bm_cue.mask_mean'; end %base neurons
  452. Sel=Sel(region&task); %only the neurons from VP interspersed
  453. %calculate model based on all reward selective neurons
  454. allRDHz=[];
  455. allpreds=[];
  456. selindex=find(Sel);
  457. for j=1:sum(Sel)
  458. allRDHz=cat(1,allRDHz,RewHist.CueHz{selindex(j),1});
  459. allpreds=cat(1,allpreds,RewHist.Predictors{selindex(j),1}(:,2:trialsback+1));
  460. end
  461. allVPmdl=fitlm(allpreds,allRDHz);
  462. stats.cue_trials.subsets{i,1}=allVPmdl.Coefficients.pValue(2:11);
  463. %plot coefficients
  464. subplot(2,3,4);
  465. hold on;
  466. errorbar(1:trialsback,allVPmdl.Coefficients.Estimate(2:trialsback+1),allVPmdl.Coefficients.SE(2:trialsback+1),'color',linecolor{i},'linewidth',1.5);
  467. end
  468. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  469. xlabel('Trials back');
  470. ylabel('Coefficient weight');
  471. title('Trial history regression, subsets');
  472. legend('Value','Unmod.');
  473. axis([0 trialsback+1 -1 3]);
  474. disp('B&G regressions')
  475. %% Value neurons based on different values
  476. bins=[-3 -1.2;-1.2 -0.5;-0.5 0.5;0.5 1.2;1.2 3];
  477. Sel=bm_cue.mask_base' & region & task;
  478. mintrials=1;
  479. global Dura Tm BSIZE
  480. BSIZE=0.01; %Do not change
  481. Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2); %should match whatever was used to create R for simplicity
  482. Baseline=[-11 -1]; %for PSTHs, should match whatever is in R
  483. %smoothing filter for whole PSTH
  484. PSTHsmoothbins=50; %number of previous bins used to smooth
  485. halfnormal=makedist('HalfNormal','mu',0,'sigma',10); %std=3.98
  486. PSTHfilterweights=pdf(halfnormal,0:PSTHsmoothbins);
  487. %smoothing filter for individual trials
  488. trialsmoothbins=25; %number of previous bins used to smooth
  489. halfnormal=makedist('HalfNormal','mu',0,'sigma',5); %std=3.98
  490. trialfilterweights=pdf(halfnormal,0:trialsmoothbins);
  491. sessions=unique(R.Ninfo(:,1));
  492. index=find(Sel);
  493. RPEPSTHs={};
  494. for i=1:sum(Sel)
  495. session=R.Ninfo(index(i),1); %what's the name of session
  496. sessnum=ismember(sessions,session); %which session of all sessions
  497. neuronnum=ismember(R.Ninfo(strcmp(session,R.Ninfo(:,1)),2),R.Ninfo(index(i),2)); %which number neuron in the session
  498. RD=strmatch('RD',RAW(sessnum).Einfo(:,2),'exact'); %get index for reward delivery times
  499. Cue=strmatch('Cue',RAW(sessnum).Einfo(:,2),'exact'); %get index for reward delivery times
  500. %get mean baseline firing for all reward trials
  501. [Bcell1,B1n]=MakePSR04(RAW(sessnum).Nrast(neuronnum),RAW(sessnum).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
  502. basespk=[];
  503. for y= 1:B1n
  504. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  505. end
  506. FiringRate=basespk/(Baseline(1,2)-Baseline(1,1));
  507. Bmean=nanmean(FiringRate);
  508. Bstd=nanstd(FiringRate);
  509. %make PSTHs around cue onset
  510. cuetimes=RAW(sessnum).Erast{Cue,1}(:,1); %cue times
  511. RDtimes=RAW(sessnum).Erast{RD,1}(:,1); %reward delivery times
  512. trialtimes=[];
  513. for trial = 1:length(RDtimes)
  514. trialtimes(trial,1) = max(cuetimes(cuetimes<RDtimes(trial)));
  515. end
  516. for j=1:length(bins)
  517. RPEs=os(index(i)).mod_cue.base.V;
  518. RPEsZ = (RPEs - mean(RPEs))/std(RPEs);
  519. toi=trialtimes(RPEsZ>=bins(j,1) & RPEsZ<=bins(j,2)); %which trials, binned by model-produced RPE
  520. if length(toi)>=mintrials
  521. %make PSTHs
  522. [PSR1,~]=MakePSR04(RAW(sessnum).Nrast(neuronnum),toi,Dura,{2});% makes collpased rasters. PSR1 is a cell(neurons)
  523. smoothedtrials=[];
  524. for trial=1:length(PSR1)
  525. binned=histcounts(PSR1{trial},[Tm Tm(end)+(Tm(end)-Tm(end-1))]);
  526. for l=1:length(Tm)
  527. 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])));
  528. end
  529. end
  530. PTH1=mean(smoothedtrials,1)/BSIZE;
  531. PTH1smooth=[];
  532. for l=1:length(Tm)
  533. 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])));
  534. end
  535. RPEPSTHs{j,1}.PSTHraw(i,:)=PTH1smooth;
  536. %normalize already smoothed activity
  537. for l=1:length(PTH1smooth)
  538. RPEPSTHs{j,1}.PSTHz(i,l)=(PTH1smooth(l)-Bmean)/Bstd;
  539. end
  540. else
  541. RPEPSTHs{j,1}.PSTHraw(i,1:length(Tm))=NaN;
  542. RPEPSTHs{j,1}.PSTHz(i,1:length(Tm))=NaN;
  543. end
  544. end
  545. end
  546. %plotting the histograms
  547. ymax=9;
  548. Xaxis=[-1 2];
  549. Ishow=find(R.Param.Tm>=Xaxis(1) & R.Param.Tm<=Xaxis(2));
  550. time1=R.Param.Tm(Ishow);
  551. alph=0.2;
  552. psthcolor=[0 0.1 0;...
  553. 0 0.3 0.2;...
  554. 0 0.5 0.3;...
  555. 0 0.75 0.5;...
  556. 0 1 0.7];
  557. subplot(2,2,4);
  558. hold on;
  559. patch([CueWindow(1) CueWindow(1) CueWindow(2) CueWindow(2)],[-5 12 12 -5],Colors('gray'),'EdgeColor','none');
  560. a={};
  561. for i=1:length(RPEPSTHs) %for each bin
  562. %plot activity for each bin
  563. psth1=nanmean(RPEPSTHs{i,1}.PSTHz(:,Ishow),1);
  564. sem1=nanste(RPEPSTHs{i,1}.PSTHz(:,Ishow),1); %calculate standard error of the mean
  565. up1=psth1+sem1;
  566. down1=psth1-sem1;
  567. %make the plot
  568. a{i}=plot(time1,psth1,'Color',psthcolor(i,:),'linewidth',1.5);
  569. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],psthcolor(i,:),'EdgeColor','none');alpha(alph);
  570. labels{i}=[num2str(bins(i,1)) ' < RPE < ' num2str(bins(i,2))];
  571. end
  572. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  573. plot([0 0],[-3 ymax],'color','k','linewidth',0.5);
  574. axis([Xaxis(1) Xaxis(2) -2 ymax]);
  575. ylabel('Mean firing (z-score)');
  576. xlabel('Seconds from cue');
  577. colormap(psthcolor);
  578. h=colorbar;
  579. set(h,'YTick',[0:0.2:1]);
  580. set(h,'yticklabel',[-3;-1.2;-.5;0.5;1.2;3])
  581. set(get(h,'title'),'string','Value');
  582. %legend([a{:}],labels,'location','northeast');
  583. end