SSA_multibird_pipeline.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. %%% SSA multibird summary
  2. % to save time in analysis, we run this code one time after running the
  3. % prelim pipeline on each bird. the result is large multi-bird data
  4. % structures summarizing SSA data
  5. %
  6. % nb, this is now very slow because we grab response vectors for each unit
  7. the_good_birds = {'D:\ssa_expmts\pipelines\pipeline_9059_210811_LH_NCM_g0.mat',
  8. 'D:\ssa_expmts\pipelines\pipeline_9072_210810_LH_NCM_g0_v2.mat',
  9. 'D:\ssa_expmts\pipelines\pipeline_9074_210811_LH_NCM_g0.mat'};
  10. pipeline_output_save_target = 'D:\ssa_expmts\multibird_summary_ssa.mat';
  11. total_motif_resp = [];
  12. total_motif_resp_mu = [];
  13. total_wfs = [];
  14. num_goods = [];
  15. num_goods_mu = [];
  16. big_rv1 = [];
  17. big_rv2 = [];
  18. big_rv3 = [];
  19. big_rv4 = [];
  20. for big_bird_num = 1:length(the_good_birds)
  21. clearvars -except big_bird_num the_good_birds ...
  22. pipeline_output_save_target total_motif_resp total_motif_resp_mu...
  23. total_wfs num_goods num_goods_mu big_rv1 big_rv2 big_rv3 big_rv4
  24. close all
  25. this_bird = the_good_birds{big_bird_num};
  26. load(this_bird)
  27. disp(['starting bird ' num2str(big_bird_num)])
  28. total_motif_resp = [total_motif_resp all_motif_resp(:,good_unit_ids)];
  29. total_motif_resp_mu...
  30. = [total_motif_resp_mu all_motif_resp_mu(:,good_unit_ids_mu)];
  31. total_wfs = [total_wfs; best_wfs(good_unit_ids, 1:81)];
  32. num_goods = [num_goods; length(good_unit_ids)];
  33. num_goods_mu = [num_goods_mu; length(good_unit_ids_mu)];
  34. % now get response vectors for single units
  35. resp_vect_win = .05; %50 ms gauss window - see what others do though
  36. npoints = resp_vect_win*fs;
  37. resp_vect_filt = gausswin(npoints); %alpha 2.5
  38. bin_win = round(0.05*fs);
  39. bin_step = round(0.01*fs);
  40. tape_lengths = zeros(4,1);
  41. for songIdx = 1:4
  42. tape_lengths(songIdx) = length(1:bin_step:(round(motif_length(songIdx)*fs)-bin_win)+1);
  43. end
  44. impt_motifs = [1 125 501 550;...
  45. 126 250 551 600;...
  46. 251 375 601 650;...
  47. 376 500 651 700];
  48. resp_vector_s1 = zeros(length(good_unit_ids), 175, tape_lengths(1));
  49. resp_vector_s2 = zeros(length(good_unit_ids), 175, tape_lengths(2));
  50. resp_vector_s3 = zeros(length(good_unit_ids), 175, tape_lengths(3));
  51. resp_vector_s4 = zeros(length(good_unit_ids), 175, tape_lengths(4));
  52. for songIdx = 1:4
  53. this_motif_length = motif_length(songIdx);
  54. this_win_frame = [0 this_motif_length]; % seconds
  55. this_impt_line = impt_motifs(songIdx,:);
  56. these_motif_starts_1 = motifStarts(this_impt_line(1):this_impt_line(2));
  57. these_motif_starts_2 = motifStarts(this_impt_line(3):this_impt_line(4));
  58. these_motif_starts = [these_motif_starts_1'; these_motif_starts_2'];
  59. eval(['this_sType_str = ''s' num2str(songIdx) ''';'])
  60. these_good_units = sp.cids(find(sp.cgs==2)); %single units
  61. these_ids = good_unit_ids;
  62. eval(['this_resp_vector = resp_vector_' this_sType_str ';'])
  63. for unitIdx = 1:length(these_ids)
  64. this_unit_id = these_ids(unitIdx);
  65. disp(['song' num2str(songIdx) ', unit' num2str(unitIdx) ' of ' num2str(length(these_ids))])
  66. theseSpikes = sp.st(sp.clu==these_good_units(this_unit_id));
  67. for trialIdx = 1:175
  68. this_start = these_motif_starts(trialIdx);
  69. this_response_window = this_start + this_win_frame;
  70. relevant_spikes = find(theseSpikes<this_response_window(2) & theseSpikes>this_response_window(1)); % indices
  71. goodSpikes = round(theseSpikes(relevant_spikes)*fs) - round(this_start*fs); % samples
  72. this_train = zeros(round(motif_length(songIdx)*fs),1);
  73. this_train(goodSpikes+1) = 1;
  74. smooth_train = conv(this_train, resp_vect_filt, 'same');
  75. bin_starts = 1:bin_step:(length(smooth_train)-bin_win+1);
  76. bin_ends = bin_starts+bin_win-1;
  77. this_tape = zeros(length(bin_starts),1);
  78. for bin_idx = 1:length(bin_starts)
  79. this_window = [bin_starts(bin_idx) bin_ends(bin_idx)];
  80. these_resps = sum(smooth_train(this_window(1):this_window(2)));
  81. this_tape(bin_idx) = these_resps;
  82. end
  83. this_resp_vector(unitIdx,trialIdx,:) = this_tape; % doesn't actually change size - i was clever with evals
  84. eval(['resp_vector_' this_sType_str '= this_resp_vector;'])
  85. end
  86. end
  87. end
  88. big_rv1 = cat(1,big_rv1, resp_vector_s1);
  89. big_rv2 = cat(1,big_rv2, resp_vector_s2);
  90. big_rv3 = cat(1,big_rv3, resp_vector_s3);
  91. big_rv4 = cat(1,big_rv4, resp_vector_s4);
  92. disp('bird done')
  93. end
  94. clearvars -except pipeline_output_save_target total_motif_resp...
  95. total_motif_resp_mu total_wfs motifInfo num_goods num_goods_mu ...
  96. big_rv1 big_rv2 big_rv3 big_rv4
  97. save(pipeline_output_save_target)
  98. disp('ssa across birds complete!')