From defa170b9fe76d247eb6854a7a01be1e24427ac8 Mon Sep 17 00:00:00 2001 From: Jamie Morton Date: Sat, 30 Jul 2016 19:18:33 -0700 Subject: [PATCH] pep8 --- gneiss/_formula.py | 22 +++++++++++++--------- gneiss/_summary.py | 14 ++++++-------- gneiss/tests/test_formula.py | 4 +--- gneiss/tests/test_summary.py | 6 +++--- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/gneiss/_formula.py b/gneiss/_formula.py index 22fa7e3..c4a2dfc 100644 --- a/gneiss/_formula.py +++ b/gneiss/_formula.py @@ -8,7 +8,7 @@ import pandas as pd import statsmodels.formula.api as smf from skbio.stats.composition import ilr -from gneiss.util import match, match_tips +from gneiss.util import match, match_tips, rename_internal_nodes from gneiss._summary import RegressionResults from gneiss.balances import balance_basis @@ -28,6 +28,17 @@ def _process(table, metadata, tree): return _table, _metadata, _tree +def _transform(table, tree): + non_tips = [n.name for n in tree.levelorder() if not n.is_tip()] + basis, _ = balance_basis(tree) + + mat = ilr(table.values, basis=basis) + ilr_table = pd.DataFrame(mat, + columns=non_tips, + index=table.index) + return ilr_table, basis + + def ols(formula, table, metadata, tree, **kwargs): """ Ordinary Least Squares applied to balances. @@ -64,14 +75,7 @@ def ols(formula, table, metadata, tree, **kwargs): statsmodels.regression.linear_model.OLS """ _table, _metadata, _tree = _process(table, metadata, tree) - basis, _ = balance_basis(_tree) - non_tips = [n.name for n in _tree.levelorder() if not n.is_tip()] - - mat = ilr(_table.values, basis=basis) - ilr_table = pd.DataFrame(mat, - columns=non_tips, - index=table.index) - + ilr_table, basis = _transform(_table, _tree) data = pd.merge(ilr_table, _metadata, left_index=True, right_index=True) fits = [] diff --git a/gneiss/_summary.py b/gneiss/_summary.py index 89bfd3d..77ce511 100644 --- a/gneiss/_summary.py +++ b/gneiss/_summary.py @@ -163,11 +163,10 @@ def residuals(self, project=False): # https://github.com/biocore/scikit-bio/pull/1396 proj_resid = ilr_inv(resid.values.T, basis=self.basis, check=False).T - proj_resid = pd.DataFrame(proj_resid, index=self.feature_names, - columns=resid.columns) - return proj_resid + return pd.DataFrame(proj_resid, index=self.feature_names, + columns=resid.columns).T else: - return resid + return resid.T def predict(self, X=None, project=False, **kwargs): """ Performs a prediction based on model. @@ -210,9 +209,8 @@ def predict(self, X=None, project=False, **kwargs): # https://github.com/biocore/scikit-bio/pull/1396 proj_prediction = ilr_inv(prediction.values.T, basis=self.basis, check=False) - proj_prediction = pd.DataFrame(proj_prediction, - columns=self.feature_names, - index=prediction.columns) - return proj_prediction + return pd.DataFrame(proj_prediction, + columns=self.feature_names, + index=prediction.columns).T return prediction.T diff --git a/gneiss/tests/test_formula.py b/gneiss/tests/test_formula.py index d6816eb..8e9233a 100644 --- a/gneiss/tests/test_formula.py +++ b/gneiss/tests/test_formula.py @@ -10,10 +10,8 @@ import numpy as np import pandas as pd import pandas.util.testing as pdt -import statsmodels.formula.api as smf import unittest -from gneiss._summary import RegressionResults -from skbio.stats.composition import _gram_schmidt_basis, ilr_inv +from skbio.stats.composition import ilr_inv from skbio import TreeNode from gneiss._formula import ols diff --git a/gneiss/tests/test_summary.py b/gneiss/tests/test_summary.py index ef9e15d..ed1d1c6 100644 --- a/gneiss/tests/test_summary.py +++ b/gneiss/tests/test_summary.py @@ -117,7 +117,7 @@ def test_regression_results_residuals_projection(self): 's5': ilr_inv(A([-1.065789, 1.184211])), 's6': ilr_inv(A([-1.144737, -0.394737])), 's7': ilr_inv(A([0.394737, 1.894737]))}, - index=['Z1', 'Z2', 'Z3']) + index=['Z1', 'Z2', 'Z3']).T feature_names = ['Z1', 'Z2', 'Z3'] basis = _gram_schmidt_basis(3) res = RegressionResults(self.results, basis=basis, @@ -134,7 +134,7 @@ def test_regression_results_residuals(self): 's5': [-1.065789, 1.184211], 's6': [-1.144737, -0.394737], 's7': [0.394737, 1.894737]}, - index=['Y1', 'Y2']) + index=['Y1', 'Y2']).T res = RegressionResults(self.results) pdt.assert_frame_equal(res.residuals(), exp_resid, check_exact=False, @@ -183,7 +183,7 @@ def test_regression_results_predict_projection(self): 's5': ilr_inv(A([3.065789, 3.815789])), 's6': ilr_inv(A([4.144737, 6.394737])), 's7': ilr_inv(A([3.605263, 5.105263]))}, - index=feature_names).T + index=feature_names) pdt.assert_frame_equal(res_predict, exp_predict)