Skip to content

Commit

Permalink
simplify UI and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chraibi committed Mar 12, 2024
1 parent 9190e8a commit b153107
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 110 deletions.
65 changes: 41 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,49 +66,66 @@ def read_data(output_file: str) -> pd.DataFrame:
# st.sidebar.info(f"{jps.__version__ = }")
# st.sidebar.info(f"{pedpy.__version__ = }")

if tab == "Initialisation":
column_1, column_2 = st.columns((1, 1))
file_name = column_1.text_input(
"Load config file: ", value="files/bottleneck.json"
if tab == "Simulation":
with st.sidebar.expander("Save/load config"):
column_1, column_2 = st.columns((1, 1))

file_name = str(
column_1.selectbox(
"Load",
sorted(list(set(st.session_state.all_files)), reverse=True),
)
)

# file_name = column_1.text_input(
# "Load", value="files/bottleneck.json", help="Load config file"
# )
json_file = Path(file_name)
data = {}
if not json_file.exists():
st.error(f"file: {file_name} does not exist!")
st.stop()

with column_1:
data = load_json(json_file)
ui_velocity_model_parameters(data)
ui_simulation_parameters(data)
ui_motivation_parameters(data)
st.session_state.data = data
st.session_state.all_files.append(file_name)
# with column_1:
data = load_json(json_file)
ui_velocity_model_parameters(data)
ui_simulation_parameters(data)
ui_motivation_parameters(data)
st.session_state.data = data
st.session_state.all_files.append(file_name)

# Save Button (optional)
new_json_name = column_2.text_input(
"Save config file: ", value="files/bottleneck.json"
"Save", help="Save config file: ", value="files/bottleneck2.json"
)
new_json_file = Path(new_json_name)
if column_2.button(
"Save config",
help=f"After changing the values, you can save the configs in a separate file ({new_json_name})",
):
save_json(new_json_file, data)
column_1.info(f"Saved file as {new_json_name}")
st.session_state.all_files.append(new_json_name)

if column_2.button("Reset", help="Delete all trajectory files"):
save_json(new_json_file, data)
# if column_1.button(
# "Save config",
# help=f"After changing the values, you can save the configs in a separate file ({new_json_name})",
# ):
# save_json(new_json_file, data)
# st.sidebar.info(f"Saved file as {new_json_name}")
st.session_state.all_files.append(new_json_name)

if column_2.button("Delete files", help="Delete all trajectory files"):
delete_txt_files()

# Run Simulation
if tab == "Simulation":
msg = st.sidebar.empty()
c1, c2, c3 = st.columns(3)
OUTPUT_FILE = c1.text_input("Result: ", value="files/trajectory.sqlite")
msg = st.empty()
CONFIG_FILE = str(
c2.selectbox("Select config file", list(set(st.session_state.all_files)))
c2.selectbox(
"Select config file",
sorted(list(set(st.session_state.all_files)), reverse=True),
)
)
strategy = data["motivation_parameters"]["motivation_strategy"]
name, extension = CONFIG_FILE.rsplit(".", 1)
sqlite_filename = f"{name}_{strategy}.{extension.replace('json', 'sqlite')}"
OUTPUT_FILE = c1.text_input("Result: ", value=f"{sqlite_filename}")

fps = c3.number_input(
"fps", min_value=1, max_value=32, value=8, help="show every nth frame"
)
Expand Down
40 changes: 12 additions & 28 deletions files/bottleneck.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@
],
"version": 0.1,
"velocity_init_parameters": {
"a_ped": 0.29,
"d_ped": 0.11,
"a_wall": 0.5,
"d_wall": 0.02
"a_ped": 1.0,
"d_ped": 0.2,
"a_wall": 1.0,
"d_wall": 0.2,
"radius": 0.10
},
"velocity_model_parameter_profiles": [
{
"id": 1.0,
"time_gap": 1.0,
"tau": 0.5,
"v0": 1.2,
"radius": 0.15
}
],
"simulation_parameters": {
"fps": 60,
"time_step": 0.01,
"number_agents": 50,
"simulation_time": 50
"simulation_time": 100
},
"measurement_line": {
"vertices": [
Expand Down Expand Up @@ -59,7 +51,7 @@
},
"motivation_parameters": {
"motivation_strategy": "default",
"active": 1,
"active": 0,
"normal_v_0": 1.2,
"normal_time_gap": 1.0,
"width": "1.0",
Expand All @@ -84,14 +76,6 @@
}
]
},
"grid_parameters": {
"min_v_0": 1,
"max_v_0": 2,
"v_0_step": 0.1,
"min_time_gap": 0.1,
"max_time_gap": 1,
"time_gap_step": 0.1
},
"accessible_areas": [
{
"id": 1,
Expand Down Expand Up @@ -119,19 +103,19 @@
"vertices": [
[
60.0,
101.4
101.7
],
[
62.0,
101.4
101.7
],
[
62.0,
102.6
102.3
],
[
60.0,
102.6
102.3
]
]
},
Expand Down Expand Up @@ -204,4 +188,4 @@
]
}
]
}
}
8 changes: 0 additions & 8 deletions requirements.txt

