Skip to content

Commit

Permalink
Merge pull request #18 from PedestrianDynamics/pyg
Browse files Browse the repository at this point in the history
Pyg
  • Loading branch information
chraibi authored Mar 10, 2024
2 parents de9f9ea + 82e6065 commit f8a115b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.helpers.log_config import setup_logging
from src.tabs.analysis_tab import run_tab3
from src.tabs.contacts_tab import run_tab_contact
from src.tabs.explorer import run_explorer
from src.tabs.map_tab import run_tab_map
from src.tabs.traj_tab import run_tab2
from src.ui.ui import init_app_looks, init_sidebar, setup_app
Expand All @@ -26,7 +27,7 @@

if selected_tab == "Trajectories":
msg = st.empty()
file_name_to_path = {path.split('/')[-1]: path for path in st.session_state.files}
file_name_to_path = {path.split("/")[-1]: path for path in st.session_state.files}
filename = str(st.selectbox(":open_file_folder: **Select a file**", file_name_to_path))
st.session_state.selected_file = file_name_to_path[filename]
run_tab2(file_name_to_path[filename], msg)
Expand All @@ -36,3 +37,6 @@

if selected_tab == "Contacts":
run_tab_contact()

if selected_tab == "Explorer":
run_explorer()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
streamlit
streamlit_folium
folium
pandas
pedpy@git+https://github.com/PedestrianDynamics/PedPy.git
pandas
plotly
matplotlib
streamlit_drawable_canvas==0.8.0
streamlit-option-menu
gpxpy
tqdm
pygwalker
74 changes: 74 additions & 0 deletions src/tabs/explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pickle
from pathlib import Path

import pandas as pd
import pedpy
import pygwalker as pyg
import streamlit as st
from pygwalker.api.streamlit import StreamlitRenderer

from ..classes.datafactory import load_file
from ..helpers.utilities import setup_walkable_area


def prepare_data(selected_file: str, delta_frame: int) -> pd.DataFrame:
if selected_file != st.session_state.file_changed:
trajectory_data = load_file(selected_file)
st.session_state.trajectory_data = trajectory_data
st.session_state.file_changed = selected_file

result_file = Path(selected_file).stem + "_results.pkl"
if not Path(result_file).exists():
with st.status("Preparing data ..."):
trajectory_data = st.session_state.trajectory_data
walkable_area = setup_walkable_area(trajectory_data)
speed = pedpy.compute_individual_speed(
traj_data=trajectory_data,
frame_step=delta_frame,
speed_calculation=pedpy.SpeedCalculation.BORDER_SINGLE_SIDED,
)
individual = pedpy.compute_individual_voronoi_polygons(traj_data=trajectory_data, walkable_area=walkable_area, cut_off=pedpy.Cutoff(radius=1.0, quad_segments=3))
density_voronoi, intersecting = pedpy.compute_voronoi_density(individual_voronoi_data=individual, measurement_area=walkable_area)
voronoi_speed = pedpy.compute_voronoi_speed(
traj_data=trajectory_data,
individual_voronoi_intersection=intersecting,
individual_speed=speed,
measurement_area=walkable_area,
)
data_with_speed = voronoi_speed.merge(trajectory_data.data, on=["frame"], how="left")
data_with_speed_density = density_voronoi.merge(data_with_speed, on=["frame"], how="left")
with open(result_file, "wb") as f:
pickle.dump(data_with_speed_density, f)
else:
with open(result_file, "rb") as f:
data_with_speed_density = pickle.load(f)

return data_with_speed_density


def run_walker(df: pd.DataFrame) -> None:
"""You should cache your pygwalker renderer, if you don't want your memory to explode."""

@st.cache_resource
def get_pyg_renderer(df: pd.DataFrame) -> "StreamlitRenderer":
# If you want to use feature of saving chart config, set `spec_io_mode="rw"`
return StreamlitRenderer(df, spec="./gw_config.json", spec_io_mode="rw", field_specs={"frame": pyg.FieldSpec(analyticType="dimension")})

renderer = get_pyg_renderer(df)
renderer.render_explore()


def run_explorer() -> None:
"""Call explorer woth dataframe from selected file."""
file_name_to_path = {path.split("/")[-1]: path for path in st.session_state.files}
filename = str(
st.selectbox(
":open_file_folder: **Select a file**",
file_name_to_path,
key="explorer_filename",
)
)
selected_file = file_name_to_path[filename]
st.session_state.selected_file = selected_file
df = prepare_data(selected_file, delta_frame=10)
run_walker(df)
9 changes: 7 additions & 2 deletions src/ui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ def init_app_looks() -> None:


def init_sidebar() -> Any:
"""Init sidebar and 4 tabs."""
"""Init sidebar and 5 tabs.
To add more tabs, add the name of the tab and add an icon from
https://icons.getbootstrap.com/
"""
return option_menu(
"Multi-agent modelling of dense crowd dynamics: Predict & Understand",
["About", "Map", "Trajectories", "Analysis", "Contacts"],
["About", "Map", "Trajectories", "Analysis", "Contacts", "Explorer"],
icons=[
"info-square",
"pin-map",
"people",
"bar-chart-line",
"exclamation-triangle",
"graph-up-arrow",
],
menu_icon="cast",
default_index=0,
Expand Down

0 comments on commit f8a115b

Please sign in to comment.