Skip to content

Commit

Permalink
ENH: style / pep8 / docstring fixes in s/l/utils/fixes.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ogrisel committed Apr 25, 2011
1 parent 274dcc2 commit 5709c56
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions scikits/learn/utils/fixes.py
@@ -1,15 +1,14 @@
"""
Fixes for older version of numpy and scipy.
"""
"""Compatibility fixes for older version of numpy and scipy"""
# Authors: Emmanuelle Gouillart <emmanuelle.gouillart@normalesup.org>
# Gael Varoquaux <gael.varoquaux@normalesup.org>
# Fabian Pedregosa <fpedregosa@acm.org>
# License: BSD

import numpy as np


def _unique(ar, return_index=False, return_inverse=False):
""" A replacement for the np.unique that appeared in numpy 1.4.
"""A replacement for the np.unique that appeared in numpy 1.4.
While np.unique existed long before, keyword return_inverse was
only added in 1.4.
Expand Down Expand Up @@ -57,10 +56,8 @@ def _unique(ar, return_index=False, return_inverse=False):
unique = np.unique


def _copysign (x1, x2):
"""
(slow) Replacement for np.copysign, which was introduced in numpy 1.4
"""
def _copysign(x1, x2):
"""Slow replacement for np.copysign, which was introduced in numpy 1.4"""
return np.abs(x1) * np.sign(x2)

if not hasattr(np, 'copysign'):
Expand All @@ -70,20 +67,19 @@ def _copysign (x1, x2):


def _in1d(ar1, ar2, assume_unique=False):
""" Replacement for in1d that is provided for numpy >= 1.4
"""
"""Replacement for in1d that is provided for numpy >= 1.4"""
if not assume_unique:
ar1, rev_idx = unique(ar1, return_inverse=True)
ar2 = np.unique(ar2)
ar = np.concatenate( (ar1, ar2) )
ar = np.concatenate((ar1, ar2))
# We need this to be a stable sort, so always use 'mergesort'
# here. The values from the first array should always come before
# the values from the second array.
order = ar.argsort(kind='mergesort')
sar = ar[order]
equal_adj = (sar[1:] == sar[:-1])
flag = np.concatenate( (equal_adj, [False] ) )
indx = order.argsort(kind='mergesort')[:len( ar1 )]
flag = np.concatenate((equal_adj, [False]))
indx = order.argsort(kind='mergesort')[:len(ar1)]

if assume_unique:
return flag[indx]
Expand All @@ -97,7 +93,8 @@ def _in1d(ar1, ar2, assume_unique=False):


def qr_economic(A, **kwargs):
"""
"""Compat function for the QR-decomposition in economic mode
Scipy 0.9 changed the keyword econ=True to mode='economic'
"""
import scipy.linalg
Expand All @@ -109,7 +106,8 @@ def qr_economic(A, **kwargs):


def arpack_eigsh(A, **kwargs):
"""
"""Compat function for sparse symmetric eigen vectors decomposition
Scipy 0.9 renamed eigen_symmetric to eigsh in
scipy.sparse.linalg.eigen.arpack
"""
Expand All @@ -118,8 +116,3 @@ def arpack_eigsh(A, **kwargs):
return arpack.eigsh(A, **kwargs)
else:
return arpack.eigen_symmetric(A, **kwargs)





0 comments on commit 5709c56

Please sign in to comment.