#NEWLINE# def _set_montage( info , montage , update_ch_names = False ) : #NEWLINE# #INDENT# 'Apply montage to data.\n\n    With a Montage, this function will replace the EEG channel names and\n    locations with the values specified for the particular montage.\n\n    With a DigMontage, this function will replace the digitizer info with\n    the values specified for the particular montage.\n\n    Usually, a montage is expected to contain the positions of all EEG\n    electrodes and a warning is raised when this is not the case.\n\n    Parameters\n    ----------\n    info : instance of Info\n        The measurement info to update.\n    montage : instance of Montage | instance of DigMontage\n        The montage to apply.\n    update_ch_names : bool\n        If True, overwrite the info channel names with the ones from montage.\n\n    Notes\n    -----\n    This function will change the info variable in place.\n    ' #NEWLINE# if isinstance ( montage , Montage ) : #NEWLINE# #INDENT# if update_ch_names : #NEWLINE# #INDENT# info [ 'chs' ] = list ( ) #NEWLINE# for ( ii , ch_name ) in enumerate ( montage . ch_names ) : #NEWLINE# #INDENT# ch_info = { #NEWLINE# #INDENT# 'cal' : 1.0 , #NEWLINE# 'logno' : ( ii + 1 ) , #NEWLINE# 'scanno' : ( ii + 1 ) , #NEWLINE# 'range' : 1.0 , #NEWLINE# 'unit_mul' : 0 , #NEWLINE# 'ch_name' : ch_name , #NEWLINE# 'unit' : FIFF . FIFF_UNIT_V , #NEWLINE# 'kind' : FIFF . FIFFV_EEG_CH , #NEWLINE# 'coord_frame' : FIFF . FIFFV_COORD_HEAD , #NEWLINE# 'coil_type' : FIFF . FIFFV_COIL_EEG , #NEWLINE# #UNINDENT# } #NEWLINE# info [ 'chs' ] . append ( ch_info ) #NEWLINE# #UNINDENT# info . _update_redundant ( ) #NEWLINE# #UNINDENT# if ( not _contains_ch_type ( info , 'eeg' ) ) : #NEWLINE# #INDENT# raise ValueError ( 'No EEG channels found.' ) #NEWLINE# #UNINDENT# sensors_found = [ ] #NEWLINE# for ( pos , ch_name ) in zip ( montage . pos , montage . ch_names ) : #NEWLINE# #INDENT# if ( ch_name not in info [ 'ch_names' ] ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# ch_idx = info [ 'ch_names' ] . index ( ch_name ) #NEWLINE# info [ 'chs' ] [ ch_idx ] [ 'loc' ] = np . r_ [ ( pos , ( [ 0.0 ] * 9 ) ) ] #NEWLINE# sensors_found . append ( ch_idx ) #NEWLINE# #UNINDENT# if ( len ( sensors_found ) == 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'None of the sensors defined in the montage were found in the info structure. Check the channel names.' ) #NEWLINE# #UNINDENT# eeg_sensors = pick_types ( info , meg = False , ref_meg = False , eeg = True , exclude = [ ] ) #NEWLINE# not_found = np . setdiff1d ( eeg_sensors , sensors_found ) #NEWLINE# if ( len ( not_found ) > 0 ) : #NEWLINE# #INDENT# not_found_names = [ info [ 'ch_names' ] [ ch ] for ch in not_found ] #NEWLINE# warn ( ( ( 'The following EEG sensors did not have a position specified in the selected montage: ' + str ( not_found_names ) ) + '. Their position has been left untouched.' ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( montage , DigMontage ) : #NEWLINE# #INDENT# dig = _make_dig_points ( nasion = montage . nasion , lpa = montage . lpa , rpa = montage . rpa , hpi = montage . hpi , dig_points = montage . hsp , dig_ch_pos = montage . dig_ch_pos ) #NEWLINE# info [ 'dig' ] = dig #NEWLINE# info [ 'dev_head_t' ] [ 'trans' ] = montage . dev_head_t #NEWLINE# if ( montage . dig_ch_pos is not None ) : #NEWLINE# #INDENT# eeg_ref_pos = montage . dig_ch_pos . get ( 'EEG000' , np . zeros ( 3 ) ) #NEWLINE# did_set = np . zeros ( len ( info [ 'ch_names' ] ) , bool ) #NEWLINE# is_eeg = np . zeros ( len ( info [ 'ch_names' ] ) , bool ) #NEWLINE# is_eeg [ pick_types ( info , meg = False , eeg = True , exclude = ( ) ) ] = True #NEWLINE# for ( ch_name , ch_pos ) in montage . dig_ch_pos . items ( ) : #NEWLINE# #INDENT# if ( ch_name == 'EEG000' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( ch_name not in info [ 'ch_names' ] ) : #NEWLINE# #INDENT# raise RuntimeError ( ( 'Montage channel %s not found in info' % ch_name ) ) #NEWLINE# #UNINDENT# idx = info [ 'ch_names' ] . index ( ch_name ) #NEWLINE# did_set [ idx ] = True #NEWLINE# this_loc = np . concatenate ( ( ch_pos , eeg_ref_pos ) ) #NEWLINE# info [ 'chs' ] [ idx ] [ 'loc' ] [ : 6 ] = this_loc #NEWLINE# #UNINDENT# did_not_set = [ info [ 'chs' ] [ ii ] [ 'ch_name' ] for ii in np . where ( ( is_eeg & ( ~ did_set ) ) ) [ 0 ] ] #NEWLINE# if ( len ( did_not_set ) > 0 ) : #NEWLINE# #INDENT# warn ( ( 'Did not set %s channel positions:\n%s' % ( len ( did_not_set ) , ', ' . join ( did_not_set ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( "Montage must be a 'Montage' or 'DigMontage' instead of '%s'." % type ( montage ) ) )