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)
Example #2
0
def apply_empty(fname_empty_room, require_filter=True):
    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]
        path_in, name = os.path.split(fn_in)
        fn_empty_nr = fn_in[:fn_in.rfind(ext_empty_raw)] + ',nr-empty.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
        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)
Example #3
0
def apply_noise_reducer(raw_fname, raw=None, **cfg):
    '''
    apply <magic ee noise reducer> thrice to reference channels with different freq parameters
    save PSD plot in subfolder /plots
   
    !!! overwrite raw-obj, works inplace !!!
  
    0) reset bads and check for dead channels
    1) apply nr low pass filter for freq below e.g.: 5Hz  <reflp>
    2) apply nr high pass filter if defined               <reflp>
    3) apply nr notch filter to remove power line noise   <refnotch>
    4) save PSD plot
    
    Parameter:
    -----------
    <noise_reducer> parameter used in this function :
     fname_raw  : input raw filename
     raw        : <None>\n
     cfg        : dict, part of config file <None>
        from config file <noise_reducer> part\n
        reflp      : <None>\n
        refhp      : <None>\n
        refnotch   : <None>\n
        
        plot: True
        plot_show : True
        plot_dir   : subdir to save plots
       
        postfix       : "nr"
        file_extention: ["meeg-raw.fif","rfDC-empty.fif"]
        
        run      : True
        save     : True
        overwrite: True
        
    ToDo add parameter extended
    <noise_reducer> parameter extended
       signals=[], noiseref=[], detrending=None,
       tmin=None, tmax=None,
       exclude_artifacts=True, checkresults=True, return_raw=False,
       complementary_signal=False, fnout=None, verbose=False

    Return:
    --------
     filename,raw-obj
    '''

    #--- init plot
    fname_out = None
    logger.info("  -> apply_noise_reducer file name: {}".format(raw_fname))
    logger.debug("  -> config parameter:\n{}".format(cfg))

    if not jb.check_file_extention(fname=raw_fname,
                                   file_extention=cfg.get("file_extention")):
        return

    if not cfg.get("run"):
        #--- return raw_fname,raw
        return jb.update_and_save_raw(raw,
                                      fin=raw_fname,
                                      fout=None,
                                      save=False,
                                      postfix=cfg.get("postfix", "nr"),
                                      overwrite=cfg.get("overwrite", True))

    logger.info(
        " --> preproc noise_reducer for raw file: {}".format(raw_fname))

    #--- noise reduction
    # apply noise reducer thrice to reference channels with different freq parameters
    # !!! overwrite raw-obj !!!
    save = False
    raw_changed = False
    jb.verbose = cfg.get("verbose")

    #--- load raw, reset bads
    raw, raw_fname = jb.get_raw_obj(raw_fname, raw=raw, reset_bads=True)

    #--- check dead channes and mark them as bad
    jb.picks.check_dead_channels(raw=raw)

    #--- start plot denoising orig raw psd, avoid reloading raw data
    if cfg.get("plot"):
        jplt = JuMEG_PLOT_PSD(n_plots=2, name="denoising", verbose=True)
        jplt.plot(raw,
                  title="orig: " + os.path.basename(raw_fname),
                  check_dead_channels=False)

#--- with redirect stdout/err
    with jumeg_logger.StreamLoggerSTD(label="noise_reducer"):
        #--- 1 nr low pass filter for freq below 5 hz
        if cfg.get("reflp"):
            raw = noise_reducer(None,
                                raw=raw,
                                reflp=cfg.get("reflp"),
                                return_raw=True,
                                verbose=cfg.get("verbose"),
                                exclude_artifacts=False)
            raw_changed = True
    #--- 2 nr high pass filter
        if cfg.get("refhp"):
            raw = noise_reducer(None,
                                raw=raw,
                                reflp=cfg.get("refhp"),
                                return_raw=True,
                                verbose=cfg.get("verbose"),
                                exclude_artifacts=False)
            raw_changed = True
    #--- 3  nr notch filter to remove power line noise
        if cfg.get("refnotch"):
            raw = noise_reducer(None,
                                raw=raw,
                                refnotch=cfg.get("refnotch"),
                                fnout=None,
                                return_raw=True,
                                verbose=cfg.get("verbose"),
                                exclude_artifacts=False)
            raw_changed = True

