def test_cached_coherence(): """Testing the cached coherence functions """ NFFT = 64 #This is the default behavior n_freqs = NFFT//2 + 1 ij = [(0,1),(1,0)] ts = np.loadtxt(os.path.join(test_dir_path,'tseries12.txt')) freqs,cache = tsa.cache_fft(ts,ij) #Are the frequencies the right ones? yield npt.assert_equal,freqs,ut.get_freqs(2*np.pi,NFFT) #Check that the fft of the first window is what we expect: hann = mlab.window_hanning(np.ones(NFFT)) w_ts = ts[0][:NFFT]*hann w_ft = np.fft.fft(w_ts)[0:n_freqs] #This is the result of the function: first_window_fft = cache['FFT_slices'][0][0] yield npt.assert_equal,w_ft,first_window_fft coh_cached = tsa.cache_to_coherency(cache,ij)[0,1] f,c = tsa.coherency(ts) coh_direct = c[0,1] yield npt.assert_almost_equal,coh_direct,coh_cached
def test_cached_coherence(): """Testing the cached coherence functions """ NFFT = 64 # This is the default behavior n_freqs = NFFT // 2 + 1 ij = [(0, 1), (1, 0)] ts = np.loadtxt(os.path.join(test_dir_path, 'tseries12.txt')) freqs, cache = tsa.cache_fft(ts, ij) # Are the frequencies the right ones? npt.assert_equal(freqs, utils.get_freqs(2 * np.pi, NFFT)) # Check that the fft of the first window is what we expect: hann = mlab.window_hanning(np.ones(NFFT)) w_ts = ts[0][:NFFT] * hann w_ft = fftpack.fft(w_ts)[0:n_freqs] # This is the result of the function: first_window_fft = cache['FFT_slices'][0][0] npt.assert_equal(w_ft, first_window_fft) coh_cached = tsa.cache_to_coherency(cache, ij)[0, 1] f, c = tsa.coherency(ts) coh_direct = c[0, 1] npt.assert_almost_equal(coh_direct, coh_cached) # Only welch PSD works and an error is thrown otherwise. This tests that # the error is thrown: with pytest.raises(ValueError) as e_info: tsa.cache_fft(ts, ij, method=methods[2]) # Take the method in which the window is defined on input: freqs, cache1 = tsa.cache_fft(ts, ij, method=methods[3]) # And compare it to the method in which it isn't: freqs, cache2 = tsa.cache_fft(ts, ij, method=methods[4]) npt.assert_equal(cache1, cache2) # Do the same, while setting scale_by_freq to False: freqs, cache1 = tsa.cache_fft(ts, ij, method=methods[3], scale_by_freq=False) freqs, cache2 = tsa.cache_fft(ts, ij, method=methods[4], scale_by_freq=False) npt.assert_equal(cache1, cache2) # Test cache_to_psd: psd1 = tsa.cache_to_psd(cache, ij)[0] # Against the standard get_spectra: f, c = tsa.get_spectra(ts) psd2 = c[0][0] npt.assert_almost_equal(psd1, psd2) # Test that prefer_speed_over_memory doesn't change anything: freqs, cache1 = tsa.cache_fft(ts, ij) freqs, cache2 = tsa.cache_fft(ts, ij, prefer_speed_over_memory=True) psd1 = tsa.cache_to_psd(cache1, ij)[0] psd2 = tsa.cache_to_psd(cache2, ij)[0] npt.assert_almost_equal(psd1, psd2)
def test_coherency_cached(): """Tests that the cached coherency gives the same result as the standard coherency""" f1, c1 = tsa.coherency(tseries) ij = [(0, 1), (1, 0)] f2, cache = tsa.cache_fft(tseries, ij) c2 = tsa.cache_to_coherency(cache, ij) npt.assert_array_almost_equal(c1[1, 0], c2[1, 0]) npt.assert_array_almost_equal(c1[0, 1], c2[0, 1])
def test_cache_to_coherency(): """ Test cache_to_coherency against the standard coherency calculation """ ij = [(0, 1), (1, 0)] ts = np.loadtxt(os.path.join(test_dir_path, 'tseries12.txt')) freqs, cache = tsa.cache_fft(ts, ij) Cxy = tsa.cache_to_coherency(cache, ij) f, c = tsa.coherency(ts) npt.assert_almost_equal(Cxy[0][1], c[0, 1]) # Check that it doesn't matter if you prefer_speed_over_memory: freqs, cache2 = tsa.cache_fft(ts, ij, prefer_speed_over_memory=True) Cxy2 = tsa.cache_to_coherency(cache2, ij) npt.assert_equal(Cxy2, Cxy) # XXX Calculating the angle of the averaged psd and calculating the average # of the angles calculated over different windows does not yield exactly # the same number, because the angle is not a linear functions (arctan), # so it is unclear how to test this, but we make sure that it runs, # whether or not you prefer_speed_over_memory: freqs, cache = tsa.cache_fft(ts, ij) tsa.cache_to_relative_phase(cache, ij) freqs, cache = tsa.cache_fft(ts, ij, prefer_speed_over_memory=True) tsa.cache_to_relative_phase(cache, ij) # Check that things run alright, even if there is just one window for the # entire ts: freqs, cache = tsa.cache_fft(ts, ij, method=dict(this_method='welch', NFFT=ts.shape[-1], n_overlap=0)) cxy_one_window = tsa.cache_to_coherency(cache, ij) ph_one_window = tsa.cache_to_relative_phase(cache, ij) # And whether or not you prefer_speed_over_memory freqs, cache = tsa.cache_fft(ts, ij, method=dict(this_method='welch', NFFT=ts.shape[-1], n_overlap=0), prefer_speed_over_memory=True) cxy_one_window = tsa.cache_to_coherency(cache, ij) ph_one_window = tsa.cache_to_relative_phase(cache, ij)
def test_coherency_multi_taper(): """Tests that the coherency algorithm runs smoothly, using the multi_taper csd routine and that the resulting matrix is symmetric""" t = np.linspace(0,16*np.pi,1024) x = np.sin(t) + np.sin(2*t) + np.sin(3*t) + np.random.rand(t.shape[-1]) y = x + np.random.rand(t.shape[-1]) method = {"this_method":'multi_taper_csd', "Fs":2*np.pi} f,c = tsa.coherency(np.vstack([x,y]),csd_method=method) npt.assert_array_almost_equal(c[0,1],c[1,0].conjugate()) npt.assert_array_almost_equal(c[0,0],np.ones(f.shape))
def test_cache_to_coherency(): """ Test cache_to_coherency against the standard coherency calculation """ ij = [(0,1),(1,0)] ts = np.loadtxt(os.path.join(test_dir_path,'tseries12.txt')) freqs,cache = tsa.cache_fft(ts,ij) Cxy = tsa.cache_to_coherency(cache,ij) f,c = tsa.coherency(ts) npt.assert_almost_equal(Cxy[0][1],c[0,1]) # Check that it doesn't matter if you prefer_speed_over_memory: freqs,cache2 = tsa.cache_fft(ts,ij,prefer_speed_over_memory=True) Cxy2 = tsa.cache_to_coherency(cache2,ij) npt.assert_equal(Cxy2,Cxy) # XXX Calculating the angle of the averaged psd and calculating the average of the # angles calculated over different windows does not yield exactly the same # number, because the angle is not a linear functions (arctan), so it is # unclear how to test this, but we make sure that it runs, whether or not # you prefer_speed_over_memory: freqs,cache = tsa.cache_fft(ts,ij) tsa.cache_to_relative_phase(cache,ij) freqs,cache = tsa.cache_fft(ts,ij,prefer_speed_over_memory=True) tsa.cache_to_relative_phase(cache,ij) # Check that things run alright, even if there is just one window for the # entire ts: freqs,cache = tsa.cache_fft(ts,ij,method=dict(this_method='welch', NFFT=ts.shape[-1], n_overlap=0)) cxy_one_window = tsa.cache_to_coherency(cache,ij) ph_one_window = tsa.cache_to_relative_phase(cache,ij) # And whether or not you prefer_speed_over_memory freqs,cache = tsa.cache_fft(ts,ij,method=dict(this_method='welch', NFFT=ts.shape[-1], n_overlap=0), prefer_speed_over_memory=True) cxy_one_window = tsa.cache_to_coherency(cache,ij) ph_one_window = tsa.cache_to_relative_phase(cache,ij)
def test_coherency(): """ Tests that the coherency algorithm runs smoothly, using the different csd routines, that the resulting matrix is symmetric and for the welch method, that the frequency bands in the output make sense """ for method in methods: f, c = tsa.coherency(tseries, csd_method=method) npt.assert_array_almost_equal(c[0, 1], c[1, 0].conjugate()) npt.assert_array_almost_equal(c[0, 0], np.ones(f.shape)) if method is not None and method['this_method'] != "multi_taper_csd": f_theoretical = utils.get_freqs(method['Fs'], method['NFFT']) npt.assert_array_almost_equal(f, f_theoretical)
def test_coherency_cached(): """Tests that the cached coherency gives the same result as the standard coherency""" t = np.linspace(0,16*np.pi,1024) x = np.sin(t) + np.sin(2*t) + np.sin(3*t) + np.random.rand(t.shape[-1]) y = x + np.random.rand(t.shape[-1]) f1,c1 = tsa.coherency(np.vstack([x,y])) ij = [(0,1),(1,0)] f2,cache = tsa.cache_fft(np.vstack([x,y]),ij) c2 = tsa.cache_to_coherency(cache,ij) npt.assert_array_almost_equal(c1[1,0],c2[1,0]) npt.assert_array_almost_equal(c1[0,1],c2[0,1])
def test_coherency_welch(): """Tests that the coherency algorithm runs smoothly, using the welch csd routine, that the resulting matrix is symmetric and that the frequency bands in the output make sense""" t = np.linspace(0,16*np.pi,1024) x = np.sin(t) + np.sin(2*t) + np.sin(3*t) + np.random.rand(t.shape[-1]) y = x + np.random.rand(t.shape[-1]) method = {"this_method":'welch', "NFFT":256, "Fs":2*np.pi} f,c = tsa.coherency(np.vstack([x,y]),csd_method=method) npt.assert_array_almost_equal(c[0,1],c[1,0].conjugate()) npt.assert_array_almost_equal(c[0,0],np.ones(f.shape)) f_theoretical = ut.get_freqs(method['Fs'],method['NFFT']) npt.assert_array_almost_equal(f,f_theoretical)