<s> """<STR_LIT>""" <EOL> import nose <EOL> import os <EOL> import warnings <EOL> import numpy as np <EOL> import numpy . testing as npt <EOL> from scipy . signal import signaltools <EOL> try : <EOL> import matplotlib <EOL> import matplotlib . mlab as mlab <EOL> has_mpl = True <EOL> if float ( matplotlib . __version__ [ : <NUM_LIT:3> ] ) < <NUM_LIT> : <EOL> w_s = "<STR_LIT>" <EOL> w_s += "<STR_LIT>" <EOL> w_s += "<STR_LIT>" <EOL> warnings . warn ( w_s , Warning ) <EOL> old_mpl = True <EOL> else : <EOL> old_mpl = False <EOL> except ImportError : <EOL> raise nose . SkipTest ( ) <EOL> from scipy import fftpack <EOL> import nitime <EOL> import nitime . algorithms as tsa <EOL> import nitime . utils as utils <EOL> test_dir_path = os . path . join ( nitime . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> t = np . linspace ( <NUM_LIT:0> , <NUM_LIT:16> * np . pi , <NUM_LIT> ) <EOL> x = np . sin ( t ) + np . sin ( <NUM_LIT:2> * t ) + np . sin ( <NUM_LIT:3> * t ) + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> y = x + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> tseries = np . vstack ( [ x , y ] ) <EOL> methods = [ None , <EOL> { "<STR_LIT>" : '<STR_LIT>' , "<STR_LIT>" : <NUM_LIT:2> * np . pi } , <EOL> { "<STR_LIT>" : '<STR_LIT>' , "<STR_LIT>" : <NUM_LIT:2> * np . pi , "<STR_LIT>" : <NUM_LIT> } ] <EOL> if has_mpl : <EOL> methods . append ( { "<STR_LIT>" : '<STR_LIT>' , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT:2> * np . pi , <EOL> "<STR_LIT>" : mlab . window_hanning ( np . ones ( <NUM_LIT> ) ) } ) <EOL> methods . append ( { "<STR_LIT>" : '<STR_LIT>' , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT:2> * np . pi } ) <EOL> def test_coherency ( ) : <EOL> """<STR_LIT>""" <EOL> for method in methods : <EOL> f , c = tsa . coherency ( tseries , csd_method = method ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] . conjugate ( ) ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:0> ] , np . ones ( f . shape ) ) <EOL> if method is not None and method [ '<STR_LIT>' ] != "<STR_LIT>" : <EOL> f_theoretical = utils . get_freqs ( method [ '<STR_LIT>' ] , method [ '<STR_LIT>' ] ) <EOL> npt . assert_array_almost_equal ( f , f_theoretical ) <EOL> def test_coherence ( ) : <EOL> """<STR_LIT>""" <EOL> for method in methods : <EOL> f , c = tsa . coherence ( tseries , csd_method = method ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:0> ] , np . ones ( f . shape ) ) <EOL> def test_coherency_regularized ( ) : <EOL> """<STR_LIT>""" <EOL> for method in methods : <EOL> f , c = tsa . coherency_regularized ( tseries , <NUM_LIT> , <NUM_LIT:1000> , <EOL> csd_method = method ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] . conjugate ( ) ) <EOL> def test_coherence_regularized ( ) : <EOL> """<STR_LIT>""" <EOL> for method in methods : <EOL> f , c = tsa . coherence_regularized ( tseries , <NUM_LIT> , <NUM_LIT:1000> , <EOL> csd_method = method ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> def test_coherency_bavg ( ) : <EOL> ub = [ np . pi / <NUM_LIT:2> , None ] <EOL> lb = [ <NUM_LIT:0> , <NUM_LIT> ] <EOL> for method in methods : <EOL> for this_lb in lb : <EOL> for this_ub in ub : <EOL> c = tsa . coherency_bavg ( tseries , lb = this_lb , ub = this_ub , <EOL> csd_method = method ) <EOL> npt . assert_equal ( len ( c . shape ) , <NUM_LIT:2> ) <EOL> npt . assert_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] . conjugate ( ) ) <EOL> def test_coherence_bavg ( ) : <EOL> ub = [ np . pi / <NUM_LIT:2> , None ] <EOL> lb = [ <NUM_LIT:0> , <NUM_LIT> ] <EOL> for method in methods : <EOL> for this_lb in lb : <EOL> for this_ub in ub : <EOL> c = tsa . coherence_bavg ( tseries , lb = this_lb , ub = this_ub , <EOL> csd_method = method ) <EOL> npt . assert_equal ( len ( c . shape ) , <NUM_LIT:2> ) <EOL> npt . assert_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] . conjugate ( ) ) <EOL> def test_coherence_partial ( ) : <EOL> """<STR_LIT>""" <EOL> x = np . sin ( t ) + np . sin ( <NUM_LIT:2> * t ) + np . sin ( <NUM_LIT:3> * t ) + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> y = x + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> z = y + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> for method in methods : <EOL> if ( method is None ) or method [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> f , c = tsa . coherence_partial ( np . vstack ( [ x , y ] ) , z , <EOL> csd_method = method ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c [ <NUM_LIT:1> , <NUM_LIT:0> ] . conjugate ( ) ) <EOL> def test_coherence_phase_delay ( ) : <EOL> """<STR_LIT>""" <EOL> nz = np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> x = np . sin ( t ) + nz <EOL> y = np . sin ( t + np . pi ) + nz <EOL> tseries = np . vstack ( [ x , y ] ) <EOL> for method in methods : <EOL> f1 , pdelay = tsa . coherency_phase_spectrum ( tseries , csd_method = method ) <EOL> f2 , tdelay = tsa . coherency_phase_delay ( tseries , csd_method = method ) <EOL> npt . assert_almost_equal ( pdelay [ <NUM_LIT:0> , <NUM_LIT:1> ] , - pdelay [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> npt . assert_almost_equal ( tdelay [ <NUM_LIT:0> , <NUM_LIT:1> ] , - tdelay [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> npt . assert_almost_equal ( tdelay [ <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> pdelay [ <NUM_LIT:0> , <NUM_LIT:1> ] [ <NUM_LIT:1> : ] / ( <NUM_LIT:2> * np . pi * f2 ) ) <EOL> def test_coherency_cached ( ) : <EOL> """<STR_LIT>""" <EOL> f1 , c1 = tsa . coherency ( tseries ) <EOL> ij = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ] <EOL> f2 , cache = tsa . cache_fft ( tseries , ij ) <EOL> c2 = tsa . cache_to_coherency ( cache , ij ) <EOL> npt . assert_array_almost_equal ( c1 [ <NUM_LIT:1> , <NUM_LIT:0> ] , c2 [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> npt . assert_array_almost_equal ( c1 [ <NUM_LIT:0> , <NUM_LIT:1> ] , c2 [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> def test_correlation_spectrum ( ) : <EOL> """<STR_LIT>""" <EOL> f , c = tsa . correlation_spectrum ( x , y , norm = True ) <EOL> @ npt . dec . skipif ( True ) <EOL> def test_coherence_linear_dependence ( ) : <EOL> """<STR_LIT>""" <EOL> t = np . linspace ( <NUM_LIT:0> , <NUM_LIT:16> * np . pi , <NUM_LIT:2> ** <NUM_LIT> ) <EOL> x = np . sin ( t ) + np . sin ( <NUM_LIT:2> * t ) + np . sin ( <NUM_LIT:3> * t ) + <NUM_LIT:0.1> * np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> N = x . shape [ - <NUM_LIT:1> ] <EOL> alpha = <NUM_LIT:10> <EOL> m = <NUM_LIT:3> <EOL> noise = <NUM_LIT:0.1> * np . random . randn ( t . shape [ - <NUM_LIT:1> ] ) <EOL> y = alpha * np . roll ( x , m ) + noise <EOL> f_noise = fftpack . fft ( noise ) [ <NUM_LIT:0> : N / <NUM_LIT:2> ] <EOL> f_x = fftpack . fft ( x ) [ <NUM_LIT:0> : N / <NUM_LIT:2> ] <EOL> c_t = ( <NUM_LIT:1> / ( <NUM_LIT:1> + ( f_noise / ( f_x * ( alpha ** <NUM_LIT:2> ) ) ) ) ) <EOL> method = { "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:2> * np . pi } <EOL> f , c = tsa . coherence ( np . vstack ( [ x , y ] ) , csd_method = method ) <EOL> c_t = np . abs ( signaltools . resample ( c_t , c . shape [ - <NUM_LIT:1> ] ) ) <EOL> npt . assert_array_almost_equal ( c [ <NUM_LIT:0> , <NUM_LIT:1> ] , c_t , <NUM_LIT:2> ) <EOL> def test_coherence_matlab ( ) : <EOL> """<STR_LIT>""" <EOL> ts = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> ts0 = ts [ <NUM_LIT:1> ] <EOL> ts1 = ts [ <NUM_LIT:0> ] <EOL> method = { } <EOL> method [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> method [ '<STR_LIT>' ] = <NUM_LIT:64> <EOL> method [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> method [ '<STR_LIT>' ] = method [ '<STR_LIT>' ] / <NUM_LIT:2> <EOL> ttt = np . vstack ( [ ts0 , ts1 ] ) <EOL> f , cxy_mlab = tsa . coherence ( ttt , csd_method = method ) <EOL> cxy_matlab = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> npt . assert_almost_equal ( cxy_mlab [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , cxy_matlab , decimal = <NUM_LIT:5> ) <EOL> @ npt . dec . skipif ( old_mpl ) <EOL> def test_cached_coherence ( ) : <EOL> """<STR_LIT>""" <EOL> NFFT = <NUM_LIT:64> <EOL> n_freqs = NFFT // <NUM_LIT:2> + <NUM_LIT:1> <EOL> ij = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ] <EOL> ts = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij ) <EOL> npt . assert_equal ( freqs , utils . get_freqs ( <NUM_LIT:2> * np . pi , NFFT ) ) <EOL> hann = mlab . window_hanning ( np . ones ( NFFT ) ) <EOL> w_ts = ts [ <NUM_LIT:0> ] [ : NFFT ] * hann <EOL> w_ft = fftpack . fft ( w_ts ) [ <NUM_LIT:0> : n_freqs ] <EOL> first_window_fft = cache [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> npt . assert_equal ( w_ft , first_window_fft ) <EOL> coh_cached = tsa . cache_to_coherency ( cache , ij ) [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> f , c = tsa . coherency ( ts ) <EOL> coh_direct = c [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> npt . assert_almost_equal ( coh_direct , coh_cached ) <EOL> npt . assert_raises ( ValueError , tsa . cache_fft , ts , ij , method = methods [ <NUM_LIT:2> ] ) <EOL> freqs , cache1 = tsa . cache_fft ( ts , ij , method = methods [ <NUM_LIT:3> ] ) <EOL> freqs , cache2 = tsa . cache_fft ( ts , ij , method = methods [ <NUM_LIT:4> ] ) <EOL> npt . assert_equal ( cache1 , cache2 ) <EOL> freqs , cache1 = tsa . cache_fft ( ts , ij , method = methods [ <NUM_LIT:3> ] , <EOL> scale_by_freq = False ) <EOL> freqs , cache2 = tsa . cache_fft ( ts , ij , method = methods [ <NUM_LIT:4> ] , <EOL> scale_by_freq = False ) <EOL> npt . assert_equal ( cache1 , cache2 ) <EOL> psd1 = tsa . cache_to_psd ( cache , ij ) [ <NUM_LIT:0> ] <EOL> f , c = tsa . get_spectra ( ts ) <EOL> psd2 = c [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> npt . assert_almost_equal ( psd1 , psd2 ) <EOL> freqs , cache1 = tsa . cache_fft ( ts , ij ) <EOL> freqs , cache2 = tsa . cache_fft ( ts , ij , prefer_speed_over_memory = True ) <EOL> psd1 = tsa . cache_to_psd ( cache1 , ij ) [ <NUM_LIT:0> ] <EOL> psd2 = tsa . cache_to_psd ( cache2 , ij ) [ <NUM_LIT:0> ] <EOL> npt . assert_almost_equal ( psd1 , psd2 ) <EOL> def test_cache_to_phase ( ) : <EOL> """<STR_LIT>""" <EOL> ij = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ] <EOL> x = np . sin ( t ) + np . sin ( <NUM_LIT:2> * t ) + np . sin ( <NUM_LIT:3> * t ) + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> y = np . sin ( t ) + np . sin ( <NUM_LIT:2> * t ) + np . sin ( <NUM_LIT:3> * t ) + np . random . rand ( t . shape [ - <NUM_LIT:1> ] ) <EOL> ts = np . vstack ( [ x , y ] ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij ) <EOL> ph = tsa . cache_to_phase ( cache , ij ) <EOL> def test_cache_to_coherency ( ) : <EOL> """<STR_LIT>""" <EOL> ij = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ] <EOL> ts = np . loadtxt ( os . path . join ( test_dir_path , '<STR_LIT>' ) ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij ) <EOL> Cxy = tsa . cache_to_coherency ( cache , ij ) <EOL> f , c = tsa . coherency ( ts ) <EOL> npt . assert_almost_equal ( Cxy [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , c [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> freqs , cache2 = tsa . cache_fft ( ts , ij , prefer_speed_over_memory = True ) <EOL> Cxy2 = tsa . cache_to_coherency ( cache2 , ij ) <EOL> npt . assert_equal ( Cxy2 , Cxy ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij ) <EOL> tsa . cache_to_relative_phase ( cache , ij ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij , prefer_speed_over_memory = True ) <EOL> tsa . cache_to_relative_phase ( cache , ij ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij , method = dict ( this_method = '<STR_LIT>' , <EOL> NFFT = ts . shape [ - <NUM_LIT:1> ] , <EOL> n_overlap = <NUM_LIT:0> ) ) <EOL> cxy_one_window = tsa . cache_to_coherency ( cache , ij ) <EOL> ph_one_window = tsa . cache_to_relative_phase ( cache , ij ) <EOL> freqs , cache = tsa . cache_fft ( ts , ij , method = dict ( this_method = '<STR_LIT>' , <EOL> NFFT = ts . shape [ - <NUM_LIT:1> ] , <EOL> n_overlap = <NUM_LIT:0> ) , <EOL> prefer_speed_over_memory = True ) <EOL> cxy_one_window = tsa . cache_to_coherency ( cache , ij ) <EOL> ph_one_window = tsa . cache_to_relative_phase ( cache , ij ) </s>
