Ensemble_cortical_relation.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. % routine to show ensemble LFP relation to generate fig6,8 and Sfig4, requires the results from
  2. % Ensemble_detection routine, LFP_calculations
  3. % --------------------------------------------
  4. % parameters:
  5. % K_opt: # of clusters
  6. % distance_opt: distance to use for clustering
  7. % important outputs:
  8. % idx_clus: clustering index for all the ensembles
  9. % idx_clus_synch: clustering index for all the pairs with the significant
  10. % zero lag interaction
  11. clear all
  12. clc
  13. close all
  14. filepath = fileparts(pwd);
  15. addpath(strcat(filepath,filesep,'Data',filesep));
  16. addpath(strcat(filepath,filesep,'Results',filesep));
  17. addpath(strcat(filepath,filesep,'Aux_functions',filesep));
  18. %% load data
  19. load('spectrogram_modulation.mat');
  20. S_to_cluster = zeros(size(S_all,1),size(S_all,2)*size(S_all,3));
  21. for i=1:size(S_all,1)
  22. tmp = squeeze(S_all(i,:,:));
  23. S_to_cluster(i,:) = tmp(:);
  24. end
  25. %% Finding the optimum parameters of clustering
  26. center = 0;
  27. %[K_opt,distance_opt] = kmeans_param_opt(S_to_cluster,center); % if u want
  28. %to generate the Sfig4
  29. K_opt = 4;
  30. distance_opt = 'correlation';
  31. %% Final Clustering and visualizing the results
  32. [idx_clus,~,~] = kmeans(S_to_cluster,K_opt,'Replicates',10,'Start','plus','MaxIter',10000,'Distance',distance_opt,'EmptyAction','drop');
  33. % load('Spectrograms_clustering_indices.mat'); % if u want the exact
  34. % results in the paper load this
  35. cmap = [-0.03 0.03]; % mapping for the colors
  36. figure()
  37. sgtitle('Average Spectrograms of Ensembles Inside Each Cluster','Fontsize',14)
  38. for i=1:K_opt
  39. tmp_cluster_eucl = S_to_cluster(idx_clus == i,:);
  40. tmp_belong_clusters = reshape(tmp_cluster_eucl,[size(tmp_cluster_eucl,1),length(f),length(t)]);
  41. Avg_S_clusters = squeeze(mean(tmp_belong_clusters,1));
  42. subplot(2,ceil(K_opt/2),i);hndl = imagesc(Avg_S_clusters);caxis(cmap);
  43. xticks([]);yticks([]);title(strcat('Cluster: ',num2str(i)),'FontSize',12)
  44. end
  45. subplot(2,2,1);ylabel('Frequency (Hz)','FontSize',12);yticks(sort(36-[3,8,14,24]));yticklabels({'100','60','30','12'})
  46. subplot(2,2,3);ylabel('Frequency (Hz)','FontSize',12);yticks(sort(36-[3,8,14,24]));yticklabels({'100','60','30','12'})
  47. subplot(2,2,3);xlabel('Time (ms)','FontSize',12);xticks([1,21,31,41,61]);xticklabels({'-300','-100','0','100','300'})
  48. subplot(2,2,4);xlabel('Time (ms)','FontSize',12);xticks([1,21,31,41,61]);xticklabels({'-300','-100','0','100','300'})
  49. % save_dir = strcat(pwd,'/results/');
  50. % save_file = strcat(save_dir,'Spectrograms_clustering_indices.mat');
  51. % save(save_file,'idx_clus');
  52. %% Significane assessment
  53. p_val_uncorrected_all = zeros([K_opt,size(S_all,2),size(S_all,3)]);
  54. h_uncorrected_all = zeros([K_opt,size(S_all,2),size(S_all,3)]);
  55. h_corrected_all = zeros([K_opt,size(S_all,2),size(S_all,3)]);
  56. for i=1:K_opt
  57. S_temp = S_all(idx_clus == i,:,:);
  58. for j=1:size(S_temp,2)
  59. for k=1:size(S_temp,3)
  60. [p_val_uncorrected_all(i,j,k),h_uncorrected_all(i,j,k)] = signrank(squeeze(S_temp(:,j,k)));
  61. end
  62. end
  63. h_corrected_all(i,:,:) = stat_fdr_bh(p_val_uncorrected_all(i,:,:));
  64. end
  65. %% Visualizing significancy
  66. figure('units','normalized','outerposition',[0 0 1 1]);
  67. for i=1:K_opt
  68. set(gcf,'papertype','a4');
  69. tmp_cluster_eucl = S_to_cluster(idx_clus == i,:);
  70. tmp_belong_clusters = reshape(tmp_cluster_eucl,[size(tmp_cluster_eucl,1),length(f),length(t)]);
  71. Avg_S_clusters = squeeze(mean(tmp_belong_clusters,1));
  72. h_prod = squeeze(h_corrected_all(i,:,:));
  73. h_prod(h_prod == 0) = nan;
  74. to_plot = Avg_S_clusters.*h_prod;
  75. subplot(2,ceil(K_opt/2),i);
  76. hndl = imagesc(to_plot);caxis(cmap);
  77. set(hndl,'AlphaData',~isnan(to_plot));xline(31,'r','linewidth',4);
  78. xticks([]);yticks([]);
  79. ylabel('Frequency (Hz)','FontSize',12);yticks(sort(36-[3,8,14,24]));yticklabels({'100','60','30','12'});ax = gca;ax.FontSize = 12;
  80. xlabel('Time (ms)','FontSize',12);xticks([1,21,31,41,61]);xticklabels({'-300','-100','0','100','300'});ax = gca;ax.FontSize = 12;
  81. set(gca,'fontsize',12,'fontname','arial');
  82. colorbar('Ticks',[-0.03,0,0.03],'TickLabels',{'-0.03','0','0.03'})
  83. end
  84. subplot(2,2,1);ylabel('Frequency (Hz)','FontSize',30);yticks(sort(36-[3,8,14,24]));yticklabels({'100','60','30','12'});ax = gca;ax.FontSize = 24;
  85. subplot(2,2,3);ylabel('Frequency (Hz)','FontSize',30);yticks(sort(36-[3,8,14,24]));yticklabels({'100','60','30','12'});ax = gca;ax.FontSize = 24;
  86. subplot(2,2,3);xlabel('Time (ms)','FontSize',30);xticks([1,21,31,41,61]);xticklabels({'-300','-100','0','100','300'});ax = gca;ax.FontSize = 24;
  87. subplot(2,2,4);xlabel('Time (ms)','FontSize',30);xticks([1,21,31,41,61]);xticklabels({'-300','-100','0','100','300'});ax = gca;ax.FontSize = 24;
  88. hp4 = get(subplot(2,2,4),'Position');
  89. colorbar('Position', [hp4(1)+hp4(3)+0.03 hp4(2) 0.02 hp4(4)],'Ticks',[-0.03,0,0.03],'TickLabels',{'-0.03','0','0.03'})
  90. %% Band assessment per cluster
  91. bands = {[4,8],[8,12],[12,30],[30,70],[70,150]};
  92. bands_name = {'theta','alpha','beta','low-gamma','high-gamma'};
  93. n_bands = length(bands);
  94. to_bar = zeros(K_opt,n_bands);
  95. err_bar = to_bar;
  96. f_idx = f(end:-1:1);
  97. p_val_band = zeros(n_bands,K_opt);
  98. for clst=1:K_opt
  99. for band=1:n_bands
  100. idx_f = f_idx >=bands{band}(1) & f_idx <bands{band}(2);
  101. S_tmp = mean(mean(S_all(idx_clus == clst,idx_f,31:end),3,'omitnan'),2,'omitnan');
  102. to_bar(clst,band) = mean(S_tmp);
  103. err_bar(clst,band) = std(S_tmp)./sqrt(length(S_tmp));
  104. p_val_band(band,clst) = signrank(S_tmp);
  105. end
  106. p_val_band(:,clst) = bonf_holm(p_val_band(:,clst));
  107. end
  108. figure()
  109. barsem(to_bar',err_bar');ylim([-0.03,0.03])
  110. %% synch spectrogram clustering
  111. load('spectrogram_modulation_synch.mat');
  112. S_to_cluster_synch = zeros(size(S_all_synch,1),size(S_all_synch,2)*size(S_all_synch,3));
  113. for i=1:size(S_all_synch,1)
  114. tmp = squeeze(S_all_synch(i,:,:));
  115. S_to_cluster_synch(i,:) = tmp(:);
  116. end
  117. eva = evalclusters(S_to_cluster_synch,'kmeans','silhouette','KList',[1:6],'Distance','correlation','ClusterPriors','equal');
  118. K_opt = eva.OptimalK;distance_opt = 'correlation';
  119. [idx_clus_synch,~,~] = kmeans(S_to_cluster_synch,K_opt,'Replicates',10,'Start','plus','MaxIter',10000,'Distance',distance_opt,'EmptyAction','drop');
  120. %load('Spectrograms_clustering_indices_synch,mat'); % for the exact results
  121. %of the paper
  122. p_val_uncorrected_all = zeros([K_opt,size(S_all_synch,2),size(S_all_synch,3)]);
  123. h_uncorrected_all = zeros([K_opt,size(S_all_synch,2),size(S_all_synch,3)]);
  124. h_corrected_all = zeros([K_opt,size(S_all_synch,2),size(S_all_synch,3)]);
  125. for i=1:K_opt
  126. S_temp = S_all_synch(idx_clus_synch == i,:,:);
  127. for j=1:size(S_temp,2)
  128. for k=1:size(S_temp,3)
  129. [p_val_uncorrected_all(i,j,k),h_uncorrected_all(i,j,k)] = signrank(squeeze(S_temp(:,j,k)));
  130. end
  131. end
  132. h_corrected_all(i,:,:) = stat_fdr_bh(p_val_uncorrected_all(i,:,:));
  133. end
  134. figure()
  135. set(gcf,'papertype','a4');
  136. for i=1:K_opt
  137. tmp_cluster = S_to_cluster_synch(idx_clus_synch == i,:);
  138. tmp_belong_clusters = reshape(tmp_cluster,[size(tmp_cluster,1),length(f),length(t)]);
  139. Avg_S_clusters = squeeze(mean(tmp_belong_clusters,1));
  140. h_prod = squeeze(h_corrected_all(i,:,:));
  141. h_prod(h_prod == 0) = nan;
  142. to_plot = Avg_S_clusters.*h_prod;
  143. subplot(2,ceil(K_opt/2),i);
  144. hndl = imagesc(to_plot);caxis([-1 1]*0.03);
  145. set(hndl,'AlphaData',~isnan(to_plot));xline(31,'r','linewidth',4);
  146. xticks([]);yticks([]);
  147. ylabel('Frequency (Hz)','FontSize',12);yticks(sort(36-[3,8,14,24]));yticklabels({'100','60','30','12'});ax = gca;ax.FontSize = 12;
  148. xlabel('Time (ms)','FontSize',12);xticks([1,21,31,41,61]);xticklabels({'-300','-100','0','100','300'});ax = gca;ax.FontSize = 12;
  149. set(gca,'fontsize',12,'fontname','arial');
  150. colorbar('Ticks',[-0.03,0,0.03],'TickLabels',{'-0.03','0','0.03'})
  151. end
  152. %% reverse correlation scatter
  153. load('reverse_correlation_modulation.mat');
  154. load('reverse_correlation_modulation_sh.mat');
  155. n_rats = length(reverse_corr_modulation);
  156. bands_name = {'theta','low-gamma','high-gamma'};
  157. n_bands = length(bands_name);
  158. len_before = 0.3;
  159. len_after = 0.4;
  160. Fs = 1082.25;
  161. t = -len_before:1/Fs:len_after;
  162. t(1) = [];
  163. bands_to_investigate = [1,4,5];
  164. to_scatter = [];
  165. to_scatter_sh = [];
  166. for band=1:n_bands
  167. count = 1;
  168. for i=1:n_rats
  169. data_rat = reverse_corr_modulation{i};
  170. data_rat_sh = reverse_corr_modulation_sh{i};
  171. n_ensembles = length(data_rat);
  172. for j=1:n_ensembles
  173. data_ensemble = data_rat{j};
  174. data_ensemble_sh = data_rat_sh{j};
  175. data_band = data_ensemble{bands_to_investigate(band)};
  176. data_band_sh = data_ensemble_sh{bands_to_investigate(band)};
  177. to_scatter(count,band) = mean(data_band(t>0));
  178. to_scatter_sh(count,band) = mean(mean(data_band_sh(t>0,:)));
  179. count = count + 1;
  180. end
  181. end
  182. end
  183. figure('units','normalized','outerposition',[0 0 1 1]);
  184. count = 1;
  185. for band_1 = 1:n_bands
  186. for band_2 = band_1+1:n_bands
  187. subplot(1,3,count);
  188. tmp = [to_scatter(:,band_1),to_scatter(:,band_2)];
  189. top_right = round(sum(tmp(:,1)>0 & tmp(:,2)>0)./length(tmp)*100);
  190. bot_left = round(sum(tmp(:,1)<0 & tmp(:,2)<0)./length(tmp)*100);
  191. top_left = round(sum(tmp(:,1)<0 & tmp(:,2)>0)./length(tmp)*100);
  192. bot_right = round(sum(tmp(:,1)>0 & tmp(:,2)<0)./length(tmp)*100);
  193. scatter(to_scatter(:,band_1),to_scatter(:,band_2),'x')
  194. hold all
  195. scatter(to_scatter_sh(:,band_1),to_scatter_sh(:,band_2),'x')
  196. xlabel(bands_name{band_1});ylabel(bands_name{band_2})
  197. xline(0,'--r');yline(0,'--r');
  198. count = count + 1;
  199. axis square;xlim([-0.01,0.01]);ylim([-0.01,0.01]);
  200. text(min(xlim), min(ylim), strcat(num2str(bot_left),'%'))
  201. text(min(xlim), max(ylim), strcat(num2str(top_left),'%'))
  202. text(max(xlim), min(ylim), strcat(num2str(bot_right),'%'))
  203. text(max(xlim), max(ylim), strcat(num2str(top_right),'%'))
  204. end
  205. end