Browse Source

integration test. Added to functionalities : (1) avoid all-red (2) generate alert in the case of improper pairs

master
김선중 1 year ago
parent
commit
73c087f4e4
11 changed files with 1160 additions and 3125 deletions
  1. +1
    -0
      .vscode/launch.json
  2. BIN
      Scripts/__pycache__/generate_signals.cpython-312.pyc
  3. BIN
      Scripts/__pycache__/preprocess_daily.cpython-312.pyc
  4. +28
    -3
      Scripts/generate_signals.py
  5. +56
    -3
      Scripts/preprocess_daily.py
  6. +10
    -2461
      analysis/0725_main_test/4_use_class_pd.ipynb
  7. +64
    -658
      analysis/0725_main_test/6_use_class_gs.ipynb
  8. +560
    -0
      analysis/0725_main_test/7_history_nonexist.ipynb
  9. +142
    -0
      analysis/0801_check_tll/1_all_red.ipynb
  10. +297
    -0
      analysis/0801_check_tll/2_improper_pairs.ipynb
  11. +2
    -0
      test_0731/results/sn_1722384300_test.add.xml

+ 1
- 0
.vscode/launch.json View File

@ -1,6 +1,7 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",

BIN
Scripts/__pycache__/generate_signals.cpython-312.pyc View File


BIN
Scripts/__pycache__/preprocess_daily.cpython-312.pyc View File


+ 28
- 3
Scripts/generate_signals.py View File

@ -7,7 +7,7 @@ from tqdm import tqdm
from datetime import datetime, timedelta
class SignalGenerator():
def __init__(self, config_name='test_0721', month=7, day=22, hour=9, minute=25):
def __init__(self, config_name='test_0731', month=7, day=31, hour=9, minute=5):
self.config_name = config_name
self.month, self.day, self.hour, self.minute = month, day, hour, minute
@ -1006,8 +1006,33 @@ class SignalGenerator():
csig = pd.concat(csig).reset_index(drop=True)
return csig
# 5-3. 신호파일 생성
# 5-3. 부적절하게 연속된 신호조합 체크
def find_impropers(self):
improper_pairs = dict()
improper_pairs['ry'] = []
improper_pairs['Gr'] = []
improper_pairs['yG'] = []
for node_id, group in self.SIGTABLE.groupby('node_id'):
state_length = len(group.iloc[0].state)
# 상태를 리스트로 변환
state_lists = group['state'].apply(list)
# 새로운 열들을 한 번에 추가
cnxnwises = {str(j): [state[j] for state in state_lists] for j in range(state_length)}
for state_order in cnxnwises:
cnxnwise = cnxnwises[state_order]
for phase_order, (p, q) in enumerate(zip(cnxnwise[:-1], cnxnwise[1:])):
if (p, q) == ('r', 'y'):
improper_pairs['ry'].append({'node_id':node_id, 'state_order':state_order, 'phase_order':phase_order})
if (p, q) == ('G', 'r'):
improper_pairs['Gr'].append({'node_id':node_id, 'state_order':state_order, 'phase_order':phase_order})
if (p, q) == ('y', 'G'):
improper_pairs['ry'].append({'node_id':node_id, 'state_order':state_order, 'phase_order':phase_order})
if any(value for value in improper_pairs.values()):
print('부적절하게 연속된 신호조합(r->y, G->r, y->G)이 존재합니다 :')
print(improper_pairs)
# 5-4. 신호파일 생성
def make_tl_file(self):
strings = ['<additional>\n']
for node_id, group in self.SIGTABLE.groupby('node_id'):

+ 56
- 3
Scripts/preprocess_daily.py View File

