@sensetime

    1. import os
    2. import re
    3. from pprint import pprint
    4. import numpy as np
    5. from scipy import signal
    6. import plotly.offline as py
    7. import plotly.tools as plotly_tools
    8. import plotly.graph_objects as go
    9. from plotly.subplots import make_subplots
    10. import time
    11. import pandas as pd
    12. from tabulate import tabulate
    13. interval = 3
    14. average_threshold = 1
    15. TemDict = {}
    16. DictB = {}
    17. Entire_Dict={}
    18. def TextSniffing(TemDict):
    19. for root, dirs, files in os.walk(".", topdown=True):
    20. MatchDir = re.match( './time_log', root)
    21. if MatchDir:
    22. TemDict[root[2:]] = ["{}/{}".format(root,elemente) for elemente in files]
    23. #print("{}: {}".format(root[2:],files))
    24. return TemDict
    25. # def TextReader(filepath):
    26. # with open(filepath, 'r') as f:
    27. # line = f.readline()
    28. # line = line[:-1]
    29. # dictA={}
    30. # while line:
    31. # line = f.readline()
    32. # a = line.split(' $ ')
    33. # if a[0] and a[-1][:-4]:
    34. # # print(a[0],' ',a[-1][:-4])
    35. # if dictA.get(a[0]):
    36. # dictA[a[0]].append(a[-1][:-4])
    37. # else:
    38. # dictA[a[0]] = [a[-1][:-4]]
    39. # else:
    40. # pass
    41. # print(dictA.keys())
    42. # return dictA
    43. def TextReader(filepath):
    44. with open(filepath, 'r') as f:
    45. lines = f.readlines()
    46. lines = lines[:-1]
    47. dictA={}
    48. for line in lines:
    49. a = line.split(' $ ')
    50. if a[0] and a[-1][:-4]:
    51. # print(a[0],' ',a[-1][:-4])
    52. if dictA.get(a[0]):
    53. dictA[a[0]].append(a[-1][:-4])
    54. else:
    55. dictA[a[0]] = [a[-1][:-4]]
    56. else:
    57. pass
    58. return dictA
    59. # def ThresholdFilter(rawDict,average_threshold):
    60. # DictC = {}
    61. # for key, value in rawDict.items():
    62. # #print(key,value[0])
    63. # for k , v in value.items():
    64. # # tem_value = np.array(rawDict[key],dtype = 'float64')
    65. # average_value = np.mean(v)
    66. # if average_value >= average_threshold:
    67. # DictC[k]=value
    68. # return DictC
    69. def ThresholdFilter(rawDict,average_threshold):
    70. DictC = {}
    71. for file_name, file_value_dict in rawDict.items():
    72. #print(key,value[0])
    73. for key , value in file_value_dict.items():
    74. # tem_value = np.array(rawDict[key],dtype = 'float64')
    75. average_value = np.mean(value)
    76. if average_value >= average_threshold:
    77. DictC[file_name][k]= value
    78. return DictC
    79. def Talulate_Writer(statistics_dict):
    80. relation_pd = pd.DataFrame(columns=('level','key','average','max_value','min_value','std_value'))
    81. for key, value in statistics_dict.items():
    82. if (len(value) == 0):
    83. continue
    84. tem_value = np.array(statistics_dict[key], dtype= 'float64')
    85. value_max = max(tem_value)
    86. value_min = min(tem_value)
    87. value_std = np.std(tem_value)
    88. # level = int(key.strip('[').split('::')[0].strip('[').strip(']'))
    89. level_judge = key.split(' $ ')[0][1]
    90. if level_judge == "[":
    91. level = int(key.split(' $ ')[0][2])
    92. else:
    93. level = -1
    94. value_average = np.average(tem_value)
    95. relation_pd=relation_pd.append(pd.DataFrame({'level':[level], 'key':[key], 'average':[value_average],
    96. 'max_value':[value_max], 'min_value':[value_min],
    97. 'std_value':[value_std]}),
    98. ignore_index = True,sort = True)
    99. return relation_pd
    100. def Formater(Talulate_Reader):
    101. Talulate_Reader.sort_values(by=["level","std_value"],inplace=True,ascending=[True,True])
    102. Talulate_Reader.set_index('level',inplace=True)
    103. Talulate_Reader = Talulate_Reader[['key','average','max_value','min_value','std_value']]
    104. print(tabulate(Talulate_Reader,headers='keys',tablefmt='psql'))
    105. print('\n')
    106. return Talulate_Reader['key'].tolist()
    107. if __name__ == "__main__":
    108. TimeLogDict = TextSniffing(TemDict)
    109. subplot_titles_list =[]
    110. for dirname, filepaths in TimeLogDict.items():
    111. for ele in filepaths:
    112. print('######## {} ########'.format(ele.split('/')[-1]))
    113. ele_dict= TextReader(ele)
    114. if not list(ele_dict.keys()):
    115. pass
    116. else:
    117. Talulate_Reader = Talulate_Writer(ele_dict)
    118. aa = Formater(Talulate_Reader)
    119. subplot_titles_list.extend(aa)
    120. Entire_Dict.update(ele_dict) #输出完整的 函数:耗时 键值对字典
    121. ######################## 绘图 ###########################
    122. # fig = make_subplots(subplot_titles=(subplot_titles_list), rows=len(subplot_titles_list), cols=1)
    123. # i = 1
    124. # for file_name, value_list in Entire_Dict.items():
    125. # for key, value in value_list.items():
    126. # x = np.arange(len(value_list[key][0::interval]))
    127. # fig.append_trace(
    128. # go.Scatter(x=x,
    129. # y=value_list[key][0::interval],
    130. # mode='markers',
    131. # name= '{} (ms)'.format(key),
    132. # marker=dict(size=2, color='#007bbb')
    133. # ),
    134. # row=i,
    135. # col=1)
    136. # fig.add_trace(go.Scatter(x=x, y=signal.savgol_filter(value_list[key][0::interval], 91, 5), # order of fitted polynomial # window size used for filtering
    137. # mode='lines',
    138. # name= '{} (ms)'.format(key),
    139. # line=dict(color='rgb(181,73,91)', width=1)
    140. # ),
    141. # row=i,
    142. # col=1)
    143. # i += 1
    144. fig = make_subplots(subplot_titles=(subplot_titles_list), rows=len(subplot_titles_list), cols=1)
    145. i = 1
    146. for key in subplot_titles_list:
    147. x = np.arange(len(Entire_Dict[key][0::interval]))
    148. # fig.append_trace(
    149. # go.Scatter(x=x,
    150. # y=Entire_Dict[key][0::interval],
    151. # mode='markers',
    152. # name= '{} (ms)'.format(key),
    153. # marker=dict(size=2, color='#007bbb')
    154. # ),
    155. # row=i,
    156. # col=1)
    157. fig.add_trace(go.Scatter(x=x, y=signal.savgol_filter(Entire_Dict[key][0::interval], 91, 5), # order of fitted polynomial # window size used for filtering
    158. mode='lines',
    159. name= '{} (ms)'.format(key),
    160. line=dict(color='rgb(181,73,91)', width=1)
    161. ),
    162. row=i,
    163. col=1)
    164. i += 1
    165. # DictC = ThresholdFilter(Entire_Dict, average_threshold) # 根据阈值过滤 TODO Entire_Dict 格式变了
    166. fig.update_layout(
    167. height=7000,
    168. showlegend=True,
    169. title_text="<b>FT Time Consuming Visual Analysis</b><br>{}".format(time.strftime("%Y%m%d", time.localtime())),
    170. )
    171. fig.show()
    172. py.plot(fig, filename = 'FT Time Consuming Analysis_{}.html'.format(time.strftime("%Y-%m-%d", time.localtime())), auto_open=False)