def test_cuda(): """Test CUDA-based filtering """ Fs = 500 sig_len_secs = 20 a = np.random.randn(sig_len_secs * Fs) set_log_file(log_file, overwrite=True) for fl in [None, 2048]: bp = band_pass_filter(a, Fs, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, Fs, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, Fs, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, Fs, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, Fs, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, Fs, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA set_log_file() out = open(log_file).readlines() assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == 8)
def test_cuda(): """Test CUDA-based filtering""" # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing # Also, using `n_jobs='cuda'` on a non-CUDA system should be fine, # as it should fall back to using n_jobs=1. sfreq = 500 sig_len_secs = 20 a = rng.randn(sig_len_secs * sfreq) with catch_logging() as log_file: for fl in ['auto', '10s', 2048]: bp = band_pass_filter(a, sfreq, 4, 8, fl, 1.0, 1.0, n_jobs=1, phase='zero') bs = band_stop_filter(a, sfreq, 4 - 1.0, 8 + 1.0, fl, 1.0, 1.0, n_jobs=1, phase='zero') lp = low_pass_filter(a, sfreq, 8, fl, 1.0, n_jobs=1, phase='zero') hp = high_pass_filter(lp, sfreq, 4, fl, 1.0, n_jobs=1, phase='zero') bp_c = band_pass_filter(a, sfreq, 4, 8, fl, 1.0, 1.0, n_jobs='cuda', verbose='INFO', phase='zero') bs_c = band_stop_filter(a, sfreq, 4 - 1.0, 8 + 1.0, fl, 1.0, 1.0, n_jobs='cuda', verbose='INFO', phase='zero') lp_c = low_pass_filter(a, sfreq, 8, fl, 1.0, n_jobs='cuda', verbose='INFO', phase='zero') hp_c = high_pass_filter(lp, sfreq, 4, fl, 1.0, n_jobs='cuda', verbose='INFO', phase='zero') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA out = log_file.getvalue().split('\n')[:-1] # triage based on whether or not we actually expected to use CUDA from mne.cuda import _cuda_capable # allow above funs to set it tot = 12 if _cuda_capable else 0 assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == tot) # check resampling for window in ('boxcar', 'triang'): for N in (997, 1000): # one prime, one even a = rng.randn(2, N) for fro, to in ((1, 2), (2, 1), (1, 3), (3, 1)): a1 = resample(a, fro, to, n_jobs=1, npad='auto', window=window) a2 = resample(a, fro, to, n_jobs='cuda', npad='auto', window=window) assert_allclose(a1, a2, rtol=1e-7, atol=1e-14) assert_array_almost_equal(a1, a2, 14) assert_array_equal(resample([0, 0], 2, 1, n_jobs='cuda'), [0., 0., 0., 0.]) assert_array_equal(resample(np.zeros(2, np.float32), 2, 1, n_jobs='cuda'), [0., 0., 0., 0.])
def test_filters(): Fs = 500 sig_len_secs = 60 # Filtering of short signals (filter length = len(a)) a = np.random.randn(sig_len_secs * Fs) bp = band_pass_filter(a, Fs, 4, 8) lp = low_pass_filter(a, Fs, 8) hp = high_pass_filter(lp, Fs, 4) assert_array_almost_equal(hp, bp, 2) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, Fs, 4, 8, filter_length) lp_oa = low_pass_filter(a, Fs, 8, filter_length) hp_oa = high_pass_filter(lp_oa, Fs, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 1000 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2)
def filter_and_make_analytic_signal(data, sfreq, l_phase_freq, h_phase_freq, l_amp_freq, h_amp_freq, method='fft', n_jobs=1): """ Filter data to required range and compute analytic signal from it. Parameters ---------- data : ndarray The signal to be analysed. l_phase_freq, h_phase_freq : float Low and high phase modulating frequencies. l_amp_freq, h_amp_freq : float Low and high amplitude modulated frequencies. method : 'fft' or 'iir' Filter method to be used. (mne.filter.band_pass_filter) n_jobs : int Number of parallel jobs to run. Returns ------- theta : ndarray Low frequency filtered signal (modulating) gamma : ndarray High frequency filtered signal (modulated) phase : ndarray Phase of low frequency signal above. amp : ndarray Amplitude envelope of the high freq. signal above. """ # filter theta and gamma signals n_jobs = 4 method = 'fft' l_phase_freq, h_phase_freq, l_amp_freq, h_amp_freq = 6, 10, 60, 150 theta = band_pass_filter(data, sfreq, l_phase_freq, h_phase_freq, method=method, n_jobs=n_jobs) gamma = band_pass_filter(data, sfreq, l_amp_freq, h_amp_freq, method=method, n_jobs=n_jobs) # phase of the low freq modulating signal phase = np.angle(hilbert(theta)) # amplitude envelope of the high freq modulated signal amp = np.abs(hilbert(gamma)) return theta, gamma, phase, amp
def test_cuda(): """Test CUDA-based filtering """ # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing # Also, using `n_jobs='cuda'` on a non-CUDA system should be fine, # as it should fall back to using n_jobs=1. tempdir = _TempDir() log_file = op.join(tempdir, 'temp_log.txt') sfreq = 500 sig_len_secs = 20 a = np.random.randn(sig_len_secs * sfreq) set_log_file(log_file, overwrite=True) for fl in ['10s', None, 2048]: bp = band_pass_filter(a, sfreq, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, sfreq, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, sfreq, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, sfreq, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, sfreq, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, sfreq, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA set_log_file() with open(log_file) as fid: out = fid.readlines() # triage based on whether or not we actually expected to use CUDA from mne.cuda import _cuda_capable # allow above funs to set it tot = 12 if _cuda_capable else 0 assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == tot) # check resampling a = np.random.RandomState(0).randn(3, sig_len_secs * sfreq) a1 = resample(a, 1, 2, n_jobs=2, npad=0) a2 = resample(a, 1, 2, n_jobs='cuda', npad=0) a3 = resample(a, 2, 1, n_jobs=2, npad=0) a4 = resample(a, 2, 1, n_jobs='cuda', npad=0) assert_array_almost_equal(a3, a4, 14) assert_array_almost_equal(a1, a2, 14) assert_array_equal(resample([0, 0], 2, 1, n_jobs='cuda'), [0., 0., 0., 0.]) assert_array_equal(resample(np.zeros(2, np.float32), 2, 1, n_jobs='cuda'), [0., 0., 0., 0.])
def modulation_index2d(data, sfreq): """ Compute the two dimensional modulation index. Parameters ---------- data : ndarray The signal data sfreq: float Sampling frequency Returns ------- mod2d : ndarray 2 dimensional modulation index """ from mne.filter import band_pass_filter from scipy.signal import hilbert flow = np.arange(2, 40, 1) flow_step = 1.0 fhigh = np.arange(5, 205, 5) fhigh_step = 5.0 mod2d = np.zeros((flow.size, fhigh.size)) method = 'fft' n_jobs = 2 for i in range(0, flow.size): theta = band_pass_filter(data, sfreq, flow[i], flow[i] + flow_step, method=method, n_jobs=n_jobs) theta = theta[sfreq:data.size - sfreq] phase = np.angle(hilbert(theta)) for j in range(0, fhigh.size): gamma = band_pass_filter(data, sfreq, fhigh[j], fhigh[j] + fhigh_step, method=method, n_jobs=n_jobs) gamma = gamma[sfreq:data.size - sfreq] amp = np.abs(hilbert(gamma)) # compute the modulation index m_norm_length = modulation_index1d(amp, phase, sfreq) mod2d[i, j] = m_norm_length return mod2d
def test_cuda(): """Test CUDA-based filtering """ # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing # Also, using `n_jobs='cuda'` on a non-CUDA system should be fine, # as it should fall back to using n_jobs=1. tempdir = _TempDir() log_file = op.join(tempdir, 'temp_log.txt') sfreq = 500 sig_len_secs = 20 a = np.random.randn(sig_len_secs * sfreq) set_log_file(log_file, overwrite=True) for fl in ['10s', None, 2048]: bp = band_pass_filter(a, sfreq, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, sfreq, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, sfreq, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, sfreq, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, sfreq, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, sfreq, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA set_log_file() with open(log_file) as fid: out = fid.readlines() # triage based on whether or not we actually expected to use CUDA tot = 12 if cuda_capable else 0 assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == tot) # check resampling a = np.random.RandomState(0).randn(3, sig_len_secs * sfreq) a1 = resample(a, 1, 2, n_jobs=2, npad=0) a2 = resample(a, 1, 2, n_jobs='cuda', npad=0) a3 = resample(a, 2, 1, n_jobs=2, npad=0) a4 = resample(a, 2, 1, n_jobs='cuda', npad=0) assert_array_almost_equal(a3, a4, 14) assert_array_almost_equal(a1, a2, 14)
def test_cuda(): """Test CUDA-based filtering """ # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing # Also, using `n_jobs='cuda'` on a non-CUDA system should be fine, # as it should fall back to using n_jobs=1. sfreq = 500 sig_len_secs = 20 a = rng.randn(sig_len_secs * sfreq) with catch_logging() as log_file: for fl in ['10s', None, 2048]: bp = band_pass_filter(a, sfreq, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, sfreq, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, sfreq, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, sfreq, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, sfreq, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, sfreq, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA out = log_file.getvalue().split('\n')[:-1] # triage based on whether or not we actually expected to use CUDA from mne.cuda import _cuda_capable # allow above funs to set it tot = 12 if _cuda_capable else 0 assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == tot) # check resampling a = rng.randn(3, sig_len_secs * sfreq) a1 = resample(a, 1, 2, n_jobs=2, npad=0) a2 = resample(a, 1, 2, n_jobs='cuda', npad=0) a3 = resample(a, 2, 1, n_jobs=2, npad=0) a4 = resample(a, 2, 1, n_jobs='cuda', npad=0) assert_array_almost_equal(a3, a4, 14) assert_array_almost_equal(a1, a2, 14) assert_array_equal(resample([0, 0], 2, 1, n_jobs='cuda'), [0., 0., 0., 0.]) assert_array_equal(resample(np.zeros(2, np.float32), 2, 1, n_jobs='cuda'), [0., 0., 0., 0.])
def bandPassFilterConcurrent(observations): """ 500Hz alpha 채널(8~13Hz)만 분리 :param observations: :return: """ return band_pass_filter(np.float64(observations),500,8,13)
def test_cuda(): """Test CUDA-based filtering """ # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing Fs = 500 sig_len_secs = 20 a = np.random.randn(sig_len_secs * Fs) set_log_file(log_file, overwrite=True) for fl in ['10s', None, 2048]: bp = band_pass_filter(a, Fs, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, Fs, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, Fs, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, Fs, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, Fs, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, Fs, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA set_log_file() with open(log_file) as fid: out = fid.readlines() assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == 12) # check resampling a = np.random.RandomState(0).randn(3, sig_len_secs * Fs) a1 = resample(a, 1, 2, n_jobs=2, npad=0) a2 = resample(a, 1, 2, n_jobs='cuda', npad=0) a3 = resample(a, 2, 1, n_jobs=2, npad=0) a4 = resample(a, 2, 1, n_jobs='cuda', npad=0) assert_array_almost_equal(a3, a4, 14) assert_array_almost_equal(a1, a2, 14)
def modulation_index2d(data, sfreq): """ Compute the two dimensional modulation index. Parameters ---------- data : ndarray The signal data sfreq: float Sampling frequency Returns ------- mod2d : ndarray 2 dimensional modulation index """ from mne.filter import band_pass_filter from scipy.signal import hilbert flow = np.arange(2, 40, 1) flow_step = 1.0 fhigh = np.arange(5, 205, 5) fhigh_step = 5.0 mod2d = np.zeros((flow.size, fhigh.size)) method = 'fft' n_jobs = 2 for i in range(0, flow.size): theta = band_pass_filter(data, sfreq, flow[i], flow[i] + flow_step, method=method, n_jobs=n_jobs) theta = theta[sfreq: data.size - sfreq] phase = np.angle(hilbert(theta)) for j in range(0, fhigh.size): gamma = band_pass_filter(data, sfreq, fhigh[j], fhigh[j] + fhigh_step, method=method, n_jobs=n_jobs) gamma = gamma[sfreq: data.size - sfreq] amp = np.abs(hilbert(gamma)) # compute the modulation index m_norm_length = modulation_index1d(amp, phase, sfreq) mod2d[i, j] = m_norm_length return mod2d
def test_cuda(): """Test CUDA-based filtering """ # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing Fs = 500 sig_len_secs = 20 a = np.random.randn(sig_len_secs * Fs) set_log_file(log_file, overwrite=True) for fl in ['10s', None, 2048]: bp = band_pass_filter(a, Fs, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, Fs, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, Fs, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, Fs, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, Fs, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, Fs, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA set_log_file() out = open(log_file).readlines() assert_true(sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == 12) # check resampling a = np.random.RandomState(0).randn(3, sig_len_secs * Fs) a1 = resample(a, 1, 2, n_jobs=2, npad=0) a2 = resample(a, 1, 2, n_jobs='cuda', npad=0) a3 = resample(a, 2, 1, n_jobs=2, npad=0) a4 = resample(a, 2, 1, n_jobs='cuda', npad=0) assert_array_almost_equal(a3, a4, 14) assert_array_almost_equal(a1, a2, 14)
def test_filters(): """Test low-, band-, and high-pass filters""" Fs = 500 sig_len_secs = 60 # Filtering of short signals (filter length = len(a)) a = np.random.randn(sig_len_secs * Fs) bp = band_pass_filter(a, Fs, 4, 8) lp = low_pass_filter(a, Fs, 8) hp = high_pass_filter(lp, Fs, 4) assert_array_almost_equal(hp, bp, 2) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, Fs, 4, 8, filter_length) lp_oa = low_pass_filter(a, Fs, 8, filter_length) hp_oa = high_pass_filter(lp_oa, Fs, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 1000 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1), 1, 2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(range(Fs*sig_len_secs))/float(Fs) # make sinusoid close to the Nyquist frequency sig = np.sin(2*np.pi*Fs/2.2*t) # signal should disappear with 2x downsampling sig_gone = resample(sig,1,2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2)
def test_filters(): """Test low-, band-, and high-pass filters""" Fs = 500 sig_len_secs = 60 # Filtering of short signals (filter length = len(a)) a = np.random.randn(sig_len_secs * Fs) bp = band_pass_filter(a, Fs, 4, 8) lp = low_pass_filter(a, Fs, 8) hp = high_pass_filter(lp, Fs, 4) assert_array_almost_equal(hp, bp, 2) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, Fs, 4, 8, filter_length) lp_oa = low_pass_filter(a, Fs, 8, filter_length) hp_oa = high_pass_filter(lp_oa, Fs, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 1000 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1), 1, 2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(range(Fs * sig_len_secs)) / float(Fs) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * Fs / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2)
def process(self, data): if self.type == 'low-pass': return low_pass_filter(data, **self.params) elif self.type == 'high-pass': return high_pass_filter(data, **self.params) elif self.type == 'band-pass': return band_pass_filter(data, **self.params) elif self.type == 'band-stop': return band_stop_filter(data, **self.params) elif self.type == 'notch': return notch_filter(data, **self.params) else: raise ValueError('Unsupported filter type: {}'.format(self.type))
def process(self, data): # fix for new MNE requirements import numpy as np data = np.asarray(data, dtype=np.float64) if self.type == 'low-pass': return low_pass_filter(data, **self.params) elif self.type == 'high-pass': return high_pass_filter(data, **self.params) elif self.type == 'band-pass': return band_pass_filter(data, **self.params) elif self.type == 'band-stop': return band_stop_filter(data, **self.params) elif self.type == 'notch': return notch_filter(data, **self.params) else: raise ValueError('Unsupported filter type: {}'.format(self.type))
def transform(self, samples): pass_samples = pd.DataFrame(index=samples.index, columns=samples.columns, dtype=samples.values.dtype) channels = select.getchannelnames(samples) set_log_level(logging.ERROR) for ix, sample in samples.iterrows(): self.update_status(len(samples)) for channel in channels: vals = band_pass_filter( sample.loc[channel].values.astype('float64'), self.rate, self.min, self.max) pass_samples.loc[ix].loc[channel].values[:] = vals return pass_samples
def filterABDT(arrExp): frDelta = 1 frTheta = 4 frAlpha = 8 frBeta = 13 expnum = arrExp.shape[1] arrFiltered = np.ndarray((4,30,expnum,750), dtype=np.float64) for i in range(4): #alpha, beta, tetha, delta if i == 0: frFreq = frDelta; toFreq = frTheta; elif i == 1: frFreq = frTheta; toFreq = frAlpha; elif i == 2: frFreq = frAlpha; toFreq = frBeta; elif i == 3: frFreq = frBeta; toFreq = 20; for j in range(expnum): # number of experiments for k in range(arrExp.shape[0]): #channel arrFiltered[i,k,j,:] = band_pass_filter(np.float64(arrExp[k,j,:]),500,frFreq, toFreq) return arrFiltered
def filterByFrequency(arrExperiment,freqs): """ 실험단위별로 분리된 3차원 배열을 프리퀀시별로 분리하여 4차원 배열로 저장 :param arrExperiment: 실험단위 별로 분리된 3차원 배열 :return: """ frDelta = 1 frTheta = 4 frAlpha = 8 frBeta = 13 expnum = arrExperiment.shape[1] obsnum = arrExperiment.shape[2] arrExperimentFrequency = np.ndarray((len(freqs),30,expnum,obsnum), dtype=np.float64) for i in range(len(freqs)): #alpha, beta, tetha, delta freq_val = freqs[i] if freq_val == 0: frFreq = frDelta; toFreq = frTheta; elif freq_val == 1: frFreq = frTheta; toFreq = frAlpha; elif freq_val == 2: frFreq = frAlpha; toFreq = frBeta; elif freq_val == 3: frFreq = frBeta; toFreq = 20; for j in range(expnum): # number of experiments for k in range(arrExperiment.shape[0]): #channel arrExperimentFrequency[i,k,j,:] = band_pass_filter(np.float64(arrExperiment[k,j,:]),500,frFreq, toFreq) return arrExperimentFrequency
def mne_bandpass_filter(self): filter.band_pass_filter(self.data, self.fs, self.lowcut, self.highcut) return mne_filter
def multiple_band_pass(sigs, fs, frequency_range, bandwidth, n_cycles=None, filter_method='pactools'): """ Band-pass filter the signal at multiple frequencies Parameters ---------- sigs : array, shape (n_epochs, n_points) Input array to filter fs : float Sampling frequency frequency_range : float, list, or array, shape (n_frequencies, ) List of center frequency of bandpass filters. bandwidth : float Bandwidth of the bandpass filters. Use it to have a constant bandwidth for all filters. Should be None if n_cycles is not None. n_cycles : float Number of cycles of the bandpass filters. Use it to have a bandwidth proportional to the center frequency. Should be None if bandwidth is not None. filter_method : string, in {'mne', 'pactools'} Method to bandpass filter. - 'pactools' uses internal wavelet-based bandpass filter. (default) - 'mne' uses mne.filter.band_pass_filter in MNE-python package Returns ------- filtered : array, shape (n_frequencies, n_epochs, n_points) Bandpass filtered version of the input signals """ fixed_n_cycles = n_cycles sigs = np.atleast_2d(sigs) n_fft = compute_n_fft(sigs) n_epochs, n_points = sigs.shape frequency_range = np.atleast_1d(frequency_range) n_frequencies = frequency_range.shape[0] if filter_method == 'carrier': fir = Carrier() filtered = np.zeros((n_frequencies, n_epochs, n_points), dtype=np.complex128) for jj, frequency in enumerate(frequency_range): if frequency <= 0: raise ValueError("Center frequency for bandpass filter should" "be non-negative. Got %s." % (frequency, )) # evaluate the number of cycle for this bandwidth and frequency if fixed_n_cycles is None: n_cycles = 1.65 * frequency / bandwidth # --------- with mne.filter.band_pass_filter if filter_method == 'mne': from mne.filter import band_pass_filter for ii in range(n_epochs): low_sig = band_pass_filter(sigs[ii, :], Fs=fs, Fp1=frequency - bandwidth / 2.0, Fp2=frequency + bandwidth / 2.0, l_trans_bandwidth=bandwidth / 4.0, h_trans_bandwidth=bandwidth / 4.0, n_jobs=1, method='iir') filtered[jj, ii, :] = hilbert(low_sig, n_fft)[:n_points] # --------- with pactools.utils.Carrier (deprecated) elif filter_method == 'carrier': for ii in range(n_epochs): fir.design(fs, frequency, n_cycles, None, zero_mean=True) low_sig = fir.direct(sigs[ii, :]) filtered[jj, ii, :] = hilbert(low_sig, n_fft)[:n_points] # --------- with pactools.utils.BandPassFilter elif filter_method == 'pactools': fir = BandPassFilter(fs, fc=frequency, n_cycles=n_cycles, bandwidth=None, zero_mean=True, extract_complex=True) low_sig, low_sig_imag = fir.transform(sigs) filtered[jj, :, :] = low_sig + 1j * low_sig_imag return filtered
def test_cuda(): """Test CUDA-based filtering """ # NOTE: don't make test_cuda() the last test, or pycuda might spew # some warnings about clean-up failing # Also, using `n_jobs='cuda'` on a non-CUDA system should be fine, # as it should fall back to using n_jobs=1. sfreq = 500 sig_len_secs = 20 a = rng.randn(sig_len_secs * sfreq) with catch_logging() as log_file: for fl in ['10s', None, 2048]: bp = band_pass_filter(a, sfreq, 4, 8, n_jobs=1, filter_length=fl) bs = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs=1, filter_length=fl) lp = low_pass_filter(a, sfreq, 8, n_jobs=1, filter_length=fl) hp = high_pass_filter(lp, sfreq, 4, n_jobs=1, filter_length=fl) bp_c = band_pass_filter(a, sfreq, 4, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') bs_c = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, n_jobs='cuda', filter_length=fl, verbose='INFO') lp_c = low_pass_filter(a, sfreq, 8, n_jobs='cuda', filter_length=fl, verbose='INFO') hp_c = high_pass_filter(lp, sfreq, 4, n_jobs='cuda', filter_length=fl, verbose='INFO') assert_array_almost_equal(bp, bp_c, 12) assert_array_almost_equal(bs, bs_c, 12) assert_array_almost_equal(lp, lp_c, 12) assert_array_almost_equal(hp, hp_c, 12) # check to make sure we actually used CUDA out = log_file.getvalue().split('\n')[:-1] # triage based on whether or not we actually expected to use CUDA from mne.cuda import _cuda_capable # allow above funs to set it tot = 12 if _cuda_capable else 0 assert_true( sum(['Using CUDA for FFT FIR filtering' in o for o in out]) == tot) # check resampling for window in ('boxcar', 'triang'): for N in (997, 1000): # one prime, one even a = rng.randn(2, N) for fro, to in ((1, 2), (2, 1), (1, 3), (3, 1)): a1 = resample(a, fro, to, n_jobs=1, npad='auto', window=window) a2 = resample(a, fro, to, n_jobs='cuda', npad='auto', window=window) assert_allclose(a1, a2, rtol=1e-7, atol=1e-14) assert_array_almost_equal(a1, a2, 14) assert_array_equal(resample([0, 0], 2, 1, n_jobs='cuda'), [0., 0., 0., 0.]) assert_array_equal(resample(np.zeros(2, np.float32), 2, 1, n_jobs='cuda'), [0., 0., 0., 0.])
def filterAlphaPhase(_chnnDsetLst): return band_pass_filter(np.float64(_chnnDsetLst),500,8,13)
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling """ sfreq = 500 sig_len_secs = 30 a = np.random.randn(2, sig_len_secs * sfreq) # let's test our catchers for fl in ['blah', [0, 1], 1000.5, '10ss', '10']: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, filter_length=fl) for nj in ['blah', 0.5]: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, n_jobs=nj) # > Nyq/2 assert_raises(ValueError, band_pass_filter, a, sfreq, 4, sfreq / 2.) assert_raises(ValueError, low_pass_filter, a, sfreq, sfreq / 2.) # check our short-filter warning: with warnings.catch_warnings(record=True) as w: # Warning for low attenuation band_pass_filter(a, sfreq, 1, 8, filter_length=1024) # Warning for too short a filter band_pass_filter(a, sfreq, 1, 8, filter_length='0.5s') assert_true(len(w) >= 2) # try new default and old default for fl in ['10s', '5000ms', None]: bp = band_pass_filter(a, sfreq, 4, 8, filter_length=fl) bs = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, filter_length=fl) lp = low_pass_filter(a, sfreq, 8, filter_length=fl, n_jobs=2) hp = high_pass_filter(lp, sfreq, 4, filter_length=fl) assert_array_almost_equal(hp, bp, 2) assert_array_almost_equal(bp + bs, a, 1) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, sfreq, 4, 8, filter_length) bs_oa = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, filter_length) lp_oa = low_pass_filter(a, sfreq, 8, filter_length) hp_oa = high_pass_filter(lp_oa, sfreq, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) # Our filters are no longer quite complementary with linear rolloffs :( # this is the tradeoff for stability of the filtering # obtained by directly using the result of firwin2 instead of # modifying it... assert_array_almost_equal(bp_oa + bs_oa, a, 1) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 0 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp_oa, 2 * bp_oa.shape[-1], axis=-1, window='boxcar'), bp_oa.shape[-1], window='boxcar', axis=-1) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(list(range(sfreq * sig_len_secs))) / float(sfreq) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * sfreq / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20) iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_true(iir_params['a'].size - 1 == 3) assert_true(iir_params['b'].size - 1 == 3) iir_params = dict(ftype='butter', order=4) iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_true(iir_params['a'].size - 1 == 4) assert_true(iir_params['b'].size - 1 == 4) # check that picks work for 3d array with one channel and picks=[0] a = np.random.randn(5 * sfreq, 5 * sfreq) b = a[:, None, :] with warnings.catch_warnings(record=True) as w: a_filt = band_pass_filter(a, sfreq, 4, 8) b_filt = band_pass_filter(b, sfreq, 4, 8, picks=[0]) assert_array_equal(a_filt[:, None, :], b_filt) # check for n-dimensional case a = np.random.randn(2, 2, 2, 2) assert_raises(ValueError, band_pass_filter, a, sfreq, Fp1=4, Fp2=8, picks=np.array([0, 1])) # test that our overlap-add filtering doesn't introduce strange # artifacts (from mne_analyze mailing list 2015/06/25) N = 300 sfreq = 100. lp = 10. sine_freq = 1. x = np.ones(N) x += np.sin(2 * np.pi * sine_freq * np.arange(N) / sfreq) with warnings.catch_warnings(record=True): # filter attenuation x_filt = low_pass_filter(x, sfreq, lp, '1s') # the firwin2 function gets us this close assert_allclose(x, x_filt, rtol=1e-3, atol=1e-3)
def filter(l_freq, h_freq, picks=None, filter_length='10s', l_trans_bandwidth=0.5, h_trans_bandwidth=0.5, n_jobs=1, method='fft', iir_params=None, verbose=None): """Filter a subset of channels. Applies a zero-phase low-pass, high-pass, band-pass, or band-stop filter to the channels selected by "picks". The data of the Raw object is modified inplace. The Raw object has to be constructed using preload=True (or string). l_freq and h_freq are the frequencies below which and above which, respectively, to filter out of the data. Thus the uses are: * ``l_freq < h_freq``: band-pass filter * ``l_freq > h_freq``: band-stop filter * ``l_freq is not None and h_freq is None``: high-pass filter * ``l_freq is None and h_freq is not None``: low-pass filter If n_jobs > 1, more memory is required as "len(picks) * n_times" additional time points need to be temporarily stored in memory. raw.info['lowpass'] and raw.info['highpass'] are only updated with picks=None. Parameters ---------- l_freq : float | None Low cut-off frequency in Hz. If None the data are only low-passed. h_freq : float | None High cut-off frequency in Hz. If None the data are only high-passed. picks : array-like of int | None Indices of channels to filter. If None only the data (MEG/EEG) channels will be filtered. filter_length : str (Default: '10s') | int | None Length of the filter to use. If None or "len(x) < filter_length", the filter length used is len(x). Otherwise, if int, overlap-add filtering with a filter of the specified length in samples) is used (faster for long signals). If str, a human-readable time in units of "s" or "ms" (e.g., "10s" or "5500ms") will be converted to the shortest power-of-two length at least that duration. Not used for 'iir' filters. l_trans_bandwidth : float Width of the transition band at the low cut-off frequency in Hz (high pass or cutoff 1 in bandpass). Not used if 'order' is specified in iir_params. h_trans_bandwidth : float Width of the transition band at the high cut-off frequency in Hz (low pass or cutoff 2 in bandpass). Not used if 'order' is specified in iir_params. n_jobs : int | str Number of jobs to run in parallel. Can be 'cuda' if scikits.cuda is installed properly, CUDA is initialized, and method='fft'. method : str 'fft' will use overlap-add FIR filtering, 'iir' will use IIR forward-backward filtering (via filtfilt). iir_params : dict | None Dictionary of parameters to use for IIR filtering. See mne.filter.construct_iir_filter for details. If iir_params is None and method="iir", 4th order Butterworth will be used. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). Defaults to raw.verbose. See Also -------- mne.Epochs.savgol_filter """ fname='ec_rest_before_tsss_mc_rsl.fif' raw = Raw(fname, preload=False) raw.preload_data() # data becomes numpy.float64 if verbose is None: verbose = raw.verbose fs = float(raw.info['sfreq']) if l_freq == 0: l_freq = None if h_freq is not None and h_freq > (fs / 2.): h_freq = None if l_freq is not None and not isinstance(l_freq, float): l_freq = float(l_freq) if h_freq is not None and not isinstance(h_freq, float): h_freq = float(h_freq) if not raw.preload: raise RuntimeError('Raw data needs to be preloaded to filter. Use ' 'preload=True (or string) in the constructor.') if picks is None: if 'ICA ' in ','.join(raw.ch_names): pick_parameters = dict(misc=True, ref_meg=False) else: pick_parameters = dict(meg=True, eeg=True, ref_meg=False) picks = pick_types(raw.info, exclude=[], **pick_parameters) # let's be safe. if len(picks) < 1: raise RuntimeError('Could not find any valid channels for ' 'your Raw object. Please contact the ' 'MNE-Python developers.') # update info if filter is applied to all data channels, # and it's not a band-stop filter if h_freq is not None: if (l_freq is None or l_freq < h_freq) and \ (raw.info["lowpass"] is None or h_freq < raw.info['lowpass']): raw.info['lowpass'] = h_freq if l_freq is not None: if (h_freq is None or l_freq < h_freq) and \ (raw.info["highpass"] is None or l_freq > raw.info['highpass']): raw.info['highpass'] = l_freq if l_freq is None and h_freq is not None: low_pass_filter(raw._data, fs, h_freq, filter_length=filter_length, trans_bandwidth=h_trans_bandwidth, method=method, iir_params=iir_params, picks=picks, n_jobs=n_jobs, copy=False) if l_freq is not None and h_freq is None: high_pass_filter(raw._data, fs, l_freq, filter_length=filter_length, trans_bandwidth=l_trans_bandwidth, method=method, iir_params=iir_params, picks=picks, n_jobs=n_jobs, copy=False) if l_freq is not None and h_freq is not None: if l_freq < h_freq: raw._data = band_pass_filter( raw._data, fs, l_freq, h_freq, filter_length=filter_length, l_trans_bandwidth=l_trans_bandwidth, h_trans_bandwidth=h_trans_bandwidth, method=method, iir_params=iir_params, picks=picks, n_jobs=n_jobs, copy=False) else: raw._data = band_stop_filter( raw._data, fs, h_freq, l_freq, filter_length=filter_length, l_trans_bandwidth=h_trans_bandwidth, h_trans_bandwidth=l_trans_bandwidth, method=method, iir_params=iir_params, picks=picks, n_jobs=n_jobs, copy=False)
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling """ Fs = 500 sig_len_secs = 30 # Filtering of short signals (filter length = len(a)) a = np.random.randn(2, sig_len_secs * Fs) bp = band_pass_filter(a, Fs, 4, 8) bs = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5) lp = low_pass_filter(a, Fs, 8) hp = high_pass_filter(lp, Fs, 4) assert_array_almost_equal(hp, bp, 2) assert_array_almost_equal(bp + bs, a, 1) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, Fs, 4, 8, filter_length) bs_oa = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, filter_length) lp_oa = low_pass_filter(a, Fs, 8, filter_length) hp_oa = high_pass_filter(lp_oa, Fs, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) assert_array_almost_equal(bp_oa + bs_oa, a, 2) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 0 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp_oa, 2 * len(bp_oa), window='boxcar'), len(bp_oa), window='boxcar') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(range(Fs * sig_len_secs)) / float(Fs) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * Fs / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20) iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_true(iir_params['a'].size - 1 == 3) assert_true(iir_params['b'].size - 1 == 3) iir_params = dict(ftype='butter', order=4) iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_true(iir_params['a'].size - 1 == 4) assert_true(iir_params['b'].size - 1 == 4)
n_signals = 3 n_epochs = 10 n_times = 500 tmin = 0. tmax = (n_times - 1) / sfreq # Use a case known to have no spurious correlations (it would bad if nosetests # could randomly fail): np.random.seed(0) data = np.random.randn(n_epochs, n_signals, n_times) times_data = np.linspace(tmin, tmax, n_times) # simulate connectivity from 5Hz..15Hz fstart, fend = 5.0, 15.0 for i in xrange(n_epochs): data[i, 1, :] = band_pass_filter(data[i, 0, :], sfreq, fstart, fend) # add some noise, so the spectrum is not exactly zero data[i, 1, :] += 1e-2 * np.random.randn(n_times) def _stc_gen(data, sfreq, tmin, combo=False): """Simulate a SourceEstimate generator""" vertices = [np.arange(data.shape[1]), np.empty(0)] for d in data: if not combo: stc = SourceEstimate(data=d, vertices=vertices, tmin=tmin, tstep=1 / float(sfreq)) yield stc else:
startOSCServer() # get measurement info guessed by MNE-Python ch_names = ['EEG-%i' % i for i in np.arange(n_eeg)] raw_info = mne.create_info(ch_names, sfreq) with FieldTripClient(info=raw_info, host='localhost', port=1972, tmax=150, wait_max=10) as rt_client: tstart = time.time() told = tstart for ii in range(100): epoch = rt_client.get_data_as_epoch(n_samples=n_samples, picks=picks) filt = band_pass_filter(epoch.get_data(), sfreq, fmin, fmax) # compute band power features bp = np.log10(np.abs(cwt_morlet(filt[0], sfreq, [(fmin + fmax) / 2.]))) if told - tstart < baseline: bp_base.append(bp[:, 0, n_samples / 2].mean()) else: bp_sample = bp[:, 0, n_samples / 2].mean() bp_ratio = (np.mean(bp_base) - bp_sample) / np.mean(bp_base) sendOSCMsg("/nmx/bandpower/", [bp_ratio]) print "%i - sending /nmx/bandpower/%i" % (ii, bp_ratio) tcurrent = time.time() time.sleep(.5) told = tcurrent
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling""" sfreq = 100 sig_len_secs = 15 a = rng.randn(2, sig_len_secs * sfreq) # let's test our catchers for fl in ['blah', [0, 1], 1000.5, '10ss', '10']: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, fl, 1.0, 1.0, phase='zero') for nj in ['blah', 0.5]: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, 100, 1.0, 1.0, n_jobs=nj, phase='zero', fir_window='hann') assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, 100, 1.0, 1.0, phase='zero', fir_window='foo') # > Nyq/2 assert_raises(ValueError, band_pass_filter, a, sfreq, 4, sfreq / 2., 100, 1.0, 1.0, phase='zero', fir_window='hann') assert_raises(ValueError, low_pass_filter, a, sfreq, sfreq / 2., 100, 1.0, phase='zero', fir_window='hann') # check our short-filter warning: with warnings.catch_warnings(record=True) as w: # Warning for low attenuation band_pass_filter(a, sfreq, 1, 8, filter_length=256, phase='zero') assert_true(any('attenuation' in str(ww.message) for ww in w)) with warnings.catch_warnings(record=True) as w: # Warning for too short a filter band_pass_filter(a, sfreq, 1, 8, filter_length='0.5s', phase='zero') assert_true(any('Increase filter_length' in str(ww.message) for ww in w)) # try new default and old default for fl in ['auto', '10s', '5000ms', 1024]: bp = band_pass_filter(a, sfreq, 4, 8, fl, 1.0, 1.0, phase='zero', fir_window='hamming') bs = band_stop_filter(a, sfreq, 4 - 1.0, 8 + 1.0, fl, 1.0, 1.0, phase='zero', fir_window='hamming') lp = low_pass_filter(a, sfreq, 8, fl, 1.0, n_jobs=2, phase='zero', fir_window='hamming') hp = high_pass_filter(lp, sfreq, 4, fl, 1.0, phase='zero', fir_window='hamming') assert_array_almost_equal(hp, bp, 4) assert_array_almost_equal(bp + bs, a, 4) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp, 2 * bp.shape[-1], axis=-1, window='boxcar'), bp.shape[-1], window='boxcar', axis=-1) assert_array_almost_equal(bp[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(list(range(sfreq * sig_len_secs))) / float(sfreq) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * sfreq / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20, output='ba') iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_equal(iir_params['a'].size - 1, 3) assert_equal(iir_params['b'].size - 1, 3) iir_params = dict(ftype='butter', order=4, output='ba') iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_equal(iir_params['a'].size - 1, 4) assert_equal(iir_params['b'].size - 1, 4) iir_params = dict(ftype='cheby1', gpass=1, gstop=20, output='sos') iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter, which requires 2 SOS ((2, 6)) assert_equal(iir_params['sos'].shape, (2, 6)) iir_params = dict(ftype='butter', order=4, output='sos') iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_equal(iir_params['sos'].shape, (2, 6)) # check that picks work for 3d array with one channel and picks=[0] a = rng.randn(5 * sfreq, 5 * sfreq) b = a[:, None, :] a_filt = band_pass_filter(a, sfreq, 4, 8, 400, 2.0, 2.0, phase='zero', fir_window='hamming') b_filt = band_pass_filter(b, sfreq, 4, 8, 400, 2.0, 2.0, picks=[0], phase='zero', fir_window='hamming') assert_array_equal(a_filt[:, None, :], b_filt) # check for n-dimensional case a = rng.randn(2, 2, 2, 2) with warnings.catch_warnings(record=True): # filter too long assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, 100, 1.0, 1.0, picks=np.array([0, 1]), phase='zero')
# correlation function to sources and returns an array of r values # This is to illustrate the way ica.find_sources_raw works. Actually, this is # the default score_func. from scipy.stats import pearsonr corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0] # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. To improve detection, we filter the the channel and pass # it directly to find sources. The method then returns an array of correlation # scores for each ICA source. ecg_ch_name = 'MEG 1531' l_freq, h_freq = 8, 16 ecg = raw[[raw.ch_names.index(ecg_ch_name)], :][0] ecg = band_pass_filter(ecg, raw.info['sfreq'], l_freq, h_freq) ecg_scores = ica.find_sources_raw(raw, target=ecg, score_func=corr) # get maximum correlation index for ECG ecg_source_idx = np.abs(ecg_scores).argmax() title = 'ICA source matching ECG' ica.plot_sources_raw(raw, ecg_source_idx, title=title, stop=3.0) # let us have a look which other components resemble the ECG. # We can do this by reordering the plot by our scores using order # and generating sort indices for the sources: ecg_order = np.abs(ecg_scores).argsort()[::-1][:30] # ascending order ica.plot_sources_raw(raw, ecg_order, start=start_plot, stop=stop_plot)
def get_peak_ecg(ecg, sfreq=1017.25, flow=10, fhigh=20, pct_thresh=95.0, default_peak2peak_min=0.5, event_id=999): # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne.filter import band_pass_filter from jumeg.jumeg_math import calc_tkeo from scipy.signal import argrelextrema as extrema # ------------------------------------------- # filter ECG to get rid of noise and drifts # ------------------------------------------- fecg = band_pass_filter(ecg, sfreq, flow, fhigh, n_jobs=1, method='fft') ecg_abs = np.abs(fecg) # ------------------------------------------- # apply Teager Kaiser energie Operator (TKEO) # ------------------------------------------- tk_ecg = calc_tkeo(fecg) # ------------------------------------------- # find all peaks of abs(EOG) # since we don't know if the EOG lead has a # positive or negative R-peak # ------------------------------------------- ixpeak = extrema(tk_ecg, np.greater, axis=0) # ------------------------------------------- # threshold for |R-peak| # ------------------------------------------ peak_thresh_min = np.percentile(tk_ecg, pct_thresh, axis=0) ix = np.where(tk_ecg[ixpeak] > peak_thresh_min)[0] npeak = len(ix) if (npeak > 1): ixpeak = ixpeak[0][ix] else: return -1 # ------------------------------------------- # threshold for max Amplitude of R-peak # fixed to: median + 3*stddev # ------------------------------------------- mag = fecg[ixpeak] mag_mean = np.median(mag) if (mag_mean > 0): nstd = 3 else: nstd = -3 peak_thresh_max = mag_mean + nstd * np.std(mag) ix = np.where(ecg_abs[ixpeak] < np.abs(peak_thresh_max))[0] npeak = len(ix) if (npeak > 1): ixpeak = ixpeak[ix] else: return -1 # ------------------------------------------- # => test if the R-peak is positive or negative # => we assume the the R-peak is the largest peak !! # # ==> sometime we have outliers and we should check # the number of npos and nneg peaks -> which is larger? -> note done yet # -> we assume at least 2 peaks -> maybe we should check the ratio # ------------------------------------------- ixp = np.where(fecg[ixpeak] > 0)[0] npos = len(ixp) ixn = np.where(fecg[ixpeak] < 0)[0] nneg = len(ixp) if (npos == 0 and nneg == 0): import pdb pdb.set_trace() if (npos > 3): peakval_pos = np.abs(np.median(ecg[ixpeak[ixp]])) else: peakval_pos = 0 if (nneg > 3): peakval_neg = np.abs(np.median(ecg[ixpeak[ixn]])) else: peakval_neg = 0 if (peakval_pos > peakval_neg): ixpeak = ixpeak[ixp] ecg_pos = ecg else: ixpeak = ixpeak[ixn] ecg_pos = - ecg npeak = len(ixpeak) if (npeak < 1): return -1 # ------------------------------------------- # check if we have peaks too close together # ------------------------------------------- peak_ecg = ixpeak/sfreq dur = (np.roll(peak_ecg, -1)-peak_ecg) ix = np.where(dur > default_peak2peak_min)[0] npeak = len(ix) if (npeak < 1): return -1 ixpeak = np.append(ixpeak[0], ixpeak[ix]) peak_ecg = ixpeak/sfreq dur = (peak_ecg-np.roll(peak_ecg, 1)) ix = np.where(dur > default_peak2peak_min)[0] npeak = len(ix) if (npeak < 1): return -1 ixpeak = np.unique(np.append(ixpeak, ixpeak[ix[npeak-1]])) npeak = len(ixpeak) # ------------------------------------------- # search around each peak if we find # higher peaks in a range of 0.1 s # ------------------------------------------- seg_length = np.ceil(0.1 * sfreq) for ipeak in range(0, npeak-1): idx = [int(np.max([ixpeak[ipeak] - seg_length, 0])), int(np.min([ixpeak[ipeak]+seg_length, len(ecg)]))] idx_want = np.argmax(ecg_pos[idx[0]:idx[1]]) ixpeak[ipeak] = idx[0] + idx_want # ------------------------------------------- # to be confirm with mne implementation # ------------------------------------------- ecg_events = np.c_[ixpeak, np.zeros(npeak), np.zeros(npeak)+event_id] return ecg_events.astype(int)
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling.""" sfreq = 100 sig_len_secs = 15 a = rng.randn(2, sig_len_secs * sfreq) # let's test our catchers for fl in ['blah', [0, 1], 1000.5, '10ss', '10']: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, fl, 1., 1.) for nj in ['blah', 0.5]: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, 100, 1., 1., n_jobs=nj) assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, 100, 1., 1., fir_window='foo') # > Nyq/2 assert_raises(ValueError, band_pass_filter, a, sfreq, 4, sfreq / 2., 100, 1.0, 1.0) assert_raises(ValueError, low_pass_filter, a, sfreq, sfreq / 2., 100, 1.0) # check our short-filter warning: with warnings.catch_warnings(record=True) as w: # Warning for low attenuation band_pass_filter(a, sfreq, 1, 8, filter_length=256) assert_true(any('attenuation' in str(ww.message) for ww in w)) with warnings.catch_warnings(record=True) as w: # Warning for too short a filter band_pass_filter(a, sfreq, 1, 8, filter_length='0.5s') assert_true(any('Increase filter_length' in str(ww.message) for ww in w)) # try new default and old default for fl in ['auto', '10s', '5000ms', 1024]: bp = band_pass_filter(a, sfreq, 4, 8, fl, 1.0, 1.0) bs = band_stop_filter(a, sfreq, 4 - 1.0, 8 + 1.0, fl, 1.0, 1.0) lp = low_pass_filter(a, sfreq, 8, fl, 1.0, n_jobs=2) hp = high_pass_filter(lp, sfreq, 4, fl, 1.0) assert_array_almost_equal(hp, bp, 4) assert_array_almost_equal(bp + bs, a, 4) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp, 2 * bp.shape[-1], axis=-1, window='boxcar'), bp.shape[-1], window='boxcar', axis=-1) assert_array_almost_equal(bp[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(list(range(sfreq * sig_len_secs))) / float(sfreq) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * sfreq / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20, output='ba') iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_equal(iir_params['a'].size - 1, 3) assert_equal(iir_params['b'].size - 1, 3) iir_params = dict(ftype='butter', order=4, output='ba') iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_equal(iir_params['a'].size - 1, 4) assert_equal(iir_params['b'].size - 1, 4) iir_params = dict(ftype='cheby1', gpass=1, gstop=20, output='sos') iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter, which requires 2 SOS ((2, 6)) assert_equal(iir_params['sos'].shape, (2, 6)) iir_params = dict(ftype='butter', order=4, output='sos') iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_equal(iir_params['sos'].shape, (2, 6)) # check that picks work for 3d array with one channel and picks=[0] a = rng.randn(5 * sfreq, 5 * sfreq) b = a[:, None, :] a_filt = band_pass_filter(a, sfreq, 4, 8, 400, 2.0, 2.0) b_filt = band_pass_filter(b, sfreq, 4, 8, 400, 2.0, 2.0, picks=[0]) assert_array_equal(a_filt[:, None, :], b_filt) # check for n-dimensional case a = rng.randn(2, 2, 2, 2) with warnings.catch_warnings(record=True): # filter too long assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, 100, 1.0, 1.0, picks=np.array([0, 1]))
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling """ sfreq = 500 sig_len_secs = 30 a = rng.randn(2, sig_len_secs * sfreq) # let's test our catchers for fl in ['blah', [0, 1], 1000.5, '10ss', '10']: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, filter_length=fl) for nj in ['blah', 0.5]: assert_raises(ValueError, band_pass_filter, a, sfreq, 4, 8, n_jobs=nj) # > Nyq/2 assert_raises(ValueError, band_pass_filter, a, sfreq, 4, sfreq / 2.) assert_raises(ValueError, low_pass_filter, a, sfreq, sfreq / 2.) # check our short-filter warning: with warnings.catch_warnings(record=True) as w: # Warning for low attenuation band_pass_filter(a, sfreq, 1, 8, filter_length=1024) # Warning for too short a filter band_pass_filter(a, sfreq, 1, 8, filter_length='0.5s') assert_true(len(w) >= 2) # try new default and old default for fl in ['10s', '5000ms', None]: bp = band_pass_filter(a, sfreq, 4, 8, filter_length=fl) bs = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, filter_length=fl) lp = low_pass_filter(a, sfreq, 8, filter_length=fl, n_jobs=2) hp = high_pass_filter(lp, sfreq, 4, filter_length=fl) assert_array_almost_equal(hp, bp, 2) assert_array_almost_equal(bp + bs, a, 1) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, sfreq, 4, 8, filter_length) bs_oa = band_stop_filter(a, sfreq, 4 - 0.5, 8 + 0.5, filter_length) lp_oa = low_pass_filter(a, sfreq, 8, filter_length) hp_oa = high_pass_filter(lp_oa, sfreq, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) # Our filters are no longer quite complementary with linear rolloffs :( # this is the tradeoff for stability of the filtering # obtained by directly using the result of firwin2 instead of # modifying it... assert_array_almost_equal(bp_oa + bs_oa, a, 1) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 0 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp_oa, 2 * bp_oa.shape[-1], axis=-1, window='boxcar'), bp_oa.shape[-1], window='boxcar', axis=-1) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(list(range(sfreq * sig_len_secs))) / float(sfreq) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * sfreq / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20) iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_true(iir_params['a'].size - 1 == 3) assert_true(iir_params['b'].size - 1 == 3) iir_params = dict(ftype='butter', order=4) iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_true(iir_params['a'].size - 1 == 4) assert_true(iir_params['b'].size - 1 == 4) # check that picks work for 3d array with one channel and picks=[0] a = rng.randn(5 * sfreq, 5 * sfreq) b = a[:, None, :] with warnings.catch_warnings(record=True) as w: a_filt = band_pass_filter(a, sfreq, 4, 8) b_filt = band_pass_filter(b, sfreq, 4, 8, picks=[0]) assert_array_equal(a_filt[:, None, :], b_filt) # check for n-dimensional case a = rng.randn(2, 2, 2, 2) assert_raises(ValueError, band_pass_filter, a, sfreq, Fp1=4, Fp2=8, picks=np.array([0, 1])) # test that our overlap-add filtering doesn't introduce strange # artifacts (from mne_analyze mailing list 2015/06/25) N = 300 sfreq = 100. lp = 10. sine_freq = 1. x = np.ones(N) x += np.sin(2 * np.pi * sine_freq * np.arange(N) / sfreq) with warnings.catch_warnings(record=True): # filter attenuation x_filt = low_pass_filter(x, sfreq, lp, '1s') # the firwin2 function gets us this close assert_allclose(x, x_filt, rtol=1e-3, atol=1e-3)
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling """ Fs = 500 sig_len_secs = 30 a = np.random.randn(2, sig_len_secs * Fs) # let's test our catchers for fl in ['blah', [0, 1], 1000.5, '10ss', '10']: assert_raises(ValueError, band_pass_filter, a, Fs, 4, 8, filter_length=fl) for nj in ['blah', 0.5, 0]: assert_raises(ValueError, band_pass_filter, a, Fs, 4, 8, n_jobs=nj) assert_raises(ValueError, band_pass_filter, a, Fs, 4, Fs / 2.) # > Nyq/2 assert_raises(ValueError, low_pass_filter, a, Fs, Fs / 2.) # > Nyq/2 # check our short-filter warning: with warnings.catch_warnings(record=True) as w: # Warning for low attenuation band_pass_filter(a, Fs, 1, 8, filter_length=1024) # Warning for too short a filter band_pass_filter(a, Fs, 1, 8, filter_length='0.5s') assert_true(len(w) >= 2) # try new default and old default for fl in ['10s', '5000ms', None]: bp = band_pass_filter(a, Fs, 4, 8, filter_length=fl) bs = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, filter_length=fl) lp = low_pass_filter(a, Fs, 8, filter_length=fl, n_jobs=2) hp = high_pass_filter(lp, Fs, 4, filter_length=fl) assert_array_almost_equal(hp, bp, 2) assert_array_almost_equal(bp + bs, a, 1) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, Fs, 4, 8, filter_length) bs_oa = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, filter_length) lp_oa = low_pass_filter(a, Fs, 8, filter_length) hp_oa = high_pass_filter(lp_oa, Fs, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) assert_array_almost_equal(bp_oa + bs_oa, a, 2) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 0 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp_oa, 2 * bp_oa.shape[-1], axis=-1, window='boxcar'), bp_oa.shape[-1], window='boxcar', axis=-1) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(list(range(Fs * sig_len_secs))) / float(Fs) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * Fs / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20) iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_true(iir_params['a'].size - 1 == 3) assert_true(iir_params['b'].size - 1 == 3) iir_params = dict(ftype='butter', order=4) iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_true(iir_params['a'].size - 1 == 4) assert_true(iir_params['b'].size - 1 == 4)
def mne_bandpass_filter(self): filter.band_pass_filter(self.data, self.fs, self.lowcut, self.highcut) return mne_filter # test_data = np.genfromtxt('data/worldcup0.csv', delimiter=',') # for data[:] # filtered_stream = Streamfilter(test_data[:, i]) # y = filtered_stream.butter_bandpass_filter() # # if __name__ == "__main__": # import numpy as np # import matplotlib.pyplot as plt # from scipy.signal import freqz # # # Sample rate and desired cutoff frequencies (in Hz). # fs = 250.0 # lowcut = 1.5 # highcut = 60.0 # # # # grab data # data = np.genfromtxt('data/worldcup0.csv', delimiter=',') # # # T = time in seconds of sample # T = 40 # nsamples = T * fs # # #for graphing purposes, space lines evenly # t = np.linspace(0, T, nsamples, endpoint=False) # a = 0.02 # # # #grab channel data # channel_1 = data [:, 0] # # #figure 1 plots noisy signal and filtered signal # plt.figure(1) # plt.clf() # plt.plot(t, channel_1, label='Noisy signal') # # y = butter_bandpass_filter(channel_1, lowcut, highcut, fs, order=6) # plt.plot(t, y, label='Filtered signal (%g Hz - %g Hz)' % (lowcut, highcut)) # plt.xlabel('time (seconds)') # plt.hlines([-a, a], 0, T, linestyles='--') # plt.grid(True) # plt.axis('tight') # plt.legend(loc='upper left') # # #figure 2 plots just the filtered signal # plt.figure(2) # plt.plot(t, y) # plt.xlabel('time (seconds)') # plt.hlines([-a, a], 0, T, linestyles='--') # plt.grid(True) # plt.axis('tight') # plt.legend(loc='upper left') # # #figure 3 plots using the awesome MNE bandpass filter!!!! # plt.figure(3) # plt.plot(filter.band_pass_filter(channel_1, fs, lowcut, highcut)) # plt.xlabel('time (seconds)') # plt.hlines([-a, a], 0, T, linestyles='--') # plt.grid(True) # plt.axis('tight') # plt.legend(loc='upper left') # plt.show()
# def __init__(self, a, b, signal): # self.a = [] # self.b = [] # self.signal = signal # def apply(self, signal): # return lfilter(self.b, self.a, signal) # from scipy.io import loadmat # mat = loadmat('bp_filter_coeff.mat') # filters = [TimeDomainFilter(b=mat['bp_filter_coeff']['b'][0, 0].squeeze(), # a=mat['bp_filter_coeff']['a'][0, 0].squeeze()), # TimeDomainFilter(b=mat['bp_filter_coeff']['b_notch'][0, 0].squeeze(), # a=mat['bp_filter_coeff']['a_notch'][0, 0].squeeze()), ] # for filter in filters: # y = TimeDomainFilter.apply(y) if "__name__" == "__main__": hz_per_bin = float(250) / 256 data = np.genfromtxt('data/sample.csv', delimiter=',') filt_data = filter.band_pass_filter(data[:, 4], 250, 1, 60) psd, fft_data = _windowed_fft(filt_data, 250) psd_per_bin = psd / hz_per_bin plt.figure(1) plt.plot(fft_data, np.sqrt(psd_per_bin)) plt.figure(2) plt.plot(filt_data) plt.show()
#figure 1 plots noisy signal and filtered signal plt.figure(1) plt.clf() plt.plot(t, channel_1, label='Noisy signal') y = butter_bandpass_filter(channel_1, lowcut, highcut, fs, order=6) plt.plot(t, y, label='Filtered signal (%g Hz - %g Hz)' % (lowcut, highcut)) plt.xlabel('time (seconds)') plt.hlines([-a, a], 0, T, linestyles='--') plt.grid(True) plt.axis('tight') plt.legend(loc='upper left') #figure 2 plots just the filtered signal plt.figure(2) plt.plot(t, y) plt.xlabel('time (seconds)') plt.hlines([-a, a], 0, T, linestyles='--') plt.grid(True) plt.axis('tight') plt.legend(loc='upper left') #figure 3 plots using the awesome MNE bandpass filter!!!! plt.figure(3) plt.plot(filter.band_pass_filter(channel_1, fs, lowcut, highcut)) plt.xlabel('time (seconds)') plt.hlines([-a, a], 0, T, linestyles='--') plt.grid(True) plt.axis('tight') plt.legend(loc='upper left') plt.show()
n_signals = 3 n_epochs = 10 n_times = 500 tmin = 0. tmax = (n_times - 1) / sfreq # Use a case known to have no spurious correlations (it would bad if nosetests # could randomly fail): np.random.seed(0) data = np.random.randn(n_epochs, n_signals, n_times) times_data = np.linspace(tmin, tmax, n_times) # simulate connectivity from 5Hz..15Hz fstart, fend = 5.0, 15.0 for i in xrange(n_epochs): data[i, 1, :] = band_pass_filter(data[i, 0, :], sfreq, fstart, fend) # add some noise, so the spectrum is not exactly zero data[i, 1, :] += 1e-2 * np.random.randn(n_times) def _stc_gen(data, sfreq, tmin, combo=False): """Simulate a SourceEstimate generator""" vertices = [np.arange(data.shape[1]), np.empty(0)] for d in data: if not combo: stc = SourceEstimate(data=d, vertices=vertices, tmin=tmin, tstep=1 / float(sfreq)) yield stc else: # simulate a combination of array and source estimate arr = d[0]
def test_filters(): """Test low-, band-, high-pass, and band-stop filters plus resampling """ Fs = 500 sig_len_secs = 30 a = np.random.randn(2, sig_len_secs * Fs) # let's test our catchers for fl in ['blah', [0, 1], 1000.5, '10ss', '10']: assert_raises(ValueError, band_pass_filter, a, Fs, 4, 8, filter_length=fl) for nj in ['blah', 0.5, 0]: assert_raises(ValueError, band_pass_filter, a, Fs, 4, 8, n_jobs=nj) # check our short-filter warning: with warnings.catch_warnings(record=True) as w: # Warning for low attenuation band_pass_filter(a, Fs, 1, 8, filter_length=1024) # Warning for too short a filter band_pass_filter(a, Fs, 1, 8, filter_length='0.5s') assert_true(len(w) >= 2) # try new default and old default for fl in ['10s', '5000ms', None]: bp = band_pass_filter(a, Fs, 4, 8, filter_length=fl) bs = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, filter_length=fl) lp = low_pass_filter(a, Fs, 8, filter_length=fl, n_jobs=2) hp = high_pass_filter(lp, Fs, 4, filter_length=fl) assert_array_almost_equal(hp, bp, 2) assert_array_almost_equal(bp + bs, a, 1) # Overlap-add filtering with a fixed filter length filter_length = 8192 bp_oa = band_pass_filter(a, Fs, 4, 8, filter_length) bs_oa = band_stop_filter(a, Fs, 4 - 0.5, 8 + 0.5, filter_length) lp_oa = low_pass_filter(a, Fs, 8, filter_length) hp_oa = high_pass_filter(lp_oa, Fs, 4, filter_length) assert_array_almost_equal(hp_oa, bp_oa, 2) assert_array_almost_equal(bp_oa + bs_oa, a, 2) # The two methods should give the same result # As filtering for short signals uses a circular convolution (FFT) and # the overlap-add filter implements a linear convolution, the signal # boundary will be slightly different and we ignore it n_edge_ignore = 0 assert_array_almost_equal(hp[n_edge_ignore:-n_edge_ignore], hp_oa[n_edge_ignore:-n_edge_ignore], 2) # and since these are low-passed, downsampling/upsampling should be close n_resamp_ignore = 10 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs=2), 1, 2, n_jobs=2) assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # note that on systems without CUDA, this line serves as a test for a # graceful fallback to n_jobs=1 bp_up_dn = resample(resample(bp_oa, 2, 1, n_jobs='cuda'), 1, 2, n_jobs='cuda') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # test to make sure our resamling matches scipy's bp_up_dn = sp_resample(sp_resample(bp_oa, 2 * len(bp_oa), window='boxcar'), len(bp_oa), window='boxcar') assert_array_almost_equal(bp_oa[n_resamp_ignore:-n_resamp_ignore], bp_up_dn[n_resamp_ignore:-n_resamp_ignore], 2) # make sure we don't alias t = np.array(range(Fs * sig_len_secs)) / float(Fs) # make sinusoid close to the Nyquist frequency sig = np.sin(2 * np.pi * Fs / 2.2 * t) # signal should disappear with 2x downsampling sig_gone = resample(sig, 1, 2)[n_resamp_ignore:-n_resamp_ignore] assert_array_almost_equal(np.zeros_like(sig_gone), sig_gone, 2) # let's construct some filters iir_params = dict(ftype='cheby1', gpass=1, gstop=20) iir_params = construct_iir_filter(iir_params, 40, 80, 1000, 'low') # this should be a third order filter assert_true(iir_params['a'].size - 1 == 3) assert_true(iir_params['b'].size - 1 == 3) iir_params = dict(ftype='butter', order=4) iir_params = construct_iir_filter(iir_params, 40, None, 1000, 'low') assert_true(iir_params['a'].size - 1 == 4) assert_true(iir_params['b'].size - 1 == 4)
def find_blinks(raw, event_id=998, thresh=100e-6, l_freq=0.5, h_freq=10, filter_length='10s', ch_name=[ 'A1', ], tstart=0., l_trans_bandwidth=0.15): """Utility function to detect blink events from specified channel. Parameters ---------- raw : instance of Raw The raw data. event_id : int The index to assign to found events. low_pass : float Low pass frequency. high_pass : float High pass frequency. filter_length : str | int | None Number of taps to use for filtering. ch_name: list | None If not None, use specified channel(s) for EOG tstart : float Start detection after tstart seconds. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). Returns ------- eog_events : array Events in MNE format, i.e., N x 3 array """ sampling_rate = raw.info['sfreq'] first_samp = raw.first_samp ch_eog = pick_channels(raw.ch_names, include=ch_name) if len(ch_eog) == 0: raise ValueError('%s not in channel list' % ch_name) else: logger.info('Detecting blinks from channel %s' % ch_name) eog, _ = raw[ch_eog, :] filteog = band_pass_filter(eog, sampling_rate, l_freq, h_freq, filter_length=filter_length, l_trans_bandwidth=l_trans_bandwidth) eog_events, blinkvals = peak_finder(filteog.squeeze(), thresh=thresh) eog_events_neg, blinkvals_neg = peak_finder(filteog.squeeze(), thresh=thresh, extrema=-1) # Discarding blinks that don't look like other blinks, electing polarity nominal_blink = np.median(np.abs(blinkvals)) nominal_blink_neg = np.median(np.abs(blinkvals_neg)) if nominal_blink_neg > nominal_blink: blinkvals = blinkvals_neg nominal_blink = nominal_blink_neg eog_events = eog_events_neg eog_events = eog_events[np.logical_and( np.abs(blinkvals) < 2 * nominal_blink, np.abs(blinkvals) > 0.5 * nominal_blink)] # Discarding blinks detected before tstart seconds eog_events = eog_events[eog_events > raw.time_as_index(tstart)] eog_events += first_samp n_events = len(eog_events) logger.info("Number of EOG events detected : %d" % n_events) eog_events = np.c_[eog_events, np.zeros(n_events), event_id * np.ones(n_events)] return np.int64(eog_events)
"""Return pearson correlation between ICA source and target time series """ correlation, pval = np.array([pearsonr(a, y) for a in x]).T return correlation # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. To improve detection, we filter the the channel and pass # it directly to find sources. The method then returns an array of correlation # scores for each ICA source. ecg_ch_name = 'MEG 1531' l_freq, h_freq = 8, 16 ecg = raw[[raw.ch_names.index(ecg_ch_name)], :][0] ecg = band_pass_filter(ecg, raw.info['sfreq'], l_freq, h_freq) ecg_scores = ica.find_sources_raw(raw, target=ecg, score_func=score_func) # get maximum correlation index for ECG ecg_source_idx = np.abs(ecg_scores).argmax() title = 'ICA source matching ECG' ica.plot_sources_raw(raw, ecg_source_idx, title=title, stop=3.0) # let us have a look which other components resemble the ECG. # We can do this by reordering the plot by our scores using order # and generating sort indices for the sources: ecg_order = np.abs(ecg_scores).argsort()[::-1] # ascending order ica.plot_sources_raw(raw, ecg_order[:15], start=start_plot, stop=stop_plot)
def test_spectral_connectivity(): """Test frequency-domain connectivity methods""" # Use a case known to have no spurious correlations (it would bad if # nosetests could randomly fail): np.random.seed(0) sfreq = 50.0 n_signals = 3 n_epochs = 8 n_times = 256 tmin = 0.0 tmax = (n_times - 1) / sfreq data = np.random.randn(n_epochs, n_signals, n_times) times_data = np.linspace(tmin, tmax, n_times) # simulate connectivity from 5Hz..15Hz fstart, fend = 5.0, 15.0 for i in range(n_epochs): data[i, 1, :] = band_pass_filter(data[i, 0, :], sfreq, fstart, fend, **filt_kwargs) # add some noise, so the spectrum is not exactly zero data[i, 1, :] += 1e-2 * np.random.randn(n_times) # First we test some invalid parameters: assert_raises(ValueError, spectral_connectivity, data, method="notamethod") assert_raises(ValueError, spectral_connectivity, data, mode="notamode") # test invalid fmin fmax settings assert_raises(ValueError, spectral_connectivity, data, fmin=10, fmax=10 + 0.5 * (sfreq / float(n_times))) assert_raises(ValueError, spectral_connectivity, data, fmin=10, fmax=5) assert_raises(ValueError, spectral_connectivity, data, fmin=(0, 11), fmax=(5, 10)) assert_raises(ValueError, spectral_connectivity, data, fmin=(11,), fmax=(12, 15)) methods = ["coh", "cohy", "imcoh", ["plv", "ppc", "pli", "pli2_unbiased", "wpli", "wpli2_debiased", "coh"]] modes = ["multitaper", "fourier", "cwt_morlet"] # define some frequencies for cwt cwt_frequencies = np.arange(3, 24.5, 1) for mode in modes: for method in methods: if method == "coh" and mode == "multitaper": # only check adaptive estimation for coh to reduce test time check_adaptive = [False, True] else: check_adaptive = [False] if method == "coh" and mode == "cwt_morlet": # so we also test using an array for num cycles cwt_n_cycles = 7.0 * np.ones(len(cwt_frequencies)) else: cwt_n_cycles = 7.0 for adaptive in check_adaptive: if adaptive: mt_bandwidth = 1.0 else: mt_bandwidth = None con, freqs, times, n, _ = spectral_connectivity( data, method=method, mode=mode, indices=None, sfreq=sfreq, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles, ) assert_true(n == n_epochs) assert_array_almost_equal(times_data, times) if mode == "multitaper": upper_t = 0.95 lower_t = 0.5 elif mode == "fourier": # other estimates have higher variance upper_t = 0.8 lower_t = 0.75 else: # cwt_morlet upper_t = 0.64 lower_t = 0.63 # test the simulated signal if method == "coh": idx = np.searchsorted(freqs, (fstart + trans_bandwidth, fend - trans_bandwidth)) # we see something for zero-lag assert_true(np.all(con[1, 0, idx[0] : idx[1]] > upper_t), con[1, 0, idx[0] : idx[1]].min()) if mode != "cwt_morlet": idx = np.searchsorted(freqs, (fstart - trans_bandwidth * 2, fend + trans_bandwidth * 2)) assert_true(np.all(con[1, 0, : idx[0]] < lower_t)) assert_true(np.all(con[1, 0, idx[1] :] < lower_t), con[1, 0, idx[1:]].max()) elif method == "cohy": idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # imaginary coh will be zero check = np.imag(con[1, 0, idx[0] : idx[1]]) assert_true(np.all(check < lower_t), check.max()) # we see something for zero-lag assert_true(np.all(np.abs(con[1, 0, idx[0] : idx[1]]) > upper_t)) idx = np.searchsorted(freqs, (fstart - trans_bandwidth * 2, fend + trans_bandwidth * 2)) if mode != "cwt_morlet": assert_true(np.all(np.abs(con[1, 0, : idx[0]]) < lower_t)) assert_true(np.all(np.abs(con[1, 0, idx[1] :]) < lower_t)) elif method == "imcoh": idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # imaginary coh will be zero assert_true(np.all(con[1, 0, idx[0] : idx[1]] < lower_t)) idx = np.searchsorted(freqs, (fstart - 1, fend + 1)) assert_true(np.all(con[1, 0, : idx[0]] < lower_t)) assert_true(np.all(con[1, 0, idx[1] :] < lower_t), con[1, 0, idx[1] :].max()) # compute same connections using indices and 2 jobs indices = np.tril_indices(n_signals, -1) if not isinstance(method, list): test_methods = (method, _CohEst) else: test_methods = method stc_data = _stc_gen(data, sfreq, tmin) con2, freqs2, times2, n2, _ = spectral_connectivity( stc_data, method=test_methods, mode=mode, indices=indices, sfreq=sfreq, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, tmin=tmin, tmax=tmax, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles, n_jobs=2, ) assert_true(isinstance(con2, list)) assert_true(len(con2) == len(test_methods)) if method == "coh": assert_array_almost_equal(con2[0], con2[1]) if not isinstance(method, list): con2 = con2[0] # only keep the first method # we get the same result for the probed connections assert_array_almost_equal(freqs, freqs2) assert_array_almost_equal(con[indices], con2) assert_true(n == n2) assert_array_almost_equal(times_data, times2) else: # we get the same result for the probed connections assert_true(len(con) == len(con2)) for c, c2 in zip(con, con2): assert_array_almost_equal(freqs, freqs2) assert_array_almost_equal(c[indices], c2) assert_true(n == n2) assert_array_almost_equal(times_data, times2) # compute same connections for two bands, fskip=1, and f. avg. fmin = (5.0, 15.0) fmax = (15.0, 30.0) con3, freqs3, times3, n3, _ = spectral_connectivity( data, method=method, mode=mode, indices=indices, sfreq=sfreq, fmin=fmin, fmax=fmax, fskip=1, faverage=True, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles, ) assert_true(isinstance(freqs3, list)) assert_true(len(freqs3) == len(fmin)) for i in range(len(freqs3)): assert_true(np.all((freqs3[i] >= fmin[i]) & (freqs3[i] <= fmax[i]))) # average con2 "manually" and we get the same result if not isinstance(method, list): for i in range(len(freqs3)): freq_idx = np.searchsorted(freqs2, freqs3[i]) con2_avg = np.mean(con2[:, freq_idx], axis=1) assert_array_almost_equal(con2_avg, con3[:, i]) else: for j in range(len(con2)): for i in range(len(freqs3)): freq_idx = np.searchsorted(freqs2, freqs3[i]) con2_avg = np.mean(con2[j][:, freq_idx], axis=1) assert_array_almost_equal(con2_avg, con3[j][:, i])
def test_spectral_connectivity(): """Test frequency-domain connectivity methods""" # Use a case known to have no spurious correlations (it would bad if # nosetests could randomly fail): np.random.seed(0) sfreq = 50. n_signals = 3 n_epochs = 8 n_times = 256 tmin = 0. tmax = (n_times - 1) / sfreq data = np.random.randn(n_epochs, n_signals, n_times) times_data = np.linspace(tmin, tmax, n_times) # simulate connectivity from 5Hz..15Hz fstart, fend = 5.0, 15.0 for i in range(n_epochs): data[i, 1, :] = band_pass_filter(data[i, 0, :], sfreq, fstart, fend, **filt_kwargs) # add some noise, so the spectrum is not exactly zero data[i, 1, :] += 1e-2 * np.random.randn(n_times) # First we test some invalid parameters: assert_raises(ValueError, spectral_connectivity, data, method='notamethod') assert_raises(ValueError, spectral_connectivity, data, mode='notamode') # test invalid fmin fmax settings assert_raises(ValueError, spectral_connectivity, data, fmin=10, fmax=10 + 0.5 * (sfreq / float(n_times))) assert_raises(ValueError, spectral_connectivity, data, fmin=10, fmax=5) assert_raises(ValueError, spectral_connectivity, data, fmin=(0, 11), fmax=(5, 10)) assert_raises(ValueError, spectral_connectivity, data, fmin=(11, ), fmax=(12, 15)) methods = [ 'coh', 'cohy', 'imcoh', [ 'plv', 'ppc', 'pli', 'pli2_unbiased', 'wpli', 'wpli2_debiased', 'coh' ] ] modes = ['multitaper', 'fourier', 'cwt_morlet'] # define some frequencies for cwt cwt_frequencies = np.arange(3, 24.5, 1) for mode in modes: for method in methods: if method == 'coh' and mode == 'multitaper': # only check adaptive estimation for coh to reduce test time check_adaptive = [False, True] else: check_adaptive = [False] if method == 'coh' and mode == 'cwt_morlet': # so we also test using an array for num cycles cwt_n_cycles = 7. * np.ones(len(cwt_frequencies)) else: cwt_n_cycles = 7. for adaptive in check_adaptive: if adaptive: mt_bandwidth = 1. else: mt_bandwidth = None con, freqs, times, n, _ = spectral_connectivity( data, method=method, mode=mode, indices=None, sfreq=sfreq, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles) assert_true(n == n_epochs) assert_array_almost_equal(times_data, times) if mode == 'multitaper': upper_t = 0.95 lower_t = 0.5 elif mode == 'fourier': # other estimates have higher variance upper_t = 0.8 lower_t = 0.75 else: # cwt_morlet upper_t = 0.64 lower_t = 0.63 # test the simulated signal if method == 'coh': idx = np.searchsorted( freqs, (fstart + trans_bandwidth, fend - trans_bandwidth)) # we see something for zero-lag assert_true(np.all(con[1, 0, idx[0]:idx[1]] > upper_t), con[1, 0, idx[0]:idx[1]].min()) if mode != 'cwt_morlet': idx = np.searchsorted(freqs, (fstart - trans_bandwidth * 2, fend + trans_bandwidth * 2)) assert_true(np.all(con[1, 0, :idx[0]] < lower_t)) assert_true(np.all(con[1, 0, idx[1]:] < lower_t), con[1, 0, idx[1:]].max()) elif method == 'cohy': idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # imaginary coh will be zero check = np.imag(con[1, 0, idx[0]:idx[1]]) assert_true(np.all(check < lower_t), check.max()) # we see something for zero-lag assert_true( np.all(np.abs(con[1, 0, idx[0]:idx[1]]) > upper_t)) idx = np.searchsorted(freqs, (fstart - trans_bandwidth * 2, fend + trans_bandwidth * 2)) if mode != 'cwt_morlet': assert_true( np.all(np.abs(con[1, 0, :idx[0]]) < lower_t)) assert_true( np.all(np.abs(con[1, 0, idx[1]:]) < lower_t)) elif method == 'imcoh': idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # imaginary coh will be zero assert_true(np.all(con[1, 0, idx[0]:idx[1]] < lower_t)) idx = np.searchsorted(freqs, (fstart - 1, fend + 1)) assert_true(np.all(con[1, 0, :idx[0]] < lower_t)) assert_true(np.all(con[1, 0, idx[1]:] < lower_t), con[1, 0, idx[1]:].max()) # compute same connections using indices and 2 jobs indices = np.tril_indices(n_signals, -1) if not isinstance(method, list): test_methods = (method, _CohEst) else: test_methods = method stc_data = _stc_gen(data, sfreq, tmin) con2, freqs2, times2, n2, _ = spectral_connectivity( stc_data, method=test_methods, mode=mode, indices=indices, sfreq=sfreq, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, tmin=tmin, tmax=tmax, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles, n_jobs=2) assert_true(isinstance(con2, list)) assert_true(len(con2) == len(test_methods)) if method == 'coh': assert_array_almost_equal(con2[0], con2[1]) if not isinstance(method, list): con2 = con2[0] # only keep the first method # we get the same result for the probed connections assert_array_almost_equal(freqs, freqs2) assert_array_almost_equal(con[indices], con2) assert_true(n == n2) assert_array_almost_equal(times_data, times2) else: # we get the same result for the probed connections assert_true(len(con) == len(con2)) for c, c2 in zip(con, con2): assert_array_almost_equal(freqs, freqs2) assert_array_almost_equal(c[indices], c2) assert_true(n == n2) assert_array_almost_equal(times_data, times2) # compute same connections for two bands, fskip=1, and f. avg. fmin = (5., 15.) fmax = (15., 30.) con3, freqs3, times3, n3, _ = spectral_connectivity( data, method=method, mode=mode, indices=indices, sfreq=sfreq, fmin=fmin, fmax=fmax, fskip=1, faverage=True, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles) assert_true(isinstance(freqs3, list)) assert_true(len(freqs3) == len(fmin)) for i in range(len(freqs3)): assert_true( np.all((freqs3[i] >= fmin[i]) & (freqs3[i] <= fmax[i]))) # average con2 "manually" and we get the same result if not isinstance(method, list): for i in range(len(freqs3)): freq_idx = np.searchsorted(freqs2, freqs3[i]) con2_avg = np.mean(con2[:, freq_idx], axis=1) assert_array_almost_equal(con2_avg, con3[:, i]) else: for j in range(len(con2)): for i in range(len(freqs3)): freq_idx = np.searchsorted(freqs2, freqs3[i]) con2_avg = np.mean(con2[j][:, freq_idx], axis=1) assert_array_almost_equal(con2_avg, con3[j][:, i])
# def __init__(self, a, b, signal): # self.a = [] # self.b = [] # self.signal = signal # def apply(self, signal): # return lfilter(self.b, self.a, signal) # from scipy.io import loadmat # mat = loadmat('bp_filter_coeff.mat') # filters = [TimeDomainFilter(b=mat['bp_filter_coeff']['b'][0, 0].squeeze(), # a=mat['bp_filter_coeff']['a'][0, 0].squeeze()), # TimeDomainFilter(b=mat['bp_filter_coeff']['b_notch'][0, 0].squeeze(), # a=mat['bp_filter_coeff']['a_notch'][0, 0].squeeze()), ] # for filter in filters: # y = TimeDomainFilter.apply(y) hz_per_bin = float(250) / 256 data = np.genfromtxt('dan_pythondata.csv', delimiter=' ') filt_data = filter.band_pass_filter(data[:, 1], 250, 1, 60) psd, fft_data = windowed_fft(filt_data, 250) psd_per_bin = psd / hz_per_bin plt.figure(1) plt.plot(fft_data, np.sqrt(psd_per_bin)) plt.figure(2) plt.plot(filt_data) plt.show()
def test_spectral_connectivity(): """Test frequency-domain connectivity methods""" # Use a case known to have no spurious correlations (it would bad if # nosetests could randomly fail): np.random.seed(0) sfreq = 50. n_signals = 3 n_epochs = 10 n_times = 500 tmin = 0. tmax = (n_times - 1) / sfreq data = np.random.randn(n_epochs, n_signals, n_times) times_data = np.linspace(tmin, tmax, n_times) # simulate connectivity from 5Hz..15Hz fstart, fend = 5.0, 15.0 for i in range(n_epochs): data[i, 1, :] = band_pass_filter(data[i, 0, :], sfreq, fstart, fend) # add some noise, so the spectrum is not exactly zero data[i, 1, :] += 1e-2 * np.random.randn(n_times) # First we test some invalid parameters: assert_raises(ValueError, spectral_connectivity, data, method='notamethod') assert_raises(ValueError, spectral_connectivity, data, mode='notamode') # test invalid fmin fmax settings assert_raises(ValueError, spectral_connectivity, data, fmin=10, fmax=10 + 0.5 * (sfreq / float(n_times))) assert_raises(ValueError, spectral_connectivity, data, fmin=10, fmax=5) assert_raises(ValueError, spectral_connectivity, data, fmin=(0, 11), fmax=(5, 10)) assert_raises(ValueError, spectral_connectivity, data, fmin=(11,), fmax=(12, 15)) methods = ['coh', 'imcoh', 'cohy', 'plv', 'ppc', 'pli', 'pli2_unbiased', 'wpli', 'wpli2_debiased', 'coh'] modes = ['multitaper', 'fourier', 'cwt_morlet'] # define some frequencies for cwt cwt_frequencies = np.arange(3, 24.5, 1) for mode in modes: for method in methods: if method == 'coh' and mode == 'multitaper': # only check adaptive estimation for coh to reduce test time check_adaptive = [False, True] else: check_adaptive = [False] if method == 'coh' and mode == 'cwt_morlet': # so we also test using an array for num cycles cwt_n_cycles = 7. * np.ones(len(cwt_frequencies)) else: cwt_n_cycles = 7. for adaptive in check_adaptive: if adaptive: mt_bandwidth = 1. else: mt_bandwidth = None con, freqs, times, n, _ = spectral_connectivity(data, method=method, mode=mode, indices=None, sfreq=sfreq, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles) assert_true(n == n_epochs) assert_array_almost_equal(times_data, times) if mode == 'multitaper': upper_t = 0.95 lower_t = 0.5 else: # other estimates have higher variance upper_t = 0.8 lower_t = 0.75 # test the simulated signal if method == 'coh': idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # we see something for zero-lag assert_true(np.all(con[1, 0, idx[0]:idx[1]] > upper_t)) if mode != 'cwt_morlet': idx = np.searchsorted(freqs, (fstart - 1, fend + 1)) assert_true(np.all(con[1, 0, :idx[0]] < lower_t)) assert_true(np.all(con[1, 0, idx[1]:] < lower_t)) elif method == 'cohy': idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # imaginary coh will be zero assert_true(np.all(np.imag(con[1, 0, idx[0]:idx[1]]) < lower_t)) # we see something for zero-lag assert_true(np.all(np.abs(con[1, 0, idx[0]:idx[1]]) > upper_t)) idx = np.searchsorted(freqs, (fstart - 1, fend + 1)) if mode != 'cwt_morlet': assert_true(np.all(np.abs(con[1, 0, :idx[0]]) < lower_t)) assert_true(np.all(np.abs(con[1, 0, idx[1]:]) < lower_t)) elif method == 'imcoh': idx = np.searchsorted(freqs, (fstart + 1, fend - 1)) # imaginary coh will be zero assert_true(np.all(con[1, 0, idx[0]:idx[1]] < lower_t)) idx = np.searchsorted(freqs, (fstart - 1, fend + 1)) assert_true(np.all(con[1, 0, :idx[0]] < lower_t)) assert_true(np.all(con[1, 0, idx[1]:] < lower_t)) # compute same connections using indices and 2 jobs, # also add a second method indices = tril_indices(n_signals, -1) test_methods = (method, _CohEst) combo = True if method == 'coh' else False stc_data = _stc_gen(data, sfreq, tmin) con2, freqs2, times2, n2, _ = spectral_connectivity(stc_data, method=test_methods, mode=mode, indices=indices, sfreq=sfreq, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, tmin=tmin, tmax=tmax, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles, n_jobs=2) assert_true(isinstance(con2, list)) assert_true(len(con2) == 2) if method == 'coh': assert_array_almost_equal(con2[0], con2[1]) con2 = con2[0] # only keep the first method # we get the same result for the probed connections assert_array_almost_equal(freqs, freqs2) assert_array_almost_equal(con[indices], con2) assert_true(n == n2) assert_array_almost_equal(times_data, times2) # compute same connections for two bands, fskip=1, and f. avg. fmin = (5., 15.) fmax = (15., 30.) con3, freqs3, times3, n3, _ = spectral_connectivity(data, method=method, mode=mode, indices=indices, sfreq=sfreq, fmin=fmin, fmax=fmax, fskip=1, faverage=True, mt_adaptive=adaptive, mt_low_bias=True, mt_bandwidth=mt_bandwidth, cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles) assert_true(isinstance(freqs3, list)) assert_true(len(freqs3) == len(fmin)) for i in range(len(freqs3)): assert_true(np.all((freqs3[i] >= fmin[i]) & (freqs3[i] <= fmax[i]))) # average con2 "manually" and we get the same result for i in range(len(freqs3)): freq_idx = np.searchsorted(freqs2, freqs3[i]) con2_avg = np.mean(con2[:, freq_idx], axis=1) assert_array_almost_equal(con2_avg, con3[:, i])
def filterFunc(arr,samplingRate,frFreq,toFreq): return band_pass_filter(arr,samplingRate,frFreq,toFreq)