Skip to content

Commit

Permalink
update notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
chraibi committed Oct 21, 2024
1 parent 62c6cc2 commit 4f0d5b9
Showing 1 changed file with 118 additions and 108 deletions.
226 changes: 118 additions & 108 deletions positions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import json\n",
"from pathlib import Path\n",
"from typing import List, Optional, Tuple\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import numpy as np\n",
"import csv\n",
"from pathlib import Path\n",
"from shapely import from_wkt\n",
"import pedpy\n",
"import plotly.graph_objects as go\n",
"import shapely\n",
"import json\n",
"from jupedsim.internal.notebook_utils import animate\n",
"from shapely import from_wkt\n",
"import shapely\n",
"from typing import Optional, List, Tuple\n",
"import plotly.graph_objects as go\n",
"\n",
"print(f\"PedPy version: {pedpy.__version__}\")"
]
Expand All @@ -34,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\", # low motivation\n",
" #\"../trajectories_croma/1C070_cam6_cam5_frameshift0_Combined.txt\", # low motivation\n",
" \"../trajectories_croma/2C070_cam6_cam5_frameshift0_Combined.txt\", # high motivation\n",
" \"../trajectories_croma/2C120_cam6_cam5_frameshift0_Combined.txt\", # high motivation\n",
" #\"../trajectories_croma/2C130_cam6_cam5_frameshift0_Combined.txt\", # low motivation\n",
" #\"../trajectories_croma/2C150_cam6_cam5_frameshift0_Combined.txt\", # low motivation\n",
"]\n",
"\n",
"\n",
Expand All @@ -49,24 +48,24 @@
" return data\n",
"\n",
"\n",
"def get_first_frame_pedestrian_passes_line(filename: str, passing_line_y: float = 20):\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",
" 70 -> \n",
" \"\"\"\n",
" df = pd.read_csv(\n",
" filename, sep=\"\\t\", names=[\"id\", \"frame\", \"x\", \"y\", \"z\", \"m\"], comment=\"#\"\n",
" )\n",
" df = df.sort_values([\"frame\", \"id\"])\n",
" df[\"y_diff\"] = df.groupby(\"id\")[\"y\"].diff()\n",
" crossing_frame = df[(df[\"y\"] >= passing_line_y) & (df[\"y_diff\"] > 0)][\"frame\"].min()\n",
" df = df.sort_values(['frame', 'id'])\n",
" df['y_diff'] = df.groupby('id')['y'].diff()\n",
" crossing_frame = df[(df['y'] >= passing_line_y) & (df['y_diff'] > 0)]['frame'].min()\n",
" if pd.isna(crossing_frame):\n",
" return df, None\n",
" else:\n",
" return df, int(crossing_frame)\n",
"\n",
" \n",
"\n",
"def plot_trajectories_around_frame(\n",
" df: pd.DataFrame,\n",
Expand Down Expand Up @@ -170,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 @@ -309,9 +308,7 @@
" process_file(filename, geometry)\n",
"\n",
"\n",
"process_files(filenames, geometry)\n",
"# 3951\n",
"# 5344"
"process_files(filenames, geometry)"
]
},
{
Expand All @@ -323,52 +320,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Parse the WKT polygon\n",
"geometry2 = from_wkt(\n",
" \"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, -0.87 19.71, -0.72 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, -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, 0.82 19.71, 0.67 19.57, 0.38 19.57, 0.23 19.71, 0.23 21.09, 0.38 21.23, 0.67 21.23, 0.82 21.09, 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))\"\n",
")\n",
"# # Parse the WKT polygon\n",
"# geometry2 = from_wkt(\n",
"# \"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, -0.87 19.71, -0.72 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, -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, 0.82 19.71, 0.67 19.57, 0.38 19.57, 0.23 19.71, 0.23 21.09, 0.38 21.23, 0.67 21.23, 0.82 21.09, 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))\"\n",
"# )\n",
"\n",
"\n",
"# Extract exterior coordinates\n",
"exterior_x, exterior_y = geometry2.exterior.xy\n",
"# # Extract exterior coordinates\n",
"# exterior_x, exterior_y = geometry2.exterior.xy\n",
"\n",
"# Extract interior coordinates (holes)\n",
"interiors = [list(interior.coords.xy) for interior in geometry2.interiors]\n",
"exterior_x = list(geometry2.exterior.xy[0])\n",
"exterior_y = list(geometry2.exterior.xy[1])\n",
"# # Extract interior coordinates (holes)\n",
"# interiors = [list(interior.coords.xy) for interior in geometry2.interiors]\n",
"# exterior_x = list(geometry2.exterior.xy[0])\n",
"# exterior_y = list(geometry2.exterior.xy[1])\n",
"\n",
"# Extract interior coordinates (holes) as lists\n",
"interiors = [\n",
" (list(interior.xy[0]), list(interior.xy[1])) for interior in geometry2.interiors\n",
"]\n",
"# # Extract interior coordinates (holes) as lists\n",
"# interiors = [\n",
"# (list(interior.xy[0]), list(interior.xy[1])) for interior in geometry2.interiors\n",
"# ]\n",
"\n",
"# Create the figure\n",
"fig = go.Figure()\n",
"# # Create the figure\n",
"# fig = go.Figure()\n",
"\n",
"# Plot exterior\n",
"fig.add_trace(go.Scatter(x=exterior_x, y=exterior_y, mode=\"lines\", name=\"Exterior\"))\n",
"# # Plot exterior\n",
"# fig.add_trace(go.Scatter(x=exterior_x, y=exterior_y, mode=\"lines\", name=\"Exterior\"))\n",
"\n",
"# Plot interiors (holes)\n",
"for i, (x, y) in enumerate(interiors):\n",
" fig.add_trace(go.Scatter(x=x, y=y, mode=\"lines+markers\", name=f\"Interior {i+1}\"))\n",
" print(f\"Interior {i+1}. {x = }, {y = }\")\n",
"# # Plot interiors (holes)\n",
"# for i, (x, y) in enumerate(interiors):\n",
"# fig.add_trace(go.Scatter(x=x, y=y, mode=\"lines+markers\", name=f\"Interior {i+1}\"))\n",
"# print(f\"Interior {i+1}. {x = }, {y = }\")\n",
"\n",
"\n",
"# fig.add_trace(go.Scatter(x=combined_x, y=combined_y, mode='lines+markers', fill='toself', name='Combined Polygon'))\n",
"\n",
"# Update layout\n",
"fig.update_layout(\n",
" title=\"Polygon with Interior Holes\",\n",
" xaxis_title=\"X Coordinate\",\n",
" yaxis_title=\"Y Coordinate\",\n",
" showlegend=True,\n",
" width=600,\n",
" height=600,\n",
")"
"# # fig.add_trace(go.Scatter(x=combined_x, y=combined_y, mode='lines+markers', fill='toself', name='Combined Polygon'))\n",
"\n",
"# # Update layout\n",
"# fig.update_layout(\n",
"# title=\"Polygon with Interior Holes\",\n",
"# xaxis_title=\"X Coordinate\",\n",
"# yaxis_title=\"Y Coordinate\",\n",
"# showlegend=True,\n",
"# width=600,\n",
"# height=600,\n",
"# )\n"
]
},
{
Expand All @@ -380,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -399,15 +397,17 @@
"# for id_to_plot in df[\"id\"].unique():\n",
"# ped_data = df[df[\"id\"] == id_to_plot]\n",
"# plt.plot(ped_data[\"y\"], ped_data[\"motivation\"],'-k', ms=1, alpha=0.6, color=colors[i])\n",
"\n"
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# x_values = df[\"x\"].to_numpy()\n",
"# y_values = df[\"y\"].to_numpy()\n",
"# motivation_values = df[\"motivation\"].to_numpy()\n",
Expand All @@ -424,53 +424,40 @@
"# plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Animation of trajectories"
]
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# def get_first_frame_pedestrian_passes_line(filename: str, passing_line_y: float=19.71):\n",
"# \"\"\"\n",
"# Return the first frame when a pedestrian passes the specified horizontal line.\n",
"# Also return the DataFrame of whole trajectories.\n",
"# \"\"\"\n",
"# # Read the CSV file\n",
"# df = pd.read_csv(\n",
"# filename, sep=\"\\t\", names=[\"id\", \"frame\", \"x\", \"y\", \"z\", \"m\"], comment=\"#\"\n",
"# )\n",
"\n",
"# # Sort the DataFrame by frame and id to ensure chronological order\n",
"# df = df.sort_values(['frame', 'id'])\n",
"\n",
"# # Group the DataFrame by id and calculate the difference in y position\n",
"# df['y_diff'] = df.groupby('id')['y'].diff()\n",
"\n",
"# # Find the first frame where a pedestrian crosses the line\n",
"# crossing_frame = df[(df['y'] >= passing_line_y) & (df['y_diff'] > 0)]['frame'].min()\n",
"\n",
"# if pd.isna(crossing_frame):\n",
"# return df, None\n",
"# else:\n",
"# return df, int(crossing_frame)\n",
"\n",
"# # Example usage\n",
"# filename = filenames[0]\n",
"# passing_line_y = 19.71 # Y-coordinate of the horizontal passing line\n",
"\n",
"# df, first_crossing_frame = get_first_frame_pedestrian_passes_line(filename, passing_line_y)\n",
"\n",
"# if first_crossing_frame is not None:\n",
"# print(f\"The first frame when a pedestrian passes the line is: {first_crossing_frame}\")\n",
"# else:\n",
"# print(\"No pedestrian crossed the line in the given data.\")"
"walkable_area = pedpy.WalkableArea(geometry)\n",
"for filename in filenames:\n",
" df, frame_after_decrease = get_first_frame_pedestrian_passes_line(filename)\n",
" df['ox'] = 0.0\n",
" df['oy'] = 0.1\n",
" delta_frame = 20\n",
" start = 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",
" anim = animate(trajectory_data, walkable_area, every_nth_frame=1, title_note=f'{extract_title(filename)}, start={frame_after_decrease}')\n",
" anim.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Animation of trajectories"
"# Premovement frame\n",
"\n",
"calculate for each file the premovement frame for each pedestrian."
]
},
{
Expand All @@ -479,18 +466,41 @@
"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 = 20\n",
"start = 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)"
"# filename = filenames[0]\n",
"# df = pd.read_csv(\n",
"# filename, sep=\"\\t\", names=[\"id\", \"frame\", \"x\", \"y\", \"z\", \"m\"], comment=\"#\"\n",
"# )\n",
"\n",
"\n",
"# threshold = 0.1\n",
"# dy= 1\n",
"# ped = 70\n",
"# df_ped = df[(df['frame']>=frame_after_decrease) & (df['id']==ped)]\n",
"# def premovement_frame(df_ped, threshold, dy):\n",
"# premovement_frame = pd.NA\n",
"# while pd.isna(premovement_frame):\n",
"# premovement_frame = df_ped[df_ped['y'].diff(dy).abs() > threshold]['frame'].min()\n",
"# dy += 1\n",
"# return premovement_frame, dy\n",
"\n",
"\n",
"\n",
"# premovement_frame, dy = premovement_frame(df_ped, threshold, dy)\n",
"\n",
"# print(f\"{premovement_frame = }, {dy = }\")\n",
"# plt.plot( df_ped['frame'], df_ped['y'],'-k', ms=1, alpha=0.6)\n",
"# plt.plot(premovement_frame, df_ped[df_ped['frame']==premovement_frame]['y'], 'ro', label='Pre-movement frame')\n",
"# plt.ylabel('Y Position')\n",
"# plt.xlabel('Frame')\n",
"# plt.title(f'Pedestrian {ped}. {premovement_frame = }, {dy = }')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 4f0d5b9

Please sign in to comment.