
%%

% The original log files contain some information:
% - the first part of the name contains the subject identifier: P31237 and P23671
% - the second part describes the task
% - the extension shows whether it is the original NBS Presentation log file itself (.log) or the custom text file written by the Presentation script (.txt)

sourcepath = './original';
targetpath = './bids';


%%
% general information that applies to all

general= [];
general.InstitutionName                 = 'Radboud University';
general.InstitutionalDepartmentName     = 'Donders Institute for Brain, Cognition and Behaviour';
general.InstitutionAddress              = 'Kapittelweg 29, 6525 EN, Nijmegen, The Netherlands';
% this goes into dataset_description.json
general.dataset_description.Name        = 'Presentation example';
general.dataset_description.BIDSVersion = 'unofficial extension';
% specify the type and target location
general.bidsroot = targetpath;
general.datatype = 'events';

%%
% the first two files are in Presentation .log format

filename = {
  'P31237_task1-MotorTaskEv_left.log'
  'P23671_task1-MotorTaskEv_right.log'
  };

subject = {'P31237', 'P23671'};
task = {'motor', 'motor'};
age = [45, 52];
handedness = {'right', 'right'};
clinical_state = {'parkinson', 'control'};


for i=1:numel(subject)
  
  cfg = general; % copy the general fields
  cfg.sub = subject{i};
  cfg.task = task{i};
  
  % you can use this to add information to the participants.tsv table
  cfg.participants.age = age(i);
  cfg.participants.handedness = handedness(i);
  cfg.participants.clinical_state = clinical_state{i};
  
  cfg.dataset = fullfile(sourcepath, filename{i});
  
  % instead of specifying cfg.dataset, you can also use FT_READ_EVENT and pass the events like this
  % cfg.events = ft_read_event(fullfile(sourcepath, filename{i}));
  
  
  data2bids(cfg);
  
end

%%

%%
% the subsequent files are in a custom tabular format

filename = {
  'P31237_prac1_logfile.txt'
  'P23671_prac1_logfile.txt'
  'P31237_task1_logfile.txt'
  'P23671_task1_logfile.txt'
  };

subject = {'P31237', 'P23671', 'P31237', 'P23671'};
task = {'practice', 'practice', 'unknown', 'unknown'};

for i=1:numel(filename)
  
  cfg = general; % copy the general fields
  cfg.sub = subject{i};
  cfg.task = task{i};
  
  % read the ascii log file
  log = readtable(fullfile(sourcepath, filename{i}));
  
  % add the onset and duration (both in seconds)
  % the Presentation software uses time stamps of 0.1 milliseconds
  log.onset = (log.Fixation_Time)/1e4;
  log.duration = (log.Response_Time - log.Fixation_Time)/1e4;
  log.duration(log.Response_Time==0) = nan;
  
  cfg.events = log;
  data2bids(cfg);
end
