Skip to content

Commit a500e8a

Browse files
committed
Add intrusion computation
1 parent e052536 commit a500e8a

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

pedpy/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
compute_passing_speed,
5656
compute_voronoi_speed,
5757
)
58+
from .methods.foo_calculator import compute_intrusion, IntrusionMethod
5859
from .plotting.plotting import (
5960
PEDPY_BLUE,
6061
PEDPY_GREEN,
@@ -116,6 +117,8 @@
116117
"compute_mean_speed_per_frame",
117118
"compute_passing_speed",
118119
"compute_voronoi_speed",
120+
"compute_intrusion",
121+
"IntrusionMethod",
119122
"PEDPY_BLUE",
120123
"PEDPY_GREEN",
121124
"PEDPY_GREY",

pedpy/column_identifier.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@
2929
START_POSITION_COL: Final = "start_position"
3030
END_POSITION_COL: Final = "end_position"
3131
WINDOW_SIZE_COL: Final = "window_size"
32+
33+
INTRUSION_COL: Final = "intrusion"

pedpy/methods/foo_calculator.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Foo."""
2+
3+
4+
from enum import Enum
5+
6+
from pedpy.data.trajectory_data import TrajectoryData
7+
from .method_utils import _compute_individual_distances
8+
from pedpy.column_identifier import FRAME_COL, ID_COL, INTRUSION_COL
9+
import pandas
10+
11+
12+
class IntrusionMethod(Enum): # pylint: disable=too-few-public-methods
13+
"""Identifier of method to compute the intrusion."""
14+
15+
MAX = "max"
16+
"""Use the max value in neighborhood as intrusion."""
17+
SUM = "sum"
18+
"""Use the sum of all values in neighborhood as intrusion."""
19+
20+
21+
def compute_intrusion(
22+
*,
23+
traj_data: TrajectoryData,
24+
r_soc: float = 0.8,
25+
l_min: float = 0.2,
26+
method: IntrusionMethod = IntrusionMethod.SUM,
27+
) -> pandas.DataFrame:
28+
"""_summary_.
29+
30+
TODO add documentation here
31+
Args:
32+
traj_data (TrajectoryData): _description_
33+
r_soc (float): _description_
34+
l_min (float): _description_
35+
method (IntrusionMethod, optional): _description_. Defaults to IntrusionMethod.SUM.
36+
37+
Returns:
38+
pandas.DataFrame: _description_
39+
"""
40+
intrusion = _compute_individual_distances(traj_data=traj_data)
41+
intrusion[INTRUSION_COL] = (
42+
(r_soc - l_min) / (intrusion.distance - l_min)
43+
) ** 2
44+
intrusion = (
45+
intrusion.groupby(by=[ID_COL, FRAME_COL])
46+
.agg(
47+
intrusion=(INTRUSION_COL, method.value),
48+
)
49+
.reset_index()
50+
)
51+
52+
return intrusion

pedpy/methods/method_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,23 @@ def _check_crossing_in_frame_range(
947947
crossed[column_name] = crossed[column_name] == "both"
948948
crossed = crossed[crossed[column_name]]
949949
return crossed
950+
951+
952+
def _compute_individual_distances(*, traj_data: TrajectoryData) -> pd.DataFrame:
953+
matrix = pd.merge(
954+
traj_data.data[["id", "frame", "point"]],
955+
traj_data.data[["id", "frame", "point"]],
956+
how="outer",
957+
on="frame",
958+
suffixes=("", "_neighbor"),
959+
)
960+
matrix = matrix[matrix.id != matrix.id_neighbor]
961+
matrix["distance"] = np.linalg.norm(
962+
shapely.get_coordinates(matrix.point)
963+
- shapely.get_coordinates(matrix.point_neighbor),
964+
axis=1,
965+
)
966+
967+
matrix = matrix.drop(columns=["point", "point_neighbor"])
968+
matrix = matrix.rename(columns={"id_neighbor": "neighbor"})
969+
return matrix.reset_index(drop=True)

0 commit comments

Comments
 (0)