@ -451,6 +451,7 @@ class DailyPreprocessor():
self.initialize_state()
self.assign_indices()
self.assign_signals()
self.avoid_all_reds()
self.save_intermediates()
# 2-1 매칭테이블 생성
@ -1102,7 +1103,58 @@ class DailyPreprocessor():
self.matching.to_csv(os.path.join(self.path_intermediates, 'matching.csv'), index=0)
print('2-4. 직진 및 좌회전(G)을 배정했습니다.')
# 2-5 기반파일 저장
# 2-5 모든 현시에서 적색신호인 경우는 상시 s로 정의
def avoid_all_reds(self):
'''
s에
'green right-turn arrow' requires stopping - vehicles may pass the junction if no vehicle uses a higher priorised foe stream. They always stop before passing. This is only generated for junction type traffic_light_right_on_red.
https://sumo.dlr.de/docs/Simulation/Traffic_Lights.html
'''
# match6에 적용
for node_id, group in self.match6.groupby('node_id'):
state_length = len(group.iloc[0].state)
consistent_length = all([len(state)==state_length for state in group.state])
# state 길이 일정 여부
if not consistent_length:
print(f"node_id : {node_id}에 대하여 state 길이가 일정하지 않습니다.")
any_non_red_indices = set()
for state in group.state:
state = list(state)
any_non_red_indices.update({index for index, char in enumerate(state) if not char == 'r'})
# 모든 현시에서 적색신호인 인덱스 목록
all_red_indices = set(range(state_length)) - any_non_red_indices
for i, row in group.iterrows():
state = list(row.state)
for index in all_red_indices:
state[index] = 's'
state = ''.join(state)
self.match6.at[i, 'state'] = state
# matching에 적용
for node_id, group in self.matching.groupby('node_id'):
state_length = len(group.iloc[0].state)
consistent_length = all([len(state)==state_length for state in group.state])
# state 길이 일정 여부
if not consistent_length:
print(f"node_id : {node_id}에 대하여 state 길이가 일정하지 않습니다.")
any_non_red_indices = set()
for state in group.state:
state = list(state)
any_non_red_indices.update({index for index, char in enumerate(state) if not char == 'r'})
# 모든 현시에서 적색신호인 인덱스 목록
all_red_indices = set(range(state_length)) - any_non_red_indices
for i, row in group.iterrows():
state = list(row.state)
for index in all_red_indices:
state[index] = 's'
state = ''.join(state)
self.matching.at[i, 'state'] = state
print("2-5. 모든 현시에서 적색신호인 경우에 대한 처리 완료")
# 2-6 기반파일 저장
def save_intermediates(self):
# 신호계획 저장
self.plan.to_csv(os.path.join(self.path_tables, 'plan.csv'), index=0)
@ -1117,8 +1169,9 @@ class DailyPreprocessor():
node2num_cycles = {node_id : inter2num_cycles[self.node2inter[node_id]] for node_id in self.node_ids}
with open(os.path.join(self.path_intermediates,'node2num_cycles.json'), 'w') as file:
json.dump(node2num_cycles, file, indent=4)
print("2-5. node2num_cycles.json를 저장했습니다.")
print("2-6. node2num_cycles.json를 저장했습니다.")
# 3. 이슈사항 저장
def write_issues(self):
print('3. 이슈사항을 저장합니다.')

+ 10
- 2461
analysis/0725_main_test/4_use_class_pd.ipynb
File diff suppressed because it is too large
View File


