def apply_create_noise_covariance(fname_empty_room, require_filter=True, verbose=None): ''' Creates the noise covariance matrix from an empty room file. Parameters ---------- fname_empty_room : String containing the filename of the empty room file (must be a fif-file) require_filter: bool If true, the empy room file is filtered before calculating the covariance matrix. (Beware, filter settings are fixed.) verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). default: verbose=None ''' # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne import compute_raw_data_covariance as cp_covariance from mne import write_cov from mne.io import Raw from mne import pick_types import os fner = get_files_from_list(fname_empty_room) nfiles = len(fner) # loop across all filenames for ifile in range(nfiles): fn_in = fner[ifile] print ">>> create noise covariance using file: " path_in , name = os.path.split(fn_in) print name if require_filter == True: print "Filtering with preset settings..." # filter empty room raw data apply_filter(fn_in, flow=1, fhigh=45, order=4, njobs=4) # reconstruct empty room file name accordingly fn_in = fn_in.split('-')[0] + ',bp1-45Hz-empty.fif' # file name for saving noise_cov fn_out = fn_in.split('-')[0] + ',empty-cov.fif' # read in data raw_empty = Raw(fn_in, verbose=verbose) # pick MEG channels only picks = pick_types(raw_empty.info, meg=True, ref_meg=False, eeg=False, stim=False, eog=False, exclude='bads') # calculate noise-covariance matrix noise_cov_mat = cp_covariance(raw_empty, picks=picks, verbose=verbose) # write noise-covariance matrix to disk write_cov(fn_out, noise_cov_mat)
def apply_create_noise_covariance(fname_empty_room, verbose=None): ''' Creates the noise covariance matrix from an empty room file. Parameters ---------- fname_empty_room : String containing the filename of the de-noise, empty room file (must be a fif-file) require_filter: bool If true, the empy room file is filtered before calculating the covariance matrix. (Beware, filter settings are fixed.) verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). default: verbose=None ''' # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne import compute_raw_data_covariance as cp_covariance from mne import write_cov, pick_types from mne.io import Raw from jumeg.jumeg_noise_reducer import noise_reducer fner = get_files_from_list(fname_empty_room) nfiles = len(fner) ext_empty_raw = '-raw.fif' ext_empty_cov = '-cov.fif' # loop across all filenames for ifile in range(nfiles): fn_in = fner[ifile] print ">>> create noise covariance using file: " path_in, name = os.path.split(fn_in) print name fn_empty_nr = fn_in[:fn_in.rfind('-raw.fif')] + ',nr-raw.fif' noise_reducer(fn_in, refnotch=50, detrending=False, fnout=fn_empty_nr) noise_reducer(fn_empty_nr, refnotch=60, detrending=False, fnout=fn_empty_nr) noise_reducer(fn_empty_nr, reflp=5, fnout=fn_empty_nr) # file name for saving noise_cov fn_out = fn_empty_nr[:fn_empty_nr.rfind(ext_empty_raw)] + ext_empty_cov # read in data raw_empty = Raw(fn_empty_nr, preload=True, verbose=verbose) raw_empty.interpolate_bads() # pick MEG channels only picks = pick_types(raw_empty.info, meg=True, ref_meg=False, eeg=False, stim=False, eog=False, exclude='bads') # calculate noise-covariance matrix noise_cov_mat = cp_covariance(raw_empty, picks=picks, verbose=verbose) # write noise-covariance matrix to disk write_cov(fn_out, noise_cov_mat)
def apply_create_noise_covariance_data(fname,raw=None,do_filter=True,filter_parameter=None,verbose=False,do_run=True,save=True ): ''' Creates the noise covariance matrix from an empty room file. Parameters ---------- fname : string containing the filename of the empty room file (must be a fif-file) do_filter: bool If true, the empy room file is filtered before calculating the covariance matrix. (Beware, filter settings are fixed.) filter_parameter: dict dict with filter parameter and flags do_run: bool execute this save: bool save output file verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). default: verbose=None RETURN --------- full empty room filname as string raw obj of input raw-obj or raw-empty-room obj ''' # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne import compute_raw_data_covariance as cp_covariance from mne import write_cov from mne.io import Raw from mne import pick_types import os mne.verbose = verbose try: (fname_empty_room,raw_empty) = jumeg_base.get_empty_room_fif(fname=fname,raw=raw,preload=do_run) except: return if raw_empty : #--- picks meg channels filter_parameter.picks = jumeg_base.pick_meg_nobads(raw_empty) #--- filter or get filter name filter_parameter.do_run = do_filter if do_filter : print "Filtering empty room fif with noise variance settings...\n" (fname_empty_room,raw_empty) = apply_filter_data(fname_empty_room,raw=raw_empty,**filter_parameter) #--- update file name for saving noise_cov fname_empty_room_cov = fname_empty_room.split('-')[0] + ',empty-cov.fif' #--- calc nois-covariance matrix if do_run : noise_cov_mat = cp_covariance(raw_empty,picks=filter_parameter.picks,verbose=verbose) #--- write noise-covariance matrix to disk if save : write_cov( fname_empty_room_cov, noise_cov_mat) return fname_empty_room_cov
def apply_create_noise_covariance(fname_empty_room, require_filter=False, require_noise_reducer=False, verbose=None): ''' Creates the noise covariance matrix from an empty room file. Parameters ---------- fname_empty_room : String containing the filename of the empty room file (must be a fif-file) File name should end with -raw.fif in order to have proper output filenames. require_filter: bool If true, the empy room file is filtered before calculating the covariance matrix. (Beware, filter settings are fixed.) require_noise_reducer: bool If true, a noise reducer is applied on the empty room file. The noise reducer frequencies are fixed to 50Hz, 60Hz and to frequencies less than 5Hz i.e. the reference channels are filtered to these frequency ranges and then signal obtained is removed from the empty room raw data. For more information please check the jumeg noise reducer. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). default: verbose=None ''' # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne import compute_raw_data_covariance as cp_covariance from mne import write_cov, pick_types from mne.io import Raw from jumeg.jumeg_noise_reducer import noise_reducer fner = get_files_from_list(fname_empty_room) nfiles = len(fner) # loop across all filenames for ifile in range(nfiles): fn_in = fner[ifile] print ">>> create noise covariance using file: " path_in, name = os.path.split(fn_in) print name if require_filter: print "Filtering with preset settings..." # filter empty room raw data apply_filter(fn_in, flow=1, fhigh=45, order=4, njobs=4) # reconstruct empty room file name accordingly fn_in = fn_in[:fn_in.rfind(ext_empty_raw)] + ',fibp1-45-raw.fif' if require_noise_reducer: fn_empty_nr = fn_in[:fn_in.rfind(ext_empty_raw)] + ',nr-raw.fif' noise_reducer(fn_in, refnotch=50, detrending=False, fnout=fn_empty_nr) noise_reducer(fn_empty_nr, refnotch=60, detrending=False, fnout=fn_empty_nr) noise_reducer(fn_empty_nr, reflp=5, fnout=fn_empty_nr) fn_in = fn_empty_nr # file name for saving noise_cov fn_out = fn_in[:fn_in.rfind(ext_empty_raw)] + ext_empty_cov # read in data raw_empty = Raw(fn_in, verbose=verbose) # pick MEG channels only picks = pick_types(raw_empty.info, meg=True, ref_meg=False, eeg=False, stim=False, eog=False, exclude='bads') # calculate noise-covariance matrix noise_cov_mat = cp_covariance(raw_empty, picks=picks, verbose=verbose) # write noise-covariance matrix to disk write_cov(fn_out, noise_cov_mat)
def apply_create_noise_covariance(fname_empty_room, fname_out, verbose=None): ''' Creates the noise covariance matrix from an empty room file. Parameters ---------- fname_empty_room : String containing the filename of the empty room file (must be a fif-file) fname_out : String containing the output filename of the noise-covariance estimation (must also be a fif-file) verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). default: verbose=None ''' # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne import compute_raw_data_covariance as cp_covariance from mne import write_cov from mne.io import Raw from mne import pick_types import os if isinstance(fname_empty_room, list): fner = fname_empty_room else: if isinstance(fname_empty_room, str): fner = list([fname_empty_room]) else: fner = list(fname_empty_room) nfiles = len(fner) if isinstance(fname_out, list): fnout = fname_out else: if isinstance(fname_out, str): fnout = list([fname_out]) else: fnout = list(fname_out) if (len(fnout) != nfiles): print ">>> Error (create_noise_covariance_matrix):" print " Number of in/out files do not match" print exit() # loop across all filenames for ifile in range(nfiles): fn_in = fner[ifile] fn_out = fnout[ifile] print ">>> create noise covariance using file: " path_in , name = os.path.split(fn_in) print name # read in data raw_empty = Raw(fn_in, verbose=verbose) # pick MEG channels only picks = pick_types(raw_empty.info, meg=True, eeg=False, stim=False, eog=False, exclude='bads') # calculate noise-covariance matrix noise_cov_mat = cp_covariance(raw_empty, picks=picks, verbose=verbose) # write noise-covariance matrix to disk write_cov(fn_out, noise_cov_mat)
def apply_cov(fname_empty_room, filtered=True): ''' Creates the noise covariance matrix from an empty room file. Parameters ---------- fname_empty_room : String containing the filename of the empty room file (must be a fif-file) File name should end with -raw.fif in order to have proper output filenames. require_filter: bool If true, the empy room file is filtered before calculating the covariance matrix. (Beware, filter settings are fixed.) require_noise_reducer: bool If true, a noise reducer is applied on the empty room file. The noise reducer frequencies are fixed to 50Hz, 60Hz and to frequencies less than 5Hz i.e. the reference channels are filtered to these frequency ranges and then signal obtained is removed from the empty room raw data. For more information please check the jumeg noise reducer. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). default: verbose=None ''' # ------------------------------------------- # import necessary modules # ------------------------------------------- from mne import compute_raw_covariance as cp_covariance from mne import write_cov, pick_types from mne.io import Raw fner = get_files_from_list(fname_empty_room) nfiles = len(fner) # loop across all filenames for ifile in range(nfiles): fn_in = fner[ifile] fn_fig1 = fn_in[:fn_in.rfind(ext_empty_raw)] + ',Magnetometers.tiff' fn_fig2 = fn_in[:fn_in.rfind(ext_empty_raw)] + ',Eigenvalue_index.tiff' #fn_out = fn_in[:fn_in.rfind(ext_empty_raw)] + ext_empty_cov path_in, name = os.path.split(fn_in) subject = name.split('_')[0] # read in data raw_empty = Raw(fn_in) # pick MEG channels only picks = pick_types(raw_empty.info, meg=True, exclude='bads') # calculate noise-covariance matrix noise_cov_mat = cp_covariance(raw_empty, tmin=None, tmax=None, tstep=0.2, picks=picks) #noise_cov_mat = cp_covariance(raw_empty, picks=picks) fig1, fig2 = mne.viz.plot_cov(noise_cov_mat, raw_empty.info) # write noise-covariance matrix to disk if filtered: fn_out = path_in + '/%s_empty,fibp1-45' % subject + ext_empty_cov else: fn_out = path_in + '/%s_empty' % subject + ext_empty_cov write_cov(fn_out, noise_cov_mat) fig1.savefig(fn_fig1) fig2.savefig(fn_fig2) pl.close('all')