#--- save and update filename in raw
    if cfg.get("save"):
        save = raw_changed

    #--- update filename in raw and save if save
    fname_out, raw = jb.update_and_save_raw(raw,
                                            fin=raw_fname,
                                            fout=None,
                                            save=save,
                                            update_raw_filenname=True,
                                            postfix=cfg.get("postfix", "nr"),
                                            overwrite=cfg.get(
                                                "overwrite", True))

    #--- plot results, avoid reloading raw data
    if cfg.get("plot"):
        jplt.plot(raw,
                  title="denoised: " + os.path.basename(fname_out),
                  check_dead_channels=False)
        if cfg.get("plot_show"):
            jplt.show()
        jplt.save(fname=fname_out, plot_dir=cfg.get("plor_dir", "plots"))

    if fname_out:
        return fname_out, raw
    else:
        raise Exception("---> ERROR file name not defined !!!")
Example #4
0
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)
Example #5
0
def apply_noise_reducer(raw_fname=None,
                        raw=None,
                        config=None,
                        label='noise reducer',
                        fname_out=None):
    '''
    apply <magic ee noise reducer> thrice to reference channels with different freq parameters
    save PSD plot in subfolder /plots

    !!! overwrite raw-obj, works inplace !!!

    0) reset bads and check for dead channels
    1) apply nr low pass filter for freq below e.g.: 5Hz  <reflp>
    2) apply nr high pass filter if defined               <reflp>
    3) apply nr notch filter to remove power line noise   <refnotch>
    4) save PSD plot


    IN config
        noise_reducer:
          file_extention:
          - meeg-raw.fif
          - rfDC-empty.fif
          fmax: 300
          noiseref_hp:
          - RFG ...
          overwrite: false
          plot: true
          plot_dir: report
          plot_show: false
          postfix: nr
          refhp: 0.1
          reflp: 5.0
          refnotch:
          - 50.0
          - 100.0
          - 150.0
          - 200.0
          - 250.0
          - 300.0
          - 350.0
          - 400.0
          run: true
          save: true
     
    Parameter:
    -----------
    <noise_reducer> parameter used in this function :
     fname_raw  : input raw filename
     raw        : <None>\n
     cfg        : dict, part of config file <None>
        from config file <noise_reducer> part\n
        reflp      : <None>\n
        refhp      : <None>\n
        refnotch   : <None>\n

        plot: True
        plot_show : True
        plot_dir   : subdir to save plots

        postfix       : "nr"
        file_extention: ["meeg-raw.fif","rfDC-empty.fif"]

        run      : True
        save     : True
        overwrite: True

    ToDo add parameter extended
    <noise_reducer> parameter extended
       signals=[], noiseref=[], detrending=None,
       tmin=None, tmax=None,
       exclude_artifacts=True, checkresults=True, return_raw=False,
       complementary_signal=False, fnout=None, verbose=False

    Return:
    --------
     filename,raw-obj
    '''
    #with JuMEG_PipelineFrame(raw_fname=raw_fname,raw=raw,name="noise reducer",config=cfg) as JPF:

    RawIsChanged = False
    #--- check dead channes and mark them as bad
    jb.picks.check_dead_channels(raw=raw)

    #--- start plot denoising orig raw psd, avoid reloading raw data
    if config.get("plot"):
        jplt = JuMEG_PLOT_PSD(n_plots=3, name="denoising",
                              verbose=True)  #,pick_types=["meg","ref"])
        jplt.plot(raw,
                  color="green",
                  title="REF: " + os.path.basename(raw_fname),
                  check_dead_channels=False,
                  fmax=config.get("fmax"),
                  picks=jb.picks.ref_nobads(raw))
        jplt.plot(raw,
                  color="blue",
                  title="MEG orig: " + os.path.basename(raw_fname),
                  check_dead_channels=False,
                  fmax=config.get("fmax"),
                  picks=jb.picks.meg_nobads(raw))
        #self.picks = jb.picks.meg_nobads(raw))

