From cc26228c41704af1872258be7b550d6ebb31a586 Mon Sep 17 00:00:00 2001 From: govin08 Date: Thu, 1 Aug 2024 09:32:19 +0900 Subject: [PATCH] last modifications --- .../generate_signals.cpython-312.pyc | Bin 69079 -> 69079 bytes analysis/0725_main_test/6_use_class_gs.ipynb | 323 +++++++++++++++--- 2 files changed, 282 insertions(+), 41 deletions(-) diff --git a/Scripts/__pycache__/generate_signals.cpython-312.pyc b/Scripts/__pycache__/generate_signals.cpython-312.pyc index 99eb67bde8994855b09e210199fef2586df34deb..1e39633eb689ac0afa90aca493eae09174728d1f 100644 GIT binary patch delta 24 ecmcaUo8|g!7OvC0yj%=GaP>+f*H$jZv%COk83(ig delta 24 ecmcaUo8|g!7OvC0yj%=Gu%5G#YbzJySzZ8OYX-vr diff --git a/analysis/0725_main_test/6_use_class_gs.ipynb b/analysis/0725_main_test/6_use_class_gs.ipynb index 79126ace8..b61028cd2 100644 --- a/analysis/0725_main_test/6_use_class_gs.ipynb +++ b/analysis/0725_main_test/6_use_class_gs.ipynb @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -91,7 +91,7 @@ " \n", " \n", " 0\n", - " 1722416326\n", + " 1722383926\n", " 436\n", " 2\n", " 2\n", @@ -101,7 +101,7 @@ " \n", " \n", " 1\n", - " 1722416326\n", + " 1722383926\n", " 437\n", " 3\n", " 3\n", @@ -111,7 +111,7 @@ " \n", " \n", " 2\n", - " 1722416326\n", + " 1722383926\n", " 438\n", " 1\n", " 1\n", @@ -121,7 +121,7 @@ " \n", " \n", " 3\n", - " 1722416326\n", + " 1722383926\n", " 442\n", " 1\n", " 1\n", @@ -131,7 +131,7 @@ " \n", " \n", " 4\n", - " 1722416326\n", + " 1722383926\n", " 443\n", " 1\n", " 1\n", @@ -151,7 +151,7 @@ " \n", " \n", " 11095\n", - " 1722417443\n", + " 1722385043\n", " 444\n", " 4\n", " 4\n", @@ -161,7 +161,7 @@ " \n", " \n", " 11096\n", - " 1722417443\n", + " 1722385043\n", " 455\n", " 2\n", " 2\n", @@ -171,7 +171,7 @@ " \n", " \n", " 11097\n", - " 1722417443\n", + " 1722385043\n", " 456\n", " 4\n", " 4\n", @@ -181,7 +181,7 @@ " \n", " \n", " 11098\n", - " 1722417443\n", + " 1722385043\n", " 457\n", " 3\n", " 3\n", @@ -191,7 +191,7 @@ " \n", " \n", " 11099\n", - " 1722417443\n", + " 1722385043\n", " 458\n", " 2\n", " 2\n", @@ -206,22 +206,22 @@ ], "text/plain": [ " start_unix inter_no phas_A phas_B STOS_NO move_A move_B\n", - "0 1722416326 436 2 2 0 8 3\n", - "1 1722416326 437 3 3 0 8 3\n", - "2 1722416326 438 1 1 0 6 2\n", - "3 1722416326 442 1 1 0 6 1\n", - "4 1722416326 443 1 1 0 6 2\n", + "0 1722383926 436 2 2 0 8 3\n", + "1 1722383926 437 3 3 0 8 3\n", + "2 1722383926 438 1 1 0 6 2\n", + "3 1722383926 442 1 1 0 6 1\n", + "4 1722383926 443 1 1 0 6 2\n", "... ... ... ... ... ... ... ...\n", - "11095 1722417443 444 4 4 0 -1 -1\n", - "11096 1722417443 455 2 2 0 -1 -1\n", - "11097 1722417443 456 4 4 0 17 -1\n", - "11098 1722417443 457 3 3 0 17 17\n", - "11099 1722417443 458 2 2 0 17 17\n", + "11095 1722385043 444 4 4 0 -1 -1\n", + "11096 1722385043 455 2 2 0 -1 -1\n", + "11097 1722385043 456 4 4 0 17 -1\n", + "11098 1722385043 457 3 3 0 17 17\n", + "11099 1722385043 458 2 2 0 17 17\n", "\n", "[11100 rows x 7 columns]" ] }, - "execution_count": 19, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -240,14 +240,22 @@ " lambda row: int(isp2move_A.get((row.inter_no, row.STOS_NO, row.phas_A), -1)), axis=1)\n", "phases['move_B'] = phases.apply(\n", " lambda row: int(isp2move_B.get((row.inter_no, row.STOS_NO, row.phas_B), -1)), axis=1)\n", - "phases['start_unix'] = pd.to_datetime(phases['start_unix'])\n", - "phases['start_unix'] = phases['start_unix'].apply(lambda x: int(x.timestamp()))\n", + "phases['start_unix'] = phases['start_unix'].apply(lambda x:int(datetime.strptime(x, '%Y-%m-%d %H:%M:%S').timestamp()))\n", "phases" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "self.history['end_unix'] = self.history['end_unix'].apply(lambda x:int(datetime.strptime(x, '%Y-%m-%d %H:%M:%S').timestamp()))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -256,38 +264,271 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 26, "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'move' is not defined", + "name": "stdout", + "output_type": "stream", + "text": [ + "1722383926\n", + "1722385043\n", + "1722384300\n", + "1722384000\n", + "1722384300\n" + ] + } + ], + "source": [ + "print(phases.start_unix.min())\n", + "print(phases.start_unix.max())\n", + "print(self.present_time)\n", + "print(fsecs[0])\n", + "print(fsecs[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "fsecs = list(range(self.present_time - self.sim_timespan, self.present_time + 1, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "Cannot merge a Series without a name", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[25], line 16\u001b[0m\n\u001b[0;32m 7\u001b[0m move \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mconcat(moves)\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# # 2. 이력 테이블 조회해서 교차로별로 유닉스시간 최대인 데이터(교차로번호, 종료유닉스타임)만 수집 : B\u001b[39;00m\n\u001b[0;32m 9\u001b[0m \u001b[38;5;66;03m# recent_histories = [group.iloc[-1:] for _, group in self.history[self.history['end_unix'] < fsec].groupby('inter_no')] # 교차로별로 유닉스시간이 최대인 행들\u001b[39;00m\n\u001b[0;32m 10\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[38;5;66;03m# rhistory = pd.concat(recent_histories)\u001b[39;00m\n\u001b[0;32m 15\u001b[0m \u001b[38;5;66;03m# recent_unix = rhistory[['inter_no', 'end_unix']]\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m \u001b[43mmove\u001b[49m\n", - "\u001b[1;31mNameError\u001b[0m: name 'move' is not defined" + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[7], line 20\u001b[0m\n\u001b[0;32m 18\u001b[0m recent_unix \u001b[38;5;241m=\u001b[39m rhistory[[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minter_no\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mend_unix\u001b[39m\u001b[38;5;124m'\u001b[39m]]\n\u001b[0;32m 19\u001b[0m \u001b[38;5;66;03m# 3. 상태 테이블 조회정보(A)와 이력 테이블 조회정보(B) 조인(키값 : 교차로번호) : C\u001b[39;00m\n\u001b[1;32m---> 20\u001b[0m move \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmerge\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmove\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrecent_unix\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhow\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mleft\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mon\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43minter_no\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 21\u001b[0m move[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mend_unix\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m move[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mend_unix\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mfillna(\u001b[38;5;241m0\u001b[39m)\u001b[38;5;241m.\u001b[39mastype(\u001b[38;5;28mint\u001b[39m)\n\u001b[0;32m 22\u001b[0m \u001b[38;5;66;03m# # 4. C데이터 프레임에 신규 컬럼(시작 유닉스타임) 생성 후 종료유닉스 타임 값 입력, 종료 유닉스 타임 컬럼 제거\u001b[39;00m\n", + "File \u001b[1;32mc:\\github\\siggen\\siggen_env\\Lib\\site-packages\\pandas\\core\\reshape\\merge.py:152\u001b[0m, in \u001b[0;36mmerge\u001b[1;34m(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)\u001b[0m\n\u001b[0;32m 135\u001b[0m \u001b[38;5;129m@Substitution\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mleft : DataFrame or named Series\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 136\u001b[0m \u001b[38;5;129m@Appender\u001b[39m(_merge_doc, indents\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)\n\u001b[0;32m 137\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmerge\u001b[39m(\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 150\u001b[0m validate: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m 151\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m DataFrame:\n\u001b[1;32m--> 152\u001b[0m left_df \u001b[38;5;241m=\u001b[39m \u001b[43m_validate_operand\u001b[49m\u001b[43m(\u001b[49m\u001b[43mleft\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 153\u001b[0m right_df \u001b[38;5;241m=\u001b[39m _validate_operand(right)\n\u001b[0;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m how \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcross\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", + "File \u001b[1;32mc:\\github\\siggen\\siggen_env\\Lib\\site-packages\\pandas\\core\\reshape\\merge.py:2689\u001b[0m, in \u001b[0;36m_validate_operand\u001b[1;34m(obj)\u001b[0m\n\u001b[0;32m 2687\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(obj, ABCSeries):\n\u001b[0;32m 2688\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m obj\u001b[38;5;241m.\u001b[39mname \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m-> 2689\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot merge a Series without a name\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 2690\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m obj\u001b[38;5;241m.\u001b[39mto_frame()\n\u001b[0;32m 2691\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", + "\u001b[1;31mValueError\u001b[0m: Cannot merge a Series without a name" ] } ], "source": [ - "fsec = fsecs[-1]\n", + "fsec = fsecs[5]\n", "# 1. 상태 테이블 조회해서 전체 데이터중 필요데이터(교차로번호, A링 현시번호, A링 이동류번호, B링 현시번호, B링 이동류번호)만 수집 : A\n", "moves = [group.iloc[-1] for _, group in phases[phases.start_unix < fsec].groupby('inter_no')]\n", "if not moves:\n", - " pass\n", + " move = pd.DataFrame({\n", + " 'start_unix':[], 'inter_no':[], 'phas_A':[], 'phas_B':[],\n", + " 'STOS_NO':[], 'move_A':[], 'move_B':[]})\n", "else:\n", " move = pd.concat(moves)\n", - "# # 2. 이력 테이블 조회해서 교차로별로 유닉스시간 최대인 데이터(교차로번호, 종료유닉스타임)만 수집 : B\n", - "# recent_histories = [group.iloc[-1:] for _, group in self.history[self.history['end_unix'] < fsec].groupby('inter_no')] # 교차로별로 유닉스시간이 최대인 행들\n", + "move = move.drop(columns='start_unix')\n", + "# 2. 이력 테이블 조회해서 교차로별로 유닉스시간 최대인 데이터(교차로번호, 종료유닉스타임)만 수집 : B\n", + "recent_histories = [group.iloc[-1:] for _, group in self.history[self.history['end_unix'] < fsec].groupby('inter_no')] # 교차로별로 유닉스시간이 최대인 행들\n", "\n", - "# if not recent_histories:\n", - "# rhistory = pd.DataFrame({'inter_no':[], 'end_unix':[]}) # recent history\n", - "# else:\n", - "# rhistory = pd.concat(recent_histories)\n", - "# recent_unix = rhistory[['inter_no', 'end_unix']]\n", - "move" + "if not recent_histories:\n", + " rhistory = pd.DataFrame({'inter_no':[], 'end_unix':[]}) # recent history\n", + "else:\n", + " rhistory = pd.concat(recent_histories)\n", + "recent_unix = rhistory[['inter_no', 'end_unix']]\n", + "# 3. 상태 테이블 조회정보(A)와 이력 테이블 조회정보(B) 조인(키값 : 교차로번호) : C\n", + "move = pd.merge(move, recent_unix, how='left', on='inter_no')\n", + "move['end_unix'] = move['end_unix'].fillna(0).astype(int)\n", + "# # 4. C데이터 프레임에 신규 컬럼(시작 유닉스타임) 생성 후 종료유닉스 타임 값 입력, 종료 유닉스 타임 컬럼 제거\n", + "move = move.rename(columns = {'end_unix':'start_unix'})\n", + "# 5. 이동류 이력정보 READ\n", + "# - CSV 파일로 서버에 저장된 이동류정보를 읽어옴(파일이 없는 경우에는 데이터가 없는 프레임 D 생성)\n", + "try:\n", + " movement # movement가 존재할 경우 그걸 그대로 씀.\n", + "except NameError: # movement가 존재하지 않는 경우 생성\n", + " movement = pd.DataFrame()\n", + "# 6. 이동류 이력정보 데이터테이블(D)에 C데이터 add\n", + "movement = pd.concat([movement, move])\n", + "# 7. D데이터 프레임에서 중복데이터 제거(교차로번호, 시작 유닉스타임, A링 현시번호, B링 현시번호 같은 행은 제거)\n", + "movement = movement.drop_duplicates(['inter_no','phas_A','phas_B','start_unix'])\n", + "# 8. D데이터 보관 시간 기준시간을 시작 유닉스 타임의 최대값 - self.subtractor // 2을 값으로 산출하고, 보관 시간 기준시간보다 작은 시작 유닉스 타임을 가진 행은 모두 제거(1시간 데이터만 보관)\n", + "movement = movement[movement.start_unix > fsec - self.subtractor // 2]\n", + "movement\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
start_unixinter_nophas_Aphas_BSTOS_NOmove_Amove_B
0172241632643622083
1172241632643733083
2172241632643811062
3172241632644211061
4172241632644311062
........................
110951722417443444440-1-1
110961722417443455220-1-1
11097172241744345644017-1
1109817224174434573301717
1109917224174434582201717
\n", + "

11100 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " start_unix inter_no phas_A phas_B STOS_NO move_A move_B\n", + "0 1722416326 436 2 2 0 8 3\n", + "1 1722416326 437 3 3 0 8 3\n", + "2 1722416326 438 1 1 0 6 2\n", + "3 1722416326 442 1 1 0 6 1\n", + "4 1722416326 443 1 1 0 6 2\n", + "... ... ... ... ... ... ... ...\n", + "11095 1722417443 444 4 4 0 -1 -1\n", + "11096 1722417443 455 2 2 0 -1 -1\n", + "11097 1722417443 456 4 4 0 17 -1\n", + "11098 1722417443 457 3 3 0 17 17\n", + "11099 1722417443 458 2 2 0 17 17\n", + "\n", + "[11100 rows x 7 columns]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phases" ] }, {