Skip to content

Commit

Permalink
add pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
chraibi committed Sep 30, 2024
1 parent 10120c7 commit 042e2c5
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 41 deletions.
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8 # Make sure you're using the correct version of Ruff
hooks:
# Run the linter.
- id: ruff
types_or: [ python, pyi ] # Target Python files and type stub files
args: [ --fix ] # Automatically fix linting issues if possible
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi ] # Format Python and stub files

# Mypy type checker
- repo: local
hooks:
- id: mypy
name: Analyze with Mypy
entry: mypy --strict # Runs Mypy with the strictest settings
language: python
types: ['python'] # Targets Python files
files: \.py$ # Only .py files
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ line-ending = "auto"
#
# This is currently disabled by default, but it is planned for this
# to be opt-out in the future.
docstring-code-format = false
docstring-code-format = true

# Set the line length limit used when formatting code snippets in
# docstrings.
Expand Down
16 changes: 12 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@

if selected_tab == "Trajectories":
msg = st.empty()
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))
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 @@ -48,7 +52,11 @@
# run_explorer()

if selected_tab == "Geometry":
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))
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_tab_animation(file_name_to_path[filename])
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ruff
mypy
pre-commit
27 changes: 21 additions & 6 deletions src/classes/datafactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def retrieve_files(self) -> None:
if not self.trajectories_directory.exists():
st.warning(f"{self.trajectories_directory} does not exist yet!")
with st.status("Downloading ...", expanded=False):
download_and_unzip_files(self.url, "data.zip", self.trajectories_directory)
download_and_unzip_files(
self.url, "data.zip", self.trajectories_directory
)

else:
logging.info("Found trajectory directory. Nothing to retrieve!")
Expand Down Expand Up @@ -97,7 +99,9 @@ def init_state_bg_image() -> None:
def init_session_state() -> None:
"""Init session_state throughout the app."""
path = Path(__file__)
trajectories_directory = path.parent.parent.parent.absolute() / "data" / "trajectories"
trajectories_directory = (
path.parent.parent.parent.absolute() / "data" / "trajectories"
)
flow_directory = path.parent.parent.parent.absolute() / "data" / "flow"
processed_directory = path.parent.parent.parent.absolute() / "data" / "processed"

Expand Down Expand Up @@ -130,7 +134,11 @@ def init_session_state() -> None:
if not hasattr(st.session_state, "trajectory_data"):
st.session_state.trajectoryData = pedpy.TrajectoryData

dataconfig = DataConfig(trajectories_directory=trajectories_directory, processed_directory=processed_directory, flow_directory=flow_directory)
dataconfig = DataConfig(
trajectories_directory=trajectories_directory,
processed_directory=processed_directory,
flow_directory=flow_directory,
)
st.session_state.files = dataconfig.files
st.session_state.config = dataconfig

Expand All @@ -151,16 +159,23 @@ def unzip_files(zip_path: Union[str, Path], destination: Union[str, Path]) -> No
# Extract only if file (ignores directories)
if not member.is_dir():
# Build target filename path
target_path = os.path.join(destination, os.path.basename(member.filename))
target_path = os.path.join(
destination, os.path.basename(member.filename)
)
st.info(f"targe_path {target_path}")
# Ensure target directory exists (e.g., if not extracting directories)
os.makedirs(os.path.dirname(target_path), exist_ok=True)
# Extract file
with zip_ref.open(member, "r") as source, open(target_path, "wb") as target:
with (
zip_ref.open(member, "r") as source,
open(target_path, "wb") as target,
):
shutil.copyfileobj(source, target)