This file was deleted.

22 changes: 12 additions & 10 deletions simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
parse_destinations,
parse_distribution_polygons,
parse_fps,
parse_radius,
parse_motivation_doors,
parse_motivation_parameter,
parse_motivation_strategy,
Expand Down Expand Up @@ -84,6 +85,8 @@ def init_simulation(
geometry = build_geometry(accessible_areas)
# areas = build_areas(destinations, labels)
a_ped, d_ped, a_wall, d_wall = parse_velocity_init_parameters(_data)
normal_v_0 = parse_normal_v_0(_data)
normal_time_gap = parse_normal_time_gap(_data)
simulation = jps.Simulation(
model=jps.CollisionFreeSpeedModel(
strength_neighbor_repulsion=a_ped,
Expand All @@ -97,8 +100,6 @@ def init_simulation(
output_file=pathlib.Path(_trajectory_path), every_nth_frame=_fps
),
)
normal_v_0 = parse_normal_v_0(_data)
normal_time_gap = parse_normal_time_gap(_data)
motivation_doors = parse_motivation_doors(_data)
if not motivation_doors:
log_error("json file does not contain any motivation door")
Expand Down Expand Up @@ -163,7 +164,6 @@ def run_simulation(
if motivation_model.active and simulation.iteration_count() % 100 == 0:
agents = simulation.agents()
number_agents_in_simulation = simulation.agent_count()
logging.info(f"{number_agents_in_simulation = }")
for agent in agents:
position = agent.position
distance = (
Expand Down Expand Up @@ -208,12 +208,6 @@ def main(
:param trajectory_file:
:returns:
"""
print("main")
print(f"{_number_agents = }")
print(f"{_fps = }")
print(f"{ _time_step = }")
print(f"{_simulation_time = }")
print(f"{ _trajectory_path = }")
simulation, motivation_model = init_simulation(
_data, _time_step, _fps, _trajectory_path
)
Expand All @@ -239,8 +233,16 @@ def main(
if not total_agents:
break

normal_v_0 = parse_normal_v_0(_data)
normal_time_gap = parse_normal_time_gap(_data)
radius = parse_radius(_data)
agent_parameters = jps.CollisionFreeSpeedModelAgentParameters(
journey_id=journey_id, stage_id=stage_id, radius=0.2
journey_id=journey_id,
stage_id=stage_id,
radius=radius,
v0=normal_v_0,
time_gap=normal_time_gap

)

ped_ids = distribute_and_add_agents(simulation, agent_parameters, positions)
Expand Down
16 changes: 6 additions & 10 deletions src/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,21 @@ def run():
selected = st.sidebar.radio(
"Choose option",
[
"Heatmap",
"Distance to entrance",
"Speed",
"Density",
"Flow",
"Speed",
"NT",
"Voronoi polygons",
"Distance to entrance",
"Heatmap",
],
)
SELECTED_OUTPUT_FILE = st.selectbox(
"Select file", list(set(glob.glob("files/*.sqlite")))
"Select file", sorted(list(set(glob.glob("files/*.sqlite"))), reverse=True)
)
CONFIG_FILE = str(
st.selectbox("Select config file", list(set(st.session_state.all_files)))
)

traj, walkable_area = read_sqlite_file(SELECTED_OUTPUT_FILE)

with open(CONFIG_FILE, "r", encoding="utf8") as f:
with open("files/bottleneck.json", "r", encoding="utf8") as f:
json_str = f.read()
json_data = json.loads(json_str)
ui_measurement_parameters(json_data)
Expand Down Expand Up @@ -129,7 +125,7 @@ def run():
ma_alpha=0.2,
).set_aspect("equal")
fig = plt.gcf()
st.pyplot(fig)
st.sidebar.pyplot(fig)
if selected == "NT":
nt, crossing_frames = pedpy.compute_n_t(
traj_data=traj,
Expand Down
14 changes: 14 additions & 0 deletions src/inifile_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ def parse_velocity_init_parameters(
return (8.0, 0.1, 5.0, 0.02)


def parse_radius(
json_data: Dict[str, Any],
) -> float:
"""Parse radius.
return radius
"""
if "velocity_init_parameters" in json_data:
return float(json_data["velocity_init_parameters"]["radius"])

else:
return 0.3


def parse_way_points(
json_data: Dict[str, Any],
) -> List[Tuple[Point, float]]:
Expand Down
Loading

0 comments on commit b153107

Please sign in to comment.