def eeg2fif(filename, interactive=False, outdir=None): """ Brain Products EEG format """ fdir, fname, fext = qc.parse_path_list(filename) if outdir is None: outdir = fdir elif outdir[-1] != '/': outdir += '/' eegfile = fdir + fname + '.eeg' matfile = fdir + fname + '.mat' markerfile = fdir + fname + '.vmrk' fiffile = outdir + fname + '.fif' # convert to mat using MATLAB if not os.path.exists(matfile): print('Converting input to mat file') run = "[sig,header]=sload('%s'); save('%s','sig','header');" % (eegfile, matfile) qc.matlab(run) if not os.path.exists(matfile): qc.print_c('>> ERROR: mat file convertion error.', 'r') sys.exit() else: print('MAT file already exists. Skipping conversion.') # extract events events = [] for l in open(markerfile): if 'Stimulus,S' in l: # event, sample_index= l.split(' ')[-1].split(',')[:2] data = l.split(',')[1:3] event = int(data[0][1:]) # ignore 'S' sample_index = int(data[1]) events.append([sample_index, 0, event]) # load data and create fif header mat = scipy.io.loadmat(matfile) # headers= mat['header'] sample_rate = int(mat['header']['SampleRate']) signals = mat['sig'].T # channels x samples nch, t_len = signals.shape ch_names = ['TRIGGER'] + ['CH%d' % (x + 1) for x in range(nch)] ch_info = ['stim'] + ['eeg'] * (nch) info = mne.create_info(ch_names, sample_rate, ch_info, montage='standard_1005') # add event channel eventch = np.zeros([1, signals.shape[1]]) signals = np.concatenate((eventch, signals), axis=0) # create Raw object raw = mne.io.RawArray(signals, info) # add events raw.add_events(events, 'TRIGGER') # save and close raw.save(fiffile, verbose=False, overwrite=True, fmt='double') print('Saved to', fiffile)
def cva_features(datadir): """ (DEPRECATED FUNCTION) """ for fin in qc.get_file_list(datadir, fullpath=True): if fin[-4:] != '.gdf': continue fout = fin + '.cva' if os.path.exists(fout): logger.info('Skipping', fout) continue logger.info("cva_features('%s')" % fin) qc.matlab("cva_features('%s')" % fin)
def convert2mat(filename, matfile): """ Convert to mat using MATLAB BioSig sload(). """ basename = '.'.join(filename.split('.')[:-1]) # extension= filename.split('.')[-1] matfile = basename + '.mat' if not os.path.exists(matfile): print('>> Converting input to mat file') run = "[sig,header]=sload('%s'); save('%s.mat','sig','header');" % (filename, basename) qc.matlab(run) if not os.path.exists(matfile): qc.print_c('>> ERROR: mat file convertion error.', 'r') sys.exit()
def bdf2fif_matlab(filename, interactive=False, outdir=None): """ BioSemi bdf reader using BioSig toolbox of MATLAB. """ # convert to mat using MATLAB (MNE's edf reader has an offset bug) fdir, fname, fext = qc.parse_path_list(filename) if outdir is None: outdir = fdir elif outdir[-1] != '/': outdir += '/' fiffile = outdir + fname + '.fif' matfile = outdir + fname + '.mat' if not os.path.exists(matfile): logger.info('Converting input to mat file') run = "[sig,header]=sload('%s'); save('%s','sig','header');" % ( filename, matfile) qc.matlab(run) if not os.path.exists(matfile): logger.error('mat file convertion error.') sys.exit() mat = scipy.io.loadmat(matfile) os.remove(matfile) sample_rate = int(mat['header']['SampleRate']) nch = mat['sig'].shape[1] # assume Biosemi always has the same number of channels if nch == 73: ch_names = CAP['BIOSEMI_64'] extra_ch = nch - len(CAP['BIOSEMI_64_INFO']) extra_names = [] for ch in range(extra_ch): extra_names.append('EXTRA%d' % ch) ch_names = ch_names + extra_names ch_info = CAP['BIOSEMI_64_INFO'] + ['misc'] * extra_ch else: logger.warning('Unrecognized number of channels (%d)' % nch) logger.warning( 'The last channel will be assumed to be trigger. Press Enter to continue, or Ctrl+C to break.' ) if interactive: input() # Set the trigger to be channel 0 because later we will move it to channel 0. ch_names = ['TRIGGER'] + ['CH%d' % (x + 1) for x in range(nch - 1)] ch_info = ['stim'] + ['eeg'] * (nch - 1) signals_raw = mat['sig'].T # -> channels x samples # Note: Biosig's sload() sometimes returns bogus event values so we use the following for events bdf = mne.io.read_raw_edf(filename, preload=True) events = mne.find_events(bdf, stim_channel='TRIGGER', shortest_event=1, consecutive=True) # signals_raw[-1][:]= bdf._data[-1][:] # overwrite with the correct event values # Move the event channel to 0 (for consistency) signals = np.concatenate( (signals_raw[-1, :].reshape(1, -1), signals_raw[:-1, :])) signals[0] *= 0 # init the event channel info = mne.create_info(ch_names, sample_rate, ch_info, montage='standard_1005') # create Raw object raw = mne.io.RawArray(signals, info) # add events raw.add_events(events, 'TRIGGER') # save and close raw.save(fiffile, verbose=False, overwrite=True, fmt='double') logger.info('Saved to %s' % fiffile) saveChannels2txt(outdir, ch_names)