Skip to content

Commit

Permalink
pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
mortonjt committed Jul 31, 2016
1 parent d0093df commit defa170
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
22 changes: 13 additions & 9 deletions gneiss/_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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 = []
Expand Down
14 changes: 6 additions & 8 deletions gneiss/_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions gneiss/tests/test_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions gneiss/tests/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit defa170

Please sign in to comment.