{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "import numpy as np\n", "import sys\n", "sys.path.append('../../Scripts')\n", "from preprocess_daily import DailyPreprocessor\n", "from generate_signals import SignalGenerator" ] }, { "cell_type": "code", "execution_count": 3, "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", "1-5. 주요 객체 (리스트, 딕셔너리)들을 저장했습니다.\n" ] } ], "source": [ "self = DailyPreprocessor()\n", "self.load_data()\n", "self.make_match1()\n", "self.make_match2()\n", "self.make_match3()\n", "self.make_match4()\n", "self.make_match5()\n", "self.make_match6()" ] }, { "cell_type": "code", "execution_count": 4, "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", "
inter_nophase_noring_typemove_noinc_direout_direinc_angleout_angleinc_edge_idout_edge_idnode_idturn_type
01751A8179000-571542797_02571500487_01i0straight
11751B4001180-571500487_01571542797_02i0straight
21752A7001090-571500487_01571545870_01i0left
31752B3179270-571542797_02571510153_01i0left
41753A6090270571545870_02571510153_01i0straight
.......................................
1032102B2270090NaNNaNu60straight
1042103A7000090NaNNaNu60left
1052103B4000180NaNNaNu60straight
1062104A8180000NaNNaNu60straight
1072104B3180270NaNNaNu60left
\n", "

108 rows × 12 columns

\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dire out_dire inc_angle \\\n", "0 175 1 A 8 남 북 179 \n", "1 175 1 B 4 북 남 001 \n", "2 175 2 A 7 북 동 001 \n", "3 175 2 B 3 남 서 179 \n", "4 175 3 A 6 동 서 090 \n", ".. ... ... ... ... ... ... ... \n", "103 210 2 B 2 서 동 270 \n", "104 210 3 A 7 북 동 000 \n", "105 210 3 B 4 북 남 000 \n", "106 210 4 A 8 남 북 180 \n", "107 210 4 B 3 남 서 180 \n", "\n", " out_angle inc_edge_id out_edge_id node_id turn_type \n", "0 000 -571542797_02 571500487_01 i0 straight \n", "1 180 -571500487_01 571542797_02 i0 straight \n", "2 090 -571500487_01 571545870_01 i0 left \n", "3 270 -571542797_02 571510153_01 i0 left \n", "4 270 571545870_02 571510153_01 i0 straight \n", ".. ... ... ... ... ... \n", "103 090 NaN NaN u60 straight \n", "104 090 NaN NaN u60 left \n", "105 180 NaN NaN u60 straight \n", "106 000 NaN NaN u60 straight \n", "107 270 NaN NaN u60 left \n", "\n", "[108 rows x 12 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.match6" ] }, { "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", "
inter_nophase_noring_typemove_noinc_direout_direinc_angleout_angleinc_edge_idout_edge_idnode_idturn_type
802021A6090270571510152_02-571510152_01i9straight
812021B2270090571510152_01571510152_01.65i9straight
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dire out_dire inc_angle \\\n", "80 202 1 A 6 동 서 090 \n", "81 202 1 B 2 서 동 270 \n", "\n", " out_angle inc_edge_id out_edge_id node_id turn_type \n", "80 270 571510152_02 -571510152_01 i9 straight \n", "81 090 571510152_01 571510152_01.65 i9 straight " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.match6[self.match6.node_id==parent_id].dropna(subset=['inc_edge_id', 'out_edge_id'])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "self.match7 = self.match6.copy()\n", "self.match7 = self.match7[['inter_no', 'node_id', 'move_no', 'inc_angle', 'out_angle', 'inc_dire', 'out_dire', 'inc_edge_id', 'out_edge_id']]\n", "# parent_id = self.parent_ids[1]\n", "\n", "for parent_id in self.parent_ids:\n", " m7 = self.match7[self.match7.node_id==parent_id].dropna(subset=['inc_edge_id', 'out_edge_id'])\n", " m6 = self.match6[self.match6.node_id==parent_id].dropna(subset=['inc_edge_id', 'out_edge_id'])\n", " inc_angles = [int(angle) for angle in np.unique(m7.inc_angle.values)]\n", " out_angles = [int(angle) for angle in np.unique(m7.out_angle.values)]\n", " # print(parent_id)\n", " # display(m6)\n", " # display(m7)\n", " # print(inc_angles)\n", " # print(out_angles)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# (1) 가능한 (진입방향, 진출방향) 목록 \n", "flows = self.nema.dropna().apply(lambda row: (row['inc_dire'], row['out_dire']), axis=1).tolist()\n", "# (2) 각 교차로별 방향 목록 : pdires (possible directions)\n", "pdires = {}\n", "for node_id in self.parent_ids:\n", " dires = self.match7[self.match7.node_id == node_id][['inc_dire','out_dire']].values.flatten()\n", " dires = {dire for dire in dires if type(dire)==str}\n", " pdires[node_id] = dires\n", "# (3) 각 (교차로, 진입방향) 별 진입id 목록 : inc2id (incoming direction to incoming edge_id)\n", "inc2id = {}\n", "for node_id in self.parent_ids:\n", " for inc_dire in pdires[node_id]:\n", " df = self.match7[(self.match7.node_id==node_id) & (self.match7.inc_dire==inc_dire)]\n", " inc2id[(node_id, inc_dire)] = df.inc_edge_id.iloc[0]\n", "# (4) 각 (교차로, 진출방향) 별 진출id 목록 : out2id (outgoing direction to outgoing edge_id)\n", "out2id = {}\n", "for node_id in self.parent_ids:\n", " for out_dire in pdires[node_id]:\n", " df = self.match7[(self.match7.node_id==node_id) & (self.match7.out_dire==out_dire)]\n", " out2id[(node_id, out_dire)] = df.out_edge_id.iloc[0]\n", "# (5) 각 교차로별 가능한 (진입방향, 진출방향) 목록 : pflow (possible flows)\n", "pflow = {}\n", "for node_id in self.parent_ids:\n", " pflow[node_id] = [flow for flow in flows if set(flow).issubset(pdires[node_id])]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i0\n" ] }, { "data": { "text/plain": [ "{1: '571545870_02',\n", " 2: '571510153_02',\n", " 3: '-571542797_02',\n", " 4: '-571500487_01',\n", " 5: '571510153_02',\n", " 6: '571545870_02',\n", " 7: '-571500487_01',\n", " 8: '-571542797_02',\n", " 9: '-571500487_01',\n", " 10: '571510153_02',\n", " 11: '571545870_02',\n", " 12: '-571500487_01',\n", " 13: '571510153_02',\n", " 14: '-571500487_01',\n", " 15: '-571500487_01',\n", " 16: '571545870_02'}" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{1: '571542797_02',\n", " 2: '571545870_01',\n", " 3: '571510153_01',\n", " 4: '571542797_02',\n", " 5: '571500487_01',\n", " 6: '571510153_01',\n", " 7: '571545870_01',\n", " 8: '571500487_01',\n", " 9: '571545870_01',\n", " 10: '571500487_01',\n", " 11: '571510153_01',\n", " 12: '571545870_01',\n", " 13: '571500487_01',\n", " 14: '571510153_01',\n", " 15: '571500487_01',\n", " 16: '571500487_01'}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p2am = dict() # parent id to a list of aligned movement numbers\n", "for parent_id in self.parent_ids:\n", " pnema = self.nema[self.nema.inc_dire.isin(pdires[parent_id]) & self.nema.out_dire.isin(pdires[parent_id])]\n", " p2am[parent_id] = list(pnema.move_no)\n", "\n", "dire2vec = dict() # direction to unit vector\n", "theta = np.pi/2\n", "for dire in self.dires:\n", " dire2vec[dire] = np.array([np.cos(theta), np.sin(theta)])\n", " theta -= np.pi/4\n", "\n", "plain_move_nos = list(range(1,17)) # plain movement numbers\n", "\n", "p2pm2inc_edge_id = dict() # parent id to pm2inc_edge_id\n", "p2pm2out_edge_id = dict() # parent id to pm2out_edge_id\n", "for parent_id in self.parent_ids:\n", " pm2inc_edge_id = dict() # plain movement to incoming edge id\n", " pm2out_edge_id = dict() # plain movement to outgoing edge id\n", " for pm in plain_move_nos:\n", " row = self.nema[self.nema.move_no==pm].iloc[0]\n", " inc_dire = row.inc_dire\n", " out_dire = row.out_dire\n", " inc_vec_true = dire2vec[inc_dire]\n", " out_vec_true = dire2vec[out_dire]\n", "\n", " node = self.net.getNode(parent_id)\n", " # 교차로의 모든 (from / to) edges\n", " inc_edges = [edge for edge in node.getIncoming() if edge.getFunction() == ''] # incoming edges\n", " out_edges = [edge for edge in node.getOutgoing() if edge.getFunction() == ''] # outgoing edges\n", " # 교차로의 모든 (from / to) unit vector\n", " inc_vecs = []\n", " for inc_edge in inc_edges:\n", " start = inc_edge.getShape()[-1]\n", " end = inc_edge.getShape()[-2]\n", " inc_vec = np.array(end) - np.array(start)\n", " inc_vec = inc_vec / (inc_vec ** 2).sum() ** 0.5\n", " inc_vecs.append(inc_vec)\n", " out_vecs = []\n", " for out_edge in out_edges:\n", " start = out_edge.getShape()[0]\n", " end = out_edge.getShape()[1]\n", " out_vec = np.array(end) - np.array(start)\n", " out_vec = out_vec / (out_vec ** 2).sum() ** 0.5\n", " out_vecs.append(out_vec)\n", " # 매칭 엣지 반환\n", " inc_index = np.array([np.dot(inc_vec, inc_vec_true) for inc_vec in inc_vecs]).argmax()\n", " out_index = np.array([np.dot(out_vec, out_vec_true) for out_vec in out_vecs]).argmax()\n", " inc_edge_id = inc_edges[inc_index].getID()\n", " out_edge_id = out_edges[out_index].getID()\n", " pm2inc_edge_id[pm] = inc_edge_id\n", " pm2out_edge_id[pm] = out_edge_id\n", " p2pm2inc_edge_id[parent_id] = pm2inc_edge_id\n", " p2pm2out_edge_id[parent_id] = pm2out_edge_id\n", "\n", "parent_id = self.parent_ids[0]\n", "print(parent_id)\n", "display(p2pm2inc_edge_id[parent_id])\n", "display(p2pm2out_edge_id[parent_id])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "plain_move_nos = list(range(1,17)) # plain movement numbers\n", "p2pm2inc_edge_id = dict() # parent id to pm2inc_edge_id\n", "p2pm2out_edge_id = dict() # parent id to pm2out_edge_id\n", "for parent_id in self.parent_ids:\n", " pm2inc_angle = dict()\n", " for pm in plain_move_nos:\n", " row = self.nema[self.nema.move_no==pm].iloc[0]\n", " # inc_dire = row.\n", "# plain movement number(1-16) to (inc_angle, out_angle)\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('동', '남'), ('서', '동'), ('남', '서'), ('북', '남'), ('서', '북'), ('동', '서'), ('북', '동'), ('남', '북'), ('북동', '남동'), ('남서', '북동'), ('남동', '남서'), ('북서', '남동'), ('남서', '북서'), ('북동', '남서'), ('북서', '북동'), ('남동', '북서')]\n", "{'i0': {'북', '남', '동', '서'}, 'i1': {'북', '남', '서'}, 'i2': {'북', '남'}, 'i3': {'북', '남', '동', '서'}, 'i6': {'북', '남', '동', '서'}, 'i7': {'북', '남'}, 'i8': {'북', '남', '서', '동'}, 'i9': {'동', '서'}}\n", "{('i0', '북'): '-571500487_01', ('i0', '남'): '-571542797_02', ('i0', '동'): '571545870_02', ('i0', '서'): '571510153_02', ('i1', '북'): '571542797_02.99', ('i1', '남'): '-571542810_01', ('i1', '서'): '571543469_02', ('i2', '북'): '571542811_02', ('i2', '남'): '-571542809_01', ('i3', '북'): '571556450_02', ('i3', '남'): '571540304_02', ('i3', '동'): '-571500475_01', ('i3', '서'): '571540303_02.21', ('i6', '북'): '571511538_02.121', ('i6', '남'): '571500585_02', ('i6', '동'): '-571542115_01', ('i6', '서'): '571500535_02.18', ('i7', '북'): '571542073_01', ('i7', '남'): '-571511538_02', ('i8', '북'): '571500583_01', ('i8', '남'): '-571500569_01', ('i8', '서'): '571500618_02', ('i8', '동'): '571500617_02', ('i9', '동'): '571510152_02', ('i9', '서'): '571510152_01'}\n", "{('i0', '북'): '571500487_01', ('i0', '남'): '571542797_02', ('i0', '동'): '571545870_01', ('i0', '서'): '571510153_01', ('i1', '북'): '-571542797_02.99', ('i1', '남'): '571542810_01', ('i1', '서'): '571543469_01', ('i2', '북'): '571542811_01', ('i2', '남'): '571542809_01', ('i3', '북'): '571556450_01', ('i3', '남'): '571540304_01', ('i3', '동'): '571500475_01', ('i3', '서'): '571540303_01', ('i6', '북'): '571511538_01', ('i6', '남'): '571500585_01', ('i6', '동'): '571542115_01', ('i6', '서'): '571500535_01', ('i7', '북'): '571542073_02', ('i7', '남'): '571511538_02', ('i8', '북'): '571500583_02', ('i8', '남'): '571500569_01', ('i8', '서'): '571500618_01', ('i8', '동'): '571500617_01', ('i9', '동'): '571510152_01.65', ('i9', '서'): '-571510152_01'}\n", "{'i0': [('동', '남'), ('서', '동'), ('남', '서'), ('북', '남'), ('서', '북'), ('동', '서'), ('북', '동'), ('남', '북')], 'i1': [('남', '서'), ('북', '남'), ('서', '북'), ('남', '북')], 'i2': [('북', '남'), ('남', '북')], 'i3': [('동', '남'), ('서', '동'), ('남', '서'), ('북', '남'), ('서', '북'), ('동', '서'), ('북', '동'), ('남', '북')], 'i6': [('동', '남'), ('서', '동'), ('남', '서'), ('북', '남'), ('서', '북'), ('동', '서'), ('북', '동'), ('남', '북')], 'i7': [('북', '남'), ('남', '북')], 'i8': [('동', '남'), ('서', '동'), ('남', '서'), ('북', '남'), ('서', '북'), ('동', '서'), ('북', '동'), ('남', '북')], 'i9': [('서', '동'), ('동', '서')]}\n" ] } ], "source": [ "print(flows)\n", "print(pdires)\n", "print(inc2id)\n", "print(out2id)\n", "print(pflow)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# (6) 가능한 이동류에 대하여 진입id, 진출id 배정 : matching\n", "dires_right = ['북', '북서', '서', '남서', '남', '남동', '동', '북동'] # ex (북, 서), (서, 남) 등은 우회전 flow\n", "self.matching = []\n", "for node_id in self.parent_ids:\n", " inter_no = self.node2inter[node_id]\n", " # 좌회전과 직진(1 ~ 16)\n", " for (inc_dire, out_dire) in pflow[node_id]:\n", " move_no = self.nema[(self.nema.inc_dire==inc_dire) & (self.nema.out_dire==out_dire)].move_no.iloc[0]\n", " inc_edge_id = inc2id[(node_id, inc_dire)]\n", " out_edge_id = out2id[(node_id, out_dire)]\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'move_no':[move_no],\n", " 'inc_dire':[inc_dire], 'out_dire':[out_dire],\n", " 'inc_edge_id':[inc_edge_id], 'out_edge_id':[out_edge_id], 'node_id':[node_id]})\n", " self.matching.append(new_row)\n", " # 보행신호(17), 전적색(18)\n", " new_row = pd.DataFrame({'inter_no':[inter_no] * 2, 'move_no':[17, 18],\n", " 'inc_dire':[None]*2, 'out_dire':[None]*2,\n", " 'inc_edge_id':[None]*2, 'out_edge_id':[None]*2, 'node_id':[node_id]*2})\n", " self.matching.append(new_row)\n", " # 신호우회전(21)\n", " for d in range(len(dires_right)):\n", " inc_dire = dires_right[d]\n", " out_dire = dires_right[(d+2)%8]\n", " if {inc_dire, out_dire}.issubset(pdires[node_id]):\n", " inc_edge_id = inc2id[(node_id, inc_dire)]\n", " out_edge_id = out2id[(node_id, out_dire)]\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'move_no':[21],\n", " 'inc_dire':[inc_dire], 'out_dire':[out_dire],\n", " 'inc_edge_id':[inc_edge_id], 'out_edge_id':[out_edge_id], 'node_id':[node_id]})\n", " self.matching.append(new_row)\n", "self.matching.append(self.match7[self.match7.node_id.isin(self.child_ids)])\n", "self.matching = pd.concat(self.matching)\n", "self.matching = self.matching.dropna(subset=['inc_edge_id', 'out_edge_id'])\\\n", " .sort_values(by=['inter_no', 'node_id', 'move_no']).reset_index(drop=True)\n", "self.matching['move_no'] = self.matching['move_no'].astype(int)\n" ] } ], "metadata": { "kernelspec": { "display_name": "sts", "language": "python", "name": "sts" }, "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 }