From 5d724fc8d467acf72ec646cbfe63c65863da2488 Mon Sep 17 00:00:00 2001 From: Jamie Morton Date: Fri, 26 Jan 2018 10:55:49 -0800 Subject: [PATCH] pycodestyle fixes (#249) * 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 --- Makefile | 2 +- ci/conda_requirements.txt | 2 +- gneiss/balances.py | 116 +--------------------------------- gneiss/layouts.py | 96 ---------------------------- gneiss/tests/test_balances.py | 46 +------------- gneiss/util.py | 2 +- 6 files changed, 5 insertions(+), 259 deletions(-) delete mode 100644 gneiss/layouts.py diff --git a/Makefile b/Makefile index e0a02d1..0d48de2 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ help: test: $(TEST_COMMAND) pep8: - pep8 gneiss setup.py + pycodestyle gneiss setup.py flake8 gneiss setup.py all: pep8 test diff --git a/ci/conda_requirements.txt b/ci/conda_requirements.txt index b7b4eae..54741c1 100644 --- a/ci/conda_requirements.txt +++ b/ci/conda_requirements.txt @@ -1,6 +1,6 @@ pip nose -pep8 +pycodestyle flake8 IPython>4.0.0 notebook diff --git a/gneiss/balances.py b/gneiss/balances.py index 542b20a..94e1672 100644 --- a/gneiss/balances.py +++ b/gneiss/balances.py @@ -1,5 +1,6 @@ """ Balances (:mod:`gneiss.balances`) + ================================= .. currentmodule:: gneiss.balances @@ -14,7 +15,6 @@ :toctree: generated/ balance_basis - balanceplot """ # ---------------------------------------------------------------------------- @@ -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): @@ -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()) - - /-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 diff --git a/gneiss/layouts.py b/gneiss/layouts.py deleted file mode 100644 index 3864b30..0000000 --- a/gneiss/layouts.py +++ /dev/null @@ -1,96 +0,0 @@ -# ---------------------------------------------------------------------------- -# Copyright (c) 2016--, gneiss development team. -# -# Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -# ---------------------------------------------------------------------------- -try: - import ete3 -except: - pass - - -def default_layout(node): - """ - Specifies the layout for the ete.TreeStyle object. - - Parameters - ---------- - node: ete.Tree - Input node for specifying which attributes. - """ - - if node.is_leaf(): - # Add node name to leaf nodes - N = ete3.AttrFace("name", fsize=14, fgcolor="black") - - ete3.faces.add_face_to_node(N, node, 0) - if "weight" in node.features: - # Creates a sphere face whose size is proportional to node's - # feature "weight" - C = ete3.CircleFace(radius=node.weight, color="Red", style="sphere") - # Let's make the sphere transparent - C.opacity = 0.5 - # Rotate the faces by 90* - C.rotation = 90 - # And place as a float face over the tree - ete3.faces.add_face_to_node(C, node, 0, position="float") - - -def barchart_layout(node, name='name', - width=20, height=40, - colors=None, min_value=0, max_value=1, - fsize=14, fgcolor="black", - alpha=0.5, - rotation=270): - """ - Specifies the layout for the ete.TreeStyle object. - - Parameters - ---------- - node: ete.Tree - Input node for specifying which attributes. - name: str, optional - Attribute to look up the name of the node. - width: int, optional - Width of the barchart. - height: int, optional - Height of the barchart. - colors: list of str, optional - List of HTML colors to color the barchart values. - min_value: int, optional - Minimum value to set the scale of the chart. - max_value: int, optional - Maximum value to set the scale of the chart. - fsize: int, optional - Font size on the leafs. - fgcolor: str, optional - Font color of the leafs. - alpha: float, optional - Transparency of the barchart. - rotation: int, optional - Orientation of the barchart. - """ - if colors is None: - colors = ['#0000FF'] - if node.is_leaf(): - # Add node name to leaf nodes - N = ete3.AttrFace("name", fsize=fsize, fgcolor=fgcolor) - ete3.faces.add_face_to_node(N, node, 0) - if "weight" in node.features: - # Creates a sphere face whose size is proportional to node's - # feature "weight" - if (isinstance(node.weight, int) or isinstance(node.weight, float)): - weight = [node.weight] - else: - weight = node.weight - C = ete3.BarChartFace(values=weight, width=width, height=height, - colors=colors, min_value=min_value, - max_value=max_value) - # Let's make the sphere transparent - C.opacity = alpha - # Rotate the faces by 270* - C.rotation = rotation - # And place as a float face over the tree - ete3.faces.add_face_to_node(C, node, 0, position="float") diff --git a/gneiss/tests/test_balances.py b/gneiss/tests/test_balances.py index 3af8262..a3f61eb 100644 --- a/gneiss/tests/test_balances.py +++ b/gneiss/tests/test_balances.py @@ -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): diff --git a/gneiss/util.py b/gneiss/util.py index efefce0..32ad71b 100644 --- a/gneiss/util.py +++ b/gneiss/util.py @@ -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