<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . testing import assert_array_almost_equal <EOL> from nose . tools import assert_true , assert_raises <EOL> import nibabel <EOL> from nilearn . _utils . testing import assert_less_equal <EOL> from nilearn . decomposition . canica import CanICA <EOL> from nilearn . image import iter_img <EOL> def _make_data_from_components ( components , affine , shape , rng = None , <EOL> n_subjects = <NUM_LIT:8> ) : <EOL> data = [ ] <EOL> if rng is None : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> for _ in range ( n_subjects ) : <EOL> this_data = np . dot ( rng . normal ( size = ( <NUM_LIT> , <NUM_LIT:4> ) ) , components ) <EOL> this_data += <NUM_LIT> * rng . normal ( size = this_data . shape ) <EOL> this_data = np . reshape ( this_data , ( <NUM_LIT> , ) + shape ) <EOL> this_data = np . rollaxis ( this_data , <NUM_LIT:0> , <NUM_LIT:4> ) <EOL> data . append ( nibabel . Nifti1Image ( this_data , affine ) ) <EOL> return data <EOL> def _make_canica_components ( shape ) : <EOL> component1 = np . zeros ( shape ) <EOL> component1 [ : <NUM_LIT:5> , : <NUM_LIT:10> ] = <NUM_LIT:1> <EOL> component1 [ <NUM_LIT:5> : <NUM_LIT:10> , : <NUM_LIT:10> ] = - <NUM_LIT:1> <EOL> component2 = np . zeros ( shape ) <EOL> component2 [ : <NUM_LIT:5> , - <NUM_LIT:10> : ] = <NUM_LIT:1> <EOL> component2 [ <NUM_LIT:5> : <NUM_LIT:10> , - <NUM_LIT:10> : ] = - <NUM_LIT:1> <EOL> component3 = np . zeros ( shape ) <EOL> component3 [ - <NUM_LIT:5> : , - <NUM_LIT:10> : ] = <NUM_LIT:1> <EOL> component3 [ - <NUM_LIT:10> : - <NUM_LIT:5> , - <NUM_LIT:10> : ] = - <NUM_LIT:1> <EOL> component4 = np . zeros ( shape ) <EOL> component4 [ - <NUM_LIT:5> : , : <NUM_LIT:10> ] = <NUM_LIT:1> <EOL> component4 [ - <NUM_LIT:10> : - <NUM_LIT:5> , : <NUM_LIT:10> ] = - <NUM_LIT:1> <EOL> return np . vstack ( ( component1 . ravel ( ) , component2 . ravel ( ) , <EOL> component3 . ravel ( ) , component4 . ravel ( ) ) ) <EOL> def _make_canica_test_data ( rng = None , n_subjects = <NUM_LIT:8> , noisy = False ) : <EOL> if rng is None : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> shape = ( <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:1> ) <EOL> affine = np . eye ( <NUM_LIT:4> ) <EOL> components = _make_canica_components ( shape ) <EOL> if noisy : <EOL> components [ rng . randn ( * components . shape ) > <NUM_LIT> ] *= - <NUM_LIT> <EOL> for mp in components : <EOL> assert_less_equal ( mp . max ( ) , - mp . min ( ) ) <EOL> data = _make_data_from_components ( components , affine , shape , rng = rng , <EOL> n_subjects = n_subjects ) <EOL> mask_img = nibabel . Nifti1Image ( np . ones ( shape , dtype = np . int8 ) , affine ) <EOL> return data , mask_img , components , rng <EOL> def test_canica_square_img ( ) : <EOL> data , mask_img , components , rng = _make_canica_test_data ( ) <EOL> canica = CanICA ( n_components = <NUM_LIT:4> , random_state = rng , mask = mask_img , <EOL> smoothing_fwhm = <NUM_LIT:0.> , n_init = <NUM_LIT:50> ) <EOL> canica . fit ( data ) <EOL> maps = canica . masker_ . inverse_transform ( canica . components_ ) . get_data ( ) <EOL> maps = np . rollaxis ( maps , <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> K = np . corrcoef ( components , maps . reshape ( <NUM_LIT:4> , <NUM_LIT> ) ) [ <NUM_LIT:4> : , : <NUM_LIT:4> ] <EOL> K_abs = np . abs ( K ) <EOL> assert_true ( np . sum ( K_abs > <NUM_LIT> ) == <NUM_LIT:4> ) <EOL> K_abs [ K_abs > <NUM_LIT> ] -= <NUM_LIT:1> <EOL> assert_array_almost_equal ( K_abs , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> assert_raises ( TypeError , canica . fit ) <EOL> def test_component_sign ( ) : <EOL> data , mask_img , components , rng = _make_canica_test_data ( n_subjects = <NUM_LIT:2> , <EOL> noisy = True ) <EOL> canica = CanICA ( n_components = <NUM_LIT:4> , random_state = rng , mask = mask_img ) <EOL> for _ in range ( <NUM_LIT:3> ) : <EOL> canica . fit ( data ) <EOL> for mp in iter_img ( canica . masker_ . inverse_transform ( <EOL> canica . components_ ) ) : <EOL> mp = mp . get_data ( ) <EOL> assert_less_equal ( - mp . min ( ) , mp . max ( ) ) </s>
