figure_neurotrace_prepare.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. function figure_neurotrace_prepare(fld,monkey,do_plots)
  2. fprintf('\n======================================================\n');
  3. fprintf(['-- Running figure_neurotrace_prepare for ' monkey ' --\n']);
  4. fprintf('======================================================\n');
  5. % settings
  6. snrthres = 2.5;
  7. daythres = 3;
  8. green = [23, 105, 13]./255;
  9. red = [201, 0, 34]./255;
  10. cols = [green; 0.3 0.3 0.3; red];
  11. %% find which data to include
  12. datainfo = session_info();
  13. if strcmp(monkey,'M1')
  14. info = datainfo(1);
  15. else
  16. info = datainfo(2);
  17. end
  18. sessions = info.dates;
  19. MP = get_mp(fld,monkey,sessions);
  20. %% set SNR threshold and find channels and sessions
  21. % limit MP to snr threshold
  22. incl = MP.snr>snrthres;
  23. MP_ = MP; % copy for backup
  24. % only include channels that have at least 5 sessions
  25. MP = MP(incl,:);
  26. [chid, ch_list] = findgroups(MP.chan_id);
  27. session_per_chan = splitapply(@length,MP.session_id,chid);
  28. % make a list of channels that we will include
  29. chans = ch_list(session_per_chan>daythres);
  30. %% initialize a figure if necessary
  31. if do_plots
  32. figure; set(gcf,'Position',[43 476 1197 622]);
  33. rws = ceil(sqrt(length(chans)));
  34. cls = ceil(length(chans)/rws);
  35. end
  36. %% load and concatenate the data we need
  37. datadir = fld.procdatadir;
  38. % first loop all channels
  39. numtrls = [];
  40. traces = [];
  41. tracesLUT = [];
  42. grandtraces = [];
  43. grandtracesLUT = [];
  44. cnt=1;
  45. for ch = chans'
  46. disp(['analysing channel ' num2str(ch) ...
  47. ', ' num2str(cnt) '/' num2str(size(chans,1))]);
  48. cnt=cnt+1;
  49. chidx = find(chans==ch);
  50. incl_sessions = MP.chan_id==ch & MP.snr>snrthres;
  51. cur_mp = MP(incl_sessions,:);
  52. numtrls(chidx) = sum(cellfun(@sum,cur_mp.goodtrials));
  53. % look at each session for each channel individually
  54. env = []; clut = [];
  55. for sid = 1:size(cur_mp,1)
  56. % session info
  57. cdate = cur_mp.date{sid};
  58. block = cur_mp.block(sid);
  59. % load the data
  60. session_id = [monkey '_' cdate '_' num2str(block)];
  61. sessiondir = fullfile(datadir, monkey, session_id);
  62. channeldir = fullfile(sessiondir, 'individual_channels');
  63. chanfile = fullfile(channeldir, ...
  64. ['Env_preprocessed_ch' num2str(ch) '_' session_id '.mat']);
  65. if ~exist(chanfile)
  66. continue
  67. end
  68. % load the data for this session for this channel
  69. load(chanfile); % loads 'data'
  70. load(fullfile(sessiondir, [session_id '_LUT.mat'])); % loads 'lut'
  71. load(fullfile(sessiondir, [session_id '.mat']),'env_t');
  72. % this is overwritten every time but it's always the same so who cares
  73. env = [env; data'];
  74. clut = [clut; lut];
  75. end
  76. lut = clut;
  77. % include only correct trials, in which the respective stimulus was in
  78. % the RF
  79. correct = strcmp(lut.Status,'Target');
  80. goodtrials = [cur_mp.sample_wise_goodtrials{:}]';
  81. targetInRF = lut.TarPos==MP.rfpos(ch);
  82. nontargInRF = lut.TarPos~=MP.rfpos(ch) & lut.DistPos~=MP.rfpos(ch);
  83. distInRF = lut.DistPos==MP.rfpos(ch);
  84. condincl = {targetInRF, nontargInRF, distInRF};
  85. % store in traces, this is the concatenated average
  86. for cond = 1:3
  87. incl = correct & goodtrials & condincl{cond};
  88. mn = nanmean(env(incl,:));
  89. traces(end+1,:) = mn;
  90. tracesLUT(end+1,:) = [ch,cond,sum(incl)];
  91. end
  92. % store in grandtraces, this is the average of the session average
  93. do_sessions = unique(lut.session);
  94. for sid = 1:length(do_sessions)
  95. this_session = strcmp(lut.session,do_sessions{sid});
  96. for cond = 1:3
  97. incl = this_session & correct & goodtrials & condincl{cond};
  98. mn = nanmean(env(incl,:));
  99. grandtraces(end+1,:) = mn;
  100. grandtracesLUT(end+1,:) = [ch,cond,sum(incl),sid];
  101. end
  102. end
  103. % do analysis for individual channels
  104. if do_plots
  105. subplot(rws,cls,find(chans==ch));
  106. anova_data = [];
  107. anova_group = [];
  108. for cond = 1:3
  109. incl = correct & goodtrials & condincl{cond};
  110. win_idx = find(env_t>0.15 & env_t<0.20);
  111. anova_data = [anova_data; nanmean(env(incl,win_idx),2)]; % window average for anova
  112. anova_group = [anova_group; repmat(cond,sum(incl),1)];
  113. mn = nanmean(env(incl,:));
  114. sem = nanstd(env(incl,:))./sqrt(sum(incl));
  115. t2 = [env_t, fliplr(env_t)];
  116. inBetween = [mn+sem, fliplr(mn-sem)];
  117. fill(t2, inBetween, 'g','LineStyle','none','FaceColor',...
  118. cols(cond,:),'FaceAlpha',0.3); hold all
  119. plot(env_t,mn,'Color',cols(cond,:));
  120. end
  121. [p,tbl,stats] = anova1(anova_data,anova_group,'off');
  122. xlim([0 .25]);
  123. title(['ch' num2str(ch) ', p=' num2str(round(p*100)/100) ...
  124. ', ' num2str(length(do_sessions)) ' days'],'FontSize',8);
  125. end
  126. end
  127. % save output to make a combined plot of all channels
  128. savedir = fullfile(fld.basedir,'results','figure_neurotrace');
  129. save(fullfile(savedir, [monkey '_averages_snr' num2str(snrthres) ...
  130. '_mindays' num2str(daythres) '.mat']));
  131. if do_plots
  132. savefig(fullfile(savedir, [monkey '_channelwise']));
  133. end