def download_and_unzip_files(url: str, destination: Union[str, Path], unzip_destination: Union[str, Path]) -> None:
def download_and_unzip_files(
url: str, destination: Union[str, Path], unzip_destination: Union[str, Path]
) -> None:
"""
Download a ZIP file from a specified URL.
Expand Down
18 changes: 14 additions & 4 deletions src/helpers/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def get_id_by_name(direction_name: str) -> int:
return -1


def get_measurement_lines(trajectory_data: pd.DataFrame, distance_to_bounding: float) -> List[Direction]:
def get_measurement_lines(
trajectory_data: pd.DataFrame, distance_to_bounding: float
) -> List[Direction]:
"""Create 4 measurement lines inside the walkable_area."""
min_x = trajectory_data.data["x"].min() + distance_to_bounding
max_x = trajectory_data.data["x"].max() - distance_to_bounding
Expand All @@ -70,11 +72,17 @@ def get_measurement_lines(trajectory_data: pd.DataFrame, distance_to_bounding: f

return [
Direction(
info=DirectionInfo(id=get_id_by_name("Right"), name="Right", color=get_color_by_name("Right")),
info=DirectionInfo(
id=get_id_by_name("Right"),
name="Right",
color=get_color_by_name("Right"),
),
line=pedpy.MeasurementLine([[min_x, min_y], [min_x, max_y]]),
),
Direction(
info=DirectionInfo(id=get_id_by_name("Left"), name="Left", color=get_color_by_name("Left")),
info=DirectionInfo(
id=get_id_by_name("Left"), name="Left", color=get_color_by_name("Left")
),
line=pedpy.MeasurementLine([[max_x, min_y], [max_x, max_y]]),
),
Direction(
Expand Down Expand Up @@ -112,7 +120,9 @@ def setup_walkable_area(trajectory_data: pd.DataFrame) -> pedpy.WalkableArea:
return pedpy.WalkableArea(rectangle_polygon)


def setup_measurement_area(min_x: float, max_x: float, min_y: float, max_y: float) -> pedpy.MeasurementArea:
def setup_measurement_area(
min_x: float, max_x: float, min_y: float, max_y: float
) -> pedpy.MeasurementArea:
"""Create measurement_area from trajectories."""
rectangle_coords = [
[min_x, min_y],
Expand Down
35 changes: 26 additions & 9 deletions src/plotting/anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
from plotly.graph_objs.layout import Shape
from shapely import Polygon

from ..classes.datafactory import (decrement_frame_start,
increment_frame_start, reset_frame_start)
from ..classes.datafactory import (
decrement_frame_start,
increment_frame_start,
reset_frame_start,
)

DUMMY_SPEED = -1000

Expand All @@ -40,7 +43,9 @@ def _get_line_color(disk_color: str) -> str:
return "black" if brightness > 127 else "white"


def _create_orientation_line(row: pd.DataFrame, line_length: float = 0.2, color: str = "black") -> Shape:
def _create_orientation_line(
row: pd.DataFrame, line_length: float = 0.2, color: str = "black"
) -> Shape:
"""Create orientation Shape object."""
end_x = row["x"] + line_length * 0
end_y = row["y"] + line_length * 0
Expand Down Expand Up @@ -111,7 +116,9 @@ def _get_colormap(frame_data: pd.DataFrame, max_speed: float) -> List[Scatter]:
return [scatter_trace]


def _get_shapes_for_frame(frame_data: pd.DataFrame, min_speed: float, max_speed: float) -> Tuple[Shape, Scatter, Shape]:
def _get_shapes_for_frame(
frame_data: pd.DataFrame, min_speed: float, max_speed: float
) -> Tuple[Shape, Scatter, Shape]:
"""Construct circles as Shapes for agents, Hover and Directions."""

def create_shape(row: pd.DataFrame) -> Shape:
Expand Down Expand Up @@ -206,7 +213,9 @@ def _create_fig(
fig = go.Figure(
data=geometry_traces + initial_scatter_trace + initial_hover_trace,
frames=frames,
layout=go.Layout(shapes=initial_shapes + initial_arrows, title=title, title_x=0.5),
layout=go.Layout(
shapes=initial_shapes + initial_arrows, title=title, title_x=0.5
),
)
fig.update_layout(
updatemenus=[_get_animation_controls()],
Expand Down Expand Up @@ -269,7 +278,9 @@ def _get_slider_controls(steps: List[Dict[str, Any]]) -> Dict[str, Any]:
}


def _get_processed_frame_data(data_df: pd.DataFrame, frame_num: int, max_agents: int) -> Tuple[pd.DataFrame, int]:
def _get_processed_frame_data(
data_df: pd.DataFrame, frame_num: int, max_agents: int
) -> Tuple[pd.DataFrame, int]:
"""Process frame data and ensure it matches the maximum agent count."""
frame_data = data_df[data_df["frame"] == frame_num]
agent_count = len(frame_data)
Expand Down Expand Up @@ -342,7 +353,9 @@ def animate(
# Calculate page_end
frame_end = st.session_state.start_frame + page_size
frame_start = st.session_state.start_frame
data_df = data_df0[(data_df0["frame"] >= frame_start) & (data_df0["frame"] <= frame_end)]
data_df = data_df0[
(data_df0["frame"] >= frame_start) & (data_df0["frame"] <= frame_end)
]

min_speed = data_df["speed"].min()
max_speed = data_df["speed"].max()
Expand All @@ -361,8 +374,12 @@ def animate(
) = _get_shapes_for_frame(initial_frame_data, min_speed, max_speed)
color_map_trace = _get_colormap(initial_frame_data, max_speed)
for frame_num in selected_frames:
frame_data, agent_count = _get_processed_frame_data(data_df, frame_num, max_agents)
shapes, hover_traces, arrows = _get_shapes_for_frame(frame_data, min_speed, max_speed)
frame_data, agent_count = _get_processed_frame_data(
data_df, frame_num, max_agents
)
shapes, hover_traces, arrows = _get_shapes_for_frame(
frame_data, min_speed, max_speed
)
# title = f"<b>{title_note + ' | ' if title_note else ''}N: {agent_count}</b>"
title = f"<b>{title_note + ' | ' if title_note else ''}Number of Agents: {initial_agent_count}. Frame: {frame_num}</b>"
frame_name = str(int(frame_num))
Expand Down
8 changes: 6 additions & 2 deletions src/plotting/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from .plots import draw_bg_img, draw_rects


def drawing_canvas(trajectory_data: pedpy.TrajectoryData, walkable_area: pedpy.WalkableArea) -> Tuple[Any, float, float, float]:
def drawing_canvas(
trajectory_data: pedpy.TrajectoryData, walkable_area: pedpy.WalkableArea
) -> Tuple[Any, float, float, float]:
"""Draw trajectories as img and prepare canvas."""
drawing_mode = st.sidebar.radio(
"**Measurement:**",
Expand All @@ -34,7 +36,9 @@ def drawing_canvas(trajectory_data: pedpy.TrajectoryData, walkable_area: pedpy.W
min_y = trajectory_data.data["y"].min()
max_y = trajectory_data.data["y"].max()

bg_img, img_width, img_height, dpi, scale = draw_bg_img(data, min_x, max_x, min_y, max_y)
bg_img, img_width, img_height, dpi, scale = draw_bg_img(
data, min_x, max_x, min_y, max_y
)
st.session_state.scale = scale
st.session_state.dpi = dpi
st.session_state.img_width = img_width
Expand Down
10 changes: 7 additions & 3 deletions src/scripts/calculate_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import pedpy as pp
from joblib import Parallel, delayed
from log_config import setup_logging
from pedpy import (DensityMethod, SpeedMethod,
compute_grid_cell_polygon_intersection_area,
compute_speed_profile, get_grid_cells)
from pedpy import (
DensityMethod,
SpeedMethod,
compute_grid_cell_polygon_intersection_area,
compute_speed_profile,
get_grid_cells,
)
from profile_config_data import Config
from tqdm import tqdm

Expand Down
3 changes: 2 additions & 1 deletion src/scripts/ploting_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def init_parameters(config: Config) -> Tuple[List[Any], pp.WalkableArea, List[st
"title": "Flow profile",
"ylabel": r"$J\, /\, 1/ms$",
"vmax": jmax,
"data_func": lambda file_: np.array(density_profiles[file_]) * np.array(speed_profiles[file_]),
"data_func": lambda file_: np.array(density_profiles[file_])
* np.array(speed_profiles[file_]),
},
]
return plot_params, walkable_area, files
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/run_profile_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def run_all() -> None:
"""Initialize the configuration."""
path = Path(__file__)
data_directory = path.parent.parent.parent.absolute() / "data" / "processed"
trajectories_directory = path.parent.parent.parent.absolute() / "data" / "trajectories"
trajectories_directory = (
path.parent.parent.parent.absolute() / "data" / "trajectories"
)
area = [[-6, 0], [5, 0], [5, 7], [-6, 7]]
config = Config(
files=sorted(glob.glob(f"{str(trajectories_directory)}/*.txt")),
Expand Down
25 changes: 20 additions & 5 deletions src/tabs/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ def prepare_data(selected_file: str, delta_frame: int) -> pd.DataFrame:
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)
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")
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:
Expand All @@ -52,7 +62,12 @@ def run_walker(df: pd.DataFrame) -> None:
@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")})
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()
Expand Down
15 changes: 11 additions & 4 deletions src/tabs/traj_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

from ..classes.datafactory import load_file
from ..plotting.anim import animate
from ..plotting.plots import (download_file, plot_trajectories,
plot_trajectories_figure_mpl)
from ..plotting.plots import (
download_file,
plot_trajectories,
plot_trajectories_figure_mpl,
)

# import cProfile
# import pstats
Expand Down Expand Up @@ -42,7 +45,9 @@ def run_tab2(selected_file: str, msg: DeltaGenerator) -> None:
frame_step=5,
speed_calculation=pedpy.SpeedCalculation.BORDER_SINGLE_SIDED,
)
data_with_speed = data_with_speed.merge(trajectory_data.data, on=["id", "frame"], how="left")
data_with_speed = data_with_speed.merge(
trajectory_data.data, on=["id", "frame"], how="left"
)

ids = trajectory_data.data["id"].unique()
start_time = time.time()
Expand Down Expand Up @@ -112,7 +117,9 @@ def run_tab2(selected_file: str, msg: DeltaGenerator) -> None:
max_value=5.0,
)
# TODO: remove
fig2 = plot_trajectories_figure_mpl(trajectory_data, walkable_area, with_colors=True, alpha=alpha, lw=lw)
fig2 = plot_trajectories_figure_mpl(
trajectory_data, walkable_area, with_colors=True, alpha=alpha, lw=lw
)
c1.pyplot(fig2)
path = Path(__file__)
data_directory = path.parent.parent.parent.absolute() / "data" / "processed"
Expand Down
Loading

0 comments on commit 042e2c5

Please sign in to comment.