% A. Delorme - Jan 2020

%% raw data files (replace with your own)
% ----------------------------------
data(1).file     = {'../s01raw/s01.set'};
data(1).session  = 1;
data(1).run      = 1;
data(1).chanlocs = '../s01raw/polhemus/s01.elp';
data(1).anat     = '../s01raw/mri/sub-001_T1w.nii.gz';

data(2).file     = {'../s02raw/s02.set'};
data(2).session  = 1;
data(2).run      = 1;
data(2).chanlocs = '../s02raw/polhemus/s02.elp';
data(2).anat     = '../s02raw/mri/sub-002_T1w.nii.gz';

% Anatomical MRI files
anattype    = 'T1w';
defacedflag = 'off';

%% Code Files used to preprocess and import to BIDS
% -----------------------------------------------------|
codefiles = {'/Users/arno/GoogleDrive/bids_p3_2subjects/matlab/bids_export_p3.m',...
             '/Users/arno/GoogleDrive/bids_p3_2subjects/matlab/loadallbdf_2020.m',...
             '/Users/arno/GoogleDrive/bids_p3_2subjects/matlab/addHEDTags.m' ...
             '/Users/arno/GoogleDrive/bids_p3_2subjects/matlab/clean_events.m' ...
             '/Users/arno/GoogleDrive/bids_p3_2subjects/matlab/convert_nifti.m' };

%% general information for dataset_description.json file
% -----------------------------------------------------
generalInfo.Name = 'P3 study';
generalInfo.ReferencesAndLinks = { "https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6673364/" };
generalInfo.BIDSVersion = 'v1.2.1';
generalInfo.License = 'CC0';
generalInfo.Authors = {'Arnaud Delorme' 'Scott Makeig' };

%% participant information for participants.tsv file
% -------------------------------------------------
pInfo = { 'gender'   'age';
          'F'	     30;
          'M'	     30 };
       
%% participant column description for participants.json file
% ---------------------------------------------------------
pInfoDesc.gender.Description = 'Sex of the participant';
pInfoDesc.gender.Levels.M    = 'male';
pInfoDesc.gender.Levels.F    = 'female';

pInfoDesc.participant_id.LongName    = 'Participant identifier';
pInfoDesc.participant_id.Description = 'Unique participant identifier';

pInfoDesc.age.Description = 'age of the participant';
pInfoDesc.age.Units       = 'years';

% event column description for xxx-events.json file (only one such file)
% ----------------------------------------------------------------------
eInfo = {'HED'         'usertags'; 
         'onset'       'init_time';
         'value'       'Code';
         'trial_type'  'type'} ;
     
eInfoDesc.onset.Description = 'Event onset';
eInfoDesc.onset.Units = 'second';

eInfoDesc.value.Description = 'Type of event (numerical)';
eInfoDesc.value.Levels.x11  = 'An image with five rectangular boxes are presented. Box No. 1 is highllighted, the disc is presented in box No 1.';
eInfoDesc.value.Levels.x12  = 'An image with five rectangular boxes are presented. Box No. 1 is highllighted, the disc is presented in box No 2.';
eInfoDesc.value.Levels.x13  = 'An image with five rectangular boxes are presented. Box No. 1 is highllighted, the disc is presented in box No 3.';
eInfoDesc.value.Levels.x14  = 'An image with five rectangular boxes are presented. Box No. 1 is highllighted, the disc is presented in box No 4.';
eInfoDesc.value.Levels.x15  = 'An image with five rectangular boxes are presented. Box No. 1 is highllighted, the disc is presented in box No 5.';

eInfoDesc.value.Levels.x21  = 'An image with five rectangular boxes are presented. Box No. 2 is highllighted, the disc is presented in box No 1.';
eInfoDesc.value.Levels.x22  = 'An image with five rectangular boxes are presented. Box No. 2 is highllighted, the disc is presented in box No 2.';
eInfoDesc.value.Levels.x23  = 'An image with five rectangular boxes are presented. Box No. 2 is highllighted, the disc is presented in box No 3.';
eInfoDesc.value.Levels.x24  = 'An image with five rectangular boxes are presented. Box No. 2 is highllighted, the disc is presented in box No 4.';
eInfoDesc.value.Levels.x25  = 'An image with five rectangular boxes are presented. Box No. 2 is highllighted, the disc is presented in box No 5.';

eInfoDesc.value.Levels.x31  = 'An image with five rectangular boxes are presented. Box No. 3 is highllighted, the disc is presented in box No 1.';
eInfoDesc.value.Levels.x32  = 'An image with five rectangular boxes are presented. Box No. 3 is highllighted, the disc is presented in box No 2.';
eInfoDesc.value.Levels.x33  = 'An image with five rectangular boxes are presented. Box No. 3 is highllighted, the disc is presented in box No 3.';
eInfoDesc.value.Levels.x34  = 'An image with five rectangular boxes are presented. Box No. 3 is highllighted, the disc is presented in box No 4.';
eInfoDesc.value.Levels.x35  = 'An image with five rectangular boxes are presented. Box No. 3 is highllighted, the disc is presented in box No 5.';

