Commit 72859e2d authored by Boers, Frank's avatar Boers, Frank
Browse files

add path cls

parent 56cceeaa
......@@ -16,10 +16,10 @@ import collections,pprint
import mne
from mne.viz import topomap
from dcnn_utils import logger, isFile, rescale, read_raw, get_raw_filename
from dcnn_utils import logger, isFile, isPath, rescale, read_raw, get_raw_filename,expandvars
from dcnn_utils import apply_noise_reduction_4d
__version__ = "2020.06.23.002"
__version__ = "2020.06.25.001"
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# SLOTS class
......@@ -53,8 +53,24 @@ class _SLOTS(object):
for k in self.__slots__:
if k.startswith("_"): continue
data[k]= getattr(self,k)
return data
return data
def __get_slots_attr_hidden(self,lstrip=True):
data = dict()
for k in self.__slots__:
if k.startswith("_"):
if lstrip:
data[ k.lstrip("_") ]= getattr(self,k)
else:
data[k] = getattr(self,k)
return data
def __set_slots_attr_hidden(self,**kwargs):
for k in kwargs:
slot = "_"+ k
if slot in self.__slots__:
self.__setattr__(slot,kwargs.get(k) )
def _update_from_kwargs(self, **kwargs):
if not kwargs: return
for k in kwargs:
......@@ -78,15 +94,30 @@ class _SLOTS(object):
def update(self,**kwargs):
self._update_from_kwargs(**kwargs)
def get_info(self):
def get_info(self,hidden=False):
"""
log class parameter in slots
Parameters
----------
hidden: if True log hidden slots e.g. _data
Returns
-------
"""
msg=["Info => {}".format(self._cls_name)]
for k in self.__slots__:
if k.startswith("_"): continue
msg.append( " -> {} : {}".format(k,getattr(self,k)) )
if hidden:
msg.append("-"*20)
for k,v in self.__get_slots_attr_hidden().items():
msg.append( " -> {} : {}".format(k,v) )
try:
logger.info("\n".join(msg))
except:
print("--->"+"\n".join(msg))
print("--->"+"\n".join(msg))
def dump(self):
"""
......@@ -149,15 +180,12 @@ class DCNN_CONFIG(object):
---------
'''
def __init__(self,**kwargs):
super().__init__()
self._cfg = None
self._missing_keys = None
self.fname = None
self.path_list = ['data_meg','data_train','report']
self.defaults = {
'version': 'v0.2',
......@@ -218,7 +246,6 @@ class DCNN_CONFIG(object):
self._cfg = None #deepcopy( self.defaults )
if not v: return
self._cfg,self._missing_keys = self._merge_and_check(v)
self._add_base_dir()
@property
def missing_keys(self): return self._missing_keys
......@@ -228,9 +255,7 @@ class DCNN_CONFIG(object):
self.defaults = kwargs.get("defaults")
if "keys_to_check" in kwargs:
self.keys_to_chek = kwargs.get("keys_to_check")
if "path_list" in kwargs:
self.path_list = kwargs.get("path_list")
if "fname" in kwargs:
if "fname" in kwargs:
self.fname = kwargs.get("fname")
if "config" in kwargs:
self.config = kwargs.get("config")
......@@ -360,29 +385,6 @@ class DCNN_CONFIG(object):
pp = pprint.PrettyPrinter(indent=2)
return ''.join(map(str,pp.pformat(d)))
def _add_base_dir(self):
'''
update path settings
Returns
-------
None.
'''
if not self.path_list: return
path = self.config.get('path')
if not path: return
basedir = path.get('basedir')
if not basedir: return
# update path settings
for p in self.path_list:
if not self.config['path'][p].startswith(basedir):
self.config['path'][p] = op.join(basedir,self.config['path'][p])
def load(self,**kwargs):
'''
load config [yaml]
......@@ -434,6 +436,101 @@ class DCNN_CONFIG(object):
logger.info("\n".join(msg))
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# PATH class
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class DCNN_PATH(_SLOTS):
__slots__= ("expandvars","exit_on_error","logmsg","mkdir","overwrite","path_list","add_basedir",
"_basedir","_data_meg","_data_train","_report")
def __init__(self,**kwargs):
super().__init__(**kwargs)
self._cls_name = "DCNN_PATH"
self.init(**kwargs)
def init(self,**kwargs): # set overwrite=True
self.clear()
self._update_from_kwargs(**kwargs)
#-- set overwrite to True for real init if not set
self.overwrite = kwargs.get("overwrite",True)
self.add_basedir = kwargs.get("add_basedir",True)
self.expandvars = kwargs.get("expandvars",True)
self.exit_on_error = kwargs.get("exit_on_error",True)
self.logmsg = kwargs.get("logmsg",True)
self.mkdir = kwargs.get("mkdir",True)
# --
self.basedir = kwargs.get("basedir")
self.data_meg = kwargs.get("data_meg")
self.data_train = kwargs.get("data_train")
self.report = kwargs.get("report")
#-- 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):
self._get_path(self._basedir)
@basedir.setter
def basedir(self,v):
self._set_path(v,"basedir")
@property
def data_meg(self):
self._get_path(self._data_meg)
@data_meg.setter
def data_meg(self,v):
self._set_path(v,"data_meg")
@property
def data_train(self):
self._get_path(self._data_train)
@data_train.setter
def data_train(self,v):
self._set_path(v,"data_train")
@property
def report(self):
self._get_path(self._report)
@report.setter
def report(self,v):
self._set_path(v,"report")
def _get_path(self,v):
if self.expandvars:
return expandvars(v)
return v
def _set_path(self,path,label):
if self.add_basedir and self.basedir:
if not path.startswith(self.basedir):
path = op.join(self.basedir,path)
if not self.overwrite:
msg=["Warning {} not allowed to overwrite path settings for: {}".format(self._cls_name,label),
" -> overwrite: {}".format(self.overwrite),
" -> path orig: {}".format(getattr(self,"_".label)),
" -> path : {}".format(path)]
logger.warning("\n".join(msg))
is_path = isPath(path,head="{} check path: {}".format(self._cls_name,label),
exit_on_error=self.exit_on_error,logmsg=self.logmsg,mkdir=self.mkdir)
if self.overwrite:
self.__setattr__("_" +label,path)
return is_path
def dump(self):
d1 = super().dump()
d2 = self.__get_slots_attr_hidden()
return {**d1,**d2}
def get_info(self):
super().get_info(hidden=True)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# PICKS class
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
......
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