def nr_plot_denoising(f1,f2,fnout): """ call orig plotting function jumeg.jumeg_noise_reducer import plot_denoising :param f1: :param f2: :param fnout: :return: """ from jumeg.jumeg_noise_reducer import plot_denoising plot_denoising([f1,f2],n_jobs=1,fnout=fnout,show=False)
def noise_reduction(dirname, raw_fname, denoised_fname, nr_cfg, state_space_fname): """ Apply the noise reducer to the raw file and save the result. Parameters: ----------- dirname : str Path to the directory where the raw file is stored. raw_fname : str File name of the raw file. denoised_fname : str File name under which the denoised raw file is saved. nr_cfg: dict Dict containing the noise reducer specific settings from the config file. state_space_fname : str Second half of the name under which the state space dict is to be saved, e.g., subj + state_space_name. Returns: -------- None """ refnotch = nr_cfg['refnotch'] reflp = nr_cfg['reflp'] refhp = nr_cfg['refhp'] noiseref_hp = nr_cfg['noiseref_hp'] from jumeg.jumeg_noise_reducer import noise_reducer, plot_denoising subj = op.basename(raw_fname).split('_')[0] ss_dict_fname = op.join(op.dirname(raw_fname), subj + state_space_fname) # read the raw file raw = mne.io.Raw(op.join(dirname, raw_fname), preload=True) # apply noise reducer thrice to reference channels with different freq parameters # the nr-raw.fif are rewritten # low pass filter for freq below 5 hz raw_nr = noise_reducer(raw_fname, raw=raw, reflp=reflp, return_raw=True) raw.close() raw_nr = noise_reducer(raw_fname, raw=raw_nr, refhp=refhp, noiseref=noiseref_hp, return_raw=True) # notch filter to remove power line noise raw_nr = noise_reducer(raw_fname, raw=raw_nr, refnotch=refnotch, fnout=op.join(dirname, denoised_fname), return_raw=True) raw_nr.close() # plot final plotting plot_name = denoised_fname.rsplit('-raw.fif')[0] + '-plot' plot_denoising( [op.join(dirname, raw_fname), op.join(dirname, denoised_fname)], n_jobs=1, fnout=op.join(dirname, plot_name), show=False) # save config file nr_dict = nr_cfg.copy() nr_dict['input_file'] = op.join(dirname, raw_fname) nr_dict['process'] = 'noise_reducer' nr_dict['output_file'] = op.join(dirname, denoised_fname) save_state_space_file(ss_dict_fname, process_config_dict=nr_dict)
def apply_noise_reducer(fname, raw=None, do_run=True, verbose=False, save=True, plot=False, fif_postfix="nr", fif_extention="-raw.fif", return_raw=True, reflp=None, refhp=None, refnotch=None, parameter={}): ''' Applies the noise reducer to raw obj data or to fif-file. the magic ee once fb modified for raw obj support imports jumeg_noise_reducer_4raw_data ''' import os # --- import noise_reducer and plot_power_spectrum function # from jumeg.jumeg_4raw_data_noise_reducer import noise_reducer_4raw_data, plot_denoising_4raw_data from jumeg.jumeg_noise_reducer import noise_reducer, plot_denoising fname_out = None nr_done = False if do_run: raw, fname_raw = jumeg_base.get_raw_obj(fname, raw=raw) fname_out = jumeg_base.get_fif_name(raw=raw, postfix=fif_postfix, extention=fif_extention, update_raw_fname=False) # --- apply noise reducer for 50 Hz (and harmonics) ''' "signals":[], "noiseref":[], "detrending": null, "tmin": null, "tmax": null, "exclude_artifacts":true, "checkresults":true, "complementary_signal":false, "return_raw":true ''' if (reflp or refhp): # raw, fname_out = noise_reducer_4raw_data(fname,raw=raw,reflp=reflp,refhp=refhp,verbose=verbose,save=False,**parameter) fin = None raw = noise_reducer(fin, raw=raw, reflp=reflp, refhp=refhp, return_raw=True, verbose=verbose, **parameter) parameter['detrending'] = None nr_done = True if refnotch: for refn in refnotch: # raw,fname_out = noise_reducer_4raw_data(None,raw=raw,refnotch=refn,verbose=verbose,save=False,**kwargs['parameter']) raw = noise_reducer(fin, raw=raw, refnotch=refn, return_raw=True, **parameter) parameter['detrending'] = None nr_done = True # raw.info['filename'] = fname_out if not nr_done: return fname_raw, raw if save: fname_out = jumeg_base.apply_save_mne_data(raw, fname=fname_out) if plot: print " --> noise reducer plot power spectrum" from distutils.dir_util import mkpath p, pdf = os.path.split(fname_raw) plot_dir = p + '/plots/' mkpath(plot_dir) fn_power_spect = plot_dir + pdf[:pdf.rfind('-raw.fif' )] + ',denoising' # plot_denoising_4raw_data([fname_raw, fname_out], show=False, fnout=fn_power_spect) plot_denoising([fname_raw, fname_out], show=False, fnout=fn_power_spect) print "---> noise reducer plot :" + fn_power_spect print "---> Done noise reducer: " + fname_out return (fname_out, raw)
fn_raw_nr = fn_raw[:fn_raw.rfind('-raw.fif')] + ',nr-raw.fif' subject = os.path.basename(fn_raw_nr).split('_')[0] fn_per = perf_path + subject noise_reducer(fn_raw, refnotch=50, detrending=False, fnout=fn_raw_nr) noise_reducer(fn_raw_nr, refnotch=60, detrending=False, fnout=fn_raw_nr) #apply noise reducer for frequencies below 5 Hz noise_reducer(fn_raw_nr, reflp=5, fnout=fn_raw_nr) # plot power spectrum fn_power_spect = fn_raw_nr[:fn_raw_nr.rfind('-raw.fif')] + ',denoising' plot_denoising([fn_raw, fn_raw_nr], stim_name=tri_name, event_id=ev_id, show=False, fnout=fn_per, tmin_stim=-0.4, tmax_stim=0.4) ########################################################## # Remove ECG and EOG artifacts #--------------------------------------------------------- if do_rej_HE: #from apply_chopICA import chop_and_apply_ica ecg_ch = 'ECG 001' eog1_ch = 'EOG 001' eog2_ch = 'EOG 002' # use 8Hz to account for slower T waves (ecg) flow_ecg, fhigh_ecg = 8., 20.