+ 64
- 658
analysis/0725_main_test/6_use_class_gs.ipynb View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@ -33,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@ -72,7 +72,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@ -91,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@ -100,7 +100,7 @@
"1722384300"
]
},
"execution_count": 51,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@ -111,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -277,7 +277,7 @@
"[11100 rows x 7 columns]"
]
},
"execution_count": 52,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@ -302,7 +302,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@ -311,7 +311,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@ -336,7 +336,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@ -730,7 +730,7 @@
"8 457 3 3 0 17 17 1722384232"
]
},
"execution_count": 55,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@ -776,7 +776,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@ -942,7 +942,7 @@
"[11100 rows x 7 columns]"
]
},
"execution_count": 56,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@ -953,7 +953,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@ -976,7 +976,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@ -1812,7 +1812,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@ -1988,7 +1988,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@ -2009,7 +2009,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@ -2213,7 +2213,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@ -2237,7 +2237,7 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@ -2250,7 +2250,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@ -2267,7 +2267,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@ -2284,7 +2284,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@ -2309,7 +2309,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
@ -2329,7 +2329,7 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@ -2346,7 +2346,7 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
@ -2376,47 +2376,25 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.history.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2. 신호이력 테이블을 변환합니다.\n"
]
},
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"self.process_history()\n",
"sorted(inter_no for inter_no in set(self.rhistory.inter_no))"
@ -2424,67 +2402,27 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.rhists.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.hrhists.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3. 이동류정보 테이블을 변환합니다.\n"
]
},
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"self.process_movement()\n",
"sorted(inter_no for inter_no in set(self.movement.inter_no))"
@ -2492,382 +2430,79 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4. 통합 테이블을 생성합니다.\n"
]
}
],
"outputs": [],
"source": [
"self.make_histids()"
]
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.movement_updated.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.movedur.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.histid.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['106231', '106234', '106332', '108769', '109836', '109901', '109986']"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(node_id for node_id in set(self.histid.node_id))"
]
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[436, 437, 442, 443, 455, 456, 457]"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(inter_no for inter_no in set(self.histids.inter_no))"
]
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['106231',\n",
" '106234',\n",
" '106332',\n",
" '107587',\n",
" '108769',\n",
" '109295',\n",
" '109296',\n",
" '109297',\n",
" '109313',\n",
" '109333',\n",
" '109836',\n",
" '109901',\n",
" '109986']"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted(node_id for node_id in set(self.histids.node_id))"
]
},
{
"cell_type": "code",
"execution_count": 82,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>inter_no</th>\n",
" <th>node_id</th>\n",
" <th>start_unix</th>\n",
" <th>phas_A</th>\n",
" <th>phas_B</th>\n",
" <th>move_A</th>\n",
" <th>move_B</th>\n",
" <th>duration</th>\n",
" <th>state_A</th>\n",
" <th>state_B</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>457</td>\n",
" <td>106234</td>\n",
" <td>1722383873</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>6</td>\n",
" <td>2</td>\n",
" <td>45</td>\n",
" <td>gGGGrgrrrrgrrrrgrrr</td>\n",
" <td>grrrrgrrrrgGGGrgrrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>457</td>\n",
" <td>106234</td>\n",
" <td>1722383873</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>20</td>\n",
" <td>grrrrgrrrrgrrrGgrrr</td>\n",
" <td>grrrGgrrrrgrrrrgrrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>457</td>\n",
" <td>106234</td>\n",
" <td>1722383873</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>40</td>\n",
" <td>grrrrgrrrrgrrrrgrrr</td>\n",
" <td>grrrrgrrrrgrrrrgrrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>457</td>\n",
" <td>106234</td>\n",
" <td>1722383873</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>8</td>\n",
" <td>3</td>\n",
" <td>35</td>\n",
" <td>grrrrgGGrrgrrrrgrrr</td>\n",
" <td>grrrrgrrGGgrrrrgrrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>457</td>\n",
" <td>106234</td>\n",
" <td>1722383873</td>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>7</td>\n",
" <td>4</td>\n",
" <td>40</td>\n",
" <td>grrrrgrrrrgrrrrgrrG</td>\n",
" <td>grrrrgrrrrgrrrrgGGr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>290</th>\n",
" <td>443</td>\n",
" <td>109333</td>\n",
" <td>1722384700</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>7</td>\n",
" <td>18</td>\n",
" <td>49</td>\n",
" <td>GGGGGGGGGGGr</td>\n",
" <td>GGGGGGGGGGGr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>291</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>6</td>\n",
" <td>2</td>\n",
" <td>71</td>\n",
" <td>gGGGGrgrrgrrrrrgrr</td>\n",
" <td>grrrrrgrrgGGGGrgrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>292</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>28</td>\n",
" <td>grrrrrgrrgrrrrGgrr</td>\n",
" <td>grrrrGgrrgrrrrrgrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>293</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>8</td>\n",
" <td>3</td>\n",
" <td>58</td>\n",
" <td>grrrrrgGrgrrrrrgrr</td>\n",
" <td>grrrrrgrGgrrrrrgrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>294</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>7</td>\n",
" <td>4</td>\n",
" <td>43</td>\n",
" <td>grrrrrgrrgrrrrrgrG</td>\n",
" <td>grrrrrgrrgrrrrrgGr</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>295 rows × 10 columns</p>\n",
"</div>"
],
"text/plain": [
" inter_no node_id start_unix phas_A phas_B move_A move_B duration \\\n",
"0 457 106234 1722383873 1 1 6 2 45 \n",
"1 457 106234 1722383873 2 2 5 1 20 \n",
"2 457 106234 1722383873 3 3 17 17 40 \n",
"3 457 106234 1722383873 4 4 8 3 35 \n",
"4 457 106234 1722383873 5 5 7 4 40 \n",
".. ... ... ... ... ... ... ... ... \n",
"290 443 109333 1722384700 3 3 7 18 49 \n",
"291 437 109986 1722384700 1 1 6 2 71 \n",
"292 437 109986 1722384700 2 2 5 1 28 \n",
"293 437 109986 1722384700 3 3 8 3 58 \n",
"294 437 109986 1722384700 4 4 7 4 43 \n",
"\n",
" state_A state_B \n",
"0 gGGGrgrrrrgrrrrgrrr grrrrgrrrrgGGGrgrrr \n",
"1 grrrrgrrrrgrrrGgrrr grrrGgrrrrgrrrrgrrr \n",
"2 grrrrgrrrrgrrrrgrrr grrrrgrrrrgrrrrgrrr \n",
"3 grrrrgGGrrgrrrrgrrr grrrrgrrGGgrrrrgrrr \n",
"4 grrrrgrrrrgrrrrgrrG grrrrgrrrrgrrrrgGGr \n",
".. ... ... \n",
"290 GGGGGGGGGGGr GGGGGGGGGGGr \n",
"291 gGGGGrgrrgrrrrrgrr grrrrrgrrgGGGGrgrr \n",
"292 grrrrrgrrgrrrrGgrr grrrrGgrrgrrrrrgrr \n",
"293 grrrrrgGrgrrrrrgrr grrrrrgrGgrrrrrgrr \n",
"294 grrrrrgrrgrrrrrgrG grrrrrgrrgrrrrrgGr \n",
"\n",
"[295 rows x 10 columns]"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"self.histids"
]
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -2876,238 +2511,9 @@
},
{
"cell_type": "code",
"execution_count": 84,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>inter_no</th>\n",
" <th>node_id</th>\n",
" <th>start_unix</th>\n",
" <th>phas_A</th>\n",
" <th>phas_B</th>\n",
" <th>move_A</th>\n",
" <th>move_B</th>\n",
" <th>duration</th>\n",
" <th>state_A</th>\n",
" <th>state_B</th>\n",
" <th>phase_sumo</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>456</td>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>15</td>\n",
" <td>grgrrrrG</td>\n",
" <td>grgrrGGr</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>456</td>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>6</td>\n",
" <td>2</td>\n",
" <td>28</td>\n",
" <td>grgGGrrr</td>\n",
" <td>grgrrGGr</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>456</td>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>7</td>\n",
" <td>7</td>\n",
" <td>12</td>\n",
" <td>gGgrrrrr</td>\n",
" <td>gGgrrrrr</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>456</td>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>35</td>\n",
" <td>grgrrrrr</td>\n",
" <td>grgrrrrr</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>456</td>\n",
" <td>106231</td>\n",
" <td>1722384057</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>15</td>\n",
" <td>grgrrrrG</td>\n",
" <td>grgrrGGr</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>153</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384007</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>7</td>\n",
" <td>4</td>\n",
" <td>49</td>\n",
" <td>grrrrrgrrgrrrrrgrG</td>\n",
" <td>grrrrrgrrgrrrrrgGr</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>154</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>6</td>\n",
" <td>2</td>\n",
" <td>71</td>\n",
" <td>gGGGGrgrrgrrrrrgrr</td>\n",
" <td>grrrrrgrrgGGGGrgrr</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>155</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>28</td>\n",
" <td>grrrrrgrrgrrrrGgrr</td>\n",
" <td>grrrrGgrrgrrrrrgrr</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>156</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>8</td>\n",
" <td>3</td>\n",
" <td>58</td>\n",
" <td>grrrrrgGrgrrrrrgrr</td>\n",
" <td>grrrrrgrGgrrrrrgrr</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>157</th>\n",
" <td>437</td>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>7</td>\n",
" <td>4</td>\n",
" <td>43</td>\n",
" <td>grrrrrgrrgrrrrrgrG</td>\n",
" <td>grrrrrgrrgrrrrrgGr</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>158 rows × 11 columns</p>\n",
"</div>"
],
"text/plain": [
" inter_no node_id start_unix phas_A phas_B move_A move_B duration \\\n",
"0 456 106231 1722383968 1 1 5 2 15 \n",
"1 456 106231 1722383968 2 2 6 2 28 \n",
"2 456 106231 1722383968 3 3 7 7 12 \n",
"3 456 106231 1722383968 4 4 17 17 35 \n",
"4 456 106231 1722384057 1 1 5 2 15 \n",
".. ... ... ... ... ... ... ... ... \n",
"153 437 109986 1722384007 4 4 7 4 49 \n",
"154 437 109986 1722384700 1 1 6 2 71 \n",
"155 437 109986 1722384700 2 2 5 1 28 \n",
"156 437 109986 1722384700 3 3 8 3 58 \n",
"157 437 109986 1722384700 4 4 7 4 43 \n",
"\n",
" state_A state_B phase_sumo \n",
"0 grgrrrrG grgrrGGr 0 \n",
"1 grgGGrrr grgrrGGr 1 \n",
"2 gGgrrrrr gGgrrrrr 2 \n",
"3 grgrrrrr grgrrrrr 3 \n",
"4 grgrrrrG grgrrGGr 0 \n",
".. ... ... ... \n",
"153 grrrrrgrrgrrrrrgrG grrrrrgrrgrrrrrgGr 3 \n",
"154 gGGGGrgrrgrrrrrgrr grrrrrgrrgGGGGrgrr 0 \n",
"155 grrrrrgrrgrrrrGgrr grrrrGgrrgrrrrrgrr 1 \n",
"156 grrrrrgGrgrrrrrgrr grrrrrgrGgrrrrrgrr 2 \n",
"157 grrrrrgrrgrrrrrgrG grrrrrgrrgrrrrrgGr 3 \n",
"\n",
"[158 rows x 11 columns]"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"self.sigtable"
]

