1414
1515log = logging .getLogger ("floatLogger" )
1616
17+
1718class FilepathMixin :
1819 """
1920 Small mixin to provide filepath management functionality to Registries that uses files to
2021 store objects
2122 """
23+
2224 workdir : Path
2325 path : Path
2426
@@ -32,7 +34,7 @@ def dir(self) -> Path:
3234 return self .path .parents [0 ]
3335
3436 @staticmethod
35- def _parse_arg (arg ) -> Union [str , list [str ]]:
37+ def _parse_arg (arg ) -> Union [str , list [str ]]:
3638 if isinstance (arg , (list , tuple )):
3739 return timewindow2str (arg )
3840 elif isinstance (arg , str ):
@@ -91,7 +93,7 @@ def abs_dir(self, *paths: Sequence[str | Path]) -> Path:
9193 _dir = _path .parents [0 ]
9294 return _dir
9395
94- def rel (self , * paths : Union [Path | str | Sequence [str | Path ]] ) -> Path :
96+ def rel (self , * paths : Union [Path | str | Sequence [str | Path ]]) -> Path :
9597 """
9698 Gets the relative path of an item, relative to the Registry workdir
9799
@@ -133,6 +135,7 @@ def file_exists(self, *args: Sequence[str | Path]):
133135 file_abspath = self .get_attr (* args )
134136 return exists (file_abspath )
135137
138+
136139class ModelRegistry (ABC ):
137140 @abstractmethod
138141 def get_input_catalog_key (self , tstring : str ) -> str :
@@ -147,18 +150,21 @@ def get_args_key(self, tstring: str) -> str:
147150 pass
148151
149152 @classmethod
150- def factory (cls , registry_type : str = 'file' , ** kwargs ) -> Union ["ModelFileRegistry" , "ModelHDF5Registry" ]:
153+ def factory (
154+ cls , registry_type : str = "file" , ** kwargs
155+ ) -> Union ["ModelFileRegistry" , "ModelHDF5Registry" ]:
151156 """Factory method. Instantiate first on any explicit option provided in the model
152157 configuration.
153158 """
154- if registry_type == ' file' :
159+ if registry_type == " file" :
155160 return ModelFileRegistry (** kwargs )
156161
157- elif registry_type == ' hdf5' :
162+ elif registry_type == " hdf5" :
158163 return ModelHDF5Registry (** kwargs )
159164 else :
160165 raise Exception ("No valid model management schema was selected" )
161166
167+
162168class ModelFileRegistry (ModelRegistry , FilepathMixin ):
163169 def __init__ (
164170 self ,
@@ -181,18 +187,16 @@ def __init__(
181187
182188 self .model_name = model_name
183189 self .workdir = Path (workdir )
184- self .path = Path (path )
185-
186- self .args_file = self .rel (self .path , args_file ) if args_file else None
187- self .input_cat = self .rel (self .path , input_cat ) if input_cat else None
190+ self .path = self .abs (Path (path ))
188191
192+ self .args_file = args_file if args_file else None
193+ self .input_cat = input_cat if input_cat else None
189194 self .forecasts = {}
190195 self .input_args = {}
191196 self .input_cats = {}
192197 self .input_store = None
193198 self ._fmt = fmt
194199
195-
196200 @property
197201 def fmt (self ) -> str :
198202 """
@@ -265,8 +269,8 @@ def build_tree(
265269 time_windows : Sequence [Sequence [datetime ]] = None ,
266270 model_class : str = "TimeIndependentModel" ,
267271 prefix : str = None ,
268- run_mode : str = ' sequential' ,
269- run_dir : Optional [str ] = None
272+ run_mode : str = " sequential" ,
273+ run_dir : Optional [str ] = None ,
270274 ) -> None :
271275 """
272276 Creates the run directory, and reads the file structure inside.
@@ -295,20 +299,20 @@ def build_tree(
295299 for _ , folder_ in dirtree .items ():
296300 os .makedirs (folder_ , exist_ok = True )
297301
298- if run_mode == ' sequential' :
302+ if run_mode == " sequential" :
299303 self .input_args = {
300- win : Path (self .path , ' input' , self .args_file ) for win in windows
304+ win : Path (self .path , " input" , self .args_file ) for win in windows
301305 }
302306 self .input_cats = {
303- win : Path (self .path , ' input' , self .input_cat ) for win in windows
307+ win : Path (self .path , " input" , self .input_cat ) for win in windows
304308 }
305- elif run_mode == ' parallel' :
309+ elif run_mode == " parallel" :
306310 self .input_args = {
307- win : Path (run_dir , win , ' input' , self .model_name , self .args_file )
311+ win : Path (run_dir , win , " input" , self .model_name , self .args_file )
308312 for win in windows
309313 }
310314 self .input_cats = {
311- win : Path (run_dir , win , ' input' , self .model_name , self .input_cat )
315+ win : Path (run_dir , win , " input" , self .model_name , self .input_cat )
312316 for win in windows
313317 }
314318
@@ -330,16 +334,21 @@ def as_dict(self) -> dict:
330334 "forecasts" : self .forecasts ,
331335 }
332336
337+
333338class ModelHDF5Registry (ModelRegistry ):
334339
335340 def __init__ (self , workdir : str , path : str ):
336341 pass
342+
337343 def get_input_catalog_key (self , tstring : str ) -> str :
338- return ''
344+ return ""
345+
339346 def get_forecast_key (self , tstring : str ) -> str :
340- return ''
347+ return ""
348+
341349 def get_args_key (self , tstring : str ) -> str :
342- return ''
350+ return ""
351+
343352
344353class ExperimentRegistry (ABC ):
345354 @abstractmethod
@@ -372,16 +381,19 @@ def build_tree(
372381 pass
373382
374383 @classmethod
375- def factory (cls , registry_type : str = 'file' , ** kwargs ) -> Optional ["ExperimentFileRegistry" ]:
384+ def factory (
385+ cls , registry_type : str = "file" , ** kwargs
386+ ) -> Optional ["ExperimentFileRegistry" ]:
376387 """Factory method. Instantiate first on any explicit option provided in the experiment
377388 configuration.
378389 """
379390
380- if registry_type == ' file' :
391+ if registry_type == " file" :
381392 return ExperimentFileRegistry (** kwargs )
382393 else :
383394 return None
384395
396+
385397class ExperimentFileRegistry (ExperimentRegistry , FilepathMixin ):
386398 """
387399 The class has the responsibility of managing the keys (based on models, timewindow and
@@ -398,7 +410,7 @@ def __init__(self, workdir: str, run_dir: str = "results") -> None:
398410 run_dir: The directory in which the results will be stored.
399411 """
400412 self .workdir = Path (workdir )
401- self .run_dir = Path (run_dir )
413+ self .run_dir = self . abs ( Path (run_dir ) )
402414 self .results = {}
403415 self .test_catalogs = {}
404416 self .figures = {}
@@ -510,7 +522,7 @@ def build_tree(
510522 time_windows : Sequence [Sequence [datetime ]],
511523 models : Sequence ["Model" ],
512524 tests : Sequence ["Evaluation" ],
513- run_mode : str = ' sequential'
525+ run_mode : str = " sequential" ,
514526 ) -> None :
515527 """
516528 Creates the run directory and reads the file structure inside.
@@ -529,8 +541,8 @@ def build_tree(
529541
530542 run_folder = self .run_dir
531543 subfolders = ["catalog" , "evaluations" , "figures" ]
532- if run_mode == ' parallel' :
533- subfolders .append (' input' )
544+ if run_mode == " parallel" :
545+ subfolders .append (" input" )
534546 dirtree = {
535547 win : {folder : self .abs (run_folder , win , folder ) for folder in subfolders }
536548 for win in windows
@@ -540,8 +552,7 @@ def build_tree(
540552 for tw , tw_folder in dirtree .items ():
541553 for _ , folder_ in tw_folder .items ():
542554 os .makedirs (folder_ , exist_ok = True )
543- if run_mode == 'parallel' and folder_ .endswith ('input' ):
544- print ('a' )
555+ if run_mode == "parallel" and folder_ .name == "input" :
545556 for model in models :
546557 os .makedirs (join (folder_ , model ), exist_ok = True )
547558 results = {
@@ -578,4 +589,3 @@ def build_tree(
578589 def as_dict (self ) -> Path :
579590
580591 return self .workdir
581-
0 commit comments