<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numpy as np <EOL> from scipy import stats <EOL> from . external . six . moves import range <EOL> def bootstrap ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( np . unique ( list ( map ( len , args ) ) ) ) > <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> n = len ( args [ <NUM_LIT:0> ] ) <EOL> n_boot = kwargs . get ( "<STR_LIT>" , <NUM_LIT> ) <EOL> func = kwargs . get ( "<STR_LIT>" , np . mean ) <EOL> axis = kwargs . get ( "<STR_LIT>" , None ) <EOL> units = kwargs . get ( "<STR_LIT>" , None ) <EOL> smooth = kwargs . get ( "<STR_LIT>" , False ) <EOL> random_seed = kwargs . get ( "<STR_LIT>" , None ) <EOL> if axis is None : <EOL> func_kwargs = dict ( ) <EOL> else : <EOL> func_kwargs = dict ( axis = axis ) <EOL> rs = np . random . RandomState ( random_seed ) <EOL> args = list ( map ( np . asarray , args ) ) <EOL> if units is not None : <EOL> units = np . asarray ( units ) <EOL> if smooth : <EOL> return _smooth_bootstrap ( args , n_boot , func , func_kwargs ) <EOL> if units is not None : <EOL> return _structured_bootstrap ( args , n_boot , units , func , <EOL> func_kwargs , rs ) <EOL> boot_dist = [ ] <EOL> for i in range ( int ( n_boot ) ) : <EOL> resampler = rs . randint ( <NUM_LIT:0> , n , n ) <EOL> sample = [ a . take ( resampler , axis = <NUM_LIT:0> ) for a in args ] <EOL> boot_dist . append ( func ( * sample , ** func_kwargs ) ) <EOL> return np . array ( boot_dist ) <EOL> def _structured_bootstrap ( args , n_boot , units , func , func_kwargs , rs ) : <EOL> """<STR_LIT>""" <EOL> unique_units = np . unique ( units ) <EOL> n_units = len ( unique_units ) <EOL> args = [ [ a [ units == unit ] for unit in unique_units ] for a in args ] <EOL> boot_dist = [ ] <EOL> for i in range ( int ( n_boot ) ) : <EOL> resampler = rs . randint ( <NUM_LIT:0> , n_units , n_units ) <EOL> sample = [ np . take ( a , resampler , axis = <NUM_LIT:0> ) for a in args ] <EOL> lengths = map ( len , sample [ <NUM_LIT:0> ] ) <EOL> resampler = [ rs . randint ( <NUM_LIT:0> , n , n ) for n in lengths ] <EOL> sample = [ [ c . take ( r , axis = <NUM_LIT:0> ) for c , r in zip ( a , resampler ) ] <EOL> for a in sample ] <EOL> sample = list ( map ( np . concatenate , sample ) ) <EOL> boot_dist . append ( func ( * sample , ** func_kwargs ) ) <EOL> return np . array ( boot_dist ) <EOL> def _smooth_bootstrap ( args , n_boot , func , func_kwargs ) : <EOL> """<STR_LIT>""" <EOL> n = len ( args [ <NUM_LIT:0> ] ) <EOL> boot_dist = [ ] <EOL> kde = [ stats . gaussian_kde ( np . transpose ( a ) ) for a in args ] <EOL> for i in range ( int ( n_boot ) ) : <EOL> sample = [ a . resample ( n ) . T for a in kde ] <EOL> boot_dist . append ( func ( * sample , ** func_kwargs ) ) <EOL> return np . array ( boot_dist ) <EOL> def randomize_corrmat ( a , tail = "<STR_LIT>" , corrected = True , n_iter = <NUM_LIT:1000> , <EOL> random_seed = None , return_dist = False ) : <EOL> """<STR_LIT>""" <EOL> if tail not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> rs = np . random . RandomState ( random_seed ) <EOL> a = np . asarray ( a , np . float ) <EOL> flat_a = a . ravel ( ) <EOL> n_vars , n_obs = a . shape <EOL> null_dist = np . empty ( ( n_vars , n_vars , n_iter ) ) <EOL> for i_i in range ( n_iter ) : <EOL> perm_i = np . concatenate ( [ rs . permutation ( n_obs ) + ( v * n_obs ) <EOL> for v in range ( n_vars ) ] ) <EOL> a_i = flat_a [ perm_i ] . reshape ( n_vars , n_obs ) <EOL> null_dist [ ... , i_i ] = np . corrcoef ( a_i ) <EOL> real_corr = np . corrcoef ( a ) <EOL> p_mat = np . zeros ( ( n_vars , n_vars ) ) <EOL> upper_tri = np . triu_indices ( n_vars , <NUM_LIT:1> ) <EOL> if corrected : <EOL> if tail == "<STR_LIT>" : <EOL> max_dist = np . abs ( null_dist [ upper_tri ] ) . max ( axis = <NUM_LIT:0> ) <EOL> elif tail == "<STR_LIT>" : <EOL> max_dist = null_dist [ upper_tri ] . min ( axis = <NUM_LIT:0> ) <EOL> elif tail == "<STR_LIT>" : <EOL> max_dist = null_dist [ upper_tri ] . max ( axis = <NUM_LIT:0> ) <EOL> cdf = lambda x : stats . percentileofscore ( max_dist , x ) / <NUM_LIT> <EOL> for i , j in zip ( * upper_tri ) : <EOL> observed = real_corr [ i , j ] <EOL> if tail == "<STR_LIT>" : <EOL> p_ij = <NUM_LIT:1> - cdf ( abs ( observed ) ) <EOL> elif tail == "<STR_LIT>" : <EOL> p_ij = cdf ( observed ) <EOL> elif tail == "<STR_LIT>" : <EOL> p_ij = <NUM_LIT:1> - cdf ( observed ) <EOL> p_mat [ i , j ] = p_ij <EOL> else : <EOL> for i , j in zip ( * upper_tri ) : <EOL> null_corrs = null_dist [ i , j ] <EOL> cdf = lambda x : stats . percentileofscore ( null_corrs , x ) / <NUM_LIT> <EOL> observed = real_corr [ i , j ] <EOL> if tail == "<STR_LIT>" : <EOL> p_ij = <NUM_LIT:2> * ( <NUM_LIT:1> - cdf ( abs ( observed ) ) ) <EOL> elif tail == "<STR_LIT>" : <EOL> p_ij = cdf ( observed ) <EOL> elif tail == "<STR_LIT>" : <EOL> p_ij = <NUM_LIT:1> - cdf ( observed ) <EOL> p_mat [ i , j ] = p_ij <EOL> p_mat += p_mat . T <EOL> p_mat [ np . diag_indices ( n_vars ) ] = np . nan <EOL> if return_dist : <EOL> return p_mat , null_dist <EOL> return p_mat </s>
