|
@@ -21,7 +21,7 @@ import re
|
|
|
#...................................故障检测函数......................................................................................................................
|
|
|
def diag_cal():
|
|
|
global SNnums
|
|
|
- global scaler1,scaler2,model1,model2,col1,col2,time_steps1,time_steps2
|
|
|
+ global list_faults,list_scalers,list_models,list_cols,list_time_steps,list_features
|
|
|
|
|
|
start=time.time()
|
|
|
now_time=datetime.datetime.now()
|
|
@@ -37,53 +37,60 @@ def diag_cal():
|
|
|
password='Qx@123456'
|
|
|
|
|
|
#读取结果库数据......................................................
|
|
|
- param='product_id,start_time,end_time,diff_min,SOC,AnoScoreV_sum_max,AnoScoreV_max_max,AnoScoreT_sum_max,AnoScoreT_max_max'
|
|
|
- tablename='fault_detection'
|
|
|
+ param='product_id,start_time,end_time,fault_class,update_time'
|
|
|
+ tablename='fault_class'
|
|
|
mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
|
|
|
cursor = mysql.cursor()
|
|
|
sql = "select {} from {} where end_time='0000-00-00 00:00:00'".format(param,tablename)
|
|
|
cursor.execute(sql)
|
|
|
res = cursor.fetchall()
|
|
|
df_diag_ram= pd.DataFrame(res,columns=param.split(','))
|
|
|
-
|
|
|
|
|
|
db_res_engine = create_engine(
|
|
|
"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
|
|
|
user, parse.quote_plus(password), host, port, db
|
|
|
))
|
|
|
|
|
|
-
|
|
|
#调用主函数................................................................................................................................................................
|
|
|
for sn in SNnums:
|
|
|
try:
|
|
|
- df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
|
|
|
- data_bms = df_data['bms']
|
|
|
- data_bms['sn']=sn
|
|
|
- if len(data_bms)>0:
|
|
|
- logger.info("SN: {} 数据开始预处理".format(sn))
|
|
|
- data_fea1=features1(data_bms)
|
|
|
- data_fea2=features2(data_bms)
|
|
|
- logger.info("SN: {} 数据开始模型预测".format(sn))
|
|
|
- df_res1=pred(data_fea1,model1,scaler1,col1,end_time,time_steps1)
|
|
|
- df_res2=pred(data_fea2,model2,scaler2,col2,end_time,time_steps2)
|
|
|
- df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
|
|
|
- res_new1,res_update1=arrange2(df_diag_ram_sn,df_res1,start_time,'B板采样失效')
|
|
|
- res_new2,res_update2=arrange2(df_diag_ram_sn,df_res2,start_time,'传感器_电芯NTC漂移')
|
|
|
-
|
|
|
- if len(res_update1)>0:
|
|
|
- cursor.execute("DELETE FROM fault_class WHERE end_time = '0000-00-00 00:00:00' and product_id='{}' and fault_class='{}'".format(sn,'B板采样失效'))
|
|
|
- mysql.commit()
|
|
|
- res_update1.to_sql("fault_class",con=db_res_engine, if_exists="append",index=False)
|
|
|
- res_new1.to_sql("fault_class",con=db_res_engine, if_exists="append",index=False)
|
|
|
- if len(res_update2)>0:
|
|
|
- cursor.execute("DELETE FROM fault_class WHERE end_time = '0000-00-00 00:00:00' and product_id='{}' and fault_class='{}'".format(sn,'传感器_电芯NTC漂移'))
|
|
|
- mysql.commit()
|
|
|
- res_update2.to_sql("fault_class",con=db_res_engine, if_exists="append",index=False)
|
|
|
- res_new2.to_sql("fault_class",con=db_res_engine, if_exists="append",index=False)
|
|
|
-
|
|
|
- #新增结果存入结果库................................................................
|
|
|
-
|
|
|
-
|
|
|
+ #sn='PK50001A100000248'
|
|
|
+ #start_time='2022-11-18 00:00:00'
|
|
|
+ #end_time='2022-11-18 03:00:00'
|
|
|
+ group=sn[:2]
|
|
|
+ if group in ['PK','MG','UD']:
|
|
|
+ df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
|
|
|
+ data_bms = df_data['bms']
|
|
|
+ data_bms['sn']=sn
|
|
|
+ if len(data_bms)>0:
|
|
|
+ df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
|
|
|
+ for i in range(len(list_faults)):
|
|
|
+ fault=list_faults[i]
|
|
|
+ df_diag_ram_sn_fault=df_diag_ram_sn[df_diag_ram_sn['fault_class']==fault]
|
|
|
+ if len(df_diag_ram_sn_fault)>0:
|
|
|
+ df_diag_ram_sn_fault.reset_index(drop=True,inplace=True)
|
|
|
+ df_diag_ram_sn_fault=df_diag_ram_sn_fault.iloc[0]
|
|
|
+ logger.info("SN: {} 数据开始预处理".format(sn))
|
|
|
+ if fault=='B板采样失效':
|
|
|
+ data_fea=features1(data_bms)
|
|
|
+ elif fault=='传感器_电芯NTC漂移':
|
|
|
+ data_fea=features2(data_bms)
|
|
|
+ else:
|
|
|
+ feas=list_features[i]
|
|
|
+ data_fea=features_filtre(data_bms,feas)
|
|
|
+ logger.info("SN: {} 数据开始模型预测".format(sn))
|
|
|
+ modeli=list_models[i]
|
|
|
+ scaleri=list_scalers[i]
|
|
|
+ coli=list_cols[i]
|
|
|
+ time_stepsi=list_time_steps[i]
|
|
|
+ df_res=pred(data_fea,modeli,scaleri,coli,end_time,time_stepsi)
|
|
|
+ res_new,res_update=arrange2(df_diag_ram_sn_fault,df_res,time_stepsi)
|
|
|
+ if len(res_update)>0:
|
|
|
+ cursor.execute("DELETE FROM fault_class WHERE end_time = '0000-00-00 00:00:00' and product_id='{}' and fault_class='{}'".format(sn,fault))
|
|
|
+ mysql.commit()
|
|
|
+ res_update.to_sql("fault_class",con=db_res_engine, if_exists="append",index=False)
|
|
|
+ res_new.to_sql("fault_class",con=db_res_engine, if_exists="append",index=False)
|
|
|
+
|
|
|
# end=time.time()
|
|
|
# print(end-start)
|
|
|
|
|
@@ -125,7 +132,7 @@ if __name__ == "__main__":
|
|
|
|
|
|
logger.info("pid is {}".format(os.getpid()))
|
|
|
|
|
|
- # # 更新sn列表
|
|
|
+ # # 更新sn列表
|
|
|
host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
|
|
|
port=3306
|
|
|
db='qixiang_oss'
|
|
@@ -138,17 +145,68 @@ if __name__ == "__main__":
|
|
|
df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
|
|
|
df_sn = df_sn.reset_index(drop=True)
|
|
|
conn.close();
|
|
|
+
|
|
|
+ #用户输出参数数据库配置
|
|
|
+ host0='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
|
|
|
+ port0=3306
|
|
|
+ db0='qx_cas'
|
|
|
+ user0='qx_algo_rw'
|
|
|
+ password0='qx@123456'
|
|
|
+
|
|
|
+ #读取用户输出参数......................................................
|
|
|
+ param='fault_name,class,time_steps,accuracy'
|
|
|
+ tablename='faultclass_output1'
|
|
|
+ mysql = pymysql.connect (host=host0, user=user0, password=password0, port=port0, database=db0)
|
|
|
+ cursor = mysql.cursor()
|
|
|
+ sql = "select {} from {}".format(param,tablename)
|
|
|
+ cursor.execute(sql)
|
|
|
+ res = cursor.fetchall()
|
|
|
+ list_param= pd.DataFrame(res,columns=param.split(','))
|
|
|
+ list_param.reset_index(drop=True,inplace=True)
|
|
|
+
|
|
|
+ #读取用户输入参数......................................................
|
|
|
+ param0='fault_name,cols,col_key,compare,threshold_filtre,time_steps,nbr_features,nbr_neurons,nbr_class,batch_size,epochs,dropout,lr,activation,loss,threshold_accuracy'
|
|
|
+ tablename0='faultclass_input'
|
|
|
+ mysql0 = pymysql.connect (host=host0, user=user0, password=password0, port=port0, database=db0)
|
|
|
+ cursor0 = mysql0.cursor()
|
|
|
+ sql0 = "select {} from {} ".format(param0,tablename0)
|
|
|
+ cursor0.execute(sql0)
|
|
|
+ res0 = cursor0.fetchall()
|
|
|
+ list_param0= pd.DataFrame(res0,columns=param0.split(','))
|
|
|
+ list_param0.reset_index(drop=True,inplace=True)
|
|
|
|
|
|
SNnums = list(df_sn['sn'])
|
|
|
|
|
|
- scaler1=pickle.load(open('LIB/MIDDLE/FaultClass/V1_0_0/models/scaler_B板采样失效.pkl','rb'))
|
|
|
- scaler2=pickle.load(open('LIB/MIDDLE/FaultClass/V1_0_0/models/scaler_传感器_电芯NTC漂移.pkl','rb'))
|
|
|
- model1=load_model('LIB/MIDDLE/FaultClass/V1_0_0/models/model_B板采样失效.h5')
|
|
|
- model2=load_model('LIB/MIDDLE/FaultClass/V1_0_0/models/model_传感器_电芯NTC漂移.h5')
|
|
|
- col1=['B板采样失效','正常']
|
|
|
- col2=['传感器_电芯NTC漂移','正常']
|
|
|
- time_steps1=60
|
|
|
- time_steps2=60
|
|
|
+ # # 加载模型
|
|
|
+ list_model=os.listdir('LIB/MIDDLE/FaultClass/V1_0_0/models')
|
|
|
+ list_faults=[]
|
|
|
+ list_scalers=[]
|
|
|
+ list_models=[]
|
|
|
+ list_cols=[]
|
|
|
+ list_time_steps=[]
|
|
|
+ list_features=[]
|
|
|
+ for k in range(len(list_model)):
|
|
|
+ fault_name=list_model[k][6:-3]
|
|
|
+ list_faults.append(fault_name)
|
|
|
+ scaler=pickle.load(open('LIB/MIDDLE/FaultClass/V1_0_0/scalers/scaler_'+fault_name+'.pkl','rb'))
|
|
|
+ list_scalers.append(scaler)
|
|
|
+ model=load_model('LIB/MIDDLE/FaultClass/V1_0_0/models/'+list_model[k])
|
|
|
+ list_models.append(model)
|
|
|
+ if fault_name=='B板采样失效':
|
|
|
+ col=['B板采样失效','正常']
|
|
|
+ time_steps=60
|
|
|
+ feature=['feature1']
|
|
|
+ elif fault_name=='传感器_电芯NTC漂移':
|
|
|
+ col=['传感器_电芯NTC漂移','正常']
|
|
|
+ time_steps=60
|
|
|
+ feature=['feature2']
|
|
|
+ else:
|
|
|
+ col=eval(list_param.loc[list_param[list_param['fault_name']==fault_name].index,'class'].values[0])
|
|
|
+ time_steps=list_param.loc[list_param[list_param['fault_name']==fault_name].index,'time_steps'].values[0]
|
|
|
+ feature=eval(list_param0.loc[list_param0[list_param0['fault_name']==fault_name].index,'cols'].values[0])
|
|
|
+ list_cols.append(col)
|
|
|
+ list_time_steps.append(time_steps)
|
|
|
+ list_features.append(feature)
|
|
|
logger.info("模型加载完成")
|
|
|
|
|
|
diag_cal()
|