Esempio n. 1
0
    def __init__(self, **kwargs):
        self._init(**kwargs)
        self._JuMEG_EPOCHER = JuMEG_Epocher()

        self._CFG = jCFG(**kwargs)
        self._plot_dir = "report"
        self.report_key = "epocher"
 def __init__(self,**kwargs):
     super().__init__()
     self.init()
     self._jumeg_cfg = {"run":True,"save":True,"overwrite":False,"path":"report",
                        "noise_reducer":{"run":True,"extention":"nr-raw.png"},
                        "ica":{"run":True,"extention":"ar.png"}}
     self._path = "."
     self.report_cfg_extention = "-report.yaml"
     self._REPORT = MNE_REPORT(**kwargs)
     self._REPORT_CFG = jCFG()
Esempio n. 3
0
 def _update_report(self, data):
     """
     
     :param fimages:
     :return:
     """
     #--- update report config
     CFG = jCFG()
     report_config = os.path.join(
         self.plot_dir,
         os.path.basename(self.raw_fname).rsplit("_", 1)[0] +
         "-report.yaml")
     d = None
     if not CFG.load_cfg(fname=report_config):
         d = {"ica": data}
     else:
         CFG.config["ica"] = data
     CFG.save_cfg(fname=report_config, data=d)
Esempio n. 4
0
    def __init__(self, **kwargs):
        super().__init__()

        self.PreFilter = JuMEG_MNE_FILTER()
        self.Chopper = JuMEG_PIPELINES_CHOPPER()
        self.ICAPerformance = JuMEG_ICA_PERFORMANCE()
        self.SVM = JuMEG_ICA_SVM()

        self.useSVM = False
        self.verbose = False
        self.debug = False
        self.report_key = "ica"

        self._CFG = jCFG(**kwargs)
        self._plot_dir = None
        self._ics_found_svm = None

        self._clear()
Esempio n. 5
0
def test1():
    #--- init/update logger
    jumeg_logger.setup_script_logging(logger=logger)

    raw = None
    stage = "$JUMEG_PATH_LOCAL_DATA/exp/MEG94T/mne"
    fcfg = os.path.join(stage, "meg94t_config01.yaml")
    fpath = "206720/MEG94T0T2/130820_1335/1/"

    path = os.path.join(stage, fpath)
    raw_fname = "206720_MEG94T0T2_130820_1335_1_c,rfDC,meeg,nr,bcc,int-raw.fif"

    logger.info("JuMEG Pipeline ICA Performance ICA mne-version: {}".format(
        mne.__version__))

    f = os.path.join(path, raw_fname)
    raw, raw_fname = jb.get_raw_obj(f, raw=None)

    raw_path = os.path.dirname(raw_fname)
    #--- get picks from raw
    picks = jb.picks.meg_nobads(raw)

    #---
    CFG = jCFG()
    CFG.update(config=fcfg)
    config = CFG.GetDataDict("ica")
    #--
    ICAPerformance = JuMEG_ICA_PERFORMANCE(
        raw=raw,
        path=path,
        fname=raw_fname,
    )

    #--- find ECG
    ICAPerformance.ECG.find_events(raw=raw, **config.get("ecg"))
    ICAPerformance.ECG.GetInfo(debug=True)
    #--- find EOG
    ICAPerformance.EOG.find_events(raw=raw, **config.get("eog"))
    ICAPerformance.EOG.GetInfo(debug=True)
Esempio n. 6
0
def test2():
    #--- init/update logger
    jumeg_logger.setup_script_logging(logger=logger)

    raw = None
    stage = "$JUMEG_PATH_LOCAL_DATA/exp/MEG94T/mne"
    fcfg = os.path.join(stage, "meg94t_config01.yaml")
    fpath = "206720/MEG94T0T2/130820_1335/1/"
    path = os.path.join(stage, fpath)

    #fraw   =  "206720_MEG94T0T2_130820_1335_1_c,rfDC,meeg,nr,bcc,int,000516-000645-raw.fif"
    #fraw_ar = "206720_MEG94T0T2_130820_1335_1_c,rfDC,meeg,nr,bcc,int,000516-000645,ar-raw.fif"

    fraw = "206720_MEG94T0T2_130820_1335_1_c,rfDC,meeg,nr,bcc,int,fibp0.10-45.0-raw.fif"
    fraw_ar = "206720_MEG94T0T2_130820_1335_1_c,rfDC,meeg,nr,bcc,int,fibp0.10-45.0,ar-raw.fif"

    logger.info("JuMEG Pipeline ICA Performance ICA mne-version: {}".format(
        mne.__version__))
    #---
    f = os.path.join(path, fraw)
    raw, raw_fname = jb.get_raw_obj(f, raw=None)
    raw_path = os.path.dirname(raw_fname)
    picks = jb.picks.meg_nobads(raw)
    #---
    f = os.path.join(path, fraw_ar)
    raw_ar, raw_ar_fname = jb.get_raw_obj(f, raw=None)

    #--- read config
    CFG = jCFG()
    CFG.update(config=fcfg)
    config = CFG.GetDataDict("ica")

    #
    jIP = JuMEG_ICA_PERFORMANCE(raw=raw, raw_clean=raw_ar, picks=picks)
    #jIP.report()

    fout = raw_fname.rsplit("-", 1)[0] + "-ar"
    jIP.plot(verbose=True, fout=fout)
