Skip to content

Commit

Permalink
Add --inifile option and refactor main simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
chraibi committed Oct 19, 2024
1 parent 77aaae3 commit 121542d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion files/inifile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"version": 0.1,
"init_trajectories_file": "../trajectories_croma/1C060_cam6_cam5_frameshift0_Combined.txt",
"init_positions_file": "../trajectories_croma/1C060_frame_3951.csv",
"init_positions_file": "../trajectories_croma/1C060_frame_5350.csv",
"velocity_init_parameters": {
"a_ped": 0.1,
"d_ped": 0.4,
Expand Down
32 changes: 18 additions & 14 deletions positions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
" \"POLYGON ((-8.88 -7.63, 8.3 -7.63, 8.3 27.95, -8.88 27.95, -8.88 -7.63), (-3.54 -1.13, -3.57 19.57, -1.52 19.57, -1.37 19.71, -1.37 21.09, -1.52 21.23, -1.67 21.23, -1.67 21.18, -1.545 21.18, -1.4200000000000002 21.065, -1.4200000000000002 19.735, -1.545 19.62, -3.6199999999999997 19.62, -3.59 -1.13, -3.54 -1.13), (3.57 -0.89, 3.64 19.64, 1.47 19.57, 1.32 19.71, 1.32 21.09, 1.47 21.23, 1.62 21.23, 1.62 21.18, 1.4949999999999999 21.18, 1.37 21.065, 1.37 19.735, 1.4949999999999999 19.62, 3.69 19.69, 3.6199999999999997 -0.89, 3.57 -0.89), (0.67 19.57, 0.82 19.71, 0.82 21.09, 0.67 21.23, 0.38 21.23, 0.23 21.09, 0.23 19.71, 0.38 19.57, 0.67 19.57), (-0.42 19.57, -0.27 19.71, -0.27 21.09, -0.42 21.23, -0.72 21.23, -0.87 21.09, -0.87 19.71, -0.72 19.57, -0.42 19.57))\"\n",
")\n",
"filenames = [\n",
" \"../trajectories_croma/1C060_cam6_cam5_frameshift0_Combined.txt\",\n",
" # \"../trajectories_croma/1C070_cam6_cam5_frameshift0_Combined.txt\",\n",
" # \"../trajectories_croma/2C070_cam6_cam5_frameshift0_Combined.txt\",\n",
" # \"../trajectories_croma/2C120_cam6_cam5_frameshift0_Combined.txt\",\n",
" # \"../trajectories_croma/2C130_cam6_cam5_frameshift0_Combined.txt\",\n",
" # \"../trajectories_croma/2C150_cam6_cam5_frameshift0_Combined.txt\",\n",
" #\"../trajectories_croma/1C060_cam6_cam5_frameshift0_Combined.txt\",\n",
" #\"../trajectories_croma/1C070_cam6_cam5_frameshift0_Combined.txt\",\n",
" \"../trajectories_croma/2C070_cam6_cam5_frameshift0_Combined.txt\",\n",
" #\"../trajectories_croma/2C120_cam6_cam5_frameshift0_Combined.txt\",\n",
" #\"../trajectories_croma/2C130_cam6_cam5_frameshift0_Combined.txt\",\n",
" #\"../trajectories_croma/2C150_cam6_cam5_frameshift0_Combined.txt\",\n",
"]\n",
"\n",
"\n",
Expand All @@ -48,10 +48,12 @@
" return data\n",
"\n",
"\n",
"def get_first_frame_pedestrian_passes_line(filename: str, passing_line_y: float=19.71):\n",
"def get_first_frame_pedestrian_passes_line(filename: str, passing_line_y: float=20):\n",
" \"\"\"\n",
" Return the first frame when a pedestrian passes the specified horizontal line.\n",
" Also return the DataFrame of whole trajectories.\n",
" 60 -> 19.71\n",
" 70 -> \n",
" \"\"\"\n",
" df = pd.read_csv(\n",
" filename, sep=\"\\t\", names=[\"id\", \"frame\", \"x\", \"y\", \"z\", \"m\"], comment=\"#\"\n",
Expand Down Expand Up @@ -84,8 +86,9 @@
" num_frames=num_frames,\n",
" )\n",
" plot_walkable_area(geometry)\n",
" # plot_doors_and_destinations(inifile)\n",
" plot_doors_and_destinations(inifile)\n",
" set_plot_properties(title)\n",
" plt.savefig(f\"files/figures/{title}.pdf\")\n",
" plt.show()\n",
"\n",
"\n",
Expand Down Expand Up @@ -166,7 +169,7 @@
"def plot_doors_and_destinations(inifile: str):\n",
" \"\"\"Plot doors and destinations from the inifile.\"\"\"\n",
" data = read_json_file(inifile)\n",
" plot_doors(data)\n",
" #plot_doors(data)\n",
" plot_destinations(data)\n",
"\n",
"\n",
Expand Down Expand Up @@ -377,7 +380,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -402,7 +405,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -425,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -479,13 +482,14 @@
"metadata": {},
"outputs": [],
"source": [
"print(filenames[0])\n",
"df, frame_after_decrease = get_first_frame_pedestrian_passes_line(filenames[0])\n",
"walkable_area = pedpy.WalkableArea(geometry)\n",
"df['ox'] = 0.0\n",
"df['oy'] = 0.1\n",
"delta_frame = 50\n",
"delta_frame = 20\n",
"start = frame_after_decrease - delta_frame\n",
"end = frame_after_decrease + delta_frame\n",
"end = frame_after_decrease + 3*delta_frame\n",
"df = df[(df['frame'] >= start) & (df['frame'] <= end)]\n",
"trajectory_data = pedpy.TrajectoryData(df, frame_rate=50)\n",
"animate(trajectory_data, walkable_area, every_nth_frame=1)\n"
Expand Down
2 changes: 1 addition & 1 deletion positions.org
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def process_file(filename: str, geometry: shapely.geometry.Polygon) -> None:

** Main function processing the files

#+begin_src python -n :results output :exports {both} :wrap results :hlines yes :python ~/.venvs/pedpy/bin/python :session imports :var figname="plot.png"
#+begin_src python -n :results output :exports pboth :wrap results :hlines yes :python ~/.venvs/pedpy/bin/python :session imports :var figname="plot.png"
for filename in filenames[0:1]:
figname = process_file(filename, geometry)
#+end_src
Expand Down
38 changes: 22 additions & 16 deletions simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pathlib
import time
from typing import Any, Dict, Iterator, List, Tuple, TypeAlias
import typer

import jupedsim as jps
from jupedsim.distributions import distribute_by_number
Expand Down Expand Up @@ -394,7 +395,7 @@ def read_positions_from_csv(file_path="points.csv"):
return tuple_list


def main(
def init_and_run_simulation(
_number_agents: int,
_fps: int,
_time_step: float,
Expand Down Expand Up @@ -473,7 +474,7 @@ def start_simulation(config_path, output_path):
simulation_time = parse_simulation_time(data)
dummy = ""
if fps and time_step:
evac_time = main(
evac_time = init_and_run_simulation(
number_agents,
fps,
time_step,
Expand All @@ -499,29 +500,30 @@ def modify_and_save_config(base_config, modification_dict, new_config_path):
json.dump(config, f, ensure_ascii=False, indent=4)


if __name__ == "__main__":
def main(
inifile: pathlib.Path = typer.Option(
pathlib.Path("files/inifile.json"),
help="Path to the initial configuration file",
),
):
"""Implement Main function. Create variations and start simulations."""
init_logger()
base_config = "files/inifile.json"
logging.info(f"{base_config = }")
# Load base configuration
with open(base_config, "r", encoding="utf8") as f:
logging.info(f"Base config = {inifile}")
with open(inifile, "r", encoding="utf8") as f:
base_config = json.load(f)

variations = [
{"motivation_parameters/width": 1.0, "motivation_parameters/seed": 1.0},
# {"motivation_parameters/width": 2.0, "motivation_parameters/seed": 300.0},
# {"motivation_parameters/width": 3.0, "motivation_parameters/seed": 200.0},
# {"motivation_parameters/width": 4.0, "motivation_parameters/seed": 300.0},
# {"motivation_parameters/width": 5.0, "motivation_parameters/seed": 200.0},
# {"motivation_parameters/width": 6.0, "motivation_parameters/seed": 300.0},
# {"motivation_parameters/width": 7.0, "motivation_parameters/seed": 300.0},
]
file_path = "files/variations/variations.json"

# Write the list of dictionaries to a JSON file
with open(file_path, "w") as f:
output_dir = pathlib.Path("files/variations")
output_dir.mkdir(parents=True, exist_ok=True)

variations_file = output_dir / "variations.json"
with open(variations_file, "w") as f:
json.dump(variations, f, indent=4)
# Run simulations with variations

for i, variation in enumerate(variations, start=1):
logging.info(f"running variation {i:03d}: {variation}")
new_config_path = f"config_variation_{i:03d}.json"
Expand All @@ -532,3 +534,7 @@ def modify_and_save_config(base_config, modification_dict, new_config_path):

evac_time = start_simulation(new_config_path, output_path)
logging.info(f"Variation {i:03d}: {evac_time = }")


if __name__ == "__main__":
typer.run(main)

0 comments on commit 121542d

Please sign in to comment.