def test_set_eeg_reference(): """Test rereference eeg data""" raw = Raw(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(ref_data is None) # Test setting an average reference when one was already present with warnings.catch_warnings(record=True): # weight tables reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def test_set_eeg_reference(): """ Test rereference eeg data""" raw = Raw(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) # Separate EEG channels from other channel types picks_eeg = pick_types(raw.info, meg=False, eeg=True, exclude='bads') picks_other = pick_types(raw.info, meg=True, eeg=False, eog=True, stim=True, exclude='bads') # Get the raw EEG data and other channel data raw_eeg_data = raw[picks_eeg][0] raw_other_data = raw[picks_other][0] # Get the rereferenced EEG data and channel other reref_eeg_data = reref[picks_eeg][0] unref_eeg_data = reref_eeg_data + ref_data # Undo rereferencing of EEG channels reref_other_data = reref[picks_other][0] # Check that both EEG data and other data is the same assert_allclose(raw_eeg_data, unref_eeg_data, 1e-6, atol=1e-15) assert_allclose(raw_other_data, reref_other_data, 1e-6, atol=1e-15) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def test_set_eeg_reference(): """ Test rereference eeg data""" raw = Raw(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) # Separate EEG channels from other channel types picks_eeg = pick_types(raw.info, meg=False, eeg=True, exclude='bads') picks_other = pick_types(raw.info, meg=True, eeg=False, eog=True, stim=True, exclude='bads') # Get the raw EEG data and other channel data raw_eeg_data = raw[picks_eeg][0] raw_other_data = raw[picks_other][0] # Get the rereferenced EEG data and channel other reref_eeg_data = reref[picks_eeg][0] unref_eeg_data = reref_eeg_data + ref_data # Undo rereferencing of EEG channels reref_other_data = reref[picks_other][0] # Check that both EEG data and other data is the same assert_array_equal(raw_eeg_data, unref_eeg_data) assert_array_equal(raw_other_data, reref_other_data) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def test_set_eeg_reference(): """Test rereference eeg data""" raw = Raw(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def test_set_eeg_reference(): """Test rereference eeg data""" raw = Raw(fif_fname, preload=True) raw.info["projs"] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info["projs"])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info["projs"])) assert_true(ref_data is None) # Test setting an average reference when one was already present reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ["EEG 001", "EEG 002"], copy=True) assert_true(reref.info["custom_ref_applied"]) _test_reference(raw, reref, ref_data, ["EEG 001", "EEG 002"]) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ["EEG 001", "EEG 002"], copy=False) assert_true(raw is reref)
def test_set_eeg_reference(): """Test rereference eeg data""" raw = Raw(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref) # Setting an average reference on a dataset that already contains a custom # reference should fail assert_raises(RuntimeError, set_eeg_reference, reref)
# if i<ntrials-1: # for j in np.arange(0,19,1): # true_eves = np.insert(true_eves, i*20+1, [true_eves_bk[i,0] + (j+1)*4096*2, 0, true_eves_bk[i,2]], axis=0) # ## true_eves[i*20+1,0] = true_eves[i*20,0] + (j+1)*4096 # elif i == ntrials-1: # for j in np.arange(0,19,1): # true_eves = np.append(true_eves, np.asarray([true_eves_bk[-1,0] + (j+1)*4096*2, 0, true_eves_bk[-1,2]]).reshape(1,3),axis=0) # #true_eves = true_eves[true_eves[:,0].argsort()] refChannels = ['EXG1', 'EXG2'] print 'Re-referencing data to', refChannels (raw, ref_data) = set_eeg_reference(raw, refChannels, copy=False) raw.filter(l_freq=70, h_freq=1500, picks=np.arange(0, 32, 1), n_jobs=8) if subj == 'C015': raw.info['bads'] = ['A20'] #elif subj == 'C012': # raw.info['bads'] = ['A3','A6','A7','A23','A24', 'A25','A27', 'A28'] if subj == 'S007': raw.info['bads'] = ['A17', 'A18'] # for S007 trig_list = np.unique(true_eves_bk[:,2]) n_trigs = trig_list.shape[0] conds = {}
def importbdf(bdfname, nchans=34, refchans=['EXG1', 'EXG2'], hptsname=None, mask=255, extrachans=[], exclude=None, verbose=None): """Wrapper around mne-python to import BDF files Parameters ---------- bdfname - Name of the biosemi .bdf filename with full path nchans - Number of EEG channels (including references) (Optional) By default, 34 (32 + 2 references) refchans - list of strings with reference channel names (Optional) By default ['EXG1','EXG2']. Use None for average reference. hptsname - Name of the electrode position file in .hpts format with path (Optional) By default a 32 channel Biosemi layout is used. If the nchans is >= 64 and < 96, a 64 channel Biosemi layout is used. If nchans >= 96, a 96 channel biosemi layout is used. Formats other than .hpts will also likely work, but behavior may vary. mask - Integer mask to use for trigger channel (Default is 255). extrachans - Additional channels other than EEG and EXG that may be in the bdf file. These will be marked as MISC in mne-python. Specify as list of names. excllude - List of channel names to exclude from importing verbose - bool, str, int, or None (Optional) The verbosity of messages to print. If a str, it can be either DEBUG, INFO, WARNING, ERROR, or CRITICAL. Returns ------- raw - MNE raw data object of rereferences and preloaded data eves - Event list (3 column array as required by mne.Epochs) Requires -------- mne-python module > release 0.7 """ # Default HPTS file if(hptsname is None): anlffr_root = os.path.dirname(sys.modules['anlffr'].__file__) if nchans >= 64 and nchans < 96: logger.info('Number of channels is greater than 64.' ' Hence loading a 64 channel montage.') hptspath = os.path.join(anlffr_root, 'helper/sysfiles/') hptsname = 'biosemi64' montage = read_montage(kind=hptsname, path=hptspath, transform=True) misc = ['EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'] else: if nchans >= 96: logger.info('Number of channels is greater than 96.' ' Hence loading a 96 channel montage.') hptspath = os.path.join(anlffr_root, 'helper/sysfiles/') hptsname = 'biosemi96' montage = read_montage(kind=hptsname, path=hptspath, transform=True) misc = ['EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'] else: if nchans == 2: logger.info('Number of channels is 2.' 'Guessing ABR montage.') montage = None misc = [] else: logger.info('Loading a default 32 channel montage.') hptspath = os.path.join(anlffr_root, 'helper/sysfiles/') hptsname = 'biosemi32' montage = read_montage(kind=hptsname, path=hptspath, transform=True) misc = ['EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'] misc += extrachans raw = edf.read_raw_edf(bdfname, montage=montage, preload=True, misc=misc, exclude=exclude, stim_channel='Status') # Rereference if refchans is not None: sys.stdout.write('Re-referencing data to: ' + ' '.join(refchans)) (raw, ref_data) = set_eeg_reference(raw, refchans, copy=False) raw.info['bads'] += refchans else: # Add average reference operator for possible use later ave_ref_operator = make_eeg_average_ref_proj(raw.info, activate=False) raw = raw.add_proj(ave_ref_operator) eves = find_events(raw, shortest_event=1, mask=mask) return (raw, eves)
def importbdf(bdfname, nchans=34, refchans=['EXG1', 'EXG2'], hptsname=None, mask=255, extrachans=[], exclude=None, verbose=None): """Wrapper around mne-python to import BDF files Parameters ---------- bdfname - Name of the biosemi .bdf filename with full path nchans - Number of EEG channels (including references) (Optional) By default, 34 (32 + 2 references) refchans - list of strings with reference channel names (Optional) By default ['EXG1','EXG2']. Use None for average reference. hptsname - Name of the electrode position file in .hpts format with path (Optional) By default a 32 channel Biosemi layout is used. If the nchans is >= 64 and < 96, a 64 channel Biosemi layout is used. If nchans >= 96, a 96 channel biosemi layout is used. Formats other than .hpts will also likely work, but behavior may vary. mask - Integer mask to use for trigger channel (Default is 255). extrachans - Additional channels other than EEG and EXG that may be in the bdf file. These will be marked as MISC in mne-python. Specify as list of names. excllude - List of channel names to exclude from importing verbose - bool, str, int, or None (Optional) The verbosity of messages to print. If a str, it can be either DEBUG, INFO, WARNING, ERROR, or CRITICAL. Returns ------- raw - MNE raw data object of rereferences and preloaded data eves - Event list (3 column array as required by mne.Epochs) Requires -------- mne-python module > release 0.7 """ # Default HPTS file if (hptsname is None): anlffr_root = os.path.dirname(sys.modules['anlffr'].__file__) if nchans >= 64 and nchans < 96: logger.info('Number of channels is greater than 64.' ' Hence loading a 64 channel montage.') hptspath = os.path.join(anlffr_root, 'helper/sysfiles/') hptsname = 'biosemi64' montage = read_montage(kind=hptsname, path=hptspath, transform=True) misc = ['EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'] else: if nchans >= 96: logger.info('Number of channels is greater than 96.' ' Hence loading a 96 channel montage.') hptspath = os.path.join(anlffr_root, 'helper/sysfiles/') hptsname = 'biosemi96' montage = read_montage(kind=hptsname, path=hptspath, transform=True) misc = ['EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'] else: if nchans == 2: logger.info('Number of channels is 2.' 'Guessing ABR montage.') montage = None misc = [] else: logger.info('Loading a default 32 channel montage.') hptspath = os.path.join(anlffr_root, 'helper/sysfiles/') hptsname = 'biosemi32' montage = read_montage(kind=hptsname, path=hptspath, transform=True) misc = ['EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'] misc += extrachans raw = edf.read_raw_edf(bdfname, montage=montage, preload=True, misc=misc, exclude=exclude, stim_channel='Status') # Rereference if refchans is not None: sys.stdout.write('Re-referencing data to: ' + ' '.join(refchans)) (raw, ref_data) = set_eeg_reference(raw, refchans, copy=False) raw.info['bads'] += refchans else: # Add average reference operator for possible use later ave_ref_operator = make_eeg_average_ref_proj(raw.info, activate=False) raw = raw.add_proj(ave_ref_operator) eves = find_events(raw, shortest_event=1, mask=mask) return (raw, eves)