Esempio n. 7
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
Esempio n. 8
0
def test():
    '''
    from jumeg.base.jumeg_base                                 import jumeg_base as jb
    from jumeg.base.pipelines.jumeg_pipelines_ica_perfromance  import ICAPerformance
    from jumeg.base.pipelines.jumeg_base_pipelines_chopper import JuMEG_PIPELINES_CHOPPER,copy_crop_and_chop,concat_and_save
    
    '''
    #---
    from jumeg.base.jumeg_base_config import JuMEG_CONFIG as jCFG

    verbose = True
    debug = False

    stage = "$JUMEG_TEST_DATA/mne/201772/INTEXT01/190212_1334/2"
    fn = "201772_INTEXT01_190212_1334_2_c,rfDC,meeg,nr,bcc,int-raw.fif"
    fn = "201772_INTEXT01_190212_1334_2_c,rfDC,meeg,nr,bcc,int,fibp0.10-45.0,ar-raw.fif"

    stage = '$JUMEG_TEST_DATA/mne/212842/INTEXT01/190430_1001/4'
    fn = '212842_INTEXT01_190430_1001_4_c,rfDC,meeg,nr,bcc,int,fibp0.10-45.0,ar-raw.fif'
    fin = os.path.join(stage, fn)

    raw, fname = jb.get_raw_obj(fname=fin)

    #--- ck for annotations in raw
    try:
        annota = raw.annotations
    except:
        from jumeg.base.pipelines.jumeg_pipelines_ica_perfromance import ICAPerformance
        IP = ICAPerformance()
        #--- find ECG
        IP.ECG.find_events(raw=raw)
        IP.ECG.GetInfo(debug=True)
        #--- find EOG
        IP.EOG.find_events(raw=raw)
        IP.EOG.GetInfo(debug=True)

#--- read epocher config

#--- define template
    template_path = "$JUMEG_PATH_TEMPLATE_EPOCHER"
    template_name = "INTEXT"
    template_extention = "jumeg_epocher_template.yaml"

    #--- CFG
    CFG = jCFG()
    fcfg = os.path.join(template_path,
                        template_name + "_" + template_extention)

    CFG.update(config=fcfg)
    condition_list = []

    for k, itm in CFG.config.items():
        if itm.get("run"):
            condition_list.append(k)

    if not condition_list: return

    epocher_path = os.path.join(os.path.dirname(fname), "epocher")
    hdf_path = epocher_path

    evt_param = {
        "condition_list": condition_list,
        "template_path": template_path,
        "template_name": template_name,
        "use_yaml": True,
        "hdf_path": hdf_path,
        "verbose": verbose,
        "debug": debug
    }

    ep_param = {
        "condition_list": condition_list,
        "template_path": template_path,
        "template_name": template_name,
        "hdf_path": hdf_path,
        "save_raw": True,
        "verbose": verbose,
        "debug": debug,
        "event_extention": ".eve",
        "output_mode": {
            "events": True,
            "epochs": True,
            "evoked": True,
            "annotations": True,
            "stage": epocher_path,
            "use_condition_in_path": True
        }
        # "weights"       :{"mode":"equal","method":"median","skip_first":null}
        # "exclude_events":{"eog_events":{"tmin":-0.4,"tmax":0.6} } },
    }

    jEP = JuMEG_PIPELIENS_EPOCHER()

    jEP.run(raw=raw,
            fname=fname,
            ep_param=ep_param,
            evt_param=evt_param,
            verbose=True,
            debug=False,
            show=True)

    #---ToDo
    # use mne plots
    '''