rsa_16x16mat_singletrial.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. clear all;
  2. RandStream.setGlobalStream(RandStream('mt19937ar','seed',sum(100*clock)))
  3. mainfolder=''
  4. addpath(genpath(['subfunctions\']))
  5. %loading list of participants (good perfomance)
  6. load('good_partis.mat')
  7. partis=good_partis;
  8. partis_sub=good_partis_sub;
  9. %% Configuration of each iteration
  10. delay=1; %(1 or 2). This variable set everything for analysing in different epochs (relative to delay 1 or 2)
  11. removingalarms=0; % Removing epochs that includes or were preceded by an alarm message (1=yes, 0=no)
  12. % selecting stimulus of interest for each iteration (1 = stim presented 1st. 2 stim presented 2nd )
  13. conf.ooi{1}=1;
  14. conf.ooi{2}=1;
  15. conf.ooi{3}=2;
  16. conf.ooi{4}=2;
  17. conf.ooi{5}=1;
  18. conf.ooi{6}=1;
  19. conf.ooi{7}=2;
  20. conf.ooi{8}=2;
  21. conf.ooi{9}=1;
  22. conf.ooi{10}=1;
  23. conf.ooi{11}=2;
  24. conf.ooi{12}=2;
  25. conf.ooi{13}=1;
  26. conf.ooi{14}=2;
  27. if delay== 1
  28. if removingalarms
  29. foldpath='results/noalarm/mean_recentered_th100_messagesOFF/euclideanmodels';
  30. else
  31. foldpath='results/mean_recentered_th100_messagesOFF/euclideanmodels';
  32. end
  33. elseif delay==2
  34. if removingalarms
  35. foldpath='results/noalarm/mean_recentered_th100_messagesOFF/euclideanmodels/2nd_cue';
  36. else
  37. foldpath='results/mean_recentered_th100_messagesOFF/euclideanmodels/2nd_cue';
  38. end
  39. end
  40. %selecting the same of the folder to save the output in each iteration.
  41. conf.folder{1} = [foldpath '/single_trial_RSA_first_check_object_1_cued_Pearson/'];
  42. conf.folder{2} = [foldpath '/single_trial_RSA_first_check_object_1_uncued_Pearson/'];
  43. conf.folder{3} = [foldpath '/single_trial_RSA_first_check_object_2_cued_Pearson/'];
  44. conf.folder{4} = [foldpath '/single_trial_RSA_first_check_object_2_uncued_Pearson/'];
  45. conf.folder{5} = [foldpath '/single_trial_RSA_first_check_object_1_cued_cat_udlr_Pearson/'];
  46. conf.folder{6} = [foldpath '/single_trial_RSA_first_check_object_1_uncued_cat_udlr_Pearson/'];
  47. conf.folder{7} = [foldpath '/single_trial_RSA_first_check_object_2_cued_cat_udlr_Pearson/'];
  48. conf.folder{8} = [foldpath '/single_trial_RSA_first_check_object_2_uncued_cat_udlr_Pearson/'];
  49. conf.folder{9} = [foldpath '/single_trial_RSA_first_check_object_1_cued_cat_udlr45_Pearson/'];
  50. conf.folder{10} = [foldpath '/single_trial_RSA_first_check_object_1_uncued_cat_udlr45_Pearson/'];
  51. conf.folder{11} = [foldpath '/single_trial_RSA_first_check_object_2_cued_cat_udlr45_Pearson/'];
  52. conf.folder{12} = [foldpath '/single_trial_RSA_first_check_object_2_uncued_cat_udlr45_Pearson/'];
  53. conf.folder{13} = [foldpath '/single_trial_RSA_first_check_object_1_Pearson/'];
  54. conf.folder{14} = [foldpath '/single_trial_RSA_first_check_object_2_Pearson/'];
  55. %do we need to create this folders? uncomment this:
  56. % for fo = 1:length(conf.folder)
  57. %
  58. % papa=foldpath;
  59. % foi=conf.folder{fo};
  60. % hijo=foi(length(papa)+1:end)
  61. % mkdir(papa, hijo)
  62. % end
  63. %selecting the cue in each iteration
  64. conf.retrooi{1}=1;
  65. conf.retrooi{2}=2;
  66. conf.retrooi{3}=2;
  67. conf.retrooi{4}=1;
  68. conf.retrooi{5}=1;
  69. conf.retrooi{6}=2;
  70. conf.retrooi{7}=2;
  71. conf.retrooi{8}=1;
  72. conf.retrooi{9}=1;
  73. conf.retrooi{10}=2;
  74. conf.retrooi{11}=2;
  75. conf.retrooi{12}=1;
  76. conf.retrooi{13}=[];
  77. conf.retrooi{14}=[];
  78. %selection the model for each iteration
  79. conf.gmodelmat{1}=creatcontmodel(1);
  80. conf.gmodelmat{2}=creatcontmodel(1);
  81. conf.gmodelmat{3}=creatcontmodel(1);
  82. conf.gmodelmat{4}=creatcontmodel(1);
  83. conf.gmodelmat{5}=creatcatcardeucl;
  84. conf.gmodelmat{6}=creatcatcardeucl;
  85. conf.gmodelmat{7}=creatcatcardeucl;
  86. conf.gmodelmat{8}=creatcatcardeucl;
  87. conf.gmodelmat{9}=creatcatcardeucl45;
  88. conf.gmodelmat{10}=creatcatcardeucl45;
  89. conf.gmodelmat{11}=creatcatcardeucl45;
  90. conf.gmodelmat{12}=creatcatcardeucl45;
  91. conf.gmodelmat{13}=creatcontmodel(1);
  92. conf.gmodelmat{14}=creatcontmodel(1);
  93. %% Starting the loop for all analyses
  94. for ii=1:14
  95. %% Parameters to be modify per analysis
  96. %folder to save results
  97. folder = conf.folder{ii}
  98. %object of interest (first of second
  99. ooi=conf.ooi{ii};
  100. %info about the first retrocue (selecting only trials based on the
  101. %retrocue)
  102. if ii>=13 %in the last iteration we do the analysis independently of the cue
  103. selectingretrocue=0 %(1==true, 0==false)
  104. else
  105. selectingretrocue=1 %(1==true, 0==false)
  106. end
  107. %retrocue of interest
  108. retrooi=conf.retrooi{ii}
  109. %info about the analysis
  110. analysis_info = ''
  111. %selecting the model that we will use
  112. modelmat=conf.gmodelmat{ii}
  113. %%
  114. for ppp=1:length(partis) %loop of participants
  115. %% Loading dataset
  116. if delay== 1
  117. toload =[mainfolder 'eyelink_preprocessed/mean_recentered_th100/object_1_onset_11_seconds/' partis{ppp,:} '.mat']; %for 1st delay
  118. elseif delay==2
  119. toload =[mainfolder 'eyelink_preprocessed/mean_recentered_th100/2nd_cue_onset_17_5__seconds/' partis{ppp,:} '.mat']; %for 2nd delay
  120. end
  121. load(toload)
  122. %Loading logfiles (outputs from PsychToolBox)
  123. subtable=[];
  124. if strcmp( partis_sub{ppp,:},'p11')
  125. load([mainfolder 'logfiles/resultfile_p11_table_repaired.mat'])
  126. subtable=sub
  127. else
  128. sub = tdfread([mainfolder 'logfiles/resultfile_' partis_sub{ppp,:} '.txt'],'tab'); %Logfile for this participant
  129. subtable=struct2table(sub);
  130. end
  131. %% marking whether trials contains a FIXATE! message before the retention period of interest
  132. subtable.delay1_invalid(1:size(subtable,1))=0;
  133. subtable.delay2_invalid(1:size(subtable,1))=0;
  134. for tr= 1:size(subtable,1)
  135. if subtable.object_null_1(tr)==1|subtable.object_null_2(tr)==1|subtable.delay_1_null(tr)==1
  136. subtable.delay1_invalid(tr)=1;
  137. end
  138. if subtable.object_null_1(tr)==1|subtable.object_null_2(tr)==1|subtable.delay_1_null(tr)==1|subtable.delay_2_null(tr)==1
  139. subtable.delay2_invalid(tr)=1;
  140. end
  141. if subtable.testing_uncued(tr)==0
  142. subtable.delay2_invalid(tr)=NaN;
  143. end
  144. end
  145. %% Adding information in the trialinfo
  146. % To make sure that repeated trials has a different trial number
  147. % (if this happened)
  148. %% Logfile
  149. foundrep=[];
  150. for tr =2:size(subtable.trial,1)
  151. if abs(subtable.trial(tr)-subtable.trial(tr-1))>400
  152. foundrep=tr
  153. end
  154. end
  155. subtable.trial(foundrep:end)=subtable.trial(foundrep:end)+1000
  156. %% Eye data
  157. foundrep=[]
  158. for tr =2:size(data_epoch_thr.trialinfo.trial,1)
  159. if abs(data_epoch_thr.trialinfo.trial(tr)-data_epoch_thr.trialinfo.trial(tr-1))>400
  160. foundrep=tr
  161. end
  162. end
  163. data_epoch_thr.trialinfo.trial(foundrep:end)=data_epoch_thr.trialinfo.trial(foundrep:end)+1000
  164. %% Adding additional info to the eye data
  165. for tr = 1:size(data_epoch_thr.trialinfo,1)
  166. toi=data_epoch_thr.trialinfo.trial(tr);
  167. found=find(subtable.trial==toi);
  168. data_epoch_thr.trialinfo.delay1_invalid(tr)=subtable.delay1_invalid(found);
  169. data_epoch_thr.trialinfo.delay2_invalid(tr)=subtable.delay2_invalid(found);
  170. end
  171. %% If the position is out of the threshold, it will be a NaN
  172. % thr=100 %threshold in pixels
  173. % thr=70 %threshold in pixels
  174. %
  175. % data_art_reject.eyedat(abs(data_epoch_recenter.eyedat(:,:,:))>thr)=NaN;
  176. data_art_reject=data_epoch_thr; % this threshold step has been already implemented in the 2022 preprocessing pipeline
  177. %% Keeping trials based on retrocue and (optional) based on FIXATE! messages
  178. if selectingretrocue
  179. if removingalarms&delay==1
  180. toi =data_art_reject.trialinfo.retrocue==retrooi&data_epoch_thr.trialinfo.delay1_invalid~=1;
  181. elseif removingalarms&delay==2
  182. toi =data_art_reject.trialinfo.retrocue==retrooi&data_epoch_thr.trialinfo.delay2_invalid~=1;
  183. elseif removingalarms==0
  184. toi =data_art_reject.trialinfo.retrocue==retrooi;
  185. end
  186. data_art_reject.eyedat=data_art_reject.eyedat(toi,:,:);
  187. data_art_reject.trialinfo=data_art_reject.trialinfo(toi,:);
  188. else
  189. if removingalarms&delay==1
  190. toi =data_epoch_thr.trialinfo.delay1_invalid~=1;
  191. elseif removingalarms&delay==2
  192. toi=data_epoch_thr.trialinfo.delay2_invalid~=1;
  193. elseif removingalarms==0
  194. toi= 1:size(data_epoch_thr.trialinfo,1);
  195. end
  196. data_art_reject.eyedat=data_art_reject.eyedat(toi,:,:);
  197. data_art_reject.trialinfo=data_art_reject.trialinfo(toi,:);
  198. end
  199. %%
  200. objlist=unique(subtable.object_cued_rot);
  201. rho=nan(size(data_art_reject.eyedat,1),size(data_art_reject.eyedat,3));
  202. parfor ttt=1:size(data_art_reject.trialinfo,1) %for each trial
  203. t_s_ave=[];
  204. item_id_rsa=nan(length(t_s_ave),3,size(data_art_reject.eyedat,3));
  205. D=nan(length(16),length(16));
  206. RRR=nan(1,size(data_art_reject.eyedat,3))
  207. %% Making averages, excluding the present trial of interest Selecting trials per item ID and rot
  208. counter=1;
  209. for k =1:length(objlist)
  210. actual_trial=data_art_reject.trialinfo.trial(ttt);
  211. if ooi==1
  212. triales = find(data_art_reject.trialinfo.object_1_rot==objlist(k)&...
  213. data_art_reject.trialinfo.trial~=actual_trial); % here we group different objects
  214. elseif ooi==2
  215. triales = find(data_art_reject.trialinfo.object_2_rot==objlist(k)&...
  216. data_art_reject.trialinfo.trial~=actual_trial); % here we group different objects
  217. end
  218. [t_s_ave{counter}] = squeeze(nanmean(data_art_reject.eyedat(triales,:,:),1));
  219. counter=counter+1;
  220. end
  221. %% Just changing the format
  222. for k = 1:length(t_s_ave);
  223. if ~isempty(t_s_ave{k})
  224. rr=(t_s_ave{k});
  225. item_id_rsa(k,:,:)=[rr];
  226. elseif isnan(t_s_ave{k})
  227. item_id_rsa(k,1:3,1:size(data_art_reject.eyedat,3))=NaN;
  228. end
  229. end
  230. %%
  231. if ooi==1
  232. obj1rotnum= data_art_reject.trialinfo.object_1_rot(ttt); %this is the number of the rotation for the single trial
  233. elseif ooi==2;
  234. obj1rotnum= data_art_reject.trialinfo.object_2_rot(ttt); %this is the number of the rotation for the single trial
  235. end
  236. for p=1:(size(item_id_rsa,3)) %per time
  237. to_rsa_id = item_id_rsa(:,1:2,p);
  238. to_rsa_id_reshape=reshape(to_rsa_id, size(to_rsa_id,1), []);
  239. %We take the euclidean distance between the actual trial and the
  240. %averages
  241. for a1=1:size(to_rsa_id_reshape,1)
  242. D(a1,1) = norm(to_rsa_id_reshape(a1,:)- data_art_reject.eyedat(ttt,1:2,p,:));
  243. end
  244. rot_pos=find(objlist==obj1rotnum);
  245. RRR(p)=corr(modelmat(rot_pos,:)',D,'type','Pearson','rows','pairwise');
  246. end
  247. rho(ttt,:)=RRR;
  248. end
  249. %%
  250. times=data_art_reject.time;
  251. scriptname= mfilename('fullpath');
  252. trialinfo=data_art_reject.trialinfo;
  253. RSA_obj_rot = struct('rho',{rho},'time',{times},'trialinfo',{trialinfo},'script',{scriptname},'analysis_info',{analysis_info});
  254. %% Saving results
  255. fold_tosave =[mainfolder folder partis{ppp,:}];
  256. save (fold_tosave,'RSA_obj_rot','-v7.3')
  257. clearvars -except ppp partis partis_sub mainfolder fold_tosave tobefound tobefound_tr analysis_info modelmat folder selectingretrocue retrooi conf ii ooi delay removingalarms
  258. end
  259. end