{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "from datetime import datetime\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "now = int(datetime.now().timestamp())\n", "oml = int(datetime.now().timestamp()) + 60 # one minute later" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
start_unixnode_iddphasedurasignal
01704384400i001grrgrrrgrrgrrr
11704384400i0132gGGgrrrgrrgrrr
21704384400i024gyygrrrgrrgrrr
31704384400i031grrgrrrgrrgrrr
41704384400i0434grrgGGGgrrgrrr
51704384400i054grrgyyygrrgrrr
61704384400i061grrgrrrgrrgrrr
71704384400i0725grrgrrrgGGgrrr
81704384400i084grrgrrrgyygrrr
91704384400i091grrgrrrgrrgrrr
101704384400i01029grrgrrrgrrgGGG
111704384400i0114grrgrrrgrrgyyy
\n", "
" ], "text/plain": [ " start_unix node_id dphase dura signal\n", "0 1704384400 i0 0 1 grrgrrrgrrgrrr\n", "1 1704384400 i0 1 32 gGGgrrrgrrgrrr\n", "2 1704384400 i0 2 4 gyygrrrgrrgrrr\n", "3 1704384400 i0 3 1 grrgrrrgrrgrrr\n", "4 1704384400 i0 4 34 grrgGGGgrrgrrr\n", "5 1704384400 i0 5 4 grrgyyygrrgrrr\n", "6 1704384400 i0 6 1 grrgrrrgrrgrrr\n", "7 1704384400 i0 7 25 grrgrrrgGGgrrr\n", "8 1704384400 i0 8 4 grrgrrrgyygrrr\n", "9 1704384400 i0 9 1 grrgrrrgrrgrrr\n", "10 1704384400 i0 10 29 grrgrrrgrrgGGG\n", "11 1704384400 i0 11 4 grrgrrrgrrgyyy" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one_cycle" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "now = int(datetime.now().timestamp()) // 10 * 10\n", "oml = now + 60 # one minute later\n", "one_cycle = pd.read_csv('one_cycle.csv')\n", "one_cycle['start_unix'] = oml\n", "cycle = one_cycle.dura.sum()\n", "# display(one_cycle)\n", "cur_time = one_cycle.start_unix.unique()[0]\n", "sev_cycle = [] # several cycles\n", "for i in range(5):\n", " cur_cycle = one_cycle.copy()\n", " cur_time = cur_time + cycle\n", " cur_cycle['start_unix'] = cur_time\n", " sev_cycle.append(cur_cycle)\n", "sev_cycle = pd.concat(sev_cycle).reset_index(drop=True)\n", "sev_cycle.to_csv(\"sev_cycle.csv\")" ] }, { "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", "
start_unixnode_iddphasedurasignal
01704384400i001grrgrrrgrrgrrr
11704384400i0132gGGgrrrgrrgrrr
21704384400i024gyygrrrgrrgrrr
31704384400i031grrgrrrgrrgrrr
41704384400i0434grrgGGGgrrgrrr
51704384400i054grrgyyygrrgrrr
61704384400i061grrgrrrgrrgrrr
71704384400i0725grrgrrrgGGgrrr
81704384400i084grrgrrrgyygrrr
91704384400i091grrgrrrgrrgrrr
101704384400i01029grrgrrrgrrgGGG
111704384400i0114grrgrrrgrrgyyy
\n", "
" ], "text/plain": [ " start_unix node_id dphase dura signal\n", "0 1704384400 i0 0 1 grrgrrrgrrgrrr\n", "1 1704384400 i0 1 32 gGGgrrrgrrgrrr\n", "2 1704384400 i0 2 4 gyygrrrgrrgrrr\n", "3 1704384400 i0 3 1 grrgrrrgrrgrrr\n", "4 1704384400 i0 4 34 grrgGGGgrrgrrr\n", "5 1704384400 i0 5 4 grrgyyygrrgrrr\n", "6 1704384400 i0 6 1 grrgrrrgrrgrrr\n", "7 1704384400 i0 7 25 grrgrrrgGGgrrr\n", "8 1704384400 i0 8 4 grrgrrrgyygrrr\n", "9 1704384400 i0 9 1 grrgrrrgrrgrrr\n", "10 1704384400 i0 10 29 grrgrrrgrrgGGG\n", "11 1704384400 i0 11 4 grrgrrrgrrgyyy" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "from xml.etree import ElementTree as ET\n", "\n", "one_cycle = pd.read_csv('one_cycle.csv')\n", "display(one_cycle)\n", "\n", "net_xml_path = 'example.net.xml'\n", "tree = ET.parse(net_xml_path)\n", "root = tree.getroot()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# Function to create a traffic light phase element\n", "def create_phase_element(duration, state):\n", " phase_elem = ET.Element(\"phase\")\n", " phase_elem.set(\"duration\", str(duration))\n", " phase_elem.set(\"state\", state)\n", " return phase_elem\n", "\n", "# Create the traffic light logic container\n", "tlLogic = ET.Element(\"tlLogic\")\n", "tlLogic.set(\"id\", \"i0\")\n", "tlLogic.set(\"type\", \"static\")\n", "tlLogic.set(\"programID\", \"1\")\n", "tlLogic.set(\"offset\", \"0\")\n", "\n", "# Add phases from the CSV data to the traffic light logic\n", "for index, row in one_cycle.iterrows():\n", " phase_elem = create_phase_element(row['dura'], row['signal'])\n", " tlLogic.append(phase_elem)\n", "\n", "# Add the traffic light logic to the junction element if not already present\n", "# First, check if a tlLogic is already defined for junction i0 and remove it\n", "for tl in root.findall(\"./tlLogic[@id='i0']\"):\n", " root.remove(tl)\n", "\n", "# Now, insert the new traffic light logic\n", "root.insert(0, tlLogic)\n", "\n", "# Write the modified XML back to a new file\n", "modified_net_xml_path = 'modified_example.net.xml'\n", "tree.write(modified_net_xml_path)\n", "\n", "modified_net_xml_path # Return the path of the modified net.xml file for download" ] } ], "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 }