highspeed_bids_events.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. %% SCRIPT: CREATE EVENT.TSV FILES FROM THE BEHAVIORAL DATA FOR BIDS
  2. % =========================================================================
  3. % PROJECT: HIGHSPEED
  4. % WRITTEN BY LENNART WITTKUHN 2018 - 2020
  5. % CONTACT: WITTKUHN AT MPIB HYPHEN BERLIN DOT MPG DOT DE
  6. % MAX PLANCK RESEARCH GROUP NEUROCODE
  7. % MAX PLANCK INSTITUTE FOR HUMAN DEVELOPMENT
  8. % MAX PLANCK UCL CENTRE FOR COMPUTATIONAL PSYCHIATRY AND AGEING RESEARCH
  9. % LENTZEALLEE 94, 14195 BERLIN, GERMANY
  10. % =========================================================================
  11. %% DEFINE PATHS AND IMPORTANT VARIABLES:
  12. % clear the workspace and command window:
  13. clear variables; clc;
  14. % define the data root path
  15. path_root = strsplit(pwd, 'code');
  16. path_root = path_root{1};
  17. % define the input path:
  18. path_input = fullfile(path_root, 'input', 'behavior', 'main');
  19. % define the output path:
  20. path_output = path_root;
  21. % get the contents of the output directory:
  22. path_output_dir = dir(path_output);
  23. % check how many subjects are in the root directory:
  24. num_subs_found = sum(contains({path_output_dir.name},'sub'));
  25. % extended output path used to check for old files:
  26. path_old_files = fullfile(path_output,'*','*','func');
  27. % find all existing events.tsv files in the output directory:
  28. prev_files = dir(fullfile(path_old_files,'*events.tsv'));
  29. % delete all previous events files:
  30. for old_file = 1:length(prev_files)
  31. delete(fullfile(prev_files(old_file).folder,prev_files(old_file).name))
  32. end
  33. % define the script path:
  34. path_script = fullfile(path_root, 'code');
  35. % read the text file containing a list of subject ids:
  36. sub_list = dlmread(fullfile(path_script, 'heudiconv', 'highspeed-participant-list.txt'));
  37. % turn the array with ids into a strings in a cell array:
  38. sub_list = cellstr(num2str(sub_list));
  39. %check if the number of subjects in the list matches the target directory
  40. if numel(sub_list) ~= num_subs_found
  41. warning(['Number of subjects in the data dir does not match ' ...
  42. 'number of subjects in the subject text file!']);
  43. sub_alt_list = cellfun(@num2str,num2cell(1:length(sub_list)),'un',0);
  44. else
  45. sub_alt_list = sub_list;
  46. sub_alt_list = cellfun(@num2str,num2cell(1:num_subs_found),'un',0);
  47. end
  48. % determine the number of study sessions:
  49. num_ses = 2;
  50. % determine the number of task runs per study session:
  51. num_run = 4;
  52. % define a cell array containing the stimulus labels in german:
  53. key_set = {'Gesicht','Haus','Katze','Schuh','Stuhl'};
  54. % define a cell array containing the stimulus labels in english:
  55. value_set = {'Face','House','Cat','Shoe','Chair'};
  56. % create a dictionary that translates the stimulus labels:
  57. label_dict = containers.Map(key_set,value_set);
  58. % create a 2d-array of run indices ordered by run (row) and sessions (col):
  59. run_array = reshape(1:num_run * num_ses, num_run, num_ses);
  60. % define the names of the four different task conditions:
  61. task_names = {'oddball','sequence','repetition','repetition'};
  62. %%
  63. for sub = 1:length(sub_alt_list)
  64. %for sub = 1:1
  65. % initialize the maximum repetition trial index:
  66. max_rep = 0;
  67. % get the current subject id:
  68. sub_id = sub_list{sub};
  69. % print progress:
  70. fprintf('Running sub %d of %d\n', sub, length(sub_alt_list))
  71. % define a template string that takes subject, session and run id:
  72. template_string = '*sub_%s_session_%d*run_%d*';
  73. % put in the current subject, session and run id:
  74. file_string = sprintf(template_string,sub_id,num_ses,num_run);
  75. % read behavioral data files of all participants:
  76. path_file = dir(fullfile(path_input,file_string));
  77. % load the behavioral data into the workspace:
  78. load(fullfile(path_input,path_file.name));
  79. for session = 1:num_ses
  80. % create a subject identifier (in bids format):
  81. pad_sub = sprintf('sub-%02d',str2double(sub_alt_list{sub}));
  82. % create a session identififer (in bids format):
  83. pad_ses = ['ses-0', num2str(session)];
  84. % combine the two identifiers as the first part of file names:
  85. sub_file_name = strcat(pad_sub,'_',pad_ses);
  86. % create the subject output path:
  87. path_output_sub = (fullfile(path_output,pad_sub,pad_ses,'func'));
  88. % create the subject directory if it does not exist yet:
  89. if ~exist(path_output_sub,'dir')
  90. system(sprintf('mkdir -p %s',path_output_sub));
  91. end
  92. for run = 1:num_run
  93. events = table;
  94. for cond = 1:4
  95. event_all = extract_bids_events(Data, Basics, Sets, pad_sub, run, session, cond);
  96. events = [events;event_all];
  97. end
  98. % sort by event onset (i.e., in chronological order):
  99. events = sortrows(events,{'onset'});
  100. % make two copies of the repetition trials:
  101. rep_trials_old = events.trial(contains(events.condition, 'repetition'));
  102. rep_trials_new = rep_trials_old;
  103. % get the old trial indices while maintaining their order:
  104. trial_old = unique(rep_trials_old, 'stable');
  105. % get the number of repetition trials in the current run:
  106. n_rep_trials = length(trial_old);
  107. % create new trial indices depending on the running number of
  108. % repetition trials:
  109. trial_new = max_rep+1:max_rep+n_rep_trials;
  110. % change the old trial indices
  111. for i = 1:n_rep_trials
  112. rep_trials_new(rep_trials_old == trial_old(i)) = trial_new(i);
  113. end
  114. % update the repetition trials of the events files:
  115. events.trial(contains(events.condition, 'repetition')) = rep_trials_new;
  116. % update the counter of the maximum repetition trial index:
  117. max_rep = max(unique(events.trial(contains(events.condition, 'repetition'))));
  118. % create template string file for data output (tsv format):
  119. string_template = '_task-highspeed_rec-prenorm_run-0%d_events';
  120. % write conditon and run information into the string:
  121. string_written = sprintf(string_template,run);
  122. % create the full filenames:
  123. outfile_name = strcat(sub_file_name,string_written);
  124. % create paths of the tsv and csv files:
  125. path_tsv = fullfile(path_output_sub,strcat(outfile_name,'.tsv'));
  126. path_csv = fullfile(path_output_sub,strcat(outfile_name,'.csv'));
  127. % write the events table as csv file:
  128. writetable(events,path_csv,'Delimiter','\t');
  129. % copy the created file from csv to tsv file:
  130. copyfile(path_csv,path_tsv)
  131. % delete the csv file:
  132. delete(path_csv);
  133. end
  134. end
  135. end