Interactions.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. % routine to calculate the results for Figure3&4, requires the results from
  2. % Ensemble_detection routine. it loads the raw data and results from the
  3. % Ensemble_detection and calculates the cross/auto correlograms and the
  4. % synch index
  5. %------------------------------------------------------------------------
  6. % parameters:
  7. % binsize: binsize used for binning spike times.
  8. % windows: how many steps in each direction to calculate the correlogram
  9. % ---------------------------------------------------------
  10. % important outputs:
  11. % xcorr_sig_big: cell array of size # of rats, inside of each binary matrices of
  12. % size # of cross-correlograms and length(windows) + 1. each 1 means a
  13. % significant excitatory interaction
  14. % xcorr_sig_small: cell array of size # of rats, inside of each binary matrices of
  15. % size # of cross-correlograms and length(windows) + 1. each 1 means a
  16. % significant inhibitory interaction
  17. % synch_index: synchrony index (refere to paper)
  18. % acorr_sig_big: like the xcorr_sig_big for autocorrelation
  19. % acorr_sig_small: like the xcorr_sig_small for autocorrelation
  20. clear all
  21. clc
  22. close all
  23. filepath = fileparts(pwd);
  24. addpath(strcat(filepath,filesep,'Data',filesep));
  25. addpath(strcat(filepath,filesep,'Results',filesep));
  26. addpath(strcat(filepath,filesep,'Aux_functions',filesep));
  27. %% load the results
  28. load('All_Coeff.mat');
  29. load('Counted_Spikes.mat');
  30. load('raw_data.mat');
  31. Number_of_rats = length(AllSpikes);
  32. rat_id = unique(recording_id,'stable');
  33. binsize = 100; % binsize that was used in msec;
  34. %% Determining the pairs correlograms
  35. windows = [-10 10]; % how many time steps to go
  36. merge = cell(1,Number_of_rats);
  37. xcorr_sig_big = cell(1,Number_of_rats);
  38. xcorr_sig_small = cell(1,Number_of_rats);
  39. sig_count = 0;
  40. total_number_of_xcorr = 0;
  41. cross_corr_all = cell(1,Number_of_rats);
  42. avg_rand_cross_all = cell(1,Number_of_rats);
  43. pairwise_max_all = cell(1,Number_of_rats);
  44. pairwise_min_all = cell(1,Number_of_rats);
  45. for i=1:Number_of_rats
  46. Number_of_ensembles = size(Activation_coeff_th{i},2);
  47. Number_of_xcorr = (Number_of_ensembles*(Number_of_ensembles-1))/2;
  48. merge{i} = zeros(Number_of_ensembles);
  49. total_number_of_xcorr = total_number_of_xcorr + ((Number_of_ensembles)*(Number_of_ensembles-1))/2;
  50. count = 1;
  51. for j=1:Number_of_ensembles
  52. time_coeff_1 = Activation_coeff_th{i}(:,j);
  53. idx_act1 = find(time_coeff_1 == 1);
  54. ts1 = (idx_act1 - 1);
  55. for k=j+1:Number_of_ensembles
  56. time_coeff_2 = Activation_coeff_th{i}(:,k);
  57. idx_act2 = find(time_coeff_2 == 1);
  58. ts2 = (idx_act2 - 1);
  59. [tsOffsets] = crosscorrelogram(ts1, ts2, windows);
  60. tsOffsets_rand = [];
  61. h = [];
  62. bintimes = (-10.5:1:10.5);
  63. for l=1:1000
  64. ts_rand1 = ts1;
  65. ts_rand2 = sort(ts2 + randi([-10 10],size(ts2)));
  66. tsOffsets_rand = crosscorrelogram(ts_rand1,ts_rand2,windows);
  67. [h(l,:),edges] = histcounts(tsOffsets_rand,bintimes);
  68. end
  69. avg_rand_cross = mean(h);
  70. pairwise_max = prctile(h,99);
  71. pairwise_min = prctile(h,1);
  72. x = movsum(edges,2)/2;
  73. x(1) = [];
  74. cross_counts = histcounts(tsOffsets,edges);
  75. cross_corr_all{i}(count,:) = cross_counts;
  76. avg_rand_cross_all{i}(count,:) = avg_rand_cross;
  77. pairwise_max_all{i}(count,:) = pairwise_max;
  78. pairwise_min_all{i}(count,:) = pairwise_min;
  79. if (cross_counts(11) > pairwise_max(11)); merge{i}(j,k) = 1; end
  80. xcorr_sig_big{i}(count,:) = cross_counts > pairwise_max;
  81. xcorr_sig_small{i}(count,:) = cross_counts < pairwise_min;
  82. count = count + 1;
  83. if sum(cross_counts > pairwise_max) > 0 || sum(cross_counts < pairwise_min) > 0
  84. sig_count = sig_count + 1;
  85. end
  86. end
  87. end
  88. end
  89. %% reporting results of significant interactions
  90. time_inhibit = zeros(1,21);
  91. time_excite = zeros(1,21);
  92. for i=1:Number_of_rats
  93. tmp_small = double(xcorr_sig_small{i});
  94. time_inhibit = time_inhibit + sum(tmp_small);
  95. tmp_big = double(xcorr_sig_big{i});
  96. time_excite = time_excite + sum(tmp_big);
  97. end
  98. subplot(2,1,1);bar(time_inhibit,'BarWidth',1);xlabel('Time (S)','FontSize',12);
  99. ylabel('Significant interactions','FontSize',12);xlim([0,22]);
  100. set(gca,'xtick',[1,8,11,14,21],'xticklabels',{'-1','-0.3','0','0.3','1'},'Fontsize',12);
  101. title('Inhibitory Interactions');axis square
  102. subplot(2,1,2);bar(time_excite,'BarWidth',1);xlabel('Time (S)','FontSize',12);
  103. ylabel('Significant interactions','FontSize',12);xlim([0,22]);
  104. set(gca,'xtick',[1,4,11,18,21],'xticklabels',{'-1','-0.7','0','0.7','1'},'Fontsize',12);
  105. title('Excitatory Interactions'); axis square
  106. %% measuring relative number of coincidents interactions at zero to total number of activations
  107. number_0_xcorr = 0;
  108. total_number_of_xcorr = 0;
  109. count = 1;
  110. synch_index = [];
  111. for i=1:Number_of_rats
  112. tmp_xcorr = merge{i};
  113. number_0_xcorr = number_0_xcorr + length(find(tmp_xcorr == 1));
  114. total_number_of_xcorr = total_number_of_xcorr + (length(tmp_xcorr)*(length(tmp_xcorr)-1))/2;
  115. tmp_0 = find(tmp_xcorr == 1);
  116. for j=1:length(tmp_0)
  117. [r,c] = ind2sub(size(tmp_xcorr),tmp_0(j));
  118. for k=1:length(r)
  119. time_coeff_1 = Activation_coeff_th{i}(:,r(k));
  120. for l=1:length(c)
  121. time_coeff_2 = Activation_coeff_th{i}(:,c(l));
  122. synch_index(count) = (2*(sum(time_coeff_1.*time_coeff_2))/(sum(time_coeff_1) + sum(time_coeff_2)))*100;
  123. count = count + 1;
  124. end
  125. end
  126. end
  127. end
  128. figure()
  129. histogram(synch_index);xlabel('synch index (%)');xline(mean(synch_index),'--r','mean','LineWidth',1);axis square
  130. ylabel('counts')
  131. % save_dir = strcat(pwd,'/results/');
  132. % save_file = strcat(save_dir,'cross_interactions.mat');
  133. % save(save_file,'avg_rand_cross_all'',cross_corr_all','pairwise_max_all','pairwise_min_all','sig_count','total_number_of_xcorr','xcorr_sig_big','xcorr_sig_small');
  134. %% Measuring signiicant autocorrelograms
  135. windows = [-10 10]; % how many time steps to go
  136. acorr_sig_big = cell(1,Number_of_rats);
  137. acorr_sig_small = cell(1,Number_of_rats);
  138. total_number_of_acorr = 0;
  139. signif_count = 0;
  140. pairwise_max_all = cell(1,Number_of_rats);
  141. pairwise_min_all = cell(1,Number_of_rats);
  142. auto_corr_all = cell(1,Number_of_rats);
  143. avg_rand_auto_all = cell(1,Number_of_rats);
  144. for i=1:Number_of_rats
  145. Number_of_ensembles = size(Activation_coeff_th{i},2);
  146. Number_of_acorr = Number_of_ensembles;
  147. count = 1;
  148. for j=1:Number_of_ensembles
  149. time_coeff_1 = Activation_coeff_th{i}(:,j);
  150. idx_act1 = find(time_coeff_1 == 1);
  151. ts1 = (idx_act1 - 1);
  152. [tsOffsets] = crosscorrelogram(ts1, ts1, windows);
  153. tsOffsets_rand = [];
  154. bintimes = (-10.5:1:10.5);
  155. for l=1:1000
  156. ts_rand1 = sort(ts1 + randi([-10 10],size(ts1)));
  157. tsOffsets_rand = crosscorrelogram(ts_rand1,ts_rand1,windows);
  158. [h(l,:),edges] = histcounts(tsOffsets_rand,bintimes);
  159. end
  160. avg_rand_auto = mean(h);
  161. pairwise_max = prctile(h,99);
  162. pairwise_min = prctile(h,1);
  163. x = movsum(edges,2)/2;
  164. x(1) = [];
  165. se_rand_auto = std(h);
  166. cross_counts = histcounts(tsOffsets,edges);
  167. avg_rand_auto_all{i}(count,:) = avg_rand_auto;
  168. pairwise_max_all{i}(count,:) = pairwise_max;
  169. pairwise_min_all{i}(count,:) = pairwise_min;
  170. auto_corr_all{i}(count,:) = cross_counts;
  171. cross_counts(11) = avg_rand_auto(11); %not to counting the zero interactions
  172. total_number_of_acorr = total_number_of_acorr + 1;
  173. acorr_sig_big{i}(count,:) = cross_counts > pairwise_max;
  174. acorr_sig_small{i}(count,:) = cross_counts < pairwise_min;
  175. count = count + 1;
  176. if sum(cross_counts > pairwise_max) > 0 || sum(cross_counts < pairwise_min) > 0
  177. signif_count = signif_count + 1;
  178. end
  179. end
  180. end
  181. time_inhibit = zeros(1,21);
  182. time_excite = zeros(1,21);
  183. for i=1:length(acorr_sig_big)
  184. tmp_small = double(acorr_sig_small{i});
  185. time_inhibit = time_inhibit + sum(tmp_small);
  186. tmp_big = double(acorr_sig_big{i});
  187. time_excite = time_excite + sum(tmp_big);
  188. end
  189. figure()
  190. subplot(2,1,1);bar(time_inhibit(11:end),'BarWidth',1);xlabel('Time (S)','FontSize',12);
  191. ylabel('Significant interactions','FontSize',12);xlim([0,11]);
  192. set(gca,'xtick',[1,6,11],'xticklabels',{'0','0.5','1'},'Fontsize',12);
  193. title('Inhibitory Interactions');axis square
  194. subplot(2,1,2);bar(time_excite(11:end),'BarWidth',1);xlabel('Time (S)','FontSize',12);
  195. ylabel('Significant interactions','FontSize',12);xlim([0,11]);
  196. set(gca,'xtick',[1,6,11],'xticklabels',{'0','0.5','1'},'Fontsize',12);
  197. title('Excitatory Interactions'); axis square
  198. % save_dir = strcat(pwd,'/results/');
  199. % save_file = strcat(save_dir,'auto_interactions.mat');
  200. % save(save_file,'avg_rand_auto_all'',auto_corr_all','pairwise_max_all','pairwise_min_all','sig_count','total_number_of_acorr','acorr_sig_big','acorr_sig_small');