#NEWLINE# def _apply_rap_music( data , info , times , forward , noise_cov , n_dipoles = 2 , picks = None , return_explained_data = False ) : #NEWLINE# #INDENT# 'RAP-MUSIC for evoked data\n\n    Parameters\n    ----------\n    data : array, shape (n_channels, n_times)\n        Evoked data.\n    info : dict\n        Measurement info.\n    times : array\n        Times.\n    forward : instance of Forward\n        Forward operator.\n    noise_cov : instance of Covariance\n        The noise covariance.\n    n_dipoles : int\n        The number of dipoles to estimate. The default value is 2.\n    picks : array-like of int | None\n        Indices (in info) of data channels. If None, MEG and EEG data channels\n        (without bad channels) will be used.\n    return_explained_data : bool\n        If True, the explained data is returned as an array.\n\n    Returns\n    -------\n    dipoles : list of instances of Dipole\n        The dipole fits.\n    explained_data : array | None\n        Data explained by the dipoles using a least square fitting with the\n        selected active dipoles and their estimated orientation.\n        Computed only if return_explained_data is True.\n    ' #NEWLINE# ( is_free_ori , ch_names , proj , vertno , G ) = _prepare_beamformer_input ( info , forward , label = None , picks = picks , pick_ori = None ) #NEWLINE# gain = G . copy ( ) #NEWLINE# ( whitener , _ ) = compute_whitener ( noise_cov , info , picks ) #NEWLINE# if info [ 'projs' ] : #NEWLINE# #INDENT# whitener = np . dot ( whitener , proj ) #NEWLINE# #UNINDENT# G = np . dot ( whitener , G ) #NEWLINE# data = np . dot ( whitener , data ) #NEWLINE# ( eig_values , eig_vectors ) = linalg . eigh ( np . dot ( data , data . T ) ) #NEWLINE# phi_sig = eig_vectors [ : , ( - n_dipoles ) : ] #NEWLINE# n_orient = ( 3 if is_free_ori else 1 ) #NEWLINE# n_channels = G . shape [ 0 ] #NEWLINE# A = np . empty ( ( n_channels , n_dipoles ) ) #NEWLINE# gain_dip = np . empty ( ( n_channels , n_dipoles ) ) #NEWLINE# oris = np . empty ( ( n_dipoles , 3 ) ) #NEWLINE# poss = np . empty ( ( n_dipoles , 3 ) ) #NEWLINE# G_proj = G . copy ( ) #NEWLINE# phi_sig_proj = phi_sig . copy ( ) #NEWLINE# for k in range ( n_dipoles ) : #NEWLINE# #INDENT# subcorr_max = ( - 1.0 ) #NEWLINE# for i_source in range ( ( G . shape [ 1 ] // n_orient ) ) : #NEWLINE# #INDENT# idx_k = slice ( ( n_orient * i_source ) , ( n_orient * ( i_source + 1 ) ) ) #NEWLINE# Gk = G_proj [ : , idx_k ] #NEWLINE# if ( n_orient == 3 ) : #NEWLINE# #INDENT# Gk = np . dot ( Gk , source_idx [ 'source_nn' ] [ idx_k ] ) #NEWLINE# #UNINDENT# ( subcorr , ori ) = _compute_subcorr ( Gk , phi_sig_proj ) #NEWLINE# if ( subcorr > subcorr_max ) : #NEWLINE# #INDENT# subcorr_max = subcorr #NEWLINE# source_idx = i_source #NEWLINE# source_ori = ori #NEWLINE# if ( ( n_orient == 3 ) and ( source_ori [ ( - 1 ) ] < 0 ) ) : #NEWLINE# #INDENT# source_ori *= ( - 1 ) #NEWLINE# #UNINDENT# source_pos = forward [ 'source_rr' ] [ i_source ] #NEWLINE# if ( n_orient == 1 ) : #NEWLINE# #INDENT# source_ori = forward [ 'source_nn' ] [ i_source ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# idx_k = slice ( ( n_orient * source_idx ) , ( n_orient * ( source_idx + 1 ) ) ) #NEWLINE# Ak = G [ : , idx_k ] #NEWLINE# if ( n_orient == 3 ) : #NEWLINE# #INDENT# Ak = np . dot ( Ak , np . dot ( forward [ 'source_nn' ] [ idx_k ] , source_ori ) ) #NEWLINE# #UNINDENT# A [ : , k ] = Ak . ravel ( ) #NEWLINE# if return_explained_data : #NEWLINE# #INDENT# gain_k = gain [ : , idx_k ] #NEWLINE# if ( n_orient == 3 ) : #NEWLINE# #INDENT# gain_k = np . dot ( gain_k , np . dot ( forward [ 'source_nn' ] [ idx_k ] , source_ori ) ) #NEWLINE# #UNINDENT# gain_dip [ : , k ] = gain_k . ravel ( ) #NEWLINE# #UNINDENT# oris [ k ] = source_ori #NEWLINE# poss [ k ] = source_pos #NEWLINE# logger . info ( ( 'source %s found: p = %s' % ( ( k + 1 ) , source_idx ) ) ) #NEWLINE# if ( n_orient == 3 ) : #NEWLINE# #INDENT# logger . info ( ( 'ori = %s %s %s' % tuple ( oris [ k ] ) ) ) #NEWLINE# #UNINDENT# projection = _compute_proj ( A [ : , : ( k + 1 ) ] ) #NEWLINE# G_proj = np . dot ( projection , G ) #NEWLINE# phi_sig_proj = np . dot ( projection , phi_sig ) #NEWLINE# #UNINDENT# sol = linalg . lstsq ( A , data ) [ 0 ] #NEWLINE# ( gof , explained_data ) = ( [ ] , None ) #NEWLINE# if return_explained_data : #NEWLINE# #INDENT# explained_data = np . dot ( gain_dip , sol ) #NEWLINE# gof = ( linalg . norm ( np . dot ( whitener , explained_data ) ) / linalg . norm ( data ) ) #NEWLINE# #UNINDENT# return ( _make_dipoles ( times , poss , oris , sol , gof ) , explained_data )