Skip to content

Commit

Permalink
pycodestyle fixes (#249)
Browse files Browse the repository at this point in the history
* DOC: Updating changelog

* Update CHANGELOG.md

* FIX: fix issues with pep8

* FIX: pep8 -> pycodestyle

* DOC: fixing toctree

* STY: flake8

* Update test_balances.py

* Update test_balances.py

* Update test_balances.py
  • Loading branch information
mortonjt committed Jan 26, 2018
1 parent 73b4485 commit 5d724fc
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 259 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -14,7 +14,7 @@ help:
test:
$(TEST_COMMAND)
pep8:
pep8 gneiss setup.py
pycodestyle gneiss setup.py
flake8 gneiss setup.py

all: pep8 test
2 changes: 1 addition & 1 deletion ci/conda_requirements.txt
@@ -1,6 +1,6 @@
pip
nose
pep8
pycodestyle
flake8
IPython>4.0.0
notebook
Expand Down
116 changes: 1 addition & 115 deletions gneiss/balances.py
@@ -1,5 +1,6 @@
"""
Balances (:mod:`gneiss.balances`)
=================================
.. currentmodule:: gneiss.balances
Expand All @@ -14,7 +15,6 @@
:toctree: generated/
balance_basis
balanceplot
"""
# ----------------------------------------------------------------------------
Expand All @@ -28,14 +28,8 @@

from __future__ import division
import numpy as np
import pandas as pd
from skbio.stats.composition import clr_inv
from collections import OrderedDict
try:
import ete3
from gneiss.layouts import default_layout
except:
pass


def _balance_basis(tree_node):
Expand Down Expand Up @@ -168,111 +162,3 @@ def _count_matrix(treenode):
counts[n]['k'] = counts[n.parent]['k'] + counts[n.parent]['r']
counts[n]['t'] = counts[n.parent]['t']
return counts, n_tips


def _attach_balances(balances, tree):
""" Appends the balances to each of the internal nodes
in the ete tree.
Parameters
----------
balances : array_like, pd.Series
Vector of balances to plot on internal nodes of the tree.
If the balances is not in a `pd.Series`, it is assumed
to be stored in level order.
tree : skbio.TreeNode
Bifurcating tree to plot balances on.
Return
------
ete.Tree
The ETE representation of the tree with balances encoded
as node weights.
"""
nodes = [n for n in tree.traverse(include_self=True)]
n_tips = sum([n.is_tip() for n in nodes])
n_nontips = len(nodes) - n_tips
if len(balances) != n_nontips:
raise IndexError('The number of balances (%d) is not '
'equal to the number of internal nodes '
'in the tree (%d)' % (len(balances), n_nontips))
ete_tree = ete3.Tree.from_skbio(tree)
# Some random features in all nodes
i = 0
for n in ete_tree.traverse():
if not n.is_leaf():
if not isinstance(balances, pd.Series):
n.add_features(weight=balances[i])
else:
n.add_features(weight=balances.loc[n.name])
i += 1
return ete_tree


def balanceplot(balances, tree,
layout=None,
mode='c'):
""" Plots balances on tree.
Parameters
----------
balances : np.array
A vector of internal nodes and their associated real-valued balances.
The order of the balances will be assumed to be in level order.
tree : skbio.TreeNode
A strictly bifurcating tree defining a hierarchical relationship
between all of the features within `table`.
layout : function, optional
A layout for formatting the tree visualization. Must take a
`ete.tree` as a parameter.
mode : str
Type of display to show the tree. ('c': circular, 'r': rectangular).
Notes
-----
The `tree` is assumed to strictly bifurcating and whose tips match
`balances`. It is not recommended to attempt to plot trees with a
ton of leaves (i.e. more than 4000 leaves).
Examples
--------
>>> from gneiss.balances import balanceplot
>>> from skbio import TreeNode
>>> tree = u"((b,c)a, d)root;"
>>> t = TreeNode.read([tree])
>>> balances = [10, -10]
>>> tr, ts = balanceplot(balances, t)
>>> print(tr.get_ascii())
<BLANKLINE>
/-b
/a|
-root \-c
|
\-d
See Also
--------
skbio.TreeNode.levelorder
"""
ete_tree = _attach_balances(balances, tree)

# Create an empty TreeStyle
ts = ete3.TreeStyle()

# Set our custom layout function
if layout is None:
ts.layout_fn = default_layout
else:
ts.layout_fn = layout
# Draw a tree
ts.mode = mode

# We will add node names manually
ts.show_leaf_name = False
# Show branch data
ts.show_branch_length = True
ts.show_branch_support = True

return ete_tree, ts
96 changes: 0 additions & 96 deletions gneiss/layouts.py

This file was deleted.

46 changes: 1 addition & 45 deletions gneiss/tests/test_balances.py
Expand Up @@ -9,57 +9,13 @@
from __future__ import absolute_import, division, print_function
import unittest
import numpy as np
import pandas as pd
import numpy.testing as npt
from gneiss.balances import (balance_basis, _count_matrix,
_balance_basis, _attach_balances,
balanceplot)
from gneiss.layouts import default_layout
_balance_basis)
from skbio import TreeNode
from skbio.util import get_data_path


class TestPlot(unittest.TestCase):

def test__attach_balances(self):
tree = TreeNode.read([u"(a,b);"])
balances = np.array([10])
res_tree = _attach_balances(balances, tree)
self.assertEqual(res_tree.weight, 10)

def test__attach_balances_level_order(self):
tree = TreeNode.read([u"((a,b)c,d)r;"])
balances = np.array([10, -10])
res_tree = _attach_balances(balances, tree)
self.assertEqual(res_tree.weight, 10)
self.assertEqual(res_tree.children[0].weight, -10)

def test__attach_balances_bad_index(self):
tree = TreeNode.read([u"((a,b)c,d)r;"])
balances = np.array([10])
with self.assertRaises(IndexError):
_attach_balances(balances, tree)

def test__attach_balances_series(self):
tree = TreeNode.read([u"((a,b)c,d)r;"])
balances = pd.Series([10, -10], index=['r', 'c'])
res_tree = _attach_balances(balances, tree)
self.assertEqual(res_tree.weight, 10)

def test__attach_balances_series_bad(self):
tree = TreeNode.read([u"((a,b)c,d)r;"])
balances = pd.Series([10, -10])
with self.assertRaises(KeyError):
_attach_balances(balances, tree)

def test_balanceplot(self):
tree = TreeNode.read([u"((a,b)c,d)r;"])
balances = np.array([10, -10])
tr, ts = balanceplot(balances, tree)
self.assertEquals(ts.mode, 'c')
self.assertEquals(ts.layout_fn[0], default_layout)


class TestBalances(unittest.TestCase):

def test_count_matrix_base_case(self):
Expand Down
2 changes: 1 addition & 1 deletion gneiss/util.py
Expand Up @@ -264,7 +264,7 @@ def _type_cast_to_float(df):
s = df[c]
try:
df[c] = s.astype(np.float64)
except:
except Exception:
continue
return df

Expand Down

0 comments on commit 5d724fc

Please sign in to comment.