{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "41.0\n" ] } ], "source": [ "time_data = \"\"\"\n", "3h\n", "0.5h\n", "3h\n", "2h\n", "2h\n", "4h\n", "3h\n", "1.5h\n", "4h\n", "3.5h\n", "2.5h\n", "2h\n", "3h\n", "3h\n", "2h\n", "1h\n", "0h\n", "0.5h\n", "0.5h\n", "\"\"\"\n", "time_list = time_data.strip().split('\\n')\n", "total_hours = sum(float(time.replace('h', '')) for time in time_list)\n", "print(total_hours)" ] }, { "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" ] }, { "name": "stderr", "output_type": "stream", "text": [ "이동류정보 불러오는 중: 100%|██████████| 17280/17280 [00:51<00:00, 334.89it/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": 4, "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" ] }, { "ename": "NameError", "evalue": "name 'pd' 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[4], line 55\u001b[0m\n\u001b[0;32m 52\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcoord \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcoord[[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minter_no\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mphase_no\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mring_type\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmove_no\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minc_dir\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout_dir\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minc_angle\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout_angle\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minc_edge\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout_edge\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnode_id\u001b[39m\u001b[38;5;124m'\u001b[39m]]\n\u001b[0;32m 54\u001b[0m \u001b[38;5;66;03m# display(coord)\u001b[39;00m\n\u001b[1;32m---> 55\u001b[0m cmatches \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241m.\u001b[39mconcat(cmatches)\n\u001b[0;32m 56\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmatch6 \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mconcat([\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmatch5, cmatches, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcoord])\u001b[38;5;241m.\u001b[39mdrop_duplicates()\u001b[38;5;241m.\u001b[39msort_values(by\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minter_no\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnode_id\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mphase_no\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mring_type\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[0;32m 57\u001b[0m \u001b[38;5;66;03m# self.match6.to_csv(os.path.join(self.path_intermediates, 'match6.csv'))\u001b[39;00m\n", "\u001b[1;31mNameError\u001b[0m: name 'pd' is not defined" ] } ], "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": 5, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'DailyPreprocessor' object has no attribute 'match6'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmatch6\u001b[49m\n", "\u001b[1;31mAttributeError\u001b[0m: 'DailyPreprocessor' object has no attribute 'match6'" ] } ], "source": [ "self.match6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "splits 딕셔너리 다시 만들기" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[37 39 55 29 0 0 0 0]\n", "[37 39 25 59 0 0 0 0]\n", "[ 37 76 131 160 160 160 160 160]\n", "[ 37 76 101 160 160 160 160 160]\n", "[ 37 76 101 131 160]\n", "[37 39 25 30 29]\n", "{(1, 1): 37, (2, 2): 39, (3, 3): 25, (3, 4): 55, (4, 4): 29, (4, 5): 59, (5, 5): 0, (6, 6): 0, (7, 7): 0, (8, 8): 0}\n", "\n", "[37 39 55 29 0 0 0 0]\n", "[37 39 25 59 0 0 0 0]\n", "[ 0 39 55 29 0 0 0 0]\n", "[ 0 0 55 29 0 0 0 0]\n", "[ 0 0 30 29 0 0 0 0]\n", "[ 0 0 30 -1 0 0 0 0]\n", "[ 0 0 30 -1 -29 0 0 0]\n" ] } ], "source": [ "import numpy as np\n", "row = self.plan.iloc[0]\n", "# print(row)\n", "inter_no = row.inter_no\n", "start_hour = row.start_hour\n", "start_minute = row.start_minute\n", "cycle = row.cycle\n", "\n", "dura_A = np.array(row[[f'dura_A{j}' for j in range(1, 9)]])\n", "dura_B = np.array(row[[f'dura_B{j}' for j in range(1, 9)]])\n", "\n", "print(np.array(dura_A))\n", "print(np.array(dura_B))\n", "\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", "\n", "print(np.array(cums_A))\n", "print(np.array(cums_B))\n", "\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", "\n", "print(detailed_durations)\n", "\n", "desired_dict = {(1, 1): 37, (2, 2): 39, (3, 3): 25, (3, 4): 55, (4, 4): 29, (4, 5): 59, (5, 5): 0, (6, 6): 0, (7, 7): 0, (8, 8): 0}\n", "print(desired_dict)\n", "\n", "print()\n", "print(dura_A)\n", "print(dura_B)\n", "j = 0\n", "for i in range(len(detailed_durations)):\n", " dura_A[j] -= detailed_durations[i]\n", " print(dura_A)\n", " j += 1" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[37 39 55 29 0 0 0 0]\n", "[37 39 25 59 0 0 0 0]\n", "[ 37 76 131 160 160 160 160 160]\n", "[ 37 76 101 160 160 160 160 160]\n", "[ 37 76 101 131 160]\n", "[37 39 25 30 29]\n", "{(1, 1): 37, (2, 2): 39, (3, 3): 25, (3, 4): 55, (4, 4): 29, (4, 5): 59, (5, 5): 0, (6, 6): 0, (7, 7): 0, (8, 8): 0}\n" ] } ], "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", "\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", "print(np.array(dura_A))\n", "print(np.array(dura_B))\n", "\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", "\n", "print(np.array(cums_A))\n", "print(np.array(cums_B))\n", "\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", "\n", "print(detailed_durations)\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 }