rsa_48x48mat_singletrial_withinbetween.m 13 KB

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