Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 "Unable to parse '{}'.".format(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 for k in res: # pylint: disable=C0206 

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

80 res[k] = res[k].strip('_') 

81 

82 rep = { 

83 'LinReg': 'LinearRegression', 

84 'LinRegressor': 'LinearRegression', 

85 'LogReg': 'LogisticRegression', 

86 'HGB': 'HistGradientBoosting', 

87 } 

88 

89 if 'model' in res: 

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

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

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

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

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

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

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

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

98 return res 

99 

100 

101def make_readable_title(infos): 

102 """ 

103 Creates a readable title based on the test information. 

104 """ 

105 sp = [infos['model']] 

106 if 'problem' in infos: 

107 sp.append('[{}]'.format(infos['problem'])) 

108 if 'scenario' in infos: 

109 sp.append('[{}]'.format(infos['scenario'])) 

110 if 'N' in infos: 

111 sp.append('N={}'.format(infos['N'])) 

112 if 'nf' in infos: 

113 sp.append('nf={}'.format(infos['nf'])) 

114 if 'opset' in infos: 

115 sp.append('ops={}'.format(infos['opset'])) 

116 if 'double' in infos: 

117 if infos['double']: 

118 sp.append('x64') 

119 if 'opt' in infos: 

120 sp.append('[{}]'.format(infos['opt'])) 

121 if 'profile' in infos: 

122 sp.append('by {}'.format(infos['profile'])) 

123 return " ".join(sp)