maincopycopy.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import CBMSBatChrgcopy
  2. import log
  3. #coding=utf-8
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager, Log
  7. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  8. # from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  9. from sqlalchemy import create_engine
  10. import time, datetime
  11. import os
  12. import numpy as np
  13. from apscheduler.schedulers.blocking import BlockingScheduler
  14. import QX_BatteryParam
  15. import pymysql
  16. import matplotlib.pyplot as plt
  17. #-------------------------------------------新建电压排序函数--------------------------------------------
  18. # def CellVolt_rang():
  19. # global SNnums
  20. # global df_Diag_Ram
  21. # start=time.time()
  22. # end_time=datetime.datetime.now()
  23. # start_time=end_time-datetime.timedelta(seconds=720)
  24. # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  25. # end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  26. # print('---------------计算中-------------------------')
  27. # for sn in SNnums:
  28. # if 'PK500' in sn:
  29. # celltype=1 #6040三元电芯
  30. # elif 'PK502' in sn:
  31. # celltype=2 #4840三元电芯
  32. # elif 'K504B' in sn:
  33. # celltype=99 #60ah林磷酸铁锂电芯
  34. # elif 'MGMLXN750' in sn:
  35. # celltype=3 #力信50ah三元电芯
  36. # elif 'MGMCLN750' or 'UD' in sn:
  37. # celltype=4 #CATL 50ah三元电芯
  38. # else:
  39. # print('SN:{},未找到对应电池类型!!!'.format(sn))
  40. # continue
  41. # # sys.exit()
  42. # param=QX_BatteryParam.BatteryInfo(celltype)
  43. # #读取结果数据库数据........................................................................................................................................................
  44. # host='47.97.96.242'
  45. # port=3306
  46. # db='didi'
  47. # user='root'
  48. # password='qx123456'
  49. # tablename='didi_data'
  50. # param='date,device_id,bat_model,position,current,soc,celltemp,cellvolt_2,cellvolt_3,cellvolt_4,cellvolt_5,cellvolt_6,cellvolt_7,cellvolt_8,cellvolt_9'
  51. # mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  52. # cursor = mysql.cursor()
  53. # sql = "select %s from %s where device_id='%s'" %(param,tablename,sn)
  54. # cursor.execute(sql)
  55. # res = cursor.fetchall()
  56. # df_bms= pd.DataFrame(res,columns=param.split(','))
  57. # cursor.close()
  58. # mysql.close()
  59. # # mode=1
  60. # # # tablename4='cellstateestimation_intershort'
  61. # # DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  62. # # with DBRead as DBRead:
  63. # # df_data=DBRead.getdata('date,device_id,position,current,soc,cellvolt_2,cellvolt_3,cellvolt_4,cellvolt_5,cellvolt_6,cellvolt_7,cellvolt_8,cellvolt_9', tablename=tablename, sn=sn, timename='date', st=start_time, sp=end_time)
  64. # # df_bms.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\origin_data\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  65. # #电压排序................................................................................................................................................................
  66. # df_temp = df_bms[df_bms['position']==2]#筛选充电数据
  67. # df_chrgr = df_temp.reset_index(drop=True)
  68. # df_chrgr_cellvolt = df_chrgr[['cellvolt_2','cellvolt_3','cellvolt_4','cellvolt_5','cellvolt_6','cellvolt_7','cellvolt_8','cellvolt_9']]
  69. # df_chrgr_cellvolt_change = np.array(df_chrgr_cellvolt)#转数组
  70. # df_chrgr_cellvolt_sort = np.argsort(df_chrgr_cellvolt_change)#取排序号
  71. # df_cellvolt_sort_dif = np.diff(df_chrgr_cellvolt_sort)#一次微分
  72. # df_cellvolt_sort_dif_confir = np.nonzero(df_cellvolt_sort_dif)#取非0值
  73. # Cell_num = set(df_cellvolt_sort_dif_confir[1])#寻找哪号电芯序号异常np.unique
  74. # X_col=np.size(df_chrgr_cellvolt,0) #计算 X 的列数
  75. # #df_cellvolt_sort_difdif = np.diff(df_cellvolt_sort_dif)#二次微分
  76. # problem_data = pd.DataFrame()
  77. # temp_list = []
  78. # for item in Cell_num:
  79. # temp_list.append(np.sum(df_cellvolt_sort_dif_confir[1]==item) > X_col/20)
  80. # if any(temp_list):#序号变化的电芯
  81. # data_temp = pd.DataFrame(df_chrgr_cellvolt_sort)
  82. # problem_data = pd.concat([df_chrgr,data_temp], axis = 1)
  83. # problem_data.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  84. # # volt_column = ['0','1','2','3','4','5','6','7']
  85. # # fig = plt.figure(figsize=(20,10))
  86. # # length = len(problem_data)
  87. # # for column in volt_column:
  88. # # plt.scatter([x for x in range(0, length)], problem_data[column][0:length], label=column)
  89. # # plt.legend()
  90. # # title = problem_data['device_id'][0]
  91. # # plt.title(title)
  92. # # plt.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+title+'.png', dpi=300)
  93. # # plt.show()
  94. # # fig = plt.figure()
  95. # # #ax = plt.subplot()
  96. # # plt.scatter(problem_data['date'], problem_data['0'], c='black',marker='o',alpha=0.5, label = "2#电芯")
  97. # # plt.scatter(problem_data['date'], problem_data['1'], c='green', marker='o',alpha=0.5, label = "3#电芯")
  98. # # plt.scatter(problem_data['date'], problem_data['2'], c='red',marker='o', alpha=0.5, label = "4#电芯")
  99. # # plt.scatter(problem_data['date'], problem_data['3'], c='black',marker='o',alpha=0.5, label = "5#电芯")
  100. # # plt.scatter(problem_data['date'], problem_data['4'], c='green', marker='o',alpha=0.5, label = "6#电芯")
  101. # # plt.scatter(problem_data['date'], problem_data['5'], c='red',marker='o', alpha=0.5, label = "7#电芯")
  102. # # plt.scatter(problem_data['date'], problem_data['6'], c='black',marker='o',alpha=0.5, label = "8#电芯")
  103. # # plt.scatter(problem_data['date'], problem_data['7'], c='green', marker='o',alpha=0.5, label = "9#电芯")
  104. # # plt.xlabel("时间", fontsize=15)
  105. # # plt.ylabel("电压排序", fontsize=15)
  106. # # title = problem_data['device_id'][0]
  107. # # plt.title(title, fontsize=20)
  108. # # plt.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+title+'.png', dpi=300)
  109. # # plt.show()
  110. # #print(problem_data)
  111. # end=time.time()
  112. # print('--------------计算时间:------------')
  113. # print(end-start)
  114. # # print(df_soh)
  115. #...............................................主函数.......................................................................................................................
  116. if __name__ == "__main__":
  117. excelpath=r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\sn.csv'
  118. # SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  119. # SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  120. # SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  121. # SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  122. # SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  123. # SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  124. # SNdata_didi = pd.read_csv(excelpath, encoding='gbk')
  125. SNdata_didi_trw = pd.read_csv(excelpath, encoding='gbk')
  126. # SNnums_6060=SNdata_6060['SN号'].tolist()
  127. # SNnums_6040=SNdata_6040['SN号'].tolist()
  128. # SNnums_4840=SNdata_4840['SN号'].tolist()
  129. # SNnums_L7255=SNdata_L7255['SN号'].tolist()
  130. # SNnums_C7255=SNdata_C7255['SN号'].tolist()
  131. # SNnums_U7255=SNdata_U7255['SN号'].tolist()
  132. # SNnums_didi = SNdata_didi['device_id'].tolist()
  133. SNnums_didi_trw = SNdata_didi_trw['device_id'].tolist()
  134. # SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  135. # SNnums=['BAA3219081550897']
  136. SNnums = SNnums_didi_trw
  137. mylog=log.Mylog('log_diag.txt','error')
  138. mylog.logcfg()
  139. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  140. #result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\result.csv',encoding='gbk')
  141. #df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
  142. print('----------------输入--------')
  143. #定时任务.......................................................................................................................................................................
  144. # scheduler = BlockingScheduler()
  145. # scheduler.add_job(CellVolt_rang, 'interval', seconds=720, id='diag_job')
  146. # try:
  147. # scheduler.start()
  148. # except Exception as e:
  149. # scheduler.shutdown()
  150. # print(repr(e))
  151. # mylog.logopt(e)
  152. print('---------------计算中-------------------------')
  153. start=time.time()
  154. for sn in SNnums:
  155. if 'PK500' in sn:
  156. celltype=1 #6040三元电芯
  157. elif 'PK502' in sn:
  158. celltype=2 #4840三元电芯
  159. elif 'K504B' in sn:
  160. celltype=99 #60ah林磷酸铁锂电芯
  161. elif 'MGMLXN750' in sn:
  162. celltype=3 #力信50ah三元电芯
  163. elif 'MGMCLN750' or 'UD' in sn:
  164. celltype=4 #CATL 50ah三元电芯
  165. else:
  166. print('SN:{},未找到对应电池类型!!!'.format(sn))
  167. continue
  168. # sys.exit()
  169. param=QX_BatteryParam.BatteryInfo(celltype)
  170. #读取结果数据库数据........................................................................................................................................................
  171. host='47.97.96.242'
  172. port=3306
  173. db='didi'
  174. user='root'
  175. password='qx123456'
  176. tablename='didi_data'
  177. param='date,device_id,bat_model,position,current,soc,celltemp,cellvolt_2,cellvolt_3,cellvolt_4,cellvolt_5,cellvolt_6,cellvolt_7,cellvolt_8,cellvolt_9'
  178. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  179. cursor = mysql.cursor()
  180. sql = "select %s from %s where device_id='%s'" %(param,tablename,sn)
  181. cursor.execute(sql)
  182. res = cursor.fetchall()
  183. df_bms= pd.DataFrame(res,columns=param.split(','))
  184. cursor.close()
  185. mysql.close()
  186. # mode=1
  187. # # tablename4='cellstateestimation_intershort'
  188. # DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  189. # with DBRead as DBRead:
  190. # df_data=DBRead.getdata('date,device_id,position,current,soc,cellvolt_2,cellvolt_3,cellvolt_4,cellvolt_5,cellvolt_6,cellvolt_7,cellvolt_8,cellvolt_9', tablename=tablename, sn=sn, timename='date', st=start_time, sp=end_time)
  191. # df_bms.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\origin_data\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  192. #电压排序................................................................................................................................................................
  193. df_temp_crnt = df_bms[df_bms['current']>1]#筛选充电数据
  194. df_temp = df_temp_crnt[df_temp_crnt['position']==2]#筛选充电数据
  195. df_chrgr = df_temp.reset_index(drop=True)
  196. df_chrgr_cellvolt = df_chrgr[['cellvolt_2','cellvolt_3','cellvolt_4','cellvolt_5','cellvolt_6','cellvolt_7','cellvolt_8','cellvolt_9']]
  197. df_chrgr_cellvolt_change = np.array(df_chrgr_cellvolt)#转数组
  198. df_chrgr_cellvolt_sort = np.argsort(df_chrgr_cellvolt_change)#取排序号
  199. df_cellvolt_sort_dif = np.diff(df_chrgr_cellvolt_sort,axis=0)#一次微分
  200. df_cellvolt_sort_dif_confir = np.nonzero(df_cellvolt_sort_dif)#取非0值
  201. Cell_num = set(df_cellvolt_sort_dif_confir[1])#寻找哪号电芯序号异常np.unique
  202. X_col=np.size(df_chrgr_cellvolt,0) #计算 X 的列数
  203. #df_cellvolt_sort_difdif = np.diff(df_cellvolt_sort_dif)#二次微分
  204. problem_data = pd.DataFrame()
  205. temp_list = []
  206. for item in Cell_num:
  207. temp_list.append(np.sum(df_cellvolt_sort_dif_confir[1]==item) > X_col/20)
  208. if any(temp_list):#序号变化的电芯
  209. data_temp = pd.DataFrame(df_chrgr_cellvolt_sort)
  210. #problem_data = pd.concat([df_chrgr,data_temp], axis = 1)
  211. problem_data = data_temp
  212. sn=sn.replace('/','')
  213. # volt_column = [0,1,2,3,4,5,6,7]
  214. # fig = plt.figure(figsize=(20,10))
  215. # length = len(problem_data)
  216. # num_length = np.arange(length)
  217. # for column in volt_column:
  218. # plt.scatter(num_length, problem_data[column], label=column)
  219. # plt.legend()
  220. # # title = problem_data['device_id'][0]
  221. # plt.title(sn)
  222. # plt.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+sn+'.png', dpi=300)
  223. # plt.show()
  224. if not problem_data.empty:
  225. problem_data.to_csv(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+'CBMS_diag_'+sn+'.csv',encoding='gbk')
  226. print(problem_data)
  227. ax=problem_data.plot(marker='*',markersize=15, figsize=(16,9))
  228. plt.xlabel('时间', fontsize=20)
  229. plt.ylabel('排序变化', fontsize=20)
  230. plt.xticks(fontsize=15)
  231. plt.yticks(fontsize=15)
  232. # plt.ylim(-30,30)
  233. plt.title(str(sn),fontsize=25)
  234. plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
  235. plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
  236. plt.legend(bbox_to_anchor=(1, 0), loc=3, fontsize=14)
  237. plt.show()
  238. fig = ax.get_figure()
  239. fig.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+str(sn)+'电压排序.png')
  240. # fig = plt.figure()
  241. # #ax = plt.subplot()
  242. # plt.scatter(problem_data['date'], problem_data['0'], c='black',marker='o',alpha=0.5, label = "2#电芯")
  243. # plt.scatter(problem_data['date'], problem_data['1'], c='green', marker='o',alpha=0.5, label = "3#电芯")
  244. # plt.scatter(problem_data['date'], problem_data['2'], c='red',marker='o', alpha=0.5, label = "4#电芯")
  245. # plt.scatter(problem_data['date'], problem_data['3'], c='black',marker='o',alpha=0.5, label = "5#电芯")
  246. # plt.scatter(problem_data['date'], problem_data['4'], c='green', marker='o',alpha=0.5, label = "6#电芯")
  247. # plt.scatter(problem_data['date'], problem_data['5'], c='red',marker='o', alpha=0.5, label = "7#电芯")
  248. # plt.scatter(problem_data['date'], problem_data['6'], c='black',marker='o',alpha=0.5, label = "8#电芯")
  249. # plt.scatter(problem_data['date'], problem_data['7'], c='green', marker='o',alpha=0.5, label = "9#电芯")
  250. # plt.xlabel("时间", fontsize=15)
  251. # plt.ylabel("电压排序", fontsize=15)
  252. # title = problem_data['device_id'][0]
  253. # plt.title(title, fontsize=20)
  254. # plt.savefig(r'D:\Work\Code_write\data_analyze_platform\test\lzx\01Qixiang\01电压排序\01算法\DBDownload\\'+title+'.png', dpi=300)
  255. # plt.show()
  256. #print(problem_data)
  257. end=time.time()
  258. print('--------------计算时间:------------')
  259. print(end-start)
  260. # print(df_soh)