Commit a587d78f authored by Boers, Frank's avatar Boers, Frank
Browse files

ar-plot bug fix events

parent 0430a91c
......@@ -26,7 +26,8 @@ from distutils.dir_util import mkpath
# fnconfig = 'config_4D_INTEXT.yaml'
# fnconfig = 'config_MEGIN.yaml'
# fnconfig = 'config_CTF_Paris.yaml'
fnconfig = 'config_CTF_Philly.yaml'
fnconfig ='config_4D_Juelich.yaml'
# fnconfig = 'config_CTF_Philly.yaml'
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
......@@ -60,8 +61,15 @@ for fname in fnames:
# check IC labels (and apply corrections)
name = op.basename(fname[:-4])
print ('>>> working on %s' % name)
tr_name="TR-"+name
figs, captions = dcnn.plot_ica_traces(fname)
report.add_figs_to_section(figs, captions=captions, section=name, replace=True)
ar_name = "AR-"+name
figs, captions = dcnn.plot_artifact_performance()
report.add_figs_to_section(figs, captions=captions, section=name, replace=True)
report.save(fnreport + '.h5', overwrite=True)
report.save(fnreport + '.html', overwrite=True, open_browser=True)
......
......@@ -21,7 +21,7 @@ import os.path as op
from mne.report import Report
from distutils.dir_util import mkpath
__version__= "2020.08.04.001"
__version__= "2020.08.05.001"
logger = setup_script_logging()
......@@ -36,8 +36,8 @@ fnconfig ='config_4D_Juelich.yaml'
# fnconfig = 'config_CTF_Philly.yaml'
basedir = "$JUMEG_PATH_LOCAL_DATA"+"/gDCNN"
fnz = "ica_labeled/Juelich/201772_INTEXT01_190212_1334_2_c,rfDC,meeg,nr,bcc,int-raw-gdcnn.npz"
fnz = op.join(basedir,fnz)
#fnz = "ica_labeled/Juelich/201772_INTEXT01_190212_1334_2_c,rfDC,meeg,nr,bcc,int-raw-gdcnn.npz"
#fnz = op.join(basedir,fnz)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# load config details and file list to process
......@@ -46,20 +46,23 @@ cfg = DCNN_CONFIG(verbose=True)
cfg.load(fname=fnconfig)
dcnn = DCNN(**cfg.config)
# dcnn.verbose = True
dcnn.verbose = True
# -- update basedir
dcnn.path.basedir = basedir
# dcnn.path.get_info()
path_in = dcnn.path.data_train
# path_in = dcnn.path.data_train
path_in = basedir + "/meg_rawdata/Freeviewing"
path_out = dcnn.path.report
fnames = find_files(path_in, pattern='*.npz')
#fnames = find_files(path_in, pattern='*.npz')
fnames = [basedir + "/meg_rawdata/Freeviewing/" + "203404_FREEVIEW01_180323_1410_1_c,rfDC,meeg_bcc,tr,nr-raw-gdcnn.npz"]
msg=["ICA Check & AR performance",
" -> path_in : {}".format(path_in),
" -> path_out: {}".format(path_out),
" --> files:\n -> {}".format("\n -> ".join(fnames))
" --> files : {}\n -> {}".format( len(fnames),"\n -> ".join(fnames))
]
logger.info("\n".join(msg))
......@@ -75,26 +78,25 @@ logger.info("START MNE report: {}".format(fnreport))
# loop across files to process
#
#++++++++++++++++++++++++++++++++++++++++++++++
for fname in fnames:
dcnn.load_gdcnn(fname)
# check IC labels (and apply corrections)
name = op.basename(fname).split(",")[0]
msg = ["TR/AR MNE report name: {}".format(name),
" -> input file: {}".format(fname)]
logger.info( "\n".join(msg) )
dcnn.load_gdcnn(fname)
# -- plt ICs
tr_name="TR-"+name
figs, captions = dcnn.plot_ica_traces(fname)
report.add_figs_to_section(figs, captions=captions, section=tr_name, replace=True)
# -- plt ECG/EOG AR
ar_name = "AR-"+name
figs, captions = dcnn.plot_artifact_performance()
report.add_figs_to_section(figs, captions=captions, section=ar_name, replace=True)
report.save(fnreport + '.h5', overwrite=True)
report.save(fnreport + '.html', overwrite=True, open_browser=True)
logger.info("\nDONE MNE report: {}".format(fnreport))
......
......@@ -19,7 +19,7 @@ from mne.viz import topomap
from dcnn_utils import logger, isFile, isPath, rescale, read_raw, get_raw_filename,expandvars
from dcnn_utils import apply_noise_reduction_4d,fig2rgb_array
__version__ = "2020.08.04.001"
__version__ = "2020.08.05.001"
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# SLOTS class
......@@ -30,9 +30,8 @@ class _SLOTS(object):
__slots__ = ('_verbose','_cls_name')
def __init__(self,**kwargs):
super().__init__()
self.verbose = False
self._init()
self._verbose = False
self._update_from_kwargs(**kwargs)
@property
......@@ -460,6 +459,8 @@ class DCNN_PATH(_SLOTS):
def init(self,**kwargs): # set overwrite=True
self.clear()
self._update_from_kwargs(**kwargs)
if not kwargs:
kwargs=dict()
#-- set overwrite to True for real init if not set
self.overwrite = kwargs.get("overwrite",True)
self.add_basedir = kwargs.get("add_basedir",True)
......@@ -475,14 +476,14 @@ class DCNN_PATH(_SLOTS):
#-- set overwrite to False for update if not set
self.overwrite = kwargs.get("overwrite",False)
#self.__set_slots_attr_hidden(**kwargs) # sets hidden slots e.g. _data-xyz
@property
def basedir(self): return self._basedir
# self._get_path(self._basedir)
@basedir.setter
def basedir(self,v):
self._set_path(v.rstrip("/"),"basedir")
if v:
self._set_path(v.rstrip("/"),"basedir")
@property
def data_meg(self):
......@@ -793,6 +794,7 @@ class DCNN_MEG(_SLOTS):
def __init__(self,**kwargs):
super().__init__(**kwargs)
if not kwargs: kwargs=dict()
self._PICKS = PICKS( raw=kwargs.get("raw") )
self.raw = kwargs.get("raw")
self._cls_name = "MEG"
......@@ -1076,6 +1078,9 @@ class _ICA_CHOP(_SLOTS):
def init(self,**kwargs):
self.clear()
self._update_from_kwargs(**kwargs)
if not kwargs: return
if "exclude" in kwargs:
self.exclude.init(**kwargs.get("exclude"))
......@@ -1125,7 +1130,9 @@ class _ICA_TOPO(_SLOTS):
def init(self, **kwargs):
self.clear()
if not kwargs: return
self._update_from_kwargs(**kwargs)
self._data = kwargs.get("data")
self._images = kwargs.get("images")
self._images_head = kwargs.get("images_head")
......@@ -1412,6 +1419,7 @@ class _ICA_SCORE(_SLOTS):
def init(self, **kwargs):
self.clear()
if not kwargs: return
self._update_from_kwargs(**kwargs)
self._ecg_ctps = kwargs.get("ecg_ctps")
self._ecg_corr = kwargs.get("ecg_corr")
......@@ -1461,6 +1469,7 @@ class DCNN_ICA(_SLOTS):
def init(self,**kwargs):
self.clear()
if not kwargs: return
self._update_from_kwargs(**kwargs)
if "topo" in kwargs:
self.topo.init( **kwargs.get("topo") )
......@@ -1578,6 +1587,7 @@ class DCNN_SOURCES(_SLOTS):
def init(self, **kwargs):
self.clear()
if not kwargs: return
self._update_from_kwargs(**kwargs)
if "data_ica" in kwargs:
self._data_ica = kwargs.get("data_ica")
......
......@@ -18,7 +18,7 @@ from dcnn_utils import collect_source_info
from dcnn_base import DCNN_PATH,DCNN_MEG, DCNN_ICA, DCNN_SOURCES
from dcnn_performance import PERFORMANCE_PLOT
__version__= "2020.08.04.001"
__version__= "2020.08.05.001"
# -----------------------------------------------------
# compute downsampling frequency and final chop length
......@@ -604,6 +604,8 @@ class DCNN(object):
"name : {}".format(name)]
logger.info("\n -> ".join(msg))
aux_labels = self.meg.picks.aux_labels
toffset = 0.0
for ichop in range( self.ica.chop.n_chop ):
captions.append('AR %s ... Chop#%d' % (name.split(",")[0], ichop))
......@@ -614,7 +616,6 @@ class DCNN(object):
# -- get data from sources
data_ica = self.sources.data_ica[ichop]
data_aux = self.sources.data_aux[ichop]
aux_labels = self.meg.picks.aux_labels
if data_aux.shape[0] != len(aux_labels):
logger.exception("ERROR : channel count in aux data [{}] is not equal to counts in aux labels [{}]".
......@@ -637,7 +638,8 @@ class DCNN(object):
n_ecg = len(self.ica.chop.exclude.ecg[ichop])
n_eog = len(self.ica.chop.exclude.eog[ichop])
suptitle = "ECG: cnts: {} ICs: {}".format( n_ecg,",".join( [str(i) for i in self.ica.chop.exclude.ecg[ichop] ]))
trange = "Time: {:.3f} <> {:.3f}".format(toffset,raw_chop.times[-1]+toffset)
suptitle = trange +" ECG: cnts: {} ICs: {}".format( n_ecg,",".join( [str(i) for i in self.ica.chop.exclude.ecg[ichop] ]))
suptitle += " "
suptitle += "EOG: cnts: {} ICs: {}".format( n_eog,",".join( [str(i) for i in self.ica.chop.exclude.eog[ichop] ]))
......@@ -650,10 +652,15 @@ class DCNN(object):
# -- loop for ECG,EOGs
for idx in range( self.PerformancePlot.n_cols ):
evt = events[idx]
self.PerformancePlot.plot(ch_name=artifacts[idx],events=evt,event_id=evt[0,2],fig_nr=ichop+1,suptitle=suptitle)
evt = events[idx]
evt_id = evt[0,2]
if toffset > 0:
evt[:,0] -= int( toffset * raw_chop.info['sfreq'] ) # correct for chop sample offset
self.PerformancePlot.plot(ch_name=artifacts[idx],events=evt,event_id=evt_id,fig_nr=ichop+1,suptitle=suptitle)
self.PerformancePlot.idx += 1
toffset += raw_chop.times[-1]
return self.PerformancePlot.figures, captions
......
......@@ -39,6 +39,11 @@ __version__= "2020.08.04.001"
class CalcSignal(_SLOTS):
def _get_epochs(self,raw,events,event_id,tmin,tmax,picks):
reject = None
return mne.Epochs(raw,events,event_id=event_id,tmin=tmin,tmax=tmax,picks=picks,
baseline=None,reject=reject)
def calc_rms(self,data,average=None,rmsmean=None):
''' Calculate the rms value of the signal.
Ported from Dr. J. Dammers IDL code.
......@@ -86,11 +91,9 @@ class CalcSignal(_SLOTS):
signal = None
range = None
times = None
if not isinstance(picks,(list,np.ndarray)):
picks = jb.picks.meg_nobads(raw)
#--- RAW mk epochs + average
ep = mne.Epochs(raw,events,event_id=event_id,tmin=tmin,tmax=tmax,picks=picks)
ep = self._get_epochs(raw,events,event_id,tmin,tmax,picks)
if len(picks) > 1:
avg = ep.average()
times = avg.times
......@@ -117,16 +120,10 @@ class CalcSignal(_SLOTS):
signal, min/max-range, times
"""
signal = None
range = None
times = None
#if not isinstance(picks,(list,np.ndarray)):
# picks = jb.picks.meg_nobads(raw)
#--- RAW mk epochs + average
# logger.info("event_id: {}\n{}".format(event_id,events))
ep = mne.Epochs(raw,events,event_id=event_id,tmin=tmin,tmax=tmax,picks=picks)
range = None
times = None
ep = self._get_epochs(raw,events,event_id,tmin,tmax,picks)
if len(picks) > 1:
avg = ep.average()
times = avg.times
......@@ -160,7 +157,7 @@ class CalcSignal(_SLOTS):
picks = jb.picks.meg_nobads(raw)
#--- RAW mk epochs + average
ep = mne.Epochs(raw,events,event_id=event_id,tmin=tmin,tmax=tmax,picks=picks)
ep = mne.Epochs(raw,events,event_id=event_id,tmin=tmin,tmax=tmax,picks=picks,baseline=None)
if len(picks) > 1:
avg = ep.average()
times = avg.times
......@@ -336,7 +333,6 @@ class PERFORMANCE_PLOT(CalcSignal):
Returns
-------
"""
ax.plot(t,data,color=color)
......@@ -372,9 +368,7 @@ class PERFORMANCE_PLOT(CalcSignal):
Returns
-------
"""
self._update_from_kwargs(**kwargs)
title = kwargs.get("title")
evt = kwargs.get("events")
......@@ -382,7 +376,8 @@ class PERFORMANCE_PLOT(CalcSignal):
sig_raw,sig_clean,range,t = self._calc_data(self.raw,self.raw_clean,evt,event_id=self.event_id,tmin=self.tmin,
tmax=self.tmax,picks=self.picks)
#--- ref channel e.g.: ECG
#--- ref channel e.g.: ECG
sig_picks = self.labels2picks(labels=self.ch_name)
sig_ref,_,_ = self._calc_signal(self.raw,evt,event_id=self.event_id,tmin=self.tmin,tmax=self.tmax,picks=sig_picks)
......@@ -428,7 +423,8 @@ class PERFORMANCE_PLOT(CalcSignal):
except:
ax1.set_ylim(-1.0,1.0)
ax2.set_ylim(-1.0,1.0)
logger.error("ERROR in performance plot : can not set ylim : {}".format(title))
logger.error("ERROR in performance plot : can not set ylim : {}".format(ylim))
# raise ValueError
plt.tight_layout()
......
%% Cell type:code id: tags:
``` python
from dcnn_base import DCNN_CONFIG
from dcnn_main import DCNN
from dcnn_utils import find_files
from dcnn_logger import setup_script_logging
import os.path as op
from mne.report import Report
from distutils.dir_util import mkpath
```
%% Cell type:code id: tags:
``` python
fnconfig ='config_4D_Juelich.yaml'
basedir = "$JUMEG_PATH_LOCAL_DATA"+"/gDCNN"
fname= basedir + "/meg_rawdata/Freeviewing/" + "203404_FREEVIEW01_180323_1410_1_c,rfDC,meeg_bcc,tr,nr-raw-gdcnn.npz"
```
%% Cell type:code id: tags:
``` python
cfg = DCNN_CONFIG(verbose=True)
cfg.load(fname=fnconfig)
dcnn = DCNN(**cfg.config)
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-e00e1cc8fc77> in <module>
2 cfg.load(fname=fnconfig)
3
----> 4 dcnn = DCNN(**cfg.config)
~/MEGBoers/programs/JuMEG/jumeg-py/jumeg-py-git-fboers-2020-04-23/projects/jugit/gdcnn/dcnn_main.py in __init__(self, version, n_jobs, path, meg, ica, sources, res, info, verbose)
155
156 # the following will later be saved in one file for each experiment
--> 157 self._PATH = DCNN_PATH(**path)
158 self._MEG = DCNN_MEG(**meg) # MEG system and data settings
159 self._ICA = DCNN_ICA(**ica)
~/MEGBoers/programs/JuMEG/jumeg-py/jumeg-py-git-fboers-2020-04-23/projects/jugit/gdcnn/dcnn_base.py in __init__(self, **kwargs)
453
454 def __init__(self,**kwargs):
--> 455 super().__init__(**kwargs)
456 self._cls_name = "DCNN_PATH"
457 self.init(**kwargs)
~/MEGBoers/programs/JuMEG/jumeg-py/jumeg-py-git-fboers-2020-04-23/projects/jugit/gdcnn/dcnn_base.py in __init__(self, **kwargs)
31
32 def __init__(self,**kwargs):
---> 33 self._init()
34 self._verbose = False
35 self._update_from_kwargs(**kwargs)
~/MEGBoers/programs/JuMEG/jumeg-py/jumeg-py-git-fboers-2020-04-23/projects/jugit/gdcnn/dcnn_base.py in _init(self)
45 #--- init slots
46 for k in self.__slots__:
---> 47 self.__setattr__(k,None)
48 # self._update_from_kwargs(**kwargs)
49
TypeError: descriptor 'expandvars' for 'DCNN_PATH' objects doesn't apply to 'DCNN_PATH' object
%% Cell type:code id: tags:
``` python
# dcnn = DCNN( **{"path":{"expandvars":True}} )
dcnn.load_gdcnn(fname)
```
%% Output
[autoreload of dcnn_base failed: Traceback (most recent call last):
File "/home/fboers/MEGBoers/megtools/python/anaconda3/envs/mne0.20/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 245, in check
superreload(m, reload, self.old_objects)
File "/home/fboers/MEGBoers/megtools/python/anaconda3/envs/mne0.20/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 410, in superreload
update_generic(old_obj, new_obj)
File "/home/fboers/MEGBoers/megtools/python/anaconda3/envs/mne0.20/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 347, in update_generic
update(a, b)
File "/home/fboers/MEGBoers/megtools/python/anaconda3/envs/mne0.20/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 317, in update_class
update_instances(old, new)
File "/home/fboers/MEGBoers/megtools/python/anaconda3/envs/mne0.20/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 280, in update_instances
ref.__class__ = new
TypeError: __class__ assignment: 'DCNN_PATH' object layout differs from 'DCNN_PATH'
]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-ac4183970f0f> in <module>
1 # dcnn = DCNN( **{"path":{"expandvars":True}} )
----> 2 dcnn.load_gdcnn(fname)
NameError: name 'dcnn' is not defined
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......@@ -1015,11 +1015,14 @@ def add_aux_channels(raws,data_aux,aux_labels):
if not isinstance(raws,(list)):
raws = [raws]
logger.info("add aux channels to raw & raw cleaned")
for raw in raws:
aux_info = mne.create_info(aux_labels,raw.info['sfreq'],[ l.split(" ")[0].lower() for l in aux_labels ])
aux_info['meas_date'] = raw.info['meas_date'] # for annotations.orig_time
aux_ch_raw = mne.io.RawArray(data_aux,aux_info)
raw.add_channels([aux_ch_raw],force_update_info=True)
logger.info("done: add aux channels to raw & raw cleaned: {}".format(aux_labels))
return raws
# ======================================================
......@@ -1043,12 +1046,16 @@ def transform_ica2data(data_ica,ica,data_aux,aux_labels):
[raw,raw_clean]
"""
# reconstruct MEG data and create raw object
logger.info("reconstruct raw-obj from ICA")
data_meg = transform_mne_ica2data(data_ica,ica)
raw = mne.io.RawArray(data_meg,ica.info)
logger.info("done: reconstruct raw-obj from ICA: {}".format(data_meg.shape))
# reconstruct MEG data, clean artifacts and create raw clean object
logger.info("reconstruct raw-cleaned-obj from ICA")
data_meg = transform_mne_ica2data(data_ica,ica,idx_zero=ica.exclude,idx_keep=None)
raw_clean = mne.io.RawArray(data_meg,ica.info)
logger.info("done: reconstruct raw-cleaned-obj from ICA: {}".format(data_meg.shape))
return add_aux_channels([raw,raw_clean],data_aux,aux_labels)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment