def check_path(self): """ Helper to check path for obj.filename assert error if no such file Results ------- True """ #jb.Log.info(" --> check path & file: " + self.system + " file name: " + str(self.filename)) return jb.isFile(self.filename, head=self.system, exit_on_error=True)
def apply_import_to_fif(opt): """ apply jumeg import 4D/BTi data to FIF convert 4D-file to FIF format using mne save raw-obj to new file jumeg wrapper for <mne.io.read_raw_bti> https://martinos.org/mne/stable/generated/mne.io.read_raw_bti.html#mne.io.read_raw_bti Parameter --------- opt Outputfile ---------- <wawa>, -raw.fif Returns ------- return mne.io.Raw instance """ # --- ck file fullpath fpdf = jb.isFile(opt.pdf_fname, path=opt.pdf_stage, head="apply_import_to_fif => file check: 4D/BTI <raw>", exit_on_error=True) if not fpdf: return fcfg = jb.isFile(opt.config_fname, path=opt.pdf_stage, head="apply_import_to_fif:=> file check: 4D/BTI <config>", exit_on_error=True) if not fcfg: return # ToDo ck if headshape file must exist # pt.headshape_fake and touch hs #--- #fhs = jb.isFile(opt.head_shape_fname,path=opt.pdf_stage,head="apply_import_to_fif:=> file check: 4D/BTI <head shape>",exit_on_error=True) #if not fhs : return #--- mk fif ouput file name # fpdf="/mnt/meg_store2/megdaw_data21/211063/INTEXT01//18-11-15@11:20/1/c,rfDC" # 211063_INTEXT01_181115_1120_1_c,rfDC-raw.fif size [Mb]: 703.534 #---> FIF / MNE path : /mnt/meg_store1/exp/INTEXT/mne/211063/INTEXT01/181115_1120/1/ fif_out = os.path.expandvars(os.path.expanduser(opt.fif_stage)) + "/" #if opt.fif_path_prefix: # fif_out += opt.fif_path_prefix+"/" fif_out += "/".join(fpdf.split("/")[-5:-1]) + "/" fif_out += "_".join(fpdf.split("/")[-5:]) fif_out = re.sub('/+', '/', fif_out).replace('@', "_").replace(":", "").replace("-", "") fif_out += opt.fif_extention if not opt.overwrite: if os.path.isfile(fif_out): if opt.verbose: logger.info( "---> 4D/BTI file : {}\n".format(fpdf) + " --> FIF / MNE file: {} size [Mb]: {}\n".format( os.path.basename(fif_out), os.path.getsize(fif_out) / 1024.0**2) + " --> FIF / MNE path: {}\n".format(os.path.dirname( fif_out)) + " -> !!! FIF File exist will not overwrite [overwrite={}\n" .format(opt.overwrite) + "===> Done") return # --- check and set opt parameter kwargs = dict() if jb.isNotEmpty(opt.rotation_x): kwargs["rotation_x"] = float(opt.rotation_x) if jb.isNotEmpty(opt.translation): kwargs["translation"] = float(opt.translation) if jb.isNotEmpty(opt.ecg_ch): kwargs["ecg_ch"] = opt.ecg_ch if jb.isNotEmpty(opt.eog_ch): kwargs["eog_ch"] = opt.eog_ch #--- run if opt.run: #print("RUN") # defaults mne017 # pdf_fname, config_fname='config', head_shape_fname='hs_file', rotation_x=0.0, translation=(0.0, 0.02, 0.11), convert=True, # rename_channels=True, sort_by_ch_name=True, ecg_ch='E31', eog_ch=('E63', 'E64'), preload=False, verbose=None try: raw = mne.io.read_raw_bti(fpdf, config_fname=opt.config_fname, head_shape_fname=opt.head_shape_fname, preload=opt.preload, convert=opt.convert, rename_channels=opt.rename_channels, sort_by_ch_name=opt.sort_by_ch_name, verbose=opt.verbose, **kwargs) except: logger.exception( "---> error in mne.io.read_raw_bti:\n -> file: {}".format( fpdf)) #--- make output filename and save if opt.save: fif_out = jb.apply_save_mne_data(raw, fname=fif_out, overwrite=opt.overwrite) if opt.verbose: logger.info("===> 4D/BTI file : {}\n".format(fpdf) + " --> FIF / MNE file: {} size [Mb]: {}\n".format( os.path.basename(fif_out), os.path.getsize(fif_out) / 1024.0**2) + " --> FIF / MNE path: {}\n".format( os.path.dirname(fif_out))) return raw
def _apply_fit(self, raw_chop=None, chop=None, idx=None): """ call to jumeg fit_ica raw_chop = None chop = None ToDo if not overwrite if ICA file exist: load ICA else calc ICA :return: ICA obj, ica-filename """ ica_obj = None self._ics_found_svm = None fname_ica, fname = self._get_chop_name(raw_chop, chop=None) msg = [ "start ICA FIT chop: {} / {}".format(idx + 1, self.Chopper.n_chops), " --> chop id : {}".format(chop), " -> ica fname : {}".format(fname_ica), " -> ica chop path: {}".format(self.path_ica_chops), " -> raw filename : {}".format(fname) ] logger.info("\n".join(msg)) #--- ck for ovewrite & ICA exist load_from_disk = False if not self.cfg.fit.overwrite: load_from_disk = jb.isFile(fname_ica, path=self.path_ica_chops) if load_from_disk: # self._ica_obj,fname_ica = jb.get_raw_obj(fname_ica,path=self.path_ica_chops) ica_obj, fname_ica = jb.get_raw_obj(fname_ica, path=self.path_ica_chops) logger.info( "DONE LOADING ICA chop form disk: {}\n -> ica filename: {}". format(chop, fname_ica)) else: if self.useArtifactRejection: with jumeg_logger.StreamLoggerSTD(label="ica fit"): ica_obj = fit_ica( raw=raw_chop, picks=self.picks, reject=self.CFG.GetDataDict(key="reject"), ecg_ch=self.cfg.ecg.ch_name, ecg_thresh=self.cfg.ecg.thresh, flow_ecg=self.cfg.ecg.flow, fhigh_ecg=self.cfg.ecg.fhigh, #--- eog_hor=self.cfg.eog.hor_ch, eog_ver=self.cfg.eog.ver_ch, flow_eog=self.cfg.eog.flow, fhigh_eog=self.cfg.eog.fhigh, eog_thresh=self.cfg.eog.thresh, #--- use_jumeg=self.cfg.ecg.use_jumeg, random_state=self.cfg.random_state) ica_obj.exclude = list(set(ica_obj.exclude)) if self.useSVM: if not ica_obj: logger.info('SVM start ICA FIT: init ICA object') #--- !!! ToDo put parameter in CFG file ica_obj = ICA(method='fastica', n_components=40, random_state=42, max_pca_components=None, max_iter=5000, verbose=False) ica_obj.fit(raw_chop, picks=self.picks, decim=None, reject=self.CFG.GetDataDict(key="reject"), verbose=True) else: logger.info('SVM ICA Obj start') #--- !!! do_copy = True => resample ica_obj, _ = self.SVM.run(raw=self.raw, ICA=ica_obj, picks=self.picks, do_crop=False, do_copy=True) logger.info('DONE SVM ICA FIT: apply ICA.fit') #-- save ica object if self.cfg.fit.save and not load_from_disk: logger.info("saving ICA chop : {} / {}\n".format( idx + 1, self.Chopper.n_chops) + " -> ica filename : {}".format(fname_ica)) ica_obj.save(os.path.join(self.path_ica_chops, fname_ica)) logger.info( "done ICA FIT for chop: {}\n".format(chop) + " -> raw chop filename : {}\n".format(fname_ica) + "-" * 30 + "\n" + " -> ICs found JuMEG/MNE : {}\n".format(self.SVM.ICsMNE) + " -> ICs found SVM : {}\n".format(self.SVM.ICsSVM) + " -> ICs excluded : {}\n".format(ica_obj.exclude) + "-" * 30 + "\n" + " -> save ica fit : {}".format(self.cfg.fit.save)) return ica_obj, fname_ica
def raw_fname(self, v): self._raw_fname = jb.isFile(v, path=self.path)
def apply(self, **kwargs): """ wrapper function for MNE version 19.2 notch filter cls data in raw-obj will be overwritten filename is updated in raw-obj call MNE <raw.notch_filter> notch_filter(self,freqs,picks=None,filter_length='auto',notch_widths=None,trans_bandwidth=1.0,n_jobs=1,method='fir', iir_params=None,mt_bandwidth=None,p_value=0.05,phase='zero',fir_window='hamming',fir_design='firwin', pad='reflect_limited',verbose=None)[source] :param kwargs: Example -------- -> notch all chanels 50.0,100.0,150.0 Hz except STIM from jumeg.base.jumeg_base import jumeg_base as jb from jumeg.filter.jumeg_mne_filter import JUMEG_NOTCH_FILTER jNFI = JUMEG_NOTCH_FILTER() fname = jNFI.apply( freqs = [50.0,100.0,150.0] picks = jb.picks.exclude_trigger(raw) ) Example -------- -> filter all chanels 0.1 -45.0 Hz except STIM from jumeg.filter.jumeg_mne_filter import JUMEG_FILTER jFI = JUMEG_FILTER() fname = jFI.apply( flow = 0.1, fhigh = 45.0, save = True, raw = raw, picks = jb.picks.exclude_trigger(raw) ) :return: fname """ self._update_from_kwargs(**kwargs) self._is_filtered = False self._is_reloaded = False v = jb.verbose jb.verbose = self.verbose logger.info("---> Filter start: {}".format(self.fname)) self._update_postfix() fname, ext = self.fname.rsplit('-', 1) #raw.fif' fname += "," + self.postfix + "-" + ext #--- ck if load from disk if not self.overwrite: if jb.isFile(fname): logger.debug("Notch Filtered RAW reloading from disk ...") self.raw, fname = jb.get_raw_obj(fname, None) self._is_filtered = True self._is_reloaded = True if not self._is_filtered: logger.info("Notch Filter start MNE filter ...") if isinstance(self.picks, (list, np.ndarray)): picks = self.picks else: logger.warning( "picks not defined : excluding channel group <stim> and <resp>" ) picks = jb.picks.exclude_trigger(self.raw) self.raw.notch_filter(self.freqs, picks=picks, filter_length=self.filter_length, notch_widths=self.notch_widths, trans_bandwidth=self.trans_bandwidth, n_jobs=self.n_jobs, method=self.method, iir_params=self.iir_params, mt_bandwidth=self.mt_bandwidth, p_value=self.p_value, phase=self.phase, fir_window=self.fir_window, fir_design=self.fir_design, pad=self.pad, verbose=self.verbose) self._fname_orig = jb.get_raw_filename(self.raw) self._is_filtered = True if self.save: logger.info("Notch Filter saving data") fname = jb.apply_save_mne_data(self.raw, fname=fname, overwrite=True) else: jb.set_raw_filename(self.raw, fname) logger.info("Notch Filter done: {}\n".format(self.fname) + " -> reloaded from disk: {}".format(self._is_reloaded)) jb.verbose = v return fname
def apply(self, **kwargs): """ wrapper function for MNE filter cls raw is filtered with MNE filter function inplace data in raw-obj will be overwritten filename is updated in raw-obj call MNE filter e.g.: raw.filter(l_freq=flow,h_freq=fhigh,picks=picks) 208497_INTEXT01_190103_1010_1_c,rfDC,meeg,nr,bcc,int,ar :param kwargs: flow,fhigh,raw,picks Example -------- -> filter all chanels 0.1 -45.0 Hz except STIM from jumeg.base.jumeg_base import jumeg_base as jb from jumeg.filter.jumeg_mne_filter import JUMEG_FILTER jFI = JUMEG_FILTER() fname = jFI.apply( flow = 0.1, fhigh = 45.0, save = True, raw = raw, picks = jb.picks.exclude_trigger(raw) ) :return: fname """ self._update_from_kwargs(**kwargs) self._is_filtered = False self._is_reloaded = False jb.verbose = self.verbose logger.info("Filter start: {}".format(self.fname)) fname = self.get_filter_filename() #--- ck if load from disk if not self.overwrite: if jb.isFile(fname): logger.debug("Filtered RAW reloading from disk ...") self.raw, fname = jb.get_raw_obj(fname, None) self._fname_orig = fname if self.annotations: self.raw.set_annotations(self.annotations) self._is_filtered = True self._is_reloaded = True if not self._is_filtered: logger.info("Filter start MNE filter ...") if isinstance(self.picks, (list, np.ndarray)): picks = self.picks else: logger.warning( "WARNING: picks not defined : excluding channel group <stim> and <resp>" ) picks = jb.picks.exclude_trigger(self.raw) if self.dcoffset: self.apply_dcoffset() self.raw.filter(l_freq=self.flow, h_freq=self.fhigh, picks=picks) self._fname_orig = jb.get_raw_filename(self.raw) self._is_filtered = True if self.annotations: self.raw.set_annotations(self.annotations.copy()) fname, _ = jb.update_and_save_raw(self.raw, fout=fname, save=self.save, overwrite=True, update_raw_filename=True) if self.verbose: self.GetInfo() return fname