Coverage for mlprodict/tools/filename_helper.py: 100%

75 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-04 02:28 +0100

1""" 

2@file 

3@brief A couple of tools related to filenames. 

4""" 

5import os 

6 

7 

8def extract_information_from_filename(name): 

9 """ 

10 Returns a dictionary with information extracted 

11 from a filename. 

12 An example is better: 

13 

14 .. runpython:: 

15 :showcode: 

16 :warningout: DeprecationWarning 

17 

18 from mlprodict.tools.filename_helper import extract_information_from_filename 

19 

20 candidates = [ 

21 'bench_DecisionTreeClassifier_default_b_cl_1_4_12_float_.py', 

22 'bench_DecisionTreeClassifier_default_b_cl_64_10_20_12_double_.py', 

23 'bench_DecisionTreeClassifier_default_b_cl_64_100_4_12_float_.py', 

24 'bench_AdaBoostClassifier_default_b_cl_1000_50_12_float__fct.svg', 

25 'bench_AdaBoostClassifier_default_m_cl_1_4_12_float__line.svg', 

26 'bench_LogisticRegression_liblinear_b_cl_solverliblinear_1_4_12_float_nozipmap_fct.svg', 

27 ] 

28 

29 for name in candidates: 

30 d = extract_information_from_filename(name) 

31 print(d) 

32 """ 

33 spl = os.path.splitext(os.path.split(name)[-1])[0].split('_') 

34 res = {} 

35 for v in spl: 

36 if v == "bench": 

37 continue 

38 if not v: 

39 continue 

40 if "A" <= v[0] <= "Z": 

41 res['model'] = v 

42 continue 

43 try: 

44 i = int(v) 

45 except ValueError: 

46 i = None 

47 

48 if i is not None: 

49 if i == 64: 

50 res['double'] = True 

51 continue 

52 if 'N' not in res: 

53 res['N'] = i 

54 continue 

55 if 'nf' not in res: 

56 res['nf'] = i 

57 continue 

58 if 'opset' not in res: 

59 res['opset'] = i 

60 continue 

61 raise ValueError( # pragma: no cover 

62 f"Unable to parse '{name}'.") 

63 

64 if 'scenario' not in res: 

65 res['scenario'] = v 

66 continue 

67 if 'N' in res: 

68 if v in ('fct', 'line'): 

69 res['profile'] = v 

70 continue 

71 res['opt'] = res.get('opt', '') + '_' + v 

72 continue 

73 if len(v) <= 4: 

74 res['problem'] = res.get('problem', '') + '_' + v 

75 else: 

76 res['opt'] = res.get('opt', '') + '_' + v 

77 

78 updated = {} 

79 for k in res: # pylint: disable=C0206 

80 if isinstance(res[k], str): 

81 updated[k] = res[k].strip('_') 

82 res.update(updated) 

83 

84 rep = { 

85 'LinReg': 'LinearRegression', 

86 'LinRegressor': 'LinearRegression', 

87 'LogReg': 'LogisticRegression', 

88 'HGB': 'HistGradientBoosting', 

89 } 

90 

91 if 'model' in res: 

92 if res['model'].endswith('Clas'): 

93 res['model'] += "sifier" 

94 elif res['model'].endswith('Reg'): 

95 res['model'] += "ressor" 

96 if res['model'].startswith('HGB'): 

97 res['model'] = "HistGradientBoosting" + \ 

98 res['model'][3:] # pragma: no cover 

99 res['model'] = rep.get(res['model'], res['model']) 

100 return res 

101 

102 

103def make_readable_title(infos): 

104 """ 

105 Creates a readable title based on the test information. 

106 """ 

107 sp = [infos['model']] 

108 if 'problem' in infos: 

109 sp.append(f"[{infos['problem']}]") 

110 if 'scenario' in infos: 

111 sp.append(f"[{infos['scenario']}]") 

112 if 'N' in infos: 

113 sp.append(f"N={infos['N']}") 

114 if 'nf' in infos: 

115 sp.append(f"nf={infos['nf']}") 

116 if 'opset' in infos: 

117 sp.append(f"ops={infos['opset']}") 

118 if 'double' in infos: 

119 if infos['double']: 

120 sp.append('x64') 

121 if 'opt' in infos: 

122 sp.append(f"[{infos['opt']}]") 

123 if 'profile' in infos: 

124 sp.append(f"by {infos['profile']}") 

125 return " ".join(sp)