{ "cells": [ { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. 데이터를 로드합니다.\n", "1-1. 네트워크가 로드되었습니다.\n", "1-2. 테이블들이 로드되었습니다.\n", "1-3. 네트워크의 모든 clean state requirement들을 체크했습니다.\n", "1-4. 테이블들의 무결성 검사를 완료했습니다.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "이동류정보 불러오는 중: 100%|██████████| 17280/17280 [00:13<00:00, 1247.47it/s]\n" ] } ], "source": [ "import sys\n", "from datetime import datetime\n", "sys.path.append('../../Scripts')\n", "from preprocess_daily import DailyPreprocessor\n", "self = DailyPreprocessor()\n", "\n", "# 1. 데이터 준비\n", "self.load_data()\n", "\n", "self.make_match1()\n", "self.make_match2()\n", "self.make_match3()\n", "self.make_match4()\n", "self.make_match5()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "u00 True\n", "u20 False\n", "u30 True\n", "u31 True\n", "u32 True\n", "u60 True\n" ] } ], "source": [ "self.node2inter = dict(zip(self.inter_node['node_id'], self.inter_node['inter_no']))\n", "\n", "child_ids = self.inter_node[self.inter_node.inter_type=='child'].node_id.unique()\n", "ch2pa = {} # child to parent\n", "for child_id in child_ids:\n", " parent_no = self.inter_node[self.inter_node.node_id==child_id].inter_no.iloc[0]\n", " sub_inter_node = self.inter_node[self.inter_node.inter_no==parent_no]\n", " ch2pa[child_id] = sub_inter_node[sub_inter_node.inter_type=='parent'].iloc[0].node_id\n", "directions = ['북', '북동', '동', '남동', '남', '남서', '서', '북서'] # 정북기준 시계방향으로 8방향\n", "\n", "# 각 uturn node에 대하여 (inc_edge_id, out_edge_id) 부여\n", "cmatches = []\n", "for _, row in self.uturn.iterrows():\n", " child_id = row.child_id\n", " parent_id = row.parent_id\n", " direction = row.direction\n", " condition = row.condition\n", " inc_edge_id = row.inc_edge\n", " out_edge_id = row.out_edge\n", " # match5에서 parent_id에 해당하는 행들을 가져옴\n", " cmatch = self.match5.copy()[self.match5.node_id==parent_id] # match dataframe for a child node\n", " cmatch = cmatch.sort_values(by=['phase_no', 'ring_type']).reset_index(drop=True)\n", " cmatch['node_id'] = child_id\n", " cmatch[['inc_edge', 'out_edge']] = np.nan\n", "\n", " # condition 별로 inc_dire, out_dire_A, out_dire_B를 정함\n", " ind = directions.index(direction)\n", " if condition == \"좌회전시\":\n", " inc_dire = direction\n", " out_dire_A = out_dire_B = directions[(ind + 2) % len(directions)]\n", " elif condition == \"보행신호시\":\n", " inc_dire = directions[(ind + 2) % len(directions)]\n", " out_dire_A = directions[(ind - 2) % len(directions)]\n", " out_dire_B = directions[(ind - 2) % len(directions)]\n", " print(child_id, ((cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A)).any())\n", " # (inc_dire, out_dire_A, out_dire_B) 별로 inc_edge_id, out_edge_id를 정함\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_B), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " if condition == '보행신호시':\n", " # 이동류번호가 17(보행신호)이면서 유턴노드방향으로 가는 신호가 없으면 (inc_edge_id, out_edge_id)를 부여한다.\n", " cmatch.loc[(cmatch.move_no==17) & (cmatch.out_dir!=direction), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " # 유턴신호의 이동류번호를 19로 부여한다.\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A), 'move_no'] = 19\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_B), 'move_no'] = 19\n", " cmatches.append(cmatch)\n", "\n", "# 각 coordination node에 대하여 (inc_edge_id, out_edge_id) 부여\n", "self.coord['inter_no'] = self.coord['parent_id'].map(self.node2inter)\n", "self.coord = self.coord.rename(columns={'child_id':'node_id'})\n", "self.coord[['inc_dir', 'out_dir', 'inc_angle','out_angle']] = np.nan\n", "self.coord['move_no'] = 20\n", "self.coord = self.coord[['inter_no', 'phase_no', 'ring_type', 'move_no', 'inc_dir', 'out_dir', 'inc_angle','out_angle', 'inc_edge', 'out_edge', 'node_id']]\n", "\n", "# display(coord)\n", "cmatches = pd.concat(cmatches)\n", "self.match6 = pd.concat([self.match5, cmatches, self.coord]).drop_duplicates().sort_values(by=['inter_no', 'node_id', 'phase_no', 'ring_type'])\n", "# self.match6.to_csv(os.path.join(self.path_intermediates, 'match6.csv'))" ] }, { "cell_type": "code", "execution_count": 34, "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", "
inter_nophase_noring_typemove_noinc_dirout_dirinc_angleout_angleinc_edgeout_edgenode_id
01751A8179004-571542797_02571500487_01i0
11751B4003176-571500487_01571542797_02i0
21752A7001095-571500487_01571545870_01i0
31752B3179270-571542797_02571510153_01i0
41753A6090270571545870_02571510153_01i0
....................................
32102B2270090NaNNaNu60
42103A7359090NaNNaNu60
52103B4000180NaNNaNu60
62104A8180000NaNNaNu60
72104B3180270NaNNaNu60
\n", "

116 rows × 11 columns

\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle out_angle \\\n", "0 175 1 A 8 남 북 179 004 \n", "1 175 1 B 4 북 남 003 176 \n", "2 175 2 A 7 북 동 001 095 \n", "3 175 2 B 3 남 서 179 270 \n", "4 175 3 A 6 동 서 090 270 \n", ".. ... ... ... ... ... ... ... ... \n", "3 210 2 B 2 서 동 270 090 \n", "4 210 3 A 7 북 동 359 090 \n", "5 210 3 B 4 북 남 000 180 \n", "6 210 4 A 8 남 북 180 000 \n", "7 210 4 B 3 남 서 180 270 \n", "\n", " inc_edge out_edge node_id \n", "0 -571542797_02 571500487_01 i0 \n", "1 -571500487_01 571542797_02 i0 \n", "2 -571500487_01 571545870_01 i0 \n", "3 -571542797_02 571510153_01 i0 \n", "4 571545870_02 571510153_01 i0 \n", ".. ... ... ... \n", "3 NaN NaN u60 \n", "4 NaN NaN u60 \n", "5 NaN NaN u60 \n", "6 NaN NaN u60 \n", "7 NaN NaN u60 \n", "\n", "[116 rows x 11 columns]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.match6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "splits 딕셔너리 다시 만들기" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dura_A1 37\n", "dura_A2 39\n", "dura_A3 55\n", "dura_A4 29\n", "dura_A5 0\n", "dura_A6 0\n", "dura_A7 0\n", "dura_A8 0\n", "Name: 0, dtype: int64\n", "dura_B1 37\n", "dura_B2 39\n", "dura_B3 25\n", "dura_B4 59\n", "dura_B5 0\n", "dura_B6 0\n", "dura_B7 0\n", "dura_B8 0\n", "Name: 0, dtype: int64\n" ] }, { "ename": "NameError", "evalue": "name 'dura_A' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[53], line 11\u001b[0m\n\u001b[0;32m 9\u001b[0m duration_dict \u001b[38;5;241m=\u001b[39m {}\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m# 두 시리즈의 길이가 같다고 가정합니다.\u001b[39;00m\n\u001b[1;32m---> 11\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;28mlen\u001b[39m(\u001b[43mdura_A\u001b[49m)):\n\u001b[0;32m 12\u001b[0m \u001b[38;5;66;03m# A와 B의 현시간이 같은 경우\u001b[39;00m\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dura_A[i] \u001b[38;5;241m==\u001b[39m dura_B[i]:\n\u001b[0;32m 14\u001b[0m duration_dict[(i\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m, i\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m)] \u001b[38;5;241m=\u001b[39m dura_A[i]\n", "\u001b[1;31mNameError\u001b[0m: name 'dura_A' is not defined" ] } ], "source": [ "import numpy as np\n", "row = self.plan.iloc[0]\n", "inter_no = row.inter_no\n", "start_hour = row.start_hour\n", "start_minute = row.start_minute\n", "cycle = row.cycle\n", "print(row[[f'dura_A{j}' for j in range(1,9)]])\n", "print(row[[f'dura_B{j}' for j in range(1,9)]])\n", "\n", "\n", "dura_A = row[[f'dura_A{j}' for j in range(1, 9)]]\n", "dura_B = row[[f'dura_B{j}' for j in range(1, 9)]]\n", "\n", "duration_dict = {}\n", "# 두 시리즈의 길이가 같다고 가정합니다.\n", "for i in range(len(dura_A)):\n", " # A와 B의 현시간이 같은 경우\n", " if dura_A[i] == dura_B[i]:\n", " duration_dict[(i+1, i+1)] = dura_A[i]\n", " # A와 B의 현시간이 다른 경우\n", " else:\n", " duration_dict[(i+1, i+1)] = min(dura_A[i], dura_B[i])\n", " duration_dict[(i+1, i+2)] = max(dura_A[i], dura_B[i])\n", "\n", "print(duration_dict)\n", "# cums_A = row[[f'dura_A{j}' for j in range(1,9)]].cumsum()\n", "# cums_B = row[[f'dura_B{j}' for j in range(1,9)]].cumsum()\n", "# print(cums_A)\n", "# print(cums_B)\n", "# detailed_cums = []\n", "# combined_row = np.unique(np.concatenate((cums_A,cums_B)))\n", "# print(combined_row)\n", "# detailed_durations = np.concatenate(([combined_row[0]], np.diff(combined_row)))\n", "# print(detailed_durations)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# split, isplit : A,B 분리 혹은 통합시 사용될 수 있는 딕셔너리 \n", "self.splits = {} # splits maps (inter_no, start_hour, start_minute) to split \n", "for i, row in self.plan.iterrows():\n", " inter_no = row.inter_no\n", " start_hour = row.start_hour\n", " start_minute = row.start_minute\n", " cycle = row.cycle\n", " cums_A = row[[f'dura_A{j}' for j in range(1,9)]].cumsum()\n", " cums_B = row[[f'dura_B{j}' for j in range(1,9)]].cumsum()\n", " self.splits[(inter_no, start_hour, start_minute)] = {} # split maps (phas_A, phas_B) to k\n", " k = 0\n", " for t in range(cycle):\n", " new_phas_A = len(cums_A[cums_A < t]) + 1\n", " new_phas_B = len(cums_B[cums_B < t]) + 1\n", " if k == 0 or ((new_phas_A, new_phas_B) != (phas_A, phas_B)):\n", " k += 1\n", " phas_A = new_phas_A\n", " phas_B = new_phas_B\n", " self.splits[(inter_no, start_hour, start_minute)][(phas_A, phas_B)] = k\n", "self.isplits = {} # the inverse of splits\n", "for i in self.splits:\n", " self.isplits[i] = {self.splits[i][k]:k for k in self.splits[i]} # isplit maps k to (phas_A, phas_B)\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{(175, 0, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (3, 4): 4, (4, 4): 5},\n", " (175, 7, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (3, 4): 4, (4, 4): 5},\n", " (175, 9, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (3, 4): 4, (4, 4): 5},\n", " (175, 18, 30): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (3, 4): 4, (4, 4): 5},\n", " (176, 0, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3},\n", " (176, 7, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3},\n", " (176, 9, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3},\n", " (176, 18, 30): {(1, 1): 1, (2, 2): 2, (3, 3): 3},\n", " (177, 0, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (177, 7, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (177, 9, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (177, 18, 30): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (178, 0, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (178, 7, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 3): 4, (4, 4): 5},\n", " (178, 9, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 3): 4, (4, 4): 5},\n", " (178, 18, 30): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 3): 4, (4, 4): 5},\n", " (201, 0, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4, (5, 5): 5},\n", " (201, 7, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4, (5, 5): 5},\n", " (201, 9, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4, (5, 5): 5},\n", " (201, 18, 30): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4, (5, 5): 5},\n", " (202, 0, 0): {(1, 1): 1, (2, 2): 2},\n", " (202, 7, 0): {(1, 1): 1, (2, 2): 2},\n", " (202, 9, 0): {(1, 1): 1, (2, 2): 2},\n", " (202, 18, 30): {(1, 1): 1, (2, 2): 2},\n", " (206, 0, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (206, 7, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (206, 9, 0): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (206, 18, 30): {(1, 1): 1, (2, 2): 2, (3, 3): 3, (4, 4): 4},\n", " (210, 0, 0): {(1, 1): 1, (1, 2): 2, (2, 2): 3, (3, 3): 4, (4, 4): 5},\n", " (210, 7, 0): {(1, 1): 1, (1, 2): 2, (2, 2): 3, (3, 3): 4, (4, 4): 5},\n", " (210, 9, 0): {(1, 1): 1, (1, 2): 2, (2, 2): 3, (3, 3): 4, (4, 4): 5},\n", " (210, 18, 30): {(1, 1): 1, (1, 2): 2, (2, 2): 3, (3, 3): 4, (4, 4): 5}}" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.splits" ] } ], "metadata": { "kernelspec": { "display_name": "rts", "language": "python", "name": "rts" }, "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }