{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### start to get data PK504B10100004341 from 2021-10-30 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 3754, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:4441: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  return super().rename(\n"
     ]
    }
   ],
   "source": [
    "# 获取数据\n",
    "import sys\n",
    "from LIB.BACKEND import DBManager\n",
    "from LIB.MIDDLE.OutlierDetection.VoltOutlier.V_1_0_0 import sta\n",
    "import pymysql\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "\n",
    "# 更新sn列表\n",
    "host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'\n",
    "port=3306\n",
    "db='qixiang_oss'\n",
    "user='qixiang_oss'\n",
    "password='Qixiang2021'\n",
    "conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)\n",
    "cursor = conn.cursor()\n",
    "cursor.execute(\"select sn, imei, add_time from app_device\")\n",
    "res = cursor.fetchall()\n",
    "df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])\n",
    "df_sn = df_sn.reset_index(drop=True)\n",
    "conn.close();\n",
    "\n",
    "window = 50\n",
    "step = 10\n",
    "window2 = 5\n",
    "step2 = 3\n",
    "volt_column = ['单体电压1', '单体电压2',\n",
    "    '单体电压3', '单体电压4','单体电压5', '单体电压6','单体电压7', '单体电压8','单体电压9', '单体电压10', '单体电压11', '单体电压12',\n",
    "    '单体电压13', '单体电压14','单体电压15', '单体电压16','单体电压17', '单体电压18','单体电压19', '单体电压20']\n",
    "columns = ['时间戳', '单体电压1', '单体电压2','单体电压3', '单体电压4','单体电压5', '单体电压6','单体电压7', '单体电压8','单体电压9', '单体电压10', '单体电压11', '单体电压12',\n",
    "'单体电压13', '单体电压14','单体电压15', '单体电压16','单体电压17', '单体电压18','单体电压19', '单体电压20']\n",
    "dbManager = DBManager.DBManager()\n",
    "\n",
    "all_result = pd.DataFrame(columns=['sn', 'suptype', 'type', 'num', 'alarm_time'])\n",
    "for sn in df_sn['sn'].tolist()[0:3]:\n",
    "    st = '2021-10-30 00:00:00'\n",
    "    et = '2021-10-31 00:00:00'\n",
    "\n",
    "    \n",
    "    df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])\n",
    "    # \n",
    "    df_bms = df_data['bms']\n",
    "\n",
    "    df_ori = df_bms[columns]\n",
    "    df_ori.rename(columns = {'时间戳':'time'}, inplace=True)\n",
    "    df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据\n",
    "    df = df.reset_index(drop=True)\n",
    "    df_result_1,time_list_1 = sta.cal_voltdiff_uniform(df,volt_column, window=window, step=step, window2=window2, step2=step2)\n",
    "    df_result_2,time_list_2 = sta.cal_volt_uniform(df,volt_column, window=window, step=step)\n",
    "\n",
    "    df_result_1['time'] = time_list_1\n",
    "    df_result_2['time'] = time_list_2\n",
    "    # second_list = [((pd.to_datetime(x)-pd.to_datetime(time_list[0])).total_seconds()) for x in time_list]  # 距离第一个点的分钟数\n",
    "\n",
    "    # fig = plt.figure(figsize=(20,10))\n",
    "\n",
    "    # length = len(df_result)\n",
    "    # for column in volt_column:\n",
    "    #     # plt.plot([x for x in range(0, length)], df_result[column][0:length], label=column)\n",
    "    #     plt.scatter(time_list[:], df_result[column][:length], label=column)\n",
    "\n",
    "    # plt.legend()\n",
    "    # plt.title('{}电压离群（电压滤波窗口大小:{}, 步进大小:{}'.format(sn, window, step, window2, step2))\n",
    "    # sn = sn.replace('/','_')\n",
    "    # plt.savefig('./结果/新数据/' + sn + '.png')\n",
    "    # plt.close()\n",
    "\n",
    "    # 报警\n",
    "    # df_result_1['time'] = time_list_1\n",
    "    # alarm_result = sta.alarm(df_result_1, volt_column,  alarm_window=20, alarm_ratio=0.9, alarm_threshold=3.5)\n",
    "    # alarm_result['sn'] = [sn] * len(alarm_result)\n",
    "    # alarm_result['suptype'] = ['电压变化量离群'] * len(alarm_result)\n",
    "    # all_result = pd.concat([all_result, alarm_result])\n",
    "\n",
    "    # df_result_2['time'] = time_list_2\n",
    "    # alarm_result = sta.alarm(df_result_2, volt_column,  alarm_window=20, alarm_ratio=0.9, alarm_threshold=3.5)\n",
    "    # alarm_result['sn'] = [sn] * len(alarm_result)\n",
    "    # alarm_result['suptype'] = ['电压离群'] * len(alarm_result)\n",
    "\n",
    "    # 记录偏差超过3的电芯编号\n",
    "    df_all_result = sta.instorage(sn, df_result_1, df_result_2)\n",
    "    \n",
    "    # all_result = pd.concat([all_result, alarm_result])\n",
    "    # all_result.to_csv('./result_volt.csv')\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### start to get data MGMCLN750N215I037 from 2021-10-23 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 32625, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:4441: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  return super().rename(\n"
     ]
    }
   ],
   "source": [
    "# 单车运行\n",
    "# 获取数据\n",
    "import sys\n",
    "from LIB.BACKEND import DBManager\n",
    "from LIB.MIDDLE.OutlierDetection.VoltOutlier.V_1_0_0 import sta\n",
    "import pymysql\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "\n",
    "# 更新sn列表\n",
    "# host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'\n",
    "# port=3306\n",
    "# db='qixiang_oss'\n",
    "# user='qixiang_oss'\n",
    "# password='Qixiang2021'\n",
    "# conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)\n",
    "# cursor = conn.cursor()\n",
    "# cursor.execute(\"select sn, imei, add_time from app_device\")\n",
    "# res = cursor.fetchall()\n",
    "# df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])\n",
    "# df_sn = df_sn.reset_index(drop=True)\n",
    "# conn.close();\n",
    "\n",
    "window = 50\n",
    "step = 10\n",
    "window2 = 5\n",
    "step2 = 3\n",
    "volt_column = ['单体电压1', '单体电压2',\n",
    "    '单体电压3', '单体电压4','单体电压5', '单体电压6','单体电压7', '单体电压8','单体电压9', '单体电压10', '单体电压11', '单体电压12',\n",
    "    '单体电压13', '单体电压14','单体电压15', '单体电压16','单体电压17', '单体电压18','单体电压19', '单体电压20']\n",
    "columns = ['时间戳', '单体电压1', '单体电压2','单体电压3', '单体电压4','单体电压5', '单体电压6','单体电压7', '单体电压8','单体电压9', '单体电压10', '单体电压11', '单体电压12',\n",
    "'单体电压13', '单体电压14','单体电压15', '单体电压16','单体电压17', '单体电压18','单体电压19', '单体电压20']\n",
    "dbManager = DBManager.DBManager()\n",
    "sns = ['MGMCLN750N215I037']\n",
    "all_result = pd.DataFrame(columns=['sn', 'suptype', 'type', 'num', 'alarm_time'])\n",
    "for sn in sns:\n",
    "    sn = 'MGMCLN750N215I037'\n",
    "    st = '2021-10-23 00:00:00'\n",
    "    et = '2021-10-31 00:00:00'\n",
    "\n",
    "    \n",
    "    df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])\n",
    "    # \n",
    "    df_bms = df_data['bms']\n",
    "\n",
    "    df_ori = df_bms[columns]\n",
    "    df_ori.rename(columns = {'时间戳':'time'}, inplace=True)\n",
    "    df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据\n",
    "    df = df.reset_index(drop=True)\n",
    "    df_result_1,time_list_1 = sta.cal_voltdiff_uniform(df,volt_column, window=window, step=step, window2=window2, step2=step2)\n",
    "    df_result_2,time_list_2 = sta.cal_volt_uniform(df,volt_column, window=window, step=step)\n",
    "\n",
    "    df_result_1['time'] = time_list_1\n",
    "    df_result_2['time'] = time_list_2\n",
    "    # second_list = [((pd.to_datetime(x)-pd.to_datetime(time_list[0])).total_seconds()) for x in time_list]  # 距离第一个点的分钟数\n",
    "\n",
    "    # fig = plt.figure(figsize=(20,10))\n",
    "\n",
    "    # length = len(df_result)\n",
    "    # for column in volt_column:\n",
    "    #     # plt.plot([x for x in range(0, length)], df_result[column][0:length], label=column)\n",
    "    #     plt.scatter(time_list[:], df_result[column][:length], label=column)\n",
    "\n",
    "    # plt.legend()\n",
    "    # plt.title('{}电压离群（电压滤波窗口大小:{}, 步进大小:{}'.format(sn, window, step, window2, step2))\n",
    "    # sn = sn.replace('/','_')\n",
    "    # plt.savefig('./结果/新数据/' + sn + '.png')\n",
    "    # plt.close()\n",
    "\n",
    "    # 报警\n",
    "    # df_result_1['time'] = time_list_1\n",
    "    # alarm_result = sta.alarm(df_result_1, volt_column,  alarm_window=20, alarm_ratio=0.9, alarm_threshold=3.5)\n",
    "    # alarm_result['sn'] = [sn] * len(alarm_result)\n",
    "    # alarm_result['suptype'] = ['电压变化量离群'] * len(alarm_result)\n",
    "    # all_result = pd.concat([all_result, alarm_result])\n",
    "\n",
    "    # df_result_2['time'] = time_list_2\n",
    "    # alarm_result = sta.alarm(df_result_2, volt_column,  alarm_window=20, alarm_ratio=0.9, alarm_threshold=3.5)\n",
    "    # alarm_result['sn'] = [sn] * len(alarm_result)\n",
    "    # alarm_result['suptype'] = ['电压离群'] * len(alarm_result)\n",
    "\n",
    "    # 记录偏差超过3的电芯编号\n",
    "    df_all_result = sta.instorage(sn, df_result_1, df_result_2)\n",
    "    \n",
    "    # all_result = pd.concat([all_result, alarm_result])\n",
    "    # all_result.to_csv('./result_volt.csv')\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1340</th>\n",
       "      <th>单体电压1</th>\n",
       "      <th>单体电压10</th>\n",
       "      <th>单体电压11</th>\n",
       "      <th>单体电压12</th>\n",
       "      <th>单体电压13</th>\n",
       "      <th>单体电压14</th>\n",
       "      <th>单体电压15</th>\n",
       "      <th>单体电压16</th>\n",
       "      <th>单体电压17</th>\n",
       "      <th>...</th>\n",
       "      <th>单体电压19</th>\n",
       "      <th>单体电压2</th>\n",
       "      <th>单体电压20</th>\n",
       "      <th>单体电压3</th>\n",
       "      <th>单体电压4</th>\n",
       "      <th>单体电压5</th>\n",
       "      <th>单体电压6</th>\n",
       "      <th>单体电压7</th>\n",
       "      <th>单体电压8</th>\n",
       "      <th>单体电压9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1340</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      1340 单体电压1 单体电压10 单体电压11 单体电压12 单体电压13 单体电压14 单体电压15 单体电压16 单体电压17  ...  \\\n",
       "1340   NaN   NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN  ...   \n",
       "\n",
       "     单体电压19 单体电压2 单体电压20 单体电压3 单体电压4 单体电压5 单体电压6 单体电压7 单体电压8 单体电压9  \n",
       "1340    NaN   NaN    NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN  \n",
       "\n",
       "[1 rows x 21 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(df_bms[1340:1341][volt_column] - df_bms[1340:1341][volt_column].mean(1))/df_bms[1340:1341][volt_column].std(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>单体电压1</th>\n",
       "      <th>单体电压2</th>\n",
       "      <th>单体电压3</th>\n",
       "      <th>单体电压4</th>\n",
       "      <th>单体电压5</th>\n",
       "      <th>单体电压6</th>\n",
       "      <th>单体电压7</th>\n",
       "      <th>单体电压8</th>\n",
       "      <th>单体电压9</th>\n",
       "      <th>单体电压10</th>\n",
       "      <th>单体电压11</th>\n",
       "      <th>单体电压12</th>\n",
       "      <th>单体电压13</th>\n",
       "      <th>单体电压14</th>\n",
       "      <th>单体电压15</th>\n",
       "      <th>单体电压16</th>\n",
       "      <th>单体电压17</th>\n",
       "      <th>单体电压18</th>\n",
       "      <th>单体电压19</th>\n",
       "      <th>单体电压20</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1340</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     单体电压1 单体电压2 单体电压3 单体电压4 单体电压5 单体电压6 单体电压7 单体电压8 单体电压9 单体电压10 单体电压11  \\\n",
       "1340   1.0   0.0   1.0   0.0   0.0  -5.0   0.0  -3.0   1.0    3.0    1.0   \n",
       "\n",
       "     单体电压12 单体电压13 单体电压14 单体电压15 单体电压16 单体电压17 单体电压18 单体电压19 单体电压20  \n",
       "1340    2.0   -2.0    1.0    3.0    2.0   -1.0   -1.0   -2.0   -1.0  "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_bms[1340:1341][volt_column] - 4159"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1340    2.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_bms[1340:1341][volt_column].std(1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([1389], dtype='int64')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_bms[df_bms['时间戳']=='2021-10-23 04:15:26'].index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>单体电压1</th>\n",
       "      <th>单体电压2</th>\n",
       "      <th>单体电压3</th>\n",
       "      <th>单体电压4</th>\n",
       "      <th>单体电压5</th>\n",
       "      <th>单体电压6</th>\n",
       "      <th>单体电压7</th>\n",
       "      <th>单体电压8</th>\n",
       "      <th>单体电压9</th>\n",
       "      <th>单体电压10</th>\n",
       "      <th>...</th>\n",
       "      <th>单体电压12</th>\n",
       "      <th>单体电压13</th>\n",
       "      <th>单体电压14</th>\n",
       "      <th>单体电压15</th>\n",
       "      <th>单体电压16</th>\n",
       "      <th>单体电压17</th>\n",
       "      <th>单体电压18</th>\n",
       "      <th>单体电压19</th>\n",
       "      <th>单体电压20</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>0.702075</td>\n",
       "      <td>0.228056</td>\n",
       "      <td>0.712609</td>\n",
       "      <td>0.038448</td>\n",
       "      <td>0.070050</td>\n",
       "      <td>-3.090080</td>\n",
       "      <td>0.322860</td>\n",
       "      <td>-1.194002</td>\n",
       "      <td>0.249124</td>\n",
       "      <td>0.933818</td>\n",
       "      <td>...</td>\n",
       "      <td>1.218230</td>\n",
       "      <td>-1.130800</td>\n",
       "      <td>0.712609</td>\n",
       "      <td>0.817947</td>\n",
       "      <td>0.828481</td>\n",
       "      <td>-0.235430</td>\n",
       "      <td>-0.351301</td>\n",
       "      <td>-0.814787</td>\n",
       "      <td>-0.488240</td>\n",
       "      <td>2021-10-23 04:15:26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>0.666068</td>\n",
       "      <td>0.300821</td>\n",
       "      <td>0.737088</td>\n",
       "      <td>0.057323</td>\n",
       "      <td>0.118198</td>\n",
       "      <td>-3.270480</td>\n",
       "      <td>0.341404</td>\n",
       "      <td>-1.109437</td>\n",
       "      <td>0.148635</td>\n",
       "      <td>0.767525</td>\n",
       "      <td>...</td>\n",
       "      <td>1.183501</td>\n",
       "      <td>-1.058708</td>\n",
       "      <td>0.777671</td>\n",
       "      <td>0.554465</td>\n",
       "      <td>0.879129</td>\n",
       "      <td>-0.135446</td>\n",
       "      <td>-0.257195</td>\n",
       "      <td>-0.703607</td>\n",
       "      <td>-0.480401</td>\n",
       "      <td>2021-10-23 04:17:06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>0.624390</td>\n",
       "      <td>0.292164</td>\n",
       "      <td>0.712332</td>\n",
       "      <td>0.096736</td>\n",
       "      <td>0.174907</td>\n",
       "      <td>-3.401409</td>\n",
       "      <td>0.380106</td>\n",
       "      <td>-1.026970</td>\n",
       "      <td>0.047880</td>\n",
       "      <td>0.624390</td>\n",
       "      <td>...</td>\n",
       "      <td>1.191128</td>\n",
       "      <td>-0.968341</td>\n",
       "      <td>0.810045</td>\n",
       "      <td>0.321478</td>\n",
       "      <td>0.927302</td>\n",
       "      <td>-0.108462</td>\n",
       "      <td>-0.206176</td>\n",
       "      <td>-0.577487</td>\n",
       "      <td>-0.430917</td>\n",
       "      <td>2021-10-23 04:18:46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>0.584256</td>\n",
       "      <td>0.310090</td>\n",
       "      <td>0.678795</td>\n",
       "      <td>0.111557</td>\n",
       "      <td>0.215551</td>\n",
       "      <td>-3.480953</td>\n",
       "      <td>0.423538</td>\n",
       "      <td>-0.956742</td>\n",
       "      <td>-0.001891</td>\n",
       "      <td>0.489716</td>\n",
       "      <td>...</td>\n",
       "      <td>1.189310</td>\n",
       "      <td>-0.909472</td>\n",
       "      <td>0.858421</td>\n",
       "      <td>0.121011</td>\n",
       "      <td>0.943507</td>\n",
       "      <td>-0.058615</td>\n",
       "      <td>-0.153154</td>\n",
       "      <td>-0.493497</td>\n",
       "      <td>-0.398958</td>\n",
       "      <td>2021-10-23 04:20:25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>0.585815</td>\n",
       "      <td>0.321195</td>\n",
       "      <td>0.686188</td>\n",
       "      <td>0.111323</td>\n",
       "      <td>0.229946</td>\n",
       "      <td>-3.538615</td>\n",
       "      <td>0.448942</td>\n",
       "      <td>-0.846786</td>\n",
       "      <td>-0.043799</td>\n",
       "      <td>0.412443</td>\n",
       "      <td>...</td>\n",
       "      <td>1.142430</td>\n",
       "      <td>-0.901535</td>\n",
       "      <td>0.859560</td>\n",
       "      <td>-0.071174</td>\n",
       "      <td>0.987308</td>\n",
       "      <td>-0.016425</td>\n",
       "      <td>-0.116798</td>\n",
       "      <td>-0.399668</td>\n",
       "      <td>-0.363169</td>\n",
       "      <td>2021-10-23 04:22:06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2886</th>\n",
       "      <td>-0.028128</td>\n",
       "      <td>0.137713</td>\n",
       "      <td>0.358835</td>\n",
       "      <td>0.648244</td>\n",
       "      <td>0.254777</td>\n",
       "      <td>-3.110822</td>\n",
       "      <td>0.410863</td>\n",
       "      <td>-0.037883</td>\n",
       "      <td>-0.232991</td>\n",
       "      <td>-1.436152</td>\n",
       "      <td>...</td>\n",
       "      <td>0.690517</td>\n",
       "      <td>0.726287</td>\n",
       "      <td>0.794574</td>\n",
       "      <td>-1.810107</td>\n",
       "      <td>0.553942</td>\n",
       "      <td>0.713279</td>\n",
       "      <td>0.719783</td>\n",
       "      <td>0.124706</td>\n",
       "      <td>0.306806</td>\n",
       "      <td>2021-10-30 09:42:21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2887</th>\n",
       "      <td>-0.032019</td>\n",
       "      <td>0.172903</td>\n",
       "      <td>0.337979</td>\n",
       "      <td>0.645362</td>\n",
       "      <td>0.283903</td>\n",
       "      <td>-3.063159</td>\n",
       "      <td>0.414825</td>\n",
       "      <td>0.070442</td>\n",
       "      <td>-0.211326</td>\n",
       "      <td>-1.475012</td>\n",
       "      <td>...</td>\n",
       "      <td>0.605516</td>\n",
       "      <td>0.727900</td>\n",
       "      <td>0.796208</td>\n",
       "      <td>-1.899087</td>\n",
       "      <td>0.528671</td>\n",
       "      <td>0.685208</td>\n",
       "      <td>0.725054</td>\n",
       "      <td>0.181441</td>\n",
       "      <td>0.303825</td>\n",
       "      <td>2021-10-30 09:44:01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2888</th>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.249315</td>\n",
       "      <td>0.308799</td>\n",
       "      <td>0.598460</td>\n",
       "      <td>0.264833</td>\n",
       "      <td>-3.040403</td>\n",
       "      <td>0.440698</td>\n",
       "      <td>0.083795</td>\n",
       "      <td>-0.270522</td>\n",
       "      <td>-1.460200</td>\n",
       "      <td>...</td>\n",
       "      <td>0.505354</td>\n",
       "      <td>0.722600</td>\n",
       "      <td>0.779498</td>\n",
       "      <td>-1.954175</td>\n",
       "      <td>0.546735</td>\n",
       "      <td>0.691565</td>\n",
       "      <td>0.735531</td>\n",
       "      <td>0.182072</td>\n",
       "      <td>0.414835</td>\n",
       "      <td>2021-10-30 09:45:41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2889</th>\n",
       "      <td>0.030937</td>\n",
       "      <td>0.264848</td>\n",
       "      <td>0.315152</td>\n",
       "      <td>0.596851</td>\n",
       "      <td>0.244727</td>\n",
       "      <td>-3.017456</td>\n",
       "      <td>0.453486</td>\n",
       "      <td>0.121483</td>\n",
       "      <td>-0.353885</td>\n",
       "      <td>-1.513381</td>\n",
       "      <td>...</td>\n",
       "      <td>0.503790</td>\n",
       "      <td>0.750277</td>\n",
       "      <td>0.747762</td>\n",
       "      <td>-1.920840</td>\n",
       "      <td>0.559124</td>\n",
       "      <td>0.699973</td>\n",
       "      <td>0.760338</td>\n",
       "      <td>0.171786</td>\n",
       "      <td>0.405698</td>\n",
       "      <td>2021-10-30 09:47:17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2890</th>\n",
       "      <td>0.035617</td>\n",
       "      <td>0.269594</td>\n",
       "      <td>0.287792</td>\n",
       "      <td>0.589363</td>\n",
       "      <td>0.235797</td>\n",
       "      <td>-3.024289</td>\n",
       "      <td>0.454176</td>\n",
       "      <td>0.103210</td>\n",
       "      <td>-0.375144</td>\n",
       "      <td>-1.506035</td>\n",
       "      <td>...</td>\n",
       "      <td>0.513971</td>\n",
       "      <td>0.781745</td>\n",
       "      <td>0.732349</td>\n",
       "      <td>-1.898597</td>\n",
       "      <td>0.532169</td>\n",
       "      <td>0.719351</td>\n",
       "      <td>0.784344</td>\n",
       "      <td>0.176003</td>\n",
       "      <td>0.420380</td>\n",
       "      <td>2021-10-30 09:48:59</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>940 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         单体电压1     单体电压2     单体电压3     单体电压4     单体电压5     单体电压6     单体电压7  \\\n",
       "134   0.702075  0.228056  0.712609  0.038448  0.070050 -3.090080  0.322860   \n",
       "135   0.666068  0.300821  0.737088  0.057323  0.118198 -3.270480  0.341404   \n",
       "136   0.624390  0.292164  0.712332  0.096736  0.174907 -3.401409  0.380106   \n",
       "137   0.584256  0.310090  0.678795  0.111557  0.215551 -3.480953  0.423538   \n",
       "138   0.585815  0.321195  0.686188  0.111323  0.229946 -3.538615  0.448942   \n",
       "...        ...       ...       ...       ...       ...       ...       ...   \n",
       "2886 -0.028128  0.137713  0.358835  0.648244  0.254777 -3.110822  0.410863   \n",
       "2887 -0.032019  0.172903  0.337979  0.645362  0.283903 -3.063159  0.414825   \n",
       "2888  0.011380  0.249315  0.308799  0.598460  0.264833 -3.040403  0.440698   \n",
       "2889  0.030937  0.264848  0.315152  0.596851  0.244727 -3.017456  0.453486   \n",
       "2890  0.035617  0.269594  0.287792  0.589363  0.235797 -3.024289  0.454176   \n",
       "\n",
       "         单体电压8     单体电压9    单体电压10  ...    单体电压12    单体电压13    单体电压14  \\\n",
       "134  -1.194002  0.249124  0.933818  ...  1.218230 -1.130800  0.712609   \n",
       "135  -1.109437  0.148635  0.767525  ...  1.183501 -1.058708  0.777671   \n",
       "136  -1.026970  0.047880  0.624390  ...  1.191128 -0.968341  0.810045   \n",
       "137  -0.956742 -0.001891  0.489716  ...  1.189310 -0.909472  0.858421   \n",
       "138  -0.846786 -0.043799  0.412443  ...  1.142430 -0.901535  0.859560   \n",
       "...        ...       ...       ...  ...       ...       ...       ...   \n",
       "2886 -0.037883 -0.232991 -1.436152  ...  0.690517  0.726287  0.794574   \n",
       "2887  0.070442 -0.211326 -1.475012  ...  0.605516  0.727900  0.796208   \n",
       "2888  0.083795 -0.270522 -1.460200  ...  0.505354  0.722600  0.779498   \n",
       "2889  0.121483 -0.353885 -1.513381  ...  0.503790  0.750277  0.747762   \n",
       "2890  0.103210 -0.375144 -1.506035  ...  0.513971  0.781745  0.732349   \n",
       "\n",
       "        单体电压15    单体电压16    单体电压17    单体电压18    单体电压19    单体电压20  \\\n",
       "134   0.817947  0.828481 -0.235430 -0.351301 -0.814787 -0.488240   \n",
       "135   0.554465  0.879129 -0.135446 -0.257195 -0.703607 -0.480401   \n",
       "136   0.321478  0.927302 -0.108462 -0.206176 -0.577487 -0.430917   \n",
       "137   0.121011  0.943507 -0.058615 -0.153154 -0.493497 -0.398958   \n",
       "138  -0.071174  0.987308 -0.016425 -0.116798 -0.399668 -0.363169   \n",
       "...        ...       ...       ...       ...       ...       ...   \n",
       "2886 -1.810107  0.553942  0.713279  0.719783  0.124706  0.306806   \n",
       "2887 -1.899087  0.528671  0.685208  0.725054  0.181441  0.303825   \n",
       "2888 -1.954175  0.546735  0.691565  0.735531  0.182072  0.414835   \n",
       "2889 -1.920840  0.559124  0.699973  0.760338  0.171786  0.405698   \n",
       "2890 -1.898597  0.532169  0.719351  0.784344  0.176003  0.420380   \n",
       "\n",
       "                     time  \n",
       "134   2021-10-23 04:15:26  \n",
       "135   2021-10-23 04:17:06  \n",
       "136   2021-10-23 04:18:46  \n",
       "137   2021-10-23 04:20:25  \n",
       "138   2021-10-23 04:22:06  \n",
       "...                   ...  \n",
       "2886  2021-10-30 09:42:21  \n",
       "2887  2021-10-30 09:44:01  \n",
       "2888  2021-10-30 09:45:41  \n",
       "2889  2021-10-30 09:47:17  \n",
       "2890  2021-10-30 09:48:59  \n",
       "\n",
       "[940 rows x 21 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_result_2[df_result_2['单体电压6']<-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n",
      "No handles with labels found to put in legend.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11400 characters]",
      "text/plain": [
       "<Figure size 1440x720 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 结果分析\n",
    "# 获取数据\n",
    "import sys\n",
    "from LIB.BACKEND import DBManager\n",
    "from LIB.MIDDLE.OutlierDetection.VoltOutlier.V_1_0_0 import sta\n",
    "import pymysql\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "from sqlalchemy import create_engine\n",
    "from sqlalchemy.orm import sessionmaker\n",
    "from urllib import parse\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "\n",
    "# # 更新sn列表\n",
    "# host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'\n",
    "# port=3306\n",
    "# db='qixiang_oss'\n",
    "# user='qixiang_oss'\n",
    "# password='Qixiang2021'\n",
    "# conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)\n",
    "# cursor = conn.cursor()\n",
    "# cursor.execute(\"select sn, imei, add_time from app_device\")\n",
    "# res = cursor.fetchall()\n",
    "# df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])\n",
    "\n",
    "# host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'\n",
    "# port = 3306\n",
    "# user = 'qx_cas'\n",
    "# password = parse.quote_plus('Qx@123456')\n",
    "# database = 'qx_cas'\n",
    "\n",
    "# db_engine = create_engine(\n",
    "#     \"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8\".format(\n",
    "#         user, password, host, port, database\n",
    "#     ))\n",
    "# DbSession = sessionmaker(bind=db_engine)\n",
    "# df_result = pd.read_sql('select * from outlierdetection_volt', db_engine)\n",
    "\n",
    "fig = plt.figure(figsize=(20,10))\n",
    "ax1 = fig.add_subplot(2,1,1)\n",
    "ax2 = fig.add_subplot(2,1,2)\n",
    "glm_sns = []\n",
    "df_alarm = pd.DataFrame(columns=['sn'])\n",
    "alarm_sn = []\n",
    "for i in range(len(df_sn)):\n",
    "    if (df_sn.loc[i, 'imei'])[0:3] == 'MGM':\n",
    "        glm_sns.append(df_sn.loc[i, 'sn'])\n",
    "for sn in glm_sns:\n",
    "    df = df_result[df_result['sn']==sn]\n",
    "    if not df.empty and not df[(df['value']>4) | (df['value']<-4)].empty:\n",
    "        df_volt_out = df[df['type']=='电压离群'][['time','cellnum','value']]\n",
    "        cellnums = sorted(set(df_volt_out['cellnum'].tolist()))\n",
    "        for cellnum in cellnums:\n",
    "            df_f = df_volt_out[df_volt_out['cellnum']==cellnum]\n",
    "            ax1.scatter(df_f['time'], df_f['value'], label=cellnum)\n",
    "        ax1.legend()\n",
    "        ax1.set_title('{} 电压离群结果图'.format(sn))\n",
    "\n",
    "        \n",
    "        df_voltdiff_out = df[df['type']=='电压变化量离群'][['time','cellnum','value']]\n",
    "        cellnums = sorted(set(df_voltdiff_out['cellnum'].tolist()))\n",
    "        for cellnum in cellnums:\n",
    "            df_f = df_voltdiff_out[df_voltdiff_out['cellnum']==cellnum]\n",
    "            ax2.scatter(df_f['time'], df_f['value'], label=cellnum)\n",
    "        ax2.legend()\n",
    "        ax2.set_title('{} 电压变化量离群结果图'.format(sn))\n",
    "        plt.savefig('./result/filter/{}.jpg'.format(sn), bbox_inches='tight')\n",
    "        ax1.clear()\n",
    "        ax2.clear()\n",
    "# df_alarm = pd.DataFrame({'sn':alarm_sn}).to_csv('alarm.csv')\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### start to get data MGMCLN750N215N155 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 1309, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:4441: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  return super().rename(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### start to get data MGMCLN750N215I004 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 1278, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n",
      "### start to get data MGMCLN750N215I027 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 25810, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n",
      "### start to get data MGMCLN750N215I033 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 6234, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n",
      "### start to get data MGMCLN750N215I065 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 41028, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n",
      "### start to get data MGMCLN750N215N096 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 15967, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n",
      "### start to get data MGMCLN750N215N268 from 2021-10-24 00:00:00 to 2021-10-31 00:00:00\n",
      "# get data from 2021-10-30 00:00:00 to 2021-10-31 00:00:00......... \n",
      "all data-getting done, bms_count is 41563, gps_count is 0, system_count is 0, accum_count is 0 \n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12648 characters]",
      "text/plain": [
       "<Figure size 1440x720 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 获取数据\n",
    "import sys\n",
    "from LIB.BACKEND import DBManager\n",
    "from LIB.MIDDLE.OutlierDetection.VoltOutlier.V_1_0_0 import sta\n",
    "import pymysql\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.ticker as ticker\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "\n",
    "# 更新sn列表\n",
    "host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'\n",
    "port=3306\n",
    "db='qixiang_oss'\n",
    "user='qixiang_oss'\n",
    "password='Qixiang2021'\n",
    "conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)\n",
    "cursor = conn.cursor()\n",
    "cursor.execute(\"select sn, imei, add_time from app_device\")\n",
    "res = cursor.fetchall()\n",
    "df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])\n",
    "df_sn = df_sn.reset_index(drop=True)\n",
    "conn.close();\n",
    "\n",
    "window = 50\n",
    "step = 10\n",
    "window2 = 5\n",
    "step2 = 3\n",
    "volt_column = ['单体电压1', '单体电压2',\n",
    "    '单体电压3', '单体电压4','单体电压5', '单体电压6','单体电压7', '单体电压8','单体电压9', '单体电压10', '单体电压11', '单体电压12',\n",
    "    '单体电压13', '单体电压14','单体电压15', '单体电压16','单体电压17', '单体电压18','单体电压19', '单体电压20']\n",
    "columns = ['时间戳', '单体电压1', '单体电压2','单体电压3', '单体电压4','单体电压5', '单体电压6','单体电压7', '单体电压8','单体电压9', '单体电压10', '单体电压11', '单体电压12',\n",
    "'单体电压13', '单体电压14','单体电压15', '单体电压16','单体电压17', '单体电压18','单体电压19', '单体电压20']\n",
    "dbManager = DBManager.DBManager()\n",
    "\n",
    "all_result = pd.DataFrame(columns=['sn', 'suptype', 'type', 'num', 'alarm_time'])\n",
    "sns = ['MGMCLN750N215N155', 'MGMCLN750N215I004', 'MGMCLN750N215I027', 'MGMCLN750N215I033', 'MGMCLN750N215I065', 'MGMCLN750N215N096',\n",
    "'MGMCLN750N215N268' ]\n",
    "fig = plt.figure(figsize=(20,10))\n",
    "ax1 = fig.add_subplot(2,1,1)\n",
    "ax2 = fig.add_subplot(2,1,2)\n",
    "st = '2021-10-24 00:00:00'\n",
    "et = '2021-10-31 00:00:00'\n",
    "for sn in sns:\n",
    "\n",
    "    df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])\n",
    "    # \n",
    "    df_bms = df_data['bms']\n",
    "\n",
    "    df_ori = df_bms[columns]\n",
    "    df_ori.rename(columns = {'时间戳':'time'}, inplace=True)\n",
    "    df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据\n",
    "    df = df.reset_index(drop=True)\n",
    "    df_result_1,time_list_1 = sta.cal_voltdiff_uniform(df,volt_column, window=window, step=step, window2=window2, step2=step2)\n",
    "    df_result_2,time_list_2 = sta.cal_volt_uniform(df,volt_column, window=window, step=step)\n",
    "\n",
    "    df_result_1['time'] = time_list_1\n",
    "    df_result_2['time'] = time_list_2\n",
    "    # second_list = [((pd.to_datetime(x)-pd.to_datetime(time_list[0])).total_seconds()) for x in time_list]  # 距离第一个点的分钟数\n",
    "\n",
    "   \n",
    "\n",
    "    length = len(df_result_2)\n",
    "    for column in volt_column:\n",
    "    # plt.plot([x for x in range(0, length)], df_result[column][0:length], label=column)\n",
    "        ax1.scatter(time_list_2[:], df_result_2[column][:length], label=column)\n",
    "\n",
    "    ax1.legend()\n",
    "    ax1.set_title('{} 电压离群结果图'.format(sn))\n",
    "    tick_spacing = int(len(time_list_2) /20)      #通过修改tick_spacing的值可以修改x轴的密度\n",
    "    ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing ))\n",
    "    for tick in ax1.get_xticklabels():\n",
    "    \n",
    "        tick.set_rotation(90)\n",
    "\n",
    "    length = len(df_result_1)\n",
    "    for column in volt_column:\n",
    "    # plt.plot([x for x in range(0, length)], df_result[column][0:length], label=column)\n",
    "        ax2.scatter(time_list_1[:], df_result_1[column][:length], label=column)\n",
    "\n",
    "    ax2.legend()\n",
    "    ax2.set_title('{} 电压变化量离群结果图'.format(sn))\n",
    "    tick_spacing = int(len(time_list_1) /20)         #通过修改tick_spacing的值可以修改x轴的密度\n",
    "    ax2.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing ))\n",
    "    for tick in ax2.get_xticklabels():\n",
    "        \n",
    "        tick.set_rotation(90)\n",
    "    plt.tight_layout()\n",
    "    plt.savefig('./result/filter2/{}.jpg'.format(sn), bbox_inches='tight')\n",
    "    ax1.clear()\n",
    "    ax2.clear()\n",
    "\n",
    "    \n",
    "\n",
    "    # 报警\n",
    "    # df_result_1['time'] = time_list_1\n",
    "    # alarm_result = sta.alarm(df_result_1, volt_column,  alarm_window=20, alarm_ratio=0.9, alarm_threshold=3.5)\n",
    "    # alarm_result['sn'] = [sn] * len(alarm_result)\n",
    "    # alarm_result['suptype'] = ['电压变化量离群'] * len(alarm_result)\n",
    "    # all_result = pd.concat([all_result, alarm_result])\n",
    "\n",
    "    # df_result_2['time'] = time_list_2\n",
    "    # alarm_result = sta.alarm(df_result_2, volt_column,  alarm_window=20, alarm_ratio=0.9, alarm_threshold=3.5)\n",
    "    # alarm_result['sn'] = [sn] * len(alarm_result)\n",
    "    # alarm_result['suptype'] = ['电压离群'] * len(alarm_result)\n",
    "\n",
    "    # 记录偏差超过3的电芯编号\n",
    "    # df_all_result = sta.instorage(sn, df_result_1, df_result_2)\n",
    "    \n",
    "    # all_result = pd.concat([all_result, alarm_result])\n",
    "    # all_result.to_csv('./result_volt.csv')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_volt_out = df[df['type']=='电压变化量离群'][['time','cellnum','value']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x1c57973afa0>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 29444 characters]",
      "text/plain": [
       "<Figure size 1440x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(20,6))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "cellnums = sorted(set(df_volt_out['cellnum'].tolist()))\n",
    "for cellnum in cellnums:\n",
    "    df_f = df_volt_out[df_volt_out['cellnum']==cellnum]\n",
    "    ax.scatter(df_f['time'], df_f['value'], label=cellnum)\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6592 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_f.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "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
}
