Skip to content

obs_helpers: seed noise from crc32 instead of builtin hash#315

Open
Lftw wants to merge 1 commit into
dev-backendfrom
fix/stable-noise-seed
Open

obs_helpers: seed noise from crc32 instead of builtin hash#315
Lftw wants to merge 1 commit into
dev-backendfrom
fix/stable-noise-seed

Conversation

@Lftw

@Lftw Lftw commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

What

The hash-seeded random helpers in obs_helpers.py (cerror_hash, hashrandn, hashrand, hashmultivariaterandn) seeded numpy from the builtin hash() of a repr string built from each quantity's identity (sites/time/pol/seed). They now route through a single new _stable_seed() helper that uses zlib.crc32.

Why

CPython salts str hashing per process (PYTHONHASHSEED), so the seed and all simulated thermal noise and gain/D-term errors changed on every run, even with the same seed argument. zlib.crc32 of the same text is identical across processes, machines, and Python versions, so simulated noise is now reproducible while preserving the per-quantity independence the helpers were designed for (draws keyed to a visibility's identity, independent of data ordering).

Surfaced while regression-testing simulated observations: clean (noise-free) obs were bit-identical across processes but noisy obs were not; pinning PYTHONHASHSEED=0 made them identical, which pointed at the salted hash().

How to test

  • pytest tests/test_obs_helpers.py pins the crc32 implementation and asserts drawn noise/gain values are byte-identical across PYTHONHASHSEED ∈ {0, 1, 999999} via subprocesses (fails on the old hash()).
  • Full calibration/noise sweep (test_obs_simulate, test_self_cal, test_network_cal, test_pol_cal, test_polgains_cal, test_caltable, test_obsdata): 340 passed.

Notes

  • Seed values change vs. the old code (crc32 ≠ hash), so noise realizations differ from previous runs but the old ones were never reproducible anyway.
  • Kept separate from the mixed-pol work.

The hash-seeded random helpers (cerror_hash, hashrandn, hashrand, hashmultivariaterandn) seeded numpy from the builtin hash() of a repr string. CPython salts str hashing per process (PYTHONHASHSEED), so simulated noise and gain/D-term errors were not reproducible across processes even with the same seed argument. Route all four through a new _stable_seed() helper using zlib.crc32 -- deterministic across processes, machines, and Python versions -- preserving the per-quantity independence the helpers rely on. Add tests/test_obs_helpers.py pinning the crc32 implementation and asserting identical draws across PYTHONHASHSEED via subprocesses.
@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 47.45%. Comparing base (8c5a439) to head (17edc7d).

Files with missing lines Patch % Lines
ehtim/observing/obs_helpers.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           dev-backend     #315   +/-   ##
============================================
  Coverage        47.44%   47.45%           
============================================
  Files               55       55           
  Lines            26984    26987    +3     
  Branches          4599     4599           
============================================
+ Hits             12803    12806    +3     
  Misses           12689    12689           
  Partials          1492     1492           

☔ View full report in Codecov by Harness.
📢 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.

@rohandahale

Copy link
Copy Markdown
Collaborator

@Lftw Looks good!

For the next PR, we can do local generator seeded through np.random.default_rng(np.random.SeedSequence(key)).standard_normal(), no global state.

@rohandahale rohandahale added this to the 2.0 milestone Jul 13, 2026
@rohandahale
rohandahale marked this pull request as ready for review July 13, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants