ibi_multibird_pipeline.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. %%% ibi 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 timing response data
  5. the_good_birds = {'D:\ibi_expmts\pipelines\pipeline_9131_220227_RH_NCM_g0.mat',
  6. 'D:\ibi_expmts\pipelines\pipeline_9138_220228_RH_NCM_g0.mat',
  7. 'D:\ibi_expmts\pipelines\pipeline_9329_220224_RH_NCM_g0.mat'};
  8. pipeline_output_save_target = 'D:\ibi_expmts\multibird_summary_ibi.mat';
  9. total_motif_resp = [];
  10. total_motif_resp_mu = [];
  11. total_wfs = [];
  12. num_goods = [];
  13. num_goods_mu = [];
  14. big_ibi_tensor = [];
  15. big_ibi_frac = [];
  16. for big_bird_num = 1:length(the_good_birds)
  17. clearvars -except big_bird_num the_good_birds ...
  18. pipeline_output_save_target total_motif_resp total_motif_resp_mu...
  19. total_wfs num_goods num_goods_mu big_ibi_tensor big_ibi_frac
  20. close all
  21. this_bird = the_good_birds{big_bird_num};
  22. load(this_bird)
  23. disp(['starting bird ' num2str(big_bird_num)])
  24. total_motif_resp = [total_motif_resp all_motif_resp(:,good_unit_ids)];
  25. total_motif_resp_mu...
  26. = [total_motif_resp_mu all_motif_resp_mu(:,good_unit_ids_mu)];
  27. total_wfs = [total_wfs; best_wfs(good_unit_ids, 1:81)];
  28. num_goods = [num_goods; length(good_unit_ids)];
  29. num_goods_mu = [num_goods_mu; length(good_unit_ids_mu)];
  30. % IBI tensors for summary plotting
  31. motif_ids = stim_timeline.motifs{1,:};
  32. ibi_ids = find(contains(motif_ids,'IBI'));
  33. key_motifs = [1 2751; 1251 2801];
  34. ibi_tags = timeline.motifs(ibi_ids);
  35. ibi_tags_s1 = ibi_tags(1:250);
  36. ibi_tags_s2 = ibi_tags(251:500);
  37. ibi_types = unique(ibi_tags);
  38. % songs 1 and 2
  39. ibi_tensor_raw_su = zeros(2,length(good_unit_ids),25,10);
  40. ibi_tensor_raw_mu = zeros(2,length(good_unit_ids_mu),25,10);
  41. ibi_tensor_frac_su = zeros(2,length(good_unit_ids),25,10);
  42. ibi_tensor_frac_mu = zeros(2,length(good_unit_ids_mu),25,10);
  43. ctrl_tensor_raw_su = ibi_tensor_raw_su;
  44. ctrl_tensor_raw_mu = ibi_tensor_raw_su;
  45. ctrl_tensor_frac_su = ibi_tensor_raw_su;
  46. ctrl_tensor_frac_mu = ibi_tensor_raw_su;
  47. for uType = [1,2]
  48. if uType == 1
  49. this_uType_str = 'mu';
  50. these_good_units = good_unit_ids_mu;
  51. these_motif_resp = all_motif_resp_mu;
  52. this_tensor_raw = ibi_tensor_raw_mu;
  53. this_tensor_frac = ibi_tensor_frac_mu;
  54. this_ctrl_raw = ctrl_tensor_raw_mu;
  55. this_ctrl_frac = ctrl_tensor_frac_mu;
  56. elseif uType == 2
  57. this_uType_str = 'su';
  58. these_good_units = good_unit_ids;
  59. these_motif_resp = all_motif_resp;
  60. this_tensor_raw = ibi_tensor_raw_su;
  61. this_tensor_frac = ibi_tensor_frac_su;
  62. this_ctrl_raw = ctrl_tensor_raw_su;
  63. this_ctrl_frac = ctrl_tensor_frac_su;
  64. end
  65. for songIdx = 1:2 % only first two songs are important
  66. tkm = key_motifs(songIdx,:);
  67. these_frac_inc_values = zeros(length(these_good_units), 250); % if error, numBouts is not 25
  68. these_raw_inc_values = zeros(length(these_good_units), 250);
  69. these_dev_values = zeros(length(these_good_units), 250);
  70. bout_nums = [ceil(tkm(1)/5) ceil(tkm(1)/5)+249];
  71. these_IBIs = diff(cell2mat(bout_starts(bout_nums(1):bout_nums(2)))); % make sure these line up!
  72. % first IBI follows first bout
  73. for unitIdx = 1:length(these_good_units)
  74. disp([this_uType_str ', song' num2str(songIdx) ', unit' num2str(unitIdx)])
  75. ibi_counts = ones(length(ibi_types),1);
  76. disp([this_uType_str num2str(unitIdx) ' of ' num2str(length(these_good_units)) ', Song ' num2str(songIdx)])
  77. this_id = these_good_units(unitIdx);
  78. this_resp_p1 = these_motif_resp(tkm(1):tkm(1)+1249, this_id);
  79. this_resp_p2 = these_motif_resp(tkm(2):tkm(2)+49, this_id);
  80. this_max_p1 = max(this_resp_p1);
  81. num_bouts = length(this_resp_p1)/5;
  82. for bIdx = 1:num_bouts
  83. this_first_motif = (bIdx-1)*5 + 1;
  84. if bIdx > 1
  85. this_ibi = ibi_tags(((songIdx-1)*250)+bIdx-1);
  86. this_ibi_idx = find(strcmp(ibi_types,this_ibi));
  87. ibi_tape_num = ibi_counts(this_ibi_idx);
  88. this_resp = this_resp_p1(this_first_motif:this_first_motif+4);
  89. last_resp = this_resp_p1(this_first_motif-5:this_first_motif-1);
  90. raw_ib_rbd = this_resp(1) - last_resp(end);
  91. frac_ib_rbd = raw_ib_rbd / this_max_p1;
  92. ctrl_raw_rbd = this_resp(2) - last_resp(end);
  93. ctrl_frac_rbd = this_resp(2) - last_resp(end);
  94. this_tensor_raw(songIdx,unitIdx,ibi_tape_num,this_ibi_idx) = raw_ib_rbd;
  95. this_tensor_frac(songIdx,unitIdx,ibi_tape_num,this_ibi_idx) = frac_ib_rbd;
  96. this_ctrl_raw(songIdx,unitIdx,ibi_tape_num,this_ibi_idx) = ctrl_raw_rbd;
  97. this_ctrl_frac(songIdx,unitIdx,ibi_tape_num,this_ibi_idx) = ctrl_frac_rbd;
  98. ibi_counts(this_ibi_idx) = ibi_counts(this_ibi_idx) + 1;
  99. end
  100. end
  101. end
  102. end
  103. eval(['ibi_tensor_raw_' this_uType_str '= this_tensor_raw;'])
  104. eval(['ibi_tensor_frac_' this_uType_str '= this_tensor_frac;'])
  105. eval(['ctrl_tensor_raw_' this_uType_str '= this_ctrl_raw;'])
  106. eval(['ctrl_tensor_frac_' this_uType_str '= this_ctrl_frac;'])
  107. end
  108. big_ibi_tensor = cat(2, big_ibi_tensor, ibi_tensor_raw_su);
  109. big_ibi_frac = cat(2, big_ibi_frac, ibi_tensor_frac_su);
  110. % final shape is (song id X unit num X rep id X ibi id)
  111. disp('bird done')
  112. end
  113. clearvars -except pipeline_output_save_target total_motif_resp...
  114. total_motif_resp_mu total_wfs motifInfo num_goods num_goods_mu ...
  115. big_ibi_tensor big_ibi_frac
  116. save(pipeline_output_save_target)
  117. disp('ibi data across birds complete!')