@@ -347,7 +347,7 @@ function bids_export(files, varargin)
347347 ' rmtempfiles' ' string' {' on' ' off' } ' on' ;
348348 ' exportformat' ' string' {' same' ' eeglab' ' edf' ' bdf' } ' eeglab' ;
349349 ' individualEventsJson' ' string' {' on' ' off' } ' off' ;
350- ' modality' ' string' {' ieeg' ' meg' ' eeg' ' auto' } ' auto' ;
350+ ' modality' ' string' {' ieeg' ' meg' ' eeg' ' emg ' ' auto' } ' auto' ;
351351 ' README' ' string' {} ' ' ;
352352 ' CHANGES' ' string' {} ' ' ;
353353 ' copydata' ' integer' {} [0 1 ]; % legacy, does nothing now
@@ -371,6 +371,13 @@ function bids_export(files, varargin)
371371 opt.SourceDatasets = opt .sourceDatasets ;
372372end
373373
374+ if strcmpi(opt .modality , ' emg' )
375+ if strcmpi(opt .exportformat , ' eeglab' )
376+ opt.exportformat = ' bdf' ;
377+ fprintf(' EMG data detected: changing export format to bdf\n ' );
378+ end
379+ end
380+
374381% deleting folder
375382% ---------------
376383fprintf(' Exporting data to %s ...\n ' , opt .targetdir );
@@ -872,6 +879,13 @@ function copy_data_bids_eeg(sIn, subjectStr, sess, fileStr, opt)
872879[pathIn ,fileInNoExt ,ext ] = fileparts(fileIn );
873880fprintf(' Processing file %s\n ' , fileIn );
874881[EEG ,opt .modality ] = eeg_import(fileIn , ' modality' , opt .modality , ' noevents' , opt .noevents , ' importfunc' , opt .importfunc );
882+
883+ % Set default export format to EDF for EMG data (after modality is determined)
884+ if strcmpi(opt .modality , ' emg' ) && strcmpi(opt .exportformat , ' eeglab' )
885+ opt.exportformat = ' edf' ;
886+ fprintf(' Note: EMG data will be exported to EDF format (default for EMG)\n ' );
887+ end
888+
875889if contains(fileIn , ' _bids_tmp_' ) && strcmpi(opt .rmtempfiles , ' on' )
876890 fprintf(' Deleting temporary file %s\n ' , fileIn );
877891 delete(fileIn );
@@ -892,15 +906,37 @@ function copy_data_bids_eeg(sIn, subjectStr, sess, fileStr, opt)
892906fileOut = [fileBase ' _' opt .modality ext ];
893907
894908% select data subset
895- EEG = eeg_selectsegment(EEG , ' eventtype' , eventtype , ' eventindex' , eventindex , ' timeoffset' , timeoffset ) ;
909+ EEG = eeg_selectsegment(EEG , ' eventtype' , eventtype , ' eventindex' , eventindex , ' timeoffset' , timeoffset ) ;
896910
897911if ~isequal(opt .exportformat , ' same' ) || isequal(ext , ' .set' )
898912 % export data if necessary
899913 [filePathTmp ,fileOutNoExt ,~ ] = fileparts(fileOut );
900914 if isequal(opt .exportformat , ' eeglab' )
901915 pop_saveset(EEG , ' filename' , [ fileOutNoExt ' .set' ], ' filepath' , filePathTmp );
902916 else
903- pop_writeeeg(EEG , fullfile(filePathTmp , [ fileOutNoExt ' .' opt .exportformat ]), ' TYPE' ,upper(opt .exportformat ));
917+ % Check for regular sampling when exporting to EDF/BDF
918+ if strcmpi(opt .exportformat , ' edf' ) || strcmpi(opt .exportformat , ' bdf' )
919+ [isRegular , avgFreq ] = bids_check_regular_sampling(EEG );
920+ if ~isRegular
921+ error([' EDF/BDF export requires regular sampling. Your data has irregular sampling (avg %.2f Hz).\n ' ...
922+ ' Please resample your data before exporting:\n ' ...
923+ ' EEG = pop_resample(EEG, %.0f );\n ' ...
924+ ' Then re-run bids_export.' ], avgFreq , round(avgFreq ));
925+ end
926+ end
927+
928+ % For EMG (and other modalities), save without events in EDF
929+ % Events are saved separately in events.tsv
930+ if strcmpi(opt .modality , ' emg' ) && (strcmpi(opt .exportformat , ' edf' ) || strcmpi(opt .exportformat , ' bdf' ))
931+ % Temporarily remove events before writing EDF
932+ tmpEvents = EEG .event ;
933+ EEG.event = [];
934+ pop_writeeeg(EEG , fullfile(filePathTmp , [ fileOutNoExt ' .' opt .exportformat ]), ' TYPE' ,upper(opt .exportformat ));
935+ % Restore events for events.tsv export
936+ EEG.event = tmpEvents ;
937+ else
938+ pop_writeeeg(EEG , fullfile(filePathTmp , [ fileOutNoExt ' .' opt .exportformat ]), ' TYPE' ,upper(opt .exportformat ));
939+ end
904940 end
905941else
906942 % copy the file
@@ -966,18 +1002,30 @@ function copy_data_bids_eeg(sIn, subjectStr, sess, fileStr, opt)
9661002 EEG= pop_chanedit(EEG , ' lookup' , opt .chanlookup );
9671003end
9681004
1005+ % Set datatype before writing channel/electrode files
1006+ if strcmpi(opt .modality , ' eeg' )
1007+ EEG.etc.datatype = ' eeg' ;
1008+ elseif strcmpi(opt .modality , ' ieeg' )
1009+ EEG.etc.datatype = ' ieeg' ;
1010+ elseif strcmpi(opt .modality , ' meg' )
1011+ EEG.etc.datatype = ' meg' ;
1012+ elseif strcmpi(opt .modality , ' emg' )
1013+ EEG.etc.datatype = ' emg' ;
1014+ end
1015+
9691016% Write electrode file information (electrodes.tsv and coordsystem.json)
9701017bids_writechanfile(EEG , fileOutRed );
971- bids_writeelectrodefile(EEG , fileOutRed , ' export' , opt .elecexport );
1018+ bids_writeelectrodefile(EEG , fileOutRed , ' export' , opt .elecexport , ' rootdir' , opt .targetdir );
1019+
1020+ % Write modality-specific info files
9721021if strcmpi(opt .modality , ' eeg' )
9731022 bids_writetinfofile(EEG , tInfo , notes , fileOutRed );
974- EEG.etc.datatype = ' eeg' ;
9751023elseif strcmpi(opt .modality , ' ieeg' )
9761024 bids_writeieegtinfofile(EEG , tInfo , notes , fileOutRed );
977- EEG.etc.datatype = ' ieeg' ;
9781025elseif strcmpi(opt .modality , ' meg' )
9791026 bids_writemegtinfofile(EEG , tInfo , notes , fileOutRed );
980- EEG.etc.datatype = ' meg' ;
1027+ elseif strcmpi(opt .modality , ' emg' )
1028+ bids_writeemgtinfofile(EEG , tInfo , notes , fileOutRed );
9811029end
9821030
9831031% write channel information
0 commit comments