Skip to content

Commit 6b38be3

Browse files
committed
Fix coverage
1 parent 10debe6 commit 6b38be3

12 files changed

+83
-11
lines changed

sgkit/io/bgen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .bgen_reader import bgen_to_zarr, read_bgen, rechunk_bgen
33

44
__all__ = ["read_bgen", "bgen_to_zarr", "rechunk_bgen"]
5-
except ImportError as e:
5+
except ImportError as e: # pragma: no cover
66
msg = (
77
"sgkit bgen requirements are not installed.\n\n"
88
"Please install them via pip :\n\n"

sgkit/io/plink/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .plink_reader import read_plink
33

44
__all__ = ["read_plink"]
5-
except ImportError as e:
5+
except ImportError as e: # pragma: no cover
66
msg = (
77
"sgkit plink requirements are not installed.\n\n"
88
"Please install them via pip :\n\n"

sgkit/io/vcf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"vcf_to_zarrs",
1212
"zarrs_to_dataset",
1313
]
14-
except ImportError as e:
14+
except ImportError as e: # pragma: no cover
1515
if platform.system() == "Windows":
1616
msg = (
1717
"sgkit-vcf is not supported on Windows.\n"

sgkit/io/vcfzarr_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _vcfzarr_to_dataset(
169169
if "variants/ID" in vcfzarr:
170170
variants_id = da.from_zarr(vcfzarr["variants/ID"]).astype(str)
171171
else:
172-
variants_id = None
172+
variants_id = None # pragma: no cover
173173

174174
ds = create_genotype_call_dataset(
175175
variant_contig_names=variant_contig_names,
877 Bytes
Binary file not shown.

sgkit/tests/io/test_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ def test_concatenate_and_rechunk__2d():
3535
np.testing.assert_array_equal(out.compute(), np.arange(30).reshape(10, 3))
3636

3737

38+
def test_concatenate_and_rechunk__tiny_file():
39+
z1 = zarr.zeros(4, chunks=3, dtype="i4")
40+
z1[:] = np.arange(4)
41+
42+
# this zarr array lies entirely within the second chunk
43+
z2 = zarr.zeros(1, chunks=3, dtype="i4")
44+
z2[:] = np.arange(4, 5)
45+
46+
z3 = zarr.zeros(5, chunks=3, dtype="i4")
47+
z3[:] = np.arange(5, 10)
48+
49+
zarrs = [z1, z2, z3]
50+
51+
out = concatenate_and_rechunk(zarrs)
52+
53+
assert out.chunks == ((3, 3, 3, 1),)
54+
np.testing.assert_array_equal(out.compute(), np.arange(10))
55+
56+
3857
def test_concatenate_and_rechunk__shape_mismatch():
3958
z1 = zarr.zeros((5, 3), chunks=(2, 3), dtype="i4")
4059
z2 = zarr.zeros((5, 4), chunks=(2, 4), dtype="i4")

sgkit/tests/test_hwe.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ def test_hwep_dataset__precomputed_counts(ds_neq: Dataset) -> None:
143143
assert np.all(p < 1e-8)
144144

145145

146+
def test_hwep_dataset__raise_on_missing_ploidy():
147+
with pytest.raises(
148+
ValueError,
149+
match="`ploidy` parameter must be set when not present as dataset dimension.",
150+
):
151+
ds = xr.Dataset({"x": (("alleles"), np.zeros((2,)))})
152+
hwep_test(ds)
153+
154+
155+
def test_hwep_dataset__raise_on_missing_alleles():
156+
with pytest.raises(
157+
ValueError,
158+
match="`alleles` parameter must be set when not present as dataset dimension.",
159+
):
160+
ds = xr.Dataset({"x": (("ploidy"), np.zeros((2,)))})
161+
hwep_test(ds)
162+
163+
146164
def test_hwep_dataset__raise_on_nondiploid():
147165
with pytest.raises(
148166
NotImplementedError, match="HWE test only implemented for diploid genotypes"

sgkit/tests/test_testing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import re
2+
3+
import pytest
14
import xarray as xr
25

36
from sgkit.testing import simulate_genotype_call_dataset
@@ -8,3 +11,10 @@ def test_simulate_genotype_call_dataset__zarr(tmp_path):
811
ds = simulate_genotype_call_dataset(n_variant=10, n_sample=10)
912
ds.to_zarr(path)
1013
xr.testing.assert_equal(ds, xr.open_zarr(path, concat_characters=False)) # type: ignore[no-untyped-call]
14+
15+
16+
def test_simulate_genotype_call_dataset__invalid_missing_pct():
17+
with pytest.raises(
18+
ValueError, match=re.escape("missing_pct must be within [0.0, 1.0]")
19+
):
20+
simulate_genotype_call_dataset(n_variant=10, n_sample=10, missing_pct=-1.0)

sgkit/tests/test_variables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def test_variables__whole_ds(dummy_ds: xr.Dataset) -> None:
7777
spec_bar = ArrayLikeSpec("bar", kind="i", ndim=1)
7878
try:
7979
SgkitVariables.register_variable(spec_foo)
80+
with pytest.raises(ValueError, match="`foo` already registered"):
81+
SgkitVariables.register_variable(spec_foo)
8082
with pytest.raises(ValueError, match="No array spec registered for bar"):
8183
variables.validate(dummy_ds)
8284
SgkitVariables.register_variable(spec_bar)

sgkit/tests/test_vcfzarr_reader.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,32 @@ def test_read_vcfzarr(shared_datadir):
7373

7474

7575
@pytest.mark.parametrize(
76-
"vcfzarr_filename, grouped_by_contig",
77-
[("sample.vcf.zarr.zip", False), ("sample-grouped.vcf.zarr.zip", True)],
76+
"vcfzarr_filename, grouped_by_contig, consolidated",
77+
[
78+
("sample.vcf.zarr.zip", False, False),
79+
("sample-grouped.vcf.zarr.zip", True, False),
80+
("sample-grouped.vcf.zarr.zip", True, True),
81+
],
7882
)
7983
@pytest.mark.parametrize(
8084
"concat_algorithm",
8185
[None, "xarray_internal"],
8286
)
8387
def test_vcfzarr_to_zarr(
84-
shared_datadir, tmp_path, vcfzarr_filename, grouped_by_contig, concat_algorithm
88+
shared_datadir,
89+
tmp_path,
90+
vcfzarr_filename,
91+
grouped_by_contig,
92+
consolidated,
93+
concat_algorithm,
8594
):
8695
# The file sample-grouped.vcf.zarr.zip was created by running the following
8796
# in a python session with the scikit-allel package installed.
8897
#
8998
# import allel
9099
# for contig in ["19", "20", "X"]:
91100
# allel.vcf_to_zarr("sample.vcf", "sample-grouped.vcf.zarr", group=contig, region=contig)
101+
# zarr.consolidate_metadata("sample-grouped.vcf.zarr")
92102
#
93103
# Then (in a shell):
94104
# (cd sample-grouped.vcf.zarr; zip -r ../sample-grouped.vcf.zarr.zip .)
@@ -100,6 +110,7 @@ def test_vcfzarr_to_zarr(
100110
output,
101111
grouped_by_contig=grouped_by_contig,
102112
concat_algorithm=concat_algorithm,
113+
consolidated=consolidated,
103114
)
104115

105116
ds = xr.open_zarr(output) # type: ignore[no-untyped-call]

sgkit/tests/test_window.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import allel
24
import dask.array as da
35
import numpy as np
@@ -16,7 +18,8 @@
1618

1719

1820
@pytest.mark.parametrize(
19-
"length, chunks, size, step", [(12, 6, 4, 4), (12, 6, 4, 2), (12, 5, 4, 4)]
21+
"length, chunks, size, step",
22+
[(12, 6, 4, 4), (12, 6, 4, 2), (12, 5, 4, 4), (12, 12, 4, 4)],
2023
)
2124
@pytest.mark.parametrize("dtype", [np.int64, np.float32, np.float64])
2225
def test_moving_statistic_1d(length, chunks, size, step, dtype):
@@ -59,6 +62,15 @@ def sum_cols(x):
5962
np.testing.assert_equal(stat, stat_sa)
6063

6164

65+
def test_moving_statistic__min_chunksize_smaller_than_size():
66+
values = da.from_array(np.arange(10), chunks=2)
67+
with pytest.raises(
68+
ValueError,
69+
match=re.escape("Minimum chunk size (2) must not be smaller than size (3)."),
70+
):
71+
moving_statistic(values, np.sum, size=3, step=3, dtype=values.dtype)
72+
73+
6274
def test_window():
6375
ds = simulate_genotype_call_dataset(n_variant=10, n_sample=3, seed=0)
6476
assert not has_windows(ds)

sgkit/variables.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _validate(
5050
Validate that xr_dataset contains array(s) of interest with alternative
5151
variable name(s). To validate all variables in the dataset, skip `specs`.
5252
"""
53-
...
53+
... # pragma: no cover
5454

5555
@classmethod
5656
@overload
@@ -59,7 +59,7 @@ def _validate(cls, xr_dataset: xr.Dataset, *specs: Spec) -> xr.Dataset:
5959
Validate that xr_dataset contains array(s) of interest with default
6060
variable name(s). To validate all variables in the dataset, skip `specs`.
6161
"""
62-
...
62+
... # pragma: no cover
6363

6464
@classmethod
6565
@overload
@@ -69,7 +69,7 @@ def _validate(cls, xr_dataset: xr.Dataset, *specs: Hashable) -> xr.Dataset:
6969
name(s). Variable must be registered in `SgkitVariables.registered_variables`.
7070
To validate all variables in the dataset, skip `specs`.
7171
"""
72-
...
72+
... # pragma: no cover
7373

7474
@classmethod
7575
def _validate(

0 commit comments

Comments
 (0)