Coverage for src/bob/bio/base/utils/__init__.py: 78%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.5, created at 2024-11-14 21:41 +0100

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3# Laurent El Shafey <Laurent.El-Shafey@idiap.ch> 

4# Roy Wallace <roy.wallace@idiap.ch> 

5# isort: skip_file 

6 

7 

8from .resources import * # noqa: F401,F403 

9from .io import * # noqa: F401,F403 

10import inspect 

11import numpy 

12 

13 

14def score_fusion_strategy(strategy_name="average"): 

15 """Returns a function to compute a fusion strategy between different scores. 

16 

17 Different strategies are employed: 

18 

19 * ``'average'`` : The averaged score is computed using the :py:func:`numpy.average` function. 

20 * ``'min'`` : The minimum score is computed using the :py:func:`min` function. 

21 * ``'max'`` : The maximum score is computed using the :py:func:`max` function. 

22 * ``'median'`` : The median score is computed using the :py:func:`numpy.median` function. 

23 * ``None`` is also accepted, in which case ``None`` is returned. 

24 """ 

25 try: 

26 return { 

27 "average": numpy.average, 

28 "min": min, 

29 "max": max, 

30 "median": numpy.median, 

31 None: None, 

32 }[strategy_name] 

33 except KeyError: 

34 # warn("score fusion strategy '%s' is unknown" % strategy_name) 

35 return None 

36 

37 

38def selected_indices(total_number_of_indices, desired_number_of_indices=None): 

39 """Returns a list of indices that will contain exactly the number of desired indices (or the number of total items in the list, if this is smaller). 

40 These indices are selected such that they are evenly spread over the whole sequence. 

41 """ 

42 if ( 

43 desired_number_of_indices is None 

44 or desired_number_of_indices >= total_number_of_indices 

45 or desired_number_of_indices < 0 

46 ): 

47 return range(total_number_of_indices) 

48 increase = float(total_number_of_indices) / float(desired_number_of_indices) 

49 # generate a regular quasi-random index list 

50 return [int((i + 0.5) * increase) for i in range(desired_number_of_indices)] 

51 

52 

53def selected_elements(list_of_elements, desired_number_of_elements=None): 

54 """Returns a list of elements that are sub-selected from the given list (or the list itself, if its length is smaller). 

55 These elements are selected such that they are evenly spread over the whole list. 

56 """ 

57 total_number_of_elements = len(list_of_elements) 

58 if ( 

59 desired_number_of_elements is None 

60 or desired_number_of_elements >= total_number_of_elements 

61 or desired_number_of_elements < 0 

62 ): 

63 return list_of_elements 

64 # sub-select 

65 return [ 

66 list_of_elements[i] 

67 for i in selected_indices( 

68 total_number_of_elements, desired_number_of_elements 

69 ) 

70 ] 

71 

72 

73def pretty_print(obj, kwargs): 

74 """Returns a pretty-print of the parameters to the constructor of a class, which should be able to copy-paste on the command line to create the object (with few exceptions).""" 

75 return "%s(%s)" % ( 

76 str(obj.__class__), 

77 ", ".join( 

78 [ 

79 "%s='%s'" % (key, value) 

80 if isinstance(value, str) 

81 else "%s=%s" % (key, value) 

82 for key, value in kwargs.items() 

83 if value is not None 

84 ] 

85 ), 

86 ) 

87 

88 

89def is_argument_available(argument, method): 

90 """ 

91 Check if an argument (or keyword argument) is available in a method 

92 

93 Attributes 

94 ---------- 

95 argument: str 

96 The name of the argument (or keyword argument). 

97 

98 method: 

99 Pointer to the method 

100 

101 """ 

102 return argument in inspect.signature(method).parameters.keys()