<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . testing import assert_almost_equal <EOL> from scipy import stats <EOL> from scipy import linalg <EOL> from mne . preprocessing . infomax_ import infomax <EOL> from mne . utils import requires_sklearn , run_tests_if_main <EOL> def center_and_norm ( x , axis = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> x = np . rollaxis ( x , axis ) <EOL> x -= x . mean ( axis = <NUM_LIT:0> ) <EOL> x /= x . std ( axis = <NUM_LIT:0> ) <EOL> @ requires_sklearn <EOL> def test_infomax_blowup ( ) : <EOL> """<STR_LIT>""" <EOL> from sklearn . decomposition import RandomizedPCA <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> n_samples = <NUM_LIT:100> <EOL> s1 = ( <NUM_LIT:2> * np . sin ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:100> , n_samples ) ) > <NUM_LIT:0> ) - <NUM_LIT:1> <EOL> s2 = stats . t . rvs ( <NUM_LIT:1> , size = n_samples ) <EOL> s = np . c_ [ s1 , s2 ] . T <EOL> center_and_norm ( s ) <EOL> s1 , s2 = s <EOL> phi = <NUM_LIT> <EOL> mixing = np . array ( [ [ np . cos ( phi ) , np . sin ( phi ) ] , <EOL> [ np . sin ( phi ) , - np . cos ( phi ) ] ] ) <EOL> m = np . dot ( mixing , s ) <EOL> center_and_norm ( m ) <EOL> X = RandomizedPCA ( n_components = <NUM_LIT:2> , whiten = True ) . fit_transform ( m . T ) <EOL> k_ = infomax ( X , extended = True , l_rate = <NUM_LIT:0.1> ) <EOL> s_ = np . dot ( k_ , X . T ) <EOL> center_and_norm ( s_ ) <EOL> s1_ , s2_ = s_ <EOL> if abs ( np . dot ( s1_ , s2 ) ) > abs ( np . dot ( s1_ , s1 ) ) : <EOL> s2_ , s1_ = s_ <EOL> s1_ *= np . sign ( np . dot ( s1_ , s1 ) ) <EOL> s2_ *= np . sign ( np . dot ( s2_ , s2 ) ) <EOL> assert_almost_equal ( np . dot ( s1_ , s1 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:2> ) <EOL> assert_almost_equal ( np . dot ( s2_ , s2 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:2> ) <EOL> @ requires_sklearn <EOL> def test_infomax_simple ( ) : <EOL> """<STR_LIT>""" <EOL> from sklearn . decomposition import RandomizedPCA <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> n_samples = <NUM_LIT> <EOL> s1 = ( <NUM_LIT:2> * np . sin ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:100> , n_samples ) ) > <NUM_LIT:0> ) - <NUM_LIT:1> <EOL> s2 = stats . t . rvs ( <NUM_LIT:1> , size = n_samples ) <EOL> s = np . c_ [ s1 , s2 ] . T <EOL> center_and_norm ( s ) <EOL> s1 , s2 = s <EOL> phi = <NUM_LIT> <EOL> mixing = np . array ( [ [ np . cos ( phi ) , np . sin ( phi ) ] , <EOL> [ np . sin ( phi ) , - np . cos ( phi ) ] ] ) <EOL> for add_noise in ( False , True ) : <EOL> m = np . dot ( mixing , s ) <EOL> if add_noise : <EOL> m += <NUM_LIT:0.1> * rng . randn ( <NUM_LIT:2> , n_samples ) <EOL> center_and_norm ( m ) <EOL> algos = [ True , False ] <EOL> for algo in algos : <EOL> X = RandomizedPCA ( n_components = <NUM_LIT:2> , whiten = True ) . fit_transform ( m . T ) <EOL> k_ = infomax ( X , extended = algo ) <EOL> s_ = np . dot ( k_ , X . T ) <EOL> center_and_norm ( s_ ) <EOL> s1_ , s2_ = s_ <EOL> if abs ( np . dot ( s1_ , s2 ) ) > abs ( np . dot ( s1_ , s1 ) ) : <EOL> s2_ , s1_ = s_ <EOL> s1_ *= np . sign ( np . dot ( s1_ , s1 ) ) <EOL> s2_ *= np . sign ( np . dot ( s2_ , s2 ) ) <EOL> if not add_noise : <EOL> assert_almost_equal ( np . dot ( s1_ , s1 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:2> ) <EOL> assert_almost_equal ( np . dot ( s2_ , s2 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:2> ) <EOL> else : <EOL> assert_almost_equal ( np . dot ( s1_ , s1 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:1> ) <EOL> assert_almost_equal ( np . dot ( s2_ , s2 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:1> ) <EOL> @ requires_sklearn <EOL> def test_non_square_infomax ( ) : <EOL> """<STR_LIT>""" <EOL> from sklearn . decomposition import RandomizedPCA <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples = <NUM_LIT:200> <EOL> t = np . linspace ( <NUM_LIT:0> , <NUM_LIT:100> , n_samples ) <EOL> s1 = np . sin ( t ) <EOL> s2 = np . ceil ( np . sin ( np . pi * t ) ) <EOL> s = np . c_ [ s1 , s2 ] . T <EOL> center_and_norm ( s ) <EOL> s1 , s2 = s <EOL> n_observed = <NUM_LIT:6> <EOL> mixing = rng . randn ( n_observed , <NUM_LIT:2> ) <EOL> for add_noise in ( False , True ) : <EOL> m = np . dot ( mixing , s ) <EOL> if add_noise : <EOL> m += <NUM_LIT:0.1> * rng . randn ( n_observed , n_samples ) <EOL> center_and_norm ( m ) <EOL> pca = RandomizedPCA ( n_components = <NUM_LIT:2> , whiten = True , random_state = rng ) <EOL> m = m . T <EOL> m = pca . fit_transform ( m ) <EOL> unmixing_ = infomax ( m , random_state = rng , extended = True ) <EOL> s_ = np . dot ( unmixing_ , m . T ) <EOL> mixing_ = linalg . pinv ( unmixing_ . T ) <EOL> assert_almost_equal ( m , s_ . T . dot ( mixing_ ) ) <EOL> center_and_norm ( s_ ) <EOL> s1_ , s2_ = s_ <EOL> if abs ( np . dot ( s1_ , s2 ) ) > abs ( np . dot ( s1_ , s1 ) ) : <EOL> s2_ , s1_ = s_ <EOL> s1_ *= np . sign ( np . dot ( s1_ , s1 ) ) <EOL> s2_ *= np . sign ( np . dot ( s2_ , s2 ) ) <EOL> if not add_noise : <EOL> assert_almost_equal ( np . dot ( s1_ , s1 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:2> ) <EOL> assert_almost_equal ( np . dot ( s2_ , s2 ) / n_samples , <NUM_LIT:1> , decimal = <NUM_LIT:2> ) <EOL> run_tests_if_main ( ) </s>
