Config is now an Abstract Method to keep this lib clean from prior project dependencies

This commit is contained in:
Si11ium
2020-04-15 17:16:41 +02:00
parent 42559ec642
commit e53107420d

View File

@@ -1,14 +1,11 @@
import ast import ast
from abc import ABC
from argparse import Namespace from argparse import Namespace
from collections import defaultdict from collections import defaultdict
from configparser import ConfigParser from configparser import ConfigParser
from pathlib import Path from pathlib import Path
from ml_lib.models.generators.cnn import CNNRouteGeneratorModel
from ml_lib.models.generators.cnn_discriminated import CNNRouteGeneratorDiscriminated
from ml_lib.models.homotopy_classification.cnn_based import ConvHomDetector
from ml_lib.utils.model_io import ModelParameters from ml_lib.utils.model_io import ModelParameters
from ml_lib.utils.transforms import AsArray from ml_lib.utils.transforms import AsArray
@@ -22,22 +19,34 @@ def is_jsonable(x):
return False return False
class Config(ConfigParser): class Config(ConfigParser, ABC):
# TODO: Do this programmatically; This did not work: # TODO: Do this programmatically; This did not work:
# Initialize Default Sections # Initialize Default Sections
# for section in self.default_sections: # for section in self.default_sections:
# self.__setattr__(section, property(lambda x :x._get_namespace_for_section(section)) # self.__setattr__(section, property(lambda x :x._get_namespace_for_section(section))
@property
def model_map(self):
"""
This is function is supposed to return a dict, which holds a mapping from string model names to model classes
Example:
from models.binary_classifier import BinaryClassifier
return dict(BinaryClassifier=BinaryClassifier,
)
:return:
"""
raise NotImplementedError
@property @property
def model_class(self): def model_class(self):
model_dict = dict(BinaryClassifier=BinaryClassifier,
)
try: try:
return model_dict[self.get('model', 'type')] return self.model_map[self.get('model', 'type')]
except KeyError as e: except KeyError as e:
raise KeyError(rf'The model alias you provided ("{self.get("model", "type")}") does not exist! \n' raise KeyError(rf'The model alias you provided ("{self.get("model", "type")}") does not exist! \n'
f'Try one of these:\n{list(model_dict.keys())}') f'Try one of these:\n{list(self.model_map.keys())}')
@property @property
def main(self): def main(self):