#NEWLINE# def _fit_Q( fwd_data , whitener , proj_op , B , B2 , B_orig , rd , ori = None ) : #NEWLINE# #INDENT# 'Fit the dipole moment once the location is known' #NEWLINE# if ( 'fwd' in fwd_data ) : #NEWLINE# #INDENT# assert ( rd is None ) #NEWLINE# fwd = fwd_data [ 'fwd' ] #NEWLINE# assert ( fwd . shape [ 0 ] == 3 ) #NEWLINE# fwd_orig = fwd_data [ 'fwd_orig' ] #NEWLINE# assert ( fwd_orig . shape [ 0 ] == 3 ) #NEWLINE# scales = fwd_data [ 'scales' ] #NEWLINE# assert ( scales . shape == ( 3 , ) ) #NEWLINE# fwd_svd = fwd_data [ 'fwd_svd' ] [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( fwd , fwd_orig , scales ) = _dipole_forwards ( fwd_data , whitener , rd [ np . newaxis , : ] ) #NEWLINE# fwd_svd = None #NEWLINE# #UNINDENT# if ( ori is None ) : #NEWLINE# #INDENT# if ( fwd_svd is None ) : #NEWLINE# #INDENT# fwd_svd = linalg . svd ( fwd , full_matrices = False ) #NEWLINE# #UNINDENT# ( uu , sing , vv ) = fwd_svd #NEWLINE# ( gof , one ) = _dipole_gof ( uu , sing , vv , B , B2 ) #NEWLINE# ncomp = len ( one ) #NEWLINE# Q = ( scales [ 0 ] * np . sum ( ( uu . T [ : ncomp ] * ( one / sing [ : ncomp ] ) [ : , np . newaxis ] ) , axis = 0 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# fwd = np . dot ( ori [ np . newaxis ] , fwd ) #NEWLINE# sing = np . linalg . norm ( fwd ) #NEWLINE# one = np . dot ( ( fwd / sing ) , B ) #NEWLINE# gof = ( ( one * one ) [ 0 ] / B2 ) #NEWLINE# Q = ( ori * ( scales [ 0 ] * np . sum ( ( one / sing ) ) ) ) #NEWLINE# #UNINDENT# B_residual = _compute_residual ( proj_op , B_orig , fwd_orig , Q ) #NEWLINE# return ( Q , rd , B_residual )