GitHub PullRequestReviewCommentEvent at 2014-04-22 16:46:13
Repository: scikit-bio
Actor: wasade
Payload: {"comment":{"url":"https://api.github.com/repos/biocore/scikit-bio/pulls/comments/11862936","id":11862936,"diff_hunk":"@@ -0,0 +1,212 @@\n+#!/usr/bin/env python\n+from __future__ import division\n+\n+# ----------------------------------------------------------------------------\n+# Copyright (c) 2013--, scikit-bio development team.\n+#\n+# Distributed under the terms of the Modified BSD License.\n+#\n+# The full license is in the file COPYING.txt, distributed with this software.\n+# ----------------------------------------------------------------------------\n+\n+import numpy as np\n+\n+from .base import _indices_to_counts, _validate, osd\n+\n+\n+def chao1(counts, bias_corrected=True):\n+    \"\"\"Calculate chao1 richness estimator.\n+\n+    Uses the bias-corrected version unless `bias_corrected` is ``False`` *and*\n+    there are both singletons and doubletons.\n+\n+    Parameters\n+    ----------\n+    counts : (N,) array_like, int\n+        Vector of counts.\n+    bias_corrected : bool, optional\n+        Indicates whether or not to use the bias-corrected version of the\n+        equation. If ``False`` *and* there are both singletons and doubletons,\n+        the uncorrected version will be used. The biased-corrected version will\n+        be used otherwise.\n+\n+    Returns\n+    -------\n+    double\n+        Computed chao1 richness estimator.\n+\n+    See Also\n+    --------\n+    chao1_confidence\n+\n+    Notes\n+    -----\n+    The implementation here is based on the equations in the EstimateS manual\n+    [1]_.\n+\n+    The uncorrected version is based on Equation 1 in the EstimateS manual (\n+    defined in [2]_):\n+\n+    ``chao1 = S_obs + N_1^2/(2*N_2)`` where ``N_1`` and ``N_2`` are the count\n+    of singletons and doubletons respectively.\n+\n+    The bias-corrected version is based on Equation 2 in the EstimateS manual (\n+    defined in [3]_):\n+\n+    ``chao1 = S_obs + N_1(N_1-1)/(2*(N_2+1))`` where ``N_1`` and ``N_2`` are\n+    the count of singletons and doubletons respectively.\n+\n+    References\n+    ----------\n+    .. [1] http://viceroy.eeb.uconn.edu/estimates/\n+    .. [2] Chao, A. 1984. Non-parametric estimation of the number of classes in\n+       a population. Scandinavian Journal of Statistics 11, 265-270.\n+    .. [3] Chao, A. 1987. Estimating the population size for capture-recapture\n+       data with unequal catchability. Biometrics 43, 783-791.\n+\n+    \"\"\"\n+    counts = _validate(counts)\n+    o, s, d = osd(counts)\n+\n+    if not bias_corrected and s and d:\n+        return o + s ** 2 / (d * 2)\n+    else:\n+        return o + s * (s - 1) / (2 * (d + 1))\n+\n+\n+def chao1_confidence(counts, bias_corrected=True, zscore=1.96):\n+    \"\"\"Calculate chao1 confidence interval.\n+\n+    Parameters\n+    ----------\n+    counts : (N,) array_like, int\n+        Vector of counts.\n+    bias_corrected : bool, optional\n+        Indicates whether or not to use the bias-corrected version of the\n+        equation. If ``False`` *and* there are both singletons and doubletons,\n+        the uncorrected version will be used. The biased-corrected version will\n+        be used otherwise.\n+    zscore : scalar, optional\n+        Score to use for confidence. Default of 1.96 is for a 95% confidence\n+        interval.\n+\n+    Returns\n+    -------\n+    tuple\n+        chao1 confidence interval as ``(lower_bound, upper_bound)``.\n+\n+    See Also\n+    --------\n+    chao1\n+\n+    Notes\n+    -----\n+    The implementation here is based on the equations in the EstimateS manual\n+    [1]_. Different equations are employed to calculate the chao1 variance and\n+    confidence interval depending on `bias_corrected` and the presence/absence\n+    of singletons and/or doubletons.\n+\n+    Specifically, the following EstimateS equations are used:\n+\n+    1. No singletons, Equation 14.\n+    2. Singletons but no doubletons, Equations 7, 13.\n+    3. Singletons and doubletons, ``bias_corrected=True``, Equations 6, 13.\n+    4. Singletons and doubletons, ``bias_corrected=False``, Equations 5, 13.\n+\n+    References\n+    ----------\n+    .. [1] http://viceroy.eeb.uconn.edu/estimates/\n+\n+    \"\"\"\n+    counts = _validate(counts)\n+    o, s, d = osd(counts)\n+    if s:\n+        chao = chao1(counts, bias_corrected)\n+        chaovar = _chao1_var(counts, bias_corrected)\n+        return _chao_confidence_with_singletons(chao, o, chaovar, zscore)\n+    else:\n+        n = counts.sum()\n+        return _chao_confidence_no_singletons(n, o, zscore)\n+\n+\n+def _chao1_var(counts, bias_corrected=True):\n+    \"\"\"Calculates chao1 variance using decision rules in EstimateS.\"\"\"\n+    o, s, d = osd(counts)\n+    if not d:\n+        c = chao1(counts, bias_corrected)\n+        return _chao1_var_no_doubletons(s, c)\n+    if not s:\n+        n = counts.sum()\n+        return _chao1_var_no_singletons(n, o)\n+    if bias_corrected:\n+        return _chao1_var_bias_corrected(s, d)\n+    else:\n+        return _chao1_var_uncorrected(s, d)\n+\n+\n+def _chao1_var_uncorrected(singles, doubles):\n+    \"\"\"Calculates chao1, uncorrected.\n+\n+    From EstimateS manual, equation 5.\n+    \"\"\"\n+    r = float(singles) / doubles\n+    return doubles * (.5 * r ** 2 + r ** 3 + .24 * r ** 4)\n+\n+\n+def _chao1_var_bias_corrected(singles, doubles):\n+    \"\"\"Calculates chao1 variance, bias-corrected.\n+\n+    From EstimateS manual, equation 6.\n+    \"\"\"\n+    s, d = float(singles), float(doubles)","path":"skbio/maths/diversity/alpha/chao1.py","position":161,"original_position":161,"commit_id":"ddcab2a86b28ca4e006784324f91536fc6db6311","original_commit_id":"ddcab2a86b28ca4e006784324f91536fc6db6311","user":{"login":"wasade","id":474290,"avatar_url":"https://avatars.githubusercontent.com/u/474290?","gravatar_id":"e45f3417df9a660f87d6e11f21d7f02a","url":"https://api.github.com/users/wasade","html_url":"https://github.com/wasade","followers_url":"https://api.github.com/users/wasade/followers","following_url":"https://api.github.com/users/wasade/following{/other_user}","gists_url":"https://api.github.com/users/wasade/gists{/gist_id}","starred_url":"https://api.github.com/users/wasade/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wasade/subscriptions","organizations_url":"https://api.github.com/users/wasade/orgs","repos_url":"https://api.github.com/users/wasade/repos","events_url":"https://api.github.com/users/wasade/events{/privacy}","received_events_url":"https://api.github.com/users/wasade/received_events","type":"User","site_admin":false},"body":"is this pep8? also, since the return from this method is subsequently used with numpy operations, casting these using a numpy type here would save an implicit cast later","created_at":"2014-04-22T16:46:13Z","updated_at":"2014-04-22T16:46:13Z","html_url":"https://github.com/biocore/scikit-bio/pull/283#discussion_r11862936","pull_request_url":"https://api.github.com/repos/biocore/scikit-bio/pulls/283","_links":{"self":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/comments/11862936"},"html":{"href":"https://github.com/biocore/scikit-bio/pull/283#discussion_r11862936"},"pull_request":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/283"}}}}