{ "cells": [ { "cell_type": "code", "execution_count": 1, "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": 2, "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": 3, "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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.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", "
inter_nonode_idmove_noinc_direout_direinc_edge_idout_edge_idturn_type
0175i01571545870_02571542797_02left
1175i02571510153_02571545870_01straight
2175i03-571542797_02571510153_01left
3175i04-571500487_01571542797_02straight
4175i05571510153_02571500487_01left
...........................
123210i612북서남동571500535_02.18571542115_01straight
124210i613남서북서571500585_02571500535_01left
125210i614북동남서571511538_02.121571500585_01straight
126210i615북서북동571500535_02.18571511538_01left
127210i616남동북서-571542115_01571500535_01straight
\n", "

128 rows × 8 columns

\n", "
" ], "text/plain": [ " inter_no node_id move_no inc_dire out_dire inc_edge_id \\\n", "0 175 i0 1 동 남 571545870_02 \n", "1 175 i0 2 서 동 571510153_02 \n", "2 175 i0 3 남 서 -571542797_02 \n", "3 175 i0 4 북 남 -571500487_01 \n", "4 175 i0 5 서 북 571510153_02 \n", ".. ... ... ... ... ... ... \n", "123 210 i6 12 북서 남동 571500535_02.18 \n", "124 210 i6 13 남서 북서 571500585_02 \n", "125 210 i6 14 북동 남서 571511538_02.121 \n", "126 210 i6 15 북서 북동 571500535_02.18 \n", "127 210 i6 16 남동 북서 -571542115_01 \n", "\n", " out_edge_id turn_type \n", "0 571542797_02 left \n", "1 571545870_01 straight \n", "2 571510153_01 left \n", "3 571542797_02 straight \n", "4 571500487_01 left \n", ".. ... ... \n", "123 571542115_01 straight \n", "124 571500535_01 left \n", "125 571500585_01 straight \n", "126 571511538_01 left \n", "127 571500535_01 straight \n", "\n", "[128 rows x 8 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "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", "\n", "# (1) 각 교차로별 방향 목록 : pdires (possible directions)\n", "p2dires = {} # parent_id to directions\n", "for parent_id in self.parent_ids:\n", " dires = self.match7[self.match7.node_id == parent_id][['inc_dire','out_dire']].values.flatten()\n", " dires = {dire for dire in dires if type(dire)==str}\n", " p2dires[parent_id] = dires\n", "\n", "# (2) 각 (교차로, 진입방향) 별 진입id 목록 : inc2id (incoming direction to incoming edge_id)\n", "inc2id = {}\n", "for parent_id in self.parent_ids:\n", " for inc_dire in p2dires[parent_id]:\n", " df = self.match7[(self.match7.node_id==parent_id) & (self.match7.inc_dire==inc_dire)]\n", " inc2id[(parent_id, inc_dire)] = df.inc_edge_id.iloc[0]\n", "\n", "# (3) 각 (교차로, 진출방향) 별 진출id 목록 : out2id (outgoing direction to outgoing edge_id)\n", "out2id = {}\n", "for parent_id in self.parent_ids:\n", " for out_dire in p2dires[parent_id]:\n", " df = self.match7[(self.match7.node_id==parent_id) & (self.match7.out_dire==out_dire)]\n", " out2id[(parent_id, out_dire)] = df.out_edge_id.iloc[0]\n", "\n", "# (4) 각 parent_id별 이동류번호 목록\n", "p2move = 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(p2dires[parent_id]) & self.nema.out_dire.isin(p2dires[parent_id])]\n", " p2move[parent_id] = list(pnema.move_no)\n", "\n", "# (5) 방위별 방향벡터\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", "# (6) 각 parent_id별 : 각 이동류별 진입/진출 엣지 id\n", "p2move2inc_edge_id = dict() # parent id to move2inc_edge_id\n", "p2move2out_edge_id = dict() # parent id to move2out_edge_id\n", "for parent_id in self.parent_ids:\n", " move2inc_edge_id = dict() # plain movement to incoming edge id\n", " move2out_edge_id = dict() # plain movement to outgoing edge id\n", " for move_no in range(1,17):\n", " row = self.nema[self.nema.move_no==move_no].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", " move2inc_edge_id[move_no] = inc_edge_id\n", " move2out_edge_id[move_no] = out_edge_id\n", " p2move2inc_edge_id[parent_id] = move2inc_edge_id\n", " p2move2out_edge_id[parent_id] = move2out_edge_id\n", "\n", "# (7) 각 이동류별 진입/진출 방위\n", "m2inc_dire = dict()\n", "m2out_dire = dict()\n", "for move_no in range(1,17):\n", " row = self.nema[self.nema.move_no==move_no].iloc[0]\n", " m2inc_dire[move_no] = row.inc_dire\n", " m2out_dire[move_no] = row.out_dire\n", "\n", "# (8) 가능한 모든 이동류에 대하여 진입id, 진출id 배정 : matching\n", "self.matching = []\n", "for parent_id in self.parent_ids:\n", " inter_no = self.node2inter[parent_id]\n", " # 좌회전과 직진(1 ~ 16)\n", " for move_no in range(1,17):\n", " inc_dire = m2inc_dire[move_no]\n", " out_dire = m2out_dire[move_no]\n", " if move_no in p2move[parent_id]:\n", " inc_edge_id = inc2id[(parent_id, inc_dire)]\n", " out_edge_id = out2id[(parent_id, out_dire)]\n", " else:\n", " inc_edge_id = p2move2inc_edge_id[parent_id][move_no]\n", " out_edge_id = p2move2out_edge_id[parent_id][move_no]\n", " if (inc_edge_id, out_edge_id) in self.n2io2turn[parent_id]:\n", " turn_type = self.n2io2turn[parent_id][inc_edge_id, out_edge_id]\n", " else:\n", " turn_type = 'left' if move_no%2 else 'straight'\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'node_id':[parent_id], '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],\n", " 'turn_type': turn_type})\n", " self.matching.append(new_row)\n", "child_matching = self.match7[self.match7.node_id.isin(self.child_ids)]\n", "child_matching = child_matching.drop(columns=['inc_angle', 'out_angle'])\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", "self.matching" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{('-571542797_02', '571500487_01'): 'straight',\n", " ('-571500487_01', '571542797_02'): 'straight',\n", " ('-571500487_01', '571545870_01'): 'left',\n", " ('-571542797_02', '571510153_01'): 'left',\n", " ('571545870_02', '571510153_01'): 'straight',\n", " ('571545870_02', '571542797_02'): 'left',\n", " ('571510153_02', '571500487_01'): 'left',\n", " ('571510153_02', '571545870_01'): 'straight'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "self.n2io2turn['i0']" ] }, { "cell_type": "code", "execution_count": 62, "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", "
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
\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", " 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 " ] }, "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", "
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
\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", " 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 " ] }, "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", "
inter_nomove_noinc_direout_direinc_edge_idout_edge_idnode_id
01751571545870_02571542797_02i0
11752571510153_02571545870_01i0
21753-571542797_02571510153_01i0
31754-571500487_01571542797_02i0
41755571510153_02571500487_01i0
\n", "
" ], "text/plain": [ " inter_no move_no inc_dire out_dire inc_edge_id out_edge_id node_id\n", "0 175 1 동 남 571545870_02 571542797_02 i0\n", "1 175 2 서 동 571510153_02 571545870_01 i0\n", "2 175 3 남 서 -571542797_02 571510153_01 i0\n", "3 175 4 북 남 -571500487_01 571542797_02 i0\n", "4 175 5 서 북 571510153_02 571500487_01 i0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# display(self.match1.head())\n", "# display(self.match2.head())\n", "# display(self.match3.head())\n", "# display(self.match4.head())\n", "display(self.match5.head())\n", "display(self.match6.head())\n", "display(self.matching.head())" ] } ], "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 }