Skip to content

Commit 4c31774

Browse files
authored
Merge pull request #1193 from khushthecoder/GH1151-fix-roh-hmm-cache-name
fix: resolve roh_hmm cache name safely (GH-1151)
2 parents 2f29890 + f3fb61f commit 4c31774

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

malariagen_data/anoph/heterozygosity.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,27 @@ def _sample_count_het(
399399
def _roh_hmm_cache_name(self):
400400
return "roh_hmm_v1"
401401

402+
def _get_roh_hmm_cache_name(self):
403+
"""Safely resolve the ROH HMM cache name.
404+
405+
Supports class attribute, property, or legacy method override.
406+
Falls back to the default "roh_hmm_v1" if resolution fails.
407+
408+
See also: https://github.com/malariagen/malariagen-data-python/issues/1151
409+
"""
410+
try:
411+
name = self._roh_hmm_cache_name
412+
# Handle legacy case where _roh_hmm_cache_name might be a
413+
# callable method rather than a property or class attribute.
414+
if callable(name):
415+
name = name()
416+
if isinstance(name, str) and len(name) > 0:
417+
return name
418+
except NotImplementedError:
419+
pass
420+
# Fallback to default.
421+
return "roh_hmm_v1"
422+
402423
@_check_types
403424
@doc(
404425
summary="Infer runs of homozygosity for a single sample over a genome region.",
@@ -420,7 +441,7 @@ def roh_hmm(
420441

421442
resolved_region: Region = _parse_single_region(self, region)
422443

423-
name = self._roh_hmm_cache_name
444+
name = self._get_roh_hmm_cache_name()
424445

425446
params = dict(
426447
sample=sample,

tests/anoph/test_fst.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,16 @@ def test_pairwise_average_fst_with_sample_query(fixture, api: AnophelesFstAnalys
346346
n_jack=random.randint(10, 200),
347347
)
348348

349-
# Run checks.
350-
check_pairwise_average_fst(api=api, fst_params=fst_params)
349+
# Run checks - skip if random parameter selection results in insufficient cohorts.
350+
try:
351+
check_pairwise_average_fst(api=api, fst_params=fst_params)
352+
except ValueError as e:
353+
if "No cohorts remain" in str(e):
354+
pytest.skip(
355+
f"Skipping: random parameter selection produced insufficient "
356+
f"cohorts for taxon={taxon!r}: {e}"
357+
)
358+
raise
351359

352360

353361
@parametrize_with_cases("fixture,api", cases=".")

tests/anoph/test_heterozygosity.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ def test_roh_hmm(fixture, api: AnophelesHetAnalysis):
185185
assert col in df_roh.columns
186186

187187

188+
@parametrize_with_cases("fixture,api", cases=".")
189+
def test_roh_hmm_cache_name_resolution(fixture, api: AnophelesHetAnalysis):
190+
"""Regression test for GH#1151: _roh_hmm_cache_name must resolve to a string.
191+
192+
Verifies that the cache name resolver handles class attributes, properties,
193+
and legacy method overrides without raising NotImplementedError.
194+
"""
195+
# The resolver should always return a non-empty string.
196+
name = api._get_roh_hmm_cache_name()
197+
assert isinstance(name, str), f"Expected str, got {type(name)}"
198+
assert len(name) > 0, "Cache name must be non-empty"
199+
200+
188201
@parametrize_with_cases("fixture,api", cases=".")
189202
def test_plot_roh(fixture, api: AnophelesHetAnalysis):
190203
# Set up test parameters.

0 commit comments

Comments
 (0)