{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os, sys, json, argparse, pickle\n", "import sumolib, traci\n", "from tqdm import tqdm\n", "from datetime import datetime\n", "path_root = os.path.dirname(os.path.dirname(os.path.abspath('.')))\n", "path_scr = os.path.join(path_root, 'scripts')\n", "sys.path.append(path_scr)\n", "from generate_signals import SignalGenerator" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "self = SignalGenerator(config_name='test_0731',\n", " month=7,\n", " day=31,\n", " hour=9,\n", " minute=5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. 데이터를 준비합니다.\n", "1-1. 네트워크가 로드되었습니다.\n", "1-2. 테이블들이 로드되었습니다.\n", "1-5. 필요한 보조 객체들이 모두 준비되었습니다.\n", "2. 신호이력 테이블을 변환합니다.\n" ] } ], "source": [ "self.prepare_data()\n", "self.process_history()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3. 이동류정보 테이블을 변환합니다.\n" ] } ], "source": [ "self.process_movement()\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4. 통합 테이블을 생성합니다.\n", "5. 신호를 생성합니다.\n" ] } ], "source": [ "self.make_histids()\n", "self.get_signals()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1722384300" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.present_time" ] }, { "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
0172238392643622083
1172238392643733083
2172238392643811062
3172238392644211061
4172238392644311062
........................
110951722385043444440-1-1
110961722385043455220-1-1
11097172238504345644017-1
1109817223850434573301717
1109917223850434582201717
\n", "

11100 rows × 7 columns

\n", "
" ], "text/plain": [ " start_unix inter_no phas_A phas_B STOS_NO move_A move_B\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 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": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phases = pd.read_csv(os.path.join(self.path_tables, 'TL_IF_SIGL.csv'))\n", "phases = phases.drop(columns=['FRST_REG_DT', 'RINGA_FLOW', 'RINGB_FLOW'])\n", "phases = phases.rename(columns={\n", " 'PHASE_DT':'start_unix','CRSRD_ID':'inter_no',\n", " 'RINGA_PHASE':'phas_A', 'RINGB_PHASE':'phas_B',\n", " 'MAP_MODE':'STOS_NO'\n", " })\n", "isp2move_A = self.isp2move['A']\n", "isp2move_B = self.isp2move['B']\n", "phases['move_A'] = phases.apply(\n", " 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'] = phases['start_unix'].apply(lambda x:int(datetime.strptime(x, '%Y-%m-%d %H:%M:%S').timestamp()))\n", "phases" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "fsecs = list(range(self.present_time - self.sim_timespan, self.present_time + 1, 5))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "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": 10, "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", " \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", " \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", "
inter_nophas_Aphas_BSTOS_NOmove_Amove_Bstart_unix
6455110621722383976
7456220621722383967
1437110621722384007
74563307-11722383967
745644017-11722383967
6455220-1-11722383976
8457110621722384053
7456110521722383967
1437220511722384007
3442110611722384098
8457220511722384053
4443110621722384102
1437330831722384007
845733017171722384053
3442220621722384098
1437440741722384007
3442330521722384098
8457440831722384053
3442440741722384098
8457550741722384053
1437110621722384207
4443220521722384102
8457110621722384232
0436110521722384236
3442550831722384098
44433307181722384102
8457220511722384232
1437220511722384207
3442660831722384098
0436220831722384236
3442110611722384297
845733017171722384232
\n", "
" ], "text/plain": [ " inter_no phas_A phas_B STOS_NO move_A move_B start_unix\n", "6 455 1 1 0 6 2 1722383976\n", "7 456 2 2 0 6 2 1722383967\n", "1 437 1 1 0 6 2 1722384007\n", "7 456 3 3 0 7 -1 1722383967\n", "7 456 4 4 0 17 -1 1722383967\n", "6 455 2 2 0 -1 -1 1722383976\n", "8 457 1 1 0 6 2 1722384053\n", "7 456 1 1 0 5 2 1722383967\n", "1 437 2 2 0 5 1 1722384007\n", "3 442 1 1 0 6 1 1722384098\n", "8 457 2 2 0 5 1 1722384053\n", "4 443 1 1 0 6 2 1722384102\n", "1 437 3 3 0 8 3 1722384007\n", "8 457 3 3 0 17 17 1722384053\n", "3 442 2 2 0 6 2 1722384098\n", "1 437 4 4 0 7 4 1722384007\n", "3 442 3 3 0 5 2 1722384098\n", "8 457 4 4 0 8 3 1722384053\n", "3 442 4 4 0 7 4 1722384098\n", "8 457 5 5 0 7 4 1722384053\n", "1 437 1 1 0 6 2 1722384207\n", "4 443 2 2 0 5 2 1722384102\n", "8 457 1 1 0 6 2 1722384232\n", "0 436 1 1 0 5 2 1722384236\n", "3 442 5 5 0 8 3 1722384098\n", "4 443 3 3 0 7 18 1722384102\n", "8 457 2 2 0 5 1 1722384232\n", "1 437 2 2 0 5 1 1722384207\n", "3 442 6 6 0 8 3 1722384098\n", "0 436 2 2 0 8 3 1722384236\n", "3 442 1 1 0 6 1 1722384297\n", "8 457 3 3 0 17 17 1722384232" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for fsec in range(self.present_time - self.sim_timespan, self.present_time + 1, 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", " 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", " 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", " # 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": 11, "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
0172238392643622083
1172238392643733083
2172238392643811062
3172238392644211061
4172238392644311062
........................
110951722385043444440-1-1
110961722385043455220-1-1
11097172238504345644017-1
1109817223850434573301717
1109917223850434582201717
\n", "

11100 rows × 7 columns

\n", "
" ], "text/plain": [ " start_unix inter_no phas_A phas_B STOS_NO move_A move_B\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 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": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phases" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "self.movement = pd.read_csv(os.path.join(self.path_tables, 'TL_IF_SIGL.csv'))\n", "self.movement = self.movement.drop(columns=['FRST_REG_DT', 'RINGA_FLOW', 'RINGB_FLOW'])\n", "self.movement = self.movement.rename(columns={\n", " 'PHASE_DT':'start_unix','CRSRD_ID':'inter_no',\n", " 'RINGA_PHASE':'phas_A', 'RINGB_PHASE':'phas_B',\n", " 'MAP_MODE':'STOS_NO'\n", " })\n", "isp2move_A = self.isp2move['A']\n", "isp2move_B = self.isp2move['B']\n", "self.movement['move_A'] = self.movement.apply(\n", " lambda row: int(isp2move_A.get((row.inter_no, row.STOS_NO, row.phas_A), -1)), axis=1)\n", "self.movement['move_B'] = self.movement.apply(\n", " lambda row: int(isp2move_B.get((row.inter_no, row.STOS_NO, row.phas_B), -1)), axis=1)\n", "self.movement['start_unix'] = pd.to_datetime(self.movement['start_unix'])\n", "self.movement['start_unix'] = self.movement['start_unix'].apply(lambda x: int(x.timestamp()))\n" ] }, { "cell_type": "code", "execution_count": 13, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PHASE_DTCRSRD_IDRINGA_PHASERINGA_FLOWRINGB_PHASERINGB_FLOWMAP_MODEFRST_REG_DT
02024-07-31 08:58:46436282302024-07-31 08:58:46.799000
12024-07-31 08:58:46437383302024-07-31 08:58:46.799000
22024-07-31 08:58:46438161202024-07-31 08:58:46.799000
32024-07-31 08:58:46442161102024-07-31 08:58:46.799000
42024-07-31 08:58:46443161202024-07-31 08:58:46.799000
...........................
110952024-07-31 09:17:23444474402024-07-31 09:17:23.051000
110962024-07-31 09:17:23455212102024-07-31 09:17:23.051000
110972024-07-31 09:17:234564174102024-07-31 09:17:23.051000
110982024-07-31 09:17:2345731731702024-07-31 09:17:23.051000
110992024-07-31 09:17:2345821721702024-07-31 09:17:23.051000
\n", "

11100 rows × 8 columns

\n", "
" ], "text/plain": [ " PHASE_DT CRSRD_ID RINGA_PHASE RINGA_FLOW RINGB_PHASE \\\n", "0 2024-07-31 08:58:46 436 2 8 2 \n", "1 2024-07-31 08:58:46 437 3 8 3 \n", "2 2024-07-31 08:58:46 438 1 6 1 \n", "3 2024-07-31 08:58:46 442 1 6 1 \n", "4 2024-07-31 08:58:46 443 1 6 1 \n", "... ... ... ... ... ... \n", "11095 2024-07-31 09:17:23 444 4 7 4 \n", "11096 2024-07-31 09:17:23 455 2 1 2 \n", "11097 2024-07-31 09:17:23 456 4 17 4 \n", "11098 2024-07-31 09:17:23 457 3 17 3 \n", "11099 2024-07-31 09:17:23 458 2 17 2 \n", "\n", " RINGB_FLOW MAP_MODE FRST_REG_DT \n", "0 3 0 2024-07-31 08:58:46.799000 \n", "1 3 0 2024-07-31 08:58:46.799000 \n", "2 2 0 2024-07-31 08:58:46.799000 \n", "3 1 0 2024-07-31 08:58:46.799000 \n", "4 2 0 2024-07-31 08:58:46.799000 \n", "... ... ... ... \n", "11095 4 0 2024-07-31 09:17:23.051000 \n", "11096 1 0 2024-07-31 09:17:23.051000 \n", "11097 1 0 2024-07-31 09:17:23.051000 \n", "11098 17 0 2024-07-31 09:17:23.051000 \n", "11099 17 0 2024-07-31 09:17:23.051000 \n", "\n", "[11100 rows x 8 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "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", "
PHASE_DTCRSRD_IDRINGA_PHASERINGB_PHASEMAP_MODE
02024-07-31 08:58:46436220
12024-07-31 08:58:46437330
22024-07-31 08:58:46438110
32024-07-31 08:58:46442110
42024-07-31 08:58:46443110
..................
110952024-07-31 09:17:23444440
110962024-07-31 09:17:23455220
110972024-07-31 09:17:23456440
110982024-07-31 09:17:23457330
110992024-07-31 09:17:23458220
\n", "

11100 rows × 5 columns

\n", "
" ], "text/plain": [ " PHASE_DT CRSRD_ID RINGA_PHASE RINGB_PHASE MAP_MODE\n", "0 2024-07-31 08:58:46 436 2 2 0\n", "1 2024-07-31 08:58:46 437 3 3 0\n", "2 2024-07-31 08:58:46 438 1 1 0\n", "3 2024-07-31 08:58:46 442 1 1 0\n", "4 2024-07-31 08:58:46 443 1 1 0\n", "... ... ... ... ... ...\n", "11095 2024-07-31 09:17:23 444 4 4 0\n", "11096 2024-07-31 09:17:23 455 2 2 0\n", "11097 2024-07-31 09:17:23 456 4 4 0\n", "11098 2024-07-31 09:17:23 457 3 3 0\n", "11099 2024-07-31 09:17:23 458 2 2 0\n", "\n", "[11100 rows x 5 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "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", "
start_unixinter_nophas_Aphas_BSTOS_NO
02024-07-31 08:58:46436220
12024-07-31 08:58:46437330
22024-07-31 08:58:46438110
32024-07-31 08:58:46442110
42024-07-31 08:58:46443110
..................
110952024-07-31 09:17:23444440
110962024-07-31 09:17:23455220
110972024-07-31 09:17:23456440
110982024-07-31 09:17:23457330
110992024-07-31 09:17:23458220
\n", "

11100 rows × 5 columns

\n", "
" ], "text/plain": [ " start_unix inter_no phas_A phas_B STOS_NO\n", "0 2024-07-31 08:58:46 436 2 2 0\n", "1 2024-07-31 08:58:46 437 3 3 0\n", "2 2024-07-31 08:58:46 438 1 1 0\n", "3 2024-07-31 08:58:46 442 1 1 0\n", "4 2024-07-31 08:58:46 443 1 1 0\n", "... ... ... ... ... ...\n", "11095 2024-07-31 09:17:23 444 4 4 0\n", "11096 2024-07-31 09:17:23 455 2 2 0\n", "11097 2024-07-31 09:17:23 456 4 4 0\n", "11098 2024-07-31 09:17:23 457 3 3 0\n", "11099 2024-07-31 09:17:23 458 2 2 0\n", "\n", "[11100 rows x 5 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "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
02024-07-31 08:58:4643622083
12024-07-31 08:58:4643733083
22024-07-31 08:58:4643811062
32024-07-31 08:58:4644211061
42024-07-31 08:58:4644311062
........................
110952024-07-31 09:17:23444440-1-1
110962024-07-31 09:17:23455220-1-1
110972024-07-31 09:17:2345644017-1
110982024-07-31 09:17:234573301717
110992024-07-31 09:17:234582201717
\n", "

11100 rows × 7 columns

\n", "
" ], "text/plain": [ " start_unix inter_no phas_A phas_B STOS_NO move_A move_B\n", "0 2024-07-31 08:58:46 436 2 2 0 8 3\n", "1 2024-07-31 08:58:46 437 3 3 0 8 3\n", "2 2024-07-31 08:58:46 438 1 1 0 6 2\n", "3 2024-07-31 08:58:46 442 1 1 0 6 1\n", "4 2024-07-31 08:58:46 443 1 1 0 6 2\n", "... ... ... ... ... ... ... ...\n", "11095 2024-07-31 09:17:23 444 4 4 0 -1 -1\n", "11096 2024-07-31 09:17:23 455 2 2 0 -1 -1\n", "11097 2024-07-31 09:17:23 456 4 4 0 17 -1\n", "11098 2024-07-31 09:17:23 457 3 3 0 17 17\n", "11099 2024-07-31 09:17:23 458 2 2 0 17 17\n", "\n", "[11100 rows x 7 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "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]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "self.movement = pd.read_csv(os.path.join(self.path_tables, 'TL_IF_SIGL.csv'))\n", "display(self.movement)\n", "self.movement = self.movement.drop(columns=['FRST_REG_DT', 'RINGA_FLOW', 'RINGB_FLOW'])\n", "display(self.movement)\n", "self.movement = self.movement.rename(columns={\n", " 'PHASE_DT':'start_unix','CRSRD_ID':'inter_no',\n", " 'RINGA_PHASE':'phas_A', 'RINGB_PHASE':'phas_B',\n", " 'MAP_MODE':'STOS_NO'\n", " })\n", "display(self.movement)\n", "isp2move_A = self.isp2move['A']\n", "isp2move_B = self.isp2move['B']\n", "self.movement['move_A'] = self.movement.apply(\n", " lambda row: int(isp2move_A.get((row.inter_no, row.STOS_NO, row.phas_A), -1)), axis=1)\n", "self.movement['move_B'] = self.movement.apply(\n", " lambda row: int(isp2move_B.get((row.inter_no, row.STOS_NO, row.phas_B), -1)), axis=1)\n", "display(self.movement)\n", "self.movement['start_unix'] = pd.to_datetime(self.movement['start_unix'])\n", "self.movement['start_unix'] = self.movement['start_unix'].apply(lambda x: int(x.timestamp()))\n", "display(self.movement)" ] }, { "cell_type": "code", "execution_count": 14, "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]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(self.movement)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3. 이동류정보 테이블을 변환합니다.\n", "4. 통합 테이블을 생성합니다.\n", "5. 신호를 생성합니다.\n" ] } ], "source": [ "self.process_movement()\n", "self.make_histids()\n", "self.get_signals()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "원본 DataFrame:\n" ] }, { "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", "
ABC
01.0NaN1.0
12.02.0NaN
2NaN3.0NaN
34.0NaN4.0
4NaN5.05.0
56.06.06.0
\n", "
" ], "text/plain": [ " A B C\n", "0 1.0 NaN 1.0\n", "1 2.0 2.0 NaN\n", "2 NaN 3.0 NaN\n", "3 4.0 NaN 4.0\n", "4 NaN 5.0 5.0\n", "5 6.0 6.0 6.0" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "결측값을 앞의 값으로 채운 DataFrame:\n" ] }, { "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", "
ABC
01.0NaN1.0
12.02.01.0
22.03.01.0
34.03.04.0
44.05.05.0
56.06.06.0
\n", "
" ], "text/plain": [ " A B C\n", "0 1.0 NaN 1.0\n", "1 2.0 2.0 1.0\n", "2 2.0 3.0 1.0\n", "3 4.0 3.0 4.0\n", "4 4.0 5.0 5.0\n", "5 6.0 6.0 6.0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# 예제 DataFrame 생성\n", "data = {\n", " 'A': [1, 2, np.nan, 4, np.nan, 6],\n", " 'B': [np.nan, 2, 3, np.nan, 5, 6],\n", " 'C': [1, np.nan, np.nan, 4, 5, 6]\n", "}\n", "df = pd.DataFrame(data)\n", "\n", "print(\"원본 DataFrame:\")\n", "display(df)\n", "\n", "# 결측값을 앞의 값으로 채우기\n", "# df_filled = df.fillna(method='ffill')\n", "df_filled = df.ffill()#fillna(method='ffill')\n", "\n", "print(\"\\n결측값을 앞의 값으로 채운 DataFrame:\")\n", "display(df_filled)\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-07-29 09:36:32\n", "1722213392\n", "2024-07-29 09:36:32\n" ] } ], "source": [ "time_str = '2024-07-29 9:36:32'\n", "time_dt = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')\n", "print(time_dt)\n", "time_unix = int(time_dt.timestamp())\n", "print(time_unix)\n", "print(datetime.fromtimestamp(time_unix))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "self = SignalGenerator(config_name='test_0731',\n", " month=7,\n", " day=31,\n", " hour=9,\n", " minute=5)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1-1. 네트워크가 로드되었습니다.\n" ] } ], "source": [ "self.load_networks()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1-2. 테이블들이 로드되었습니다.\n" ] } ], "source": [ "self.load_tables()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1722383967\n", "1722385038\n", "1722384300\n", "1722351600\n", "1722438000\n" ] } ], "source": [ "print(self.history.end_unix.min())\n", "print(self.history.end_unix.max())\n", "print(self.present_time)\n", "print(self.midnight)\n", "print(self.next_day)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# dura_As = [f'dura_A{i}' for i in range(1,9)]\n", "# dura_Bs = [f'dura_B{i}' for i in range(1,9)]\n", "# for i, row in self.plan.iterrows():\n", "# is_zero_A = (row[dura_As]==0).all()\n", "# is_zero_B = (row[dura_Bs]==0).all()\n", "# assert not (is_zero_A & is_zero_B)\n", "# if is_zero_B:\n", "# for j in range(1, 9):\n", "# self.plan.at[i, f'dura_B{j}'] = row[f'dura_A{j}']\n", "# if is_zero_A:\n", "# for j in range(1, 9):\n", "# self.plan.at[i, f'dura_A{j}'] = row[f'dura_B{j}']" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1-5. 필요한 보조 객체들이 모두 준비되었습니다.\n" ] } ], "source": [ "self.prepare_auxiliaries()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# valid_parent_ids = [self.inter2node[inter_no] for inter_no in set(self.history.inter_no)]\n", "# invalid_parent_ids = [parent_id for parent_id in self.parent_ids if not parent_id in valid_parent_ids ]\n", "# print(sorted(self.parent_ids))\n", "# print(sorted(valid_parent_ids))\n", "# print(sorted(invalid_parent_ids))\n", "# invalid_child_ids = []\n", "# for parent_id in invalid_parent_ids:\n", "# invalid_child_ids.extend(self.pa2ch[parent_id])\n", "# print(sorted(invalid_child_ids))\n", "\n", "# print()\n", "# print()\n", "# self.parent_ids = sorted(set(self.parent_ids) - set(invalid_parent_ids))\n", "# self.node_ids = sorted(set(self.node_ids) - set(invalid_parent_ids))\n", "# self.child_ids = sorted(set(self.child_ids) - set(invalid_child_ids))\n", "# self.uturn_ids = sorted(set(self.uturn_ids) - set(invalid_child_ids))\n", "# self.coord_ids = sorted(set(self.coord_ids) - set(invalid_child_ids))\n", "# print(self.parent_ids)\n", "# print(self.node_ids)\n", "# print(self.child_ids)\n", "# print(self.uturn_ids)\n", "# print(self.coord_ids)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.history.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.process_history()\n", "sorted(inter_no for inter_no in set(self.rhistory.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.rhists.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.hrhists.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.process_movement()\n", "sorted(inter_no for inter_no in set(self.movement.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.make_histids()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.movement_updated.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.movedur.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.histid.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(node_id for node_id in set(self.histid.node_id))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.histids.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(node_id for node_id in set(self.histids.node_id))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.histids" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.set_timepoints()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.sigtable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.assign_red_yellow()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.make_tl_file()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.plan[self.plan.inter_no==455]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.SIGTABLE = []\n", "node_id = self.node_ids[11]\n", "print(node_id)\n", "sig = self.sigtable.query('node_id==@node_id')\n", "display(sig)\n", "for i, row in sig.iterrows():\n", " inter_no = row.inter_no\n", " phas_A = row.phas_A\n", " phas_B = row.phas_B\n", " start_unix = row.start_unix\n", " prow = self.load_prow(inter_no, start_unix)[1].iloc[0]\n", " # 황색 및 적색시간 컬럼 생성\n", " red_A = prow[f'red_A{phas_A}']\n", " yellow_A = prow[f'yellow_A{phas_A}']\n", " red_B = prow[f'red_B{phas_B}']\n", " yellow_B = prow[f'yellow_B{phas_B}']\n", " sig.loc[i, ['red_A', 'red_B', 'yellow_A', 'yellow_B']] = red_A, red_B, yellow_A, yellow_B\n", "display(sig)\n", "sig = sig.astype({'red_A': int, 'red_B': int, 'yellow_A': int, 'yellow_B': int,\n", " 'phas_A':str, 'phas_B':str})\n", "sig = sig.drop(['move_A','move_B'], axis=1)\n", "display(sig)\n", "\n", "sig_A = sig[['start_unix', 'phas_A', 'duration', 'state_A', 'red_A', 'yellow_A']].reset_index(drop=True)\n", "sig_B = sig[['start_unix', 'phas_B', 'duration', 'state_B', 'red_B', 'yellow_B']].reset_index(drop=True)\n", "display(sig_A)\n", "display(sig_B)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.parent_ids" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# csig_A = self.cumulate(sig_A, 'A')\n", "sig = sig_A\n", "alph = 'A'\n", "csig = [] # cumulated sig\n", "pre = pd.Series({f'phas_{alph}':None})\n", "\n", "sig[f'phas_{alph}'] = sig[f'phas_{alph}'].astype(int)\n", "# 만약 현시번호가 1로 일정하면 2현시를 가상으로 추가\n", "if (sig[f'phas_{alph}']==1).all():\n", " sig_dup = sig.copy() \n", " sig_dup[f'phas_{alph}'] = 2\n", " sig = pd.concat([sig, sig_dup]).sort_values(by=['start_unix', f'phas_{alph}'])\n", "\n", "start_time = 0\n", "elapsed = 0\n", "for i, cur in sig.iterrows():\n", " start_unix = cur.start_unix\n", " \n", " # pre, nex\n", " if i != 0:\n", " pre = sig.iloc[i-1]\n", " \n", " if i != len(sig) - 1:\n", " nex = sig.iloc[i+1]\n", " \n", " # duration\n", " if cur[f'phas_{alph}'] == nex[f'phas_{alph}']:\n", " continue\n", " if cur[f'phas_{alph}'] == pre[f'phas_{alph}']:\n", " duration = cur.duration + pre.duration\n", " else:\n", " duration = cur.duration\n", " start_times = []\n", " states = []\n", " phases = []\n", "\n", " # red\n", " if i != 0:\n", " start_time += elapsed\n", " start_times.append(start_time)\n", " states.append(self.get_red(pre[f'state_{alph}'], cur[f'state_{alph}']))\n", " phases.append(f'{cur[f\"phas_{alph}\"]}r')\n", "\n", " elapsed = cur[f'red_{alph}']\n", "\n", " # green\n", " if i == 0:\n", " start_time = 0\n", " else:\n", " start_time += elapsed\n", " start_times.append(start_time)\n", " states.append(cur[f'state_{alph}'])\n", " phases.append(f'{cur[f\"phas_{alph}\"]}g')\n", " if i == 0:\n", " elapsed = duration - cur[f'yellow_{alph}']\n", " else:\n", " elapsed = duration - cur[f'yellow_{alph}'] - cur[f'red_{alph}']\n", "\n", " # yellow\n", " if i != len(sig) - 1:\n", " start_time += elapsed\n", " start_times.append(start_time)\n", " states.append(self.get_yellow(cur[f'state_{alph}'], nex[f'state_{alph}']))\n", " phases.append(f'{cur[f\"phas_{alph}\"]}y')\n", " elapsed = cur[f'yellow_{alph}']\n", " sig_ = pd.DataFrame({'start_time':start_times, f'phas_{alph}':phases, f'state_{alph}':states})\n", " sig_['start_unix'] = start_unix\n", " csig.append(sig_)\n", "csig = pd.concat(csig).reset_index(drop=True)\n", "csig\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.SIGTABLE = []\n", "node_id = self.node_ids[11]\n", "print(node_id)\n", "sig = self.sigtable.query('node_id==@node_id')\n", "display(sig)\n", "for i, row in sig.iterrows():\n", " inter_no = row.inter_no\n", " phas_A = row.phas_A\n", " phas_B = row.phas_B\n", " start_unix = row.start_unix\n", " prow = self.load_prow(inter_no, start_unix)[1].iloc[0]\n", " # 황색 및 적색시간 컬럼 생성\n", " red_A = prow[f'red_A{phas_A}']\n", " yellow_A = prow[f'yellow_A{phas_A}']\n", " red_B = prow[f'red_B{phas_B}']\n", " yellow_B = prow[f'yellow_B{phas_B}']\n", " sig.loc[i, ['red_A', 'red_B', 'yellow_A', 'yellow_B']] = red_A, red_B, yellow_A, yellow_B\n", "display(sig)\n", "sig = sig.astype({'red_A': int, 'red_B': int, 'yellow_A': int, 'yellow_B': int,\n", " 'phas_A':str, 'phas_B':str})\n", "sig = sig.drop(['move_A','move_B'], axis=1)\n", "display(sig)\n", "\n", "sig_A = sig[['start_unix', 'phas_A', 'duration', 'state_A', 'red_A', 'yellow_A']].reset_index(drop=True)\n", "sig_B = sig[['start_unix', 'phas_B', 'duration', 'state_B', 'red_B', 'yellow_B']].reset_index(drop=True)\n", "display(sig_A)\n", "display(sig_B)\n", "\n", "csig_A = self.cumulate(sig_A, 'A')\n", "csig_B = self.cumulate(sig_B, 'B')\n", "display(csig_A)\n", "display(csig_B)\n", "\n", "SIG = pd.merge(csig_A, csig_B, on=['start_time', 'start_unix'], how='outer')\n", "display(SIG[:40])\n", "SIG = SIG.sort_values(by='start_time').reset_index(drop=True)\n", "SIG[['phas_A', 'state_A']] = SIG[['phas_A', 'state_A']].fillna(method='ffill')\n", "SIG[['phas_B', 'state_B']] = SIG[['phas_B', 'state_B']].fillna(method='ffill')\n", "SIG['phase'] = SIG['phas_A'] + \"_\" + SIG['phas_B']\n", "SIG['node_id'] = node_id\n", "SIG = SIG[['node_id', 'start_unix', 'start_time', 'phase', 'state_A', 'state_B']]\n", "SIG['duration'] = SIG['start_time'].shift(-1) - SIG['start_time']\n", "SIG = SIG[:-1]\n", "SIG['duration'] = SIG['duration'].astype(int)\n", "display(SIG[:40])\n", "for row in SIG.itertuples():\n", " state = ''\n", " for a, b, in zip(row.state_A, row.state_B):\n", " if a == 'r':\n", " state += b\n", " elif b == 'r':\n", " state += a\n", " elif a == b:\n", " state += a\n", " else:\n", " raise ValueError(f\"예상되지 않은 조합 발생: a={a}, b={b}\")\n", " SIG.at[row.Index, 'state'] = state\n", "display(SIG[:40])\n", "SIG = SIG[SIG.duration!=0]\n", "SIG = SIG.drop(columns=['start_time', 'state_A', 'state_B'])\n", "display(SIG[:40])\n", "self.SIGTABLE.append(SIG)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.offsets = {}\n", "self.sigtable = []\n", "sim_start = self.present_time - self.sim_timespan\n", "print(self.present_time)\n", "print(self.sim_timespan)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "node_id = '106231'\n", "group = self.histids[self.histids.node_id==node_id]\n", "min_start_unix = int(group['start_unix'].min())\n", "min_start_unix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "node_id = '107587'\n", "group = self.histids[self.histids.node_id==node_id]\n", "series = group[group['start_unix'] < sim_start]['start_unix']\n", "min_start_unix = int(group['start_unix'].min())\n", "inter_no = self.node2inter[node_id]\n", "_, prow = self.load_prow(inter_no, min_start_unix)\n", "cycle = prow.iloc[0]['cycle']\n", "lsbs = min_start_unix\n", "print(lsbs, sim_start, cycle)\n", "print(lsbs > sim_start)\n", "print(type(lsbs), 'hello', type(sim_start))\n", "while lsbs > sim_start:\n", " lsbs -= cycle\n", "print(min_start_unix)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.offsets = {}\n", "self.sigtable = []\n", "sim_start = self.present_time - self.sim_timespan\n", "for node_id, group in self.histids.groupby('node_id'):\n", " series = group[group['start_unix'] < sim_start]['start_unix']\n", " # lsbs : the last start_unix before sim_start\n", " if len(series):\n", " lsbs = series.max()\n", " else:\n", " min_start_unix = int(group['start_unix'].min())\n", " inter_no = self.node2inter[node_id]\n", " _, prow = self.load_prow(inter_no, min_start_unix)\n", " cycle = prow.iloc[0]['cycle']\n", " lsbs = min_start_unix\n", " while lsbs > sim_start:\n", " lsbs -= cycle\n", " self.offsets[node_id] = lsbs - sim_start\n", " group = group[group.start_unix >= lsbs]\n", " start_unixes = np.array(group.start_unix)\n", " start_unixes = np.sort(np.unique(start_unixes))[:self.node2num_cycles[node_id]]\n", "\n", " group = group[group.start_unix.isin(start_unixes)]\n", " self.sigtable.append(group)\n", "self.sigtable = pd.concat(self.sigtable).reset_index(drop=True)\n", "self.sigtable['phase_sumo'] = self.sigtable.groupby(['node_id', 'start_unix']).cumcount()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(inter_no for inter_no in set(self.sigtable.inter_no))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(node_id for node_id in set(self.sigtable.node_id))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 5-1\n", "self.set_timepoints()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "self.SIGTABLE = []\n", "for node_id in self.node_ids:\n", " sig = self.sigtable.query('node_id==@node_id')\n", " display(sig)\n", " for i, row in sig.iterrows():\n", " inter_no = row.inter_no\n", " phas_A = row.phas_A\n", " phas_B = row.phas_B\n", " start_unix = row.start_unix\n", " prow = self.load_prow(inter_no, start_unix)[1].iloc[0]\n", " red_A = prow[f'red_A{phas_A}']\n", " yellow_A = prow[f'yellow_A{phas_A}']\n", " red_B = prow[f'red_B{phas_B}']\n", " yellow_B = prow[f'yellow_B{phas_B}']\n", " sig.loc[i, ['red_A', 'red_B', 'yellow_A', 'yellow_B']] = red_A, red_B, yellow_A, yellow_B\n", " try:\n", " sig = sig.astype({'red_A': int, 'red_B': int, 'yellow_A': int, 'yellow_B': int,\n", " 'phas_A':str, 'phas_B':str})\n", " sig = sig.drop(['move_A','move_B'], axis=1)\n", " except:\n", " print(node_id)\n", "\n", " # sig_A = sig[['start_unix', 'phas_A', 'duration', 'state_A', 'red_A', 'yellow_A']].reset_index(drop=True)\n", " # sig_B = sig[['start_unix', 'phas_B', 'duration', 'state_B', 'red_B', 'yellow_B']].reset_index(drop=True)\n", "\n", " # csig_A = self.cumulate(sig_A, 'A')\n", " # csig_B = self.cumulate(sig_B, 'B')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 5-2. 적색 및 황색신호 부여\n", "self.assign_red_yellow()" ] } ], "metadata": { "kernelspec": { "display_name": "siggen_env", "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.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }