<s> from copy import deepcopy <EOL> from datetime import datetime as dt <EOL> import os . path as op <EOL> import numpy as np <EOL> from scipy import linalg <EOL> from . pick import channel_type <EOL> from . constants import FIFF <EOL> from . open import fiff_open <EOL> from . tree import dir_tree_find <EOL> from . tag import read_tag , find_tag <EOL> from . proj import _read_proj , _write_proj , _uniquify_projs , _normalize_proj <EOL> from . ctf_comp import read_ctf_comp , write_ctf_comp <EOL> from . write import ( start_file , end_file , start_block , end_block , <EOL> write_string , write_dig_point , write_float , write_int , <EOL> write_coord_trans , write_ch_info , write_name_list , <EOL> write_julian , write_float_matrix ) <EOL> from . proc_history import _read_proc_history , _write_proc_history <EOL> from . . utils import logger , verbose , warn <EOL> from . . fixes import Counter <EOL> from . . import __version__ <EOL> from . . externals . six import b , BytesIO , string_types , text_type <EOL> _kind_dict = dict ( <EOL> eeg = ( FIFF . FIFFV_EEG_CH , FIFF . FIFFV_COIL_EEG , FIFF . FIFF_UNIT_V ) , <EOL> mag = ( FIFF . FIFFV_MEG_CH , FIFF . FIFFV_COIL_VV_MAG_T3 , FIFF . FIFF_UNIT_T ) , <EOL> grad = ( FIFF . FIFFV_MEG_CH , FIFF . FIFFV_COIL_VV_PLANAR_T1 , FIFF . FIFF_UNIT_T_M ) , <EOL> ref_meg = ( FIFF . FIFFV_REF_MEG_CH , FIFF . FIFFV_COIL_VV_MAG_T3 , <EOL> FIFF . FIFF_UNIT_T ) , <EOL> misc = ( FIFF . FIFFV_MISC_CH , FIFF . FIFFV_COIL_NONE , FIFF . FIFF_UNIT_NONE ) , <EOL> stim = ( FIFF . FIFFV_STIM_CH , FIFF . FIFFV_COIL_NONE , FIFF . FIFF_UNIT_V ) , <EOL> eog = ( FIFF . FIFFV_EOG_CH , FIFF . FIFFV_COIL_NONE , FIFF . FIFF_UNIT_V ) , <EOL> ecg = ( FIFF . FIFFV_ECG_CH , FIFF . FIFFV_COIL_NONE , FIFF . FIFF_UNIT_V ) , <EOL> seeg = ( FIFF . FIFFV_SEEG_CH , FIFF . FIFFV_COIL_EEG , FIFF . FIFF_UNIT_V ) , <EOL> bio = ( FIFF . FIFFV_BIO_CH , FIFF . FIFFV_COIL_NONE , FIFF . FIFF_UNIT_V ) , <EOL> ecog = ( FIFF . FIFFV_ECOG_CH , FIFF . FIFFV_COIL_EEG , FIFF . FIFF_UNIT_V ) , <EOL> ) <EOL> def _summarize_str ( st ) : <EOL> """<STR_LIT>""" <EOL> return st [ : <NUM_LIT> ] [ : : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] [ : : - <NUM_LIT:1> ] + '<STR_LIT>' <EOL> class Info ( dict ) : <EOL> """<STR_LIT>""" <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return Info ( deepcopy ( self ) ) <EOL> def normalize_proj ( self ) : <EOL> """<STR_LIT>""" <EOL> _normalize_proj ( self ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> strs = [ '<STR_LIT>' ] <EOL> non_empty = <NUM_LIT:0> <EOL> for k , v in self . items ( ) : <EOL> if k in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> entr = ( '<STR_LIT:U+002CU+0020>' . join ( b for ii , b in enumerate ( v ) if ii < <NUM_LIT:10> ) <EOL> if v else '<STR_LIT>' ) <EOL> if len ( entr ) >= <NUM_LIT> : <EOL> entr = _summarize_str ( entr ) <EOL> elif k == '<STR_LIT:filename>' and v : <EOL> path , fname = op . split ( v ) <EOL> entr = path [ : <NUM_LIT:10> ] + '<STR_LIT>' + fname <EOL> elif k == '<STR_LIT>' and v : <EOL> entr = '<STR_LIT:U+002CU+0020>' . join ( p [ '<STR_LIT>' ] + '<STR_LIT>' % <EOL> { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT:n>' } [ p [ '<STR_LIT>' ] ] for p in v ) <EOL> if len ( entr ) >= <NUM_LIT> : <EOL> entr = _summarize_str ( entr ) <EOL> elif k == '<STR_LIT>' and np . iterable ( v ) : <EOL> entr = dt . fromtimestamp ( v [ <NUM_LIT:0> ] ) . strftime ( '<STR_LIT>' ) <EOL> else : <EOL> this_len = ( len ( v ) if hasattr ( v , '<STR_LIT>' ) else <EOL> ( '<STR_LIT:%s>' % v if v is not None else None ) ) <EOL> entr = ( ( '<STR_LIT>' % this_len ) if isinstance ( this_len , int ) <EOL> else ( '<STR_LIT:%s>' % this_len if this_len else '<STR_LIT>' ) ) <EOL> if entr : <EOL> non_empty += <NUM_LIT:1> <EOL> entr = '<STR_LIT>' + entr <EOL> if k == '<STR_LIT>' : <EOL> ch_types = [ channel_type ( self , idx ) for idx in range ( len ( v ) ) ] <EOL> ch_counts = Counter ( ch_types ) <EOL> entr += "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT>" % ( ch_type . upper ( ) , count ) <EOL> for ch_type , count <EOL> in ch_counts . items ( ) ) <EOL> strs . append ( '<STR_LIT>' % ( k , str ( type ( v ) ) [ <NUM_LIT:7> : - <NUM_LIT:2> ] , entr ) ) <EOL> if k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> strs [ - <NUM_LIT:1> ] += '<STR_LIT>' <EOL> strs_non_empty = sorted ( s for s in strs if '<STR_LIT:|>' in s ) <EOL> strs_empty = sorted ( s for s in strs if '<STR_LIT:|>' not in s ) <EOL> st = '<STR_LIT>' . join ( strs_non_empty + strs_empty ) <EOL> st += '<STR_LIT>' <EOL> st %= non_empty <EOL> return st <EOL> def _anonymize ( self ) : <EOL> if self . get ( '<STR_LIT>' ) is not None : <EOL> del self [ '<STR_LIT>' ] <EOL> self [ '<STR_LIT>' ] = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for key_1 in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for key_2 in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self [ key_1 ] [ key_2 ] = <NUM_LIT:0> <EOL> def _check_consistency ( self ) : <EOL> """<STR_LIT>""" <EOL> missing = [ bad for bad in self [ '<STR_LIT>' ] if bad not in self [ '<STR_LIT>' ] ] <EOL> if len ( missing ) > <NUM_LIT:0> : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> % ( missing , ) ) <EOL> chs = [ ch [ '<STR_LIT>' ] for ch in self [ '<STR_LIT>' ] ] <EOL> if len ( self [ '<STR_LIT>' ] ) != len ( chs ) or any ( <EOL> ch_1 != ch_2 for ch_1 , ch_2 in zip ( self [ '<STR_LIT>' ] , chs ) ) or self [ '<STR_LIT>' ] != len ( chs ) : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if self . get ( key ) is not None : <EOL> self [ key ] = float ( self [ key ] ) <EOL> unique_ids = np . unique ( self [ '<STR_LIT>' ] , return_index = True ) [ <NUM_LIT:1> ] <EOL> if len ( unique_ids ) != self [ '<STR_LIT>' ] : <EOL> dups = set ( self [ '<STR_LIT>' ] [ x ] <EOL> for x in np . setdiff1d ( range ( self [ '<STR_LIT>' ] ) , unique_ids ) ) <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % dups ) <EOL> def _update_redundant ( self ) : <EOL> """<STR_LIT>""" <EOL> self [ '<STR_LIT>' ] = [ ch [ '<STR_LIT>' ] for ch in self [ '<STR_LIT>' ] ] <EOL> self [ '<STR_LIT>' ] = len ( self [ '<STR_LIT>' ] ) <EOL> def read_fiducials ( fname ) : <EOL> """<STR_LIT>""" <EOL> fid , tree , _ = fiff_open ( fname ) <EOL> with fid : <EOL> isotrak = dir_tree_find ( tree , FIFF . FIFFB_ISOTRAK ) <EOL> isotrak = isotrak [ <NUM_LIT:0> ] <EOL> pts = [ ] <EOL> coord_frame = FIFF . FIFFV_COORD_UNKNOWN <EOL> for k in range ( isotrak [ '<STR_LIT>' ] ) : <EOL> kind = isotrak [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = isotrak [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_DIG_POINT : <EOL> tag = read_tag ( fid , pos ) <EOL> pts . append ( tag . data ) <EOL> elif kind == FIFF . FIFF_MNE_COORD_FRAME : <EOL> tag = read_tag ( fid , pos ) <EOL> coord_frame = tag . data [ <NUM_LIT:0> ] <EOL> if coord_frame == FIFF . FIFFV_COORD_UNKNOWN : <EOL> err = ( "<STR_LIT>" <EOL> "<STR_LIT>" % fname ) <EOL> raise ValueError ( err ) <EOL> for pt in pts : <EOL> pt [ '<STR_LIT>' ] = coord_frame <EOL> return pts , coord_frame <EOL> def write_fiducials ( fname , pts , coord_frame = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> pts_frames = set ( ( pt . get ( '<STR_LIT>' , coord_frame ) for pt in pts ) ) <EOL> bad_frames = pts_frames - set ( ( coord_frame , ) ) <EOL> if len ( bad_frames ) > <NUM_LIT:0> : <EOL> err = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( coord_frame , str ( tuple ( bad_frames ) ) ) ) <EOL> raise ValueError ( err ) <EOL> fid = start_file ( fname ) <EOL> start_block ( fid , FIFF . FIFFB_ISOTRAK ) <EOL> write_int ( fid , FIFF . FIFF_MNE_COORD_FRAME , coord_frame ) <EOL> for pt in pts : <EOL> write_dig_point ( fid , pt ) <EOL> end_block ( fid , FIFF . FIFFB_ISOTRAK ) <EOL> end_file ( fid ) <EOL> def _read_dig_fif ( fid , meas_info ) : <EOL> """<STR_LIT>""" <EOL> isotrak = dir_tree_find ( meas_info , FIFF . FIFFB_ISOTRAK ) <EOL> dig = None <EOL> if len ( isotrak ) == <NUM_LIT:0> : <EOL> logger . info ( '<STR_LIT>' ) <EOL> elif len ( isotrak ) > <NUM_LIT:1> : <EOL> warn ( '<STR_LIT>' ) <EOL> else : <EOL> isotrak = isotrak [ <NUM_LIT:0> ] <EOL> dig = [ ] <EOL> for k in range ( isotrak [ '<STR_LIT>' ] ) : <EOL> kind = isotrak [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = isotrak [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_DIG_POINT : <EOL> tag = read_tag ( fid , pos ) <EOL> dig . append ( tag . data ) <EOL> dig [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] = FIFF . FIFFV_COORD_HEAD <EOL> return dig <EOL> def _read_dig_points ( fname , comments = '<STR_LIT:%>' ) : <EOL> """<STR_LIT>""" <EOL> dig_points = np . loadtxt ( fname , comments = comments , ndmin = <NUM_LIT:2> ) <EOL> if dig_points . shape [ - <NUM_LIT:1> ] != <NUM_LIT:3> : <EOL> err = '<STR_LIT>' % ( dig_points . shape , ) <EOL> raise ValueError ( err ) <EOL> return dig_points <EOL> def _write_dig_points ( fname , dig_points ) : <EOL> """<STR_LIT>""" <EOL> _ , ext = op . splitext ( fname ) <EOL> dig_points = np . asarray ( dig_points ) <EOL> if ( dig_points . ndim != <NUM_LIT:2> ) or ( dig_points . shape [ <NUM_LIT:1> ] != <NUM_LIT:3> ) : <EOL> err = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( dig_points . shape , ) ) <EOL> raise ValueError ( err ) <EOL> if ext == '<STR_LIT>' : <EOL> with open ( fname , '<STR_LIT:wb>' ) as fid : <EOL> version = __version__ <EOL> now = dt . now ( ) . strftime ( "<STR_LIT>" ) <EOL> fid . write ( b ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( version = version , <EOL> now = now ) ) ) <EOL> fid . write ( b ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( N = len ( dig_points ) ) ) ) <EOL> np . savetxt ( fid , dig_points , delimiter = '<STR_LIT:\t>' , newline = '<STR_LIT:\n>' ) <EOL> else : <EOL> msg = "<STR_LIT>" % ext <EOL> raise ValueError ( msg ) <EOL> def _make_dig_points ( nasion = None , lpa = None , rpa = None , hpi = None , <EOL> dig_points = None , dig_ch_pos = None ) : <EOL> """<STR_LIT>""" <EOL> dig = [ ] <EOL> if lpa is not None : <EOL> lpa = np . asarray ( lpa ) <EOL> if lpa . shape == ( <NUM_LIT:3> , ) : <EOL> dig . append ( { '<STR_LIT:r>' : lpa , '<STR_LIT>' : FIFF . FIFFV_POINT_LPA , <EOL> '<STR_LIT>' : FIFF . FIFFV_POINT_CARDINAL , <EOL> '<STR_LIT>' : FIFF . FIFFV_COORD_HEAD } ) <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> % ( lpa . shape , ) ) <EOL> raise ValueError ( msg ) <EOL> if nasion is not None : <EOL> nasion = np . asarray ( nasion ) <EOL> if nasion . shape == ( <NUM_LIT:3> , ) : <EOL> dig . append ( { '<STR_LIT:r>' : nasion , '<STR_LIT>' : FIFF . FIFFV_POINT_NASION , <EOL> '<STR_LIT>' : FIFF . FIFFV_POINT_CARDINAL , <EOL> '<STR_LIT>' : FIFF . FIFFV_COORD_HEAD } ) <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> % ( nasion . shape , ) ) <EOL> raise ValueError ( msg ) <EOL> if rpa is not None : <EOL> rpa = np . asarray ( rpa ) <EOL> if rpa . shape == ( <NUM_LIT:3> , ) : <EOL> dig . append ( { '<STR_LIT:r>' : rpa , '<STR_LIT>' : FIFF . FIFFV_POINT_RPA , <EOL> '<STR_LIT>' : FIFF . FIFFV_POINT_CARDINAL , <EOL> '<STR_LIT>' : FIFF . FIFFV_COORD_HEAD } ) <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> % ( rpa . shape , ) ) <EOL> raise ValueError ( msg ) <EOL> if hpi is not None : <EOL> hpi = np . asarray ( hpi ) <EOL> if hpi . shape [ <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> for idx , point in enumerate ( hpi ) : <EOL> dig . append ( { '<STR_LIT:r>' : point , '<STR_LIT>' : idx + <NUM_LIT:1> , <EOL> '<STR_LIT>' : FIFF . FIFFV_POINT_HPI , <EOL> '<STR_LIT>' : FIFF . FIFFV_COORD_HEAD } ) <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT:%s>' % ( hpi . shape , ) ) <EOL> raise ValueError ( msg ) <EOL> if dig_points is not None : <EOL> dig_points = np . asarray ( dig_points ) <EOL> if dig_points . shape [ <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> for idx , point in enumerate ( dig_points ) : <EOL> dig . append ( { '<STR_LIT:r>' : point , '<STR_LIT>' : idx + <NUM_LIT:1> , <EOL> '<STR_LIT>' : FIFF . FIFFV_POINT_EXTRA , <EOL> '<STR_LIT>' : FIFF . FIFFV_COORD_HEAD } ) <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT:%s>' % ( dig_points . shape , ) ) <EOL> raise ValueError ( msg ) <EOL> if dig_ch_pos is not None : <EOL> keys = sorted ( dig_ch_pos . keys ( ) ) <EOL> for key in keys : <EOL> dig . append ( { '<STR_LIT:r>' : dig_ch_pos [ key ] , '<STR_LIT>' : int ( key [ - <NUM_LIT:3> : ] ) , <EOL> '<STR_LIT>' : FIFF . FIFFV_POINT_EEG , <EOL> '<STR_LIT>' : FIFF . FIFFV_COORD_HEAD } ) <EOL> return dig <EOL> @ verbose <EOL> def read_info ( fname , verbose = None ) : <EOL> """<STR_LIT>""" <EOL> f , tree , _ = fiff_open ( fname ) <EOL> with f as fid : <EOL> info = read_meas_info ( fid , tree ) [ <NUM_LIT:0> ] <EOL> return info <EOL> def read_bad_channels ( fid , node ) : <EOL> """<STR_LIT>""" <EOL> nodes = dir_tree_find ( node , FIFF . FIFFB_MNE_BAD_CHANNELS ) <EOL> bads = [ ] <EOL> if len ( nodes ) > <NUM_LIT:0> : <EOL> for node in nodes : <EOL> tag = find_tag ( fid , node , FIFF . FIFF_MNE_CH_NAME_LIST ) <EOL> if tag is not None and tag . data is not None : <EOL> bads = tag . data . split ( '<STR_LIT::>' ) <EOL> return bads <EOL> @ verbose <EOL> def read_meas_info ( fid , tree , clean_bads = False , verbose = None ) : <EOL> """<STR_LIT>""" <EOL> meas = dir_tree_find ( tree , FIFF . FIFFB_MEAS ) <EOL> if len ( meas ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( meas ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> meas = meas [ <NUM_LIT:0> ] <EOL> meas_info = dir_tree_find ( meas , FIFF . FIFFB_MEAS_INFO ) <EOL> if len ( meas_info ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( meas_info ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> meas_info = meas_info [ <NUM_LIT:0> ] <EOL> dev_head_t = None <EOL> ctf_head_t = None <EOL> dev_ctf_t = None <EOL> meas_date = None <EOL> highpass = None <EOL> lowpass = None <EOL> nchan = None <EOL> sfreq = None <EOL> chs = [ ] <EOL> experimenter = None <EOL> description = None <EOL> proj_id = None <EOL> proj_name = None <EOL> line_freq = None <EOL> custom_ref_applied = False <EOL> xplotter_layout = None <EOL> for k in range ( meas_info [ '<STR_LIT>' ] ) : <EOL> kind = meas_info [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = meas_info [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_NCHAN : <EOL> tag = read_tag ( fid , pos ) <EOL> nchan = int ( tag . data ) <EOL> elif kind == FIFF . FIFF_SFREQ : <EOL> tag = read_tag ( fid , pos ) <EOL> sfreq = float ( tag . data ) <EOL> elif kind == FIFF . FIFF_CH_INFO : <EOL> tag = read_tag ( fid , pos ) <EOL> chs . append ( tag . data ) <EOL> elif kind == FIFF . FIFF_LOWPASS : <EOL> tag = read_tag ( fid , pos ) <EOL> lowpass = float ( tag . data ) <EOL> elif kind == FIFF . FIFF_HIGHPASS : <EOL> tag = read_tag ( fid , pos ) <EOL> highpass = float ( tag . data ) <EOL> elif kind == FIFF . FIFF_MEAS_DATE : <EOL> tag = read_tag ( fid , pos ) <EOL> meas_date = tag . data <EOL> elif kind == FIFF . FIFF_COORD_TRANS : <EOL> tag = read_tag ( fid , pos ) <EOL> cand = tag . data <EOL> if cand [ '<STR_LIT>' ] == FIFF . FIFFV_COORD_DEVICE and cand [ '<STR_LIT:to>' ] == FIFF . FIFFV_COORD_HEAD : <EOL> dev_head_t = cand <EOL> elif cand [ '<STR_LIT>' ] == FIFF . FIFFV_MNE_COORD_CTF_HEAD and cand [ '<STR_LIT:to>' ] == FIFF . FIFFV_COORD_HEAD : <EOL> ctf_head_t = cand <EOL> elif cand [ '<STR_LIT>' ] == FIFF . FIFFV_MNE_COORD_CTF_DEVICE and cand [ '<STR_LIT:to>' ] == FIFF . FIFFV_MNE_COORD_CTF_HEAD : <EOL> dev_ctf_t = cand <EOL> elif kind == FIFF . FIFF_EXPERIMENTER : <EOL> tag = read_tag ( fid , pos ) <EOL> experimenter = tag . data <EOL> elif kind == FIFF . FIFF_DESCRIPTION : <EOL> tag = read_tag ( fid , pos ) <EOL> description = tag . data <EOL> elif kind == FIFF . FIFF_PROJ_ID : <EOL> tag = read_tag ( fid , pos ) <EOL> proj_id = tag . data <EOL> elif kind == FIFF . FIFF_PROJ_NAME : <EOL> tag = read_tag ( fid , pos ) <EOL> proj_name = tag . data <EOL> elif kind == FIFF . FIFF_LINE_FREQ : <EOL> tag = read_tag ( fid , pos ) <EOL> line_freq = float ( tag . data ) <EOL> elif kind in [ FIFF . FIFF_MNE_CUSTOM_REF , <NUM_LIT> ] : <EOL> tag = read_tag ( fid , pos ) <EOL> custom_ref_applied = bool ( tag . data ) <EOL> elif kind == FIFF . FIFF_XPLOTTER_LAYOUT : <EOL> tag = read_tag ( fid , pos ) <EOL> xplotter_layout = str ( tag . data ) <EOL> if nchan is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if sfreq is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( chs ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( chs ) != nchan : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if dev_head_t is None or ctf_head_t is None : <EOL> hpi_result = dir_tree_find ( meas_info , FIFF . FIFFB_HPI_RESULT ) <EOL> if len ( hpi_result ) == <NUM_LIT:1> : <EOL> hpi_result = hpi_result [ <NUM_LIT:0> ] <EOL> for k in range ( hpi_result [ '<STR_LIT>' ] ) : <EOL> kind = hpi_result [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = hpi_result [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_COORD_TRANS : <EOL> tag = read_tag ( fid , pos ) <EOL> cand = tag . data <EOL> if ( cand [ '<STR_LIT>' ] == FIFF . FIFFV_COORD_DEVICE and <EOL> cand [ '<STR_LIT:to>' ] == FIFF . FIFFV_COORD_HEAD and <EOL> dev_head_t is None ) : <EOL> dev_head_t = cand <EOL> elif ( cand [ '<STR_LIT>' ] == FIFF . FIFFV_MNE_COORD_CTF_HEAD and <EOL> cand [ '<STR_LIT:to>' ] == FIFF . FIFFV_COORD_HEAD and <EOL> ctf_head_t is None ) : <EOL> ctf_head_t = cand <EOL> dig = _read_dig_fif ( fid , meas_info ) <EOL> acqpars = dir_tree_find ( meas_info , FIFF . FIFFB_DACQ_PARS ) <EOL> acq_pars = None <EOL> acq_stim = None <EOL> if len ( acqpars ) == <NUM_LIT:1> : <EOL> acqpars = acqpars [ <NUM_LIT:0> ] <EOL> for k in range ( acqpars [ '<STR_LIT>' ] ) : <EOL> kind = acqpars [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = acqpars [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_DACQ_PARS : <EOL> tag = read_tag ( fid , pos ) <EOL> acq_pars = tag . data <EOL> elif kind == FIFF . FIFF_DACQ_STIM : <EOL> tag = read_tag ( fid , pos ) <EOL> acq_stim = tag . data <EOL> projs = _read_proj ( fid , meas_info ) <EOL> comps = read_ctf_comp ( fid , meas_info , chs ) <EOL> bads = read_bad_channels ( fid , meas_info ) <EOL> if tree [ '<STR_LIT:id>' ] is not None : <EOL> info = Info ( file_id = tree [ '<STR_LIT:id>' ] ) <EOL> else : <EOL> info = Info ( file_id = None ) <EOL> events = dir_tree_find ( meas_info , FIFF . FIFFB_EVENTS ) <EOL> evs = list ( ) <EOL> for event in events : <EOL> ev = dict ( ) <EOL> for k in range ( event [ '<STR_LIT>' ] ) : <EOL> kind = event [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = event [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_EVENT_CHANNELS : <EOL> ev [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_EVENT_LIST : <EOL> ev [ '<STR_LIT:list>' ] = read_tag ( fid , pos ) . data <EOL> evs . append ( ev ) <EOL> info [ '<STR_LIT>' ] = evs <EOL> hpi_results = dir_tree_find ( meas_info , FIFF . FIFFB_HPI_RESULT ) <EOL> hrs = list ( ) <EOL> for hpi_result in hpi_results : <EOL> hr = dict ( ) <EOL> hr [ '<STR_LIT>' ] = [ ] <EOL> for k in range ( hpi_result [ '<STR_LIT>' ] ) : <EOL> kind = hpi_result [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = hpi_result [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_DIG_POINT : <EOL> hr [ '<STR_LIT>' ] . append ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_HPI_DIGITIZATION_ORDER : <EOL> hr [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_COILS_USED : <EOL> hr [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_COIL_MOMENTS : <EOL> hr [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_FIT_GOODNESS : <EOL> hr [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_FIT_GOOD_LIMIT : <EOL> hr [ '<STR_LIT>' ] = float ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_HPI_FIT_DIST_LIMIT : <EOL> hr [ '<STR_LIT>' ] = float ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_HPI_FIT_ACCEPT : <EOL> hr [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_COORD_TRANS : <EOL> hr [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> hrs . append ( hr ) <EOL> info [ '<STR_LIT>' ] = hrs <EOL> hpi_meass = dir_tree_find ( meas_info , FIFF . FIFFB_HPI_MEAS ) <EOL> hms = list ( ) <EOL> for hpi_meas in hpi_meass : <EOL> hm = dict ( ) <EOL> for k in range ( hpi_meas [ '<STR_LIT>' ] ) : <EOL> kind = hpi_meas [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = hpi_meas [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_CREATOR : <EOL> hm [ '<STR_LIT>' ] = text_type ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_SFREQ : <EOL> hm [ '<STR_LIT>' ] = float ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_NCHAN : <EOL> hm [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_NAVE : <EOL> hm [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_HPI_NCOIL : <EOL> hm [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_FIRST_SAMPLE : <EOL> hm [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_LAST_SAMPLE : <EOL> hm [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> hpi_coils = dir_tree_find ( hpi_meas , FIFF . FIFFB_HPI_COIL ) <EOL> hcs = [ ] <EOL> for hpi_coil in hpi_coils : <EOL> hc = dict ( ) <EOL> for k in range ( hpi_coil [ '<STR_LIT>' ] ) : <EOL> kind = hpi_coil [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = hpi_coil [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_HPI_COIL_NO : <EOL> hc [ '<STR_LIT>' ] = int ( read_tag ( fid , pos ) . data ) <EOL> elif kind == FIFF . FIFF_EPOCH : <EOL> hc [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_SLOPES : <EOL> hc [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_CORR_COEFF : <EOL> hc [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> elif kind == FIFF . FIFF_HPI_COIL_FREQ : <EOL> hc [ '<STR_LIT>' ] = read_tag ( fid , pos ) . data <EOL> hcs . append ( hc ) <EOL> hm [ '<STR_LIT>' ] = hcs <EOL> hms . append ( hm ) <EOL> info [ '<STR_LIT>' ] = hms <EOL> subject_info = dir_tree_find ( meas_info , FIFF . FIFFB_SUBJECT ) <EOL> si = None <EOL> if len ( subject_info ) == <NUM_LIT:1> : <EOL> subject_info = subject_info [ <NUM_LIT:0> ] <EOL> si = dict ( ) <EOL> for k in range ( subject_info [ '<STR_LIT>' ] ) : <EOL> kind = subject_info [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = subject_info [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_SUBJ_ID : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT:id>' ] = int ( tag . data ) <EOL> elif kind == FIFF . FIFF_SUBJ_HIS_ID : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = text_type ( tag . data ) <EOL> elif kind == FIFF . FIFF_SUBJ_LAST_NAME : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = text_type ( tag . data ) <EOL> elif kind == FIFF . FIFF_SUBJ_FIRST_NAME : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = text_type ( tag . data ) <EOL> elif kind == FIFF . FIFF_SUBJ_MIDDLE_NAME : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = text_type ( tag . data ) <EOL> elif kind == FIFF . FIFF_SUBJ_BIRTH_DAY : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = tag . data <EOL> elif kind == FIFF . FIFF_SUBJ_SEX : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = int ( tag . data ) <EOL> elif kind == FIFF . FIFF_SUBJ_HAND : <EOL> tag = read_tag ( fid , pos ) <EOL> si [ '<STR_LIT>' ] = int ( tag . data ) <EOL> info [ '<STR_LIT>' ] = si <EOL> hpi_subsystem = dir_tree_find ( meas_info , FIFF . FIFFB_HPI_SUBSYSTEM ) <EOL> hs = None <EOL> if len ( hpi_subsystem ) == <NUM_LIT:1> : <EOL> hpi_subsystem = hpi_subsystem [ <NUM_LIT:0> ] <EOL> hs = dict ( ) <EOL> for k in range ( hpi_subsystem [ '<STR_LIT>' ] ) : <EOL> kind = hpi_subsystem [ '<STR_LIT>' ] [ k ] . kind <EOL> pos = hpi_subsystem [ '<STR_LIT>' ] [ k ] . pos <EOL> if kind == FIFF . FIFF_HPI_NCOIL : <EOL> tag = read_tag ( fid , pos ) <EOL> hs [ '<STR_LIT>' ] = int ( tag . data ) <EOL> elif kind == FIFF . FIFF_EVENT_CHANNEL : <EOL> tag = read_tag ( fid , pos ) <EOL> hs [ '<STR_LIT>' ] = text_type ( tag . data ) <EOL> hpi_coils = dir_tree_find ( hpi_subsystem , FIFF . FIFFB_HPI_COIL ) <EOL> hc = [ ] <EOL> for coil in hpi_coils : <EOL> this_coil = dict ( ) <EOL> for j in range ( coil [ '<STR_LIT>' ] ) : <EOL> kind = coil [ '<STR_LIT>' ] [ j ] . kind <EOL> pos = coil [ '<STR_LIT>' ] [ j ] . pos <EOL> if kind == FIFF . FIFF_EVENT_BITS : <EOL> tag = read_tag ( fid , pos ) <EOL> this_coil [ '<STR_LIT>' ] = np . array ( tag . data ) <EOL> hc . append ( this_coil ) <EOL> hs [ '<STR_LIT>' ] = hc <EOL> info [ '<STR_LIT>' ] = hs <EOL> _read_proc_history ( fid , tree , info ) <EOL> if meas_info [ '<STR_LIT>' ] is None : <EOL> if meas_info [ '<STR_LIT:id>' ] is None : <EOL> if meas [ '<STR_LIT:id>' ] is None : <EOL> if meas [ '<STR_LIT>' ] is None : <EOL> info [ '<STR_LIT>' ] = info [ '<STR_LIT>' ] <EOL> else : <EOL> info [ '<STR_LIT>' ] = meas [ '<STR_LIT>' ] <EOL> else : <EOL> info [ '<STR_LIT>' ] = meas [ '<STR_LIT:id>' ] <EOL> else : <EOL> info [ '<STR_LIT>' ] = meas_info [ '<STR_LIT:id>' ] <EOL> else : <EOL> info [ '<STR_LIT>' ] = meas_info [ '<STR_LIT>' ] <EOL> info [ '<STR_LIT>' ] = experimenter <EOL> info [ '<STR_LIT:description>' ] = description <EOL> info [ '<STR_LIT>' ] = proj_id <EOL> info [ '<STR_LIT>' ] = proj_name <EOL> if meas_date is None : <EOL> meas_date = [ info [ '<STR_LIT>' ] [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> info [ '<STR_LIT>' ] = meas_date <EOL> info [ '<STR_LIT>' ] = sfreq <EOL> info [ '<STR_LIT>' ] = highpass if highpass is not None else <NUM_LIT:0.> <EOL> info [ '<STR_LIT>' ] = lowpass if lowpass is not None else info [ '<STR_LIT>' ] / <NUM_LIT> <EOL> info [ '<STR_LIT>' ] = line_freq <EOL> info [ '<STR_LIT>' ] = chs <EOL> info [ '<STR_LIT>' ] = dev_head_t <EOL> info [ '<STR_LIT>' ] = ctf_head_t <EOL> info [ '<STR_LIT>' ] = dev_ctf_t <EOL> if dev_head_t is not None and ctf_head_t is not None and dev_ctf_t is None : <EOL> from . . transforms import Transform <EOL> head_ctf_trans = linalg . inv ( ctf_head_t [ '<STR_LIT>' ] ) <EOL> dev_ctf_trans = np . dot ( head_ctf_trans , info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> info [ '<STR_LIT>' ] = Transform ( '<STR_LIT>' , '<STR_LIT>' , dev_ctf_trans ) <EOL> info [ '<STR_LIT>' ] = dig <EOL> info [ '<STR_LIT>' ] = bads <EOL> info . _update_redundant ( ) <EOL> if clean_bads : <EOL> info [ '<STR_LIT>' ] = [ b for b in bads if b in info [ '<STR_LIT>' ] ] <EOL> info [ '<STR_LIT>' ] = projs <EOL> info [ '<STR_LIT>' ] = comps <EOL> info [ '<STR_LIT>' ] = acq_pars <EOL> info [ '<STR_LIT>' ] = acq_stim <EOL> info [ '<STR_LIT>' ] = custom_ref_applied <EOL> info [ '<STR_LIT>' ] = xplotter_layout <EOL> info . _check_consistency ( ) <EOL> return info , meas <EOL> def write_meas_info ( fid , info , data_type = None , reset_range = True ) : <EOL> """<STR_LIT>""" <EOL> info . _check_consistency ( ) <EOL> start_block ( fid , FIFF . FIFFB_MEAS_INFO ) <EOL> for event in info [ '<STR_LIT>' ] : <EOL> start_block ( fid , FIFF . FIFFB_EVENTS ) <EOL> if event . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_EVENT_CHANNELS , event [ '<STR_LIT>' ] ) <EOL> if event . get ( '<STR_LIT:list>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_EVENT_LIST , event [ '<STR_LIT:list>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_EVENTS ) <EOL> for hpi_result in info [ '<STR_LIT>' ] : <EOL> start_block ( fid , FIFF . FIFFB_HPI_RESULT ) <EOL> for d in hpi_result [ '<STR_LIT>' ] : <EOL> write_dig_point ( fid , d ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_int ( fid , FIFF . FIFF_HPI_DIGITIZATION_ORDER , <EOL> hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_int ( fid , FIFF . FIFF_HPI_COILS_USED , hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_float_matrix ( fid , FIFF . FIFF_HPI_COIL_MOMENTS , <EOL> hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_float ( fid , FIFF . FIFF_HPI_FIT_GOODNESS , <EOL> hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_float ( fid , FIFF . FIFF_HPI_FIT_GOOD_LIMIT , <EOL> hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_float ( fid , FIFF . FIFF_HPI_FIT_DIST_LIMIT , <EOL> hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_int ( fid , FIFF . FIFF_HPI_FIT_ACCEPT , hpi_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in hpi_result : <EOL> write_coord_trans ( fid , hpi_result [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_HPI_RESULT ) <EOL> for hpi_meas in info [ '<STR_LIT>' ] : <EOL> start_block ( fid , FIFF . FIFFB_HPI_MEAS ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_CREATOR , hpi_meas [ '<STR_LIT>' ] ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_float ( fid , FIFF . FIFF_SFREQ , hpi_meas [ '<STR_LIT>' ] ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_NCHAN , hpi_meas [ '<STR_LIT>' ] ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_NAVE , hpi_meas [ '<STR_LIT>' ] ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_HPI_NCOIL , hpi_meas [ '<STR_LIT>' ] ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_FIRST_SAMPLE , hpi_meas [ '<STR_LIT>' ] ) <EOL> if hpi_meas . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_LAST_SAMPLE , hpi_meas [ '<STR_LIT>' ] ) <EOL> for hpi_coil in hpi_meas [ '<STR_LIT>' ] : <EOL> start_block ( fid , FIFF . FIFFB_HPI_COIL ) <EOL> if hpi_coil . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_HPI_COIL_NO , hpi_coil [ '<STR_LIT>' ] ) <EOL> if hpi_coil . get ( '<STR_LIT>' ) is not None : <EOL> write_float_matrix ( fid , FIFF . FIFF_EPOCH , hpi_coil [ '<STR_LIT>' ] ) <EOL> if hpi_coil . get ( '<STR_LIT>' ) is not None : <EOL> write_float ( fid , FIFF . FIFF_HPI_SLOPES , hpi_coil [ '<STR_LIT>' ] ) <EOL> if hpi_coil . get ( '<STR_LIT>' ) is not None : <EOL> write_float ( fid , FIFF . FIFF_HPI_CORR_COEFF , <EOL> hpi_coil [ '<STR_LIT>' ] ) <EOL> if hpi_coil . get ( '<STR_LIT>' ) is not None : <EOL> write_float ( fid , FIFF . FIFF_HPI_COIL_FREQ , <EOL> hpi_coil [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_HPI_COIL ) <EOL> end_block ( fid , FIFF . FIFFB_HPI_MEAS ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> start_block ( fid , FIFF . FIFFB_ISOTRAK ) <EOL> for d in info [ '<STR_LIT>' ] : <EOL> write_dig_point ( fid , d ) <EOL> end_block ( fid , FIFF . FIFFB_ISOTRAK ) <EOL> if info [ '<STR_LIT>' ] is not None or info [ '<STR_LIT>' ] is not None : <EOL> start_block ( fid , FIFF . FIFFB_DACQ_PARS ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_string ( fid , FIFF . FIFF_DACQ_PARS , info [ '<STR_LIT>' ] ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_string ( fid , FIFF . FIFF_DACQ_STIM , info [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_DACQ_PARS ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_coord_trans ( fid , info [ '<STR_LIT>' ] ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_coord_trans ( fid , info [ '<STR_LIT>' ] ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_coord_trans ( fid , info [ '<STR_LIT>' ] ) <EOL> _write_proj ( fid , info [ '<STR_LIT>' ] ) <EOL> if len ( info [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> start_block ( fid , FIFF . FIFFB_MNE_BAD_CHANNELS ) <EOL> write_name_list ( fid , FIFF . FIFF_MNE_CH_NAME_LIST , info [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_MNE_BAD_CHANNELS ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_EXPERIMENTER , info [ '<STR_LIT>' ] ) <EOL> if info . get ( '<STR_LIT:description>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_DESCRIPTION , info [ '<STR_LIT:description>' ] ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_PROJ_ID , info [ '<STR_LIT>' ] ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_PROJ_NAME , info [ '<STR_LIT>' ] ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_MEAS_DATE , info [ '<STR_LIT>' ] ) <EOL> write_int ( fid , FIFF . FIFF_NCHAN , info [ '<STR_LIT>' ] ) <EOL> write_float ( fid , FIFF . FIFF_SFREQ , info [ '<STR_LIT>' ] ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_float ( fid , FIFF . FIFF_LOWPASS , info [ '<STR_LIT>' ] ) <EOL> if info [ '<STR_LIT>' ] is not None : <EOL> write_float ( fid , FIFF . FIFF_HIGHPASS , info [ '<STR_LIT>' ] ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> write_float ( fid , FIFF . FIFF_LINE_FREQ , info [ '<STR_LIT>' ] ) <EOL> if data_type is not None : <EOL> write_int ( fid , FIFF . FIFF_DATA_PACK , data_type ) <EOL> if info . get ( '<STR_LIT>' ) : <EOL> write_int ( fid , FIFF . FIFF_MNE_CUSTOM_REF , info [ '<STR_LIT>' ] ) <EOL> if info . get ( '<STR_LIT>' ) : <EOL> write_string ( fid , FIFF . FIFF_XPLOTTER_LAYOUT , info [ '<STR_LIT>' ] ) <EOL> for k , c in enumerate ( info [ '<STR_LIT>' ] ) : <EOL> c = deepcopy ( c ) <EOL> c [ '<STR_LIT>' ] = k + <NUM_LIT:1> <EOL> if reset_range is True : <EOL> c [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> write_ch_info ( fid , c ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> start_block ( fid , FIFF . FIFFB_SUBJECT ) <EOL> si = info [ '<STR_LIT>' ] <EOL> if si . get ( '<STR_LIT:id>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_SUBJ_ID , si [ '<STR_LIT:id>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_SUBJ_HIS_ID , si [ '<STR_LIT>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_SUBJ_LAST_NAME , si [ '<STR_LIT>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_SUBJ_FIRST_NAME , si [ '<STR_LIT>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_SUBJ_MIDDLE_NAME , si [ '<STR_LIT>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_julian ( fid , FIFF . FIFF_SUBJ_BIRTH_DAY , si [ '<STR_LIT>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_SUBJ_SEX , si [ '<STR_LIT>' ] ) <EOL> if si . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_SUBJ_HAND , si [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_SUBJECT ) <EOL> if info . get ( '<STR_LIT>' ) is not None : <EOL> hs = info [ '<STR_LIT>' ] <EOL> start_block ( fid , FIFF . FIFFB_HPI_SUBSYSTEM ) <EOL> if hs . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_HPI_NCOIL , hs [ '<STR_LIT>' ] ) <EOL> if hs . get ( '<STR_LIT>' ) is not None : <EOL> write_string ( fid , FIFF . FIFF_EVENT_CHANNEL , hs [ '<STR_LIT>' ] ) <EOL> if hs . get ( '<STR_LIT>' ) is not None : <EOL> for coil in hs [ '<STR_LIT>' ] : <EOL> start_block ( fid , FIFF . FIFFB_HPI_COIL ) <EOL> if coil . get ( '<STR_LIT>' ) is not None : <EOL> write_int ( fid , FIFF . FIFF_EVENT_BITS , <EOL> coil [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_HPI_COIL ) <EOL> end_block ( fid , FIFF . FIFFB_HPI_SUBSYSTEM ) <EOL> write_ctf_comp ( fid , info [ '<STR_LIT>' ] ) <EOL> end_block ( fid , FIFF . FIFFB_MEAS_INFO ) <EOL> _write_proc_history ( fid , info ) <EOL> def write_info ( fname , info , data_type = None , reset_range = True ) : <EOL> """<STR_LIT>""" <EOL> fid = start_file ( fname ) <EOL> start_block ( fid , FIFF . FIFFB_MEAS ) <EOL> write_meas_info ( fid , info , data_type , reset_range ) <EOL> end_block ( fid , FIFF . FIFFB_MEAS ) <EOL> end_file ( fid ) <EOL> def _is_equal_dict ( dicts ) : <EOL> """<STR_LIT>""" <EOL> tests = zip ( * [ d . items ( ) for d in dicts ] ) <EOL> is_equal = [ ] <EOL> for d in tests : <EOL> k0 , v0 = d [ <NUM_LIT:0> ] <EOL> if ( isinstance ( v0 , ( list , np . ndarray ) ) and len ( v0 ) > <NUM_LIT:0> and <EOL> isinstance ( v0 [ <NUM_LIT:0> ] , dict ) ) : <EOL> for k , v in d : <EOL> is_equal . append ( ( k0 == k ) and _is_equal_dict ( v ) ) <EOL> else : <EOL> is_equal . append ( all ( np . all ( k == k0 ) and <EOL> np . all ( v == v0 ) for k , v in d ) ) <EOL> return all ( is_equal ) <EOL> @ verbose <EOL> def _merge_dict_values ( dicts , key , verbose = None ) : <EOL> """<STR_LIT>""" <EOL> values = [ d [ key ] for d in dicts ] <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % key ) <EOL> def _flatten ( lists ) : <EOL> return [ item for sublist in lists for item in sublist ] <EOL> def _check_isinstance ( values , kind , func ) : <EOL> return func ( [ isinstance ( v , kind ) for v in values ] ) <EOL> def _where_isinstance ( values , kind ) : <EOL> """<STR_LIT>""" <EOL> return np . where ( [ isinstance ( v , type ) for v in values ] ) [ <NUM_LIT:0> ] <EOL> if _check_isinstance ( values , list , all ) : <EOL> lists = ( d [ key ] for d in dicts ) <EOL> return ( _uniquify_projs ( _flatten ( lists ) ) if key == '<STR_LIT>' <EOL> else _flatten ( lists ) ) <EOL> elif _check_isinstance ( values , list , any ) : <EOL> idx = _where_isinstance ( values , list ) <EOL> if len ( idx ) == <NUM_LIT:1> : <EOL> return values [ int ( idx ) ] <EOL> elif len ( idx ) > <NUM_LIT:1> : <EOL> lists = ( d [ key ] for d in dicts if isinstance ( d [ key ] , list ) ) <EOL> return _flatten ( lists ) <EOL> elif _check_isinstance ( values , dict , all ) : <EOL> is_qual = _is_equal_dict ( values ) <EOL> if is_qual : <EOL> return values [ <NUM_LIT:0> ] <EOL> else : <EOL> RuntimeError ( msg ) <EOL> elif _check_isinstance ( values , dict , any ) : <EOL> idx = _where_isinstance ( values , dict ) <EOL> if len ( idx ) == <NUM_LIT:1> : <EOL> return values [ int ( idx ) ] <EOL> elif len ( idx ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( msg ) <EOL> elif _check_isinstance ( values , np . ndarray , all ) : <EOL> is_qual = all ( np . all ( values [ <NUM_LIT:0> ] == x ) for x in values [ <NUM_LIT:1> : ] ) <EOL> if is_qual : <EOL> return values [ <NUM_LIT:0> ] <EOL> elif key == '<STR_LIT>' : <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % key ) <EOL> return None <EOL> else : <EOL> raise RuntimeError ( msg ) <EOL> elif _check_isinstance ( values , np . ndarray , any ) : <EOL> idx = _where_isinstance ( values , np . ndarray ) <EOL> if len ( idx ) == <NUM_LIT:1> : <EOL> return values [ int ( idx ) ] <EOL> elif len ( idx ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( msg ) <EOL> else : <EOL> unique_values = set ( values ) <EOL> if len ( unique_values ) == <NUM_LIT:1> : <EOL> return list ( values ) [ <NUM_LIT:0> ] <EOL> elif isinstance ( list ( unique_values ) [ <NUM_LIT:0> ] , BytesIO ) : <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return None <EOL> elif isinstance ( list ( unique_values ) [ <NUM_LIT:0> ] , string_types ) : <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return None <EOL> else : <EOL> raise RuntimeError ( msg ) <EOL> @ verbose <EOL> def _merge_info ( infos , verbose = None ) : <EOL> """<STR_LIT>""" <EOL> for info in infos : <EOL> info . _check_consistency ( ) <EOL> info = Info ( ) <EOL> info [ '<STR_LIT>' ] = [ ] <EOL> for this_info in infos : <EOL> info [ '<STR_LIT>' ] . extend ( this_info [ '<STR_LIT>' ] ) <EOL> info . _update_redundant ( ) <EOL> duplicates = set ( [ ch for ch in info [ '<STR_LIT>' ] <EOL> if info [ '<STR_LIT>' ] . count ( ch ) > <NUM_LIT:1> ] ) <EOL> if len ( duplicates ) > <NUM_LIT:0> : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % list ( duplicates ) ) <EOL> raise ValueError ( msg ) <EOL> transforms = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for trans_name in transforms : <EOL> trans = [ i [ trans_name ] for i in infos if i [ trans_name ] ] <EOL> if len ( trans ) == <NUM_LIT:0> : <EOL> info [ trans_name ] = None <EOL> elif len ( trans ) == <NUM_LIT:1> : <EOL> info [ trans_name ] = trans [ <NUM_LIT:0> ] <EOL> elif all ( np . all ( trans [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == x [ '<STR_LIT>' ] ) and <EOL> trans [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == x [ '<STR_LIT>' ] and <EOL> trans [ <NUM_LIT:0> ] [ '<STR_LIT:to>' ] == x [ '<STR_LIT:to>' ] <EOL> for x in trans [ <NUM_LIT:1> : ] ) : <EOL> info [ trans_name ] = trans [ <NUM_LIT:0> ] <EOL> else : <EOL> msg = ( "<STR_LIT>" % <EOL> trans_name ) <EOL> raise ValueError ( msg ) <EOL> other_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:filename>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for k in other_fields : <EOL> info [ k ] = _merge_dict_values ( infos , k ) <EOL> info . _check_consistency ( ) <EOL> return info <EOL> def create_info ( ch_names , sfreq , ch_types = None , montage = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( ch_names , int ) : <EOL> ch_names = list ( np . arange ( ch_names ) . astype ( str ) ) <EOL> if not isinstance ( ch_names , ( list , tuple ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> sfreq = float ( sfreq ) <EOL> if sfreq <= <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> nchan = len ( ch_names ) <EOL> if ch_types is None : <EOL> ch_types = [ '<STR_LIT>' ] * nchan <EOL> if isinstance ( ch_types , string_types ) : <EOL> ch_types = [ ch_types ] * nchan <EOL> if len ( ch_types ) != nchan : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> info = _empty_info ( sfreq ) <EOL> info [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> ] , np . int32 ) <EOL> loc = np . concatenate ( ( np . zeros ( <NUM_LIT:3> ) , np . eye ( <NUM_LIT:3> ) . ravel ( ) ) ) . astype ( np . float32 ) <EOL> for ci , ( name , kind ) in enumerate ( zip ( ch_names , ch_types ) ) : <EOL> if not isinstance ( name , string_types ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if not isinstance ( kind , string_types ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if kind not in _kind_dict : <EOL> raise KeyError ( '<STR_LIT>' <EOL> % ( list ( _kind_dict . keys ( ) ) , kind ) ) <EOL> kind = _kind_dict [ kind ] <EOL> chan_info = dict ( loc = loc . copy ( ) , unit_mul = <NUM_LIT:0> , range = <NUM_LIT:1.> , cal = <NUM_LIT:1.> , <EOL> kind = kind [ <NUM_LIT:0> ] , coil_type = kind [ <NUM_LIT:1> ] , <EOL> unit = kind [ <NUM_LIT:2> ] , coord_frame = FIFF . FIFFV_COORD_UNKNOWN , <EOL> ch_name = name , scanno = ci + <NUM_LIT:1> , logno = ci + <NUM_LIT:1> ) <EOL> info [ '<STR_LIT>' ] . append ( chan_info ) <EOL> info . _update_redundant ( ) <EOL> if montage is not None : <EOL> from . . channels . montage import ( Montage , DigMontage , _set_montage , <EOL> read_montage ) <EOL> if not isinstance ( montage , list ) : <EOL> montage = [ montage ] <EOL> for montage_ in montage : <EOL> if isinstance ( montage_ , ( Montage , DigMontage ) ) : <EOL> _set_montage ( info , montage_ ) <EOL> elif isinstance ( montage_ , string_types ) : <EOL> montage_ = read_montage ( montage_ ) <EOL> _set_montage ( info , montage_ ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % type ( montage ) ) <EOL> info . _check_consistency ( ) <EOL> return info <EOL> RAW_INFO_FIELDS = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def _empty_info ( sfreq ) : <EOL> """<STR_LIT>""" <EOL> from . . transforms import Transform <EOL> _none_keys = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> _list_keys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> info = Info ( ) <EOL> for k in _none_keys : <EOL> info [ k ] = None <EOL> for k in _list_keys : <EOL> info [ k ] = list ( ) <EOL> info [ '<STR_LIT>' ] = False <EOL> info [ '<STR_LIT>' ] = Transform ( '<STR_LIT>' , '<STR_LIT>' , np . eye ( <NUM_LIT:4> ) ) <EOL> info [ '<STR_LIT>' ] = <NUM_LIT:0.> <EOL> info [ '<STR_LIT>' ] = float ( sfreq ) <EOL> info [ '<STR_LIT>' ] = info [ '<STR_LIT>' ] / <NUM_LIT> <EOL> info . _update_redundant ( ) <EOL> info . _check_consistency ( ) <EOL> return info </s>
