@slow_test
@testing.requires_testing_data
def test_filter():
 raw = read_raw_fif(fif_fname).crop(0, 7)
 raw.load_data()
 sig_dec_notch = 12
 sig_dec_notch_fit = 12
 picks_meg = pick_types(raw.info, meg=True, exclude='bads')
 picks = picks_meg[:4]
 trans = 2.0
 filter_params = dict(picks=picks, filter_length='auto', h_trans_bandwidth=trans, l_trans_bandwidth=trans)
 raw_lp = raw.copy().filter(None, 8.0, **filter_params)
 raw_hp = raw.copy().filter(16.0, None, **filter_params)
 raw_bp = raw.copy().filter((8.0 + trans), (16.0 - trans), **filter_params)
 raw_bs = raw.copy().filter(16.0, 8.0, **filter_params)
 (data, _) = raw[picks, :]
 (lp_data, _) = raw_lp[picks, :]
 (hp_data, _) = raw_hp[picks, :]
 (bp_data, _) = raw_bp[picks, :]
 (bs_data, _) = raw_bs[picks, :]
 tols = dict(atol=1e-20, rtol=1e-05)
 assert_allclose(bs_data, (lp_data + hp_data), **tols)
 assert_allclose(data, ((lp_data + bp_data) + hp_data), **tols)
 assert_allclose(data, (bp_data + bs_data), **tols)
 filter_params_iir = dict(picks=picks, n_jobs=2, method='iir', iir_params=dict(output='ba'))
 raw_lp_iir = raw.copy().filter(None, 4.0, **filter_params_iir)
 raw_hp_iir = raw.copy().filter(8.0, None, **filter_params_iir)
 raw_bp_iir = raw.copy().filter(4.0, 8.0, **filter_params_iir)
 del filter_params_iir
 (lp_data_iir, _) = raw_lp_iir[picks, :]
 (hp_data_iir, _) = raw_hp_iir[picks, :]
 (bp_data_iir, _) = raw_bp_iir[picks, :]
 summation = ((lp_data_iir + hp_data_iir) + bp_data_iir)
 assert_array_almost_equal(data[:, 100:(-100)], summation[:, 100:(-100)], 11)
 (data, _) = raw[picks_meg[4:], :]
 (bp_data, _) = raw_bp[picks_meg[4:], :]
 assert_array_equal(data, bp_data)
 (bp_data_iir, _) = raw_bp_iir[picks_meg[4:], :]
 assert_array_equal(data, bp_data_iir)
 raw_copy = raw.copy()
 raw_copy.filter(None, 20.0, n_jobs=2, **filter_params)
 assert_true((raw._data[(0, 0)] != raw_copy._data[(0, 0)]))
 assert_equal(raw.copy().filter(None, 20.0, **filter_params)._data, raw_copy._data)
 with warnings.catch_warnings(record=True):
  warnings.simplefilter('always')
  raw_bs = raw.copy().filter((60.0 + trans), (60.0 - trans), **filter_params)
  (data_bs, _) = raw_bs[picks, :]
  raw_notch = raw.copy().notch_filter(60.0, picks=picks, n_jobs=2, method='fir', trans_bandwidth=(2 * trans))
 (data_notch, _) = raw_notch[picks, :]
 assert_array_almost_equal(data_bs, data_notch, sig_dec_notch)
 raw_notch = raw.copy().notch_filter(None, picks=picks, n_jobs=2, method='spectrum_fit')
 (data_notch, _) = raw_notch[picks, :]
 (data, _) = raw[picks, :]
 assert_array_almost_equal(data, data_notch, sig_dec_notch_fit)
 raw = RawArray(np.random.randn(3, 1000), create_info(3, 1000.0, ((['eeg'] * 2) + ['stim'])))
 raw.info['lowpass'] = raw.info['highpass'] = None
 for kind in ('none', 'lowpass', 'highpass', 'bandpass', 'bandstop'):
  print kind
  h_freq = l_freq = None
  if (kind in ('lowpass', 'bandpass')):
   h_freq = 70
  if (kind in ('highpass', 'bandpass')):
   l_freq = 30
  if (kind == 'bandstop'):
   (l_freq, h_freq) = (70, 30)
  assert_true((raw.info['lowpass'] is None))
  assert_true((raw.info['highpass'] is None))
  kwargs = dict(l_trans_bandwidth=20, h_trans_bandwidth=20, filter_length='auto', phase='zero', fir_window='hann')
  raw_filt = raw.copy().filter(l_freq, h_freq, picks=np.arange(1), **kwargs)
  assert_true((raw.info['lowpass'] is None))
  assert_true((raw.info['highpass'] is None))
  raw_filt = raw.copy().filter(l_freq, h_freq, **kwargs)
  wanted_h = (h_freq if (kind != 'bandstop') else None)
  wanted_l = (l_freq if (kind != 'bandstop') else None)
  assert_equal(raw_filt.info['lowpass'], wanted_h)
  assert_equal(raw_filt.info['highpass'], wanted_l)
  raw_filt = raw.copy().filter(l_freq, h_freq, picks=np.arange(2), **kwargs)
  assert_equal(raw_filt.info['lowpass'], wanted_h)
  assert_equal(raw_filt.info['highpass'], wanted_l)

