FigsDemo.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. function FigsDemo
  2. % FIGSDEMO - demonstration of plotting contents of a processed data file to
  3. % make many of the figures in the manuscript. Runs in Matlab.
  4. % This assumes that dD, dEN files containing data from experiment SW421 are
  5. % in the Matlab workspace.
  6. gray = [0.5 0.5 0.5];
  7. % Get the data from SW421
  8. load('SW421_dD_dEN_C.mat')
  9. % FIGURE 1A. Plot the raw waveform (blue), fit waveform (red), and
  10. % deconvolved event signal (green) for the examples in this figure.The
  11. % EPSCs are located at the following times, s.:
  12. EPSClocs = [337.0708 1.0000;
  13. 337.1683 1.0000;
  14. 337.2380 7.0000;
  15. 337.2835 5.0000;
  16. 337.3419 4.0000;
  17. 337.3556 1.0000;
  18. 337.4153 3.0000;
  19. 337.4400 2.0000;
  20. 337.4828 1.0000;
  21. 337.6855 6.0000;
  22. 337.7531 3.0000;
  23. 337.8212 1.0000;
  24. 337.9187 1.0000];
  25. figure(1); clf
  26. % Signals are sampled at dD.ut_s s. Convert files to indices of the sigs.
  27. indices = round(EPSClocs(:,1)/dD.ut_s); fivems = round(0.005/dD.ut_s);
  28. rw = []; fw = []; ds = []; % To hold the 3 plotted signals
  29. for jn=1:size(EPSClocs,1)
  30. rw = [rw; dD.DataDC(indices(jn)-fivems:indices(jn)+fivems)];
  31. fw = [fw; dEN.dataOIL(indices(jn)-fivems:indices(jn)+fivems)];
  32. ds = [ds; dEN.xsigL(indices(jn)-fivems:indices(jn)+fivems)];
  33. end
  34. tim = 0:dD.ut_s:(length(rw)-1)*dD.ut_s;
  35. plot(tim, fw, 'r-'); hold on
  36. plot(tim, rw, 'b-'); plot(tim, ds, 'g-')
  37. xlabel('Seconds'); ylabel('Currents, pA')
  38. legend('Fitted waveshape', 'Data', 'Deconv. signal')
  39. title('Figure 1A')
  40. % FIGURE 1-supplementary can be obtained as
  41. % plot(dD.timK, dD.AvgKernel)
  42. % FIGURE 2A - histograms of dEN.cluspks, which are the amplitudes of the
  43. % EPSCS, which contain dEN.nsigpks events.
  44. thpks = -dEN.cluspks; % EPSC amplitudes, sign changed to +
  45. % Bin edges estimated with Scott's rule:
  46. bw = 3.5*std(thpks)/numel(thpks)^1/3;
  47. bw = 10*ceil(bw/10);
  48. edges = (-50:bw:max(thpks)+bw)';
  49. fprintf('bw=%g pA.\n', bw)
  50. % Plot the amplitude histograms
  51. figure(2); clf
  52. %i1 = find(dEN.nsigpks==1); % Indices of monos in the data
  53. histogram(thpks(dEN.nsigpks==1), edges)
  54. hold on
  55. for jn=2:4
  56. if jn<4
  57. iI = find(dEN.nsigpks==jn);
  58. else
  59. iI = find(dEN.nsigpks>=4);
  60. end
  61. histogram(thpks(iI), edges, 'DisplayStyle','stairs', 'LineWidth', 2) % multis
  62. end
  63. histogram(thpks, edges,'DisplayStyle','stairs', 'EdgeColor','k', ...
  64. 'LineWidth',2)
  65. legend('Monophasic','2-events', '3-events', '>=4 events','all')
  66. xlabel('Peak current, pA'); ylabel('Number')
  67. title('Figure 2A')
  68. % FIGURES 2C, 2D. The data are in Figure234data.mat.
  69. % Expt H is in row 6; expt T is in row 2.
  70. load Figure234data; aa = Figure234data;
  71. figure(25); clf
  72. nex = size(aa.Nevents,1);
  73. aaS = sum(aa.Nevents,2);
  74. aaPL = (ones(1,size(aa.Nevents,2))./aaS).*aa.Nevents; % Compute fractions
  75. semilogy(aa.colmns, mean(aaPL), 'k-', 'Linewidth', 2) % Mean of fractions
  76. hold on
  77. for jr=1:nex; semilogy(aa.colmns, aaPL(jr,:), 'b-'); end
  78. xlabel('Number of events'); ylabel('Fraction of EPSCs')
  79. legend('Mean', 'Indiv. expts.')
  80. title('Figure 2C')
  81. figure(26); clf
  82. MA = mean(aa.Mampl)'; % Mean amplitudes across expts
  83. plot(aa.colmns', MA, 'k-', 'Linewidth', 2); hold on
  84. mdl1 = fitnlm(aa.colmns', MA, @(b,xv)b(1)+b(2)*xv+b(3)*xv.^2, ...
  85. [MA(1) (MA(end)-MA(1))/length(MA) 1]); % Fit with a quadratic
  86. plot(aa.colmns', mdl1.Fitted, 'g--', 'LineWidth', 2) % Plot the fit
  87. for jr=1:nex; plot(aa.colmns, aa.Mampl(jr,:), 'b-'); end % Plot the data
  88. R2 = 1-mdl1.SSE/mdl1.SST; % Coefficient of determination
  89. fprintf('Fig 2D Model: Ampl = %g + %g*NEv + %g*NEv^2. R^2=%g, P=%g\n', ...
  90. mdl1.Coefficients.Estimate, R2, 1-tcdf(sqrt(R2*(nex-2)/(1-R2)),nex-2))
  91. xlabel('Number of events'); ylabel('Mean amplitude, pA')
  92. legend('Mean', 'Quad. fit', 'Indiv. expts')
  93. title('Figure 2D')
  94. % FIGURE 3A - histogram of dEN.clusArea, the areas of EPSCs.
  95. thare = -dEN.clusArea*dEN.ut_s; % EPSC amplitudes, sign changed to +,
  96. % converted to pC = pA*s
  97. % Bin edges set for 100 bins:
  98. upperlimit = 0.1*ceil(prctile(thare, 99)/0.1); bw = upperlimit/100;
  99. edges = (0:bw:upperlimit)';
  100. fprintf('bw=%g pA.\n', bw)
  101. % Plot the amplitude histograms
  102. figure(3); clf
  103. for jn=1:4
  104. if jn<4
  105. iI = find(dEN.nsigpks==jn);
  106. else
  107. iI = find(dEN.nsigpks>=4);
  108. end
  109. histogram(thare(iI), edges, 'DisplayStyle','stairs', 'LineWidth', 2)
  110. hold on
  111. end
  112. legend('Monophasic','2-events', '3-events', '>=4 events')
  113. xlabel('EPSC area, pC'); ylabel('Number')
  114. title('Figure 3A')
  115. % FIGURE 3D. The data are in Figure234data.mat.
  116. figure(36); clf
  117. MAR = mean(aa.Marea); % Mean area across expts
  118. mdl2 = fitnlm(aa.colmns, MAR, @(b,xv)b(1)+b(2)*xv, ...
  119. [MAR(1) (MAR(end)-MAR(1))/length(MAR)]); % Fit with a line
  120. plot(aa.colmns, MAR, 'k-', 'LineWidth', 2); hold on % Plot avg. data
  121. plot(aa.colmns', mdl2.Fitted, 'g--', 'LineWidth', 2) % Plot the fit
  122. for jr=1:nex; plot(aa.colmns,aa.Marea(jr,:), 'b-'); end % Plot data
  123. R2A = 1-mdl2.SSE/mdl2.SST; % Coefficient of determination
  124. fprintf('Fig 3D Model: Area = %g + %g*Nev, R^2=%g, P=%g\n', ...
  125. mdl2.Coefficients.Estimate, R2A, 1-tcdf(sqrt(R2A*(nex-2)/(1-R2A)),nex-2))
  126. xlabel('Number of events'); ylabel('EPSCarea, pC')
  127. legend('Mean', 'Indiv. expts.')
  128. title('Figure 3D')
  129. axv = axis; axis([1 axv(2) 0 axv(4)])
  130. % Expt H is in row 6; expt T is in row 2.
  131. % FIGURE 4A. The data are in Figure234data.cmNsig, not normalized by expt.
  132. % normcm is the normalizer. There are NaNs in the data, where the analysis
  133. % yielded too few samples.
  134. figure(4); clf
  135. nex = size(aa.cmNsig, 2); nec = size(aa.cmNsig, 1);
  136. cmNmean = zeros(nec,1); % Compute mean across expts
  137. for j1=1:nec
  138. cmNmean(j1) = mean(aa.cmNsig(j1,~isnan(aa.cmNsig(j1,:))));
  139. end
  140. Mcmn = cmNmean/mean(aa.normcm); % Normalize the mean
  141. plot((1:nec)', Mcmn, 'k-', 'LineWidth', 3) % Plot normalized mean across expts
  142. hold on
  143. mdl4 = fitnlm((1:nec)', Mcmn, @(b,xv)b(1)+b(2)*xv, ...
  144. [Mcmn(1) (Mcmn(end)-Mcmn(1))/nec]); % Fit with a line
  145. plot((1:nec)',mdl4.Fitted,'g--','Linewidth', 2) % Plot fit line
  146. for ju=1:nex
  147. plot((1:nec)', aa.cmNsig(:,ju)/aa.normcm(ju), 'b-') % Plot data
  148. end
  149. R2cm = 1-mdl4.SSE/mdl4.SST; % Coefficient of determination of fit
  150. fprintf('Fig 4A Model: M(nth) = %g + %g*N(N-1th), R^2=%g, P=%g\n', ...
  151. mdl4.Coefficients.Estimate, R2cm, 1-tcdf(sqrt(R2cm*(nex-2)/(1-R2cm)), ...
  152. nec-2))
  153. axv = axis; axis([0 axv(2) 0 axv(4)])
  154. xlabel('Event count in n-1''th cluster')
  155. ylabel('Mean count in n''th cluster')
  156. legend('Mean', 'Linear model', 'Data')
  157. title('Figure 4A')
  158. % FIGURE 4B.
  159. figure(45); clf
  160. iplB = find(Figure234data.intNN(:,1)<=0.1); % Only for intervals <=0.1 s
  161. [xxB,yyB,~] = slideMean(Figure234data.intNN(iplB,1), ...
  162. Figure234data.intNN(iplB,2), 9, 1); % Smooth data in sets of 9
  163. % (~9*33 original EPSCs)
  164. plot(xxB, yyB, 'k-', 'LineWidth', 2); hold on
  165. mnxxB = min(xxB); iplB2 = find(xxB<=0.02); % Model fit only 1st 0.02 s
  166. mdlB = fitnlm(xxB(iplB2)-mnxxB, yyB(iplB2), ...
  167. @(b,xv)b(1)-b(2)*exp(-xv/b(3)), [1 0.3 0.001]);
  168. R2B = 1-mdlB.SSE/mdlB.SST; % Coefficient of determination of fit
  169. fprintf('Fig 4B Model: NN = %g - %g*exp(-Int/%g), R^2=%g, P=%g\n', ...
  170. mdlB.Coefficients.Estimate, R2B, ...
  171. 1-tcdf(sqrt(R2B*(length(xxB(iplB2))-2)/(1-R2B)),nec-2))
  172. plot(xxB(iplB2), mdlB.Fitted, 'g--', 'LineWidth', 2)
  173. plot(Figure234data.intNN(iplB,1), Figure234data.intNN(iplB,2), 'o', ...
  174. 'Color', gray, 'MarkerSize',4, 'MarkerFaceColor', gray)
  175. axv = axis; axis([0 0.02 axv(3:4)])
  176. line([0 0.1], [1, 1], 'Color', 'k', 'LineStyle', '--')
  177. xlabel('Interval to prev cluster, s.')
  178. ylabel('Normalized number of events/EPSC')
  179. legend('Mean', 'Exponential model', 'Data')
  180. title('Figure 4B')
  181. mdlB % Print the model
  182. % FIGURE 4C. Data are in Figure234data.allArea. These are all the data
  183. % points plotted in Fig 4C, not separated by experiment. Similar data for
  184. % normalized amplitude versus interval are in Figure234data.allAmpl
  185. figure(46); clf
  186. ipl = find(Figure234data.intArea(:,1)<=0.1); % Only for intervals <=100 ms
  187. [xx,yy,~] = slideMean(Figure234data.intArea(ipl,1), ...
  188. Figure234data.intArea(ipl,2), 21, 0.5); % Smooth data in sets in sets
  189. % of 21 (~1000 original EPSCs)
  190. plot(xx, yy, 'b-', 'LineWidth', 2); hold on
  191. mnxx = min(xx);
  192. mdlR = fitnlm(xx-mnxx, yy, @(b,xv)b(1)-b(2)*exp(-xv/b(3)), [1 0.3 0.01]);
  193. R2R = 1-mdlR.SSE/mdlR.SST; % Coefficient of determination of fit
  194. fprintf('Fig 4C Model: Area = %g - %g*exp(-Int/%g), R^2=%g, P=%g\n', ...
  195. mdlR.Coefficients.Estimate, R2R, ...
  196. 1-tcdf(sqrt(R2R*(length(xx)-2)/(1-R2R)),nec-2))
  197. plot(xx, mdlR.Fitted, 'g--', 'LineWidth', 2)
  198. plot(Figure234data.intArea(ipl,1), Figure234data.intArea(ipl,2), '.', ...
  199. 'Color', gray)
  200. line([0 0.1], [1, 1], 'Color', 'k', 'LineStyle', '--')
  201. xlabel('Interval to prev cluster, s.')
  202. ylabel('Normalized area of cluster')
  203. legend('Mean', 'Exponential model', 'Data')
  204. title('Figure 4C')
  205. mdlR % Print the model
  206. % For the analysis of amplitude, mentioned in the ms, repeat the above
  207. % using Figure234data.intAmpl
  208. [xxM,yyM,~] = slideMean(Figure234data.intAmpl(ipl,1), ...
  209. Figure234data.intAmpl(ipl,2), 21, 0.5); % Smooth data in sets in sets
  210. mnxxM = min(xxM);
  211. mdlM = fitnlm(xxM-mnxxM, yyM, @(b,xv)b(1)-b(2)*exp(-xv/b(3)), [1 0.3 0.01]);
  212. R2M = 1-mdlM.SSE/mdlM.SST; % Coefficient of determination of fit
  213. fprintf('Not plotted, but same analysis on EPSC amplitude gives:\n')
  214. fprintf('Fig 4C Model: Ampl = %g - %g*exp(-Int/%g), R^2=%g, P=%g\n', ...
  215. mdlM.Coefficients.Estimate, R2M, ...
  216. 1-tcdf(sqrt(R2M*(length(xxM)-2)/(1-R2M)),nec-2))
  217. mdlM % Print the model
  218. % FIGURE 5A. Plot a randomly selected 10 examples of EPSCs with NEv events.
  219. % EPSCs are from the example dataset in dD, dEN.
  220. isdone = 'x';
  221. while isdone~='q' && isdone~='Q'
  222. maxntsh = 7; % Max number of examples to show
  223. axtim = 0.005; % Length of x-axis
  224. axleng = round(axtim/dEN.ut_s); % Axis length in indices for axtim s
  225. onems = round(0.001/dEN.ut_s); % 1 ms in indices
  226. NEv = input('Show EPSCs with ?? events? ');
  227. iE = find(dEN.nsigpks==NEv); % All the EPSCs with NEv events
  228. ntsh = min(maxntsh, length(iE)); % The number to show
  229. ir = randperm(length(iE)); iEP = iE(ir(1:ntsh)); % randomly choose ntsh exmpls
  230. tim = 1000*(0:dEN.ut_s:(axleng-1)*dEN.ut_s)'; % time axis, in ms
  231. figure(5); clf; ah = 0.9/ntsh;
  232. wpos = get(5,'Position'); set(5,'Position',[wpos(1) wpos(2)+wpos(4)-840 ...
  233. wpos(3) 840])
  234. for je=1:length(iEP)
  235. ithEst = dEN.iClust(iEP(je),2) - onems; % Index, start of EPSC - 1 ms
  236. ithEen = dEN.iClust(iEP(je),3) + onems; % and end of the EPSC + 1 ms
  237. ipad = length(tim) - (ithEen - ithEst + 1); % Zero padding to fill axtim s
  238. if ipad<0; ithEen = ithEen + ipad; ipad = 0; end % If long long one
  239. axes('Position', [0.1 0.96-ah*je 0.8 ah-0.02]);
  240. plot(tim, [dEN.dataOIL(ithEst:ithEen); zeros(ipad,1)], 'r-', ...
  241. 'LineWidth', 1.5); hold on
  242. plot(tim, [dD.DataDC(ithEst:ithEen); zeros(ipad,1)], 'b-', ...
  243. 'LineWidth', 1.5)
  244. plot(tim, [dEN.xsigL(ithEst:ithEen); zeros(ipad,1)], 'g--', ...
  245. 'LineWidth', 1)
  246. tm0 = ithEst*dEN.ut_s*1000 - 0.1;
  247. plot(dEN.iEvClustL{iEP(je)}(:,1)*dEN.ut_s*1000-tm0, dEN.PeakAreas{iEP(je)}, ...
  248. 'go', 'MarkerSize', 4, 'MarkerFaceColor', 'g')
  249. if je==1
  250. legend('Fit waveform','Data','Deconv. signal','Event ampls', ...
  251. 'AutoUpdate','off')
  252. end
  253. line([0 axtim], [0 0], 'Color', 'k', 'LineWidth', 0.8)
  254. if je==length(iEP)
  255. xlabel('Time, ms'); ylabel('EPSC, pA')
  256. end
  257. end
  258. isdone = input('Quit, Again? ','s'); isdone = isdone(1);
  259. end
  260. return
  261. function [xxo, yyo, Nadv] = slideMean(xxe,yye,N,Fadv)
  262. % SLIDEMEAN - for each N (xx,yy) points in order sort(xx), compute
  263. % xxo=mean(xx) and yyo=mean(yy), advance by (Fadv*N) points between computes.
  264. % [xxo, yyo, Nadv] = slideMean(xx,yy,N,Fadv);
  265. % Fadv =0.5 is 50% overlap, =1 is independent samples, =0 is a bad idea.
  266. [xx,iso] = sort(xxe); yy = yye(iso); % Sort data along x axis
  267. Nadv = round(max(Fadv*N, N/10)); % Number of points to advance between bins
  268. xxo = []; yyo = [];
  269. i1 = 1; i2 = N;
  270. while i2<=length(xx)
  271. xxo = [xxo; mean(xx(i1:i2))];
  272. yyo = [yyo; mean(yy(i1:i2))];
  273. i1 = i1 + Nadv; i2 = i2 + Nadv;
  274. end
  275. return