from ctypes import Structure from re import M import pandas as pd import numpy as np import datetime import time, datetime import matplotlib.pyplot as plt from pylab import* from scipy.signal import savgol_filter from sklearn.linear_model import LinearRegression import os import log def get_file(): #创建一个空列表 files =os.listdir(path) files.sort() #排序 file_list= [] for file in files: if not os.path.isdir(path +file): #判断该文件是否是一个文件夹 f_name = str(file) # print(f_name) tr = '\\' #多增加一个斜杠 filename = path + tr + f_name file_list.append(filename) return file_list mylog=log.Mylog('log_diag.txt','error') mylog.logcfg() fig = plt.figure(figsize=(20,15)) path = r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\02析锂检测\02析锂分析\各项目析锂情况_211130\增加波峰高度与峰谷比计算结果\析锂前后五电池的soh' file_list = get_file() a1 = [] b1 = [] for file in file_list: df_time = [] data = pd.read_csv(file,encoding='GB18030') # fig = plt.figure(figsize=(20,10)) df_fig = data df_fig = df_fig.reset_index(drop=True) length = len(df_fig) df_soh_temp = df_fig['soh'] df_soh = savgol_filter(df_soh_temp, 11, 3) df_hms_temp = pd.to_datetime(df_fig['time_sp'])#转换时间格式 # df_hms = df_hms_temp.dt.time#仅保留时分秒 df_hms_fig = df_hms_temp.apply(lambda x:x.strftime('%Y-%m-%d')) temp = file.split('\\') sn_name = temp[-1].split('.')[0] title = sn_name time_reshape = np.array(df_hms_temp).reshape(-1,1) soh_reshape = df_soh.reshape(-1,1) delta_time = (np.diff(df_hms_temp)/pd.Timedelta(1, 'days'))#计算时间差 df_time_len = len(delta_time) for i in range(df_time_len): df_time.append(sum(delta_time[0:i])) soh_reshape = np.delete(soh_reshape, [-1]) # b1.append(lineModel.intercept_[0])#对应sn #新建一个线性回归模型,并把数据放进去对模型进行训练 lineModel = LinearRegression() lineModel.fit(np.array(df_time).reshape(-1,1), soh_reshape) # a1.append(lineModel.coef_[0][0])#拟合系数 fit_soh = lineModel.predict(np.array(df_time).reshape(-1,1)) fit_soh = 100*fit_soh/fit_soh[0] # plt.scatter([x for x in range(0, length)], df_fig[volt_num][0:length], label=volt_num) plt.plot(df_time, fit_soh,linewidth = 2, linestyle = '-', marker = 's',label=title) mpl.rcParams['font.sans-serif']=['KaiTi'] mpl.rcParams['axes.unicode_minus']=False #用来正常显示负号 plt.legend(fontsize=20) plt.xlabel('时间(天)', fontsize=26) plt.ylabel('soh随时间演化', fontsize=26) plt.xticks(range(1,len(df_time),5),rotation=45, fontsize=26) plt.yticks(fontsize=26) # plt.xlim((df_hms_temp[0]-datetime.timedelta(minutes=5)).dt.time,(df_hms_temp[0] + datetime.timedelta(hours=1.5)).dt.time) # plt.ylim(-2.5,0.5) plt.title('析锂量前5与后5的电池包soh演化', fontsize=28) # plt.show() plt.savefig(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\02析锂检测\02析锂分析\各项目析锂情况_211130\增加波峰高度与峰谷比计算结果\soh图片\\'+'析锂量前5与后5电池包的soh.png', dpi=300)