@ slow_test @ testing . requires_testing_data def test_filter ( ) : raw = read_raw_fif ( fif_fname ) . crop ( 0 , 7 ) raw . load_data ( ) sig_dec_notch = 12 sig_dec_notch_fit = 12 picks_meg = pick_types ( raw . info , meg = True , exclude = 'bads' ) picks = picks_meg [ : 4 ] trans = 2.0 filter_params = dict ( picks = picks , filter_length = 'auto' , h_trans_bandwidth = trans , l_trans_bandwidth = trans ) raw_lp = raw . copy ( ) . filter ( None , 8.0 , ** filter_params ) raw_hp = raw . copy ( ) . filter ( 16.0 , None , ** filter_params ) raw_bp = raw . copy ( ) . filter ( ( 8.0 + trans ) , ( 16.0 - trans ) , ** filter_params ) raw_bs = raw . copy ( ) . filter ( 16.0 , 8.0 , ** filter_params ) ( data , _ ) = raw [ picks , : ] ( lp_data , _ ) = raw_lp [ picks , : ] ( hp_data , _ ) = raw_hp [ picks , : ] ( bp_data , _ ) = raw_bp [ picks , : ] ( bs_data , _ ) = raw_bs [ picks , : ] tols = dict ( atol = 1e-20 , rtol = 1e-05 ) assert_allclose ( bs_data , ( lp_data + hp_data ) , ** tols ) assert_allclose ( data , ( ( lp_data + bp_data ) + hp_data ) , ** tols ) assert_allclose ( data , ( bp_data + bs_data ) , ** tols ) filter_params_iir = dict ( picks = picks , n_jobs = 2 , method = 'iir' , iir_params = dict ( output = 'ba' ) ) raw_lp_iir = raw . copy ( ) . filter ( None , 4.0 , ** filter_params_iir ) raw_hp_iir = raw . copy ( ) . filter ( 8.0 , None , ** filter_params_iir ) raw_bp_iir = raw . copy ( ) . filter ( 4.0 , 8.0 , ** filter_params_iir ) del filter_params_iir ( lp_data_iir , _ ) = raw_lp_iir [ picks , : ] ( hp_data_iir , _ ) = raw_hp_iir [ picks , : ] ( bp_data_iir , _ ) = raw_bp_iir [ picks , : ] summation = ( ( lp_data_iir + hp_data_iir ) + bp_data_iir ) assert_array_almost_equal ( data [ : , 100 : ( - 100 ) ] , summation [ : , 100 : ( - 100 ) ] , 11 ) ( data , _ ) = raw [ picks_meg [ 4 : ] , : ] ( bp_data , _ ) = raw_bp [ picks_meg [ 4 : ] , : ] assert_array_equal ( data , bp_data ) ( bp_data_iir , _ ) = raw_bp_iir [ picks_meg [ 4 : ] , : ] assert_array_equal ( data , bp_data_iir ) raw_copy = raw . copy ( ) raw_copy . filter ( None , 20.0 , n_jobs = 2 , ** filter_params ) assert_true ( ( raw . _data [ ( 0 , 0 ) ] != raw_copy . _data [ ( 0 , 0 ) ] ) ) assert_equal ( raw . copy ( ) . filter ( None , 20.0 , ** filter_params ) . _data , raw_copy . _data ) with warnings . catch_warnings ( record = True ) : warnings . simplefilter ( 'always' ) raw_bs = raw . copy ( ) . filter ( ( 60.0 + trans ) , ( 60.0 - trans ) , ** filter_params ) ( data_bs , _ ) = raw_bs [ picks , : ] raw_notch = raw . copy ( ) . notch_filter ( 60.0 , picks = picks , n_jobs = 2 , method = 'fir' , trans_bandwidth = ( 2 * trans ) ) ( data_notch , _ ) = raw_notch [ picks , : ] assert_array_almost_equal ( data_bs , data_notch , sig_dec_notch ) raw_notch = raw . copy ( ) . notch_filter ( None , picks = picks , n_jobs = 2 , method = 'spectrum_fit' ) ( data_notch , _ ) = raw_notch [ picks , : ] ( data , _ ) = raw [ picks , : ] assert_array_almost_equal ( data , data_notch , sig_dec_notch_fit ) raw = RawArray ( np . random . randn ( 3 , 1000 ) , create_info ( 3 , 1000.0 , ( ( [ 'eeg' ] * 2 ) + [ 'stim' ] ) ) ) raw . info [ 'lowpass' ] = raw . info [ 'highpass' ] = None for kind in ( 'none' , 'lowpass' , 'highpass' , 'bandpass' , 'bandstop' ) : print kind h_freq = l_freq = None if ( kind in ( 'lowpass' , 'bandpass' ) ) : h_freq = 70 if ( kind in ( 'highpass' , 'bandpass' ) ) : l_freq = 30 if ( kind == 'bandstop' ) : ( l_freq , h_freq ) = ( 70 , 30 ) assert_true ( ( raw . info [ 'lowpass' ] is None ) ) assert_true ( ( raw . info [ 'highpass' ] is None ) ) kwargs = dict ( l_trans_bandwidth = 20 , h_trans_bandwidth = 20 , filter_length = 'auto' , phase = 'zero' , fir_window = 'hann' ) raw_filt = raw . copy ( ) . filter ( l_freq , h_freq , picks = np . arange ( 1 ) , ** kwargs ) assert_true ( ( raw . info [ 'lowpass' ] is None ) ) assert_true ( ( raw . info [ 'highpass' ] is None ) ) raw_filt = raw . copy ( ) . filter ( l_freq , h_freq , ** kwargs ) wanted_h = ( h_freq if ( kind != 'bandstop' ) else None ) wanted_l = ( l_freq if ( kind != 'bandstop' ) else None ) assert_equal ( raw_filt . info [ 'lowpass' ] , wanted_h ) assert_equal ( raw_filt . info [ 'highpass' ] , wanted_l ) raw_filt = raw . copy ( ) . filter ( l_freq , h_freq , picks = np . arange ( 2 ) , ** kwargs ) assert_equal ( raw_filt . info [ 'lowpass' ] , wanted_h ) assert_equal ( raw_filt . info [ 'highpass' ] , wanted_l )
test a network filter to verify if its valid .