+ 560
- 0
analysis/0725_main_test/7_history_nonexist.ipynb View File

@ -0,0 +1,560 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"hour=9,\n",
"minute=3 (더 정확하게는 minute <= 4)\n",
"\n",
"입력 시 make_rhists에서 에러가 나고 있음.\n",
"이때의 에러는, 특정 교차로에 대하여 history 데이터가 없기 때문으로 추정됨.\n",
"\n",
"특정 교차로에 대하여 history가 없더라도 에러가 나지 않도록 디버깅하려고 시도했으나, 일단 잘 되지 않았음. 나중으로 미룸 (2024. 8. 2. 09:27)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import os, sys, json, argparse, pickle\n",
"import sumolib, traci\n",
"from tqdm import tqdm\n",
"from datetime import datetime\n",
"path_root = os.path.dirname(os.path.dirname(os.path.abspath('.')))\n",
"path_scr = os.path.join(path_root, 'scripts')\n",
"sys.path.append(path_scr)\n",
"from generate_signals import SignalGenerator"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. 데이터를 준비합니다.\n",
"1-1. 네트워크가 로드되었습니다.\n",
"1-2. 테이블들이 로드되었습니다.\n",
"1-5. 필요한 보조 객체들이 모두 준비되었습니다.\n"
]
}
],
"source": [
"self = SignalGenerator(config_name='test_0731',\n",
" month=7,\n",
" day=31,\n",
" hour=9,\n",
" minute=3)\n",
"self.prepare_data()\n",
"self.make_rhistory()\n",
"# display(self.rhistory.head())\n",
"# self.make_rhists()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[436, 437, 438, 442, 443, 455, 456, 457, 458]\n"
]
}
],
"source": [
"print(self.inter_nos)\n",
"inter_no = 457"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1722384000\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>end_unix</th>\n",
" <th>inter_no</th>\n",
" <th>dura_A1</th>\n",
" <th>dura_A2</th>\n",
" <th>dura_A3</th>\n",
" <th>dura_A4</th>\n",
" <th>dura_A5</th>\n",
" <th>dura_A6</th>\n",
" <th>dura_A7</th>\n",
" <th>dura_A8</th>\n",
" <th>dura_B1</th>\n",
" <th>dura_B2</th>\n",
" <th>dura_B3</th>\n",
" <th>dura_B4</th>\n",
" <th>dura_B5</th>\n",
" <th>dura_B6</th>\n",
" <th>dura_B7</th>\n",
" <th>dura_B8</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1722384411</td>\n",
" <td>457</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>1722384773</td>\n",
" <td>457</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>1722384591</td>\n",
" <td>457</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>1722384053</td>\n",
" <td>457</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <td>1722384232</td>\n",
" <td>457</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43</th>\n",
" <td>1722384953</td>\n",
" <td>457</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" end_unix inter_no dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 \\\n",
"5 1722384411 457 45 20 40 40 35 \n",
"11 1722384773 457 45 20 40 40 35 \n",
"26 1722384591 457 45 20 40 40 35 \n",
"33 1722384053 457 45 20 40 35 40 \n",
"36 1722384232 457 45 20 40 40 35 \n",
"43 1722384953 457 45 20 40 40 35 \n",
"\n",
" dura_A6 dura_A7 dura_A8 dura_B1 dura_B2 dura_B3 dura_B4 dura_B5 \\\n",
"5 0 0 0 45 20 40 40 35 \n",
"11 0 0 0 45 20 40 40 35 \n",
"26 0 0 0 45 20 40 40 35 \n",
"33 0 0 0 45 20 40 35 40 \n",
"36 0 0 0 45 20 40 40 35 \n",
"43 0 0 0 45 20 40 40 35 \n",
"\n",
" dura_B6 dura_B7 dura_B8 \n",
"5 0 0 0 \n",
"11 0 0 0 \n",
"26 0 0 0 \n",
"33 0 0 0 \n",
"36 0 0 0 \n",
"43 0 0 0 "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(self.present_time)\n",
"self.history[self.history.inter_no==457]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>inter_no</th>\n",
" <th>start_unix</th>\n",
" <th>dura_A1</th>\n",
" <th>dura_A2</th>\n",
" <th>dura_A3</th>\n",
" <th>dura_A4</th>\n",
" <th>dura_A5</th>\n",
" <th>dura_A6</th>\n",
" <th>dura_A7</th>\n",
" <th>dura_A8</th>\n",
" <th>dura_B1</th>\n",
" <th>dura_B2</th>\n",
" <th>dura_B3</th>\n",
" <th>dura_B4</th>\n",
" <th>dura_B5</th>\n",
" <th>dura_B6</th>\n",
" <th>dura_B7</th>\n",
" <th>dura_B8</th>\n",
" <th>cycle</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>457</td>\n",
" <td>1722384400</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>180.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>457</td>\n",
" <td>1722384400</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>180.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>457</td>\n",
" <td>1722384400</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>20</td>\n",
" <td>40</td>\n",
" <td>40</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>180.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 dura_A6 \\\n",
"0 457 1722384400 45 20 40 35 40 0 \n",
"0 457 1722384400 45 20 40 35 40 0 \n",
"0 457 1722384400 45 20 40 40 35 0 \n",
"\n",
" dura_A7 dura_A8 dura_B1 dura_B2 dura_B3 dura_B4 dura_B5 dura_B6 \\\n",
"0 0 0 45 20 40 35 40 0 \n",
"0 0 0 45 20 40 35 40 0 \n",
"0 0 0 45 20 40 40 35 0 \n",
"\n",
" dura_B7 dura_B8 cycle \n",
"0 0 0 180.0 \n",
"0 0 0 180.0 \n",
"0 0 0 180.0 "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"self.rhistory[self.rhistory.inter_no==457]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"self.make_rhists()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rhistory = self.history[self.history.inter_no==inter_no]\n",
"rhistory = rhistory[(rhistory.end_unix <= self.present_time) & (rhistory.end_unix > self.present_time - self.subtractor)]\n",
"rhistory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# rhistory에 모든 교차로번호가 존재하지 않으면 해당 교차로번호에 대한 신호이력을 추가함 (at 최근 프로그램 시작시각)\n",
"inter_no = inter_no\n",
"program_start, prow = self.load_prow(inter_no, self.present_time - self.subtractor)\n",
"cycle = prow.cycle.iloc[0]\n",
"row1 = prow.copy()\n",
"row2 = prow.copy()\n",
"# prow에서 필요한 부분을 rhistory에 추가\n",
"row1['end_unix'] = program_start\n",
"row2['end_unix'] = program_start + cycle\n",
"display(row1)\n",
"display(row2)\n",
"rhistory = pd.concat([rhistory, row1, row2])#.reset_index(drop=True)\n",
"rhistory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# present_time + adder 의 시각에 한 주기의 신호 추가\n",
"program_start, prow = self.load_prow(inter_no, self.present_time)\n",
"cycle = prow.cycle.iloc[0]\n",
"row3 = prow.copy()\n",
"# # prow에서 필요한 부분을 rhistory에 추가\n",
"row3['end_unix'] = self.present_time + self.adder\n",
"rhistory = pd.concat([rhistory, row3])#.reset_index(drop=True)\n",
"rhistory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(rhistory)\n",
"i = 0\n",
"row = rhistory.iloc[i]\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 = self.history[:i] # previous rows\n",
"print(inter_no in pre_rows.inter_no.unique())\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\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"self.inter_nos"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "siggen_env",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

+ 142
- 0
analysis/0801_check_tll/1_all_red.ipynb View File

@ -0,0 +1,142 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import os, sys, copy, argparse, json, pickle\n",
"import sumolib, traci\n",
"from tqdm import tqdm\n",
"from datetime import datetime\n",
"path_root = os.path.dirname(os.path.dirname(os.path.abspath('.')))\n",
"path_scr = os.path.join(path_root, 'scripts')\n",
"sys.path.append(path_scr)\n",
"from preprocess_daily import DailyPreprocessor\n",
"# from generate_signals import SignalGenerator"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"self = DailyPreprocessor(config_name = 'test_0731')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. 데이터를 로드합니다.\n",
"1-1. 네트워크가 로드되었습니다.\n",
"1-2. 테이블들이 로드되었습니다.\n",
"1-5. 테이블을 표준화했습니다.\n",
"1-6. 주요 객체 (리스트, 딕셔너리)들을 저장했습니다.\n"
]
}
],
"source": [
"# 1. 데이터 불러오기\n",
"self.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2. 중간산출물을 생성합니다.\n",
"2-1. 매칭 테이블들을 생성했습니다.\n",
"2-2. 초기화 신호가 지정되었습니다. (우회전 : g)\n",
"2-3. 유턴 인덱스 / 비보호좌회전 인덱스를 지정했습니다.\n",
"2-4. 직진 및 좌회전(G)을 배정했습니다.\n",
"2-5. 모든 현시에서 적색신호인 경우에 대한 처리 완료\n",
"2-6. node2num_cycles.json를 저장했습니다.\n"
]
}
],
"source": [
"self.get_intermediates()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"106231 set()\n",
"106234 set()\n",
"106238 set()\n",
"106332 set()\n",
"106350 set()\n",
"107587 set()\n",
"108769 set()\n",
"109295 set()\n",
"109296 set()\n",
"109297 set()\n",
"109313 set()\n",
"109333 set()\n",
"109836 set()\n",
"109901 set()\n",
"109986 set()\n"
]
}
],
"source": [
"# match6에 적용\n",
"for node_id, group in self.match6.groupby('node_id'):\n",
" state_length = len(group.iloc[0].state)\n",
" consistent_length = all([len(state)==state_length for state in group.state])\n",
" # state 길이 일정 여부\n",
" if not consistent_length:\n",
" print(f\"node_id : {node_id}에 대하여 state 길이가 일정하지 않습니다.\")\n",
" any_non_red_indices = set()\n",
" for state in group.state:\n",
" state = list(state)\n",
" any_non_red_indices.update({index for index, char in enumerate(state) if not char == 'r'})\n",
" # 모든 현시에서 적색신호인 인덱스 목록\n",
" all_red_indices = set(range(state_length)) - any_non_red_indices\n",
" print(node_id, all_red_indices)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "siggen_env",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

+ 297
- 0
analysis/0801_check_tll/2_improper_pairs.ipynb View File

@ -0,0 +1,297 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import os, sys, json, argparse, pickle\n",
"import sumolib, traci\n",
"from tqdm import tqdm\n",
"from datetime import datetime, timedelta\n",
"path_root = os.path.dirname(os.path.dirname(os.path.abspath('.')))\n",
"path_scr = os.path.join(path_root, 'scripts')\n",
"sys.path.append(path_scr)\n",
"# from preprocess_daily import DailyPreprocessor\n",
"from generate_signals import SignalGenerator"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"self = SignalGenerator()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. 데이터를 준비합니다.\n",
"1-1. 네트워크가 로드되었습니다.\n",
"1-2. 테이블들이 로드되었습니다.\n",
"1-5. 필요한 보조 객체들이 모두 준비되었습니다.\n",
"2. 신호이력 테이블을 변환합니다.\n",
"3. 이동류정보 테이블을 변환합니다.\n",
"4. 통합 테이블을 생성합니다.\n"
]
}
],
"source": [
"self.prepare_data()\n",
"self.process_history()\n",
"self.process_movement()\n",
"self.make_histids()\n",
"self.set_timepoints()\n",
"self.assign_red_yellow()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>node_id</th>\n",
" <th>start_unix</th>\n",
" <th>phase</th>\n",
" <th>duration</th>\n",
" <th>state</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>1g_1g</td>\n",
" <td>11</td>\n",
" <td>grgrrGGG</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>1y_1y</td>\n",
" <td>4</td>\n",
" <td>grgrrGGy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>2g_2g</td>\n",
" <td>24</td>\n",
" <td>grgGGGGr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>2y_2y</td>\n",
" <td>4</td>\n",
" <td>grgyyyyr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>106231</td>\n",
" <td>1722383968</td>\n",
" <td>3g_3g</td>\n",
" <td>8</td>\n",
" <td>gGgrrrrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>250</th>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>1g_1g</td>\n",
" <td>67</td>\n",
" <td>gGGGGrgrrgGGGGrgrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>251</th>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>1y_1y</td>\n",
" <td>4</td>\n",
" <td>gyyyyrgrrgyyyyrgrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>252</th>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>2g_2g</td>\n",
" <td>24</td>\n",
" <td>grrrrGgrrgrrrrGgrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>253</th>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>2y_2y</td>\n",
" <td>4</td>\n",
" <td>grrrrygrrgrrrrygrr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>254</th>\n",
" <td>109986</td>\n",
" <td>1722384700</td>\n",
" <td>3g_3g</td>\n",
" <td>54</td>\n",
" <td>grrrrrgGGgrrrrrgrr</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>255 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" node_id start_unix phase duration state\n",
"0 106231 1722383968 1g_1g 11 grgrrGGG\n",
"1 106231 1722383968 1y_1y 4 grgrrGGy\n",
"2 106231 1722383968 2g_2g 24 grgGGGGr\n",
"3 106231 1722383968 2y_2y 4 grgyyyyr\n",
"4 106231 1722383968 3g_3g 8 gGgrrrrr\n",
".. ... ... ... ... ...\n",
"250 109986 1722384700 1g_1g 67 gGGGGrgrrgGGGGrgrr\n",
"251 109986 1722384700 1y_1y 4 gyyyyrgrrgyyyyrgrr\n",
"252 109986 1722384700 2g_2g 24 grrrrGgrrgrrrrGgrr\n",
"253 109986 1722384700 2y_2y 4 grrrrygrrgrrrrygrr\n",
"254 109986 1722384700 3g_3g 54 grrrrrgGGgrrrrrgrr\n",
"\n",
"[255 rows x 5 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"self.SIGTABLE = self.SIGTABLE.reset_index(drop=True)\n",
"self.SIGTABLE"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(0, 1): 1,\n",
" (1, 2): 3,\n",
" (2, 3): 5,\n",
" (3, 4): 7,\n",
" (4, 5): 9,\n",
" (5, 6): 11,\n",
" (6, 7): 13,\n",
" (7, 8): 15,\n",
" (8, 9): 17}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = range(10)\n",
"B = dict()\n",
"for a, b in zip(A[:-1], A[1:]):\n",
" B[a, b] = a + b\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"improper_pairs = dict()\n",
"improper_pairs['ry'] = []\n",
"improper_pairs['Gr'] = []\n",
"improper_pairs['yG'] = []\n",
"for node_id, group in self.SIGTABLE.groupby('node_id'):\n",
" state_length = len(group.iloc[0].state)\n",
" # 상태를 리스트로 변환\n",
" state_lists = group['state'].apply(list)\n",
" # 새로운 열들을 한 번에 추가\n",
" cnxnwises = {str(j): [state[j] for state in state_lists] for j in range(state_length)}\n",
" for state_order in cnxnwises:\n",
" cnxnwise = cnxnwises[state_order]\n",
" for phase_order, (p, q) in enumerate(zip(cnxnwise[:-1], cnxnwise[1:])):\n",
" if (p, q) == ('r', 'y'):\n",
" improper_pairs['ry'].append({'node_id':node_id, 'state_order':state_order, 'phase_order':phase_order})\n",
" if (p, q) == ('G', 'r'):\n",
" improper_pairs['Gr'].append({'node_id':node_id, 'state_order':state_order, 'phase_order':phase_order})\n",
" if (p, q) == ('y', 'G'):\n",
" improper_pairs['ry'].append({'node_id':node_id, 'state_order':state_order, 'phase_order':phase_order})\n",
"if any(value for value in improper_pairs.values()):\n",
" print('부적절하게 연속된 신호조합(r->y, G->r, y->G)이 존재합니다 :')\n",
" print(improper_pairs)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "siggen_env",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

+ 2
- 0
test_0731/results/sn_1722384300_test.add.xml View File

@ -0,0 +1,2 @@
<additional>
</additional>

Loading…
Cancel
Save