highspeed-bids-events.m 6.8 KB

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