Fix get_j() feeding uninitialized memory into the Junge slope fit - #406
Open
animmosmith wants to merge 3 commits into
Open
Conversation
4 tasks
animmosmith
added a commit
that referenced
this pull request
Jul 31, 2026
5 tasks
animmosmith
changed the base branch from
main
to
403-streamline-test-suite-with-shared-fixtures
July 31, 2026 17:00
emlynjdavies
previously approved these changes
Aug 5, 2026
Fixes #405. get_j() used np.log's `where=` to skip non-positive number_distribution bins (to avoid log(0)/log(negative)), added in #314 specifically to "handle zero values in number_distribution[ind]". But `where=` without a matching `out=` leaves the skipped positions as uninitialized memory rather than excluding them, and that array was fed straight into np.polyfit for the Junge slope fit - a real, user-facing value returned by nc_vc_from_stats() and stored in image_stats['junge']. Excludes non-positive bins from the fit outright (properly completing the intent of #314's fix), and returns NaN when fewer than two bins remain in the 150-300um fitting range (e.g. an image with no particles in that range), rather than crashing or fitting garbage - matching how other undefined/no-data cases are already handled elsewhere in the codebase. The 150-300um range itself is untouched: it's a deliberate choice (per the existing comment) to fit the Junge slope only where LISST-100 and SilCam data are considered mutually valid, not an arbitrary restriction.
animmosmith
force-pushed
the
405-fix-junge-slope-uninitialized-memory
branch
from
August 5, 2026 10:07
6682b57 to
607665d
Compare
…hared-fixtures' into 405-fix-junge-slope-uninitialized-memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #405. Found via a NumPy
UserWarningsurfaced in #404's CI (an unrelated PR - just where it was noticed).get_j()usednp.log'swhere=to skip non-positivenumber_distributionbins (to avoidlog(0)/log(negative)), added in #314 specifically to "handle zero values in number_distribution[ind]". Butwhere=without a matchingout=leaves the skipped positions as uninitialized memory rather than excluding them, and that array was fed straight intonp.polyfitfor the Junge slope fit — a real, user-facing value returned bync_vc_from_stats()and stored inimage_stats['junge'].Fix: excludes non-positive bins from the fit outright (properly completing the intent of #314's original fix, rather than changing it), and returns
NaNwhen fewer than two bins remain in the 150-300um fitting range (e.g. an image with no particles in that range at all), rather than crashing or fitting garbage — matching how other undefined/no-data cases are already handled elsewhere in the codebase (e.g.PerClassConcentration's empty-image handling).The 150-300um range itself is untouched — per the existing code comment, it's a deliberate choice to fit the slope only where LISST-100 and SilCam data are considered mutually valid/comparable, not an arbitrary restriction, so I didn't touch that part.
Test plan
test_get_j_excludes_non_positive_bins_from_fit— confirmed it fails against the old code (uninitialized memory produces a different, wrong slope) and passes with the fixtest_get_j_returns_nan_when_no_particles_in_fitting_range— covers the "zero valid bins" edge case that the naive exclude-fix alone would've crashed on (this exact case is exercised by the existingtest_calculate_image_stats_uses_configured_path_length, which has only one synthetic particle far outside the 150-300um range)pyopia/tests/test_pipeline.py(7 tests, including the real holo/silcam pipeline tests) passes🤖 Generated with Claude Code