fix: add guard for empty array in g123 when window_size exceeds avail#937
Open
31puneet wants to merge 2 commits intomalariagen:masterfrom
Open
fix: add guard for empty array in g123 when window_size exceeds avail#93731puneet wants to merge 2 commits intomalariagen:masterfrom
31puneet wants to merge 2 commits intomalariagen:masterfrom
Conversation
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.
Description
Fixes #571
Relates to #695
This PR fixes the intermittent
IndexErrorin G123 tests (test_g123_gwss_...[af1_sim]andtest_g123_calibration[af1_sim]).The Bug
allel.moving_statistic(gt, ..., size=window_size)returns an empty array (0,) whengt.shape[0] < window_size. Downstream code that indexes into this array (x[0],x[-1]) then crashes with:Why It's Intermittent
The test fixture in conftest.py used
p_site=np.random.random(), and the test in test_g123.py selects randomwindow_sizevalues between 100-500. When a low p_site is randomly chosen (resulting in few sites) and a largewindow_sizeis picked, the number of sites falls below the window size, triggering the crash.Changes Made
1. g123.py
ValueErrorguard in _g123_gwss (beforeallel.moving_statistic) that checks ifgt.shape[0] < window_sizewindow_sizesloop)2. conftest.py
p_site=np.random.random()→p_site=np.random.uniform(0.5, 1.0)inAf1Simulator.init_hap_sites()to ensure test data always generates sufficient haplotype sitesTest Results
Before Fix (no guard,
p_site=np.random.random())Ran
test_g123.py -k "af1_sim"in a loop of 20:IndexErrorontest_g123_gwss_with_phased_sites[af1_sim]andtest_g123_calibration[af1_sim]After Fix (guard added,
p_site=np.random.uniform(0.5, 1.0))Ran
test_g123.py -k "af1_sim"in a loop of 50:Also verified related test suites (
test_h12,test_h1x,test_fst) with af1_sim — 20/20 passed.Impact
window_sizelarger than available sites will now get a clearValueErrorinstead of a confusingIndexError