Skip to content

Improve profile computation memory efficiency#525

Draft
schroedtert wants to merge 16 commits intoPedestrianDynamics:mainfrom
schroedtert:improve-large-traj-profiles
Draft

Improve profile computation memory efficiency#525
schroedtert wants to merge 16 commits intoPedestrianDynamics:mainfrom
schroedtert:improve-large-traj-profiles

Conversation

@schroedtert
Copy link
Copy Markdown
Collaborator

Profile functions (compute_density_profile, compute_speed_profile, compute_profiles) no longer require pre-computed grid intersections. When grid_intersections_area is not provided, intersections are computed per frame internally, avoiding the allocation of the full (grid_cells × pedestrians) matrix.

  • Add _compute_frame_grid_intersection helper for single-frame intersection
  • Change _compute_grid_polygon_intersection to process frame-by-frame using NumPy instead of a single broadcast
  • Make grid_intersections_area optional in compute_density_profile and compute_speed_profile (on-the-fly when omitted)
  • Simplify compute_profiles to delegate to compute_density_profile and compute_speed_profile per frame with shared intersection
  • Update user guide to use the simpler API without compute_grid_cell_polygon_intersection_area

@schroedtert schroedtert added the profiles Anything related to computing profiles label Mar 28, 2026
@schroedtert schroedtert requested a review from Copilot March 28, 2026 16:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates PedPy’s profile computation APIs to avoid requiring a precomputed full grid×pedestrian intersection matrix, enabling per-frame intersection computation to reduce peak memory usage for large datasets.

Changes:

  • Adds _compute_frame_grid_intersection and updates profile methods to compute intersections per frame when grid_intersections_area is omitted.
  • Refactors compute_profiles to iterate frame-by-frame and share per-frame intersections between density and speed computations.
  • Updates plotting title handling, notebooks/user guide examples, and pre-commit tooling (including nbstripout metadata stripping).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pedpy/plotting/plotting.py Ensures plot titles aren’t overwritten by plot_walkable_area defaults.
pedpy/methods/profile_calculator.py Implements per-frame intersection computation and updates profile APIs/logic accordingly.
notebooks/user_guide.ipynb Updates examples to use the simplified API without precomputing intersections.
notebooks/getting_started.ipynb Strips notebook kernel metadata (via nbstripout configuration).
notebooks/fundamental_diagram_at_measurement_line.ipynb Strips notebook kernel metadata.
notebooks/fundamental_diagram.ipynb Strips notebook kernel metadata.
.pre-commit-config.yaml Bumps tool versions and configures nbstripout to remove kernel/language metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@schroedtert schroedtert added the usability Anything increasing the usablity label Mar 28, 2026
@chraibi chraibi requested a review from Copilot April 7, 2026 20:19
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chraibi
Copy link
Copy Markdown
Collaborator

chraibi commented Apr 7, 2026

Benchmark results

Ran scripts/bench_profiles.py --frames 300 --peds 150 --repeats 1 (45k rows, 20×20 grid) on main vs this branch:

Metric main this branch
compute_profiles time 24.9s 15.1s
compute_profiles peak memory 1238 MB 7.3 MB
Precomputation time 12.6s 7.7s
density_profile (precomputed) 0.12s 0.10s

Peak memory drops from 1.2 GB → 7 MB (~170× reduction) with a ~40% speed improvement on the combined path. The per-frame approach trades some compute overhead for dramatically lower memory usage, which was the goal.

Benchmark script added in scripts/bench_profiles.py for reproducibility.

@chraibi chraibi marked this pull request as ready for review April 7, 2026 20:41
@chraibi chraibi requested review from JuleAdrian and Copilot April 7, 2026 20:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

schroedtert and others added 13 commits April 7, 2026 22:57
Used pre-commit autoupdate to get the dependency revisions to the
current version. Additionally sync the dependencies from mirror-mypy
with project dependencies.
Profile functions (compute_density_profile, compute_speed_profile,
compute_profiles) no longer require pre-computed grid intersections.
When grid_intersections_area is not provided, intersections are computed
per frame internally, avoiding the allocation of the full
(grid_cells × pedestrians) matrix.

- Add _compute_frame_grid_intersection helper for single-frame intersection
- Change _compute_grid_polygon_intersection to process frame-by-frame
  using NumPy instead of a single broadcast
- Make grid_intersections_area optional in compute_density_profile and
  compute_speed_profile (on-the-fly when omitted)
- Simplify compute_profiles to delegate to compute_density_profile and
  compute_speed_profile per frame with shared intersection
- Update user guide to use the simpler API without
  compute_grid_cell_polygon_intersection_area
Let compute_speed_profile use its own default (0.5) when
gaussian_width is not provided to compute_profiles, instead of
hardcoding the value at the call site.
… work

compute_profiles was calling compute_density_profile and
compute_speed_profile per frame, which each redundantly called
get_grid_cells() and data.groupby(FRAME_COL) again. Call the
internal _compute_*_profile helpers directly instead, computing
grid cells, centroids, and bounds once up front.
…section

Remove duplicated intersection logic by delegating to the
single-frame helper introduced in this PR.
Return an empty (num_grid_cells, 0) array instead of crashing
with ValueError from np.concatenate on an empty list.
Only compute grid cell centroids when a Gaussian method is selected.
Use shapely.centroid(grid_cells) instead of np.vectorize(shapely.centroid)
for better performance.
The parameter is used for both DensityMethod.GAUSSIAN and
SpeedMethod.GAUSSIAN in compute_profiles.
Verify that compute_density_profile and compute_speed_profile produce
identical results whether grid_intersections_area is precomputed or
computed on-the-fly per frame.
Measures wall time and peak memory for compute_profiles,
compute_density_profile, and compute_speed_profile with both
precomputed and on-the-fly intersection paths.
@chraibi chraibi force-pushed the improve-large-traj-profiles branch from ecdceba to a2ca5be Compare April 7, 2026 20:58
chraibi added 2 commits April 7, 2026 23:11
Variables like grid_intersections_area_frame and center_x/center_y
are guaranteed non-None by the needs_intersection/needs_grid_centers
guards, but mypy cannot infer this across branches.
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 67.74194% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.45%. Comparing base (7299724) to head (43c8fe8).

Files with missing lines Patch % Lines
pedpy/methods/profile_calculator.py 67.74% 20 Missing ⚠️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chraibi chraibi removed the request for review from JuleAdrian April 8, 2026 09:43
@schroedtert schroedtert marked this pull request as draft April 9, 2026 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

profiles Anything related to computing profiles usability Anything increasing the usablity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants