Coverage for source/model/model_blue_prints/general_sklearn_blue_print.py: 40%

15 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-07-30 20:59 +0000

1# model/model_blue_prints/general_sklearn_blue_print.py 

2 

3# global imports 

4from sklearn.base import BaseEstimator 

5 

6# local imports 

7from source.model import BluePrintBase, ModelAdapterBase, SklearnModelAdapter 

8 

9class GeneralSklearnBluePrint(BluePrintBase): 

10 """ 

11 Implements a general blueprint for scikit-learn models. It provides a method to instantiate 

12 a model based on the provided base estimator class and keyword arguments. 

13 """ 

14 

15 # local constants 

16 __VERBOSE_KEY: str = "verbose" 

17 

18 def __init__(self, base_estimator_class: type, **kwargs) -> None: 

19 """ 

20 Class constructor. Initializes the blueprint with a base estimator class and optional keyword arguments. 

21 

22 Parameters: 

23 base_estimator_class (type): The base estimator class to use for model instantiation. 

24 (**kwargs): Optional keyword arguments for the model. 

25 """ 

26 

27 if not issubclass(base_estimator_class, BaseEstimator): 

28 raise TypeError( 

29 f"Parameter base_estimator_class must be a subclass of BaseEstimator," 

30 f" got {base_estimator_class.__name__}" 

31 ) 

32 

33 self.__base_estimator_class = base_estimator_class 

34 self.__kwargs = kwargs 

35 

36 if self.__VERBOSE_KEY not in self.__kwargs: 

37 self.__kwargs[self.__VERBOSE_KEY] = True 

38 

39 def instantiate_model(self, **kwargs) -> ModelAdapterBase: 

40 """ 

41 Instantiates a model based on the provided keyword arguments. 

42 

43 Parameters: 

44 (**kwargs): Optional keyword arguments for the model. 

45 

46 Returns: 

47 (ModelAdapterBase): The model adapter for the instantiated model. 

48 """ 

49 

50 if len(kwargs.keys()) == 0: 

51 kwargs = self.__kwargs 

52 

53 return SklearnModelAdapter(self.__base_estimator_class(**kwargs))