<s> """<STR_LIT>""" <EOL> from operator import itemgetter <EOL> import numpy as np <EOL> from scipy . stats import scoreatpercentile <EOL> from sklearn . decomposition import fastica <EOL> from sklearn . externals . joblib import Memory , delayed , Parallel <EOL> from sklearn . utils import check_random_state <EOL> from . multi_pca import MultiPCA <EOL> class CanICA ( MultiPCA ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mask = None , n_components = <NUM_LIT:20> , smoothing_fwhm = <NUM_LIT:6> , <EOL> do_cca = True , <EOL> threshold = '<STR_LIT>' , <EOL> n_init = <NUM_LIT:10> , <EOL> random_state = None , <EOL> standardize = True , detrend = True , <EOL> low_pass = None , high_pass = None , t_r = None , <EOL> target_affine = None , target_shape = None , <EOL> mask_strategy = '<STR_LIT>' , mask_args = None , <EOL> memory = Memory ( cachedir = None ) , memory_level = <NUM_LIT:0> , <EOL> n_jobs = <NUM_LIT:1> , verbose = <NUM_LIT:0> <EOL> ) : <EOL> super ( CanICA , self ) . __init__ ( <EOL> n_components = n_components , <EOL> do_cca = do_cca , <EOL> random_state = random_state , <EOL> mask = mask , smoothing_fwhm = smoothing_fwhm , <EOL> standardize = standardize , detrend = detrend , <EOL> low_pass = low_pass , high_pass = high_pass , t_r = t_r , <EOL> target_affine = target_affine , target_shape = target_shape , <EOL> mask_strategy = mask_strategy , mask_args = mask_args , <EOL> memory = memory , memory_level = memory_level , <EOL> n_jobs = n_jobs , verbose = verbose ) <EOL> self . threshold = threshold <EOL> self . n_init = n_init <EOL> def _unmix_components ( self ) : <EOL> """<STR_LIT>""" <EOL> random_state = check_random_state ( self . random_state ) <EOL> seeds = random_state . randint ( np . iinfo ( np . int32 ) . max , size = self . n_init ) <EOL> results = Parallel ( n_jobs = self . n_jobs , verbose = self . verbose ) ( <EOL> delayed ( self . _cache ( fastica , func_memory_level = <NUM_LIT:2> ) ) <EOL> ( self . components_ . T , whiten = True , fun = '<STR_LIT>' , <EOL> random_state = seed ) <EOL> for seed in seeds ) <EOL> ica_maps_gen_ = ( result [ <NUM_LIT:2> ] . T for result in results ) <EOL> ica_maps_and_sparsities = ( ( ica_map , <EOL> np . sum ( np . abs ( ica_map ) , axis = <NUM_LIT:1> ) . max ( ) ) <EOL> for ica_map in ica_maps_gen_ ) <EOL> ica_maps , _ = min ( ica_maps_and_sparsities , key = itemgetter ( - <NUM_LIT:1> ) ) <EOL> ratio = None <EOL> if isinstance ( self . threshold , float ) : <EOL> ratio = self . threshold <EOL> elif self . threshold == '<STR_LIT>' : <EOL> ratio = <NUM_LIT:1.> <EOL> elif self . threshold is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> str ( self . threshold ) ) <EOL> if ratio is not None : <EOL> abs_ica_maps = np . abs ( ica_maps ) <EOL> threshold = scoreatpercentile ( <EOL> abs_ica_maps , <EOL> <NUM_LIT> - ( <NUM_LIT> / len ( ica_maps ) ) * ratio ) <EOL> ica_maps [ abs_ica_maps < threshold ] = <NUM_LIT:0.> <EOL> self . components_ = ica_maps <EOL> for component in self . components_ : <EOL> if component . max ( ) < - component . min ( ) : <EOL> component *= - <NUM_LIT:1> <EOL> def _raw_fit ( self , data ) : <EOL> """<STR_LIT>""" <EOL> MultiPCA . _raw_fit ( self , data ) <EOL> self . _unmix_components ( ) <EOL> return self </s>
