figure_neuropriming_prepare.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. function figure_neuropriming_prepare(fld,monkey)
  2. fprintf('\n======================================================\n');
  3. fprintf(['-- Running neuropriming for ' monkey ' --\n']);
  4. fprintf('======================================================\n');
  5. % settings
  6. snrthres = 2.5;
  7. daythres = 3;
  8. cols = [1 0 0;...
  9. 0 0 0;...
  10. 0 1 0];
  11. do_cleantraces = true; % if true we throw out artefact traces
  12. %% find which data to include
  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. MP = get_mp(fld,monkey,sessions);
  21. %% set SNR threshold and find channels and sessions
  22. % limit MP to snr threshold
  23. incl = MP.snr>snrthres;
  24. MP_ = MP; % copy for backup
  25. % only include channels that have at least 5 sessions
  26. MP = MP(incl,:);
  27. [chid, ch_list] = findgroups(MP.chan_id);
  28. session_per_chan = splitapply(@length,MP.session_id,chid);
  29. % make a list of channels that we will include
  30. chans = ch_list(session_per_chan>daythres);
  31. %% load and concatenate the data we need
  32. datadir = fld.procdatadir;
  33. % first loop all channels
  34. numtrls = [];
  35. targmod = []; targmag = [];
  36. distmod = []; distmag = [];
  37. tracesLUT = [];
  38. for ch = chans'
  39. chidx = find(chans==ch);
  40. disp(['analysing channel ' num2str(ch) ', ' ...
  41. num2str(chidx) '/' num2str(size(chans,1))]);
  42. incl_sessions = MP.chan_id==ch & MP.snr>snrthres;
  43. cur_mp = MP(incl_sessions,:);
  44. numtrls(chidx) = sum(cellfun(@sum,cur_mp.goodtrials));
  45. % look at each session for each channel individually
  46. env = []; clut = [];
  47. for sid = 1:size(cur_mp,1)
  48. % session info
  49. cdate = cur_mp.date{sid};
  50. block = cur_mp.block(sid);
  51. % load the data
  52. session_id = [monkey '_' cdate '_' num2str(block)];
  53. sessiondir = fullfile(datadir, monkey, session_id);
  54. channeldir = fullfile(sessiondir, 'individual_channels');
  55. chanfile = fullfile(channeldir, ['Env_preprocessed_ch' ...
  56. num2str(ch) '_' session_id '.mat']);
  57. if ~exist(chanfile)
  58. continue
  59. end
  60. % load the data for this session for this channel
  61. load(chanfile); % loads 'data'
  62. load(fullfile(sessiondir, [session_id '_LUT.mat'])); % loads 'lut'
  63. load(fullfile(sessiondir, [session_id '.mat']),'env_t');
  64. % this is overwritten every time but it's always the same so who cares
  65. if do_cleantraces % throw out artefact traces
  66. prewin = env_t > -0.1 & env_t < 0;
  67. postwin = env_t > 0 & env_t < 0.1;
  68. trace = mean(data');
  69. sd_pre = std(trace(prewin));
  70. m_post = mean(trace(postwin));
  71. if m_post < 3*sd_pre
  72. data = nan(size(data));
  73. end
  74. end
  75. env = [env; data'];
  76. clut = [clut; lut];
  77. end
  78. lut = clut;
  79. % add a shapeswitch column, because apparently no one ever looked at
  80. % this before
  81. shapeSwitch = zeros(size(lut,1),1);
  82. shapeSwitch(1) = nan;
  83. shapeSwitch(abs(lut.TarShape - [nan; lut.TarShape(1:end-1)]) > 0) = 1;
  84. lut = addvars(lut, shapeSwitch,'after','colSwitch');
  85. targetInRF = lut.TarPos==MP.rfpos(ch);
  86. nontargInRF = lut.TarPos~=MP.rfpos(ch) & lut.DistPos~=MP.rfpos(ch);
  87. distInRF = lut.DistPos==MP.rfpos(ch);
  88. goodtrials = [cur_mp.sample_wise_goodtrials{:}]';
  89. for shapeswitch = [0 1]
  90. incl = lut.TarChoice==lut.TarPos & ...
  91. targetInRF & goodtrials & lut.shapeSwitch==shapeswitch;
  92. mn1 = nanmean(env(incl,:));
  93. % same, when a non-target is in the RF
  94. incl = lut.TarChoice==lut.TarPos & ...
  95. nontargInRF & goodtrials & lut.shapeSwitch==shapeswitch;
  96. mn2 = nanmean(env(incl,:));
  97. % same, for distractor
  98. incl = lut.TarChoice==lut.TarPos & ...
  99. distInRF & goodtrials & lut.shapeSwitch==shapeswitch;
  100. mn3 = nanmean(env(incl,:));
  101. % store the difference in traces
  102. targmod(end+1,:) = mn1-mn2;
  103. distmod(end+1,:) = mn3-mn2;
  104. targmag(end+1,:) = mn1;
  105. distmag(end+1,:) = mn3;
  106. tracesLUT(end+1,:) = [ch,shapeswitch,nan,sum(incl)];
  107. end
  108. for colorswitch = [0 1]
  109. incl = lut.TarChoice==lut.TarPos & ...
  110. targetInRF & goodtrials & lut.colSwitch==colorswitch;
  111. mn1 = nanmean(env(incl,:));
  112. % same, when a non-target is in the RF
  113. incl = lut.TarChoice==lut.TarPos & ...
  114. nontargInRF & goodtrials & lut.colSwitch==colorswitch;
  115. mn2 = nanmean(env(incl,:));
  116. % same, for distractor
  117. incl = lut.TarChoice==lut.TarPos & ...
  118. distInRF & goodtrials & lut.colSwitch==colorswitch;
  119. mn3 = nanmean(env(incl,:));
  120. % store the difference in traces
  121. targmod(end+1,:) = mn1-mn2;
  122. distmod(end+1,:) = mn3-mn2;
  123. targmag(end+1,:) = mn1;
  124. distmag(end+1,:) = mn3;
  125. tracesLUT(end+1,:) = [ch,nan,colorswitch,sum(incl)];
  126. end
  127. end
  128. tracesLUT = array2table(tracesLUT,'VariableNames',{'channel',...
  129. 'shapeswitch','colorswitch','numtrials'});
  130. % save output to make a combined plot of all channels
  131. savedir = fullfile(fld.basedir,'results','figure_neuropriming');
  132. save(fullfile(savedir, [monkey '_averages_snr' num2str(snrthres) ...
  133. '_mindays' num2str(daythres) '.mat']));