ssa_rebound_fig3_habituation.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. % figure 3 - habituation
  2. %% prep and set variables
  3. clear
  4. close all
  5. pipeline_out = 'D:\ssa_expmts\multibird_summary_ssa.mat';
  6. raster_bird = [2, 3];
  7. raster_unit = [8, 22];
  8. raster_song = [3, 2];
  9. % 9072 su008, song 3 (raster ok)
  10. % 9072 su039, song 1 (raster good)
  11. % 9059 su070, song 4 (raster just fine)
  12. % 9074 su022, song 2 (raster good)
  13. curve_bird = [1, 2, 2];
  14. curve_unit = [65, 4, 22];
  15. curve_song = [4, 2, 1];
  16. %% example rasters
  17. for ex_idx = 1:length(raster_bird)
  18. ex_bird = raster_bird(ex_idx);
  19. ex_unit = raster_unit(ex_idx);
  20. ex_song = raster_song(ex_idx);
  21. switch ex_bird
  22. case 1
  23. load('D:\ssa_expmts\pipelines\pipeline_9059_210811_LH_NCM_g0.mat')
  24. case 2
  25. load('D:\ssa_expmts\pipelines\pipeline_9072_210810_LH_NCM_g0_v2.mat')
  26. case 3
  27. load('D:\ssa_expmts\pipelines\pipeline_9074_210811_LH_NCM_g0.mat')
  28. end
  29. raster_win = [-0.025 0.2];
  30. impt_trials = [1 116;...
  31. 126 241;...
  32. 251 366;...
  33. 376 491];
  34. songIdx = ex_song;
  35. this_mot_len = (motifInfo{impt_trials(songIdx,1),3} - motifInfo{impt_trials(songIdx,1),1}) + 0.2;
  36. this_rast_win = raster_win + [0 this_mot_len];
  37. ats_1 = motifInfo(impt_trials(songIdx,1):impt_trials(songIdx,1)+9);
  38. ats_2 = motifInfo(impt_trials(songIdx,2):impt_trials(songIdx,2)+9);
  39. these_ats = cell2mat([ats_1'; ats_2']);
  40. these_gu = sp.cids(find(sp.cgs==uType));
  41. these_spikes = sp.st(sp.clu == these_gu(ex_unit));
  42. ssa_rebound_rasters_v2(these_spikes, these_ats, ex_unit, this_rast_win, micData, dataset_name, 'su')
  43. h5 = gcf;
  44. print(['figure_pieces/fig3_habRasterEx' '_' num2str(ex_idx)], '-dsvg', '-r300')
  45. saveas(h5, ['figure_pieces/fig3_habRasterEx' '_' num2str(ex_idx) '.fig'])
  46. end
  47. %% example curve trajectories
  48. load(pipeline_out)
  49. for ex_idx = 1:length(curve_bird)
  50. ex_bird = curve_bird(ex_idx);
  51. ex_unit = curve_unit(ex_idx);
  52. ex_song = curve_song(ex_idx);
  53. switch ex_bird
  54. case 1
  55. these_units = 1:num_goods(1);
  56. case 2
  57. these_units = num_goods(1)+1:num_goods(1)+num_goods(2);
  58. case 3
  59. these_units = num_goods(2)+1:sum(num_goods);
  60. end
  61. this_unit = these_units(ex_unit);
  62. relevant_motif_chunks = [1 5; 6 25; 26 120; 121 125;];
  63. line_colors = {'r-', 'm-', 'b-', 'k-'};
  64. eval(['this_resp_vector = big_rv' num2str(ex_song) ';'])
  65. this_resp_piece = squeeze(this_resp_vector(this_unit,:,:));
  66. % this_resp_piece = this_resp_piece ./ max(this_resp_piece);
  67. for lineIdx = 1:4
  68. impt_motifs = relevant_motif_chunks(lineIdx,:);
  69. good_motifs = this_resp_piece(impt_motifs(1):impt_motifs(2),:);
  70. this_line = mean(good_motifs,1);
  71. max_line_val = max(max_line_val, max(this_line));
  72. plot(this_line, line_colors{lineIdx})
  73. if lineIdx == 1
  74. hold on;
  75. elseif lineIdx == 4
  76. hold off;
  77. end
  78. end
  79. title(['Bird ' num2str(ex_bird) ', Unit ' num2str(ex_unit) ', Song' num2str(songIdx)])
  80. ylabel('Resp. Str.')
  81. xlabel('Time')
  82. h5 = gcf;
  83. print(['figure_pieces/fig3_habCurveEx' '_' num2str(ex_idx)], '-dsvg', '-r300')
  84. saveas(h5, ['figure_pieces/fig3_habCurveEx' '_' num2str(ex_idx) '.fig'])
  85. end
  86. %% gain model calculation and plotting
  87. % goodness of fit, calculated as the max of the xcorr of the two sequences.
  88. % then, also take peak lag (and track those just in case)
  89. %
  90. % Still tracking best habituation?
  91. %%
  92. gain_vals = {};
  93. err_vals = {};
  94. bof_vals = {};
  95. lag_vals = {};
  96. for song_idx = 1:4
  97. eval(['this_rv = big_rv' num2str(song_idx) ';'])
  98. rv_inits = squeeze(mean(this_rv(:,1:5,:),2));
  99. rv_ends = squeeze(mean(this_rv(:,121:125,:),2));
  100. rv_long = [rv_inits rv_ends];
  101. max_r = max(rv_long,[],2);
  102. rv_inits = rv_inits ./ max_r;
  103. rv_ends = rv_ends ./ max_r;
  104. gain_checks = 0:0.01:3;
  105. this_gain_vals = [];
  106. this_err_vals = [];
  107. this_bof_vals = [];
  108. this_lag_vals = [];
  109. for unit_idx = 1:length(big_rv1(:,1,1))
  110. c1 = rv_inits(unit_idx,:);
  111. c2 = rv_ends(unit_idx,:);
  112. zero_lag = length(c1);%+1;
  113. auto1 = xcorr(c1);
  114. auto2 = xcorr(c2);
  115. auto_prod = auto1(zero_lag)*auto2(zero_lag);
  116. norm_factor = 1/sqrt(auto_prod);
  117. xc = xcorr(c1, c2) .* norm_factor;
  118. bof = xc(zero_lag);
  119. [bof,lag] = max(xc);
  120. lag = lag - zero_lag;
  121. best_err = 100; % maximum conceivable is just number of timesteps, <100
  122. best_gain = 0;
  123. for gain_idx = 1:length(gain_checks)
  124. c1x = c1*gain_checks(gain_idx);
  125. this_err = sum((c1x - c2).^2);
  126. if this_err < best_err
  127. best_err = this_err;
  128. best_gain = gain_checks(gain_idx);
  129. end
  130. end
  131. % great for visualizing individual examples
  132. % figure(9999);
  133. % plot(c1, 'r-')
  134. % hold on;
  135. % plot(c2, 'k-')
  136. % plot(c1*best_gain, 'r--')
  137. % hold off;
  138. if best_err<100
  139. this_gain_vals = [this_gain_vals; best_gain];
  140. this_err_vals = [this_err_vals; best_err];
  141. this_bof_vals = [this_bof_vals; bof];
  142. this_lag_vals = [this_lag_vals; lag];
  143. end
  144. % this_bof_vals = [this_bof_vals; bof];
  145. % this_lag_vals = [this_lag_vals; lag];
  146. end
  147. gain_vals{end+1} = this_gain_vals;
  148. err_vals{end+1} = this_err_vals;
  149. bof_vals{end+1} = this_bof_vals;
  150. lag_vals{end+1} = this_lag_vals;
  151. figure(91919)
  152. subplot(2,2,song_idx)
  153. histogram(this_bof_vals)%, 'BinEdges', 0:0.25:15)
  154. title('X-Corr Values')
  155. figure(91920)
  156. subplot(2,2,song_idx)
  157. histogram(this_gain_vals)
  158. title('Gain Values')
  159. % just for visualization - not included in dissertation
  160. figure(91921+song_idx)
  161. scatterhist(this_bof_vals, this_gain_vals,'Marker','.')
  162. title('Comparisons')
  163. xlabel('Xcorr Values')
  164. ylabel('Optimal Gain')
  165. end
  166. figure(91919)
  167. h5 = gcf;
  168. print(['figure_pieces/fig3_xcorrVals'], '-dsvg', '-r300')
  169. saveas(h5, ['figure_pieces/fig3_xcorrVals.fig'])
  170. figure(91920)
  171. h5 = gcf;
  172. print(['figure_pieces/fig3_gainVals'], '-dsvg', '-r300')
  173. saveas(h5, ['figure_pieces/fig3_gainVals.fig'])