Skip to content

Commit de3c7bf

Browse files
Merge branch 'master' into optimize/cohort-heterozygosity-vectorized
2 parents e16dd22 + 49fcc68 commit de3c7bf

2 files changed

Lines changed: 86 additions & 11 deletions

File tree

malariagen_data/util.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ def __eq__(self, other):
570570
and (self.end == other.end)
571571
)
572572

573+
def __repr__(self):
574+
return f"Region({self._contig!r}, {self._start!r}, {self._end!r})"
575+
573576
def __str__(self):
574577
out = self._contig
575578
if self._start is not None or self._end is not None:
@@ -927,7 +930,20 @@ def _jitter(a, fraction, random_state=np.random):
927930

928931

929932
class CacheMiss(Exception):
930-
pass
933+
"""Raised when a requested item is not present in the cache."""
934+
935+
def __init__(self, key=None):
936+
self.key = key
937+
if key is not None:
938+
message = f"Cache miss for key: {key!r}"
939+
else:
940+
message = "Cache miss: requested item not found in cache."
941+
super().__init__(message)
942+
943+
def __repr__(self):
944+
if self.key is not None:
945+
return f"CacheMiss({self.key!r})"
946+
return "CacheMiss()"
931947

932948

933949
class LoggingHelper:
@@ -1531,12 +1547,10 @@ def _apply_allele_mapping(x, mapping, max_allele):
15311547

15321548
def _dask_apply_allele_mapping(v, mapping, max_allele):
15331549
if not isinstance(v, da.Array):
1534-
raise TypeError(
1535-
f"Expected v to be a dask.array.Array, " f"got {type(v).__name__}"
1536-
)
1550+
raise TypeError(f"Expected v to be a dask.array.Array, got {type(v).__name__}")
15371551
if not isinstance(mapping, np.ndarray):
15381552
raise TypeError(
1539-
f"Expected mapping to be a numpy.ndarray, " f"got {type(mapping).__name__}"
1553+
f"Expected mapping to be a numpy.ndarray, got {type(mapping).__name__}"
15401554
)
15411555
assert v.ndim == 2
15421556
assert mapping.ndim == 2
@@ -1558,12 +1572,10 @@ def _genotype_array_map_alleles(gt, mapping):
15581572
# N.B., scikit-allel does not handle empty blocks well, so we
15591573
# include some extra logic to handle that better.
15601574
if not isinstance(gt, np.ndarray):
1561-
raise TypeError(
1562-
f"Expected gt to be a numpy.ndarray, " f"got {type(gt).__name__}"
1563-
)
1575+
raise TypeError(f"Expected gt to be a numpy.ndarray, got {type(gt).__name__}")
15641576
if not isinstance(mapping, np.ndarray):
15651577
raise TypeError(
1566-
f"Expected mapping to be a numpy.ndarray, " f"got {type(mapping).__name__}"
1578+
f"Expected mapping to be a numpy.ndarray, got {type(mapping).__name__}"
15671579
)
15681580
assert gt.ndim == 3
15691581
assert mapping.ndim == 3
@@ -1585,11 +1597,11 @@ def _genotype_array_map_alleles(gt, mapping):
15851597
def _dask_genotype_array_map_alleles(gt, mapping):
15861598
if not isinstance(gt, da.Array):
15871599
raise TypeError(
1588-
f"Expected gt to be a dask.array.Array, " f"got {type(gt).__name__}"
1600+
f"Expected gt to be a dask.array.Array, got {type(gt).__name__}"
15891601
)
15901602
if not isinstance(mapping, np.ndarray):
15911603
raise TypeError(
1592-
f"Expected mapping to be a numpy.ndarray, " f"got {type(mapping).__name__}"
1604+
f"Expected mapping to be a numpy.ndarray, got {type(mapping).__name__}"
15931605
)
15941606
assert gt.ndim == 3
15951607
assert mapping.ndim == 2

tests/test_util.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Tests for Region.__repr__ and CacheMiss.__repr__ / default message."""
2+
3+
import pytest
4+
5+
from malariagen_data.util import CacheMiss, Region
6+
7+
8+
# ---------------------------------------------------------------------------
9+
# Region
10+
# ---------------------------------------------------------------------------
11+
12+
13+
def test_region_repr_contig_only():
14+
r = Region("2L")
15+
assert repr(r) == "Region('2L', None, None)"
16+
assert str(r) == "2L"
17+
18+
19+
def test_region_repr_with_coords():
20+
r = Region("2L", 100_000, 200_000)
21+
assert repr(r) == "Region('2L', 100000, 200000)"
22+
assert str(r) == "2L:100,000-200,000"
23+
24+
25+
def test_region_repr_in_list():
26+
regions = [Region("2L", 10, 20), Region("3R", 30, 40)]
27+
assert repr(regions) == "[Region('2L', 10, 20), Region('3R', 30, 40)]"
28+
29+
30+
def test_region_repr_start_only():
31+
r = Region("X", start=500, end=None)
32+
assert repr(r) == "Region('X', 500, None)"
33+
assert str(r) == "X:500-"
34+
35+
36+
# ---------------------------------------------------------------------------
37+
# CacheMiss
38+
# ---------------------------------------------------------------------------
39+
40+
41+
def test_cache_miss_no_key():
42+
cm = CacheMiss()
43+
assert repr(cm) == "CacheMiss()"
44+
assert "Cache miss" in str(cm)
45+
46+
47+
def test_cache_miss_string_key():
48+
cm = CacheMiss("my_key")
49+
assert repr(cm) == "CacheMiss('my_key')"
50+
assert "my_key" in str(cm)
51+
52+
53+
def test_cache_miss_tuple_key():
54+
cm = CacheMiss(("contig", 100))
55+
assert repr(cm) == "CacheMiss(('contig', 100))"
56+
assert "('contig', 100)" in str(cm)
57+
58+
59+
def test_cache_miss_is_exception():
60+
with pytest.raises(CacheMiss) as exc_info:
61+
raise CacheMiss("lookup_key")
62+
assert "lookup_key" in str(exc_info.value)
63+
assert repr(exc_info.value) == "CacheMiss('lookup_key')"

0 commit comments

Comments
 (0)