{"action":"created","comment":{"url":"https://api.github.com/repos/biocore/scikit-bio/pulls/comments/27506591","id":27506591,"diff_hunk":"@@ -0,0 +1,384 @@\n+r\"\"\"\n+Composition Statistics (:mod:`skbio.stats.composition`)\n+=======================================================\n+\n+.. currentmodule:: skbio.stats.composition\n+\n+This module provides functions for compositional data analysis.\n+\n+Many 'omics datasets are inherently compositional - meaning that they\n+are best interpreted as proportions or percentages rather than\n+absolute counts.\n+\n+Formally, :math:`x` is a composition if :math:`\\sum_{i=0}^D x_{i} = c`\n+and :math:`x_{i} > 0`, :math:`1 \\leq i \\leq D` and :math:`c` is a real\n+valued constant and there are :math:`D` components for each\n+composition. In this module :math:`c=1`. Compositional data can be\n+analyzed using Aitchison geometry. [1]_\n+\n+However, in this framework, standard real Euclidean operations such as\n+addition and multiplication no longer apply. Only operations such as\n+perturbation and power can be used to manipulate this data. [1]_\n+\n+This module allows two styles of manipulation of compositional data.\n+Compositional data can be analyzed using perturbation and power\n+operations, which can be useful for simulation studies. The\n+alternative strategy is to transform compositional data into the real\n+space.  Right now, the centre log ratio transform (clr) [1]_ can be\n+used to accomplish this.  This transform can be useful for performing\n+standard statistical tools such as parametric hypothesis testing,\n+regressions and more.\n+\n+The major caveat of using this framework is dealing with zeros.  In\n+the Aitchison geometry, only compositions with nonzero components can\n+be considered. The multiplicative replacement technique [2]_ can be\n+used to substitute these zeros with small pseudocounts without\n+introducing major distortions to the data.\n+\n+Functions\n+---------\n+\n+.. autosummary::\n+   :toctree: generated/\n+\n+   closure\n+   multiplicative_replacement\n+   perturb\n+   perturb_inv\n+   power\n+   clr\n+   centralize\n+\n+Examples\n+--------\n+\n+>>> import numpy as np\n+\n+Consider a very simple environment with only 3 species. The species\n+in the environment are equally distributed and their proportions are\n+equivalent:\n+\n+>>> otus = np.array([1./3, 1./3., 1./3])\n+\n+Suppose that an antibiotic kills off half of the population for the\n+first two species, but doesn't harm the third species. Then the\n+perturbation vector would be as follows\n+\n+>>> antibiotic = np.array([1./2, 1./2, 1])\n+\n+And the resulting perturbation would be\n+\n+>>> perturb(otus, antibiotic)\n+array([ 0.25,  0.25,  0.5 ])\n+\n+Reference\n+---------\n+.. [1] V. Pawlowsky-Glahn. \"Lecture Notes on Compositional Data Analysis\"\n+.. [2] J. A. Martin-Fernandez. \"Dealing With Zeros and Missing Values in\n+       Compositional Data Sets Using Nonparametric Imputation\"\n+\n+\"\"\"\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+from __future__ import absolute_import, division, print_function\n+import numpy as np\n+import scipy.stats as ss\n+\n+\n+def closure(mat):\n+    \"\"\"\n+    Performs closure to ensure that all elements add up to 1.\n+\n+    Parameters\n+    ----------\n+    mat : array_like\n+       a matrix of proportions where\n+       rows = compositions\n+       columns = components\n+\n+    Returns\n+    -------\n+    array_like, np.float64\n+       A matrix of proportions where all of the values\n+       are nonzero and each composition (row) adds up to 1\n+\n+    \"\"\"\n+    mat = np.atleast_2d(mat)\n+    if np.any(mat < 0):\n+        raise ValueError(\"Cannot have negative proportions\")\n+    if mat.ndim > 2:\n+        raise ValueError(\"Input matrix can only have two dimensions or less\")\n+    mat = mat / mat.sum(axis=1, keepdims=True)\n+    return mat.squeeze()\n+\n+\n+def multiplicative_replacement(mat, delta=None):\n+    \"\"\"Replace all zeros with small non-zero values.\n+\n+    It uses the multiplicative replacement strategy, replacing zeros\n+    with small positive values and performing a closure operation so\n+    that the compositions still add up to 1\n+\n+    Parameters\n+    ----------\n+    mat: array_like\n+       a matrix of proportions where\n+       rows = compositions and\n+       columns = components\n+\n+    Returns\n+    -------\n+    numpy.ndarray, np.float64\n+       A matrix of proportions where all of the values\n+       are nonzero and each composition (row) adds up to 1\n+\n+    Examples\n+    --------\n+    >>> import numpy as np\n+    >>> from skbio.stats.composition import multiplicative_replacement\n+    >>> X = np.array([[.2,.4,.4, 0],[0,.5,.5,0]])\n+    >>> multiplicative_replacement(X)\n+    array([[ 0.1875,  0.375 ,  0.375 ,  0.0625],\n+           [ 0.0625,  0.4375,  0.4375,  0.0625]])\n+\n+    \"\"\"\n+    mat = np.atleast_2d(mat)\n+    if mat.ndim > 2:\n+        raise ValueError(\"Input matrix can only have two dimensions or less\")\n+    if np.any(mat < 0):\n+        raise ValueError(\"Cannot have negative proportions\")\n+    if not np.all(np.isclose(mat.sum(axis=1), 1)):\n+        raise ValueError(\"Rows need to sum up to 1\")\n+\n+    z_mat = (mat == 0)\n+\n+    num_samps, num_feats = mat.shape\n+    tot = z_mat.sum(axis=1, keepdims=True)\n+\n+    if delta is None:\n+        delta = (1. / num_feats)**2\n+\n+    zcnts = 1 - tot * delta\n+    mat = np.where(z_mat, delta, zcnts * mat)\n+    return mat.squeeze()\n+\n+\n+def perturb(x, y):\n+    r\"\"\"\n+    Performs the perturbation operation.\n+\n+    This operation is defined as\n+    :math:`x \\oplus y = C[x_1 y_1, ..., x_D y_D]`\n+\n+    :math:`C[x]` is the closure operation defined as\n+    :math:`C[x] = [\\frac{x_1}{\\sum x},...,\\frac{x_D}{\\sum x}]`\n+    for some :math:`D` dimensional real vector :math:`x` and\n+    :math:`D` is the number of components for every composition.\n+\n+    Parameters\n+    ----------\n+    x : array_like, float\n+        a matrix of proportions where\n+        rows = compositions and\n+        columns = components\n+    y : array_like, float\n+        a matrix of proportions where\n+        rows = compositions and\n+        columns = components\n+\n+    Returns\n+    -------\n+    numpy.ndarray, np.float64\n+       A matrix of proportions where all of the values\n+       are nonzero and each composition (row) adds up to 1\n+\n+    Notes\n+    -----\n+    - All of the values in x and y must be non negative\n+\n+    Examples\n+    --------\n+    >>> import numpy as np\n+    >>> from skbio.stats.composition import perturb\n+    >>> x = np.array([.1,.3,.4, .2])\n+    >>> y = np.array([1./6,1./6,1./3,1./3])\n+    >>> perturb(x,y)\n+    array([ 0.0625,  0.1875,  0.5   ,  0.25  ])\n+\n+    \"\"\"\n+    x, y = closure(x), closure(y)\n+    return closure(x * y)\n+\n+\n+def perturb_inv(x, y):","path":"skbio/stats/composition.py","position":217,"original_position":220,"commit_id":"c4c057045e5e70d0b34b4a41d23fc5e1097829f4","original_commit_id":"8b73995b6ef98d4c12fe1a41028cdf6ee46c9d05","user":{"login":"jairideout","id":1847232,"avatar_url":"https://avatars.githubusercontent.com/u/1847232?v=3","gravatar_id":"","url":"https://api.github.com/users/jairideout","html_url":"https://github.com/jairideout","followers_url":"https://api.github.com/users/jairideout/followers","following_url":"https://api.github.com/users/jairideout/following{/other_user}","gists_url":"https://api.github.com/users/jairideout/gists{/gist_id}","starred_url":"https://api.github.com/users/jairideout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jairideout/subscriptions","organizations_url":"https://api.github.com/users/jairideout/orgs","repos_url":"https://api.github.com/users/jairideout/repos","events_url":"https://api.github.com/users/jairideout/events{/privacy}","received_events_url":"https://api.github.com/users/jairideout/received_events","type":"User","site_admin":false},"body":"Sounds good, that makes sense. Thanks for explaining!","created_at":"2015-03-31T18:14:19Z","updated_at":"2015-03-31T18:14:19Z","html_url":"https://github.com/biocore/scikit-bio/pull/860#discussion_r27506591","pull_request_url":"https://api.github.com/repos/biocore/scikit-bio/pulls/860","_links":{"self":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/comments/27506591"},"html":{"href":"https://github.com/biocore/scikit-bio/pull/860#discussion_r27506591"},"pull_request":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/860"}}},"pull_request":{"url":"https://api.github.com/repos/biocore/scikit-bio/pulls/860","id":29784050,"html_url":"https://github.com/biocore/scikit-bio/pull/860","diff_url":"https://github.com/biocore/scikit-bio/pull/860.diff","patch_url":"https://github.com/biocore/scikit-bio/pull/860.patch","issue_url":"https://api.github.com/repos/biocore/scikit-bio/issues/860","number":860,"state":"open","locked":false,"title":"Composition Analysis methods","user":{"login":"mortonjt","id":4184797,"avatar_url":"https://avatars.githubusercontent.com/u/4184797?v=3","gravatar_id":"","url":"https://api.github.com/users/mortonjt","html_url":"https://github.com/mortonjt","followers_url":"https://api.github.com/users/mortonjt/followers","following_url":"https://api.github.com/users/mortonjt/following{/other_user}","gists_url":"https://api.github.com/users/mortonjt/gists{/gist_id}","starred_url":"https://api.github.com/users/mortonjt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mortonjt/subscriptions","organizations_url":"https://api.github.com/users/mortonjt/orgs","repos_url":"https://api.github.com/users/mortonjt/repos","events_url":"https://api.github.com/users/mortonjt/events{/privacy}","received_events_url":"https://api.github.com/users/mortonjt/received_events","type":"User","site_admin":false},"body":"This is continuing the discussion from https://github.com/biocore/scikit-bio/issues/835\r\n\r\nThis methods are the barebones required to construct a compositional biplot\r\n\r\nThe math is a bit funky, so don't hesitate to ask questions - I know that it took me a while to understand it.\r\n\r\n@Jorge-C @ElDeveloper @antgonza are you still willing to look over this PR?","created_at":"2015-02-23T03:16:02Z","updated_at":"2015-03-31T18:14:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"789a685510c8c468e176254eb0a4b26f8a0eab65","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/biocore/scikit-bio/pulls/860/commits","review_comments_url":"https://api.github.com/repos/biocore/scikit-bio/pulls/860/comments","review_comment_url":"https://api.github.com/repos/biocore/scikit-bio/pulls/comments{/number}","comments_url":"https://api.github.com/repos/biocore/scikit-bio/issues/860/comments","statuses_url":"https://api.github.com/repos/biocore/scikit-bio/statuses/c4c057045e5e70d0b34b4a41d23fc5e1097829f4","head":{"label":"mortonjt:master","ref":"master","sha":"c4c057045e5e70d0b34b4a41d23fc5e1097829f4","user":{"login":"mortonjt","id":4184797,"avatar_url":"https://avatars.githubusercontent.com/u/4184797?v=3","gravatar_id":"","url":"https://api.github.com/users/mortonjt","html_url":"https://github.com/mortonjt","followers_url":"https://api.github.com/users/mortonjt/followers","following_url":"https://api.github.com/users/mortonjt/following{/other_user}","gists_url":"https://api.github.com/users/mortonjt/gists{/gist_id}","starred_url":"https://api.github.com/users/mortonjt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mortonjt/subscriptions","organizations_url":"https://api.github.com/users/mortonjt/orgs","repos_url":"https://api.github.com/users/mortonjt/repos","events_url":"https://api.github.com/users/mortonjt/events{/privacy}","received_events_url":"https://api.github.com/users/mortonjt/received_events","type":"User","site_admin":false},"repo":{"id":23898042,"name":"scikit-bio","full_name":"mortonjt/scikit-bio","owner":{"login":"mortonjt","id":4184797,"avatar_url":"https://avatars.githubusercontent.com/u/4184797?v=3","gravatar_id":"","url":"https://api.github.com/users/mortonjt","html_url":"https://github.com/mortonjt","followers_url":"https://api.github.com/users/mortonjt/followers","following_url":"https://api.github.com/users/mortonjt/following{/other_user}","gists_url":"https://api.github.com/users/mortonjt/gists{/gist_id}","starred_url":"https://api.github.com/users/mortonjt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mortonjt/subscriptions","organizations_url":"https://api.github.com/users/mortonjt/orgs","repos_url":"https://api.github.com/users/mortonjt/repos","events_url":"https://api.github.com/users/mortonjt/events{/privacy}","received_events_url":"https://api.github.com/users/mortonjt/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mortonjt/scikit-bio","description":"Core objects, functions and statistics for working with biological data in Python.","fork":true,"url":"https://api.github.com/repos/mortonjt/scikit-bio","forks_url":"https://api.github.com/repos/mortonjt/scikit-bio/forks","keys_url":"https://api.github.com/repos/mortonjt/scikit-bio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mortonjt/scikit-bio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mortonjt/scikit-bio/teams","hooks_url":"https://api.github.com/repos/mortonjt/scikit-bio/hooks","issue_events_url":"https://api.github.com/repos/mortonjt/scikit-bio/issues/events{/number}","events_url":"https://api.github.com/repos/mortonjt/scikit-bio/events","assignees_url":"https://api.github.com/repos/mortonjt/scikit-bio/assignees{/user}","branches_url":"https://api.github.com/repos/mortonjt/scikit-bio/branches{/branch}","tags_url":"https://api.github.com/repos/mortonjt/scikit-bio/tags","blobs_url":"https://api.github.com/repos/mortonjt/scikit-bio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mortonjt/scikit-bio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mortonjt/scikit-bio/git/refs{/sha}","trees_url":"https://api.github.com/repos/mortonjt/scikit-bio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mortonjt/scikit-bio/statuses/{sha}","languages_url":"https://api.github.com/repos/mortonjt/scikit-bio/languages","stargazers_url":"https://api.github.com/repos/mortonjt/scikit-bio/stargazers","contributors_url":"https://api.github.com/repos/mortonjt/scikit-bio/contributors","subscribers_url":"https://api.github.com/repos/mortonjt/scikit-bio/subscribers","subscription_url":"https://api.github.com/repos/mortonjt/scikit-bio/subscription","commits_url":"https://api.github.com/repos/mortonjt/scikit-bio/commits{/sha}","git_commits_url":"https://api.github.com/repos/mortonjt/scikit-bio/git/commits{/sha}","comments_url":"https://api.github.com/repos/mortonjt/scikit-bio/comments{/number}","issue_comment_url":"https://api.github.com/repos/mortonjt/scikit-bio/issues/comments{/number}","contents_url":"https://api.github.com/repos/mortonjt/scikit-bio/contents/{+path}","compare_url":"https://api.github.com/repos/mortonjt/scikit-bio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mortonjt/scikit-bio/merges","archive_url":"https://api.github.com/repos/mortonjt/scikit-bio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mortonjt/scikit-bio/downloads","issues_url":"https://api.github.com/repos/mortonjt/scikit-bio/issues{/number}","pulls_url":"https://api.github.com/repos/mortonjt/scikit-bio/pulls{/number}","milestones_url":"https://api.github.com/repos/mortonjt/scikit-bio/milestones{/number}","notifications_url":"https://api.github.com/repos/mortonjt/scikit-bio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mortonjt/scikit-bio/labels{/name}","releases_url":"https://api.github.com/repos/mortonjt/scikit-bio/releases{/id}","created_at":"2014-09-11T00:39:57Z","updated_at":"2015-03-31T17:46:48Z","pushed_at":"2015-03-31T17:46:48Z","git_url":"git://github.com/mortonjt/scikit-bio.git","ssh_url":"git@github.com:mortonjt/scikit-bio.git","clone_url":"https://github.com/mortonjt/scikit-bio.git","svn_url":"https://github.com/mortonjt/scikit-bio","homepage":"http://scikit-bio.org","size":30277,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"biocore:master","ref":"master","sha":"79b8eda5bf608d74d636b3913382e0bebea36f30","user":{"login":"biocore","id":6241857,"avatar_url":"https://avatars.githubusercontent.com/u/6241857?v=3","gravatar_id":"","url":"https://api.github.com/users/biocore","html_url":"https://github.com/biocore","followers_url":"https://api.github.com/users/biocore/followers","following_url":"https://api.github.com/users/biocore/following{/other_user}","gists_url":"https://api.github.com/users/biocore/gists{/gist_id}","starred_url":"https://api.github.com/users/biocore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biocore/subscriptions","organizations_url":"https://api.github.com/users/biocore/orgs","repos_url":"https://api.github.com/users/biocore/repos","events_url":"https://api.github.com/users/biocore/events{/privacy}","received_events_url":"https://api.github.com/users/biocore/received_events","type":"Organization","site_admin":false},"repo":{"id":15168264,"name":"scikit-bio","full_name":"biocore/scikit-bio","owner":{"login":"biocore","id":6241857,"avatar_url":"https://avatars.githubusercontent.com/u/6241857?v=3","gravatar_id":"","url":"https://api.github.com/users/biocore","html_url":"https://github.com/biocore","followers_url":"https://api.github.com/users/biocore/followers","following_url":"https://api.github.com/users/biocore/following{/other_user}","gists_url":"https://api.github.com/users/biocore/gists{/gist_id}","starred_url":"https://api.github.com/users/biocore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biocore/subscriptions","organizations_url":"https://api.github.com/users/biocore/orgs","repos_url":"https://api.github.com/users/biocore/repos","events_url":"https://api.github.com/users/biocore/events{/privacy}","received_events_url":"https://api.github.com/users/biocore/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/biocore/scikit-bio","description":"Core objects, functions and statistics for working with biological data in Python.","fork":false,"url":"https://api.github.com/repos/biocore/scikit-bio","forks_url":"https://api.github.com/repos/biocore/scikit-bio/forks","keys_url":"https://api.github.com/repos/biocore/scikit-bio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/biocore/scikit-bio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/biocore/scikit-bio/teams","hooks_url":"https://api.github.com/repos/biocore/scikit-bio/hooks","issue_events_url":"https://api.github.com/repos/biocore/scikit-bio/issues/events{/number}","events_url":"https://api.github.com/repos/biocore/scikit-bio/events","assignees_url":"https://api.github.com/repos/biocore/scikit-bio/assignees{/user}","branches_url":"https://api.github.com/repos/biocore/scikit-bio/branches{/branch}","tags_url":"https://api.github.com/repos/biocore/scikit-bio/tags","blobs_url":"https://api.github.com/repos/biocore/scikit-bio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/biocore/scikit-bio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/biocore/scikit-bio/git/refs{/sha}","trees_url":"https://api.github.com/repos/biocore/scikit-bio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/biocore/scikit-bio/statuses/{sha}","languages_url":"https://api.github.com/repos/biocore/scikit-bio/languages","stargazers_url":"https://api.github.com/repos/biocore/scikit-bio/stargazers","contributors_url":"https://api.github.com/repos/biocore/scikit-bio/contributors","subscribers_url":"https://api.github.com/repos/biocore/scikit-bio/subscribers","subscription_url":"https://api.github.com/repos/biocore/scikit-bio/subscription","commits_url":"https://api.github.com/repos/biocore/scikit-bio/commits{/sha}","git_commits_url":"https://api.github.com/repos/biocore/scikit-bio/git/commits{/sha}","comments_url":"https://api.github.com/repos/biocore/scikit-bio/comments{/number}","issue_comment_url":"https://api.github.com/repos/biocore/scikit-bio/issues/comments{/number}","contents_url":"https://api.github.com/repos/biocore/scikit-bio/contents/{+path}","compare_url":"https://api.github.com/repos/biocore/scikit-bio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/biocore/scikit-bio/merges","archive_url":"https://api.github.com/repos/biocore/scikit-bio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/biocore/scikit-bio/downloads","issues_url":"https://api.github.com/repos/biocore/scikit-bio/issues{/number}","pulls_url":"https://api.github.com/repos/biocore/scikit-bio/pulls{/number}","milestones_url":"https://api.github.com/repos/biocore/scikit-bio/milestones{/number}","notifications_url":"https://api.github.com/repos/biocore/scikit-bio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/biocore/scikit-bio/labels{/name}","releases_url":"https://api.github.com/repos/biocore/scikit-bio/releases{/id}","created_at":"2013-12-13T16:24:41Z","updated_at":"2015-03-30T17:20:36Z","pushed_at":"2015-03-31T17:46:49Z","git_url":"git://github.com/biocore/scikit-bio.git","ssh_url":"git@github.com:biocore/scikit-bio.git","clone_url":"https://github.com/biocore/scikit-bio.git","svn_url":"https://github.com/biocore/scikit-bio","homepage":"http://scikit-bio.org","size":36701,"stargazers_count":111,"watchers_count":111,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":59,"mirror_url":null,"open_issues_count":118,"forks":59,"open_issues":118,"watchers":111,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/860"},"html":{"href":"https://github.com/biocore/scikit-bio/pull/860"},"issue":{"href":"https://api.github.com/repos/biocore/scikit-bio/issues/860"},"comments":{"href":"https://api.github.com/repos/biocore/scikit-bio/issues/860/comments"},"review_comments":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/860/comments"},"review_comment":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/biocore/scikit-bio/pulls/860/commits"},"statuses":{"href":"https://api.github.com/repos/biocore/scikit-bio/statuses/c4c057045e5e70d0b34b4a41d23fc5e1097829f4"}}}}