<s> """<STR_LIT>""" <EOL> import os . path <EOL> import numpy as np <EOL> from nose . tools import assert_true , assert_false , assert_raises <EOL> from sklearn . utils . testing import assert_less <EOL> from nilearn import signal as nisignal <EOL> from nilearn . signal import clean <EOL> import scipy . signal <EOL> def generate_signals ( n_features = <NUM_LIT> , n_confounds = <NUM_LIT:5> , length = <NUM_LIT> , <EOL> same_variance = True , order = "<STR_LIT:C>" ) : <EOL> """<STR_LIT>""" <EOL> rand_gen = np . random . RandomState ( <NUM_LIT:0> ) <EOL> confounds_shape = ( length , n_confounds ) <EOL> confounds = np . ndarray ( confounds_shape , order = order ) <EOL> confounds [ ... ] = rand_gen . randn ( * confounds_shape ) <EOL> confounds [ ... ] = scipy . signal . detrend ( confounds , axis = <NUM_LIT:0> ) <EOL> factors = rand_gen . randn ( n_confounds , n_features ) <EOL> noises_shape = ( length , n_features ) <EOL> noises = np . ndarray ( noises_shape , order = order ) <EOL> noises [ ... ] = np . dot ( confounds , factors ) <EOL> noises [ ... ] = scipy . signal . detrend ( noises , axis = <NUM_LIT:0> ) <EOL> signals_shape = noises_shape <EOL> signals = np . ndarray ( signals_shape , order = order ) <EOL> if same_variance : <EOL> signals [ ... ] = rand_gen . randn ( * signals_shape ) <EOL> else : <EOL> signals [ ... ] = ( <NUM_LIT> * abs ( rand_gen . randn ( signals_shape [ <NUM_LIT:1> ] ) ) + <NUM_LIT:0.5> <EOL> ) * rand_gen . randn ( * signals_shape ) <EOL> signals [ ... ] = scipy . signal . detrend ( signals , axis = <NUM_LIT:0> ) <EOL> return signals , noises , confounds <EOL> def generate_trends ( n_features = <NUM_LIT> , length = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> rand_gen = np . random . RandomState ( <NUM_LIT:0> ) <EOL> trends = scipy . signal . detrend ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:1.0> , length ) , type = "<STR_LIT>" ) <EOL> trends = np . repeat ( np . atleast_2d ( trends ) . T , n_features , axis = <NUM_LIT:1> ) <EOL> factors = rand_gen . randn ( n_features ) <EOL> return trends * factors <EOL> def test_butterworth ( ) : <EOL> rand_gen = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_features = <NUM_LIT> <EOL> n_samples = <NUM_LIT:100> <EOL> sampling = <NUM_LIT:100> <EOL> low_pass = <NUM_LIT:30> <EOL> high_pass = <NUM_LIT:10> <EOL> data = rand_gen . randn ( n_samples ) <EOL> data_original = data . copy ( ) <EOL> out_single = nisignal . butterworth ( data , sampling , <EOL> low_pass = low_pass , high_pass = high_pass , <EOL> copy = True ) <EOL> np . testing . assert_almost_equal ( data , data_original ) <EOL> nisignal . butterworth ( data , sampling , <EOL> low_pass = low_pass , high_pass = high_pass , <EOL> copy = False , save_memory = True ) <EOL> np . testing . assert_almost_equal ( out_single , data ) <EOL> data = rand_gen . randn ( n_samples , n_features ) <EOL> data [ : , <NUM_LIT:0> ] = data_original <EOL> data_original = data . copy ( ) <EOL> out1 = nisignal . butterworth ( data , sampling , <EOL> low_pass = low_pass , high_pass = high_pass , <EOL> copy = True ) <EOL> np . testing . assert_almost_equal ( data , data_original ) <EOL> np . testing . assert_almost_equal ( out1 [ : , <NUM_LIT:0> ] , out_single ) <EOL> nisignal . butterworth ( data , sampling , <EOL> low_pass = low_pass , high_pass = high_pass , <EOL> copy = False ) <EOL> np . testing . assert_almost_equal ( out1 , data ) <EOL> out1 = nisignal . butterworth ( data , sampling , <EOL> low_pass = <NUM_LIT> , <EOL> copy = True ) <EOL> out2 = nisignal . butterworth ( data , sampling , <EOL> low_pass = <NUM_LIT> , <EOL> copy = True ) <EOL> np . testing . assert_almost_equal ( out1 , out2 ) <EOL> def test_standardize ( ) : <EOL> rand_gen = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_features = <NUM_LIT:10> <EOL> n_samples = <NUM_LIT> <EOL> a = rand_gen . random_sample ( ( n_samples , n_features ) ) <EOL> a += np . linspace ( <NUM_LIT:0> , <NUM_LIT> , n_features ) <EOL> b = nisignal . _standardize ( a , normalize = True ) <EOL> energies = ( b ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) <EOL> np . testing . assert_almost_equal ( energies , np . ones ( n_features ) ) <EOL> np . testing . assert_almost_equal ( b . sum ( axis = <NUM_LIT:0> ) , np . zeros ( n_features ) ) <EOL> a = np . atleast_2d ( np . linspace ( <NUM_LIT:0> , <NUM_LIT> , n_features ) ) . T <EOL> b = nisignal . _standardize ( a , detrend = True , normalize = False ) <EOL> np . testing . assert_almost_equal ( b , np . zeros ( b . shape ) ) <EOL> length_1_signal = np . atleast_2d ( np . linspace ( <NUM_LIT:0> , <NUM_LIT> , n_features ) ) <EOL> np . testing . assert_array_equal ( length_1_signal , <EOL> nisignal . _standardize ( length_1_signal , <EOL> normalize = True ) ) <EOL> def test_detrend ( ) : <EOL> """<STR_LIT>""" <EOL> point_number = <NUM_LIT> <EOL> features = <NUM_LIT> <EOL> signals , _ , _ = generate_signals ( n_features = features , <EOL> length = point_number , <EOL> same_variance = True ) <EOL> trends = generate_trends ( n_features = features , length = point_number ) <EOL> x = signals + trends + <NUM_LIT:1> <EOL> original = x . copy ( ) <EOL> detrended = nisignal . _detrend ( x , inplace = False , type = "<STR_LIT>" ) <EOL> assert_true ( abs ( detrended . mean ( axis = <NUM_LIT:0> ) ) . max ( ) <EOL> < <NUM_LIT> * np . finfo ( np . float ) . eps ) <EOL> detrended = nisignal . _detrend ( x , inplace = False ) <EOL> detrended_scipy = scipy . signal . detrend ( x , axis = <NUM_LIT:0> ) <EOL> np . testing . assert_almost_equal ( original , x , decimal = <NUM_LIT> ) <EOL> assert_true ( abs ( detrended . mean ( axis = <NUM_LIT:0> ) ) . max ( ) < <EOL> <NUM_LIT> * np . finfo ( np . float ) . eps ) <EOL> np . testing . assert_almost_equal ( detrended_scipy , detrended , decimal = <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( detrended , signals , decimal = <NUM_LIT> ) <EOL> nisignal . _detrend ( x , inplace = True ) <EOL> assert_true ( abs ( x . mean ( axis = <NUM_LIT:0> ) ) . max ( ) < <NUM_LIT> * np . finfo ( np . float ) . eps ) <EOL> np . testing . assert_almost_equal ( detrended_scipy , detrended , decimal = <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( x , signals , decimal = <NUM_LIT> ) <EOL> length_1_signal = x [ <NUM_LIT:0> ] <EOL> length_1_signal = length_1_signal [ np . newaxis , : ] <EOL> np . testing . assert_array_equal ( length_1_signal , <EOL> nisignal . _detrend ( length_1_signal ) ) <EOL> detrended = nisignal . _detrend ( x . astype ( np . int64 ) , inplace = True , <EOL> type = "<STR_LIT>" ) <EOL> assert_less ( abs ( detrended . mean ( axis = <NUM_LIT:0> ) ) . max ( ) , <EOL> <NUM_LIT> * np . finfo ( np . float ) . eps ) <EOL> def test_mean_of_squares ( ) : <EOL> """<STR_LIT>""" <EOL> n_samples = <NUM_LIT:11> <EOL> n_features = <NUM_LIT> <EOL> signals , _ , _ = generate_signals ( n_features = n_features , <EOL> length = n_samples , <EOL> same_variance = True ) <EOL> var1 = np . copy ( signals ) <EOL> var1 **= <NUM_LIT:2> <EOL> var1 = var1 . mean ( axis = <NUM_LIT:0> ) <EOL> var2 = nisignal . _mean_of_squares ( signals ) <EOL> np . testing . assert_almost_equal ( var1 , var2 ) <EOL> def test_clean_detrending ( ) : <EOL> n_samples = <NUM_LIT> <EOL> n_features = <NUM_LIT> <EOL> signals , _ , _ = generate_signals ( n_features = n_features , <EOL> length = n_samples ) <EOL> trends = generate_trends ( n_features = n_features , <EOL> length = n_samples ) <EOL> x = signals + trends <EOL> x_detrended = nisignal . clean ( x , standardize = False , detrend = True , <EOL> low_pass = None , high_pass = None ) <EOL> np . testing . assert_almost_equal ( x_detrended , signals , decimal = <NUM_LIT> ) <EOL> x_undetrended = nisignal . clean ( x , standardize = False , detrend = False , <EOL> low_pass = None , high_pass = None ) <EOL> assert_false ( abs ( x_undetrended - signals ) . max ( ) < <NUM_LIT> ) <EOL> def test_clean_frequencies ( ) : <EOL> sx1 = np . sin ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT> ) ) <EOL> sx2 = np . sin ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT> ) ) <EOL> sx = np . vstack ( ( sx1 , sx2 ) ) . T <EOL> assert_true ( clean ( sx , standardize = False , high_pass = <NUM_LIT> , low_pass = None ) <EOL> . max ( ) > <NUM_LIT:0.1> ) <EOL> assert_true ( clean ( sx , standardize = False , high_pass = <NUM_LIT> , low_pass = None ) <EOL> . max ( ) < <NUM_LIT> ) <EOL> assert_true ( clean ( sx , standardize = False , low_pass = <NUM_LIT> ) . max ( ) > <NUM_LIT> ) <EOL> assert_raises ( ValueError , clean , sx , low_pass = <NUM_LIT> , high_pass = <NUM_LIT:0.5> ) <EOL> def test_clean_confounds ( ) : <EOL> signals , noises , confounds = generate_signals ( n_features = <NUM_LIT> , <EOL> n_confounds = <NUM_LIT:5> , length = <NUM_LIT> ) <EOL> eps = np . finfo ( np . float ) . eps <EOL> noises1 = noises . copy ( ) <EOL> cleaned_signals = nisignal . clean ( noises , confounds = confounds , <EOL> detrend = True , standardize = False ) <EOL> assert_true ( abs ( cleaned_signals ) . max ( ) < <NUM_LIT> * eps ) <EOL> np . testing . assert_almost_equal ( noises , noises1 , decimal = <NUM_LIT:12> ) <EOL> cleaned_signals = nisignal . clean ( signals + noises , confounds = confounds , <EOL> detrend = False , standardize = True ) <EOL> assert_true ( abs ( np . dot ( confounds . T , cleaned_signals ) ) . max ( ) < <NUM_LIT> * eps ) <EOL> confounds1 = np . hstack ( ( np . ones ( ( <NUM_LIT> , <NUM_LIT:1> ) ) , confounds ) ) <EOL> cleaned_signals1 = nisignal . clean ( signals + noises , confounds = confounds1 , <EOL> detrend = False , standardize = True ) <EOL> np . testing . assert_almost_equal ( cleaned_signals1 , cleaned_signals ) <EOL> temp = confounds . T <EOL> temp += np . arange ( confounds . shape [ <NUM_LIT:0> ] ) <EOL> cleaned_signals = nisignal . clean ( signals + noises , confounds = confounds , <EOL> detrend = False , standardize = False ) <EOL> coeffs = np . polyfit ( np . arange ( cleaned_signals . shape [ <NUM_LIT:0> ] ) , <EOL> cleaned_signals , <NUM_LIT:1> ) <EOL> assert_true ( ( abs ( coeffs ) > <NUM_LIT> ) . any ( ) ) <EOL> cleaned_signals = nisignal . clean ( signals + noises , confounds = confounds , <EOL> detrend = True , standardize = False ) <EOL> coeffs = np . polyfit ( np . arange ( cleaned_signals . shape [ <NUM_LIT:0> ] ) , <EOL> cleaned_signals , <NUM_LIT:1> ) <EOL> assert_true ( ( abs ( coeffs ) < <NUM_LIT> * eps ) . all ( ) ) <EOL> input_signals = <NUM_LIT:10> * signals <EOL> cleaned_signals = nisignal . clean ( input_signals , detrend = False , <EOL> standardize = False ) <EOL> np . testing . assert_almost_equal ( cleaned_signals , input_signals ) <EOL> cleaned_signals = nisignal . clean ( input_signals , detrend = False , <EOL> standardize = True ) <EOL> np . testing . assert_almost_equal ( cleaned_signals . var ( axis = <NUM_LIT:0> ) , <EOL> np . ones ( cleaned_signals . shape [ <NUM_LIT:1> ] ) ) <EOL> current_dir = os . path . split ( __file__ ) [ <NUM_LIT:0> ] <EOL> signals , _ , confounds = generate_signals ( n_features = <NUM_LIT> , <EOL> n_confounds = <NUM_LIT:3> , length = <NUM_LIT:20> ) <EOL> filename1 = os . path . join ( current_dir , "<STR_LIT:data>" , "<STR_LIT>" ) <EOL> filename2 = os . path . join ( current_dir , "<STR_LIT:data>" , <EOL> "<STR_LIT>" ) <EOL> nisignal . clean ( signals , detrend = False , standardize = False , <EOL> confounds = filename1 ) <EOL> nisignal . clean ( signals , detrend = False , standardize = False , <EOL> confounds = filename2 ) <EOL> nisignal . clean ( signals , detrend = False , standardize = False , <EOL> confounds = confounds [ : , <NUM_LIT:1> ] ) <EOL> nisignal . clean ( signals , detrend = False , standardize = False , <EOL> confounds = [ filename1 , confounds [ : , <NUM_LIT:0> : <NUM_LIT:2> ] , <EOL> filename2 , confounds [ : , <NUM_LIT:2> ] ] ) <EOL> assert_raises ( TypeError , nisignal . clean , signals , confounds = <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , nisignal . clean , signals , confounds = np . zeros ( <NUM_LIT:2> ) ) <EOL> assert_raises ( ValueError , nisignal . clean , signals , <EOL> confounds = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> assert_raises ( ValueError , nisignal . clean , signals , <EOL> confounds = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) ) <EOL> assert_raises ( ValueError , nisignal . clean , signals [ : - <NUM_LIT:1> , : ] , <EOL> confounds = filename1 ) <EOL> assert_raises ( TypeError , nisignal . clean , signals , <EOL> confounds = [ None ] ) <EOL> np . testing . assert_almost_equal ( nisignal . clean ( np . ones ( ( <NUM_LIT:20> , <NUM_LIT:2> ) ) , <EOL> standardize = False , <EOL> confounds = np . ones ( <NUM_LIT:20> ) , <EOL> detrend = False , <EOL> ) . mean ( ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:2> ) ) ) <EOL> def test_high_variance_confounds ( ) : <EOL> n_features = <NUM_LIT> <EOL> length = <NUM_LIT:20> <EOL> n_confounds = <NUM_LIT:5> <EOL> seriesC , _ , _ = generate_signals ( n_features = n_features , <EOL> length = length , order = "<STR_LIT:C>" ) <EOL> seriesF , _ , _ = generate_signals ( n_features = n_features , <EOL> length = length , order = "<STR_LIT:F>" ) <EOL> np . testing . assert_almost_equal ( seriesC , seriesF , decimal = <NUM_LIT> ) <EOL> outC = nisignal . high_variance_confounds ( seriesC , n_confounds = n_confounds , <EOL> detrend = False ) <EOL> outF = nisignal . high_variance_confounds ( seriesF , n_confounds = n_confounds , <EOL> detrend = False ) <EOL> np . testing . assert_almost_equal ( outC , outF , decimal = <NUM_LIT> ) <EOL> seriesG = <NUM_LIT:2> * seriesC <EOL> outG = nisignal . high_variance_confounds ( seriesG , n_confounds = n_confounds , <EOL> detrend = False ) <EOL> np . testing . assert_almost_equal ( outC , outG , decimal = <NUM_LIT> ) <EOL> assert ( outG . shape == ( length , n_confounds ) ) <EOL> seriesG = seriesC <EOL> outG = nisignal . high_variance_confounds ( seriesG , percentile = <NUM_LIT:1.> , <EOL> n_confounds = n_confounds , <EOL> detrend = False ) <EOL> assert_raises ( AssertionError , np . testing . assert_almost_equal , <EOL> outC , outG , decimal = <NUM_LIT> ) <EOL> assert ( outG . shape == ( length , n_confounds ) ) <EOL> out = nisignal . high_variance_confounds ( seriesG , n_confounds = <NUM_LIT:7> , <EOL> detrend = False ) <EOL> assert ( out . shape == ( length , <NUM_LIT:7> ) ) <EOL> seriesG = seriesC <EOL> trends = generate_trends ( n_features = n_features , length = length ) <EOL> seriesGt = seriesG + trends <EOL> outG = nisignal . high_variance_confounds ( seriesG , detrend = False , <EOL> n_confounds = n_confounds ) <EOL> outGt = nisignal . high_variance_confounds ( seriesGt , detrend = True , <EOL> n_confounds = n_confounds ) <EOL> np . testing . assert_almost_equal ( np . abs ( outG . T . dot ( outG ) ) , <EOL> np . identity ( outG . shape [ <NUM_LIT:1> ] ) , <EOL> decimal = <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( <EOL> np . min ( np . abs ( np . dstack ( [ outG - outGt , outG + outGt ] ) ) , axis = <NUM_LIT:2> ) , <EOL> np . zeros ( outG . shape ) ) </s>
