Skip to content

Commit ce4305a

Browse files
committed
Skip tests that use scikit-allel if not installed
1 parent ed1beea commit ce4305a

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

sgkit/tests/io/vcf/test_vcf_scikit_allel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from pathlib import Path
2323
from typing import Any
2424

25-
import allel
2625
import numpy as np
2726
import pytest
2827
import xarray as xr
@@ -32,6 +31,8 @@
3231
from sgkit.io.utils import INT_FILL, INT_MISSING
3332
from sgkit.io.vcf import vcf_to_zarr
3433

34+
allel = pytest.importorskip("allel")
35+
3536

3637
def assert_identical(ds1: Dataset, ds2: Dataset) -> None:
3738
"""Assert two Datasets are identical, including dtypes for all variables, except strings."""

sgkit/tests/test_pca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Any, Optional
22

3-
import allel
43
import dask.array as da
54
import numpy as np
65
import pytest
@@ -54,6 +53,7 @@ def simulate_dataset(
5453

5554

5655
def allel_pca(gn: ArrayLike, randomized: bool = False, **kwargs: Any) -> Dataset:
56+
allel = pytest.importorskip("allel")
5757
fn = allel.randomized_pca if randomized else allel.pca
5858
pcs, est = fn(gn, **kwargs)
5959
return xr.Dataset(

sgkit/tests/test_popgen.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import itertools
22

3-
import allel
43
import dask.array as da
54
import msprime # type: ignore
65
import numpy as np
76
import pytest
87
import tskit # type: ignore
98
import xarray as xr
10-
from allel import hudson_fst
119
from hypothesis import given, settings
1210
from hypothesis import strategies as st
1311

@@ -136,6 +134,8 @@ def test_diversity(sample_size, chunks, cohort_allele_count):
136134

137135
@pytest.mark.parametrize("sample_size", [10])
138136
def test_diversity__windowed(sample_size):
137+
allel = pytest.importorskip("allel")
138+
139139
ts = simulate_ts(sample_size, length=200)
140140
ds = ts_to_dataset(ts)
141141
ds, subsets = add_cohorts(ds, ts, cohort_key_names=["cohorts"])
@@ -223,6 +223,8 @@ def test_divergence__windowed(sample_size, n_cohorts, chunks):
223223
@pytest.mark.parametrize("chunks", [(-1, -1), (50, -1)])
224224
@pytest.mark.xfail() # combine with test_divergence__windowed when this is passing
225225
def test_divergence__windowed_scikit_allel_comparison(sample_size, n_cohorts, chunks):
226+
allel = pytest.importorskip("allel")
227+
226228
ts = simulate_ts(sample_size, length=200)
227229
ds = ts_to_dataset(ts, chunks)
228230
ds, subsets = add_cohorts(ds, ts, n_cohorts)
@@ -272,6 +274,8 @@ def test_divergence__missing_calls():
272274

273275
@pytest.mark.parametrize("sample_size", [2, 3, 10, 100])
274276
def test_Fst__Hudson(sample_size):
277+
allel = pytest.importorskip("allel")
278+
275279
# scikit-allel can only calculate Fst for pairs of cohorts (populations)
276280
n_cohorts = 2
277281
ts = simulate_ts(sample_size)
@@ -285,7 +289,7 @@ def test_Fst__Hudson(sample_size):
285289
# scikit-allel
286290
ac1 = ds.cohort_allele_count.values[:, 0, :]
287291
ac2 = ds.cohort_allele_count.values[:, 1, :]
288-
num, den = hudson_fst(ac1, ac2)
292+
num, den = allel.hudson_fst(ac1, ac2)
289293
ska_fst = np.sum(num) / np.sum(den)
290294

291295
np.testing.assert_allclose(fst, ska_fst)
@@ -326,6 +330,8 @@ def test_Fst__unknown_estimator():
326330
)
327331
@pytest.mark.parametrize("chunks", [(-1, -1), (50, -1)])
328332
def test_Fst__windowed(sample_size, n_cohorts, chunks):
333+
allel = pytest.importorskip("allel")
334+
329335
ts = simulate_ts(sample_size, length=200)
330336
ds = ts_to_dataset(ts, chunks)
331337
ds, subsets = add_cohorts(ds, ts, n_cohorts)
@@ -395,6 +401,8 @@ def test_Tajimas_D_per_site(sample_size):
395401
[(10, 3), (20, 4)],
396402
)
397403
def test_pbs(sample_size, n_cohorts):
404+
allel = pytest.importorskip("allel")
405+
398406
ts = simulate_ts(sample_size)
399407
ds = ts_to_dataset(ts)
400408
ds, subsets = add_cohorts(
@@ -432,6 +440,8 @@ def test_pbs(sample_size, n_cohorts):
432440
)
433441
@pytest.mark.parametrize("chunks", [(-1, -1), (50, -1)])
434442
def test_pbs__windowed(sample_size, n_cohorts, cohorts, cohort_indexes, chunks):
443+
allel = pytest.importorskip("allel")
444+
435445
ts = simulate_ts(sample_size, length=200)
436446
ds = ts_to_dataset(ts, chunks)
437447
ds, subsets = add_cohorts(
@@ -475,6 +485,8 @@ def test_pbs__windowed(sample_size, n_cohorts, cohorts, cohort_indexes, chunks):
475485
def test_Garud_h(
476486
n_variants, n_samples, n_contigs, n_cohorts, cohorts, cohort_indexes, chunks
477487
):
488+
allel = pytest.importorskip("allel")
489+
478490
ds = simulate_genotype_call_dataset(
479491
n_variant=n_variants, n_sample=n_samples, n_contig=n_contigs
480492
)
@@ -674,6 +686,8 @@ def test_observed_heterozygosity__windowed(chunks, cohorts, expectation):
674686
def test_observed_heterozygosity__scikit_allel_comparison(
675687
n_variant, n_sample, missing_pct, window_size, seed
676688
):
689+
allel = pytest.importorskip("allel")
690+
677691
ds = simulate_genotype_call_dataset(
678692
n_variant=n_variant,
679693
n_sample=n_sample,

sgkit/tests/test_preprocessing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from contextlib import nullcontext
22
from typing import Any
33

4-
import allel.stats.preprocessing
54
import dask
65
import dask.array as da
76
import numpy as np
@@ -30,6 +29,8 @@ def simulate_alternate_allele_counts(
3029
@pytest.mark.parametrize("shape", [(100, 50), (50, 100), (50, 50)])
3130
@pytest.mark.parametrize("ploidy", [2, 4])
3231
def test_patterson_scaler__allel_comparison(shape, ploidy):
32+
allel = pytest.importorskip("allel")
33+
3334
ac = simulate_alternate_allele_counts(*shape, ploidy=ploidy) # type: ignore[misc]
3435
expected = sgkit.stats.preprocessing.PattersonScaler(ploidy=ploidy).fit_transform(
3536
ac

sgkit/tests/test_vcfzarr_reader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import allel
21
import numpy as np
32
import pytest
43
import xarray as xr
@@ -8,6 +7,8 @@
87
from sgkit import read_scikit_allel_vcfzarr
98
from sgkit.io.vcfzarr_reader import _ensure_2d, vcfzarr_to_zarr
109

10+
allel = pytest.importorskip("allel")
11+
1112

1213
def create_vcfzarr(
1314
shared_datadir, tmpdir, *, fields=None, grouped_by_contig=False, consolidated=False

sgkit/tests/test_window.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22

3-
import allel
43
import dask.array as da
54
import numpy as np
65
import pandas as pd
@@ -29,6 +28,8 @@
2928
)
3029
@pytest.mark.parametrize("dtype", [np.int64, np.float32, np.float64])
3130
def test_moving_statistic_1d(length, chunks, size, step, dtype):
31+
allel = pytest.importorskip("allel")
32+
3233
values = da.from_array(np.arange(length, dtype=dtype), chunks=chunks)
3334

3435
stat = moving_statistic(values, np.sum, size=size, step=step, dtype=values.dtype)
@@ -49,6 +50,8 @@ def test_moving_statistic_1d(length, chunks, size, step, dtype):
4950
)
5051
@pytest.mark.parametrize("dtype", [np.int64, np.float32, np.float64])
5152
def test_moving_statistic_2d(length, chunks, size, step, dtype):
53+
allel = pytest.importorskip("allel")
54+
5255
arr = np.arange(length * 3, dtype=dtype).reshape(length, 3)
5356

5457
def sum_cols(x):

0 commit comments

Comments
 (0)