Skip to content

fix: raise clear error when window_size is too large#1152

Open
suhr25 wants to merge 11 commits intomalariagen:masterfrom
suhr25:fix/fst-gwss-empty-window-guard
Open

fix: raise clear error when window_size is too large#1152
suhr25 wants to merge 11 commits intomalariagen:masterfrom
suhr25:fix/fst-gwss-empty-window-guard

Conversation

@suhr25
Copy link
Copy Markdown
Contributor

@suhr25 suhr25 commented Mar 18, 2026

SUMMARY

Fixes a crash when window_size is too large in Fst GWSS by raising a clear error instead of returning empty data.


FIX

Before

x = allel.moving_statistic(pos, statistic=np.mean, size=window_size)
return dict(x=x, fst=fst)

After

x = allel.moving_statistic(pos, statistic=np.mean, size=window_size)

if len(x) == 0:
    raise ValueError("window_size is larger than available SNPs")

return dict(x=x, fst=fst)

VERIFICATION

Using a large window_size on a small region used to crash with an IndexError. Now it raises a clear ValueError. A test confirms this behavior.

…gwss

Signed-off-by: Suhrid Marwah <suhridmarwah07@gmail.com>
@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Mar 18, 2026

Hi @jonbrenas,
Kindly take a look at this PR.
Thanks!

@jonbrenas
Copy link
Copy Markdown
Collaborator

Hi @suhr25, would there be a way to instead raise a warning and modify the window_size so that a result is returned?

Replace hard error when window_size exceeds available SNPs with a warning,
and automatically adjust window_size to the maximum valid value so that
computation can proceed.

Signed-off-by: Suhrid Marwah <suhridmarwah07@gmail.com>
@suhr25 suhr25 force-pushed the fix/fst-gwss-empty-window-guard branch from 1b9797e to a05bab0 Compare March 19, 2026 06:09
@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Mar 19, 2026

Thanks for the suggestion! @jonbrenas
I have updated the behavior to issue a warning and automatically adjust window_size to the available SNP count so the computation can proceed. Also updated tests and documentation accordingly.

Let me know if you would prefer any different handling.

@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Mar 20, 2026

Hi @jonbrenas,
Kindly take a look,Thanks!

@jonbrenas
Copy link
Copy Markdown
Collaborator

Thanks @suhr25,

Because we want to have a moving statistics, the window size would need to be a factor smaller than the number of SNPs (let's say, 10 by default but it should be a parameter). The window size should thus be adjusted, unless it falls under a given number (again it should be a parameter with a default value say 1 000), in which your original error message probably becomes the solution.

@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Mar 20, 2026

Sure thanks,
I will look into it.

@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Mar 29, 2026

Thanks, that makes sense. I've updated the implementation to automatically adjust window_size when possible and only raise an error for very small SNP counts. Let me know if you prefer different defaults.

@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Apr 3, 2026

Hi @jonbrenas ,
Kindly review this PR.
Thanks!

@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Apr 9, 2026

Hi @jonbrenas,
Gentle follow-up on this PR.
Thanks!

@jonbrenas
Copy link
Copy Markdown
Collaborator

Hi @suhr25,

Both _min_snps_threshold and _window_adjustment_factor should be parameters of the function (with these values by default as most people will not set values for them).

@suhr25 suhr25 force-pushed the fix/fst-gwss-empty-window-guard branch from ce9a455 to 2d0c75f Compare April 9, 2026 12:41
suhr25 and others added 3 commits April 9, 2026 18:38
- Define min_snps_threshold and window_adjustment_factor as Annotated
  TypeAlias in fst_params.py so numpydoc_decorator 2.2.1 can extract
  their documentation automatically (plain int params without Annotated
  or explicit parameters dict cause DocumentationError at import time)
- Add explicit parameters dict entries to @doc(fst_gwss) as belt-and-
  suspenders documentation for both new parameters
- Switch fst_gwss signature from plain int to fst_params.min_snps_threshold
  and fst_params.window_adjustment_factor (consistent with codebase style)
- Replace unseeded random.choice/random.sample in new tests with seeded
  np.random.choice (matching all existing tests that use np.random seeded
  with 42 via the autouse session fixture)
- Remove import random from test_fst.py (no longer needed)
@suhr25
Copy link
Copy Markdown
Contributor Author

suhr25 commented Apr 10, 2026

Hi @jonbrenas,
Made changes which you asked for ,kindly take a look.
Thanks!

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