#--- with redirect stdout/err
    with jumeg_logger.StreamLoggerSTD(label=label):
        #--- 1 nr low pass filter for freq below 5 hz
        if config.get("reflp"):
            raw = noise_reducer(None,
                                raw=raw,
                                reflp=config.get("reflp"),
                                return_raw=True,
                                verbose=config.get("verbose"),
                                exclude_artifacts=False)
            RawIsChanged = True
    #--- 2 nr high pass filter
        if config.get("refhp"):
            raw = noise_reducer(None,
                                raw=raw,
                                reflp=config.get("refhp"),
                                return_raw=True,
                                verbose=config.get("verbose"),
                                exclude_artifacts=False)
            RawIsChanged = True
    #--- 3  nr notch filter to remove power line noise
        if config.get("refnotch"):
            raw = noise_reducer(None,
                                raw=raw,
                                refnotch=config.get("refnotch"),
                                fnout=None,
                                return_raw=True,
                                verbose=config.get("verbose"),
                                exclude_artifacts=False)
            RawIsChanged = True

#--- plot results, avoid reloading raw data
    if config.get("plot"):
        jplt.plot(raw,
                  title="MEG denoised: " + os.path.basename(fname_out),
                  check_dead_channels=False,
                  fmax=config.get("fmax"),
                  picks=jb.picks.meg_nobads(raw))
        if config.get("plot_show"):
            jplt.show()
        fout = jplt.save(fname=fname_out,
                         plot_dir=config.get("plot_dir", "report"))

        #--- update image list in report-config for later update MNE Report
        CFG = jCFG()
        data = None
        report_path = os.path.dirname(fout)
        report_config = os.path.join(
            report_path,
            raw_fname.rsplit("_", 1)[0] + "-report.yaml")

        if not CFG.load_cfg(fname=report_config):
            data = {"noise_reducer": {"files": os.path.basename(fout)}}
        else:
            CFG.config["noise_reducer"] = {"files": os.path.basename(fout)}
        CFG.save_cfg(fname=report_config, data=data)

    return fname_out, raw, RawIsChanged, None
Example #6
0
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)
                                    fir_window='hamming',
                                    fir_design='firwin')
raw_notch.plot_psd(fmin=0.,
                   fmax=100.,
                   tmin=None,
                   tmax=60.,
                   n_fft=None,
                   picks=picks,
                   ax=ax2)
ax2.set_title('Notch filter 50Hz applied')

# powerline removal using noise_reducer
raw_nr_notch = noise_reducer(raw_fname,
                             raw=raw.copy(),
                             detrending=False,
                             reflp=None,
                             refhp=None,
                             refnotch=[50., 100., 150.],
                             return_raw=True,
                             verbose=False)
raw_nr_notch.plot_psd(fmin=0.,
                      fmax=100.,
                      tmin=None,
                      tmax=60.,
                      n_fft=None,
                      picks=picks,
                      ax=ax3)
ax3.set_title('Noise reducer notch filter 50Hz applied')

# remove high freq noise (>0.1Hz) from ref channels
raw_nr2 = noise_reducer(raw_fname,
                        raw=raw_nr_notch,
Example #8
0
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)
Example #9
0
data_path = op.join(get_jumeg_path(), 'data')
print data_path

# example filname
raw_fname = "/Volumes/megraid21/sripad/cau_fif_data/jumeg_test_data/" \
            "109925_CAU01A_100715_0842_2_c,rfDC-raw.fif"

# load the model for artifact rejection
# the details of the model is provided in the x_validation_shuffle_v4_split_23.txt
model_name = op.join(data_path, "dcnn_model.hdf5")

model = load_model(model_name)

# noise reducer
raw_nr = noise_reducer(raw_fname, reflp=5., return_raw=True)

raw_nr = noise_reducer(raw_fname,
                       raw=raw_nr,
                       refhp=0.1,
                       noiseref=['RFG ...'],
                       return_raw=True)

# 50HZ and 60HZ notch filter to remove noise
raw = noise_reducer(raw_fname, raw=raw_nr, refnotch=refnotch, return_raw=True)

picks = mne.pick_types(raw.info,
                       meg=True,
                       eeg=False,
                       eog=False,
                       stim=False,
Example #10
0
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)
Example #11
0
    mne.write_events(fn_eve, eog2_correct_events)

