Commit b054b88e authored by James Desjardins's avatar James Desjardins
Browse files

modify bids_init to be derivative compliant and add optional inputs for...

modify bids_init to be derivative compliant and add optional inputs for populating eeg sidecar json files.
parent 21842211
init_bids;
sub_dirs = dir('bids/sub*');
function init_bids_all(rootdir,fnamesuf,varargin)
if nargin<2
if ~exist('rootdir','var');
rootdir='bids/';
end
if ~exist('fnamesuf','var');
fnamesuf='';
end
end
%init_bids;
sub_dirs = dir([rootdir,'/sub*']);
for i=1:length(sub_dirs); % iterate through all subject directories
% find set file in current directory:
disp(['Searching ' sub_dirs(i).name ' for .set files...']);
fileinfo = dir(['bids/',sub_dirs(i).name,'/eeg/*.set']);
if length(fileinfo)>1;
disp(['WARNING: Found multiple files for ' sub_dirs(i).name '!']);
disp('The _eeg.set file will be used, if exists...');
fileinfo = dir(['bids/',sub_dirs(i).name,'/eeg/*eeg.set']);
end
if isempty(fileinfo);
disp(['Could not find _eeg.set file... ignoring ' sub_dirs(i).name '.']);
continue;
else
disp(['Found ' fileinfo.name ' file...']);
end
fileinfo = dir([rootdir,'/',sub_dirs(i).name,'/eeg/*.set']);
if length(fileinfo)>1;
% disp(['WARNING: Found multiple files for ' sub_dirs(i).name '!']);
% disp('The _eeg.set file will be used, if exists...');
fileinfo = dir([rootdir,'/',sub_dirs(i).name,'/eeg/*eeg',fnamesuf,'.set']);
end
% if isempty(fileinfo);
% disp(['Could not find _eeg,',fnamesuf,'.set file... ignoring ' sub_dirs(i).name '.']);
% continue;
% else
% disp(['Found ' fileinfo.name ' file...']);
% end
fname_cell = cellstr(fileinfo.name); % get filename into cellstr
% grab string from cellstr and add full path:
fname = strcat('bids/',sub_dirs(i).name,'/eeg/',fname_cell{1});
fname = strcat(rootdir,'/',sub_dirs(i).name,'/eeg/',fname_cell{1});
EEG = pop_loadset('filename',fname);
swap_ind = strfind(fname,'_'); % find indices of all '_' chars
swap_ind = strfind(fname,'_eeg'); % find indices of all '_' chars
if ~isempty(swap_ind);
swap_ind = swap_ind(end); % find final '_' character
fname = fname(1:swap_ind-1); % strip chars right of the final '_'
rootfname = fname(1:swap_ind-1+4); % strip chars right of the final '_'
end
% if filename doesn't end with _eeg.set:
if ~strcmp(EEG.filename(end-7:end),'_eeg.set');
disp(['Changing filename to ' fname '_eeg.set...']);
EEG = pop_saveset(EEG,strcat(fname,'_eeg.set'));
end
init_bids_sub(EEG,fname);
% if ~strcmp(EEG.filename(end-7:end),'_eeg.set');
% disp(['Changing filename to ' fname '_eeg.set...']);
% EEG = pop_saveset(EEG,strcat(fname,'_eeg.set'));
% end
init_bids_sub(EEG,rootfname,fnamesuf,varargin{:});
end
disp('Done.');
\ No newline at end of file
function init_bids_sub(EEG,rootfname,fnamesuf)
function init_bids_sub(EEG,rootfname,fnamesuf,varargin)
sidecar_eeg=mk_sidecar_eeg(EEG,[rootfname,'_',fnamesuf,'.set']);
savejson('',sidecar_eeg,[rootfname,'_',fnamesuf,'.json']);
channels=mk_channels(EEG,[rootfname,'_',fnamesuf,'.set']);
cell2tsv([rootfname,'_channels.tsv'],channels,'%s\t%s\t%s\n');
electrodes=mk_electrodes(EEG,[rootfname,'_',fnamesuf,'.set']);
cell2tsv([rootfname,'_electrodes.tsv'],electrodes,'%s\t%5.3f\t%5.3f\t%5.3f\t%s\t%s\n');
events=mk_events(EEG,[rootfname,'_',fnamesuf,'.set']);
cell2tsv([rootfname,'_events.tsv'],events,'%5.3f\t%5.3f\t%s\n');
[chan_struct,comp_struct,time_struct]=mk_marks(EEG,[rootfname,'_',fnamesuf,'.set']);
savejson('',chan_struct,[rootfname,'_chan_info.json']);
savejson('',comp_struct,[rootfname,'_comp_info.json']);
savejson('',time_struct,[rootfname,'_time_info.json']);
sidecar_eeg=mk_sidecar_eeg(EEG,[rootfname,fnamesuf,'.set'],varargin{:});
savejson('',sidecar_eeg,[rootfname,fnamesuf,'.json']);
channels=mk_channels(EEG,[rootfname,fnamesuf,'.set']);
cell2tsv([rootfname,fnamesuf,'_channels.tsv'],channels,'%s\t%s\t%s\n');
electrodes=mk_electrodes(EEG,[rootfname,fnamesuf,'.set']);
cell2tsv([rootfname,fnamesuf,'_electrodes.tsv'],electrodes,'%s\t%5.3f\t%5.3f\t%5.3f\t%s\t%s\n');
events=mk_events(EEG,[rootfname,fnamesuf,'.set']);
cell2tsv([rootfname,fnamesuf,'_events.tsv'],events,'%5.3f\t%5.3f\t%s\n');
if isfield(EEG,'marks');
[chan_struct,comp_struct,time_struct]=mk_marks(EEG,[rootfname,fnamesuf,'.set']);
savejson('',chan_struct,[rootfname,fnamesuf,'_chan_info.json']);
savejson('',comp_struct,[rootfname,fnamesuf,'_comp_info.json']);
savejson('',time_struct,[rootfname,fnamesuf,'_time_info.json']);
end
if ~isempty(EEG.icaweights)
dlmwrite([rootfname,fnamesuf,'_icaweights.tsv'],{EEG.icaweights},'\t');
dlmwrite([rootfname,fnamesuf,'_icasphere.tsv'],{EEG.icasphere},'\t');
end
\ No newline at end of file
function sidecar_eeg=mk_sidecar_eeg(EEG,fname)
function sidecar_eeg=mk_sidecar_eeg(EEG,fname,varargin)
opts=struct(varargin{:});
% load current file only if it hasn't been loaded already
if ~exist('EEG','var') || (exist('EEG','var') && isempty(EEG)) || ...
if ~exist('EEG','var') || ...
(exist('EEG','var') && isempty(EEG)) || ...
(~isempty(EEG) && ~strcmp(fname,[EEG.filepath '/' EEG.filename]));
EEG = pop_loadset('filename',fname);
end
......@@ -9,38 +13,81 @@ end
[path,fname,ext] = fileparts(fname);
disp(['Creating ' fname '.json file...']);
sidecar_eeg.TaskName='all';
sidecar_eeg.TaskDescription='';
sidecar_eeg.Instructions='';
sidecar_eeg.CogAltasID='';
sidecar_eeg.CogPOID='';
sidecar_eeg.InstitutionName='';
sidecar_eeg.InstitutionAddress='';
sidecar_eeg.DeviceSerialNumber='';
sidecar_eeg.EEGSamplingFrequency=EEG.srate;
sidecar_eeg.ManufacturersAmplifierModelName='';
sidecar_eeg.ManufacturerCapModelName='';
if isfield(opts,'TaskName');
sidecar_eeg.TaskName=opts.TaskName;
end
if isfield(opts,'TaskDescription');
sidecar_eeg.TaskDescription=opts.TaskDescription;
end
if isfield(opts,'Instructions');
sidecar_eeg.Instructions=opts.Instructions;
end
if isfield(opts,'CogAtlasID');
sidecar_eeg.CogAltasID=opts.CogAltasID;
end
if isfield(opts,'CogPOID');
sidecar_eeg.CogPOID=opts.CogPOID;
end
if isfield(opts,'InstitutionName');
sidecar_eeg.InstitutionName=opts.InstitutionName;
end
if isfield(opts,'InstitutionAddress');
sidecar_eeg.InstitutionAddress=opts.InstitutionAddress;
end
if isfield(opts,'DeviceSerialNumber');
sidecar_eeg.DeviceSerialNumber=opts.DeviceSerialNumber;
end
sidecar_eeg.SamplingFrequency=EEG.srate;
sidecar_eeg.EEGChannelCount=EEG.nbchan;
sidecar_eeg.EOGChannelCount='';
sidecar_eeg.EMGChannelCount='';
sidecar_eeg.EEGReference=EEG.ref;
sidecar_eeg.MiscChannelCount='';
sidecar_eeg.TriggerChannelCount='';
sidecar_eeg.PowerLineFrequency='';
sidecar_eeg.EEGPlacementScheme='';
sidecar_eeg.EEGReference=EEG.ref;
sidecar_eeg.HardwareFilters='';
sidecar_eeg.SoftwareFilters='';
sidecar_eeg.RecordingDuration=EEG.pnts*(1/EEG.srate);
if isfield(opts,'PowerLineFrequency');
sidecar_eeg.PowerLineFrequency='';
end
if isfield(opts,'EEGPlacementScheme');
sidecar_eeg.EEGPlacementScheme='';
end
if isfield(opts,'Manufacturer');
sidecar_eeg.Manufacturer='';
end
if isfield(opts,'ManufacturerModelName');
sidecar_eeg.ManufacturerModelName='';
end
if isfield(opts,'CapManufacturer');
sidecar_eeg.CapManufacturer='';
end
if isfield(opts,'CapModelName');
sidecar_eeg.CapModelName='';
end
if isfield(opts,'HardwareFilters');
sidecar_eeg.HardwareFilters='';
end
if isfield(opts,'SoftwareFilters');
sidecar_eeg.SoftwareFilters='';
end
if EEG.trials==1;
sidecar_eeg.RecordingType='continuous';
sidecar_eeg.EpochLength='Inf';
sidecar_eeg.RecordingDuration=EEG.pnts*(1/EEG.srate);
else
sidecar_eeg.RecordingType='epoched';
sidecar_eeg.EpochLength=EEG.xmax-EEGxmin;
sidecar_eeg.EpochLength=EEG.xmax-EEG.xmin;
%to propose
sidecar_eeg.EpochCount=EEG.trials;
end
if isfield(opts,'DeviceSoftwareVersion');
sidecar_eeg.DeviceSoftwareVersion='';
end
if isfield(opts,'SubjectArtefactDescription');
sidecar_eeg.SubjectArtefactDescription='';
end
%to propose
if isfield(opts,'Category')
sidecar_eeg.Category=opts.Category;
end
sidecar_eeg.DeviceSoftwareVersion='';
sidecar_eeg.SubjectArtefactDescription='';
sidecar_eeg.DigitizedLandmarks='';
sidecar_eeg.DigitizedHeadPoints='';
sidecar_eeg.DigitizedElectrodes='';
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment