Skip to content

fix: real-valued high BC anisotropy on NumPy >= 2 - #133

Merged
cbueth merged 1 commit into
mainfrom
fix/numpy-complex-anisotropy
Aug 24, 2026
Merged

fix: real-valued high BC anisotropy on NumPy >= 2#133
cbueth merged 1 commit into
mainfrom
fix/numpy-complex-anisotropy

Conversation

@cbueth

@cbueth cbueth commented Aug 24, 2026

Copy link
Copy Markdown
Member

Description

Scheduled CI on main failed in the Partitioning and Metrics jobs: raised RepresenterError on values like (5.73+0j), and the anisotropy special-case tests got inf+nanj instead of inf.

There was a upstream change for NumPy >= 2, where np.linalg.eigvals returns complex128 eigenvalues even for real symmetric covariance matrices. This leaked complex scalars into the metrics and key figures.

This PR switches the calculation to np.linalg.eigvalsh (always real for symmetric input), clips numerical-noise negative eigenvalues, and returns a plain float. As a fallback, _make_yaml_compatible now collapses zero-imaginary complex scalars to float, so YAML dumps can't crash on a stray complex value again.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 📝 Documentation update
  • 🧹 Code refactor (no functional changes)
  • ✅ Test update

How Has This Been Tested?

  • Unit tests
  • Integration tests
  • Manual testing

Test configuration:

  • OS: MacOS, NumPy 2.5.2
  • pytest "tests/metrics/test_metric.py::TestMetric::test_saving_and_loading" -k "ResidentialPart or BetweennessPart"
  • pytest tests/partitioning/test_base.py -k "save_key_figures and Adliswil"

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally

@cbueth cbueth self-assigned this Aug 24, 2026
@cbueth cbueth added the bug Something isn't working label Aug 24, 2026
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

NumPy >= 2 returns complex128 eigenvalues from np.linalg.eigvals even
for real symmetric covariance matrices. This leaked complex scalars
into the metrics, crashing ruamel.yaml key figure dumps with a
RepresenterError and yielding inf+nanj instead of inf for degenerate
covariances.

Use eigvalsh (always real for symmetric input) and clip numerical-noise
negative eigenvalues; additionally make _make_yaml_compatible collapse
zero-imaginary complex scalars to float as a fallback.
@cbueth
cbueth force-pushed the fix/numpy-complex-anisotropy branch from 5d8039f to 132bfa7 Compare August 24, 2026 17:01
@cbueth
cbueth merged commit a756638 into main Aug 24, 2026
86 of 87 checks passed
@cbueth
cbueth deleted the fix/numpy-complex-anisotropy branch August 24, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant