{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "import sumolib\n", "import random\n", "from tqdm import tqdm\n", "from datetime import datetime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A. 이동류 매칭" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 15/17280 [00:00<01:56, 148.29it/s]" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 17280/17280 [01:11<00:00, 242.06it/s]\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_Bmove_Amove_B
01751184
11752273
21753361
31753462
41754452
51761184
61762283
717633518
81771184
91772273
\n", "
" ], "text/plain": [ " inter_no phas_A phas_B move_A move_B\n", "0 175 1 1 8 4\n", "1 175 2 2 7 3\n", "2 175 3 3 6 1\n", "3 175 3 4 6 2\n", "4 175 4 4 5 2\n", "5 176 1 1 8 4\n", "6 176 2 2 8 3\n", "7 176 3 3 5 18\n", "8 177 1 1 8 4\n", "9 177 2 2 7 3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# [이동류번호] 불러오기 (약 1분의 소요시간)\n", "path_moves = '../../Data/tables/moves/'\n", "csv_moves = os.listdir('../../Data/tables/moves/')\n", "moves = [pd.read_csv(path_moves + csv_move, index_col=0) for csv_move in tqdm(csv_moves)]\n", "match1 = pd.concat(moves).drop_duplicates().sort_values(by=['inter_no','phas_A','phas_B']).reset_index(drop=True)\n", "match1.head(10)" ] }, { "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", "
inter_nophase_noring_typemove_no
01751A8
01751B4
11752A7
11752B3
21753A6
21753B1
41754A5
31754B2
51761A8
51761B4
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no\n", "0 175 1 A 8\n", "0 175 1 B 4\n", "1 175 2 A 7\n", "1 175 2 B 3\n", "2 175 3 A 6\n", "2 175 3 B 1\n", "4 175 4 A 5\n", "3 175 4 B 2\n", "5 176 1 A 8\n", "5 176 1 B 4" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 계층화 (inter_no, phas_A, phas_B, move_A, move_B) -> ('inter_no', 'phase_no', 'ring_type', 'move_no')\n", "matchA = match1[['inter_no', 'phas_A', 'move_A']].copy()\n", "matchA.columns = ['inter_no', 'phase_no', 'move_no']\n", "matchA['ring_type'] = 'A'\n", "matchB = match1[['inter_no', 'phas_B', 'move_B']].copy()\n", "matchB.columns = ['inter_no', 'phase_no', 'move_no']\n", "matchB['ring_type'] = 'B'\n", "match2 = pd.concat([matchA, matchB]).drop_duplicates()\n", "match2 = match2[['inter_no', 'phase_no', 'ring_type', 'move_no']]\n", "match2 = match2.sort_values(by=list(match2.columns))\n", "match2.head(10)" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_dir
01751A8
11751B4
21752A7
31752B3
41753A6
51753B1
61754A5
71754B2
81761A8
91761B4
101762A8
111762B3
121763A5
131763B18NaNNaN
141771A8
151771B4
161772A7
171772B3
181773A17NaNNaN
191773B18NaNNaN
201774A5
211774B1
221781A8
231781B4
241782A7
251782B3
261783A5
271783B2
281784A6
291784B1
302011A8
312011B3
322012A5
332012B2
342013A6
352013B2
362014A6
372014B1
382015A7
392015B4
402021A6
412021B2
422022A17NaNNaN
432022B18NaNNaN
442061A8
452061B4
462062A17NaNNaN
472062B18NaNNaN
482063A8
492063B4
502064A17NaNNaN
512064B18NaNNaN
522101A6
532101B18NaNNaN
542102A5
552102B2
562103A7
572103B4
582104A8
592104B3
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir\n", "0 175 1 A 8 남 북\n", "1 175 1 B 4 북 남\n", "2 175 2 A 7 북 동\n", "3 175 2 B 3 남 서\n", "4 175 3 A 6 동 서\n", "5 175 3 B 1 동 남\n", "6 175 4 A 5 서 북\n", "7 175 4 B 2 서 동\n", "8 176 1 A 8 남 북\n", "9 176 1 B 4 북 남\n", "10 176 2 A 8 남 북\n", "11 176 2 B 3 남 서\n", "12 176 3 A 5 서 북\n", "13 176 3 B 18 NaN NaN\n", "14 177 1 A 8 남 북\n", "15 177 1 B 4 북 남\n", "16 177 2 A 7 북 동\n", "17 177 2 B 3 남 서\n", "18 177 3 A 17 NaN NaN\n", "19 177 3 B 18 NaN NaN\n", "20 177 4 A 5 서 북\n", "21 177 4 B 1 동 남\n", "22 178 1 A 8 남 북\n", "23 178 1 B 4 북 남\n", "24 178 2 A 7 북 동\n", "25 178 2 B 3 남 서\n", "26 178 3 A 5 서 북\n", "27 178 3 B 2 서 동\n", "28 178 4 A 6 동 서\n", "29 178 4 B 1 동 남\n", "30 201 1 A 8 남 북\n", "31 201 1 B 3 남 서\n", "32 201 2 A 5 서 북\n", "33 201 2 B 2 서 동\n", "34 201 3 A 6 동 서\n", "35 201 3 B 2 서 동\n", "36 201 4 A 6 동 서\n", "37 201 4 B 1 동 남\n", "38 201 5 A 7 북 동\n", "39 201 5 B 4 북 남\n", "40 202 1 A 6 동 서\n", "41 202 1 B 2 서 동\n", "42 202 2 A 17 NaN NaN\n", "43 202 2 B 18 NaN NaN\n", "44 206 1 A 8 남 북\n", "45 206 1 B 4 북 남\n", "46 206 2 A 17 NaN NaN\n", "47 206 2 B 18 NaN NaN\n", "48 206 3 A 8 남 북\n", "49 206 3 B 4 북 남\n", "50 206 4 A 17 NaN NaN\n", "51 206 4 B 18 NaN NaN\n", "52 210 1 A 6 동 서\n", "53 210 1 B 18 NaN NaN\n", "54 210 2 A 5 서 북\n", "55 210 2 B 2 서 동\n", "56 210 3 A 7 북 동\n", "57 210 3 B 4 북 남\n", "58 210 4 A 8 남 북\n", "59 210 4 B 3 남 서" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# [nema 이동류목록] 불러오기 및 병합\n", "nema = pd.read_csv('../../Data/tables/nema.csv', encoding='cp949')\n", "match3 = pd.merge(match2, nema, how='left', on='move_no').drop_duplicates()\n", "match3" ] }, { "cell_type": "code", "execution_count": 5, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_angle
01751A8179004
11751B4003176
21752A7001095
31752B3179270
41753A6090270
51753B1090180
61754A5268000
71754B2270090
81761A8180000
91761B4359180
101762A8180000
111762B3180270
121763A5270356
131763B18NaNNaNNaNNaN
141771A8180000
151771B4001176
161772A7000090
171772B3179270
181773A17NaNNaNNaNNaN
191773B18NaNNaNNaNNaN
201774A5268000
211774B1090180
221781A8180000
231781B4000180
241782A7000090
251782B3180270
261783A5270000
271783B2270090
281784A6090270
291784B1090180
302011A8180000
312011B3180270
322012A5270000
332012B2270090
342013A6090270
352013B2270090
362014A6090270
372014B1090180
382015A7000090
392015B4000180
402021A6090270
412021B2270090
422022A17NaNNaNNaNNaN
432022B18NaNNaNNaNNaN
442061A8180000
452061B4000180
462062A17NaNNaNNaNNaN
472062B18NaNNaNNaNNaN
482063A8180000
492063B4000180
502064A17NaNNaNNaNNaN
512064B18NaNNaNNaNNaN
522101A6090270
532101B18NaNNaNNaNNaN
542102A5268000
552102B2270090
562103A7359090
572103B4000180
582104A8180000
592104B3180270
\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", "5 175 3 B 1 동 남 090 180\n", "6 175 4 A 5 서 북 268 000\n", "7 175 4 B 2 서 동 270 090\n", "8 176 1 A 8 남 북 180 000\n", "9 176 1 B 4 북 남 359 180\n", "10 176 2 A 8 남 북 180 000\n", "11 176 2 B 3 남 서 180 270\n", "12 176 3 A 5 서 북 270 356\n", "13 176 3 B 18 NaN NaN NaN NaN\n", "14 177 1 A 8 남 북 180 000\n", "15 177 1 B 4 북 남 001 176\n", "16 177 2 A 7 북 동 000 090\n", "17 177 2 B 3 남 서 179 270\n", "18 177 3 A 17 NaN NaN NaN NaN\n", "19 177 3 B 18 NaN NaN NaN NaN\n", "20 177 4 A 5 서 북 268 000\n", "21 177 4 B 1 동 남 090 180\n", "22 178 1 A 8 남 북 180 000\n", "23 178 1 B 4 북 남 000 180\n", "24 178 2 A 7 북 동 000 090\n", "25 178 2 B 3 남 서 180 270\n", "26 178 3 A 5 서 북 270 000\n", "27 178 3 B 2 서 동 270 090\n", "28 178 4 A 6 동 서 090 270\n", "29 178 4 B 1 동 남 090 180\n", "30 201 1 A 8 남 북 180 000\n", "31 201 1 B 3 남 서 180 270\n", "32 201 2 A 5 서 북 270 000\n", "33 201 2 B 2 서 동 270 090\n", "34 201 3 A 6 동 서 090 270\n", "35 201 3 B 2 서 동 270 090\n", "36 201 4 A 6 동 서 090 270\n", "37 201 4 B 1 동 남 090 180\n", "38 201 5 A 7 북 동 000 090\n", "39 201 5 B 4 북 남 000 180\n", "40 202 1 A 6 동 서 090 270\n", "41 202 1 B 2 서 동 270 090\n", "42 202 2 A 17 NaN NaN NaN NaN\n", "43 202 2 B 18 NaN NaN NaN NaN\n", "44 206 1 A 8 남 북 180 000\n", "45 206 1 B 4 북 남 000 180\n", "46 206 2 A 17 NaN NaN NaN NaN\n", "47 206 2 B 18 NaN NaN NaN NaN\n", "48 206 3 A 8 남 북 180 000\n", "49 206 3 B 4 북 남 000 180\n", "50 206 4 A 17 NaN NaN NaN NaN\n", "51 206 4 B 18 NaN NaN NaN NaN\n", "52 210 1 A 6 동 서 090 270\n", "53 210 1 B 18 NaN NaN NaN NaN\n", "54 210 2 A 5 서 북 268 000\n", "55 210 2 B 2 서 동 270 090\n", "56 210 3 A 7 북 동 359 090\n", "57 210 3 B 4 북 남 000 180\n", "58 210 4 A 8 남 북 180 000\n", "59 210 4 B 3 남 서 180 270" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# [방위각정보] 불러오기, 계층화, 병합\n", "# 불러오기\n", "dtype_dict = {f'angle_{alph}{j}':'str' for alph in ['A', 'B'] for j in range(1,9)}\n", "angle_original = pd.read_csv('../../Data/tables/angle.csv', index_col=0, dtype = dtype_dict)\n", "# 계층화\n", "angle = []\n", "for i, row in angle_original.iterrows():\n", " angle_codes = row[[f'angle_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " new = pd.DataFrame({'inter_no':[row.inter_no] * 16, 'phase_no':list(range(1, 9))*2, 'ring_type':['A'] * 8 + ['B'] * 8, 'angle_code':angle_codes.to_list()})\n", " angle.append(new)\n", "angle = pd.concat(angle)\n", "angle = angle.dropna().reset_index(drop=True)\n", "# 병합\n", "six_chars = angle.angle_code.apply(lambda x:len(x)==6)\n", "angle.loc[six_chars,'inc_angle'] = angle.angle_code.apply(lambda x:x[:3])\n", "angle.loc[six_chars,'out_angle'] = angle.angle_code.apply(lambda x:x[3:])\n", "angle = angle.drop('angle_code', axis=1)\n", "match4 = pd.merge(match3, angle, how='left', left_on=['inter_no', 'phase_no', 'ring_type'],\n", " right_on=['inter_no', 'phase_no', 'ring_type']).drop_duplicates()\n", "match4" ] }, { "cell_type": "code", "execution_count": 6, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
51753B1090180571545870_02571542797_02i0
61754A5268000571510153_02571500487_01i0
71754B2270090571510153_02571545870_01i0
81761A8180000-571542810_01-571542797_02.99i1
91761B4359180571542797_02.99571542810_01i1
101762A8180000-571542810_01-571542797_02.99i1
111762B3180270-571542810_01571543469_01i1
121763A5270356571543469_02-571542797_02.99i1
131763B18NaNNaNNaNNaNNaNNaNi1
141771A8180000-571542809_01571542811_01i2
151771B4001176571542811_02571542809_01i2
161772A7000090571542811_02571542107_01i2
171772B3179270-571542809_01571542809_01i2
181773A17NaNNaNNaNNaNNaNNaNi2
191773B18NaNNaNNaNNaNNaNNaNi2
201774A5268000-571542809_01571542811_01i2
211774B1090180571542107_02571542809_01i2
221781A8180000571540304_02571556450_01i3
231781B4000180571556450_02571540304_01i3
241782A7000090571556450_02571500475_01i3
251782B3180270571540304_02571540303_01i3
261783A5270000571540303_02.21571556450_01i3
271783B2270090571540303_02.21571500475_01i3
281784A6090270-571500475_01571540303_01i3
291784B1090180-571500475_01571540304_01i3
302011A8180000-571500569_01571500583_02i8
312011B3180270-571500569_01571500618_01i8
322012A5270000571500618_02571500583_02i8
332012B2270090571500618_02571500617_01i8
342013A6090270571500617_02571500618_01i8
352013B2270090571500618_02571500617_01i8
362014A6090270571500617_02571500618_01i8
372014B1090180571500617_02571500569_01i8
382015A7000090571500583_01571500617_01i8
392015B4000180571500583_01571500569_01i8
402021A6090270571510152_02-571510152_01i9
412021B2270090571510152_01571510152_01.65i9
422022A17NaNNaNNaNNaNNaNNaNi9
432022B18NaNNaNNaNNaNNaNNaNi9
442061A8180000-571511538_02571542073_02i7
452061B4000180571542073_01571511538_02i7
462062A17NaNNaNNaNNaNNaNNaNi7
472062B18NaNNaNNaNNaNNaNNaNi7
482063A8180000-571511538_02571542073_02i7
492063B4000180571542073_01571511538_02i7
502064A17NaNNaNNaNNaNNaNNaNi7
512064B18NaNNaNNaNNaNNaNNaNi7
522101A6090270-571542115_01571500535_01i6
532101B18NaNNaNNaNNaNNaNNaNi6
542102A5268000571500535_02.18571511538_01i6
552102B2270090571500535_02.18571542115_01i6
562103A7359090571511538_02.121571542115_01i6
572103B4000180571511538_02.121571500585_01i6
582104A8180000571500585_02571511538_01i6
592104B3180270571500585_02571500535_01i6
\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", "5 175 3 B 1 동 남 090 180 \n", "6 175 4 A 5 서 북 268 000 \n", "7 175 4 B 2 서 동 270 090 \n", "8 176 1 A 8 남 북 180 000 \n", "9 176 1 B 4 북 남 359 180 \n", "10 176 2 A 8 남 북 180 000 \n", "11 176 2 B 3 남 서 180 270 \n", "12 176 3 A 5 서 북 270 356 \n", "13 176 3 B 18 NaN NaN NaN NaN \n", "14 177 1 A 8 남 북 180 000 \n", "15 177 1 B 4 북 남 001 176 \n", "16 177 2 A 7 북 동 000 090 \n", "17 177 2 B 3 남 서 179 270 \n", "18 177 3 A 17 NaN NaN NaN NaN \n", "19 177 3 B 18 NaN NaN NaN NaN \n", "20 177 4 A 5 서 북 268 000 \n", "21 177 4 B 1 동 남 090 180 \n", "22 178 1 A 8 남 북 180 000 \n", "23 178 1 B 4 북 남 000 180 \n", "24 178 2 A 7 북 동 000 090 \n", "25 178 2 B 3 남 서 180 270 \n", "26 178 3 A 5 서 북 270 000 \n", "27 178 3 B 2 서 동 270 090 \n", "28 178 4 A 6 동 서 090 270 \n", "29 178 4 B 1 동 남 090 180 \n", "30 201 1 A 8 남 북 180 000 \n", "31 201 1 B 3 남 서 180 270 \n", "32 201 2 A 5 서 북 270 000 \n", "33 201 2 B 2 서 동 270 090 \n", "34 201 3 A 6 동 서 090 270 \n", "35 201 3 B 2 서 동 270 090 \n", "36 201 4 A 6 동 서 090 270 \n", "37 201 4 B 1 동 남 090 180 \n", "38 201 5 A 7 북 동 000 090 \n", "39 201 5 B 4 북 남 000 180 \n", "40 202 1 A 6 동 서 090 270 \n", "41 202 1 B 2 서 동 270 090 \n", "42 202 2 A 17 NaN NaN NaN NaN \n", "43 202 2 B 18 NaN NaN NaN NaN \n", "44 206 1 A 8 남 북 180 000 \n", "45 206 1 B 4 북 남 000 180 \n", "46 206 2 A 17 NaN NaN NaN NaN \n", "47 206 2 B 18 NaN NaN NaN NaN \n", "48 206 3 A 8 남 북 180 000 \n", "49 206 3 B 4 북 남 000 180 \n", "50 206 4 A 17 NaN NaN NaN NaN \n", "51 206 4 B 18 NaN NaN NaN NaN \n", "52 210 1 A 6 동 서 090 270 \n", "53 210 1 B 18 NaN NaN NaN NaN \n", "54 210 2 A 5 서 북 268 000 \n", "55 210 2 B 2 서 동 270 090 \n", "56 210 3 A 7 북 동 359 090 \n", "57 210 3 B 4 북 남 000 180 \n", "58 210 4 A 8 남 북 180 000 \n", "59 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", "5 571545870_02 571542797_02 i0 \n", "6 571510153_02 571500487_01 i0 \n", "7 571510153_02 571545870_01 i0 \n", "8 -571542810_01 -571542797_02.99 i1 \n", "9 571542797_02.99 571542810_01 i1 \n", "10 -571542810_01 -571542797_02.99 i1 \n", "11 -571542810_01 571543469_01 i1 \n", "12 571543469_02 -571542797_02.99 i1 \n", "13 NaN NaN i1 \n", "14 -571542809_01 571542811_01 i2 \n", "15 571542811_02 571542809_01 i2 \n", "16 571542811_02 571542107_01 i2 \n", "17 -571542809_01 571542809_01 i2 \n", "18 NaN NaN i2 \n", "19 NaN NaN i2 \n", "20 -571542809_01 571542811_01 i2 \n", "21 571542107_02 571542809_01 i2 \n", "22 571540304_02 571556450_01 i3 \n", "23 571556450_02 571540304_01 i3 \n", "24 571556450_02 571500475_01 i3 \n", "25 571540304_02 571540303_01 i3 \n", "26 571540303_02.21 571556450_01 i3 \n", "27 571540303_02.21 571500475_01 i3 \n", "28 -571500475_01 571540303_01 i3 \n", "29 -571500475_01 571540304_01 i3 \n", "30 -571500569_01 571500583_02 i8 \n", "31 -571500569_01 571500618_01 i8 \n", "32 571500618_02 571500583_02 i8 \n", "33 571500618_02 571500617_01 i8 \n", "34 571500617_02 571500618_01 i8 \n", "35 571500618_02 571500617_01 i8 \n", "36 571500617_02 571500618_01 i8 \n", "37 571500617_02 571500569_01 i8 \n", "38 571500583_01 571500617_01 i8 \n", "39 571500583_01 571500569_01 i8 \n", "40 571510152_02 -571510152_01 i9 \n", "41 571510152_01 571510152_01.65 i9 \n", "42 NaN NaN i9 \n", "43 NaN NaN i9 \n", "44 -571511538_02 571542073_02 i7 \n", "45 571542073_01 571511538_02 i7 \n", "46 NaN NaN i7 \n", "47 NaN NaN i7 \n", "48 -571511538_02 571542073_02 i7 \n", "49 571542073_01 571511538_02 i7 \n", "50 NaN NaN i7 \n", "51 NaN NaN i7 \n", "52 -571542115_01 571500535_01 i6 \n", "53 NaN NaN i6 \n", "54 571500535_02.18 571511538_01 i6 \n", "55 571500535_02.18 571542115_01 i6 \n", "56 571511538_02.121 571542115_01 i6 \n", "57 571511538_02.121 571500585_01 i6 \n", "58 571500585_02 571511538_01 i6 \n", "59 571500585_02 571500535_01 i6 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# [네트워크], [교차로-노드 매칭], [교차로정보] 불러오기\n", "net = sumolib.net.readNet('../../Data/networks/SN_sample.net.xml')\n", "inter_node = pd.read_csv('../../Data/tables/inter_node.csv', index_col=0)\n", "inter_info = pd.read_csv('../../Data/tables/inter_info.csv', index_col=0)\n", "\n", "inter_node1 = inter_node[inter_node.inter_type == 'parent'].drop('inter_type', axis=1)\n", "inter_info1 = inter_info[['inter_no', 'inter_lat', 'inter_lon']]\n", "inter = pd.merge(inter_node1, inter_info1, how='left', left_on=['inter_no'],\n", " right_on=['inter_no']).drop_duplicates()\n", "\n", "inter2node = dict(zip(inter['inter_no'], inter['node_id']))\n", "\n", "match5 = match4.copy()\n", "# 진입진출ID 매칭\n", "for index, row in match5.iterrows():\n", " node_id = inter2node[row.inter_no]\n", " node = net.getNode(node_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) directions\n", " inc_dirs = []\n", " for inc_edge in inc_edges:\n", " start = inc_edge.getShape()[-2]\n", " end = inc_edge.getShape()[-1]\n", " inc_dir = np.array(end) - np.array(start)\n", " inc_dir = inc_dir / (inc_dir ** 2).sum() ** 0.5\n", " inc_dirs.append(inc_dir)\n", " out_dirs = []\n", " for out_edge in out_edges:\n", " start = out_edge.getShape()[0]\n", " end = out_edge.getShape()[1]\n", " out_dir = np.array(end) - np.array(start)\n", " out_dir = out_dir / (out_dir ** 2).sum() ** 0.5\n", " out_dirs.append(out_dir)\n", " # 진입각, 진출각 불러오기\n", " if not pd.isna(row.inc_angle):\n", " inc_angle = int(row.inc_angle)\n", " out_angle = int(row.out_angle)\n", " # 방위각을 일반각으로 가공, 라디안 변환, 단위벡터로 변환\n", " inc_angle = (-90 - inc_angle) % 360\n", " inc_angle = inc_angle * np.pi / 180.\n", " inc_dir_true = np.array([np.cos(inc_angle), np.sin(inc_angle)])\n", " out_angle = (90 - out_angle) % 360\n", " out_angle = out_angle * np.pi / 180.\n", " out_dir_true = np.array([np.cos(out_angle), np.sin(out_angle)])\n", " # 매칭 엣지 반환\n", " inc_index = np.array([np.dot(inc_dir, inc_dir_true) for inc_dir in inc_dirs]).argmax()\n", " out_index = np.array([np.dot(out_dir, out_dir_true) for out_dir in out_dirs]).argmax()\n", " inc_edge_id = inc_edges[inc_index].getID()\n", " out_edge_id = out_edges[out_index].getID()\n", " match5.at[index, 'inc_edge'] = inc_edge_id\n", " match5.at[index, 'out_edge'] = out_edge_id\n", "match5['node_id'] = match5['inter_no'].map(inter2node)\n", "match5 = match5.sort_values(by=['inter_no','phase_no','ring_type']).reset_index(drop=True)\n", "match5" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['u00', 'u20', 'u30', 'u31', 'u32', 'u60'], dtype=object)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 유턴/연동교차로에 대하여 진입ID, 진출ID 부여\n", "node2inter = dict(zip(inter_node['node_id'], inter_node['inter_no']))\n", "\n", "uturn = pd.read_csv('../../Data/tables/child_uturn.csv')\n", "coord = pd.read_csv('../../Data/tables/child_coord.csv')\n", "\n", "child_ids = inter_node[inter_node.inter_type=='child'].node_id.unique()\n", "ch2pa = {} # child to parent\n", "for child_id in child_ids:\n", " parent_no = inter_node[inter_node.node_id==child_id].inter_no.iloc[0]\n", " sub_inter_node = inter_node[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", "u_ids = uturn.child_id.unique()\n", "u_ids" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "u00 i0 좌회전시 북\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
01751A8179004NaNNaNu00
11751B4003176NaNNaNu00
21752A19001095571500487_02571500487_01.32u00
31752B3179270NaNNaNu00
41753A6090270NaNNaNu00
51753B1090180NaNNaNu00
61754A5268000NaNNaNu00
71754B2270090NaNNaNu00
\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 19 북 동 001 095 \n", "3 175 2 B 3 남 서 179 270 \n", "4 175 3 A 6 동 서 090 270 \n", "5 175 3 B 1 동 남 090 180 \n", "6 175 4 A 5 서 북 268 000 \n", "7 175 4 B 2 서 동 270 090 \n", "\n", " inc_edge out_edge node_id \n", "0 NaN NaN u00 \n", "1 NaN NaN u00 \n", "2 571500487_02 571500487_01.32 u00 \n", "3 NaN NaN u00 \n", "4 NaN NaN u00 \n", "5 NaN NaN u00 \n", "6 NaN NaN u00 \n", "7 NaN NaN u00 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "u20 i2 보행신호시 북\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
01771A8180000NaNNaNu20
11771B4001176NaNNaNu20
21772A7000090NaNNaNu20
31772B3179270NaNNaNu20
41773A17NaNNaNNaNNaN571542810_01.51571542810_02u20
51773B18NaNNaNNaNNaNNaNNaNu20
61774A5268000NaNNaNu20
71774B1090180NaNNaNu20
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle out_angle \\\n", "0 177 1 A 8 남 북 180 000 \n", "1 177 1 B 4 북 남 001 176 \n", "2 177 2 A 7 북 동 000 090 \n", "3 177 2 B 3 남 서 179 270 \n", "4 177 3 A 17 NaN NaN NaN NaN \n", "5 177 3 B 18 NaN NaN NaN NaN \n", "6 177 4 A 5 서 북 268 000 \n", "7 177 4 B 1 동 남 090 180 \n", "\n", " inc_edge out_edge node_id \n", "0 NaN NaN u20 \n", "1 NaN NaN u20 \n", "2 NaN NaN u20 \n", "3 NaN NaN u20 \n", "4 571542810_01.51 571542810_02 u20 \n", "5 NaN NaN u20 \n", "6 NaN NaN u20 \n", "7 NaN NaN u20 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "u30 i3 보행신호시 북\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
01781A8180000NaNNaNu30
11781B4000180NaNNaNu30
21782A7000090NaNNaNu30
31782B3180270NaNNaNu30
41783A5270000NaNNaNu30
51783B2270090NaNNaNu30
61784A19090270571556452_01571556452_02u30
71784B1090180NaNNaNu30
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle out_angle \\\n", "0 178 1 A 8 남 북 180 000 \n", "1 178 1 B 4 북 남 000 180 \n", "2 178 2 A 7 북 동 000 090 \n", "3 178 2 B 3 남 서 180 270 \n", "4 178 3 A 5 서 북 270 000 \n", "5 178 3 B 2 서 동 270 090 \n", "6 178 4 A 19 동 서 090 270 \n", "7 178 4 B 1 동 남 090 180 \n", "\n", " inc_edge out_edge node_id \n", "0 NaN NaN u30 \n", "1 NaN NaN u30 \n", "2 NaN NaN u30 \n", "3 NaN NaN u30 \n", "4 NaN NaN u30 \n", "5 NaN NaN u30 \n", "6 571556452_01 571556452_02 u30 \n", "7 NaN NaN u30 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "u31 i3 보행신호시 동\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
01781A19180000571500475_02571500475_01.26u31
11781B4000180NaNNaNu31
21782A7000090NaNNaNu31
31782B3180270NaNNaNu31
41783A5270000NaNNaNu31
51783B2270090NaNNaNu31
61784A6090270NaNNaNu31
71784B1090180NaNNaNu31
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle out_angle \\\n", "0 178 1 A 19 남 북 180 000 \n", "1 178 1 B 4 북 남 000 180 \n", "2 178 2 A 7 북 동 000 090 \n", "3 178 2 B 3 남 서 180 270 \n", "4 178 3 A 5 서 북 270 000 \n", "5 178 3 B 2 서 동 270 090 \n", "6 178 4 A 6 동 서 090 270 \n", "7 178 4 B 1 동 남 090 180 \n", "\n", " inc_edge out_edge node_id \n", "0 571500475_02 571500475_01.26 u31 \n", "1 NaN NaN u31 \n", "2 NaN NaN u31 \n", "3 NaN NaN u31 \n", "4 NaN NaN u31 \n", "5 NaN NaN u31 \n", "6 NaN NaN u31 \n", "7 NaN NaN u31 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "u32 i3 보행신호시 서\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
01781A8180000NaNNaNu32
11781B19000180571540303_02-571540303_02u32
21782A7000090NaNNaNu32
31782B3180270NaNNaNu32
41783A5270000NaNNaNu32
51783B2270090NaNNaNu32
61784A6090270NaNNaNu32
71784B1090180NaNNaNu32
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle out_angle \\\n", "0 178 1 A 8 남 북 180 000 \n", "1 178 1 B 19 북 남 000 180 \n", "2 178 2 A 7 북 동 000 090 \n", "3 178 2 B 3 남 서 180 270 \n", "4 178 3 A 5 서 북 270 000 \n", "5 178 3 B 2 서 동 270 090 \n", "6 178 4 A 6 동 서 090 270 \n", "7 178 4 B 1 동 남 090 180 \n", "\n", " inc_edge out_edge node_id \n", "0 NaN NaN u32 \n", "1 571540303_02 -571540303_02 u32 \n", "2 NaN NaN u32 \n", "3 NaN NaN u32 \n", "4 NaN NaN u32 \n", "5 NaN NaN u32 \n", "6 NaN NaN u32 \n", "7 NaN NaN u32 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "u60 i6 직좌시 서\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
02101A6090270NaNNaNu60
12101B18NaNNaNNaNNaNNaNNaNu60
22102A19268000571500535_02-571500535_02u60
32102B19270090571500535_02-571500535_02u60
42103A7359090NaNNaNu60
52103B4000180NaNNaNu60
62104A8180000NaNNaNu60
72104B3180270NaNNaNu60
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle out_angle \\\n", "0 210 1 A 6 동 서 090 270 \n", "1 210 1 B 18 NaN NaN NaN NaN \n", "2 210 2 A 19 서 북 268 000 \n", "3 210 2 B 19 서 동 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 NaN NaN u60 \n", "1 NaN NaN u60 \n", "2 571500535_02 -571500535_02 u60 \n", "3 571500535_02 -571500535_02 u60 \n", "4 NaN NaN u60 \n", "5 NaN NaN u60 \n", "6 NaN NaN u60 \n", "7 NaN NaN u60 " ] }, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
inter_nophase_noring_typemove_noinc_dirout_dirinc_angleout_angleinc_edgeout_edgenode_id
01781A20NaNNaNNaNNaNNaNNaNc30
11781B20NaNNaNNaNNaNNaNNaNc30
21782A20NaNNaNNaNNaN571542116_01-571542116_02.96c30
31782B20NaNNaNNaNNaN571542116_02.96571542116_02.164c30
41783A20NaNNaNNaNNaN571542116_01-571542116_02.96c30
51783B20NaNNaNNaNNaN571542116_02.96571542116_02.164c30
61784A20NaNNaNNaNNaN571542116_01-571542116_02.96c30
71784B20NaNNaNNaNNaN571542116_02.96571542116_02.164c30
\n", "
" ], "text/plain": [ " inter_no phase_no ring_type move_no inc_dir out_dir inc_angle \\\n", "0 178 1 A 20 NaN NaN NaN \n", "1 178 1 B 20 NaN NaN NaN \n", "2 178 2 A 20 NaN NaN NaN \n", "3 178 2 B 20 NaN NaN NaN \n", "4 178 3 A 20 NaN NaN NaN \n", "5 178 3 B 20 NaN NaN NaN \n", "6 178 4 A 20 NaN NaN NaN \n", "7 178 4 B 20 NaN NaN NaN \n", "\n", " out_angle inc_edge out_edge node_id \n", "0 NaN NaN NaN c30 \n", "1 NaN NaN NaN c30 \n", "2 NaN 571542116_01 -571542116_02.96 c30 \n", "3 NaN 571542116_02.96 571542116_02.164 c30 \n", "4 NaN 571542116_01 -571542116_02.96 c30 \n", "5 NaN 571542116_02.96 571542116_02.164 c30 \n", "6 NaN 571542116_01 -571542116_02.96 c30 \n", "7 NaN 571542116_02.96 571542116_02.164 c30 " ] }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
....................................
32102B19270090571500535_02-571500535_02u60
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 19 서 동 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 571500535_02 -571500535_02 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]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 유턴/연동교차로에 대하여 진입ID, 진출ID 부여\n", "node2inter = dict(zip(inter_node['node_id'], inter_node['inter_no']))\n", "\n", "uturn = pd.read_csv('../../Data/tables/child_uturn.csv')\n", "coord = pd.read_csv('../../Data/tables/child_coord.csv')\n", "child_ids = inter_node[inter_node.inter_type=='child'].node_id.unique()\n", "ch2pa = {} # child to parent\n", "for child_id in child_ids:\n", " parent_no = inter_node[inter_node.node_id==child_id].inter_no.iloc[0]\n", " sub_inter_node = inter_node[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", "u_ids = uturn.child_id.unique()\n", "# 각 child uturn node에 대하여 (inc_edge_id, out_edge_id) 부여\n", "cmatches = []\n", "for _, row in 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", " ind = directions.index(direction)\n", " if condition == \"좌회전시\":\n", " print(child_id, parent_id, condition, direction)\n", " inc_dire = direction\n", " out_dire_A = out_dire_B = directions[(ind + 2) % len(directions)]\n", " elif condition == \"직진시\":\n", " print(child_id, parent_id, condition, direction)\n", " inc_dire = direction\n", " out_dire_A = out_dire_B = directions[(ind + 4) % len(directions)]\n", " elif condition == \"직좌시\":\n", " print(child_id, parent_id, condition, direction)\n", " inc_dire = direction\n", " out_dire_A = directions[(ind + 2) % len(directions)]\n", " out_dire_B = directions[(ind + 4) % len(directions)]\n", " elif condition == \"보행신호시\":\n", " print(child_id, parent_id, condition, direction)\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", " cmatch = match5.copy()[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", " if condition == '직좌시':\n", " ap = cmatch[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A)].phase_no.iloc[0]\n", " bp = cmatch[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_B)].phase_no.iloc[0]\n", " # 직진과 좌회전이 같은 현시에 있는 경우에만 (inc_edge_id, out_edge_id)를 부여한다.\n", " if ap == bp:\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", " elif condition == '보행신호시':\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", " # 이동류번호가 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", " else:\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", " # 유턴신호의 이동류번호를 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", " display(cmatch)\n", " cmatches.append(cmatch)\n", "\n", "# 각 child coordination node에 대하여 (inc_edge_id, out_edge_id) 부여\n", "coord['inter_no'] = coord['parent_id'].map(node2inter)\n", "coord = coord.rename(columns={'child_id':'node_id'})\n", "coord[['inc_dir', 'out_dir', 'inc_angle','out_angle']] = np.nan\n", "coord['move_no'] = 20\n", "coord = coord[['inter_no', 'phase_no', 'ring_type', 'move_no', 'inc_dir', 'out_dir', 'inc_angle','out_angle', 'inc_edge', 'out_edge', 'node_id']]\n", "# display(coord)\n", "cmatches = pd.concat(cmatches)\n", "display(coord)\n", "match6 = pd.concat([match5, cmatches, coord]).drop_duplicates().sort_values(by=['inter_no', 'node_id', 'phase_no', 'ring_type'])\n", "# with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n", "match6.to_csv('../../Data/tables/matching/match6.csv')\n", "display(match6)" ] }, { "cell_type": "code", "execution_count": 9, "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", "
inter_nomove_noinc_dirout_dirinc_edgeout_edgenode_id
01751571545870_02571542797_02i0
11752571510153_02571545870_01i0
21753-571542797_02571510153_01i0
31754-571500487_01571542797_02i0
41755571510153_02571500487_01i0
........................
692108571500585_02571511538_01i6
7021021571511538_02.121571500535_01i6
7121021571500535_02.18571500585_01i6
7221021571500585_02571542115_01i6
7321021-571542115_01571511538_01i6
\n", "

