diff --git a/src/lingrex/regularity.py b/src/lingrex/regularity.py index 7bc01bd..8b487a2 100644 --- a/src/lingrex/regularity.py +++ b/src/lingrex/regularity.py @@ -6,6 +6,7 @@ from lingpy import log + def regularity(wordlist, threshold=3, ref="cogid", min_refs=3, word_threshold=0.75, sound_classes="cv"): """ @@ -92,6 +93,20 @@ def regularity(wordlist, threshold=3, ref="cogid", min_refs=3, else: irregular_words += len(set(msa["taxa"])) + if patterns == 0: + raise ValueError( + "Cannot compute regularity: no patterns were detected in the data. " + "Check sound_classes or input data." + ) + if full_proportion == 0: + raise ValueError( + "Cannot compute regularity: no eligible alignment sites were found." + ) + if (regular_words + irregular_words) == 0: + raise ValueError( + "Cannot compute regularity: no words satisfy the min_refs threshold." + ) + return ( regular_patterns, patterns - regular_patterns, diff --git a/tests/test_regularity.py b/tests/test_regularity.py index b27375e..786be50 100644 --- a/tests/test_regularity.py +++ b/tests/test_regularity.py @@ -33,3 +33,17 @@ def test_regularity(): sound_classes="cv") assert output == (2, 5, 7, 0.29, 4, 5, 9, 0.44, 3, 4, 7, 0.43) + with raises( + ValueError, + match=r"Cannot compute regularity: no words satisfy the min_refs threshold\.", + ): + regularity( + test_alg, threshold=2, word_threshold=0.5, sound_classes="cv", min_refs=5 + ) + with raises( + ValueError, + match=r"Cannot compute regularity: no patterns were detected in the data\.", + ): + regularity( + test_alg, threshold=2, word_threshold=0.5, sound_classes="T" + )