Coverage for nilearn/_utils/class_inspect.py: 5%

12 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-20 10:58 +0200

1"""Small utilities to inspect classes.""" 

2 

3 

4def get_params(cls, instance, ignore=None): 

5 """Retrieve the initialization parameters corresponding to a class. 

6 

7 This helper function retrieves the parameters of function __init__ for 

8 class 'cls' and returns the value for these parameters in object 

9 'instance'. When using a composition pattern (e.g. with a NiftiMasker 

10 class), it is useful to forward parameters from one instance to another. 

11 

12 Parameters 

13 ---------- 

14 cls : class 

15 The class that gives us the list of parameters we are interested in. 

16 

17 instance : object, instance of BaseEstimator 

18 The object that gives us the values of the parameters. 

19 

20 ignore : None or list of strings 

21 Names of the parameters that are not returned. 

22 

23 Returns 

24 ------- 

25 params : dict 

26 The dict of parameters. 

27 

28 """ 

29 _ignore = {"memory", "memory_level", "verbose", "copy", "n_jobs"} 

30 if ignore is not None: 

31 _ignore.update(ignore) 

32 

33 param_names = cls._get_param_names() 

34 

35 params = {} 

36 for param_name in param_names: 

37 if param_name in _ignore: 

38 continue 

39 if hasattr(instance, param_name): 

40 params[param_name] = getattr(instance, param_name) 

41 

42 return params