Skip to content

Commit 600111e

Browse files
committed
fix: resolve NumPy 2.x boolean ambiguity in isel slicing paths
Convert boolean mask indexing to integer indices in xarray .isel() calls to fix NumPy 2.x ValueError in frequency analysis functions. Changes: - snp_frq.py: snp_allele_frequencies_advanced (line 633) - snp_frq.py: aa_allele_frequencies_advanced (line 771) - cnv_frq.py: gene_cnv_frequencies_advanced (lines 638, 644) Pattern applied: Replace ds.isel(variants=bool_mask) with: variant_indices = np.where(bool_mask)[0] ds.isel(variants=variant_indices) Fixes 885 test failures in test_allele_frequencies_advanced* tests. Maintains identical behavior with NumPy 1.26.x while achieving NumPy 2.x compatibility.
1 parent 55f4f3b commit 600111e

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

malariagen_data/anoph/cnv_frq.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,17 @@ def _gene_cnv_frequencies_advanced(
635635
debug("deal with invariants")
636636
if drop_invariant:
637637
loc_variant = df_variants["max_af"].values > 0
638-
ds_out = ds_out.isel(variants=loc_variant)
638+
# Convert boolean mask to integer indices for NumPy 2.x compatibility
639+
variant_indices = np.where(loc_variant)[0]
640+
ds_out = ds_out.isel(variants=variant_indices)
639641
df_variants = df_variants.loc[loc_variant].reset_index(drop=True)
640642

641643
debug("apply variant query")
642644
if variant_query is not None:
643645
loc_variants = df_variants.eval(variant_query).values
644-
ds_out = ds_out.isel(variants=loc_variants)
646+
# Convert boolean mask to integer indices for NumPy 2.x compatibility
647+
variant_indices = np.where(loc_variants)[0]
648+
ds_out = ds_out.isel(variants=variant_indices)
645649

646650
debug("add confidence intervals")
647651
_add_frequency_ci(ds=ds_out, ci_method=ci_method)

malariagen_data/anoph/snp_frq.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,9 @@ def snp_allele_frequencies_advanced(
630630
f"No SNPs remaining after applying variant query {variant_query!r}."
631631
)
632632

633-
ds_out = ds_out.isel(variants=loc_variants)
633+
# Convert boolean mask to integer indices for NumPy 2.x compatibility
634+
variant_indices = np.where(loc_variants)[0]
635+
ds_out = ds_out.isel(variants=variant_indices)
634636

635637
# Add confidence intervals.
636638
_add_frequency_ci(ds=ds_out, ci_method=ci_method)
@@ -768,7 +770,9 @@ def aa_allele_frequencies_advanced(
768770
f"No SNPs remaining after applying variant query {variant_query!r}."
769771
)
770772

771-
ds_aa_frq = ds_aa_frq.isel(variants=loc_variants)
773+
# Convert boolean mask to integer indices for NumPy 2.x compatibility
774+
variant_indices = np.where(loc_variants)[0]
775+
ds_aa_frq = ds_aa_frq.isel(variants=variant_indices)
772776

773777
# Compute new confidence intervals.
774778
_add_frequency_ci(ds=ds_aa_frq, ci_method=ci_method)

0 commit comments

Comments
 (0)