check_snr_over_sessions.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. function check_snr_over_sessions(fld,monkey,plot_responses)
  2. %
  3. % this function explores some information about the channels and their SNR.
  4. % it first concatenates MP, which is created in
  5. % analyse_individual_channels.m. it chucks out channels that never see a
  6. % stimulus, and it plots the distribution of SNR over sessions. it can plot
  7. % the average responses for individual sessions and individual channels,
  8. % but this is pretty time consuming, because data needs to be loaded.
  9. %% find which data to include
  10. fprintf('\n======================================================\n');
  11. fprintf(['-- Running check_snr_over_sessions for ' monkey ' --\n']);
  12. fprintf('======================================================\n');
  13. datainfo = session_info();
  14. if strcmp(monkey,'M1')
  15. info = datainfo(1);
  16. else
  17. info = datainfo(2);
  18. end
  19. sessions = info.dates;
  20. datadir = fld.procdatadir;
  21. MP = get_mp(fld.basedir,monkey,sessions);
  22. %% inspect SNR
  23. chan = findgroups(MP.chan_id);
  24. snr = splitapply(@nanmean,MP.snr,chan);
  25. snr_std = splitapply(@nanstd,MP.snr,chan);
  26. numses = splitapply(@length,MP.snr,chan);
  27. disp(['out of ' num2str(size(MP,1)) ' rows, ' num2str(sum(MP.snr>2.5)) ...
  28. ' have SNR bigger than 2.5, ' num2str(sum(MP.snr>1)) ' bigger than 1']);
  29. figure; set(gcf,'Position',[129 547 1097 500]);
  30. boxplot(MP.snr,MP.chan_id);hold all
  31. p2 = plot([1 length(chan)],[2.5 2.5],'r');
  32. p1 = plot([1 length(chan)],[1 1],'g');
  33. set(gca,'YScale','log');
  34. title(['SNR per channel for ' monkey ', ' ...
  35. num2str(length(unique(MP.session_id))) ' sessions']);
  36. chans = unique(MP.chan_id);
  37. %% let's try to get the mean response per channel per day
  38. if plot_responses
  39. cnt = 1;
  40. mns = []; mns_lut = [];
  41. for ch = chans'
  42. disp(['loading channel ' num2str(ch)]);
  43. for sid = 1:length(sessions)
  44. % session info
  45. session = sessions{sid};
  46. q = strsplit(session,'_');
  47. cdate = q{2};
  48. block = str2num(q{3}(end-4));
  49. % load the data
  50. session_id = [monkey '_' cdate '_' num2str(block)];
  51. sessiondir = fullfile(datadir, monkey, session_id);
  52. channeldir = fullfile(sessiondir, 'individual_channels');
  53. chanfile = fullfile(channeldir, ...
  54. ['Env_preprocessed_ch' num2str(ch) '_' session_id '.mat']);
  55. if ~exist(chanfile)
  56. continue
  57. end
  58. load(chanfile);
  59. load(fullfile(sessiondir, [session_id '_LUT.mat']));
  60. % calculate the average response to when the target is in the RF
  61. inclTrls = lut.TarPos==1 & strcmp(lut.Status,'Target');
  62. % store in a matrix
  63. mns(cnt,:) = nanmean(data(:,inclTrls),2)';
  64. mns_lut(cnt,:) = [ch, sid];
  65. cnt = cnt + 1;
  66. end
  67. end
  68. save([monkey '_mns_per_channel_per_day.mat'],'mns','mns_lut','MP');
  69. %% plot average response per session
  70. cols = ceil(sqrt(length(chans)));
  71. rows = ceil(length(chans)/cols);
  72. for sid = 1:length(sessions)
  73. session = sessions{sid};
  74. q = strsplit(session,'_');
  75. cdate = q{2};
  76. block = str2num(q{3}(end-4));
  77. figure; set(gcf,'Position',[92 475 1055 620])
  78. cnt = 1;
  79. for ch = chans'
  80. subplot(rows,cols,cnt);
  81. q = mns_lut(:,1)==ch & mns_lut(:,2)==sid;
  82. mn = mns(q,:);
  83. % find snr in this session
  84. q = MP.session_id==sid & MP.chan_id==ch;
  85. snr = MP.snr(q);
  86. % plot and set title
  87. if snr>2.5
  88. plot(mn,'r');
  89. elseif snr>1
  90. plot(mn,'g');
  91. else
  92. plot(mn,'k');
  93. end
  94. title(['snr:' num2str(snr)]);
  95. cnt = cnt + 1;
  96. end
  97. mtit([monkey ' ' cdate '\_B' num2str(block)],'xoff',0,'yoff',.025);
  98. end
  99. %% plot average across sessions
  100. figure; set(gcf,'Position',[92 475 1055 620])
  101. cnt = 1;
  102. for ch = chans'
  103. % get means from sessions higher than snr 2.5
  104. inclSessions = MP.session_id(MP.chan_id==ch & MP.snr>2.5);
  105. snrs = MP.snr(MP.chan_id==ch & MP.snr>1);
  106. q = mns_lut(:,1)==ch & ismember(mns_lut(:,2),inclSessions);
  107. mn = nanmean(mns(q,:));
  108. subplot(rows,cols,cnt);
  109. plot(mn);
  110. title(['snr:' num2str(round(mean(snrs)*100)/100) ' #' ...
  111. num2str(length(inclSessions))]);
  112. ylim([0 1]);
  113. cnt = cnt + 1;
  114. end
  115. end
  116. %% make a bar plot to indicate how many sessions would be included per channel
  117. % for different levels of SNR.
  118. threshes = [1 2 2.5];
  119. figure; set(gcf,'Position',[83 500 1180 452]);
  120. for ti = 1:length(threshes)
  121. thres = threshes(ti);
  122. incl = MP.snr>thres;
  123. q = MP(incl,:);
  124. [chid, inclch] = findgroups(q.chan_id);
  125. session_per_chan = splitapply(@length,q.session_id,chid);
  126. subplot(1,length(threshes),ti);
  127. b = bar(session_per_chan);
  128. set(gca,'XTick',[1:length(inclch)],'XTickLabel',inclch);
  129. xtickangle(90);
  130. title(['SNR>' num2str(thres) ', #' ...
  131. num2str(sum(session_per_chan>5)) ' chans>5 days'],'fontsize',9);
  132. ylabel('#sessions that meet SNR criterion');
  133. xlabel('channel id');
  134. end
  135. mtit([monkey],'xoff',0,'yoff',0.035);