Skip to content

Vectorize numpy operations in structure splitting#592

Open
aymuos15 wants to merge 3 commits intobrainglobe:mainfrom
aymuos15:vectorize-structure-splitting
Open

Vectorize numpy operations in structure splitting#592
aymuos15 wants to merge 3 commits intobrainglobe:mainfrom
aymuos15:vectorize-structure-splitting

Conversation

@aymuos15
Copy link
Copy Markdown

Description

What is this PR

  • Other

Refactoring / performance improvement.

Why is this PR needed?

The post-processing in split_cells used element-by-element Python loops and a per-point helper function (check_centre_in_cuboid) instead of idiomatic vectorized numpy. This is predominantly slower and the patch is much cleaner.

What does this PR do?

Replaces three loop/element-wise patterns in split_cells() with vectorized numpy equivalents:

  1. relative_orig_centre — element-by-element subtraction → orig_centre - orig_corner
  2. Outlier filtering — Python list comprehension calling check_centre_in_cuboid per point → np.all(..., axis=1) boolean mask
  3. absolute_centres — per-column addition into a pre-allocated array → relative_centres + orig_corner

The helper function check_centre_in_cuboid is removed as its logic is now inlined.

Benchmark results (median, 50 repeats)

n_centres old (µs) new (µs) speedup
10 32.3 15.3 2.11x
100 389.9 7.9 49.50x
1,000 1421.2 34.9 40.66x
10,000 14850.2 276.7 53.67x
100,000 149660.4 2817.0 53.13x

Correctness verified: old and new outputs match (np.allclose) at every size.

References

N/A

How has this PR been tested?

  • Existing integration test test_underflow_issue_435 exercises split_cells end-to-end and continues to pass.
  • Standalone benchmark script verified correctness via np.allclose between old and new implementations across 5 input sizes.

Is this a breaking change?

No. Pure internal refactor with identical outputs.

Does this PR require an update to the documentation?

No.

Checklist:

  • The code has been tested locally
  • Tests have been added to cover all new functionality (unit & integration)
  • The documentation has been updated to reflect any changes
  • The code has been formatted with pre-commit"

@aymuos15
Copy link
Copy Markdown
Author

aymuos15 commented Feb 20, 2026

I realise the codecov tests fail because of the tests. Should I patch this as well within this PR? #285

@adamltyson
Copy link
Copy Markdown
Member

Yes, please make sure all code is tested.

aymuos15 and others added 2 commits February 20, 2026 17:10
Tests the vectorized masking logic that was refactored in
structure splitting optimization. Ensures code coverage for
the new vectorized array operations.

partially attempting issue brainglobe#285.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants