Skip to content

Commit

Permalink
Wrap byton mbindings to C-API again in Python
Browse files Browse the repository at this point in the history
We want to provide the best possible user experience when using
jupedsim. Therfore we want to provide all docstrings from the python
layer itself. Further this allows us to extend the Simulation type for
example with more custom functionality only present in python, e.g.
builtin serialisation or working directly with shaply types.

Co-authored-by: schroedtert <[email protected]>
Co-authored-by: Christian Hirt <[email protected]>
  • Loading branch information
3 people committed Sep 7, 2023
1 parent 9eead5b commit d50778c
Show file tree
Hide file tree
Showing 25 changed files with 875 additions and 64 deletions.
12 changes: 6 additions & 6 deletions examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from shapely import GeometryCollection, Polygon, to_wkt

import jupedsim as jps
from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry


def log_debug(msg):
Expand Down Expand Up @@ -41,7 +39,7 @@ def main():
p1 = Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])
p2 = Polygon([(10, 4), (20, 4), (20, 6), (10, 6)])
area = GeometryCollection(p1.union(p2))
geometry = build_jps_geometry(area)
geometry = jps.build_jps_geometry(area)

model_builder = jps.VelocityModelBuilder(
a_ped=8, d_ped=0.1, a_wall=5, d_wall=0.02
Expand All @@ -56,7 +54,8 @@ def main():
simulation = jps.Simulation(model=model, geometry=geometry, dt=0.01)

stage_id = simulation.add_waiting_set_stage([(16, 5), (15, 5), (14, 5)])
exit_stage = simulation.get_stage_proxy(stage_id)
waiting_stage = simulation.get_stage_proxy(stage_id)
assert isinstance(waiting_stage, jps.WaitingSetProxy)
exit_id = simulation.add_exit_stage([(18, 4), (20, 4), (20, 6), (18, 6)])

journey = jps.JourneyDescription()
Expand All @@ -77,7 +76,7 @@ def main():

print("Running simulation")

writer = SqliteTrajectoryWriter(pathlib.Path("example1_out.sqlite"))
writer = jps.SqliteTrajectoryWriter(pathlib.Path("example1_out.sqlite"))
writer.begin_writing(10, to_wkt(area, rounding_precision=-1))

while simulation.agent_count() > 0:
Expand All @@ -89,7 +88,8 @@ def main():
print(f"{a.model.e0}")
break
if simulation.iteration_count() == 1300:
exit_stage.state = jps.WaitingSetState.Inactive
if waiting_stage.state == jps.WaitingSetState.ACTIVE:
waiting_stage.state = jps.WaitingSetState.INACTIVE
except KeyboardInterrupt:
writer.end_writing()
print("CTRL-C Recieved! Shuting down")
Expand Down
8 changes: 3 additions & 5 deletions examples/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from shapely import GeometryCollection, Polygon, to_wkt

import jupedsim as jps
from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry


def log_debug(msg):
Expand Down Expand Up @@ -40,7 +38,7 @@ def main():
area = GeometryCollection(
Polygon([(0, 0), (100, 0), (100, 100), (0, 100)])
)
geometry = build_jps_geometry(area)
geometry = jps.build_jps_geometry(area)

model_builder = jps.VelocityModelBuilder(
a_ped=8, d_ped=0.1, a_wall=5, d_wall=0.02
Expand Down Expand Up @@ -97,7 +95,7 @@ def main():
agent_parameters.position = (6, 50)
simulation.add_agent(agent_parameters)

writer = SqliteTrajectoryWriter(pathlib.Path("example2_out.sqlite"))
writer = jps.SqliteTrajectoryWriter(pathlib.Path("example2_out.sqlite"))
writer.begin_writing(
25,
to_wkt(area, rounding_precision=-1),
Expand All @@ -120,7 +118,7 @@ def main():
)

if signal_once and any(simulation.agents_in_range((60, 60), 1)):
stage.state = jps.WaitingSetState.Inactive
stage.state = jps.WaitingSetState.INACTIVE
print(f"Stop Waiting @{simulation.iteration_count()}")
signal_once = False
if simulation.iteration_count() % 4 == 0:
Expand Down
6 changes: 2 additions & 4 deletions examples/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from shapely import GeometryCollection, Polygon, to_wkt

import jupedsim as jps
from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry


def log_debug(msg):
Expand Down Expand Up @@ -40,7 +38,7 @@ def main():
area = GeometryCollection(
Polygon([(0, 0), (100, 0), (100, 100), (0, 100), (0, 0)])
)
geometry = build_jps_geometry(area)
geometry = jps.build_jps_geometry(area)

model_builder = jps.VelocityModelBuilder(
a_ped=8, d_ped=0.1, a_wall=5, d_wall=0.02
Expand Down Expand Up @@ -82,7 +80,7 @@ def main():
agent_parameters.position = (0.5, y)
simulation.add_agent(agent_parameters)

writer = SqliteTrajectoryWriter(pathlib.Path("example3_out.sqlite"))
writer = jps.SqliteTrajectoryWriter(pathlib.Path("example3_out.sqlite"))
writer.begin_writing(25, to_wkt(area, rounding_precision=-1))
while (
simulation.agent_count() > 0 and simulation.iteration_count() < 20_000
Expand Down
6 changes: 2 additions & 4 deletions examples/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from shapely import GeometryCollection, Polygon, to_wkt

import jupedsim as jps
from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry


def log_debug(msg):
Expand Down Expand Up @@ -42,7 +40,7 @@ def main():
area = GeometryCollection(
Polygon(shell=[(0, 0), (1000, 0), (1000, 5000), (0, 5000), (0, 0)])
)
geometry = build_jps_geometry(area)
geometry = jps.build_jps_geometry(area)

model_builder = jps.VelocityModelBuilder(
a_ped=8, d_ped=0.1, a_wall=5, d_wall=0.02
Expand Down Expand Up @@ -73,7 +71,7 @@ def main():
agent_parameters.position = (0.5 + x, y + 0.5)
simulation.add_agent(agent_parameters)

writer = SqliteTrajectoryWriter(pathlib.Path("example4_out.sqlite"))
writer = jps.SqliteTrajectoryWriter(pathlib.Path("example4_out.sqlite"))
writer.begin_writing(5, to_wkt(area, rounding_precision=-1))
while simulation.agent_count() > 0:
try:
Expand Down
10 changes: 4 additions & 6 deletions performancetest/grosser_stern.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import sys
import time

import py_jupedsim as jps
import shapely
from shapely import to_wkt

from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry
import jupedsim as jps
from performancetest.geometry import geometries
from performancetest.stats_writer import StatsWriter

Expand Down Expand Up @@ -44491,7 +44489,7 @@ def main():
jps.set_error_callback(log_error)

geo = shapely.from_wkt(geometries["grosser_stern"])
geometry = build_jps_geometry(geo)
geometry = jps.build_jps_geometry(geo)

model_builder = jps.VelocityModelBuilder(
a_ped=8, d_ped=0.1, a_wall=5, d_wall=0.02
Expand Down Expand Up @@ -44525,7 +44523,7 @@ def main():
agent_parameters.journey_id = random.choice(journeys)
simulation.add_agent(agent_parameters)

writer = SqliteTrajectoryWriter(
writer = jps.SqliteTrajectoryWriter(
pathlib.Path(
f"{jps.get_build_info().git_commit_hash}_grosser_stern.sqlite"
)
Expand Down Expand Up @@ -44565,7 +44563,7 @@ def main():
)
except KeyboardInterrupt:
writer.end_writing()
print("\nCTRL-C Recieved! Shuting down")
print("\nCTRL-C Received! Shutting down")
sys.exit(1)

writer.end_writing()
Expand Down
14 changes: 6 additions & 8 deletions performancetest/large_street_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import sys
import time

import py_jupedsim as jps
import shapely
from shapely import to_wkt

from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry
import jupedsim as jps
from performancetest.geometry import geometries
from performancetest.stats_writer import StatsWriter

Expand Down Expand Up @@ -200,7 +198,7 @@ def main():
jps.set_error_callback(log_error)

geo = shapely.from_wkt(geometries["large_street_network"])
geometry = build_jps_geometry(geo)
geometry = jps.build_jps_geometry(geo)

model_builder = jps.VelocityModelBuilder(
a_ped=8, d_ped=0.1, a_wall=5, d_wall=0.02
Expand Down Expand Up @@ -228,7 +226,7 @@ def main():
),
]

writer = SqliteTrajectoryWriter(
writer = jps.SqliteTrajectoryWriter(
pathlib.Path(
f"{jps.get_build_info().git_commit_hash}_large_street_network.sqlite"
)
Expand All @@ -246,9 +244,9 @@ def main():
for s in spawners:
s.spawn(iteration)
if (iteration + 100 * 30) % (100 * 60) == 0:
waiting_area.state = jps.WaitingSetState.Inactive
waiting_area.state = jps.WaitingSetState.INACTIVE
if iteration % (100 * 60) == 0:
waiting_area.state = jps.WaitingSetState.Active
waiting_area.state = jps.WaitingSetState.ACTIVE
if iteration % (100 * 8) == 0:
queue.pop(1)
if iteration % 50 == 0:
Expand Down Expand Up @@ -276,7 +274,7 @@ def main():
)
except KeyboardInterrupt:
writer.end_writing()
print("\nCTRL-C Recieved! Shuting down")
print("\nCTRL-C Received! Shutting down")
sys.exit(1)

writer.end_writing()
Expand Down
18 changes: 9 additions & 9 deletions python_bindings_jupedsim/bindings_jupedsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ PYBIND11_MODULE(py_jupedsim, m)
},
"Add area where agents can move")
.def(
"exclude_from_accssible_area",
"exclude_from_accessible_area",
[](const JPS_GeometryBuilder_Wrapper& w, std::vector<JPS_Point> polygon) {
JPS_GeometryBuilder_ExcludeFromAccessibleArea(
w.handle, polygon.data(), polygon.size());
Expand Down Expand Up @@ -321,14 +321,14 @@ PYBIND11_MODULE(py_jupedsim, m)
maxf_Wall));
}),
py::kw_only(),
py::arg("nuPed"),
py::arg("nuWall"),
py::arg("distEffPed"),
py::arg("distEffWall"),
py::arg("intpWidthPed"),
py::arg("intpWidthWall"),
py::arg("maxfPed"),
py::arg("maxfWall"))
py::arg("nu_ped"),
py::arg("nu_wall"),
py::arg("dist_eff_ped"),
py::arg("dist_eff_wall"),
py::arg("intp_width_ped"),
py::arg("intp_width_wall"),
py::arg("maxf_ped"),
py::arg("maxf_wall"))
.def(
"add_parameter_profile",
[](JPS_GCFMModelBuilder_Wrapper& w,
Expand Down
95 changes: 85 additions & 10 deletions python_modules/jupedsim/jupedsim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,88 @@
# Copyright © 2012-2023 Forschungszentrum Jülich GmbH
# SPDX-License-Identifier: LGPL-3.0-or-later
try:
from .py_jupedsim import *
except ModuleNotFoundError:
from py_jupedsim import *
from jupedsim.distributions import (
AgentNumberError,
IncorrectParameterError,
NegativeValueError,
OverlappingCirclesError,
distribute_by_density,
distribute_by_number,
distribute_by_percentage,
distribute_in_circles_by_density,
distribute_in_circles_by_number,
distribute_till_full,
)
from jupedsim.native.agent import Agent
from jupedsim.native.geometry import Geometry, GeometryBuilder
from jupedsim.native.journey import JourneyDescription
from jupedsim.native.library import (
BuildInfo,
get_build_info,
set_debug_callback,
set_error_callback,
set_info_callback,
set_warning_callback,
)
from jupedsim.native.models import (
GCFMModelAgentParameters,
GCFMModelBuilder,
GeneralizedCentrifugalForceModelState,
VelocityModelAgentParameters,
VelocityModelBuilder,
VelocityModelState,
)
from jupedsim.native.routing import RoutingEngine
from jupedsim.native.simulation import Simulation
from jupedsim.native.stages import (
ExitProxy,
NotifiableQueueProxy,
WaitingSetProxy,
WaitingSetState,
WaypointProxy,
)
from jupedsim.native.tracing import Trace
from jupedsim.recording import Recording, RecordingAgent, RecordingFrame
from jupedsim.trajectory_writer_sqlite import SqliteTrajectoryWriter
from jupedsim.util import build_jps_geometry

import jupedsim.aabb
import jupedsim.distributions
import jupedsim.grid
import jupedsim.serialization
import jupedsim.trajectory_writer_sqlite
import jupedsim.util
__all__ = [
"AgentNumberError",
"IncorrectParameterError",
"NegativeValueError",
"OverlappingCirclesError",
"distribute_by_density",
"distribute_by_number",
"distribute_by_percentage",
"distribute_in_circles_by_density",
"distribute_in_circles_by_number",
"distribute_till_full",
"Agent",
"Geometry",
"GeometryBuilder",
"JourneyDescription",
"BuildInfo",
"get_build_info",
"set_debug_callback",
"set_error_callback",
"set_info_callback",
"set_warning_callback",
"GCFMModelAgentParameters",
"GCFMModelBuilder",
"GeneralizedCentrifugalForceModelState",
"VelocityModelAgentParameters",
"VelocityModelBuilder",
"VelocityModelState",
"RoutingEngine",
"Simulation",
"ExitProxy",
"NotifiableQueueProxy",
"WaitingSetProxy",
"WaitingSetState",
"WaypointProxy",
"Trace",
"Recording",
"RecordingAgent",
"RecordingFrame",
"SqliteTrajectoryWriter",
"build_jps_geometry",
]
2 changes: 1 addition & 1 deletion python_modules/jupedsim/jupedsim/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import shapely.geometry as shply

from jupedsim.grid import Grid
from jupedsim.internal.grid import Grid


class AgentNumberError(Exception):
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d50778c

Please sign in to comment.