From 239e5cf3cac17e90f40faf6544f37215519013e3 Mon Sep 17 00:00:00 2001 From: Mohcine Chraibi Date: Fri, 15 Mar 2024 20:47:26 +0100 Subject: [PATCH] processed directory created --- src/classes/datafactory.py | 11 +++++-- src/tabs/analysis_tab.py | 62 ++++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/classes/datafactory.py b/src/classes/datafactory.py index fc7a31f..1933ff6 100644 --- a/src/classes/datafactory.py +++ b/src/classes/datafactory.py @@ -8,7 +8,7 @@ from dataclasses import dataclass, field from pathlib import Path from typing import List, Union - +import logging import pedpy import requests # type: ignore import streamlit as st @@ -35,7 +35,10 @@ class Direction: class DataConfig: """Datastructure for the app.""" + # trajectories directory: Path + # results + processed_directory: Path files: List[str] = field(default_factory=list) # data: Dict[str, List] = field(default_factory=lambda: defaultdict(list)) url: str = "https://go.fzj.de/madras-data" @@ -43,6 +46,8 @@ class DataConfig: def __post_init__(self) -> None: """Initialize the DataConfig instance by retrieving files for each country.""" self.directory.parent.mkdir(parents=True, exist_ok=True) + logging.info(f"Create {self.processed_directory}") + self.processed_directory.mkdir(parents=True, exist_ok=True) self.retrieve_files() def retrieve_files(self) -> None: @@ -92,6 +97,8 @@ def init_session_state() -> None: """Init session_state throughout the app.""" path = Path(__file__) trajectories_directory = path.parent.parent.parent.absolute() / "data" / "trajectories" + processed_directory = path.parent.parent.parent.absolute() / "data" / "processed" + logging.info(f"{trajectories_directory = }") init_state_bg_image() # Initialize a list of DirectionInfo objects using the provided dictionaries @@ -118,7 +125,7 @@ def init_session_state() -> None: if not hasattr(st.session_state, "trajectory_data"): st.session_state.trajectoryData = pedpy.TrajectoryData - dataconfig = DataConfig(trajectories_directory) + dataconfig = DataConfig(trajectories_directory, processed_directory) st.session_state.files = dataconfig.files diff --git a/src/tabs/analysis_tab.py b/src/tabs/analysis_tab.py index 416c8b7..5d084f5 100644 --- a/src/tabs/analysis_tab.py +++ b/src/tabs/analysis_tab.py @@ -13,15 +13,14 @@ import pandas as pd import pedpy import streamlit as st - +from pedpy import SpeedMethod from ..classes.datafactory import load_file from ..docs.docs import density_speed, flow -from ..helpers.utilities import (download, get_measurement_lines, - is_running_locally, setup_walkable_area) +from ..helpers.utilities import download, get_measurement_lines, is_running_locally, setup_walkable_area + +# from ..helpers.speed_profile import compute_gaussian_weighted_speed_profile, compute_mean_speed_profile, compute_speed_profile from ..plotting.drawing import drawing_canvas, get_measurement_area -from ..plotting.plots import (download_file, plot_fundamental_diagram_all, - plot_fundamental_diagram_all_mpl, - plot_time_series, plt_plot_time_series, show_fig) +from ..plotting.plots import download_file, plot_fundamental_diagram_all, plot_fundamental_diagram_all_mpl, plot_time_series, plt_plot_time_series, show_fig st_column: TypeAlias = st.delta_generator.DeltaGenerator @@ -388,7 +387,9 @@ def calculate_nt( figname = figname.with_name(figname.stem + ".pdf") path = Path(__file__) data_directory = path.parent.parent.parent.absolute() / "data" / "processed" + logging.info(f"Check existance {data_directory}: {data_directory.exists()}") figname = data_directory / Path(figname) + logging.info(f"Try to save {figname}") fig1.savefig(figname, bbox_inches="tight", pad_inches=0.1) download_file(Path(figname)) @@ -503,10 +504,29 @@ def calculate_speed_profile( ["Nan", "0"], ) ) + chose_method = st.sidebar.radio( + "Chose method", + ["Classic", "Gaussian"], + help="See [PedPy-documentation](https://pedpy.readthedocs.io/en/latest/user_guide.html#density-profiles).", + ) + chose_method = str(chose_method) + if chose_method == "Gaussian": + width = float( + st.sidebar.number_input( + "fwhm", + value=0.5, + min_value=0.2, + max_value=1.0, + step=0.1, + help="full width at half maximum", + format="%.2f", + ) + ) if fil == "Nan": fil_empty = np.nan else: fil_empty = 0.0 + individual_speed = pedpy.compute_individual_speed( traj_data=trajectory_data, frame_step=10, @@ -516,13 +536,29 @@ def calculate_speed_profile( trajectory_data.data, on=[pedpy.column_identifier.ID_COL, pedpy.column_identifier.FRAME_COL], ) - speed_profiles = pedpy.compute_speed_profile( - data=combined_data, - walkable_area=walkable_area, - grid_size=grid_size, - speed_method=pedpy.SpeedMethod.MEAN, - fill_value=fil_empty, - ) + if chose_method == "Gaussian": + # speed_profiles = compute_gaussian_weighted_speed_profile2(data=combined_data, walkable_area=walkable_area, grid_size=grid_size, width=width, fill_value=fil_empty) + # speed_profiles = compute_speed_profile( + # data=combined_data, walkable_area=walkable_area, grid_size=grid_size, fill_value=fil_empty, width=width, speed_method=SpeedMethod.MEAN + # ) + st.info("work in progress") + speed_profiles = pedpy.compute_speed_profile( + data=combined_data, + walkable_area=walkable_area, + grid_size=grid_size, + speed_method=pedpy.SpeedMethod.MEAN, + fill_value=fil_empty, + ) + + else: + speed_profiles = pedpy.compute_speed_profile( + data=combined_data, + walkable_area=walkable_area, + grid_size=grid_size, + speed_method=pedpy.SpeedMethod.MEAN, + fill_value=fil_empty, + ) + vmax = float( st.sidebar.number_input( "Max speed",