{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ctypes import Structure\n", "from re import M\n", "import pandas as pd\n", "import numpy as np\n", "import datetime\n", "import time, datetime\n", "import matplotlib.pyplot as plt\n", "from pylab import*\n", "from scipy.signal import savgol_filter\n", "import os\n", "\n", "path = r'D:\\Work\\Code_write\\data_analyze_platform\\USER\\lzx\\05问题查询\\PK504B00100004003内阻可疑-211206\\原始数据'\n", " \n", "def get_file(): #创建一个空列表\n", " files =os.listdir(path)\n", " files.sort() #排序\n", " file_list= []\n", " for file in files:\n", " if not os.path.isdir(path +file): #判断该文件是否是一个文件夹 \n", " f_name = str(file) \n", "# print(f_name)\n", " tr = '\\\\' #多增加一个斜杠\n", " filename = path + tr + f_name \n", " file_list.append(filename) \n", " return file_list \n", "\n", "file_list = get_file()\n", "for file in file_list:\n", " data = pd.read_csv(file,encoding='GB18030')\n", " #----------------------------------------定义滤波函数-----------------------------------------------------------------------\n", " def moving_average(interval, windowsize):\n", " window = np.ones(int(windowsize)) / float(windowsize)\n", " re = np.convolve(interval, window, 'same')\n", " return re\n", " #----------------------------------------区分充放电数据----------------------------------------------------------------------\n", " df_soc = data['SOC[%]']\n", " # df_soc[df_soc >= 99] = 100\n", " # df_soc_flt = moving_average(df_soc, 3)#SOC滤波\n", " df_soc_len = len(df_soc)\n", " df_soc_add = df_soc.iloc[df_soc_len-3:df_soc_len-1]#由于对SOC二次微分,增加两行数据\n", " df_soc_new = df_soc.append(df_soc_add)\n", " # df_soc_cal = savgol_filter(df_soc_new, 13, 3)\n", " df_soc_new_dif = np.diff(df_soc_new,axis=0)#电压一次微分,判断升降\n", " df_soc_new_dif = pd.DataFrame(df_soc_new_dif)\n", " df_soc_new_dif[df_soc_new_dif > 0] = 1#SOC升高\n", " df_soc_new_dif[df_soc_new_dif == 0] = 0#SOC升高或静置\n", " df_soc_new_dif[df_soc_new_dif < 0] = -1#SOC下降\n", " df_soc_dif_arr = np.array(df_soc_new_dif)\n", " pos_change = nonzero(df_soc_dif_arr)#寻找非零元素位置\n", " value_change = df_soc_dif_arr[pos_change[0]]#非零元素数值\n", " value_change_dif = np.diff(value_change)#对非零元素值二次微分\n", " peak_pos = np.where(value_change_dif == -2)#充电结束点\n", " bot_pos = np.where(value_change_dif == 2)#放电结束点\n", " # df_soc_new_difdif = np.diff(df_soc_new_dif,axis=0)#三次微分,利用-2,2判断充电-静置与放电-静置的区别\n", " # df_soc_new_difdif = pd.DataFrame(df_soc_new_difdif)\n", " # peak_pos = np.where(df_soc_new_difdif == -2)\n", " # bot_pos = np.where(df_soc_new_difdif == 2)\n", " print(pos_change)\n", " # print(df_soc)\n", " # print(df_soc_new_dif)\n", " print(value_change)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([ 6855, 6860, 9481, 9510, 9519, 9538, 9554, 9564, 9618,\n", " 9637, 9681, 9687, 9694, 9704, 9722, 9752, 9812, 9845,\n", " 9892, 9916, 9945, 9974, 10004, 10021, 10033, 10053, 10068,\n", " 10123, 10138, 10150, 10184, 10271, 10374, 10387, 10406, 10449,\n", " 10459, 10510, 10554, 10567, 10599, 10605], dtype=int64), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " dtype=int64))\n", "[[ 1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]\n", " [-1]]\n" ] } ], "source": [ "print(pos_change)\n", "print(value_change)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([ 6855, 6860, 9481, 9510, 9519, 9538, 9554, 9564, 9618,\n", " 9637, 9681, 9687, 9694, 9704, 9722, 9752, 9812, 9845,\n", " 9892, 9916, 9945, 9974, 10004, 10021, 10033, 10053, 10068,\n", " 10123, 10138, 10150, 10184, 10271, 10374, 10387, 10406, 10449,\n", " 10459, 10510, 10554, 10567, 10599, 10605], dtype=int64), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " dtype=int64))\n", "[ 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n", " -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]\n", "\n" ] } ], "source": [ "#-------------------------------------------------------利用soc的升降判断充放电---------------------------------------------------------\n", "pos_change_dif = np.diff(pos_change[0])#SOC变化时的位置信息\n", "pos_change_del_temp = np.where(pos_change_dif < 10)\n", "pos_change_del = pos_change_del_temp[0] + 1\n", "value_change_new = np.delete(value_change[:,0], pos_change_del)#SOC发生变化数值\n", "pos_change_new = np.delete(pos_change[0], pos_change_del)#SOC发生变化位置\n", "print(pos_change)\n", "print(value_change_new)\n", "value_change_new_dif = np.diff(value_change_new)#寻找充电或放电结束位置\n", "value_change_new_dif = np.insert(value_change_new_dif, 0, value_change_new_dif[0])\n", "chrgr_dischrgr_pos_temp = np.where(abs(value_change_new_dif) == 2)#寻找充电或放电结束位置\n", "splice_num = []\n", "if len(chrgr_dischrgr_pos_temp[0]) >= 1:\n", " chrgr_dischrgr_pos = pos_change_new[chrgr_dischrgr_pos_temp[0]]#原数据中充电或放电结束位置\n", " chrgr_dischrgr_pos = np.insert(chrgr_dischrgr_pos, 0, 0)\n", " data_len = len(df_soc)#数据长度\n", " pos_len = len(chrgr_dischrgr_pos)#切片长度\n", " chrgr_dischrgr_pos = np.insert(chrgr_dischrgr_pos, pos_len, data_len-1)#\n", " print(type(splice_num))\n", " for item in range(0,len(chrgr_dischrgr_pos)-1):\n", " splice_num.extend(item*np.ones(chrgr_dischrgr_pos[item +1]-chrgr_dischrgr_pos[item]))\n", " splice_num = np.insert(splice_num, data_len-2, item)\n", "else:\n", " splice_num = np.zeros(len(df_soc))\n", " pos_ful = np.array([0])\n", "data['stat'] = splice_num" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 6855, 9481, 12141], dtype=int64)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chrgr_dischrgr_pos" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#--------------------------------------------------------筛选充电数据与放电数据-------------------------------------\n", "chrgr_data = pd.DataFrame()\n", "dischrgr_data = pd.DataFrame()\n", "for num_chrgr in range(0,len(chrgr_dischrgr_pos)-1):\n", " df_data_temp = data.loc[(data['stat'] == num_chrgr)]#筛选每段数据\n", " data_splice_len = len(df_data_temp)\n", " if np.count_nonzero(df_data_temp['总电流[A]'] > 0) > 0.3*data_splice_len:\n", " chrgr_data_temp = df_data_temp.loc[df_data_temp['总电流[A]'] >= 0]#筛选具有充电电流的充电数据\n", " chrgr_data = chrgr_data.append(chrgr_data_temp)\n", " chrgr_data = chrgr_data.reset_index(drop = True)\n", " else:\n", " dischrgr_data_temp = df_data_temp.loc[df_data_temp['总电流[A]'] <= 0]#筛选具有放电电流的放电数据\n", " dischrgr_data = dischrgr_data.append(dischrgr_data_temp)\n", " dischrgr_data = dischrgr_data.reset_index(drop = True)\n", "chrgr_num_difference = np.unique(chrgr_data['stat'])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Unnamed: 0 时间戳 GSM信号 故障等级 故障代码 总电流[A] 总电压[V] \\\n", "9481 9481 2021-12-02 20:29:00 19 0 0 0.2 65.5 \n", "9483 9483 2021-12-02 20:29:44 30 0 0 0.0 65.6 \n", "9484 9484 2021-12-02 20:29:54 30 0 0 0.6 65.6 \n", "9485 9485 2021-12-02 20:30:03 31 0 0 0.5 65.6 \n", "9486 9486 2021-12-02 20:30:13 31 0 0 0.4 65.7 \n", "... ... ... ... ... ... ... ... \n", "12137 12137 2021-12-03 23:55:59 23 0 0 0.0 65.6 \n", "12138 12138 2021-12-03 23:56:59 23 0 0 0.0 65.6 \n", "12139 12139 2021-12-03 23:57:59 23 0 0 0.0 65.6 \n", "12140 12140 2021-12-03 23:58:59 23 0 0 0.0 65.6 \n", "12141 12141 2021-12-03 23:59:59 23 0 0 0.0 65.5 \n", "\n", " 外电压 总输出状态 上锁状态 ... 单体温度1 单体温度2 单体温度3 单体温度4 其他温度1 其他温度2 其他温度3 \\\n", "9481 0 3 0 ... 13 14 14 14 16 15 13 \n", "9483 0 3 0 ... 14 14 14 13 18 16 13 \n", "9484 0 3 0 ... 14 14 14 14 18 16 13 \n", "9485 0 3 0 ... 14 14 14 14 18 16 13 \n", "9486 0 3 0 ... 13 14 14 14 18 16 13 \n", "... ... ... ... ... ... ... ... ... ... ... ... \n", "12137 0 3 0 ... 14 14 14 14 15 15 13 \n", "12138 0 3 0 ... 14 14 14 14 15 15 13 \n", "12139 0 3 0 ... 14 14 14 14 15 15 13 \n", "12140 0 3 0 ... 14 14 14 14 15 15 13 \n", "12141 0 3 0 ... 14 14 14 14 15 15 13 \n", "\n", " 其他温度4 其他温度5 stat \n", "9481 15 14 2.0 \n", "9483 15 13 2.0 \n", "9484 15 14 2.0 \n", "9485 15 14 2.0 \n", "9486 15 14 2.0 \n", "... ... ... ... \n", "12137 15 14 2.0 \n", "12138 15 14 2.0 \n", "12139 15 14 2.0 \n", "12140 15 14 2.0 \n", "12141 15 14 2.0 \n", "\n", "[2590 rows x 47 columns]\n" ] } ], "source": [ "print(chrgr_data_temp)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "LookTab_SOC = [0.00, \t2.40, \t6.38, \t10.37, \t14.35, \t18.33, \t22.32, \t26.30, \t30.28, \t35.26, \t40.24, \t45.22, \t50.20, \t54.19, \t58.17, \t60.16, \t65.14, \t70.12, \t75.10, \t80.08, \t84.06, \t88.05, \t92.03, \t96.02, \t100.00, 105]\n", "LookTab_OCV = [2.7151,\t3.0298,\t3.1935,\t3.2009,\t3.2167,\t3.2393,\t3.2561,\t3.2703,\t3.2843,\t3.2871,\t3.2874,\t3.2868,\t3.2896,\t3.2917,\t3.2967,\t3.3128,\t3.3283,\t3.3286,\t3.3287,\t3.3288,\t3.3289,\t3.3296,\t3.3302,\t3.3314,\t3.3429, 3.6]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 单体电压1 单体电压2 单体电压3 单体电压4 单体电压5 单体电压6 单体电压7 单体电压8 单体电压9 单体电压10 \\\n", "787 3.281 3.286 3.283 3.284 3.283 3.283 3.286 3.286 3.287 3.285 \n", "\n", " 单体电压11 单体电压12 单体电压13 单体电压14 单体电压15 单体电压16 单体电压17 单体电压18 单体电压19 \\\n", "787 3.282 3.288 3.287 3.284 3.284 3.286 3.289 3.289 3.288 \n", "\n", " 单体电压20 \n", "787 3.287 \n" ] } ], "source": [ "#--------------------------------------------------------统计充电截止电压、充电SOC区间---------------------------------------------------------\n", "from scipy import interpolate\n", "cell_name = ['单体电压'+str(x) for x in range(1,21)]#电芯电压名称\n", "chrgr_volt = pd.DataFrame()\n", "chrgr_fin_volt = pd.DataFrame()\n", "chrgr_soc = pd.DataFrame()\n", "f = interpolate.interp1d(LookTab_OCV, LookTab_SOC, kind = 'linear')#差值函数\n", "for chrgr_num in chrgr_num_difference:\n", " data_temp = chrgr_data.loc[chrgr_data['stat'] == chrgr_num]#该片段数据\n", " chrgr_crnt_data = data_temp.loc[data_temp['总电流[A]'] > 0]#充电数据中有电流部分,充电时电流为正\n", " chrgr_crnt_data = chrgr_crnt_data.reset_index(drop = True)\n", " chrgr_crnt_zero_before = data_temp.loc[data_temp['时间戳'] < chrgr_crnt_data['时间戳'][0]]#充电数据中出现电流前数据\n", " chrgr_crnt_zero_after = data_temp.loc[data_temp['时间戳'] > chrgr_crnt_data.iloc[-1]['时间戳']]#充电数据中出现电流前数据\n", " chrgr_crnt_zero_before = chrgr_crnt_zero_before.reset_index(drop = True)\n", " chrgr_crnt_zero_after = chrgr_crnt_zero_after.reset_index(drop = True)\n", " time_before = pd.to_datetime(chrgr_crnt_zero_before['时间戳'])\n", " time_after = pd.to_datetime(chrgr_crnt_zero_after['时间戳'])\n", " chrgr_rest_len = len(time_after)\n", " if chrgr_rest_len > 1:\n", " if (time_after[chrgr_rest_len - 1] - time_after[0])/pd.Timedelta(1, 'min') > 10:\n", " # chrgr_volt = chrgr_volt.append(chrgr_crnt_zero_before.iloc[-1])#充电开始前静置电压\n", " chrgr_volt = chrgr_volt.append(chrgr_crnt_zero_after.iloc[-1])#充电结束静置电压\n", " df_cell_volt = chrgr_volt[cell_name]/1000\n", " df_cell_volt[df_cell_volt >= 3.6] = 3.595\n", " df_cell_volt[df_cell_volt <= 2.715] = 2.72\n", " print(df_cell_volt)\n", " chrgr_soc = f(df_cell_volt)\n", "# for cell_num in cell_name:#获取各个电芯充电结束后的SOC\n", "# chrgr_cellsoc = f(chrgr_volt[cell_num]/1000)\n", "# chrgr_cellsoc_temp = pd.DataFrame(chrgr_cellsoc)\n", "# chrgr_soc = pd.concat([chrgr_soc, chrgr_cellsoc_temp], axis = 1)#按列合并各个电芯\n", "# chrgr_soc = chrgr_soc.reset_index(drop = True)\n", "# chrgr_soc.columns = cell_name\n", "# chrgr_soc['时间戳'] = list(chrgr_volt['时间戳'])\n", "# chrgr_soc['stat'] = list(chrgr_volt['stat'])\n", "# chrgr_soc['soc_delta'] = np.max(chrgr_soc[cell_name], axis = 1) - np.min(chrgr_soc[cell_name], axis = 1)\n", "# chrgr_soc['sn'] = 'sn'\n", "# chrgr_soc.to_csv(r'D:\\Work\\Code_write\\data_analyze_platform\\USER\\lzx\\05问题查询\\PK504B00100004003内阻可疑-211206\\充电行为统计\\充电截止SOC.csv',index=False,encoding='GB18030')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.0\n", "1 0.0\n", "2 0.0\n", "3 0.0\n", "4 0.0\n", " ... \n", "12137 2.0\n", "12138 2.0\n", "12139 2.0\n", "12140 2.0\n", "12141 2.0\n", "Name: stat, Length: 12142, dtype: float64\n" ] } ], "source": [ "print(data['stat'])" ] } ], "metadata": { "interpreter": { "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f" }, "kernelspec": { "display_name": "Python 3.8.8 64-bit ('base': conda)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }