extract_bids_events.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. function event_all = extract_bids_events(Data, Basics, Sets, pad_sub, run, ses, cond)
  2. %% CONVERT DATASET TO TABLE AND GET THE VARIABLE NAMES:
  3. % convert data table from dataset to table type:
  4. data = dataset2table(Data(cond).data);
  5. % get the variable names of the current data table:
  6. var_names = data.Properties.VariableNames;
  7. % get all events we want to convert:
  8. all_events = var_names(contains(var_names, {'tFlip'}));
  9. %% BASIC TASK STATS
  10. % determine the number of study sessions:
  11. num_ses = 2;
  12. % determine the number of task runs per study session:
  13. num_run = 4;
  14. % get the indices of the current sessions (as booleans):
  15. idx_session = Basics.runInfo.session == ses;
  16. % get the indices of the current run (as booleans):
  17. idx_run = Basics.runInfo.run == run;
  18. % get the timestamp of of the first scanner trigger:
  19. t_trigger = Basics.runInfo.tTrigger(idx_session & idx_run);
  20. % get the data indices of the current session:
  21. idx_data_ses = data.session == ses;
  22. % get the data indices of the current run within session:
  23. idx_data_run = data.run == run;
  24. % combine the indices to get the correct data indices:
  25. index = idx_data_ses & idx_data_run;
  26. % create a 2d-array of run indices ordered by run (row) and sessions (col):
  27. run_array = reshape(1:num_run * num_ses, num_run, num_ses);
  28. % define the names of the four different task conditions:
  29. task_names = {'oddball','sequence','repetition','repetition'};
  30. %% DEFINE DICTIONAIRY FOR THE STIMULUS LABELS:
  31. % define a cell array containing the stimulus labels in german:
  32. keys_stim = {'Gesicht','Haus','Katze','Schuh','Stuhl'};
  33. % define a cell array containing the stimulus labels in english:
  34. value_stim = {'face','house','cat','shoe','chair'};
  35. % create a dictionary that translates the stimulus labels:
  36. dict_stim = containers.Map(keys_stim,value_stim);
  37. %% DEFINE DICTIONAIRY FOR THE EVENTS:
  38. % define a cell array containing the stimulus labels in german:
  39. keys_type = {'tFlipCue','tFlipBlank','tFlipFix','tFlipStim','tFlipITI','tFlipDelay','tFlipResp','tResponse'};
  40. % define a cell array containing the stimulus labels in english:
  41. value_type = {'cue','blank','fixation','stimulus','interval','delay','choice','response'};
  42. % create a dictionary that translates the stimulus labels:
  43. dict_type = containers.Map(keys_type,value_type);
  44. %% LOOP OVER ALL EVENTS AND GATHER THE EVENT INFORMATION
  45. event_all = table;
  46. for i = 1:length(all_events)
  47. % get the current event:
  48. event_type = all_events{i};
  49. % get the number of sequential stimuli of that event:
  50. num_seq_stim = size(data{:,event_type},2);
  51. % number of trials of cond in the current run and session:
  52. num_events = sum(index) * num_seq_stim;
  53. % initialize empty events struct
  54. event = struct;
  55. % onsets, in seconds from first trigger:
  56. event.onset = data{index, event_type} - t_trigger;
  57. event.onset = reshape(transpose(event.onset),[],1);
  58. % duration, in seconds
  59. if strcmp(event_type,'tFlipCue')
  60. event.duration = repmat(Basics.tTargetCue,num_events,1);
  61. elseif strcmp(event_type, 'tFlipBlank')
  62. event.duration = repmat(Basics.tPreFixation,num_events,1);
  63. elseif strcmp(event_type, 'tFlipFix')
  64. event.duration = repmat(Basics.tFixation,num_events,1);
  65. elseif strcmp(event_type, 'tFlipStim')
  66. event.duration = repmat(Sets(cond).set.tStim,num_events,1);
  67. elseif strcmp(event_type, 'tFlipStim')
  68. event.duration = repmat(Sets(cond).set.tStim,num_events,1);
  69. elseif strcmp(event_type, 'tFlipITI')
  70. event.duration = repelem(data.tITI(index,:),num_seq_stim,1);
  71. elseif strcmp(event_type, 'tFlipDelay')
  72. event.duration = (data{index, 'tFlipResp'} - t_trigger) - event.onset;
  73. elseif strcmp(event_type, 'tFlipResp')
  74. event.duration = repmat(Basics.tResponseLimit,num_events,1);
  75. end
  76. % participant id
  77. event.subject = repmat({pad_sub},num_events,1);
  78. % add column that contains the session identifier:
  79. event.session = repmat(ses,num_events,1);
  80. % run within session:
  81. event.run_session = repmat(run,num_events,1);
  82. % run across the entire experiment:
  83. event.run_study = repmat(run_array(run,ses),num_events,1);
  84. % add column that contains the trial counter
  85. if cond == 4
  86. trial_indices = 41:1:45;
  87. event.trial = repelem(trial_indices(index)',num_seq_stim,1);
  88. else
  89. event.trial = repelem(find(index),num_seq_stim,1);
  90. end
  91. % add column that contains the condition:
  92. event.condition = repmat(task_names(cond),num_events,1);
  93. % add column that contains the trial type:
  94. event.trial_type = (repmat({dict_type(event_type)},num_events,1));
  95. % initialize all other event information:
  96. event.serial_position = nan(num_events,1);
  97. event.interval_time = nan(num_events,1);
  98. event.stim_orient = nan(num_events,1);
  99. event.stim_index = nan(num_events,1);
  100. event.stim_label = event.trial_type;
  101. %event.stim_file = strcat('images/',event.stim_label,'.jpg');
  102. event.target = nan(num_events,1);
  103. event.nontarget = nan(num_events,1);
  104. event.key_down = nan(num_events,1);
  105. event.key_id = repmat({NaN},num_events,1);
  106. event.key_target = repmat({NaN},num_events,1);
  107. event.accuracy = nan(num_events,1);
  108. event.response_time = nan(num_events,1);
  109. if strcmp(event_type, 'tFlipStim')
  110. % add column that contains the sequential position:
  111. event.serial_position = repmat(1:num_seq_stim,1,sum(index))';
  112. % add column that contains the inter-stimulus interval:
  113. event.interval_time = repelem(data.tITI(index,:),num_seq_stim,1);
  114. % add column that contains the stimulus orientation:
  115. event.stim_orient = repelem(data.orient(index,:),num_seq_stim,1);
  116. % get stimulus labels of the current run:
  117. event.stim_index = data.stimIndex(index,:);
  118. event.stim_index = reshape(transpose(event.stim_index),[],1);
  119. % add column that contains the path to the stimulus folder:
  120. event.stim_label = transpose(value_stim(event.stim_index));
  121. %event.stim_file = strcat('images/',event.stim_label,'.jpg');
  122. % add column that indicates whether stimulus is a target:
  123. if cond == 1
  124. event.target = double(event.stim_orient == 180);
  125. event.nontarget = nan(sum(index) * num_seq_stim,1);
  126. elseif cond == 2 || cond == 3 || cond == 4
  127. A = data.stimIndex(index,:);
  128. V = data.targetPos(index,:);
  129. W = data.targetPosAlt(index,:);
  130. event.target = bsxfun(@eq, cumsum(ones(size(A)), 2), V);
  131. event.target = reshape(transpose(event.target),[],1);
  132. event.nontarget = bsxfun(@eq, cumsum(ones(size(A)), 2), W);
  133. event.nontarget = reshape(transpose(event.nontarget),[],1);
  134. end
  135. end
  136. % add participant responses:
  137. if (strcmp(event_type, 'tFlipStim') && strcmp(task_names{cond}, 'oddball')) || ...
  138. (strcmp(event_type, 'tFlipResp') && ~strcmp(task_names{cond}, 'oddball'))
  139. % key press
  140. event.key_down = repelem(data.keyIsDown(index,:),num_seq_stim,1);
  141. % key identity
  142. event.key_id = repelem(data.keyIndex(index,:),num_seq_stim,1);
  143. if ~isempty(event.key_id)
  144. event.key_id = cellstr(num2str(event.key_id));
  145. event.key_id(strcmp(strrep(event.key_id,' ',''),'90')) = {'left'};
  146. event.key_id(strcmp(strrep(event.key_id,' ',''),'71')) = {'right'};
  147. event.key_id(~strcmp(event.key_id,'left') & ...
  148. ~strcmp(event.key_id,'right')) = {NaN};
  149. end
  150. % key target
  151. if ismember('keyTarget',data.Properties.VariableNames)
  152. event.key_target = repelem(data.keyTarget(index,:),num_seq_stim,1);
  153. else
  154. event.key_target = repmat({NaN},sum(index) * num_seq_stim,1);
  155. end
  156. % accuracy
  157. event.accuracy = repelem(data.acc(index,:),num_seq_stim,1);
  158. % response time
  159. event.response_time = repelem(data.rt(index,:),num_seq_stim,1);
  160. end
  161. events = struct2table(event);
  162. event_all = [event_all;events];
  163. end
  164. % remove all events that have no onset:
  165. event_all(isnan(event_all.onset),:) = [];
  166. end