|
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +from malariagen_data.anopheles import AnophelesDataResource |
| 7 | +from malariagen_data.util import CacheMiss |
| 8 | + |
| 9 | + |
| 10 | +class DummyAnophelesDataResource(AnophelesDataResource): |
| 11 | + @property |
| 12 | + def _xpehh_gwss_cache_name(self): |
| 13 | + return "dummy_xpehh_gwss_cache" |
| 14 | + |
| 15 | + @property |
| 16 | + def _ihs_gwss_cache_name(self): |
| 17 | + return "dummy_ihs_gwss_cache" |
| 18 | + |
| 19 | + @property |
| 20 | + def _roh_hmm_cache_name(self): |
| 21 | + return "dummy_roh_hmm_cache" |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + self._log = SimpleNamespace(debug=lambda *args, **kwargs: None) |
| 25 | + self._cache = {} |
| 26 | + self.block_jackknife_calls = 0 |
| 27 | + |
| 28 | + def sample_metadata(self, sample_sets=None, sample_query=None): # noqa: ARG002 |
| 29 | + data = pd.DataFrame( |
| 30 | + { |
| 31 | + "sample_id": ["s1", "s2", "s3"], |
| 32 | + "cohort_admin1_year": ["cohort_a", "cohort_a", "cohort_b"], |
| 33 | + "taxon": ["gambiae", "gambiae", "coluzzii"], |
| 34 | + "year": [2020, 2020, 2021], |
| 35 | + "month": [1, 2, 3], |
| 36 | + "country": ["Ghana", "Ghana", "Benin"], |
| 37 | + "admin1_iso": ["GH-AA", "GH-AA", "BJ-AK"], |
| 38 | + "admin1_name": ["Accra", "Accra", "Atlantique"], |
| 39 | + "admin2_name": ["Accra", "Accra", "Abomey-Calavi"], |
| 40 | + "longitude": [-0.2, -0.4, 2.3], |
| 41 | + "latitude": [5.6, 5.8, 6.4], |
| 42 | + } |
| 43 | + ) |
| 44 | + if sample_query is not None: |
| 45 | + return data.query(sample_query).reset_index(drop=True) |
| 46 | + return data |
| 47 | + |
| 48 | + def snp_allele_counts(self, **kwargs): # noqa: ARG002 |
| 49 | + return np.array([[2, 0], [1, 1], [0, 2]]) |
| 50 | + |
| 51 | + def _block_jackknife_cohort_diversity_stats( |
| 52 | + self, *, cohort_label, ac, n_jack, confidence_level # noqa: ARG002 |
| 53 | + ): |
| 54 | + self.block_jackknife_calls += 1 |
| 55 | + return { |
| 56 | + "cohort": cohort_label, |
| 57 | + "theta_pi": 0.123, |
| 58 | + "theta_pi_estimate": 0.124, |
| 59 | + "theta_pi_bias": 0.001, |
| 60 | + "theta_pi_std_err": 0.01, |
| 61 | + "theta_pi_ci_err": 0.02, |
| 62 | + "theta_pi_ci_low": 0.1, |
| 63 | + "theta_pi_ci_upp": 0.14, |
| 64 | + "theta_w": 0.111, |
| 65 | + "theta_w_estimate": 0.112, |
| 66 | + "theta_w_bias": 0.001, |
| 67 | + "theta_w_std_err": 0.01, |
| 68 | + "theta_w_ci_err": 0.02, |
| 69 | + "theta_w_ci_low": 0.09, |
| 70 | + "theta_w_ci_upp": 0.13, |
| 71 | + "tajima_d": 0.3, |
| 72 | + "tajima_d_estimate": 0.31, |
| 73 | + "tajima_d_bias": 0.01, |
| 74 | + "tajima_d_std_err": 0.05, |
| 75 | + "tajima_d_ci_err": 0.1, |
| 76 | + "tajima_d_ci_low": 0.2, |
| 77 | + "tajima_d_ci_upp": 0.4, |
| 78 | + } |
| 79 | + |
| 80 | + def results_cache_get(self, *, name, params): |
| 81 | + key = (name, repr(params)) |
| 82 | + if key not in self._cache: |
| 83 | + raise CacheMiss |
| 84 | + return self._cache[key] |
| 85 | + |
| 86 | + def results_cache_set(self, *, name, params, results): |
| 87 | + key = (name, repr(params)) |
| 88 | + self._cache[key] = results |
| 89 | + |
| 90 | + |
| 91 | +def test_cohort_diversity_stats_uses_cache(): |
| 92 | + api = DummyAnophelesDataResource() |
| 93 | + |
| 94 | + stats1 = api.cohort_diversity_stats( |
| 95 | + cohort="cohort_a", |
| 96 | + cohort_size=2, |
| 97 | + region="2L", |
| 98 | + n_jack=10, |
| 99 | + confidence_level=0.95, |
| 100 | + ) |
| 101 | + stats2 = api.cohort_diversity_stats( |
| 102 | + cohort="cohort_a", |
| 103 | + cohort_size=2, |
| 104 | + region="2L", |
| 105 | + n_jack=10, |
| 106 | + confidence_level=0.95, |
| 107 | + ) |
| 108 | + |
| 109 | + assert api.block_jackknife_calls == 1 |
| 110 | + pd.testing.assert_series_equal(stats1.sort_index(), stats2.sort_index()) |
| 111 | + |
0 commit comments