@@ -45,51 +45,67 @@ class Experiment(object):
4545 >>> shutil.rmtree(params['path_out'], ignore_errors=True)
4646 """
4747
48- def __init__ (self , dict_params , time_stamp = True ):
49- self .params = copy .deepcopy (dict_params )
48+ def __init__ (self , params , time_stamp = True ):
49+ """ constructor
50+
51+ :param dict params: define experimenatl parameters
52+ :param bool time_stamp: add to experiment unique time stamp
53+ """
54+ self .params = copy .deepcopy (params )
5055 self .params ['class' ] = self .__class__ .__name__
51- self .__check_exist_path ()
52- self .__create_folder (time_stamp )
56+ self ._check_exist_paths ()
57+ self ._create_folder (time_stamp )
5358 set_experiment_logger (self .params ['path_exp' ])
5459 logging .info (string_dict (self .params , desc = 'PARAMETERS' ))
5560
5661 def run (self , gt = True ):
62+ """ run the main Experimental body
63+
64+ :param bool gt: try to load Ground Truth
65+ """
5766 self ._load_data (gt )
5867 self ._perform ()
5968 self ._evaluate ()
6069 self ._summarise ()
6170 logging .getLogger ().handlers = []
6271
63- def _load_data (self , gt ):
64- pass
72+ def _load_data (self , gt = True ):
73+ """ loading the experiment data
74+
75+ :param bool gt: try to load ground truth
76+ """
77+ logging .warning ('Not implemented yet...' )
6578
6679 def _perform (self ):
67- pass
80+ logging . warning ( 'Not implemented yet...' )
6881
6982 def _evaluate (self ):
70- pass
83+ logging . warning ( 'Not implemented yet...' )
7184
7285 def _summarise (self ):
73- pass
86+ logging . warning ( 'Not implemented yet...' )
7487
75- def __check_exist_path (self ):
76- for p in [self .params [n ] for n in self .params
77- if 'dir_name' in n .lower () or 'path' in n .lower ()]:
88+ def _check_exist_paths (self ):
89+ """ Check all required paths in parameters whether they exist """
90+ for p in (self .params [n ] for n in self .params
91+ if 'dir' in n .lower () or 'path' in n .lower ()):
7892 if not os .path .exists (p ):
79- raise Exception ('given folder "{} " does not exist!' . format ( p ) )
80- for p in [ self .params [n ] for n in self .params if 'file' in n .lower ()] :
93+ raise Exception ('given folder "%s " does not exist!' % p )
94+ for p in ( self .params [n ] for n in self .params if 'file' in n .lower ()) :
8195 if not os .path .exists (p ):
82- raise Exception ('given file "{}" does not exist!' .format (p ))
96+ raise Exception ('given folder "%s" does not exist!' % p )
97+
98+ def _create_folder (self , time_stamp = True ):
99+ """ Create the experiment folder and iterate while there is no available
83100
84- def __create_folder (self , time_stamp ):
85- """ create the experiment folder and iterate while there is no available
101+ :param bool time_stamp: mark if you want an unique folder per experiment
86102 """
87103 # create results folder for experiments
88- assert os .path .isdir (self .params .get ('path_out' )), \
89- 'missing %s ' % self .params .get ('path_out' )
104+ if not os .path .exists (self .params .get ('path_out' , 'NONE' )):
105+ raise ValueError ( 'no results folder "%r" ' % self .params .get ('path_out' , None ) )
90106 self .params = create_experiment_folder (self .params ,
91107 self .__class__ .__name__ ,
92- stamp_unique = time_stamp )
108+ time_stamp )
93109
94110
95111# def check_exist_dirs_files(params):
@@ -146,15 +162,13 @@ def create_experiment_folder(params, dir_name, stamp_unique=True, skip_load=True
146162 if os .path .exists (path_config ) and not skip_load :
147163 params_in = params
148164 logging .debug ('loading saved params from file "%s"' , CONFIG_YAML )
149- with open (path_config , 'r' ) as fp :
150- params = yaml .load (fp )
165+ params = load_config_yaml (path_config )
151166 params .update ({k : params_in [k ] for k in params_in if 'path' in k })
152167 logging .info ('loaded following PARAMETERS: %s' , string_dict (params ))
153168 params .update ({'computer' : os .uname (),
154169 'path_exp' : path_expt })
155170 logging .debug ('saving params to file "%s"' , CONFIG_YAML )
156- with open (path_config , 'w' ) as fp :
157- yaml .dump (params , fp , default_flow_style = False )
171+ save_config_yaml (path_config , params )
158172 return params
159173
160174
@@ -404,3 +418,30 @@ def __len__(self):
404418# for out in map(wrap_func, iterate_vals):
405419# yield out
406420# tqdm_bar.update()
421+
422+
423+ def load_config_yaml (path_config ):
424+ """ loading the
425+
426+ :param str path_config:
427+ :return dict:
428+
429+ >>> p_conf = './testing-congif.yaml'
430+ >>> save_config_yaml(p_conf, {'a': 2})
431+ >>> load_config_yaml(p_conf)
432+ {'a': 2}
433+ >>> os.remove(p_conf)
434+ """
435+ with open (path_config , 'r' ) as fp :
436+ config = yaml .load (fp )
437+ return config
438+
439+
440+ def save_config_yaml (path_config , config ):
441+ """ exporting configuration as YAML file
442+
443+ :param str path_config:
444+ :param dict config:
445+ """
446+ with open (path_config , 'w' ) as fp :
447+ yaml .dump (config , fp , default_flow_style = False )
0 commit comments