From 52bb1316a27de42cd955a800fc100cfea58569e2 Mon Sep 17 00:00:00 2001 From: Andreas Mayer Date: Tue, 21 May 2024 14:15:24 +0100 Subject: [PATCH] make chao estimators robust to singletons only --- pyrepseq/stats.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyrepseq/stats.py b/pyrepseq/stats.py index 7bf1867..2f36c65 100644 --- a/pyrepseq/stats.py +++ b/pyrepseq/stats.py @@ -161,7 +161,7 @@ def convert_to_array(array: Union[Iterable, DataFrame]) -> ndarray: def chao1(counts): """Estimate richness from sampled counts.""" - if counts[1] == 0: + if (len(counts)==1) or (counts[1] == 0): return np.sum(counts) + (counts[0]* (counts[0] - 1)) / 2 return np.sum(counts) + counts[0] ** 2 / (2 * counts[1]) @@ -170,6 +170,8 @@ def var_chao1(counts): """Variance estimator for Chao1 richness.""" f1 = counts[0] + if (len(counts)==1) or (counts[1] == 0): + return np.inf f2 = counts[1] ratio = f1 / f2 return f2 * ((ratio / 4) ** 4 + ratio**3 + (ratio / 2) ** 2)