exampleraster.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. function [ras,R]=exampleraster(ev, ts,varargin)
  2. % [ax_handle,data]=rasterC(ev, ts, varargin)
  3. % pairs={'pre' 3;...
  4. % 'post' 3;...
  5. % 'binsz' 0.050;...
  6. % 'cnd' 1;...
  7. % 'meanflg' 0;...
  8. % 'krn' 0.25;...
  9. % 'ax_handle' [];...
  10. % 'legend_str' '';...
  11. % 'renderer', 'opengl';...
  12. % 'ref_label', 'REF';...
  13. % 'psth_height', 0.248;...
  14. % 'total_height' 0.8;...
  15. % 'corner' [0.1 0.1];...
  16. % 'ax_width' 0.55;...
  17. % 'font_name' 'Helvetica';...
  18. % 'font_size' 9;...
  19. % 'legend_pos' [0.73 0.1 0.2 0.15];...
  20. % 'clrs' {'c','b','r','m','r','g','m'};...
  21. % 'x_label','';...
  22. % 'pre_mask', -inf;...
  23. % 'post_mask',+inf;...
  24. % 'cout',[];...
  25. % 'stim_back',[];...
  26. % 'errorbars', 0;...
  27. % 'testfunc', [];...
  28. % 'sortby', [];...
  29. %
  30. corner=[]; % this is necessary because corner is a function
  31. iod = @utils.inputordefault;
  32. pre = iod('pre',3,varargin);
  33. post = iod('post',3,varargin);
  34. binsz = iod('binsz',0.01,varargin);
  35. cnd = iod('cnd',1,varargin);
  36. meanflg = iod('meanflg',0,varargin);
  37. krn = iod('krn',0.1,varargin);
  38. ax_handle = iod('ax_handle',[],varargin);
  39. legend_str = iod('legend_str','',varargin);
  40. renderer =iod('renderer','painters',varargin);
  41. ref_label=iod('ref_label','REF',varargin);
  42. psth_height=iod('psth_height',0.248,varargin);
  43. total_height=iod('total_height',0.8,varargin);
  44. corner=iod('corner',[0.1 0.1],varargin);
  45. ax_width=iod('ax_width',0.55,varargin);
  46. font_name=iod('font_name','Helvetica',varargin);
  47. font_size=iod('font_size',14,varargin);
  48. legend_pos=iod('legend_pos',[0.73 0.1 0.2 0.15],varargin);
  49. clrs=iod('clrs',{'b','m','r','c','k','g','y',[1 0.5 0],[0.5 0.5 0.5]},varargin);
  50. x_label=iod('x_label','',varargin);
  51. pre_mask=iod('pre_mask', -inf,varargin);
  52. post_mask=iod('post_mask',+inf,varargin);
  53. cout=iod('cout',[],varargin);
  54. stim_back=iod('stim_back',[],varargin);
  55. sb_clr=iod('sb_clr',[0.8 0.8 0.4],varargin);
  56. errorbars=iod('errorbars',1,varargin);
  57. testfunc=iod('testfunc',[],varargin);
  58. show_yinfo=iod('show_yinfo',1,varargin);
  59. sortby=iod('sortby',[],varargin);
  60. do_legstr = iod('do_legstr',0,varargin);
  61. set(gcf, 'Renderer',renderer);
  62. [ntrials,nrefs]=size(ev);
  63. mutau=zeros(1,nrefs);
  64. for rx=2:nrefs
  65. mutau(rx)=nanmedian(ev(:,rx)-ev(:,1));
  66. end
  67. if isscalar(pre_mask)
  68. pre_mask=zeros(1,ntrials)+pre_mask;
  69. elseif numel(pre_mask)~=ntrials
  70. fprintf(1,'numel(pre_mask) must equal num ref events or be scalar');
  71. return;
  72. end
  73. if isscalar(post_mask)
  74. post_mask=zeros(1,ntrials)+post_mask;
  75. elseif numel(post_mask)~=ntrials
  76. fprintf(1,'numel(post_mask) must equal num ref events or be scalar');
  77. return;
  78. end
  79. if isscalar(krn)
  80. dx=ceil(5*krn);
  81. kx=-dx:binsz:dx;
  82. krn=normpdf(kx,0, krn);
  83. if isempty(find(kx==0, 1))
  84. error('Your binsz needs to divide 1 second into interger # of bins');
  85. end
  86. krn(kx<0)=0;
  87. krn=krn/sum(krn);
  88. end
  89. if numel(cnd)==1
  90. cnd=ones(1,ntrials);
  91. end
  92. if iscell(cnd)
  93. cnd_nan = cellfun(@(x)any(isnan(x)), cnd); % use any to deal with character arrays.
  94. cnd(cnd_nan) = {'NaN'};
  95. cnd = categorical(cnd);
  96. n_cnd = categories(cnd);
  97. elseif isstring(cnd)
  98. cnd = categorical(cnd);
  99. n_cnd = categories(cnd);
  100. elseif isnumeric(cnd)
  101. n_cnd=unique(cnd);
  102. else
  103. n_cnd = categories(cnd);
  104. end
  105. raster_height=total_height-psth_height;
  106. y_ind=psth_height+corner(2)+0.005;
  107. height_per_trial=raster_height/ntrials;
  108. psthax=axes('Position',[corner(1) corner(2) ax_width psth_height]);
  109. hold(psthax,'on');
  110. set(psthax,'FontName',font_name);
  111. set(psthax,'FontSize',font_size)
  112. %[Y,x,W]=warpfilter(ev,ts,krn,'pre',pre,'post',post,'kernel_bin_size',binsz);
  113. [Y,x]=stats.spike_filter(ev,ts,krn,'pre',pre,'post',post,'kernel_bin_size',binsz);
  114. W = ts;
  115. for ci=1:numel(n_cnd)
  116. sampz=sum(cnd==n_cnd(ci));
  117. ref=cnd==n_cnd(ci);
  118. if ~isempty(sortby)
  119. idx=1:ntrials; idx=idx(:); ref=ref(:);
  120. ref=sortrows([sortby(ref) idx(ref)]);
  121. ref=ref(:,2);
  122. end
  123. y=Y(ref,:);
  124. [y2,x2]=draw.rasterplot(ev(ref,1),W,pre,post,'pre_mask',pre_mask(ref),'post_mask',post_mask(ref),'plotthis',0);
  125. ras(ci)=axes('Position',[corner(1) y_ind ax_width height_per_trial*sampz]);
  126. y_ind=y_ind+height_per_trial*sampz+0.001;
  127. if ~isempty(stim_back)
  128. patchplot(stim_back(ref,:),'clr',sb_clr)
  129. end
  130. %% Plot the rasters
  131. ll=line(x2,y2);
  132. set(ll,'color','k');
  133. set(gca,'XTickLabel',[]);
  134. set(gca,'YTick',[]);
  135. set(gca,'Box','off')
  136. if isempty(y2)
  137. set(gca,'YLim',[0 1e-8]);
  138. else
  139. set(gca,'YLim',[0 max(y2)]);
  140. end
  141. set(gca,'XLim',[-pre post]);
  142. for rx=1:nrefs
  143. ll=line([mutau(rx) mutau(rx)],[0 max(y2)]);
  144. set(ll,'LineStyle','-','color',clrs{ci},'LineWidth',1);
  145. end
  146. if ~isempty(cout)
  147. hold on;
  148. h=plot(ras(ci),cout(ref,:),1:sampz,'o','Color','k','MarkerFaceColor',clrs{ci},'MarkerSize',2);
  149. end
  150. %% Calculate the mean and ci of the
  151. [y x]=draw.maskraster(x,y,pre_mask(ref),post_mask(ref));
  152. ymn(ci,:) = nanmean(y,1);
  153. yst(ci,:)= stats.nanstderr(y,1);
  154. R{ci}={y x n_cnd{ci}};
  155. %axes(psthax);
  156. hold on
  157. % hh=line(x/1000,ymn(ci,:));
  158. % set(hh,'LineWidth',1,'LineStyle','-','Color',clrs{ci});
  159. if strcmpi(renderer,'opengl')
  160. sh(ci)=draw.shadeplot(x,ymn(ci,:)-yst(ci,:),ymn(ci,:)+yst(ci,:),{clrs{ci},psthax,0.6});
  161. % lh=line(x,ymn(ci,:),'Color',clrs{ci},'LineWidth',2);
  162. else
  163. if errorbars
  164. hh(1)=line(x,ymn(ci,:)-yst(ci,:),'Parent',psthax);
  165. hh(2)=line(x,ymn(ci,:)+yst(ci,:),'Parent',psthax);
  166. set(hh,'LineWidth',1,'LineStyle','-','Color',clrs{ci});
  167. %lh=line(x,ymn(ci,:),'Color',clrs{ci},'LineWidth',1,'Parent',psthax);
  168. lh=hh(1);
  169. sh(ci)=lh;
  170. else
  171. hh(1)=line(x,ymn(ci,:),'Parent',psthax);
  172. set(hh,'LineWidth',2,'LineStyle','-','Color',clrs{ci});
  173. %lh=line(x,ymn(ci,:),'Color',clrs{ci},'LineWidth',1,'Parent',psthax);
  174. lh=hh(1);
  175. sh(ci)=lh;
  176. end
  177. end
  178. peaky(ci)=max(ymn(ci,:)+yst(ci,:));
  179. set(psthax,'XLim',[-pre,post]);
  180. if do_legstr
  181. legstr{ci}=[n_cnd{ci} ', n=' num2str(sampz)];
  182. ylabel(ras(ci),legstr{ci},'Rotation',0,'HorizontalAlign','right','VerticalAlign','middle');
  183. end
  184. end
  185. cur_ylim=get(psthax,'YLim');
  186. maxpsthylim = max(peaky)*1.15;
  187. if maxpsthylim<=0 || isnan(maxpsthylim)
  188. maxpsthylim = 0.5;
  189. end
  190. ylim(psthax,[0 maxpsthylim]);
  191. for rx=1:nrefs
  192. ll=plot(psthax,[mutau(rx) mutau(rx)],[0 maxpsthylim]);
  193. set(ll,'LineStyle','-','color',[0.5 0.5 0.5],'LineWidth',1);
  194. end
  195. ch=get(psthax,'Children');
  196. set(psthax,'Children',[ch(nrefs+1:end); ch(1:nrefs)]);
  197. xticks=get(psthax,'XTick');
  198. set(psthax,'XTick',xticks);
  199. set(ras,'XTick',xticks);
  200. if ~isempty(legend_pos) && ~isempty(legend_str)
  201. [lh,oh]=legend(sh,legend_str);
  202. legend boxoff
  203. % keyboard
  204. set(lh,'Position',legend_pos);
  205. end
  206. hold off
  207. %set(gca,'FontSize',36);
  208. if isempty(x_label)
  209. xh=xlabel(psthax,['Time from ' ref_label '(sec)']); set(xh,'interpreter','none');
  210. else
  211. xh=xlabel(psthax,x_label);set(xh,'interpreter','none');
  212. end
  213. if show_yinfo
  214. ylabel(psthax,'Hz \pm SE')
  215. else
  216. set(psthax,'YTick',[]);
  217. end
  218. ras(end+1)=psthax;