<s> import numpy as np <EOL> from . constants import FIFF <EOL> def get_current_comp ( info ) : <EOL> """<STR_LIT>""" <EOL> comp = None <EOL> first_comp = - <NUM_LIT:1> <EOL> for k , chan in enumerate ( info [ '<STR_LIT>' ] ) : <EOL> if chan [ '<STR_LIT>' ] == FIFF . FIFFV_MEG_CH : <EOL> comp = int ( chan [ '<STR_LIT>' ] ) >> <NUM_LIT:16> <EOL> if first_comp < <NUM_LIT:0> : <EOL> first_comp = comp <EOL> elif comp != first_comp : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return comp <EOL> def set_current_comp ( info , comp ) : <EOL> """<STR_LIT>""" <EOL> comp_now = get_current_comp ( info ) <EOL> for k , chan in enumerate ( info [ '<STR_LIT>' ] ) : <EOL> if chan [ '<STR_LIT>' ] == FIFF . FIFFV_MEG_CH : <EOL> rem = chan [ '<STR_LIT>' ] - ( comp_now << <NUM_LIT:16> ) <EOL> chan [ '<STR_LIT>' ] = int ( rem + ( comp << <NUM_LIT:16> ) ) <EOL> def _make_compensator ( info , kind ) : <EOL> """<STR_LIT>""" <EOL> for k in range ( len ( info [ '<STR_LIT>' ] ) ) : <EOL> if info [ '<STR_LIT>' ] [ k ] [ '<STR_LIT>' ] == kind : <EOL> this_data = info [ '<STR_LIT>' ] [ k ] [ '<STR_LIT:data>' ] <EOL> presel = np . zeros ( ( this_data [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] ) ) <EOL> for col , col_name in enumerate ( this_data [ '<STR_LIT>' ] ) : <EOL> ind = [ k for k , ch in enumerate ( info [ '<STR_LIT>' ] ) <EOL> if ch == col_name ] <EOL> if len ( ind ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT:data>' % col_name ) <EOL> elif len ( ind ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' % col_name ) <EOL> presel [ col , ind [ <NUM_LIT:0> ] ] = <NUM_LIT:1.0> <EOL> postsel = np . zeros ( ( info [ '<STR_LIT>' ] , this_data [ '<STR_LIT>' ] ) ) <EOL> for c , ch_name in enumerate ( info [ '<STR_LIT>' ] ) : <EOL> ind = [ k for k , ch in enumerate ( this_data [ '<STR_LIT>' ] ) <EOL> if ch == ch_name ] <EOL> if len ( ind ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' % ch_name ) <EOL> elif len ( ind ) == <NUM_LIT:1> : <EOL> postsel [ c , ind [ <NUM_LIT:0> ] ] = <NUM_LIT:1.0> <EOL> this_comp = np . dot ( postsel , np . dot ( this_data [ '<STR_LIT:data>' ] , presel ) ) <EOL> return this_comp <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % kind ) <EOL> def make_compensator ( info , from_ , to , exclude_comp_chs = False ) : <EOL> """<STR_LIT>""" <EOL> if from_ == to : <EOL> return None <EOL> if from_ == <NUM_LIT:0> : <EOL> C1 = np . zeros ( ( info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> C1 = _make_compensator ( info , from_ ) <EOL> if to == <NUM_LIT:0> : <EOL> C2 = np . zeros ( ( info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> C2 = _make_compensator ( info , to ) <EOL> comp = np . eye ( info [ '<STR_LIT>' ] ) + C1 - C2 - np . dot ( C2 , C1 ) <EOL> if exclude_comp_chs : <EOL> pick = [ k for k , c in enumerate ( info [ '<STR_LIT>' ] ) <EOL> if c [ '<STR_LIT>' ] != FIFF . FIFFV_REF_MEG_CH ] <EOL> if len ( pick ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> comp = comp [ pick , : ] <EOL> return comp </s>
