#NEWLINE# def regularize( cov , info , mag = 0.1 , grad = 0.1 , eeg = 0.1 , exclude = 'bads' , proj = True , verbose = None ) : #NEWLINE# #INDENT# "Regularize noise covariance matrix.\n\n    This method works by adding a constant to the diagonal for each\n    channel type separately. Special care is taken to keep the\n    rank of the data constant.\n\n    **Note:** This function is kept for reasons of backward-compatibility.\n    Please consider explicitly using the ``method`` parameter in\n    `compute_covariance` to directly combine estimation with regularization\n    in a data-driven fashion see the\n    `faq <http://martinos.org/mne/dev/faq.html#how-should-i-regularize-the-covariance-matrix>`_\n    for more information.\n\n    Parameters\n    ----------\n    cov : Covariance\n        The noise covariance matrix.\n    info : dict\n        The measurement info (used to get channel types and bad channels).\n    mag : float (default 0.1)\n        Regularization factor for MEG magnetometers.\n    grad : float (default 0.1)\n        Regularization factor for MEG gradiometers.\n    eeg : float (default 0.1)\n        Regularization factor for EEG.\n    exclude : list | 'bads' (default 'bads')\n        List of channels to mark as bad. If 'bads', bads channels\n        are extracted from both info['bads'] and cov['bads'].\n    proj : bool (default true)\n        Apply or not projections to keep rank of data.\n    verbose : bool | str | int | None (default None)\n        If not None, override default verbose level (see mne.verbose).\n\n    Returns\n    -------\n    reg_cov : Covariance\n        The regularized covariance matrix.\n\n    See Also\n    --------\n    compute_covariance\n    " #NEWLINE# cov = cp . deepcopy ( cov ) #NEWLINE# info . _check_consistency ( ) #NEWLINE# if ( exclude is None ) : #NEWLINE# #INDENT# raise ValueError ( 'exclude must be a list of strings or "bads"' ) #NEWLINE# #UNINDENT# if ( exclude == 'bads' ) : #NEWLINE# #INDENT# exclude = ( info [ 'bads' ] + cov [ 'bads' ] ) #NEWLINE# #UNINDENT# sel_eeg = pick_types ( info , meg = False , eeg = True , ref_meg = False , exclude = exclude ) #NEWLINE# sel_mag = pick_types ( info , meg = 'mag' , eeg = False , ref_meg = False , exclude = exclude ) #NEWLINE# sel_grad = pick_types ( info , meg = 'grad' , eeg = False , ref_meg = False , exclude = exclude ) #NEWLINE# info_ch_names = info [ 'ch_names' ] #NEWLINE# ch_names_eeg = [ info_ch_names [ i ] for i in sel_eeg ] #NEWLINE# ch_names_mag = [ info_ch_names [ i ] for i in sel_mag ] #NEWLINE# ch_names_grad = [ info_ch_names [ i ] for i in sel_grad ] #NEWLINE# cov_good = pick_channels_cov ( cov , include = info_ch_names , exclude = exclude ) #NEWLINE# ch_names = cov_good . ch_names #NEWLINE# ( idx_eeg , idx_mag , idx_grad ) = ( [ ] , [ ] , [ ] ) #NEWLINE# for ( i , ch ) in enumerate ( ch_names ) : #NEWLINE# #INDENT# if ( ch in ch_names_eeg ) : #NEWLINE# #INDENT# idx_eeg . append ( i ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ch in ch_names_mag ) : #NEWLINE# #INDENT# idx_mag . append ( i ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ch in ch_names_grad ) : #NEWLINE# #INDENT# idx_grad . append ( i ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'channel is unknown type' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# C = cov_good [ 'data' ] #NEWLINE# assert ( len ( C ) == ( ( len ( idx_eeg ) + len ( idx_mag ) ) + len ( idx_grad ) ) ) #NEWLINE# if proj : #NEWLINE# #INDENT# projs = ( info [ 'projs' ] + cov_good [ 'projs' ] ) #NEWLINE# projs = activate_proj ( projs ) #NEWLINE# #UNINDENT# for ( desc , idx , reg ) in [ ( 'EEG' , idx_eeg , eeg ) , ( 'MAG' , idx_mag , mag ) , ( 'GRAD' , idx_grad , grad ) ] : #NEWLINE# #INDENT# if ( ( len ( idx ) == 0 ) or ( reg == 0.0 ) ) : #NEWLINE# #INDENT# logger . info ( ( '    %s regularization : None' % desc ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# logger . info ( ( '    %s regularization : %s' % ( desc , reg ) ) ) #NEWLINE# this_C = C [ np . ix_ ( idx , idx ) ] #NEWLINE# if proj : #NEWLINE# #INDENT# this_ch_names = [ ch_names [ k ] for k in idx ] #NEWLINE# ( P , ncomp , _ ) = make_projector ( projs , this_ch_names ) #NEWLINE# U = linalg . svd ( P ) [ 0 ] [ : , : ( - ncomp ) ] #NEWLINE# if ( ncomp > 0 ) : #NEWLINE# #INDENT# logger . info ( ( '    Created an SSP operator for %s (dimension = %d)' % ( desc , ncomp ) ) ) #NEWLINE# this_C = np . dot ( U . T , np . dot ( this_C , U ) ) #NEWLINE# #UNINDENT# #UNINDENT# sigma = np . mean ( np . diag ( this_C ) ) #NEWLINE# this_C . flat [ : : ( len ( this_C ) + 1 ) ] += ( reg * sigma ) #NEWLINE# if ( proj and ( ncomp > 0 ) ) : #NEWLINE# #INDENT# this_C = np . dot ( U , np . dot ( this_C , U . T ) ) #NEWLINE# #UNINDENT# C [ np . ix_ ( idx , idx ) ] = this_C #NEWLINE# #UNINDENT# idx = pick_channels ( cov . ch_names , info_ch_names , exclude = exclude ) #NEWLINE# cov [ 'data' ] [ np . ix_ ( idx , idx ) ] = C #NEWLINE# return cov