##########################################################
# Noise_reducer for noise around 50Hz, 60Hz and below 5Hz
#---------------------------------------------------------
if do_pre:
    fn_list = glob.glob(subjects_dir + '*[0-9]/MEG/*rfDC,bcc-raw.fif')
    perf_path = subjects_dir + 'rn_perf/'
    set_directory(perf_path)
    for fn_raw in fn_list:
        #import noise_reducer and plot_power_spectrum function
        #apply noise reducer for 50 Hz (and harmonics)
        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)
def apply_noise_reducer(raw_fname=None,
                        raw=None,
                        config=None,
                        label="noise reducer",
                        fname_out=None):
    '''
    apply <magic ee noise reducer> thrice to reference channels with different freq parameters
    save PSD plot in subfolder /plots

    !!! overwrite raw-obj, works inplace !!!

    0) reset bads and check for dead channels
    1) apply nr low pass filter for freq below e.g.: 5Hz  <reflp>
    2) apply nr high pass filter if defined               <reflp>
    3) apply nr notch filter to remove power line noise   <refnotch>
    4) save PSD plot

    Parameter:
    -----------
    <noise_reducer> parameter used in this function :
     fname_raw  : input raw filename
     raw        : <None>\n
     cfg        : dict, part of config file <None>
        from config file <noise_reducer> part\n
        reflp      : <None>\n
        refhp      : <None>\n
        refnotch   : <None>\n

        plot: True
        plot_show : True
        plot_dir   : subdir to save plots

        postfix       : "nr"
        file_extention: ["meeg-raw.fif","rfDC-empty.fif"]

        run      : True
        save     : True
        overwrite: True

    ToDo add parameter extended
    <noise_reducer> parameter extended
       signals=[], noiseref=[], detrending=None,
       tmin=None, tmax=None,
       exclude_artifacts=True, checkresults=True, return_raw=False,
       complementary_signal=False, fnout=None, verbose=False

    Return:
    --------
     filename,raw-obj
    '''
    #with JuMEG_PipelineFrame(raw_fname=raw_fname,raw=raw,name="noise reducer",config=cfg) as JPF:

    RawIsChanged = False
    #--- check dead channes and mark them as bad
    jb.picks.check_dead_channels(raw=raw)

    #--- start plot denoising orig raw psd, avoid reloading raw data
    if config.get("plot"):
        jplt = JuMEG_PLOT_PSD(n_plots=2, name="denoising", verbose=True)
        jplt.plot(raw,
                  title="orig: " + os.path.basename(raw_fname),
                  check_dead_channels=False,
                  fmax=config.get("fmax"))

#--- with redirect stdout/err
    with jumeg_logger.StreamLoggerSTD(label=label):
        #--- 1 nr low pass filter for freq below 5 hz
        if config.get("reflp"):
            raw = noise_reducer(None,
                                raw=raw,
                                reflp=config.get("reflp"),
                                return_raw=True,
                                verbose=config.get("verbose"),
                                exclude_artifacts=False)
            RawIsChanged = True
    #--- 2 nr high pass filter
        if config.get("refhp"):
            raw = noise_reducer(None,
                                raw=raw,
                                reflp=config.get("refhp"),
                                return_raw=True,
                                verbose=config.get("verbose"),
                                exclude_artifacts=False)
            RawIsChanged = True
    #--- 3  nr notch filter to remove power line noise
        if config.get("refnotch"):
            raw = noise_reducer(None,
                                raw=raw,
                                refnotch=config.get("refnotch"),
                                fnout=None,
                                return_raw=True,
                                verbose=config.get("verbose"),
                                exclude_artifacts=False)
            RawIsChanged = True

#--- plot results, avoid reloading raw data
    if config.get("plot"):
        jplt.plot(raw,
                  title="denoised: " + os.path.basename(fname_out),
                  check_dead_channels=False,
                  fmax=config.get("fmax"))
        if config.get("plot_show"):
            jplt.show()
        jplt.save(fname=fname_out, plot_dir=config.get("plor_dir", "plots"))

    return fname_out, raw, RawIsChanged