Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lingrex/regularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
"""
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_regularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)