ExcessTemp.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from LIB.BACKEND import DBManager
  2. dbManager = DBManager.DBManager()
  3. import pandas as pd
  4. import numpy as np
  5. import datetime
  6. import seaborn as sns
  7. import matplotlib.pyplot as plt
  8. now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #type: str
  9. now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S') #type: datetime
  10. start_time=now_time-datetime.timedelta(minutes=1)
  11. end_time=str(now_time)
  12. start_time=str(start_time)
  13. def makedataset(cellname):
  14. dataset = pd.DataFrame()
  15. for k in range(len(cellname)):
  16. sn = cellname[k]
  17. datasn = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  18. datasn = datasn['bms']
  19. datasn['SN号']=sn
  20. if len(datasn)>0:
  21. datasn=datasn.iloc[-1]
  22. dataset=dataset.append(datasn)
  23. return dataset
  24. fileNamesPK504 = pd.read_excel('sn-20210903.xlsx',sheet_name='科易6060')
  25. fileNamesPK500 = pd.read_excel('sn-20210903.xlsx',sheet_name='科易6040')
  26. fileNamesPK502 = pd.read_excel('sn-20210903.xlsx',sheet_name='科易4840')
  27. fileNamesMGML = pd.read_excel('sn-20210903.xlsx',sheet_name='格林美-力信7255')
  28. fileNamesMGMC = pd.read_excel('sn-20210903.xlsx',sheet_name='格林美-CATL7255')
  29. fileNamesUDO = pd.read_excel('sn-20210903.xlsx',sheet_name='优旦7255')
  30. dataPK504=makedataset(list(fileNamesPK504['SN号']))
  31. dataPK500=makedataset(list(fileNamesPK500['SN号']))
  32. dataPK502=makedataset(list(fileNamesPK502['SN号']))
  33. dataMGML=makedataset(list(fileNamesMGML['SN号']))
  34. dataMGMC=makedataset(list(fileNamesMGMC['SN号']))
  35. dataUDO=makedataset(list(fileNamesUDO['SN号']))
  36. dataPK504=dataPK504[['SN号','单体温度1','单体温度2','单体温度3','单体温度4','其他温度2','其他温度4','其他温度5']]
  37. dataPK500=dataPK500[['SN号','单体温度1','单体温度2','单体温度3','单体温度4','其他温度2','其他温度3','其他温度4','其他温度5']]
  38. dataPK502=dataPK502[['SN号','单体温度1','单体温度2','单体温度3','单体温度4','其他温度2','其他温度3','其他温度4','其他温度5']]
  39. dataMGML=dataMGML[['SN号','单体温度1','单体温度2','单体温度3','单体温度4','其他温度1','其他温度3','其他温度4','其他温度5','其他温度6']]
  40. dataMGMC=dataMGMC[['SN号','单体温度1','单体温度2']]
  41. dataUDO=dataUDO[['SN号','单体温度1','单体温度2']]
  42. datatotal1=pd.concat([dataPK504,dataPK500,dataPK502])
  43. datatotal2=pd.concat([dataMGMC,dataUDO])
  44. def calculavg(datacell):
  45. avg_temp=[]
  46. max_temp=[]
  47. min_temp=[]
  48. for i in range(len(datacell)):
  49. avg_temp.append(np.mean(datacell.iloc[i,1:]))
  50. max_temp.append(max(datacell.iloc[i,1:]))
  51. min_temp.append(min(datacell.iloc[i,1:]))
  52. datacell['平均温度']=avg_temp
  53. datacell['最高温度']=max_temp
  54. datacell['最低温度']=min_temp
  55. return datacell
  56. datatotal1=calculavg(datatotal1)
  57. datatotal2=calculavg(datatotal2)
  58. dataMGML=calculavg(dataMGML)
  59. datatotal1=datatotal1[datatotal1['最低温度']>-40]
  60. datatotal2=datatotal2[datatotal2['最低温度']>-40]
  61. dataMGML=dataMGML[dataMGML['最低温度']>-40]
  62. datatotal1=datatotal1.reset_index(drop=True)
  63. datatotal2=datatotal2.reset_index(drop=True)
  64. dataMGML=dataMGML.reset_index(drop=True)
  65. def boxplot_fill(col,a):
  66. # 计算iqr:数据四分之三分位值与四分之一分位值的差
  67. iqr = col.quantile(0.75)-col.quantile(0.25)
  68. # 根据iqr计算异常值判断阈值
  69. u_th = col.quantile(0.75) + a*iqr # 上界
  70. l_th = col.quantile(0.25) - a*iqr # 下界
  71. # 定义转换函数:如果数字大于上界则用上界值填充,小于下界则用下界值填充。
  72. return l_th,u_th
  73. uptemp_out1=list(boxplot_fill(datatotal1['最高温度'],2.5))[1]
  74. uptemp_out2=list(boxplot_fill(datatotal2['最高温度'],5))[1]
  75. uptemp_out3=list(boxplot_fill(dataMGML['最高温度'],3.5))[1]
  76. anomalies1 = datatotal1[(datatotal1['最高温度']>uptemp_out1) & (datatotal1['最高温度']<120) & (datatotal1['最高温度']!=datatotal1['单体温度4']) & (datatotal1['单体温度4']<50) & (datatotal1['最高温度']!=datatotal1['其他温度2']) & (datatotal1['其他温度2']<50)]
  77. anomalies2 = datatotal2[(datatotal2['最高温度']>uptemp_out2) & (datatotal2['最高温度']<120)]
  78. anomalies3 = dataMGML[(dataMGML['最高温度']>uptemp_out3) & (dataMGML['最高温度']<120)]
  79. anomalies=pd.concat([anomalies1,anomalies2,anomalies3])