plot_TH.m 25 KB

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