eInfoDesc.value.Levels.x41  = 'An image with five rectangular boxes are presented. Box No. 4 is highllighted, the disc is presented in box No 1.';
eInfoDesc.value.Levels.x42  = 'An image with five rectangular boxes are presented. Box No. 4 is highllighted, the disc is presented in box No 2.';
eInfoDesc.value.Levels.x43  = 'An image with five rectangular boxes are presented. Box No. 4 is highllighted, the disc is presented in box No 3.';
eInfoDesc.value.Levels.x44  = 'An image with five rectangular boxes are presented. Box No. 4 is highllighted, the disc is presented in box No 4.';
eInfoDesc.value.Levels.x45  = 'An image with five rectangular boxes are presented. Box No. 4 is highllighted, the disc is presented in box No 5.';

eInfoDesc.value.Levels.x51  = 'An image with five rectangular boxes are presented. Box No. 5 is highllighted, the disc is presented in box No 1.';
eInfoDesc.value.Levels.x52  = 'An image with five rectangular boxes are presented. Box No. 5 is highllighted, the disc is presented in box No 2.';
eInfoDesc.value.Levels.x53  = 'An image with five rectangular boxes are presented. Box No. 5 is highllighted, the disc is presented in box No 3.';
eInfoDesc.value.Levels.x54  = 'An image with five rectangular boxes are presented. Box No. 5 is highllighted, the disc is presented in box No 4.';
eInfoDesc.value.Levels.x55  = 'An image with five rectangular boxes are presented. Box No. 5 is highllighted, the disc is presented in box No 5.';

eInfoDesc.value.Levels.x201  = 'Button press whenever a disc appeared in the highlighted location';

% HED Tags
eInfoDesc.HED.Description = 'Hierarchical Event Descricptor';

% Trial types correspondance with event types/values
% BIDS allows for both trial types and event values
% --------------------------------------------------
trialTypes = {'11' 'Picture';
              '12' 'Picture';
              '13' 'Picture';
              '14' 'Picture';
              '15' 'Picture';
              '21' 'Picture';
              '22' 'Picture';
              '23' 'Picture';
              '24' 'Picture';
              '25' 'Picture';
              '31' 'Picture';
              '32' 'Picture';
              '33' 'Picture';
              '34' 'Picture';
              '35' 'Picture';
              '41' 'Picture';
              '42' 'Picture';
              '43' 'Picture';
              '44' 'Picture';
              '45' 'Picture';
              '51' 'Picture';
              '52' 'Picture';
              '53' 'Picture';
              '54' 'Picture';
              '55' 'Picture';
              '201' 'Response'};

% Content for README file
% -----------------------
README = sprintf( [ 'Data for this selective attention task was collected in 2004' 10 ...
                    'at the Swartz Center for Computational Neuroscience at UCSD.' 10 ...
                    'These datasets are part of a larger corpus of 32-channel data' 10 ...
                    'collected a few years prior. The experiment is identical' 10 ...
                    'although the number of channel is larger (256), the electrode' 10 ...
                    'positions are scanned and the anatomical MRI is provided' 10 ...
                    '(allowing for precise source localization). See publication' 10 ...
                    'for more details.' 10 10 ...
                    'Raw data manipulation before export:' 10 ...
                    '- Fuse all BDF BIOSEMI files and reference to electrode 135 (see loadallbdf_2020.m)' 10 ...
                    '- Fuse with presentation file information (see loadallbdf_2020.m)' 10 ...
                    '- Remove spurious events of type ''condition'' and ''201'' (see clean_events.m)' 10 ...
                    '- Add HED tags (see addHEDTags.m)' 10 ...
                    '- Convert MRI to NIFTI format (MRIcron) and reorient (MRIcrogl) (see convert_nifti.m)' 10 ...
                    ]);
                
% Content for CHANGES file
% ------------------------
CHANGES = sprintf([ 'Revision history for P3 dataset\n\n' ...
                    'version 1.0 beta - 18 Feb 2020\n' ...
                    ' - Initial release\n' ]);                    
                
% Task information for xxxx-eeg.json file
% ---------------------------------------
tInfo.InstitutionAddress = 'Swartz Center for Computational Neuroscience';
tInfo.InstitutionName = 'University of California, San Diego';
tInfo.InstitutionalDepartmentName = 'Institute of Neural Computation';
tInfo.PowerLineFrequency = 60;
tInfo.ManufacturersModelName = 'ActiveTwo';

% call to the export function
% ---------------------------
bids_export(data, 'targetdir', '../BIDS', 'taskName', 'attention', 'trialtype', trialTypes, 'gInfo', generalInfo, 'pInfo', pInfo, 'pInfoDesc', pInfoDesc, 'eInfoDesc', eInfoDesc, 'README', README, 'CHANGES', CHANGES, 'codefiles', codefiles, 'tInfo', tInfo,'defaced',defacedflag, 'anattype', anattype,'copydata', 1);