74 rows × 7 columns

\n", "
" ], "text/plain": [ " inter_no move_no inc_dir out_dir inc_edge out_edge 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\n", ".. ... ... ... ... ... ... ...\n", "69 210 8 남 북 571500585_02 571511538_01 i6\n", "70 210 21 북 서 571511538_02.121 571500535_01 i6\n", "71 210 21 서 남 571500535_02.18 571500585_01 i6\n", "72 210 21 남 동 571500585_02 571542115_01 i6\n", "73 210 21 동 북 -571542115_01 571511538_01 i6\n", "\n", "[74 rows x 7 columns]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 이동류 매칭\n", "# 각 교차로에 대하여, 가능한 모든 이동류(1~18, 21)에 대한 진입·진출엣지ID를 지정한다.\n", "# 모든 이동류에 대해 지정하므로, 시차제시 이전과 다른 이동류가 등장하더라도 항상 진입·진출 엣지 ID를 지정할 수 있다.\n", "match7 = match6.copy()\n", "match7 = match7[['inter_no', 'move_no', 'inc_dir', 'out_dir', 'inc_edge', 'out_edge', 'node_id']]\n", "\n", "parent_ids = sorted(inter_node[inter_node.inter_type=='parent'].node_id.unique())\n", "child_ids = sorted(inter_node[inter_node.inter_type=='child'].node_id.unique())\n", "\n", "# (1) 가능한 (진입방향, 진출방향) 목록\n", "flows = nema.dropna().apply(lambda row: (row['inc_dir'], row['out_dir']), axis=1).tolist()\n", "# (2) 각 교차로별 방향 목록 : pdires (possible directions)\n", "pdires = {}\n", "for node_id in parent_ids:\n", " dires = match7[match7.node_id == node_id][['inc_dir','out_dir']].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 parent_ids:\n", " for inc_dir in pdires[node_id]:\n", " df = match7[(match7.node_id==node_id) & (match7.inc_dir==inc_dir)]\n", " inc2id[(node_id, inc_dir)] = df.inc_edge.iloc[0]\n", "# (4) 각 (교차로, 진출방향) 별 진출id 목록 : out2id (outgoing direction to outgoing edge_id)\n", "out2id = {}\n", "for node_id in parent_ids:\n", " for out_dir in pdires[node_id]:\n", " df = match7[(match7.node_id==node_id) & (match7.out_dir==out_dir)]\n", " out2id[(node_id, out_dir)] = df.out_edge.iloc[0]\n", "# (5) 각 교차로별 가능한 (진입방향, 진출방향) 목록 : pflow (possible flows)\n", "pflow = {}\n", "for node_id in parent_ids:\n", " pflow[node_id] = [flow for flow in flows if set(flow).issubset(pdires[node_id])]\n", "# (6) 가능한 이동류에 대하여 진입id, 진출id 배정 : matching\n", "node2inter = dict(zip(match7['node_id'], match7['inter_no']))\n", "dires_right = ['북', '서', '남', '동', '북'] # ex (북, 서), (서, 남) 등은 우회전 flow\n", "matching = []\n", "for node_id in parent_ids:\n", " inter_no = node2inter[node_id]\n", " # 좌회전과 직진(1 ~ 16)\n", " for (inc_dir, out_dir) in pflow[node_id]:\n", " move_no = nema[(nema.inc_dir==inc_dir) & (nema.out_dir==out_dir)].move_no.iloc[0]\n", " inc_edge = inc2id[(node_id, inc_dir)]\n", " out_edge = out2id[(node_id, out_dir)]\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'move_no':[move_no],\n", " 'inc_dir':[inc_dir], 'out_dir':[out_dir],\n", " 'inc_edge':[inc_edge], 'out_edge':[out_edge], 'node_id':[node_id]})\n", " matching.append(new_row)\n", " # 보행신호(17), 전적색(18)\n", " new_row = pd.DataFrame({'inter_no':[inter_no] * 2, 'move_no':[17, 18],\n", " 'inc_dir':[None]*2, 'out_dir':[None]*2,\n", " 'inc_edge':[None]*2, 'out_edge':[None]*2, 'node_id':[node_id]*2})\n", " matching.append(new_row)\n", " # 신호우회전(21)\n", " for d in range(len(dires_right)-1):\n", " inc_dir = dires_right[d]\n", " out_dir = dires_right[d+1]\n", " if {inc_dir, out_dir}.issubset(pdires[node_id]):\n", " inc_edge = inc2id[(node_id, inc_dir)]\n", " out_edge = out2id[(node_id, out_dir)]\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'move_no':[21],\n", " 'inc_dir':[inc_dir], 'out_dir':[out_dir],\n", " 'inc_edge':[inc_edge], 'out_edge':[out_edge], 'node_id':[node_id]})\n", " matching.append(new_row)\n", "matching.append(match7[match7.node_id.isin(child_ids)])\n", "matching = pd.concat(matching)\n", "matching = matching.dropna().sort_values(by=['inter_no', 'node_id', 'move_no']).reset_index(drop=True)\n", "matching['move_no'] = matching['move_no'].astype(int)\n", "matching.to_csv('../../Data/tables/matching/matching.csv')\n", "display(matching)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# B. 5초 간격으로 이동류번호 수집" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 8/17280 [00:00<04:13, 68.11it/s]" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 17280/17280 [04:19<00:00, 66.61it/s]\n" ] } ], "source": [ "# 5초 단위로 이동류번호 저장 및 신호이력에서 유닉스시각 가져와서 표시, 한시간동안의 데이터만 보관\n", "midnight = int(datetime(2024, 1, 5, 0, 0, 0).timestamp())\n", "next_day = int(datetime(2024, 1, 6, 0, 0, 0).timestamp())\n", "fsecs = range(midnight, next_day, 5) # fsecs : unix time by Five SECondS\n", "fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", "# time2move = dict(zip(fsecs,moves)) # move : 어느 순간의 이동류정보\n", "history = pd.read_csv('../../Data/tables/history.csv', index_col=0)\n", "\n", "time2movement = {} # movement : 어느 순간의, 그 순간으로부터 한시간 동안의 (교차로번호 + 현시별이동류번호 + 시작시간)\n", "# - 아래 절차를 5초마다 반복\n", "for fsec in tqdm(fsecs): # fsec : unix time by Five SECond\n", " # 1. 상태 테이블 조회해서 전체 데이터중 필요데이터(교차로번호, A링 현시번호, A링 이동류번호, B링 현시번호, B링 이동류번호)만 수집 : A\n", " # move = time2move[fsec]\n", " move = pd.read_csv(f'../../Data/tables/moves/move_{fsec}.csv', index_col=0)\n", " # 2. 이력 테이블 조회해서 교차로별로 유닉스시간 최대인 데이터(교차로변호, 종료유닉스타임)만 수집 : B\n", " recent_histories = [group.iloc[-1:] for _, group in history[history['end_unix'] < fsec].groupby('inter_no')] # 교차로별로 유닉스시간이 최대인 행들\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", " move = move.drop_duplicates()\n", " # 4. C데이터 프레임에 신규 컬럼(시작 유닉스타임) 생성 후 종료유닉스 타임 값 입력, 종료 유닉스 타임 컬럼 제거\n", " move = move.rename(columns = {'end_unix':'start_unix'})\n", " # 5. 이동류 이력정보 READ\n", " # - CSV 파일로 서버에 저장된 이동류정보를 읽어옴(파일이 없는 경우에는 데이터가 없는 프레임 D 생성)\n", " try:\n", " if isinstance(movement, pd.DataFrame): # movement가 존재할 경우 그걸 그대로 씀.\n", " pass\n", " else: \n", " movement = pd.DataFrame()\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데이터 보관 시간 기준시간을 시작 유닉스 타임의 최대값 - 3600을 값으로 산출하고, 보관 시간 기준시간보다 작은 시작 유닉스 타임을 가진 행은 모두 제거(1시간 데이터만 보관)\n", " movement = movement[movement.start_unix > fsec - 3600]\n", " movement = movement.sort_values(by=['start_unix','inter_no','phas_A','phas_B']).reset_index(drop=True)\n", "\n", " time2movement[fsec] = movement\n", " movement.to_csv(f'../../Data/tables/movements/movements_{fsec}.csv')\n", "\n", "# 각 movement들의 길이 시각화\n", "import matplotlib.pyplot as plt\n", "plt.plot(fsecs, [len(time2movement[fsec]) for fsec in fsecs])\n", "plt.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# C. 5분 간격으로 신호이력 수집 및 통합테이블 생성" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "plan = pd.read_csv('../../Data/tables/plan.csv', index_col=0)\n", "history = pd.read_csv('../../Data/tables/history.csv', index_col=0)" ] }, { "cell_type": "code", "execution_count": 12, "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", "
start_hourstart_minutestart_seconds
0001704380400
1701704405600
2901704412800
318301704447000
\n", "
" ], "text/plain": [ " start_hour start_minute start_seconds\n", "0 0 0 1704380400\n", "1 7 0 1704405600\n", "2 9 0 1704412800\n", "3 18 30 1704447000" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# split, isplit : A,B 분리 혹은 통합시 사용될 수 있는 딕셔너리\n", "splits = {} # splits maps (inter_no, start_hour, start_minute) to split\n", "for i, row in 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", " 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", " splits[(inter_no, start_hour, start_minute)][(phas_A, phas_B)] = k\n", "\n", "isplits = {} # the inverse of splits\n", "for i in splits:\n", " isplits[i] = {splits[i][k]:k for k in splits[i]} # isplit maps k to (phas_A, phas_B)\n", "\n", "# timetable\n", "timetable = plan[['start_hour', 'start_minute']].drop_duplicates()\n", "timetable['start_seconds'] = midnight + timetable['start_hour'] * 3600 + timetable['start_minute'] * 60\n", "timetable" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-01-05 16:40:00\n" ] } ], "source": [ "m = 200\n", "fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", "present_time = fmins[m] # 현재시점\n", "print(datetime.fromtimestamp(present_time))\n", "\n", "# 1. 조회시점의 유닉스 타임 이전의 신호이력 수집\n", "rhistory = history.copy() # recent history\n", "rhistory = rhistory[(rhistory.end_unix < present_time)]\n", "# 2. 시작 유닉스 타임컬럼 생성 후 종류 유닉스 타임에서 현시별 현시기간 컬럼의 합을 뺀 값으로 입력\n", "# - 현시시간의 합을 뺀 시간의 +- 10초 이내에 이전 주기정보가 존재하면 그 유닉스 시간을 시작 유닉스시간 값으로 하고, 존재하지 않으면 현시시간의 합을 뺀 유닉스 시간을 시작 유닉스 시간으로 지정\n", "for i, row in rhistory.iterrows():\n", " inter_no = row.inter_no\n", " end_unix = row.end_unix\n", " elapsed_time = row[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]].sum() // 2 # 현시시간 합\n", " # 이전 유닉스 존재하지 않음 : 현시시간 합의 차\n", " start_unix = end_unix - elapsed_time\n", " pre_rows = history[:i] # previous rows\n", " if inter_no in pre_rows.inter_no.unique(): # 이전 유닉스 존재\n", " pre_unix = pre_rows[pre_rows.inter_no == inter_no]['end_unix'].iloc[-1] # previous unix time\n", " # 이전 유닉스 존재, abs < 10 : 이전 유닉스\n", " if abs(pre_unix - start_unix) < 10:\n", " start_unix = pre_unix\n", " # 이전 유닉스 존재, abs >=10 : 현시시간 합의 차\n", " else:\n", " pass\n", " rhistory.loc[i, 'start_unix'] = start_unix \n", "rhistory[rhistory.isna()] = 0\n", "rhistory['start_unix'] = rhistory['start_unix'].astype(int)\n", "rhistory[['inter_no', 'start_unix', 'cycle']][rhistory.inter_no==175]\n", "rhistory = rhistory[['inter_no', 'start_unix'] + [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)] + ['cycle']]\n", "inter_no = 201" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def calculate_DS(rhist, curr_unix):\n", " ghour_lt_curr_unix = hours[hours <= curr_unix].max() # the greatest hour less than (or equal to) curr_unix\n", " start_unixes = rhist.start_unix.unique()\n", " start_unixes_lt_ghour = np.sort(start_unixes[start_unixes < ghour_lt_curr_unix]) # start unixes less than ghour_lt_curr_unix\n", " # 기준유닉스(base_unix) : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 start_unix\n", " if list(start_unixes_lt_ghour):\n", " base_unix = start_unixes_lt_ghour[-5]\n", " # start_unixes_lt_ghour가 비었을 경우에는 맨 앞 start_unix로 base_unix를 지정\n", " else:\n", " base_unix = rhist.start_unix.min()\n", " D_n = curr_unix - base_unix\n", " S_n_durs = rhist[(rhist.start_unix > base_unix) & (rhist.start_unix <= curr_unix)] \\\n", " [[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = S_n_durs.values.sum() // 2\n", " return D_n, S_n" ] }, { "cell_type": "code", "execution_count": 15, "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", "
inter_nostart_unixcycleD_nS_ndiff
65202170438950014025202520140.0
66202170438964014026602660140.0
67202170438978014028002800140.0
68202170438992014029402940140.0
69202170439038914034093080469.0
70202170439052914035493220140.0
71202170439067014036903360141.0
72202170439081014038303500140.0
73202170439095014039703640140.0
74202170439109014041103780140.0
752021704391230140701700140.0
762021704391370140841840140.0
772021704391509140980980139.0
78202170439165014011211120141.0
79202170439179014012611260140.0
80202170439193014014011400140.0
81202170439207014015411540140.0
82202170439221114016821680141.0
83202170439235014018211820139.0
84202170439249014019611960140.0
\n", "
" ], "text/plain": [ " inter_no start_unix cycle D_n S_n diff\n", "65 202 1704389500 140 2520 2520 140.0\n", "66 202 1704389640 140 2660 2660 140.0\n", "67 202 1704389780 140 2800 2800 140.0\n", "68 202 1704389920 140 2940 2940 140.0\n", "69 202 1704390389 140 3409 3080 469.0\n", "70 202 1704390529 140 3549 3220 140.0\n", "71 202 1704390670 140 3690 3360 141.0\n", "72 202 1704390810 140 3830 3500 140.0\n", "73 202 1704390950 140 3970 3640 140.0\n", "74 202 1704391090 140 4110 3780 140.0\n", "75 202 1704391230 140 701 700 140.0\n", "76 202 1704391370 140 841 840 140.0\n", "77 202 1704391509 140 980 980 139.0\n", "78 202 1704391650 140 1121 1120 141.0\n", "79 202 1704391790 140 1261 1260 140.0\n", "80 202 1704391930 140 1401 1400 140.0\n", "81 202 1704392070 140 1541 1540 140.0\n", "82 202 1704392211 140 1682 1680 141.0\n", "83 202 1704392350 140 1821 1820 139.0\n", "84 202 1704392490 140 1961 1960 140.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inter_no = 202\n", "# 2-1. 참값 판단 프로세스\n", "hours = np.array(range(midnight, next_day + 1, 3600)) # 정각에 해당하는 시각들 목록\n", "\n", "rhist = rhistory.copy()[rhistory.inter_no==inter_no]\n", "rhist = rhist.drop_duplicates(subset=['start_unix']).reset_index(drop=True)\n", "\n", "# D_n 및 S_n 값 정의\n", "rhist['D_n'] = 0 # D_n : 시간차이\n", "rhist['S_n'] = 0 # S_n : 현시시간합\n", "for n in range(len(rhist)):\n", " curr_unix = rhist.iloc[n].start_unix # current start_unix\n", " rhist.loc[n, ['D_n', 'S_n']] = calculate_DS(rhist, curr_unix)\n", "\n", "# 결측 및 이상치 처리\n", "prev_unix = rhist.loc[0, 'start_unix'] # previous start_unix\n", "curr_unix = rhist.loc[1, 'start_unix'] # current start_unix\n", "fina_unix = rhist.start_unix.max() # final start_unix\n", "rhist_diff = rhist.copy()\n", "rhist_diff['diff'] = rhist['start_unix'].diff()\n", "rhist_diff[list(rhist_diff.columns)[:2]+list(rhist_diff.columns)[-4:]][65:85]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([175, 176, 177, 178, 201, 202, 206, 210], dtype=int64)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plan.inter_no.unique()" ] }, { "cell_type": "code", "execution_count": 19, "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", "
abc
0111111
1222222
2333333
\n", "
" ], "text/plain": [ " a b c\n", "0 1 11 111\n", "1 2 22 222\n", "2 3 33 333" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "22" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({'a':[1,2,3],'b':[11,22,33], 'c':[111,222,333]})\n", "display(df)\n", "# curr_unix = rhist.loc[n, 'start_unix']\n", "df.loc[1,'b']\n" ] }, { "cell_type": "code", "execution_count": 91, "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", "
inter_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5cycleD_nS_ndiffstart_dt
24176170438405437734000150804750204.02024-01-05 01:00:54
8117617043925783773400015020782100128.02024-01-05 03:22:58
143176170440200437734000150874750274.02024-01-05 06:00:04
1441761704402240377340001501110900236.02024-01-05 06:04:00
167176170440569037934000170750770150.02024-01-05 07:01:30
1711761704406231379340001701291145031.02024-01-05 07:10:31
2101761704412860371034000180851860169.02024-01-05 09:01:00
25317617044204423710340001801283144022.02024-01-05 11:07:22
27717617044246003710340001801820198020.02024-01-05 12:16:40
\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 cycle \\\n", "24 176 1704384054 37 73 40 0 0 150 \n", "81 176 1704392578 37 73 40 0 0 150 \n", "143 176 1704402004 37 73 40 0 0 150 \n", "144 176 1704402240 37 73 40 0 0 150 \n", "167 176 1704405690 37 93 40 0 0 170 \n", "171 176 1704406231 37 93 40 0 0 170 \n", "210 176 1704412860 37 103 40 0 0 180 \n", "253 176 1704420442 37 103 40 0 0 180 \n", "277 176 1704424600 37 103 40 0 0 180 \n", "\n", " D_n S_n diff start_dt \n", "24 804 750 204.0 2024-01-05 01:00:54 \n", "81 2078 2100 128.0 2024-01-05 03:22:58 \n", "143 874 750 274.0 2024-01-05 06:00:04 \n", "144 1110 900 236.0 2024-01-05 06:04:00 \n", "167 750 770 150.0 2024-01-05 07:01:30 \n", "171 1291 1450 31.0 2024-01-05 07:10:31 \n", "210 851 860 169.0 2024-01-05 09:01:00 \n", "253 1283 1440 22.0 2024-01-05 11:07:22 \n", "277 1820 1980 20.0 2024-01-05 12:16:40 " ] }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5cycleD_nS_ndiffstart_dt
7017617043909503773400015040504050150.02024-01-05 02:55:50
7117617043911003773400015042004200150.02024-01-05 02:58:20
72176170439125037734000150750750150.02024-01-05 03:00:50
73176170439139937734000150899900149.02024-01-05 03:03:19
7417617043915503773400015010501050151.02024-01-05 03:05:50
7517617043917003773400015012001200150.02024-01-05 03:08:20
7617617043918503773400015013501350150.02024-01-05 03:10:50
7717617043920003773400015015001500150.02024-01-05 03:13:20
7817617043921513773400015016511650151.02024-01-05 03:15:51
7917617043923003773400015018001800149.02024-01-05 03:18:20
8017617043924503773400015019501950150.02024-01-05 03:20:50
8117617043925783773400015020782100128.02024-01-05 03:22:58
8217617043927283773400015022282250150.02024-01-05 03:25:28
8317617043928803773400015023802400152.02024-01-05 03:28:00
8417617043930303773400015025302550150.02024-01-05 03:30:30
8517617043931803773400015026802700150.02024-01-05 03:33:00
8617617043933303773400015028302850150.02024-01-05 03:35:30
8717617043934803773400015029803000150.02024-01-05 03:38:00
8817617043936303773400015031303150150.02024-01-05 03:40:30
8917617043937803773400015032803300150.02024-01-05 03:43:00
\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 cycle \\\n", "70 176 1704390950 37 73 40 0 0 150 \n", "71 176 1704391100 37 73 40 0 0 150 \n", "72 176 1704391250 37 73 40 0 0 150 \n", "73 176 1704391399 37 73 40 0 0 150 \n", "74 176 1704391550 37 73 40 0 0 150 \n", "75 176 1704391700 37 73 40 0 0 150 \n", "76 176 1704391850 37 73 40 0 0 150 \n", "77 176 1704392000 37 73 40 0 0 150 \n", "78 176 1704392151 37 73 40 0 0 150 \n", "79 176 1704392300 37 73 40 0 0 150 \n", "80 176 1704392450 37 73 40 0 0 150 \n", "81 176 1704392578 37 73 40 0 0 150 \n", "82 176 1704392728 37 73 40 0 0 150 \n", "83 176 1704392880 37 73 40 0 0 150 \n", "84 176 1704393030 37 73 40 0 0 150 \n", "85 176 1704393180 37 73 40 0 0 150 \n", "86 176 1704393330 37 73 40 0 0 150 \n", "87 176 1704393480 37 73 40 0 0 150 \n", "88 176 1704393630 37 73 40 0 0 150 \n", "89 176 1704393780 37 73 40 0 0 150 \n", "\n", " D_n S_n diff start_dt \n", "70 4050 4050 150.0 2024-01-05 02:55:50 \n", "71 4200 4200 150.0 2024-01-05 02:58:20 \n", "72 750 750 150.0 2024-01-05 03:00:50 \n", "73 899 900 149.0 2024-01-05 03:03:19 \n", "74 1050 1050 151.0 2024-01-05 03:05:50 \n", "75 1200 1200 150.0 2024-01-05 03:08:20 \n", "76 1350 1350 150.0 2024-01-05 03:10:50 \n", "77 1500 1500 150.0 2024-01-05 03:13:20 \n", "78 1651 1650 151.0 2024-01-05 03:15:51 \n", "79 1800 1800 149.0 2024-01-05 03:18:20 \n", "80 1950 1950 150.0 2024-01-05 03:20:50 \n", "81 2078 2100 128.0 2024-01-05 03:22:58 \n", "82 2228 2250 150.0 2024-01-05 03:25:28 \n", "83 2380 2400 152.0 2024-01-05 03:28:00 \n", "84 2530 2550 150.0 2024-01-05 03:30:30 \n", "85 2680 2700 150.0 2024-01-05 03:33:00 \n", "86 2830 2850 150.0 2024-01-05 03:35:30 \n", "87 2980 3000 150.0 2024-01-05 03:38:00 \n", "88 3130 3150 150.0 2024-01-05 03:40:30 \n", "89 3280 3300 150.0 2024-01-05 03:43:00 " ] }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5cycleD_nS_ndiffstart_dt
7017617043909503773400015040504050150.02024-01-05 02:55:50
7117617043911003773400015042004200150.02024-01-05 02:58:20
72176170439125037734000150750750150.02024-01-05 03:00:50
73176170439139937734000150899900149.02024-01-05 03:03:19
7417617043915503773400015010501050151.02024-01-05 03:05:50
7517617043917003773400015012001200150.02024-01-05 03:08:20
7617617043918503773400015013501350150.02024-01-05 03:10:50
7717617043920003773400015015001500150.02024-01-05 03:13:20
7817617043921513773400015016511650151.02024-01-05 03:15:51
7917617043923003773400015018001800149.02024-01-05 03:18:20
8017617043924503773400015019501950150.02024-01-05 03:20:50
8117617043925783262340012820782078128.02024-01-05 03:22:58
8217617043927283773400015022282228150.02024-01-05 03:25:28
8317617043928803774410015223802380152.02024-01-05 03:28:00
8417617043930303773400015025302530150.02024-01-05 03:30:30
8517617043931803773400015026802680150.02024-01-05 03:33:00
8617617043933303773400015028302830150.02024-01-05 03:35:30
8717617043934803773400015029802980150.02024-01-05 03:38:00
8817617043936303773400015031303130150.02024-01-05 03:40:30
8917617043937803773400015032803280150.02024-01-05 03:43:00
\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 cycle \\\n", "70 176 1704390950 37 73 40 0 0 150 \n", "71 176 1704391100 37 73 40 0 0 150 \n", "72 176 1704391250 37 73 40 0 0 150 \n", "73 176 1704391399 37 73 40 0 0 150 \n", "74 176 1704391550 37 73 40 0 0 150 \n", "75 176 1704391700 37 73 40 0 0 150 \n", "76 176 1704391850 37 73 40 0 0 150 \n", "77 176 1704392000 37 73 40 0 0 150 \n", "78 176 1704392151 37 73 40 0 0 150 \n", "79 176 1704392300 37 73 40 0 0 150 \n", "80 176 1704392450 37 73 40 0 0 150 \n", "81 176 1704392578 32 62 34 0 0 128 \n", "82 176 1704392728 37 73 40 0 0 150 \n", "83 176 1704392880 37 74 41 0 0 152 \n", "84 176 1704393030 37 73 40 0 0 150 \n", "85 176 1704393180 37 73 40 0 0 150 \n", "86 176 1704393330 37 73 40 0 0 150 \n", "87 176 1704393480 37 73 40 0 0 150 \n", "88 176 1704393630 37 73 40 0 0 150 \n", "89 176 1704393780 37 73 40 0 0 150 \n", "\n", " D_n S_n diff start_dt \n", "70 4050 4050 150.0 2024-01-05 02:55:50 \n", "71 4200 4200 150.0 2024-01-05 02:58:20 \n", "72 750 750 150.0 2024-01-05 03:00:50 \n", "73 899 900 149.0 2024-01-05 03:03:19 \n", "74 1050 1050 151.0 2024-01-05 03:05:50 \n", "75 1200 1200 150.0 2024-01-05 03:08:20 \n", "76 1350 1350 150.0 2024-01-05 03:10:50 \n", "77 1500 1500 150.0 2024-01-05 03:13:20 \n", "78 1651 1650 151.0 2024-01-05 03:15:51 \n", "79 1800 1800 149.0 2024-01-05 03:18:20 \n", "80 1950 1950 150.0 2024-01-05 03:20:50 \n", "81 2078 2078 128.0 2024-01-05 03:22:58 \n", "82 2228 2228 150.0 2024-01-05 03:25:28 \n", "83 2380 2380 152.0 2024-01-05 03:28:00 \n", "84 2530 2530 150.0 2024-01-05 03:30:30 \n", "85 2680 2680 150.0 2024-01-05 03:33:00 \n", "86 2830 2830 150.0 2024-01-05 03:35:30 \n", "87 2980 2980 150.0 2024-01-05 03:38:00 \n", "88 3130 3130 150.0 2024-01-05 03:40:30 \n", "89 3280 3280 150.0 2024-01-05 03:43:00 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "inter_no = 176\n", "a, b = 70, 90\n", "# 2-1. 참값 판단 프로세스\n", "hours = np.array(range(midnight, next_day + 1, 3600)) # 정각에 해당하는 시각들 목록\n", "\n", "rhist = rhistory.copy()[rhistory.inter_no==inter_no]\n", "rhist = rhist.drop_duplicates(subset=['start_unix']).reset_index(drop=True)\n", "\n", "# D_n 및 S_n 값 정의\n", "rhist['D_n'] = 0 # D_n : 시간차이\n", "rhist['S_n'] = 0 # S_n : 현시시간합\n", "for n in range(len(rhist)):\n", " curr_unix = rhist.iloc[n].start_unix # current start_unix\n", " rhist.loc[n, ['D_n', 'S_n']] = calculate_DS(rhist, curr_unix)\n", "\n", "rhist_diff = rhist.copy()\n", "rhist_diff['diff'] = rhist_diff['start_unix'].diff()\n", "rhist_diff = rhist_diff[list(rhist_diff.columns)[:7] + list(rhist_diff.columns)[-4:]]\n", "rhist_diff['start_dt'] = rhist_diff['start_unix'].map(lambda x:datetime.fromtimestamp(x))\n", "display(rhist_diff[abs(rhist_diff['diff'] - rhist_diff['cycle'])>10])\n", "display(rhist_diff[a:b])\n", "\n", "# 이전시각, 현재시각\n", "prev_unix = rhist.loc[0, 'start_unix'] # previous start_unix\n", "curr_unix = rhist.loc[1, 'start_unix'] # current start_unix\n", "\n", "# rhist의 마지막 행에 도달할 때까지 반복\n", "while True:\n", " n = rhist[rhist.start_unix==curr_unix].index[0]\n", " cycle = rhist.loc[n, 'cycle']\n", " D_n = rhist.loc[n, 'D_n']\n", " S_n = rhist.loc[n, 'S_n']\n", " # 참값인 경우\n", " if (abs(D_n - S_n) <= 5):\n", " pass\n", " # 참값이 아닌 경우\n", " else:\n", " # 2-1-1. 결측치 처리 : 인접한 두 start_unix의 차이가 계획된 주기의 두 배보다 크면 결측이 일어났다고 판단, 신호계획의 현시시간으로 \"대체\"\n", " if curr_unix - prev_unix >= 2 * cycle:\n", " # prev_unix를 계획된 주기만큼 늘려가면서 한 행씩 채워나간다.\n", " # (curr_unix와의 차이가 계획된 주기보다 작거나 같아질 때까지)\n", " new_rows = []\n", " while curr_unix - prev_unix > cycle:\n", " prev_unix += cycle\n", " # 신호 계획(prow) 불러오기\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= prev_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan.copy()[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)] # planned row\n", " # prow에서 필요한 부분을 rhist에 추가\n", " prow['start_unix'] = prev_unix\n", " prow = prow.drop(['start_hour', 'start_minute', 'offset'], axis=1)\n", " cycle = prow.iloc[0].cycle\n", " rhist = pd.concat([rhist, prow])\n", " rhist = rhist.sort_values(by='start_unix').reset_index(drop=True)\n", " n += 1\n", "\n", " # 2-1-2. 이상치 처리 : 비율에 따라 해당 행을 \"삭제\"(R_n <= 0.5) 또는 \"조정\"(R_n > 0.5)한다\n", " R_n = (curr_unix - prev_unix) / cycle # R_n : 비율\n", " # R_n이 0.5보다 작거나 같으면 해당 행을 삭제\n", " if R_n <= 0.5:\n", " rhist = rhist.drop(index=n).reset_index(drop=True)\n", " # 행삭제에 따른 curr_unix, R_n 재정의\n", " curr_unix = rhist.loc[n, 'start_unix']\n", " R_n = (curr_unix - prev_unix) / cycle # R_n : 비율\n", "\n", " # R_n이 0.5보다 크면 해당 행 조정 (비율을 유지한 채로 현시시간 대체)\n", " if R_n > 0.5:\n", " # 신호 계획(prow) 불러오기\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)] # planned row\n", " # 조정된 현시시간 (prow에 R_n을 곱하고 정수로 바꿈)\n", " adjusted_dur = prow.copy()[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] * R_n\n", " int_parts = adjusted_dur.iloc[0].apply(lambda x: int(x))\n", " frac_parts = adjusted_dur.iloc[0] - int_parts\n", " difference = round(adjusted_dur.iloc[0].sum()) - int_parts.sum()\n", " for _ in range(difference): # 소수 부분이 가장 큰 상위 'difference'개의 값에 대해 올림 처리\n", " max_frac_index = frac_parts.idxmax()\n", " int_parts[max_frac_index] += 1\n", " frac_parts[max_frac_index] = 0 # 이미 처리된 항목은 0으로 설정\n", " # rhist에 조정된 현시시간을 반영\n", " rhist.loc[n, [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] = int_parts.values\n", " rhist.loc[n, 'cycle'] = int_parts.sum().sum() // 2\n", "\n", " if n == rhist.index[-1]:\n", " break\n", " prev_unix = curr_unix\n", " curr_unix = rhist.loc[n+1, 'start_unix']\n", "# rhist['start_dt'] = rhist['start_unix'].map(lambda x:datetime.fromtimestamp(x))\n", "\n", "# 생략해도 무방할 코드\n", "rhist = rhist.reset_index(drop=True)\n", "rhist = rhist.sort_values(by=['start_unix'])\n", "\n", "# D_n 및 S_n 값 재정의\n", "for n in range(len(rhist)):\n", " curr_unix = rhist.iloc[n].start_unix # current start_unix\n", " rhist.loc[n, ['D_n', 'S_n']] = calculate_DS(rhist, curr_unix)\n", "\n", "rhist_diff = rhist.copy()\n", "rhist_diff['diff'] = rhist_diff['start_unix'].diff()\n", "rhist_diff = rhist_diff[list(rhist_diff.columns)[:7] + list(rhist_diff.columns)[-4:]]\n", "rhist_diff['start_dt'] = rhist_diff['start_unix'].map(lambda x:datetime.fromtimestamp(x))\n", "display(rhist_diff[a:b])" ] }, { "cell_type": "code", "execution_count": 237, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5cycleD_nS_n
260177170442177043277040018025202520
261177170442195043277040018027002700
262177170442213043277040018028802880
263177170442231043277040018030603060
264177170442249043277040018032403240
265177170442267043277040018034203420
266177170442284943277040018035993600
267177170442303043277040018037803780
268177170442321043277040018039603960
269177170442339043277040018041404140
270177170442357043277040018043204320
2711771704423750432770400180901900
272177170442392943277040018010801080
273177170442411043277040018012611260
274177170442429043277040018014411440
275177170442446943277040018016201620
10177170442464943277040018018001620
10177170442482943277040018019801800
10177170442500943277040018021601980
10177170442518943277040018023402160
10177170442536943277040018025202340
10177170442554943277040018027002520
10177170442572943277040018028802700
10177170442590943277040018030602880
10177170442608943277040018032403060
10177170442626943277040018034203240
10177170442644943277040018036003420
10177170442662943277040018037803600
10177170442680943277040018039603780
10177170442698943277040018041403960
\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 cycle \\\n", "260 177 1704421770 43 27 70 40 0 180 \n", "261 177 1704421950 43 27 70 40 0 180 \n", "262 177 1704422130 43 27 70 40 0 180 \n", "263 177 1704422310 43 27 70 40 0 180 \n", "264 177 1704422490 43 27 70 40 0 180 \n", "265 177 1704422670 43 27 70 40 0 180 \n", "266 177 1704422849 43 27 70 40 0 180 \n", "267 177 1704423030 43 27 70 40 0 180 \n", "268 177 1704423210 43 27 70 40 0 180 \n", "269 177 1704423390 43 27 70 40 0 180 \n", "270 177 1704423570 43 27 70 40 0 180 \n", "271 177 1704423750 43 27 70 40 0 180 \n", "272 177 1704423929 43 27 70 40 0 180 \n", "273 177 1704424110 43 27 70 40 0 180 \n", "274 177 1704424290 43 27 70 40 0 180 \n", "275 177 1704424469 43 27 70 40 0 180 \n", "10 177 1704424649 43 27 70 40 0 180 \n", "10 177 1704424829 43 27 70 40 0 180 \n", "10 177 1704425009 43 27 70 40 0 180 \n", "10 177 1704425189 43 27 70 40 0 180 \n", "10 177 1704425369 43 27 70 40 0 180 \n", "10 177 1704425549 43 27 70 40 0 180 \n", "10 177 1704425729 43 27 70 40 0 180 \n", "10 177 1704425909 43 27 70 40 0 180 \n", "10 177 1704426089 43 27 70 40 0 180 \n", "10 177 1704426269 43 27 70 40 0 180 \n", "10 177 1704426449 43 27 70 40 0 180 \n", "10 177 1704426629 43 27 70 40 0 180 \n", "10 177 1704426809 43 27 70 40 0 180 \n", "10 177 1704426989 43 27 70 40 0 180 \n", "\n", " D_n S_n \n", "260 2520 2520 \n", "261 2700 2700 \n", "262 2880 2880 \n", "263 3060 3060 \n", "264 3240 3240 \n", "265 3420 3420 \n", "266 3599 3600 \n", "267 3780 3780 \n", "268 3960 3960 \n", "269 4140 4140 \n", "270 4320 4320 \n", "271 901 900 \n", "272 1080 1080 \n", "273 1261 1260 \n", "274 1441 1440 \n", "275 1620 1620 \n", "10 1800 1620 \n", "10 1980 1800 \n", "10 2160 1980 \n", "10 2340 2160 \n", "10 2520 2340 \n", "10 2700 2520 \n", "10 2880 2700 \n", "10 3060 2880 \n", "10 3240 3060 \n", "10 3420 3240 \n", "10 3600 3420 \n", "10 3780 3600 \n", "10 3960 3780 \n", "10 4140 3960 " ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rhist[list(rhist.columns)[:7] + list(rhist.columns)[-3:]].sort_values(by=['start_unix'])[260:290]" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['inter_no', 'start_unix', 'dura_A1', 'dura_A2', 'dura_A3', 'dura_A4', 'dura_A5', 'dura_A6', 'dura_A7', 'dura_A8', 'dura_B1', 'dura_B2', 'dura_B3', 'dura_B4', 'dura_B5', 'dura_B6', 'dura_B7', 'dura_B8', 'cycle', 'D_n', 'S_n', 'offset']\n", "['inter_no', 'dura_A1', 'dura_A2', 'dura_A3', 'dura_A4', 'dura_A5', 'dura_A6', 'dura_A7', 'dura_A8', 'dura_B1', 'dura_B2', 'dura_B3', 'dura_B4', 'dura_B5', 'dura_B6', 'dura_B7', 'dura_B8', 'cycle', 'offset', 'start_unix']\n" ] } ], "source": [ "print(list(rhist.columns))\n", "print(list(new_rows[0].columns))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 2-1. 참값 판단 프로세스\n", "hours = np.array(range(midnight, next_day + 1, 3600)) # 정각에 해당하는 시각들 목록\n", "\n", "rhist = rhistory.copy()[rhistory.inter_no==inter_no].reset_index(drop=True)\n", "\n", "# D_n 및 S_n 정의\n", "rhist['D_n'] = 0 # D_n : 시간차이\n", "rhist['S_n'] = 0 # S_n : 현시시간합\n", "for n in range(len(rhist)):\n", " curr_unix = rhist.iloc[n].start_unix # current start_unix\n", " ghour_lt_curr_unix = hours[hours <= curr_unix].max() # the greatest hour less than (or equal to) curr_unix\n", " start_unixes = rhist.start_unix.unique()\n", " start_unixes_lt_ghour = np.sort(start_unixes[start_unixes < ghour_lt_curr_unix]) # start unixes less than ghour_lt_curr_unix\n", " # 기준유닉스(base_unix) : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 start_unix\n", " if list(start_unixes_lt_ghour):\n", " base_unix = start_unixes_lt_ghour[-5]\n", " # start_unixes_lt_ghour가 비었을 경우에는 맨 앞 start_unix로 base_unix를 지정\n", " else:\n", " base_unix = rhist.start_unix.min()\n", " D_n = curr_unix - base_unix\n", " S_n_durs = rhist[(rhist.start_unix > base_unix) & (rhist.start_unix <= curr_unix)] \\\n", " [[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = S_n_durs.values.sum() // 2\n", " rhist.loc[n, ['D_n', 'S_n']] = [D_n, S_n]\n", "rhist[list(rhist.columns[:10]) + list(rhist.columns[-2:])]\n", "\n", "new_rows = []\n", "# 결측 및 이상치 처리\n", "prev_unix = rhist.loc[0, 'start_unix'] # previous start_unix\n", "curr_unix = rhist.loc[1, 'start_unix'] # current start_unix\n", "fina_unix = rhist.loc[-1, 'start_unix'] # final start_unix\n", "n = rhist[rhist.start_unix==curr_unix].index\n", "print(n)\n", "while curr_unix <= fina_unix:\n", " prev_unix = rhist.loc[n-1, 'start_unix'] # current start_unix\n", " curr_unix = rhist.loc[n, 'start_unix'] # previous start_unix\n", " cycle = rhist.iloc[n].cycle\n", " R_n = (curr_unix - prev_unix) / cycle # R_n : 비율\n", " # 매 반복마다 D_n 및 S_n을 다시 정의\n", " ghour_lt_curr_unix = hours[hours <= curr_unix].max() # the greatest hour less than (or equal to) curr_unix\n", " start_unixes = rhist.start_unix.unique()\n", " start_unixes_lt_ghour = np.sort(start_unixes[start_unixes < ghour_lt_curr_unix]) # start unixes less than ghour_lt_curr_unix\n", " # 기준유닉스(base_unix) : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 start_unix\n", " if list(start_unixes_lt_ghour):\n", " base_unix = start_unixes_lt_ghour[-5]\n", " # start_unixes_lt_ghour가 비었을 경우에는 맨 앞 start_unix로 base_unix를 지정\n", " else:\n", " base_unix = rhist.start_unix.min()\n", " D_n = curr_unix - base_unix\n", " S_n_durs = rhist[(rhist.start_unix > base_unix) & (rhist.start_unix <= curr_unix)] \\\n", " [[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = S_n_durs.values.sum() // 2\n", "\n", " # 참값인 경우\n", " if (abs(D_n - S_n) <= 5):\n", " print(n, 'true', D_n, S_n, curr_unix - prev_unix)\n", " pass\n", "\n", " # 참값이 아닌 경우\n", " else:\n", " # print(n, 'false', D_n, S_n, curr_unix - prev_unix)\n", " # 2-1-1. 결측치 처리 : 인접한 두 end_unix의 차이가 계획된 주기의 두 배보다 크면 결측이 일어났다고 판단\n", " if curr_unix - prev_unix >= 2 * cycle:\n", " # 현재 unix를 계획된 주기만큼 늘려가면서 한 행씩 채워나간다.\n", " # (다음 unix와의 차이가 계획된 주기보다 작거나 같아질 때까지)\n", " while curr_unix - prev_unix > cycle:\n", " # print(n, 'missing_values', curr_unix - prev_unix)\n", " prev_unix += cycle\n", " # 계획된 현시시간(prow) 불러오기\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)] # planned row\n", " prow = prow.drop(['start_hour', 'start_minute'], axis=1)\n", " prow['start_unix'] = prev_unix\n", " cycle = prow.iloc[0].cycle\n", " new_rows.append(prow)\n", "\n", " n += 1\n", "new_rows = pd.concat(new_rows)\n", "new_rows[list(new_rows.columns)[:6] + list(new_rows.columns)[-2:]]" ] }, { "cell_type": "code", "execution_count": 117, "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", "
inter_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5dura_A6dura_A7dura_A8...dura_B3dura_B4dura_B5dura_B6dura_B7dura_B8cycleD_nS_ndiff
1551771704403768362068260000...6826000015025182400268.0
2761771704432930432770400000...7040000018091809008461.0
\n", "

2 rows × 22 columns

\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 \\\n", "155 177 1704403768 36 20 68 26 0 \n", "276 177 1704432930 43 27 70 40 0 \n", "\n", " dura_A6 dura_A7 dura_A8 ... dura_B3 dura_B4 dura_B5 dura_B6 \\\n", "155 0 0 0 ... 68 26 0 0 \n", "276 0 0 0 ... 70 40 0 0 \n", "\n", " dura_B7 dura_B8 cycle D_n S_n diff \n", "155 0 0 150 2518 2400 268.0 \n", "276 0 0 180 9180 900 8461.0 \n", "\n", "[2 rows x 22 columns]" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rhist_diff = rhist.copy()\n", "rhist_diff['diff'] = rhist_diff['start_unix'].diff()\n", "rhist_diff[rhist_diff['diff'] - rhist_diff['cycle'] > 10]" ] }, { "cell_type": "code", "execution_count": 130, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5cycleD_nS_n
270177170442357043277040018043204320
2711771704423750432770400180901900
272177170442392943277040018010801080
273177170442411043277040018012611260
274177170442429043277040018014411440
275177170442446943277040018016201620
27617717044329304327704001809180900
277177170443311043277040018093601080
278177170443329043277040018095401260
279177170443347043277040018097201440
280177170443365043277040018099001620
2811771704433830432770400180100801800
2821771704434011432770400180102611980
2831771704434190432770400180104402160
2841771704434370432770400180106202340
2851771704434550432770400180900900
286177170443472943277040018010791080
287177170443491043277040018012601260
288177170443509043277040018014401440
289177170443527043277040018016201620
290177170443545043277040018018001800
291177170443563043277040018019801980
292177170443581043277040018021602160
293177170443599043277040018023402340
294177170443617043277040018025202520
295177170443635143277040018027012700
296177170443653043277040018028802880
297177170443671043277040018030603060
298177170443689043277040018032403240
299177170443707043277040018034203420
\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 cycle \\\n", "270 177 1704423570 43 27 70 40 0 180 \n", "271 177 1704423750 43 27 70 40 0 180 \n", "272 177 1704423929 43 27 70 40 0 180 \n", "273 177 1704424110 43 27 70 40 0 180 \n", "274 177 1704424290 43 27 70 40 0 180 \n", "275 177 1704424469 43 27 70 40 0 180 \n", "276 177 1704432930 43 27 70 40 0 180 \n", "277 177 1704433110 43 27 70 40 0 180 \n", "278 177 1704433290 43 27 70 40 0 180 \n", "279 177 1704433470 43 27 70 40 0 180 \n", "280 177 1704433650 43 27 70 40 0 180 \n", "281 177 1704433830 43 27 70 40 0 180 \n", "282 177 1704434011 43 27 70 40 0 180 \n", "283 177 1704434190 43 27 70 40 0 180 \n", "284 177 1704434370 43 27 70 40 0 180 \n", "285 177 1704434550 43 27 70 40 0 180 \n", "286 177 1704434729 43 27 70 40 0 180 \n", "287 177 1704434910 43 27 70 40 0 180 \n", "288 177 1704435090 43 27 70 40 0 180 \n", "289 177 1704435270 43 27 70 40 0 180 \n", "290 177 1704435450 43 27 70 40 0 180 \n", "291 177 1704435630 43 27 70 40 0 180 \n", "292 177 1704435810 43 27 70 40 0 180 \n", "293 177 1704435990 43 27 70 40 0 180 \n", "294 177 1704436170 43 27 70 40 0 180 \n", "295 177 1704436351 43 27 70 40 0 180 \n", "296 177 1704436530 43 27 70 40 0 180 \n", "297 177 1704436710 43 27 70 40 0 180 \n", "298 177 1704436890 43 27 70 40 0 180 \n", "299 177 1704437070 43 27 70 40 0 180 \n", "\n", " D_n S_n \n", "270 4320 4320 \n", "271 901 900 \n", "272 1080 1080 \n", "273 1261 1260 \n", "274 1441 1440 \n", "275 1620 1620 \n", "276 9180 900 \n", "277 9360 1080 \n", "278 9540 1260 \n", "279 9720 1440 \n", "280 9900 1620 \n", "281 10080 1800 \n", "282 10261 1980 \n", "283 10440 2160 \n", "284 10620 2340 \n", "285 900 900 \n", "286 1079 1080 \n", "287 1260 1260 \n", "288 1440 1440 \n", "289 1620 1620 \n", "290 1800 1800 \n", "291 1980 1980 \n", "292 2160 2160 \n", "293 2340 2340 \n", "294 2520 2520 \n", "295 2701 2700 \n", "296 2880 2880 \n", "297 3060 3060 \n", "298 3240 3240 \n", "299 3420 3420 " ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rhist[list(rhist.columns)[:7] + list(rhist.columns)[-3:]][270:300]" ] }, { "cell_type": "code", "execution_count": 109, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_nostart_unixdura_A1dura_A2dura_A3dura_A4dura_A5cycleD_nS_n
210177170441278040257134017044204420
2111771704412950432770400180850860
212177170441313043277040018010301040
213177170441331043277040018012101220
214177170441348943277040018013891400
215177170441367043277040018015701580
216177170441385043277040018017501760
217177170441403043277040018019301940
218177170441421043277040018021102120
219177170441439043277040018022902300
220177170441457043277040018024702480
221177170441475043277040018026502660
222177170441493043277040018028302840
223177170441511043277040018030103020
224177170441529043277040018031903200
225177170441547043277040018033703380
226177170441565043277040018035503560
227177170441583043277040018037303740
228177170441601043277040018039103920
229177170441619043277040018040904100
230177170441637043277040018042704280
2311771704416551432770400180901900
232177170441673043277040018010801080
233177170441691143277040018012611260
234177170441709043277040018014401440
235177170441727043277040018016201620
236177170441744943277040018017991800
237177170441763043277040018019801980
238177170441781043277040018021602160
239177170441799043277040018023402340
240177170441816943277040018025192520
241177170441835043277040018027002700
242177170441853043277040018028802880
243177170441871043277040018030603060
244177170441889043277040018032403240
245177170441907043277040018034203420
246177170441925043277040018036003600
247177170441943043277040018037803780
248177170441961043277040018039603960
249177170441979043277040018041404140
250177170441996943277040018043194320
2511771704420150432770400180900900
252177170442033043277040018010801080
253177170442051043277040018012601260
254177170442069043277040018014401440
255177170442087043277040018016201620
256177170442105043277040018018001800
257177170442123043277040018019801980
258177170442141143277040018021612160
259177170442159043277040018023402340
260177170442177043277040018025202520
261177170442195043277040018027002700
262177170442213043277040018028802880
263177170442231043277040018030603060
264177170442249043277040018032403240
265177170442267043277040018034203420
266177170442284943277040018035993600
267177170442303043277040018037803780
268177170442321043277040018039603960
269177170442339043277040018041404140
\n", "
" ], "text/plain": [ " inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 cycle \\\n", "210 177 1704412780 40 25 71 34 0 170 \n", "211 177 1704412950 43 27 70 40 0 180 \n", "212 177 1704413130 43 27 70 40 0 180 \n", "213 177 1704413310 43 27 70 40 0 180 \n", "214 177 1704413489 43 27 70 40 0 180 \n", "215 177 1704413670 43 27 70 40 0 180 \n", "216 177 1704413850 43 27 70 40 0 180 \n", "217 177 1704414030 43 27 70 40 0 180 \n", "218 177 1704414210 43 27 70 40 0 180 \n", "219 177 1704414390 43 27 70 40 0 180 \n", "220 177 1704414570 43 27 70 40 0 180 \n", "221 177 1704414750 43 27 70 40 0 180 \n", "222 177 1704414930 43 27 70 40 0 180 \n", "223 177 1704415110 43 27 70 40 0 180 \n", "224 177 1704415290 43 27 70 40 0 180 \n", "225 177 1704415470 43 27 70 40 0 180 \n", "226 177 1704415650 43 27 70 40 0 180 \n", "227 177 1704415830 43 27 70 40 0 180 \n", "228 177 1704416010 43 27 70 40 0 180 \n", "229 177 1704416190 43 27 70 40 0 180 \n", "230 177 1704416370 43 27 70 40 0 180 \n", "231 177 1704416551 43 27 70 40 0 180 \n", "232 177 1704416730 43 27 70 40 0 180 \n", "233 177 1704416911 43 27 70 40 0 180 \n", "234 177 1704417090 43 27 70 40 0 180 \n", "235 177 1704417270 43 27 70 40 0 180 \n", "236 177 1704417449 43 27 70 40 0 180 \n", "237 177 1704417630 43 27 70 40 0 180 \n", "238 177 1704417810 43 27 70 40 0 180 \n", "239 177 1704417990 43 27 70 40 0 180 \n", "240 177 1704418169 43 27 70 40 0 180 \n", "241 177 1704418350 43 27 70 40 0 180 \n", "242 177 1704418530 43 27 70 40 0 180 \n", "243 177 1704418710 43 27 70 40 0 180 \n", "244 177 1704418890 43 27 70 40 0 180 \n", "245 177 1704419070 43 27 70 40 0 180 \n", "246 177 1704419250 43 27 70 40 0 180 \n", "247 177 1704419430 43 27 70 40 0 180 \n", "248 177 1704419610 43 27 70 40 0 180 \n", "249 177 1704419790 43 27 70 40 0 180 \n", "250 177 1704419969 43 27 70 40 0 180 \n", "251 177 1704420150 43 27 70 40 0 180 \n", "252 177 1704420330 43 27 70 40 0 180 \n", "253 177 1704420510 43 27 70 40 0 180 \n", "254 177 1704420690 43 27 70 40 0 180 \n", "255 177 1704420870 43 27 70 40 0 180 \n", "256 177 1704421050 43 27 70 40 0 180 \n", "257 177 1704421230 43 27 70 40 0 180 \n", "258 177 1704421411 43 27 70 40 0 180 \n", "259 177 1704421590 43 27 70 40 0 180 \n", "260 177 1704421770 43 27 70 40 0 180 \n", "261 177 1704421950 43 27 70 40 0 180 \n", "262 177 1704422130 43 27 70 40 0 180 \n", "263 177 1704422310 43 27 70 40 0 180 \n", "264 177 1704422490 43 27 70 40 0 180 \n", "265 177 1704422670 43 27 70 40 0 180 \n", "266 177 1704422849 43 27 70 40 0 180 \n", "267 177 1704423030 43 27 70 40 0 180 \n", "268 177 1704423210 43 27 70 40 0 180 \n", "269 177 1704423390 43 27 70 40 0 180 \n", "\n", " D_n S_n \n", "210 4420 4420 \n", "211 850 860 \n", "212 1030 1040 \n", "213 1210 1220 \n", "214 1389 1400 \n", "215 1570 1580 \n", "216 1750 1760 \n", "217 1930 1940 \n", "218 2110 2120 \n", "219 2290 2300 \n", "220 2470 2480 \n", "221 2650 2660 \n", "222 2830 2840 \n", "223 3010 3020 \n", "224 3190 3200 \n", "225 3370 3380 \n", "226 3550 3560 \n", "227 3730 3740 \n", "228 3910 3920 \n", "229 4090 4100 \n", "230 4270 4280 \n", "231 901 900 \n", "232 1080 1080 \n", "233 1261 1260 \n", "234 1440 1440 \n", "235 1620 1620 \n", "236 1799 1800 \n", "237 1980 1980 \n", "238 2160 2160 \n", "239 2340 2340 \n", "240 2519 2520 \n", "241 2700 2700 \n", "242 2880 2880 \n", "243 3060 3060 \n", "244 3240 3240 \n", "245 3420 3420 \n", "246 3600 3600 \n", "247 3780 3780 \n", "248 3960 3960 \n", "249 4140 4140 \n", "250 4319 4320 \n", "251 900 900 \n", "252 1080 1080 \n", "253 1260 1260 \n", "254 1440 1440 \n", "255 1620 1620 \n", "256 1800 1800 \n", "257 1980 1980 \n", "258 2161 2160 \n", "259 2340 2340 \n", "260 2520 2520 \n", "261 2700 2700 \n", "262 2880 2880 \n", "263 3060 3060 \n", "264 3240 3240 \n", "265 3420 3420 \n", "266 3599 3600 \n", "267 3780 3780 \n", "268 3960 3960 \n", "269 4140 4140 " ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rhist[210:210+60][list(rhist.columns)[:7] + list(rhist.columns)[-3:]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-01-05 16:40:00\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_idstart_unixphas_Aphas_Bdurationinc_edge_Aout_edge_Ainc_edge_Bout_edge_B
0202i917044368101146571510152_02-571510152_01571510152_01571510152_01.65
1202i9170443681022114NaNNaNNaNNaN
2178i317044368601138571540304_02571556450_01571556450_02571540304_01
3178i317044368602239571556450_02571500475_01571540304_02571540303_01
4178i317044368603343571540303_02.21571556450_01571540303_02.21571500475_01
.................................
477202i9170444017122114NaNNaNNaNNaN
478206i717044401711145-571511538_02571542073_02571542073_01571511538_02
479206i717044401712253NaNNaNNaNNaN
480206i717044401713326-571511538_02571542073_02571542073_01571511538_02
481206i717044401714426NaNNaNNaNNaN
\n", "

482 rows × 10 columns

\n", "
" ], "text/plain": [ " inter_no node_id start_unix phas_A phas_B duration inc_edge_A \\\n", "0 202 i9 1704436810 1 1 46 571510152_02 \n", "1 202 i9 1704436810 2 2 114 NaN \n", "2 178 i3 1704436860 1 1 38 571540304_02 \n", "3 178 i3 1704436860 2 2 39 571556450_02 \n", "4 178 i3 1704436860 3 3 43 571540303_02.21 \n", ".. ... ... ... ... ... ... ... \n", "477 202 i9 1704440171 2 2 114 NaN \n", "478 206 i7 1704440171 1 1 45 -571511538_02 \n", "479 206 i7 1704440171 2 2 53 NaN \n", "480 206 i7 1704440171 3 3 26 -571511538_02 \n", "481 206 i7 1704440171 4 4 26 NaN \n", "\n", " out_edge_A inc_edge_B out_edge_B \n", "0 -571510152_01 571510152_01 571510152_01.65 \n", "1 NaN NaN NaN \n", "2 571556450_01 571556450_02 571540304_01 \n", "3 571500475_01 571540304_02 571540303_01 \n", "4 571556450_01 571540303_02.21 571500475_01 \n", ".. ... ... ... \n", "477 NaN NaN NaN \n", "478 571542073_02 571542073_01 571511538_02 \n", "479 NaN NaN NaN \n", "480 571542073_02 571542073_01 571511538_02 \n", "481 NaN NaN NaN \n", "\n", "[482 rows x 10 columns]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = 200\n", "fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", "present_time = fmins[m] # 현재시점\n", "print(datetime.fromtimestamp(present_time))\n", "\n", "# (구) make_rhistory\n", "Rhists = [] # Recent history (1시간 이내)\n", "for inter_no in history.inter_no.unique():\n", " # - 5분마다 신호이력 데이터 수집해서 통합테이블 생성할때\n", " # 1. 조회시점의 유닉스 타임 이전의 신호이력 수집\n", " rhistory = history.copy() # recent history\n", " rhistory = rhistory[(rhistory.end_unix < present_time)]\n", " hours = np.array(range(midnight, next_day + 1, 3600))\n", " rhist = rhistory.copy()[rhistory.inter_no == inter_no] # 특정한 inter_no\n", " rhist = rhist.reset_index(drop=True)\n", " new_rows = []\n", " # 1-1. 결측치 처리 : 인접한 두 end_unix의 차이가 계획된 주기의 두 배보다 크면 결측이 일어났다고 판단\n", " for n in range(len(rhist) - 1):\n", " curr_unix = rhist.iloc[n].end_unix # current end_unix\n", " next_unix = rhist.iloc[n+1].end_unix # next end_unix\n", " cycle = rhist.iloc[n].cycle\n", " if next_unix - curr_unix >= 2 * cycle:\n", " # 현재 unix를 계획된 주기만큼 늘려가면서 한 행씩 채워나간다.\n", " #(다음 unix와의 차이가 계획된 주기보다 작거나 같아질 때까지)\n", " while next_unix - curr_unix > cycle:\n", " curr_unix += cycle\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)] # planned row\n", " prow = prow.drop(['start_hour', 'start_minute'], axis=1)\n", " prow['end_unix'] = curr_unix\n", " cycle = prow.iloc[0].cycle\n", " new_rows.append(prow)\n", " rhist = pd.concat([rhist] + new_rows).sort_values(['end_unix'])\n", " rhist = rhist.reset_index(drop=True)\n", "\n", " # 1-2. 이상치 처리 : 기준유닉스로부터의 시간차이와 현시시간합이 11 이상 차이나면 이상치가 발생했다고 판단\n", " Rhist = rhist.copy() # recent history 1704393231\n", " Rhist = Rhist[(Rhist.end_unix >= present_time - 3600)] # Recent history (1시간 이내)\n", " Rhist = Rhist.reset_index(drop=True)\n", " Rhist['D_n'] = 0\n", " Rhist['S_n'] = 0\n", " for n in range(len(Rhist)):\n", " curr_unix = Rhist.iloc[n].end_unix # current end_unix\n", " cycle = Rhist.iloc[n].cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이 \n", " D_n = curr_unix - base_unix\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " # S_n : 현시시간합\n", " S_n = ddurations.values.sum() // 2\n", " Rhist.loc[n, ['D_n', 'S_n']] = [D_n, S_n]\n", " n = 1\n", " while n < len(Rhist):\n", " prev_unix = Rhist[Rhist.index==n-1]['end_unix'].iloc[0] # previous end_unix\n", " curr_unix = Rhist[Rhist.index==n]['end_unix'].iloc[0] # current end_unix\n", " R_n = (curr_unix - prev_unix) / cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " # S_n : 현시시간합\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = ddurations.values.sum() // 2\n", " # 비율이 0.5보다 작거나 같으면 해당 행을 삭제\n", " if (abs(D_n - S_n) > 10) & (R_n <= 0.5):\n", " Rhist = Rhist.drop(index=n)\n", " n += 1\n", " # 행삭제에 따른 curr_unix, D_n, S_n 등 재정의\n", " if not Rhist[Rhist.index==n]['end_unix'].empty: # 마지막 행을 삭제하여 뒤의 행이 없을 때를 대비\n", " curr_unix = Rhist[Rhist.index==n]['end_unix'].iloc[0] # current end_unix\n", " R_n = (curr_unix - prev_unix) / cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " # S_n : 현시시간합\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = ddurations.values.sum() // 2\n", " # 비율이 0.5보다 크면 해당 행 조정 (비율을 유지한 채로 현시시간 대체)\n", " if (abs(D_n - S_n) > 10) & (R_n > 0.5):\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)].copy().reset_index(drop=True).iloc[0] # planned row\n", " adjusted_dur = prow[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] * R_n\n", " # 조정된 현시시간을 정수로 바꿈\n", " int_parts = adjusted_dur.apply(lambda x: int(x))\n", " frac_parts = adjusted_dur - int_parts\n", " difference = int(round(adjusted_dur.sum())) - int_parts.sum()\n", " # 소수 부분이 가장 큰 상위 'difference'개의 값에 대해 올림 처리\n", " for _ in range(difference):\n", " max_frac_index = frac_parts.idxmax()\n", " int_parts[max_frac_index] += 1\n", " frac_parts[max_frac_index] = 0 # 이미 처리된 항목은 0으로 설정\n", " Rhist.loc[n, [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] = int_parts.values\n", " Rhist.loc[n, 'cycle'] = int_parts.sum() // 2\n", " n += 1\n", " Rhist = Rhist.drop(columns=['offset', 'D_n', 'S_n'])\n", " Rhists.append(Rhist)\n", "Rhists = pd.concat(Rhists)\n", "Rhists = Rhists.sort_values(by=['end_unix', 'inter_no']).reset_index(drop=True)\n", "\n", "# (구) make_histid\n", "# 2. 시작 유닉스 타임컬럼 생성 후 종류 유닉스 타임에서 현시별 현시기간 컬럼의 합을 뺀 값으로 입력\n", "# - 현시시간의 합을 뺀 시간의 +- 10초 이내에 이전 주기정보가 존재하면 그 유닉스 시간을 시작 유닉스시간 값으로 하고, 존재하지 않으면 현시시간의 합을 뺀 유닉스 시간을 시작 유닉스 시간으로 지정\n", "for i, row in rhistory.iterrows():\n", " # 이전 유닉스 존재하지 않음 => 현시시간 합의 차\n", " # 이전 유닉스 존재, abs < 10 => 이전 유닉스\n", " # 이전 유닉스 존재, abs >=10 => 현시시간 합의 차\n", " inter_no = row.inter_no\n", " end_unix = row.end_unix\n", " elapsed_time = row[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]].sum() // 2 # 현시시간 합\n", " start_unix = end_unix - elapsed_time\n", " pre_rows = history[:i] # previous rows\n", " if inter_no in pre_rows.inter_no.unique(): # 이전 유닉스 존재\n", " pre_unix = pre_rows[pre_rows.inter_no == inter_no]['end_unix'].iloc[-1] # previous unix time\n", " if abs(pre_unix - start_unix) < 10: # abs < 10\n", " start_unix = pre_unix\n", " else: # abs >= 10\n", " pass\n", " rhistory.loc[i, 'start_unix'] = start_unix \n", "rhistory[rhistory.isna()] = 0\n", "rhistory['start_unix'] = rhistory['start_unix'].astype(int)\n", "rhistory = rhistory[['inter_no', 'start_unix'] + [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)] + ['cycle']]\n", "\n", "# 계층화된 형태로 변환\n", "hrhistory = [] # hierarchied recent history\n", "for i, row in rhistory.iterrows():\n", " inter_no = row.inter_no\n", " start_unix = row.start_unix\n", "\n", " ind = (timetable['start_seconds'] <= row.start_unix).sum() - 1\n", " start_hour = timetable.iloc[ind].start_hour\n", " start_minute = timetable.iloc[ind].start_minute\n", " isplit = isplits[(inter_no, start_hour, start_minute)]\n", " phas_As = [isplit[j][0] for j in isplit.keys()]\n", " phas_Bs = [isplit[j][1] for j in isplit.keys()]\n", " durs_A = row[[f'dura_A{j}' for j in range(1,9)]]\n", " durs_B = row[[f'dura_B{j}' for j in range(1,9)]]\n", " durations = []\n", " for j in range(1, len(isplit)+1):\n", " ja = isplit[j][0]\n", " jb = isplit[j][1]\n", " if ja == jb:\n", " durations.append(min(durs_A[ja-1], durs_B[jb-1]))\n", " else:\n", " durations.append(abs(durs_A[ja-1] - durs_B[ja-1]))\n", " new_rows = pd.DataFrame({'inter_no':[inter_no] * len(durations), 'start_unix':[start_unix] * len(durations),\n", " 'phas_A':phas_As, 'phas_B':phas_Bs, 'duration':durations})\n", " hrhistory.append(new_rows)\n", "hrhistory = pd.concat(hrhistory)\n", "hrhistory = hrhistory.sort_values(by = ['start_unix', 'inter_no', 'phas_A', 'phas_B']).reset_index(drop=True)\n", "\n", "# 5초단위로 수집한 이동류정보(time2movement[present_time])와 최근 1시간 신호이력(hrhistory)을 병합\n", "movedur = pd.merge(time2movement[present_time], hrhistory, how='inner', on=['inter_no', 'start_unix', 'phas_A', 'phas_B']) # movements and durations\n", "movedur = movedur.sort_values(by=['start_unix', 'inter_no', 'phas_A','phas_B'])\n", "movedur = movedur[['inter_no', 'start_unix', 'phas_A', 'phas_B', 'move_A', 'move_B', 'duration']]\n", "\n", "# 이동류 매칭 테이블에서 진입id, 진출id를 가져와서 붙임.\n", "for i, row in movedur.iterrows():\n", " inter_no = row.inter_no\n", " start_unix = row.start_unix\n", " # incoming and outgoing edges A\n", " move_A = row.move_A\n", " if move_A in [17, 18]:\n", " inc_edge_A = np.nan\n", " out_edge_A = np.nan\n", " else:\n", " match_A = matching[(matching.inter_no == inter_no) & (matching.move_no == move_A)].iloc[0]\n", " inc_edge_A = match_A.inc_edge\n", " out_edge_A = match_A.out_edge\n", " movedur.loc[i, ['inc_edge_A', 'out_edge_A']] = [inc_edge_A, out_edge_A]\n", " # incoming and outgoing edges B\n", " move_B = row.move_B\n", " if move_B in [17, 18]:\n", " inc_edge_B = np.nan\n", " out_edge_B = np.nan\n", " else:\n", " match_B = matching[(matching.inter_no == inter_no) & (matching.move_no == move_B)].iloc[0]\n", " inc_edge_B = match_B.inc_edge\n", " out_edge_B = match_B.out_edge\n", " movedur.loc[i, ['inc_edge_B', 'out_edge_B']] = [inc_edge_B, out_edge_B]\n", "\n", "# 이동류 컬럼 제거\n", "movedur = movedur.drop(['move_A', 'move_B'], axis=1)\n", "\n", "histid = movedur.copy() # history with edge ids (incoming and outgoing edge ids)\n", "histid['node_id'] = histid['inter_no'].map(inter2node)\n", "histid = histid[['inter_no', 'node_id', 'start_unix', 'phas_A', 'phas_B', 'duration', 'inc_edge_A', 'out_edge_A', 'inc_edge_B', 'out_edge_B']]\n", "histid = histid[histid.start_unix > present_time - 3600]\n", "# 시뮬레이션 시작시각 : 현재시각 - 600\n", "# 시뮬레이션 종료시각 : 현재시각 - 300\n", "# 현재시각 : present_time, PT\n", "# PT-900 ... PT-600 ... PT-300 ... PT\n", "histid" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-01-05 04:10:00\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_idstart_unixphas_Aphas_Bdurationinc_edge_Aout_edge_Ainc_edge_Bout_edge_B
0176i117043918501137-571542810_01-571542797_02.99571542797_02.99571542810_01
1176i117043918502273-571542810_01-571542797_02.99-571542810_01571543469_01
2176i117043918503340571543469_02-571542797_02.99NaNNaN
3178i317043918801138571540304_02571556450_01571556450_02571540304_01
4178i317043918802239571556450_02571500475_01571540304_02571540303_01
.................................
528201i817043952501124-571500569_01571500583_02-571500569_01571500618_01
529201i817043952502224571500618_02571500583_02571500618_02571500617_01
530201i817043952503317571500617_02571500618_01571500618_02571500617_01
531201i817043952504458571500617_02571500618_01571500617_02571500569_01
532201i817043952505517571500583_01571500617_01571500583_01571500569_01
\n", "

533 rows × 10 columns

\n", "
" ], "text/plain": [ " inter_no node_id start_unix phas_A phas_B duration inc_edge_A \\\n", "0 176 i1 1704391850 1 1 37 -571542810_01 \n", "1 176 i1 1704391850 2 2 73 -571542810_01 \n", "2 176 i1 1704391850 3 3 40 571543469_02 \n", "3 178 i3 1704391880 1 1 38 571540304_02 \n", "4 178 i3 1704391880 2 2 39 571556450_02 \n", ".. ... ... ... ... ... ... ... \n", "528 201 i8 1704395250 1 1 24 -571500569_01 \n", "529 201 i8 1704395250 2 2 24 571500618_02 \n", "530 201 i8 1704395250 3 3 17 571500617_02 \n", "531 201 i8 1704395250 4 4 58 571500617_02 \n", "532 201 i8 1704395250 5 5 17 571500583_01 \n", "\n", " out_edge_A inc_edge_B out_edge_B \n", "0 -571542797_02.99 571542797_02.99 571542810_01 \n", "1 -571542797_02.99 -571542810_01 571543469_01 \n", "2 -571542797_02.99 NaN NaN \n", "3 571556450_01 571556450_02 571540304_01 \n", "4 571500475_01 571540304_02 571540303_01 \n", ".. ... ... ... \n", "528 571500583_02 -571500569_01 571500618_01 \n", "529 571500583_02 571500618_02 571500617_01 \n", "530 571500618_01 571500618_02 571500617_01 \n", "531 571500618_01 571500617_02 571500569_01 \n", "532 571500617_01 571500583_01 571500569_01 \n", "\n", "[533 rows x 10 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_histid(50)" ] }, { "cell_type": "code", "execution_count": 16, "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", "
inter_noend_unixdura_A1dura_A2dura_A3dura_A4dura_A5dura_A6dura_A7dura_A8...dura_B3dura_B4dura_B5dura_B6dura_B7dura_B8cycleoffsetD_nS_n
0202170438054039101000000...00000014010300
1202170438068039101000000...00000014010300
2202170438082039101000000...00000014010300
3202170438096039101000000...00000014010300
4202170438110039101000000...00000014010300
..................................................................
200202170440967946114000000...00000016010300
201202170440984046114000000...00000016010300
202202170441000046114000000...00000016010300
203202170441016046114000000...00000016010300
204202170441032046114000000...00000016010300
\n", "

205 rows × 22 columns

\n", "
" ], "text/plain": [ " inter_no end_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 \\\n", "0 202 1704380540 39 101 0 0 0 \n", "1 202 1704380680 39 101 0 0 0 \n", "2 202 1704380820 39 101 0 0 0 \n", "3 202 1704380960 39 101 0 0 0 \n", "4 202 1704381100 39 101 0 0 0 \n", ".. ... ... ... ... ... ... ... \n", "200 202 1704409679 46 114 0 0 0 \n", "201 202 1704409840 46 114 0 0 0 \n", "202 202 1704410000 46 114 0 0 0 \n", "203 202 1704410160 46 114 0 0 0 \n", "204 202 1704410320 46 114 0 0 0 \n", "\n", " dura_A6 dura_A7 dura_A8 ... dura_B3 dura_B4 dura_B5 dura_B6 \\\n", "0 0 0 0 ... 0 0 0 0 \n", "1 0 0 0 ... 0 0 0 0 \n", "2 0 0 0 ... 0 0 0 0 \n", "3 0 0 0 ... 0 0 0 0 \n", "4 0 0 0 ... 0 0 0 0 \n", ".. ... ... ... ... ... ... ... ... \n", "200 0 0 0 ... 0 0 0 0 \n", "201 0 0 0 ... 0 0 0 0 \n", "202 0 0 0 ... 0 0 0 0 \n", "203 0 0 0 ... 0 0 0 0 \n", "204 0 0 0 ... 0 0 0 0 \n", "\n", " dura_B7 dura_B8 cycle offset D_n S_n \n", "0 0 0 140 103 0 0 \n", "1 0 0 140 103 0 0 \n", "2 0 0 140 103 0 0 \n", "3 0 0 140 103 0 0 \n", "4 0 0 140 103 0 0 \n", ".. ... ... ... ... ... ... \n", "200 0 0 160 103 0 0 \n", "201 0 0 160 103 0 0 \n", "202 0 0 160 103 0 0 \n", "203 0 0 160 103 0 0 \n", "204 0 0 160 103 0 0 \n", "\n", "[205 rows x 22 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = 100\n", "fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", "present_time = fmins[m] # 현재시점\n", "\n", "# 1. 신호이력 데이터 수집\n", "rhistory = history.copy()[(history.end_unix < present_time)]\n", "rhistory = rhistory.reset_index(drop=True)\n", "hours = np.array(range(midnight, next_day + 1, 3600))\n", "rhist = rhistory.copy()[rhistory.inter_no == inter_no] # 특정한 inter_no\n", "rhist = rhist.reset_index(drop=True)\n", "rhist['D_n'] = 0 # D_n : 시간차이\n", "rhist['S_n'] = 0 # S_n : 현시시간합\n", "rhist" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-01-05 08:19:00\n", "2024-01-05 07:20:20\n" ] } ], "source": [ "movement = time2movement[present_time]\n", "max_unix = movement.start_unix.max()\n", "min_unix = movement.start_unix.min()\n", "print(datetime.fromtimestamp(max_unix))\n", "print(datetime.fromtimestamp(min_unix))" ] }, { "cell_type": "code", "execution_count": 25, "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", "
inter_nophas_Aphas_Bmove_Amove_Bstart_unix
020111831704406820
120122521704406820
220133621704406820
320144611704406820
420155741704406820
.....................
59820211621704410320
5992022217181704410320
60020111831704410340
60120122521704410340
60220155741704410340
\n", "

603 rows × 6 columns

\n", "
" ], "text/plain": [ " inter_no phas_A phas_B move_A move_B start_unix\n", "0 201 1 1 8 3 1704406820\n", "1 201 2 2 5 2 1704406820\n", "2 201 3 3 6 2 1704406820\n", "3 201 4 4 6 1 1704406820\n", "4 201 5 5 7 4 1704406820\n", ".. ... ... ... ... ... ...\n", "598 202 1 1 6 2 1704410320\n", "599 202 2 2 17 18 1704410320\n", "600 201 1 1 8 3 1704410340\n", "601 201 2 2 5 2 1704410340\n", "602 201 5 5 7 4 1704410340\n", "\n", "[603 rows x 6 columns]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", "present_time = fmins[m] # 현재시점\n", "print(datetime.fromtimestamp(present_time))\n", "\n", "Rhists = [] # Recent history (1시간 이내)\n", "for inter_no in history.inter_no.unique():\n", " # - 5분마다 신호이력 데이터 수집해서 통합테이블 생성할때\n", " # 1. 조회시점의 유닉스 타임을 기준으로 신호이력의 유닉스 타임이 1시간 이내인(Rhist) 데이터 수집\n", " rhistory = history.copy() # recent history\n", " rhistory = rhistory[(rhistory.end_unix < present_time)]\n", " hours = np.array(range(midnight, next_day + 1, 3600))\n", " rhist = rhistory.copy()[rhistory.inter_no == inter_no] # 특정한 inter_no\n", " rhist = rhist.reset_index(drop=True)\n", " new_rows = []\n", " # 1-1. 결측치 처리 : 인접한 두 end_unix의 차이가 계획된 주기의 두 배보다 크면 결측이 일어났다고 판단\n", " for n in range(len(rhist) - 1):\n", " curr_unix = rhist.iloc[n].end_unix # current end_unix\n", " next_unix = rhist.iloc[n+1].end_unix # next end_unix\n", " cycle = rhist.iloc[n].cycle\n", " if next_unix - curr_unix >= 2 * cycle:\n", " # 현재 unix를 계획된 주기만큼 늘려가면서 한 행씩 채워나간다.\n", " #(다음 unix와의 차이가 계획된 주기보다 작거나 같아질 때까지)\n", " while next_unix - curr_unix > cycle:\n", " curr_unix += cycle \n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)] # planned row\n", " prow = prow.drop(['start_hour', 'start_minute'], axis=1)\n", " prow['end_unix'] = curr_unix\n", " cycle = prow.iloc[0].cycle\n", " new_rows.append(prow)\n", " rhist = pd.concat([rhist] + new_rows).sort_values(['end_unix'])\n", " rhist = rhist.reset_index(drop=True)\n", "\n", " # 1-2. 이상치 처리 : 기준유닉스로부터의 시간차이와 현시시간합이 11 이상 차이나면 이상치가 발생했다고 판단\n", " Rhist = rhist.copy() # recent history 1704393231\n", " Rhist = Rhist[(Rhist.end_unix >= present_time - 3600)] # Recent history (1시간 이내)\n", " Rhist = Rhist.reset_index(drop=True)\n", " Rhist['D_n'] = 0\n", " Rhist['S_n'] = 0\n", " for n in range(len(Rhist)):\n", " curr_unix = Rhist.iloc[n].end_unix # current end_unix\n", " cycle = Rhist.iloc[n].cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " # S_n : 현시시간합\n", " S_n = ddurations.values.sum() // 2\n", " Rhist.loc[n, ['D_n', 'S_n']] = [D_n, S_n]\n", " n = 1\n", " while n < len(Rhist):\n", " prev_unix = Rhist[Rhist.index==n-1]['end_unix'].iloc[0] # previous end_unix\n", " curr_unix = Rhist[Rhist.index==n]['end_unix'].iloc[0] # current end_unix\n", " R_n = (curr_unix - prev_unix) / cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " # S_n : 현시시간합\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = ddurations.values.sum() // 2\n", " # 비율이 0.5보다 작거나 같으면 해당 행을 삭제\n", " if (abs(D_n - S_n) > 10) & (R_n <= 0.5):\n", " # print(\"lt\", inter_no, curr_unix, round(R_n,2), D_n, S_n)\n", " # display(Rhist.iloc[n])\n", " Rhist = Rhist.drop(index=n)\n", " n += 1\n", " # 행삭제에 따른 curr_unix, D_n, S_n 등 재정의\n", " if not Rhist[Rhist.index==n]['end_unix'].empty: # 마지막 행을 삭제하여 뒤의 행이 없을 때를 대비\n", " curr_unix = Rhist[Rhist.index==n]['end_unix'].iloc[0] # current end_unix\n", " R_n = (curr_unix - prev_unix) / cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " # S_n : 현시시간합\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = ddurations.values.sum() // 2\n", " # 비율이 0.5보다 크면 해당 행 조정 (비율을 유지한 채로 현시시간 대체)\n", " if (abs(D_n - S_n) > 10) & (R_n > 0.5):\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)].copy().reset_index(drop=True).iloc[0] # planned row\n", " adjusted_dur = prow[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] * R_n\n", " # 조정된 현시시간을 정수로 바꿈\n", " int_parts = adjusted_dur.apply(lambda x: int(x))\n", " frac_parts = adjusted_dur - int_parts\n", " difference = int(round(adjusted_dur.sum())) - int_parts.sum()\n", " # 소수 부분이 가장 큰 상위 'difference'개의 값에 대해 올림 처리\n", " for _ in range(difference):\n", " max_frac_index = frac_parts.idxmax()\n", " int_parts[max_frac_index] += 1\n", " frac_parts[max_frac_index] = 0 # 이미 처리된 항목은 0으로 설정\n", " Rhist.loc[n, [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] = int_parts.values\n", " Rhist.loc[n, 'cycle'] = int_parts.sum() // 2\n", " # print(\"gt\", inter_no, curr_unix, round(R_n,2), D_n, S_n)\n", " n += 1\n", " Rhist = Rhist.drop(columns=['offset', 'D_n', 'S_n'])\n", " Rhists.append(Rhist)\n", "Rhists = pd.concat(Rhists)\n", "Rhists = Rhists.sort_values(by=['end_unix', 'inter_no']).reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 16, "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", "
start_hourstart_minutestart_seconds
0001704380400
1701704405600
2901704412800
318301704447000
\n", "
" ], "text/plain": [ " start_hour start_minute start_seconds\n", "0 0 0 1704380400\n", "1 7 0 1704405600\n", "2 9 0 1704412800\n", "3 18 30 1704447000" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# split, isplit : A,B 분리 혹은 통합시 사용될 수 있는 딕셔너리\n", "splits = {} # splits maps (inter_no, start_hour, start_minute) to split\n", "for i, row in 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", " 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", " splits[(inter_no, start_hour, start_minute)][(phas_A, phas_B)] = k\n", "\n", "isplits = {} # the inverse of splits\n", "for i in splits:\n", " isplits[i] = {splits[i][k]:k for k in splits[i]} # isplit maps k to (phas_A, phas_B)\n", "\n", "# timetable\n", "timetable = plan[['start_hour', 'start_minute']].drop_duplicates()\n", "timetable['start_seconds'] = midnight + timetable['start_hour'] * 3600 + timetable['start_minute'] * 60\n", "timetable" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def make_rhistory(m:int):\n", " '''\n", " input : m\n", " - m ranges from 0 to 287, but 0 makes an error where 288 = 86400//300\n", " - present_time = fmins[m] : 현재시점\n", " + fmins[m-2] : 시뮬레이션 시작시점\n", " + fmins[m-1] : 시뮬레이션 종료시점\n", " output : rhistory\n", " - recent history\n", " - 현재시각(present_time) 이전 1시간 동안의 신호이력에 대하여 결측치 및 이상치를 처리한 결과\n", " - 교차로번호(inter_no), 종료유닉스(end_unix), 현시시간(dur_Aj, dur_Bj), 주기(cycle), 옵셋(offset)\n", " '''\n", " fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", " present_time = fmins[m] # 현재시점\n", " print(datetime.fromtimestamp(present_time))\n", "\n", " Rhists = [] # Recent history (1시간 이내)\n", " for inter_no in history.inter_no.unique():\n", " # - 5분마다 신호이력 데이터 수집해서 통합테이블 생성할때\n", " # 1. 조회시점의 유닉스 타임을 기준으로 신호이력의 유닉스 타임이 1시간 이내인(Rhist) 데이터 수집\n", " rhistory = history.copy() # recent history\n", " rhistory = rhistory[(rhistory.end_unix < present_time)]\n", " hours = np.array(range(midnight, next_day + 1, 3600))\n", " rhist = rhistory.copy()[rhistory.inter_no == inter_no] # 특정한 inter_no\n", " rhist = rhist.reset_index(drop=True)\n", " new_rows = []\n", " # 1-1. 결측치 처리 : 인접한 두 end_unix의 차이가 계획된 주기의 두 배보다 크면 결측이 일어났다고 판단\n", " for n in range(len(rhist) - 1):\n", " curr_unix = rhist.iloc[n].end_unix # current end_unix\n", " next_unix = rhist.iloc[n+1].end_unix # next end_unix\n", " cycle = rhist.iloc[n].cycle\n", " if next_unix - curr_unix >= 2 * cycle:\n", " # 현재 unix를 계획된 주기만큼 늘려가면서 한 행씩 채워나간다.\n", " #(다음 unix와의 차이가 계획된 주기보다 작거나 같아질 때까지)\n", " while next_unix - curr_unix > cycle:\n", " curr_unix += cycle\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)] # planned row\n", " prow = prow.drop(['start_hour', 'start_minute'], axis=1)\n", " prow['end_unix'] = curr_unix\n", " cycle = prow.iloc[0].cycle\n", " new_rows.append(prow)\n", " rhist = pd.concat([rhist] + new_rows).sort_values(['end_unix'])\n", " rhist = rhist.reset_index(drop=True)\n", "\n", " # 1-2. 이상치 처리 : 기준유닉스로부터의 시간차이와 현시시간합이 11 이상 차이나면 이상치가 발생했다고 판단\n", " Rhist = rhist.copy() # recent history 1704393231\n", " Rhist = Rhist[(Rhist.end_unix >= present_time - 3600)] # Recent history (1시간 이내)\n", " Rhist = Rhist.reset_index(drop=True)\n", " Rhist['D_n'] = 0\n", " Rhist['S_n'] = 0\n", " for n in range(len(Rhist)):\n", " curr_unix = Rhist.iloc[n].end_unix # current end_unix\n", " cycle = Rhist.iloc[n].cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " # S_n : 현시시간합\n", " S_n = ddurations.values.sum() // 2\n", " Rhist.loc[n, ['D_n', 'S_n']] = [D_n, S_n]\n", " n = 1\n", " while n < len(Rhist):\n", " prev_unix = Rhist[Rhist.index==n-1]['end_unix'].iloc[0] # previous end_unix\n", " curr_unix = Rhist[Rhist.index==n]['end_unix'].iloc[0] # current end_unix\n", " R_n = (curr_unix - prev_unix) / cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " # S_n : 현시시간합\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = ddurations.values.sum() // 2\n", " # 비율이 0.5보다 작거나 같으면 해당 행을 삭제\n", " if (abs(D_n - S_n) > 10) & (R_n <= 0.5):\n", " Rhist = Rhist.drop(index=n)\n", " n += 1\n", " # 행삭제에 따른 curr_unix, D_n, S_n 등 재정의\n", " if not Rhist[Rhist.index==n]['end_unix'].empty: # 마지막 행을 삭제하여 뒤의 행이 없을 때를 대비\n", " curr_unix = Rhist[Rhist.index==n]['end_unix'].iloc[0] # current end_unix\n", " R_n = (curr_unix - prev_unix) / cycle\n", " ghour_lt_curr_unix = hours[hours < curr_unix].max() # the greatest hour less than curr_unix\n", " end_unixes = rhist.end_unix.unique()\n", " end_unixes_lt_ghour = np.sort(end_unixes[end_unixes < ghour_lt_curr_unix]) # end unixes less than ghour_lt_end_unix\n", " base_unix = end_unixes_lt_ghour[-5] # 기준유닉스 : curr_unix보다 작은 hour 중에서 가장 큰 값으로부터 다섯 번째로 작은 end_unix\n", " # D_n : 시간차이\n", " D_n = curr_unix - base_unix\n", " # S_n : 현시시간합\n", " ddurations = rhist[(rhist.end_unix > base_unix) & (rhist.end_unix <= curr_unix)][[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " S_n = ddurations.values.sum() // 2\n", " # 비율이 0.5보다 크면 해당 행 조정 (비율을 유지한 채로 현시시간 대체)\n", " if (abs(D_n - S_n) > 10) & (R_n > 0.5):\n", " start_seconds = np.array(timetable.start_seconds)\n", " idx = (start_seconds <= curr_unix).sum() - 1\n", " start_hour = timetable.iloc[idx].start_hour\n", " start_minute = timetable.iloc[idx].start_minute\n", " prow = plan[(plan.inter_no==inter_no) & (plan.start_hour==start_hour) & (plan.start_minute==start_minute)].copy().reset_index(drop=True).iloc[0] # planned row\n", " adjusted_dur = prow[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] * R_n\n", " # 조정된 현시시간을 정수로 바꿈\n", " int_parts = adjusted_dur.apply(lambda x: int(x))\n", " frac_parts = adjusted_dur - int_parts\n", " difference = int(round(adjusted_dur.sum())) - int_parts.sum()\n", " # 소수 부분이 가장 큰 상위 'difference'개의 값에 대해 올림 처리\n", " for _ in range(difference):\n", " max_frac_index = frac_parts.idxmax()\n", " int_parts[max_frac_index] += 1\n", " frac_parts[max_frac_index] = 0 # 이미 처리된 항목은 0으로 설정\n", " Rhist.loc[n, [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]] = int_parts.values\n", " Rhist.loc[n, 'cycle'] = int_parts.sum() // 2\n", " n += 1\n", " Rhist = Rhist.drop(columns=['offset', 'D_n', 'S_n'])\n", " Rhists.append(Rhist)\n", " Rhists = pd.concat(Rhists)\n", " Rhists = Rhists.sort_values(by=['end_unix', 'inter_no']).reset_index(drop=True)\n", " return Rhists" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-01-05 08:20:00\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_noend_unixdura_A1dura_A2dura_A3dura_A4dura_A5dura_A6dura_A7dura_A8dura_B1dura_B2dura_B3dura_B4dura_B5dura_B6dura_B7dura_B8cycle
0201170440682030361858180003036185818000160
11771704406830402571340000402571340000170
22101704406850433965230000245865230000170
31751704406870404255330000404229590000170
417617044069103793400000037934000000170
............................................................
1461751704410269404255330000404229590000170
14717617044103103793400000037934000000170
1481781704410310383942410000383962210000160
14920217044103204611400000046114000000160
150201170441034030361858180003036185818000160
\n", "

151 rows × 19 columns

\n", "
" ], "text/plain": [ " inter_no end_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 \\\n", "0 201 1704406820 30 36 18 58 18 \n", "1 177 1704406830 40 25 71 34 0 \n", "2 210 1704406850 43 39 65 23 0 \n", "3 175 1704406870 40 42 55 33 0 \n", "4 176 1704406910 37 93 40 0 0 \n", ".. ... ... ... ... ... ... ... \n", "146 175 1704410269 40 42 55 33 0 \n", "147 176 1704410310 37 93 40 0 0 \n", "148 178 1704410310 38 39 42 41 0 \n", "149 202 1704410320 46 114 0 0 0 \n", "150 201 1704410340 30 36 18 58 18 \n", "\n", " dura_A6 dura_A7 dura_A8 dura_B1 dura_B2 dura_B3 dura_B4 dura_B5 \\\n", "0 0 0 0 30 36 18 58 18 \n", "1 0 0 0 40 25 71 34 0 \n", "2 0 0 0 24 58 65 23 0 \n", "3 0 0 0 40 42 29 59 0 \n", "4 0 0 0 37 93 40 0 0 \n", ".. ... ... ... ... ... ... ... ... \n", "146 0 0 0 40 42 29 59 0 \n", "147 0 0 0 37 93 40 0 0 \n", "148 0 0 0 38 39 62 21 0 \n", "149 0 0 0 46 114 0 0 0 \n", "150 0 0 0 30 36 18 58 18 \n", "\n", " dura_B6 dura_B7 dura_B8 cycle \n", "0 0 0 0 160 \n", "1 0 0 0 170 \n", "2 0 0 0 170 \n", "3 0 0 0 170 \n", "4 0 0 0 170 \n", ".. ... ... ... ... \n", "146 0 0 0 170 \n", "147 0 0 0 170 \n", "148 0 0 0 160 \n", "149 0 0 0 160 \n", "150 0 0 0 160 \n", "\n", "[151 rows x 19 columns]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rhists = make_rhistory(100)\n", "Rhists" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def make_histid(m):\n", " fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", " present_time = fmins[m]\n", " rhistory = make_rhistory(m)\n", " # 2. 시작 유닉스 타임컬럼 생성 후 종류 유닉스 타임에서 현시별 현시기간 컬럼의 합을 뺀 값으로 입력\n", " # - 현시시간의 합을 뺀 시간의 +- 10초 이내에 이전 주기정보가 존재하면 그 유닉스 시간을 시작 유닉스시간 값으로 하고, 존재하지 않으면 현시시간의 합을 뺀 유닉스 시간을 시작 유닉스 시간으로 지정\n", " for i, row in rhistory.iterrows():\n", " # 이전 유닉스 존재하지 않음 => 현시시간 합의 차\n", " # 이전 유닉스 존재, abs < 10 => 이전 유닉스\n", " # 이전 유닉스 존재, abs >=10 => 현시시간 합의 차\n", " inter_no = row.inter_no\n", " end_unix = row.end_unix\n", " elapsed_time = row[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]].sum() // 2 # 현시시간 합\n", " start_unix = end_unix - elapsed_time\n", " pre_rows = history[:i] # previous rows\n", " if inter_no in pre_rows.inter_no.unique(): # 이전 유닉스 존재\n", " pre_unix = pre_rows[pre_rows.inter_no == inter_no]['end_unix'].iloc[-1] # previous unix time\n", " if abs(pre_unix - start_unix) < 10: # abs < 10\n", " start_unix = pre_unix\n", " else: # abs >= 10\n", " pass\n", " rhistory.loc[i, 'start_unix'] = start_unix\n", " rhistory[rhistory.isna()] = 0\n", " rhistory['start_unix'] = rhistory['start_unix'].astype(int)\n", " # # with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n", " # # display(rhistory)\n", " rhistory = rhistory[['inter_no', 'start_unix'] + [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)] + ['cycle']]\n", "\n", " # 계층화된 형태로 변환\n", " hrhistory = [] # hierarchied recent history\n", " for i, row in rhistory.iterrows():\n", " inter_no = row.inter_no\n", " start_unix = row.start_unix\n", "\n", " ind = (timetable['start_seconds'] <= row.start_unix).sum() - 1\n", " start_hour = timetable.iloc[ind].start_hour\n", " start_minute = timetable.iloc[ind].start_minute\n", " isplit = isplits[(inter_no, start_hour, start_minute)]\n", " phas_As = [isplit[j][0] for j in isplit.keys()]\n", " phas_Bs = [isplit[j][1] for j in isplit.keys()]\n", " durs_A = row[[f'dura_A{j}' for j in range(1,9)]]\n", " durs_B = row[[f'dura_B{j}' for j in range(1,9)]]\n", " durations = []\n", " for j in range(1, len(isplit)+1):\n", " ja = isplit[j][0]\n", " jb = isplit[j][1]\n", " if ja == jb:\n", " durations.append(min(durs_A[ja-1], durs_B[jb-1]))\n", " else:\n", " durations.append(abs(durs_A[ja-1] - durs_B[ja-1]))\n", " new_rows = pd.DataFrame({'inter_no':[inter_no] * len(durations), 'start_unix':[start_unix] * len(durations),\n", " 'phas_A':phas_As, 'phas_B':phas_Bs, 'duration':durations})\n", " hrhistory.append(new_rows)\n", " hrhistory = pd.concat(hrhistory)\n", " hrhistory = hrhistory.sort_values(by = ['start_unix', 'inter_no', 'phas_A', 'phas_B']).reset_index(drop=True)\n", "\n", " # 5초단위로 수집한 이동류정보(time2movement[present_time])와 최근 1시간 신호이력(hrhistory)을 병합\n", " movedur = pd.merge(time2movement[present_time], hrhistory, how='inner', on=['inter_no', 'start_unix', 'phas_A', 'phas_B']) # movements and durations\n", " movedur = movedur.sort_values(by=['start_unix', 'inter_no', 'phas_A','phas_B'])\n", " movedur = movedur[['inter_no', 'start_unix', 'phas_A', 'phas_B', 'move_A', 'move_B', 'duration']]\n", "\n", " # 이동류 매칭 테이블에서 진입id, 진출id를 가져와서 붙임.\n", " for i, row in movedur.iterrows():\n", " inter_no = row.inter_no\n", " start_unix = row.start_unix\n", " # incoming and outgoing edges A\n", " move_A = row.move_A\n", " if move_A in [17, 18]:\n", " inc_edge_A = np.nan\n", " out_edge_A = np.nan\n", " else:\n", " match_A = matching[(matching.inter_no == inter_no) & (matching.move_no == move_A)].iloc[0]\n", " inc_edge_A = match_A.inc_edge\n", " out_edge_A = match_A.out_edge\n", " movedur.loc[i, ['inc_edge_A', 'out_edge_A']] = [inc_edge_A, out_edge_A]\n", " # incoming and outgoing edges B\n", " move_B = row.move_B\n", " if move_B in [17, 18]:\n", " inc_edge_B = np.nan\n", " out_edge_B = np.nan\n", " else:\n", " match_B = matching[(matching.inter_no == inter_no) & (matching.move_no == move_B)].iloc[0]\n", " inc_edge_B = match_B.inc_edge\n", " out_edge_B = match_B.out_edge\n", " movedur.loc[i, ['inc_edge_B', 'out_edge_B']] = [inc_edge_B, out_edge_B]\n", "\n", " # 이동류 컬럼 제거\n", " movedur = movedur.drop(['move_A', 'move_B'], axis=1)\n", "\n", " histid = movedur.copy() # history with edge ids (incoming and outgoing edge ids)\n", " histid['node_id'] = histid['inter_no'].map(inter2node)\n", " histid = histid[['inter_no', 'node_id', 'start_unix', 'phas_A', 'phas_B', 'duration', 'inc_edge_A', 'out_edge_A', 'inc_edge_B', 'out_edge_B']]\n", " histid = histid[histid.start_unix > present_time - 3600]\n", " # 시뮬레이션 시작시각 : 현재시각 - 600\n", " # 시뮬레이션 종료시각 : 현재시각 - 300\n", " # 현재시각 : present_time, PT\n", " # PT-900 ... PT-600 ... PT-300 ... PT\n", " return histid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = 120\n", "fmins = range(midnight, next_day, 300) # fmins : unix time by Five MINuteS\n", "present_time = fmins[m]\n", "rhistory = make_rhistory(m)\n", "# 2. 시작 유닉스 타임컬럼 생성 후 종류 유닉스 타임에서 현시별 현시기간 컬럼의 합을 뺀 값으로 입력\n", "# - 현시시간의 합을 뺀 시간의 +- 10초 이내에 이전 주기정보가 존재하면 그 유닉스 시간을 시작 유닉스시간 값으로 하고, 존재하지 않으면 현시시간의 합을 뺀 유닉스 시간을 시작 유닉스 시간으로 지정\n", "for i, row in rhistory.iterrows():\n", " # 이전 유닉스 존재하지 않음 => 현시시간 합의 차\n", " # 이전 유닉스 존재, abs < 10 => 이전 유닉스\n", " # 이전 유닉스 존재, abs >=10 => 현시시간 합의 차\n", " inter_no = row.inter_no\n", " end_unix = row.end_unix\n", " elapsed_time = row[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]].sum() // 2 # 현시시간 합\n", " start_unix = end_unix - elapsed_time\n", " pre_rows = history[:i] # previous rows\n", " if inter_no in pre_rows.inter_no.unique(): # 이전 유닉스 존재\n", " pre_unix = pre_rows[pre_rows.inter_no == inter_no]['end_unix'].iloc[-1] # previous unix time\n", " if abs(pre_unix - start_unix) < 10: # abs < 10\n", " start_unix = pre_unix\n", " else: # abs >= 10\n", " pass\n", " rhistory.loc[i, 'start_unix'] = start_unix\n", "rhistory[rhistory.isna()] = 0\n", "rhistory['start_unix'] = rhistory['start_unix'].astype(int)\n", "# # with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n", "# # display(rhistory)\n", "rhistory = rhistory[['inter_no', 'start_unix'] + [f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)] + ['cycle']]\n", "\n", "# rhistoryy = rhistory.copy()[rhistory.inter_no==175]\n", "# rhistoryy['diff'] = rhistoryy['start_unix'].diff()\n", "\n", "# 계층화된 형태로 변환\n", "hrhistory = [] # hierarchied recent history\n", "for i, row in rhistory.iterrows():\n", " inter_no = row.inter_no\n", " start_unix = row.start_unix\n", "\n", " ind = (timetable['start_seconds'] <= row.start_unix).sum() - 1\n", " start_hour = timetable.iloc[ind].start_hour\n", " start_minute = timetable.iloc[ind].start_minute\n", " isplit = isplits[(inter_no, start_hour, start_minute)]\n", " phas_As = [isplit[j][0] for j in isplit.keys()]\n", " phas_Bs = [isplit[j][1] for j in isplit.keys()]\n", " durs_A = row[[f'dura_A{j}' for j in range(1,9)]]\n", " durs_B = row[[f'dura_B{j}' for j in range(1,9)]]\n", " durations = []\n", " for j in range(1, len(isplit)+1):\n", " ja = isplit[j][0]\n", " jb = isplit[j][1]\n", " if ja == jb:\n", " durations.append(min(durs_A[ja-1], durs_B[jb-1]))\n", " else:\n", " durations.append(abs(durs_A[ja-1] - durs_B[ja-1]))\n", " new_rows = pd.DataFrame({'inter_no':[inter_no] * len(durations), 'start_unix':[start_unix] * len(durations),\n", " 'phas_A':phas_As, 'phas_B':phas_Bs, 'duration':durations})\n", " hrhistory.append(new_rows)\n", "hrhistory = pd.concat(hrhistory)\n", "hrhistory = hrhistory.sort_values(by = ['start_unix', 'inter_no', 'phas_A', 'phas_B']).reset_index(drop=True)\n", "\n", "hrhistoryy = rhistory.copy()[rhistory.inter_no==175]\n", "hrhistoryy['diff'] = hrhistoryy['start_unix'].diff()\n", "\n", "# display(hrhistoryy[:60])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for inter_no in sorted(plan.inter_no.unique()):\n", " print(inter_no)\n", " movement = time2movement[present_time]\n", " movementt = movement.copy()[movement.inter_no==inter_no]\n", " hrhistoryy = rhistory.copy()[rhistory.inter_no==inter_no]\n", " mdts = sorted(movementt.start_unix.unique())\n", " hdts = sorted(hrhistoryy.start_unix.unique())\n", " import matplotlib.pyplot as plt\n", " plt.scatter(mdts, [0]*len(mdts), c='b')\n", " plt.scatter(hdts, [1]*len(hdts), c='r')\n", " plt.show()\n", " # display(movementt)\n", " # display(hrhistoryy)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 5초단위로 수집한 이동류정보(time2movement[present_time])와 최근 1시간 신호이력(hrhistory)을 병합\n", "movedur = pd.merge(time2movement[present_time], hrhistory, how='inner', on=['inter_no', 'start_unix', 'phas_A', 'phas_B']) # movements and durations\n", "movedur = movedur.sort_values(by=['start_unix', 'inter_no', 'phas_A','phas_B'])\n", "movedur = movedur[['inter_no', 'start_unix', 'phas_A', 'phas_B', 'move_A', 'move_B', 'duration']]\n", "\n", "# 이동류 매칭 테이블에서 진입id, 진출id를 가져와서 붙임.\n", "for i, row in movedur.iterrows():\n", " inter_no = row.inter_no\n", " start_unix = row.start_unix\n", " # incoming and outgoing edges A\n", " move_A = row.move_A\n", " if move_A in [17, 18]:\n", " inc_edge_A = np.nan\n", " out_edge_A = np.nan\n", " else:\n", " match_A = matching[(matching.inter_no == inter_no) & (matching.move_no == move_A)].iloc[0]\n", " inc_edge_A = match_A.inc_edge\n", " out_edge_A = match_A.out_edge\n", " movedur.loc[i, ['inc_edge_A', 'out_edge_A']] = [inc_edge_A, out_edge_A]\n", " # incoming and outgoing edges B\n", " move_B = row.move_B\n", " if move_B in [17, 18]:\n", " inc_edge_B = np.nan\n", " out_edge_B = np.nan\n", " else:\n", " match_B = matching[(matching.inter_no == inter_no) & (matching.move_no == move_B)].iloc[0]\n", " inc_edge_B = match_B.inc_edge\n", " out_edge_B = match_B.out_edge\n", " movedur.loc[i, ['inc_edge_B', 'out_edge_B']] = [inc_edge_B, out_edge_B]\n", "\n", "# 이동류 컬럼 제거\n", "movedur = movedur.drop(['move_A', 'move_B'], axis=1)\n", "\n", "histid = movedur.copy() # history with edge ids (incoming and outgoing edge ids)\n", "histid['node_id'] = histid['inter_no'].map(inter2node)\n", "histid = histid[['inter_no', 'node_id', 'start_unix', 'phas_A', 'phas_B', 'duration', 'inc_edge_A', 'out_edge_A', 'inc_edge_B', 'out_edge_B']]\n", "histid = histid[histid.start_unix > present_time - 3600]\n", "# 시뮬레이션 시작시각 : 현재시각 - 600\n", "# 시뮬레이션 종료시각 : 현재시각 - 300\n", "# 현재시각 : present_time, PT\n", "# PT-900 ... PT-600 ... PT-300 ... PT\n", "\n", "histidd = histid.copy()[histid.inter_no==175]\n", "histidd['diff'] = histidd['start_unix'].diff()\n", "histidd[:60]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-01-05 08:20:00\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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_idstart_unixphas_Aphas_Bdurationinc_edge_Aout_edge_Ainc_edge_Bout_edge_B
0177i217044068301140-571542809_01571542811_01571542811_02571542809_01
1177i217044068302225571542811_02571542107_01-571542809_01571542809_01
2177i217044068303371NaNNaNNaNNaN
3177i217044068304434-571542809_01571542811_01571542107_02571542809_01
4175i017044068701140-571542797_02571500487_01-571500487_01571542797_02
5175i017044068702242-571500487_01571545870_01-571542797_02571510153_01
6175i017044068703329571545870_02571510153_01571545870_02571542797_02
7175i017044068703426571545870_02571510153_01571510153_02571545870_01
8175i017044068704433571510153_02571500487_01571510153_02571545870_01
9176i117044069101137-571542810_01-571542797_02.99571542797_02.99571542810_01
10176i117044069102293-571542810_01-571542797_02.99-571542810_01571543469_01
11176i117044069103340571543469_02-571542797_02.99NaNNaN
12178i317044069501138571540304_02571556450_01571556450_02571540304_01
13178i317044069502239571556450_02571500475_01571540304_02571540303_01
14178i317044069503342571540303_02.21571556450_01571540303_02.21571500475_01
15178i317044069504421-571500475_01571540303_01-571500475_01571540304_01
16202i917044069501146571510152_02-571510152_01571510152_01571510152_01.65
17202i9170440695022114NaNNaNNaNNaN
18177i217044070001140-571542809_01571542811_01571542811_02571542809_01
19177i217044070002225571542811_02571542107_01-571542809_01571542809_01
20177i217044070003371NaNNaNNaNNaN
21177i217044070004434-571542809_01571542811_01571542107_02571542809_01
22175i017044070401140-571542797_02571500487_01-571500487_01571542797_02
23175i017044070402242-571500487_01571545870_01-571542797_02571510153_01
24175i017044070403329571545870_02571510153_01571545870_02571542797_02
25175i017044070403426571545870_02571510153_01571510153_02571545870_01
26175i017044070404433571510153_02571500487_01571510153_02571545870_01
27176i117044070801137-571542810_01-571542797_02.99571542797_02.99571542810_01
28176i117044070802293-571542810_01-571542797_02.99-571542810_01571543469_01
29176i117044070803340571543469_02-571542797_02.99NaNNaN
\n", "
" ], "text/plain": [ " inter_no node_id start_unix phas_A phas_B duration inc_edge_A \\\n", "0 177 i2 1704406830 1 1 40 -571542809_01 \n", "1 177 i2 1704406830 2 2 25 571542811_02 \n", "2 177 i2 1704406830 3 3 71 NaN \n", "3 177 i2 1704406830 4 4 34 -571542809_01 \n", "4 175 i0 1704406870 1 1 40 -571542797_02 \n", "5 175 i0 1704406870 2 2 42 -571500487_01 \n", "6 175 i0 1704406870 3 3 29 571545870_02 \n", "7 175 i0 1704406870 3 4 26 571545870_02 \n", "8 175 i0 1704406870 4 4 33 571510153_02 \n", "9 176 i1 1704406910 1 1 37 -571542810_01 \n", "10 176 i1 1704406910 2 2 93 -571542810_01 \n", "11 176 i1 1704406910 3 3 40 571543469_02 \n", "12 178 i3 1704406950 1 1 38 571540304_02 \n", "13 178 i3 1704406950 2 2 39 571556450_02 \n", "14 178 i3 1704406950 3 3 42 571540303_02.21 \n", "15 178 i3 1704406950 4 4 21 -571500475_01 \n", "16 202 i9 1704406950 1 1 46 571510152_02 \n", "17 202 i9 1704406950 2 2 114 NaN \n", "18 177 i2 1704407000 1 1 40 -571542809_01 \n", "19 177 i2 1704407000 2 2 25 571542811_02 \n", "20 177 i2 1704407000 3 3 71 NaN \n", "21 177 i2 1704407000 4 4 34 -571542809_01 \n", "22 175 i0 1704407040 1 1 40 -571542797_02 \n", "23 175 i0 1704407040 2 2 42 -571500487_01 \n", "24 175 i0 1704407040 3 3 29 571545870_02 \n", "25 175 i0 1704407040 3 4 26 571545870_02 \n", "26 175 i0 1704407040 4 4 33 571510153_02 \n", "27 176 i1 1704407080 1 1 37 -571542810_01 \n", "28 176 i1 1704407080 2 2 93 -571542810_01 \n", "29 176 i1 1704407080 3 3 40 571543469_02 \n", "\n", " out_edge_A inc_edge_B out_edge_B \n", "0 571542811_01 571542811_02 571542809_01 \n", "1 571542107_01 -571542809_01 571542809_01 \n", "2 NaN NaN NaN \n", "3 571542811_01 571542107_02 571542809_01 \n", "4 571500487_01 -571500487_01 571542797_02 \n", "5 571545870_01 -571542797_02 571510153_01 \n", "6 571510153_01 571545870_02 571542797_02 \n", "7 571510153_01 571510153_02 571545870_01 \n", "8 571500487_01 571510153_02 571545870_01 \n", "9 -571542797_02.99 571542797_02.99 571542810_01 \n", "10 -571542797_02.99 -571542810_01 571543469_01 \n", "11 -571542797_02.99 NaN NaN \n", "12 571556450_01 571556450_02 571540304_01 \n", "13 571500475_01 571540304_02 571540303_01 \n", "14 571556450_01 571540303_02.21 571500475_01 \n", "15 571540303_01 -571500475_01 571540304_01 \n", "16 -571510152_01 571510152_01 571510152_01.65 \n", "17 NaN NaN NaN \n", "18 571542811_01 571542811_02 571542809_01 \n", "19 571542107_01 -571542809_01 571542809_01 \n", "20 NaN NaN NaN \n", "21 571542811_01 571542107_02 571542809_01 \n", "22 571500487_01 -571500487_01 571542797_02 \n", "23 571545870_01 -571542797_02 571510153_01 \n", "24 571510153_01 571545870_02 571542797_02 \n", "25 571510153_01 571510153_02 571545870_01 \n", "26 571500487_01 571510153_02 571545870_01 \n", "27 -571542797_02.99 571542797_02.99 571542810_01 \n", "28 -571542797_02.99 -571542810_01 571543469_01 \n", "29 -571542797_02.99 NaN NaN " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_histid(100)[:30]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inter_no = 175\n", "rhist = make_histid(100)\n", "rhis = rhist.copy()[rhist.inter_no==inter_no]\n", "rhis['diff'] = rhis['start_unix'].diff()\n", "rhis[:60]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inter_no = 175\n", "histid = make_histid(100)\n", "his = histid.copy()[histid.inter_no==inter_no]\n", "his['diff'] = his['start_unix'].diff()\n", "his[:60]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for m in range(30, 288):\n", " print(m)\n", " make_histid(m).to_csv(f'../../Data/tables/histids/histids_{fmins[m]}.csv')" ] } ], "metadata": { "kernelspec": { "display_name": "rts", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 2 }