Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ENH Added `axis=None` functionality to ranking. #43

Merged
merged 3 commits into from

2 participants

@stroxler

Closes #32

The change affects la.farray.ranking() as well as larry's ranking
method. In addition to adding unit tests for axis=None, I also ported
over some of the newer farray tests to larry tests, since it appeared
that was how things had been working.

Steven Troxler added some commits
Steven Troxler ENH Added `axis=None` functionality to ranking.
Closes #32

The change affects la.farray.ranking() as well as larry's ranking
method. In addition to adding unit tests for `axis=None`, I also ported
over some of the newer farray tests to larry tests, since it appeared
that was how things had been working.
bc4eb27
Steven Troxler STY Using assert_larry_equal
Changed all larry ranking method tests to use assert_larry_equal.
0fd7cfe
Steven Troxler STY whitespace tweak 117bdaa
@kwgoodman kwgoodman merged commit 117bdaa into kwgoodman:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 4, 2012
  1. ENH Added `axis=None` functionality to ranking.

    Steven Troxler authored
    Closes #32
    
    The change affects la.farray.ranking() as well as larry's ranking
    method. In addition to adding unit tests for `axis=None`, I also ported
    over some of the newer farray tests to larry tests, since it appeared
    that was how things had been working.
  2. STY Using assert_larry_equal

    Steven Troxler authored
    Changed all larry ranking method tests to use assert_larry_equal.
  3. STY whitespace tweak

    Steven Troxler authored
This page is out of date. Refresh to see the latest.
View
4 README.rst
@@ -118,9 +118,9 @@ After you have installed ``la``, run the suite of unit tests::
>>> import la
>>> la.test()
<snip>
- Ran 3027 tests in 1.175s
+ Ran 3038 tests in 1.308s
OK
- <nose.result.TextTestResult run=3027 errors=0 failures=0>
+ <nose.result.TextTestResult run=3038 errors=0 failures=0>
The ``la`` package contains C extensions that speed up common alignment
operations such as adding two unaligned larrys. If the C extensions don't
View
1  RELEASE.rst
@@ -22,6 +22,7 @@ la 0.6
- datime.time and datetime.datetime labels can now be (HDF5) archived
- la.align() can now skip the axes you do not wish to align
- Upgrade numpydoc from 0.3.1 to 0.4 to support Sphinx 1.0.1
+- la.farray.ranking() and larry ranking method support `axis=None`
**Breakage from la 0.5**
View
2  la/deflarry.py
@@ -2992,7 +2992,7 @@ def ranking(self, axis=0, norm='-1,1'):
Parameters
----------
- axis : int, optional
+ axis : {int, None} optional
Axis to rank over. Default axis is 0.
norm: str, optional
A string that specifies the normalization:
View
5 la/farray/normalize.py
@@ -131,7 +131,7 @@ def ranking(x, axis=0, norm='-1,1'):
----------
x : ndarray
Data to be ranked.
- axis : int, optional
+ axis : {int, None} optional
Axis to rank over. Default axis is 0.
norm: str, optional
A string that specifies the normalization:
@@ -161,6 +161,9 @@ def ranking(x, axis=0, norm='-1,1'):
example, the output will have the same min and max along all columns.
"""
+ if axis is None:
+ ranked_x = ranking(x.reshape(-1), norm=norm)
+ return ranked_x.reshape(*x.shape)
ax = axis
if ax < 0:
# This converts a negative axis to the equivalent positive axis
View
34 la/farray/tests/farray_test.py
@@ -326,6 +326,40 @@ def test_ranking_14(self):
actual = ranking(x, axis=0)
assert_almost_equal(actual, desired)
+ def test_ranking_15(self):
+ "farray.ranking_15"
+ x = np.array([ -np.inf, nan, 1.0, np.inf])
+ desired = np.array([-1.0, nan, 0.0, 1.0])
+ actual = ranking(x, axis=None)
+ assert_almost_equal(actual, desired)
+
+ def test_ranking_16(self):
+ "farray.ranking_16"
+ x = np.array([[ 1.0, 1.0, 1.0, 1.0],
+ [ 1.0, 1.0, 2.0, 2.0],
+ [ 2.0, 2.0, 1.0, 2.0],
+ [ 2.0, 2.0, 1.0, 2.0]])
+ desired = np.array([[-1.0, -1.0, -1.0, -1.0],
+ [-1.0, -1.0, 1.0, 1.0],
+ [ 1.0, 1.0, -1.0, 1.0],
+ [ 1.0, 1.0, -1.0, 1.0]])
+ desired *= 8.0 / 15
+ actual = ranking(x, axis=None)
+ assert_almost_equal(actual, desired)
+
+ def test_ranking_17(self):
+ "farray.ranking_17"
+ x = np.array([[ nan, 1.0, 1.0, 1.0],
+ [ 1.0, 1.5, 2.0, 2.0],
+ [ 2.0, nan, 1.0, 2.0],
+ [ 2.0, 1.5, 1.0, 2.0]])
+ desired = np.array([[ nan, -1.0, -1.0, -1.0],
+ [-1.0, 0.0, 1.0, 1.0],
+ [ 1.0, nan, -1.0, 1.0],
+ [ 1.0, 0.0, -1.0, 1.0]])
+ desired *= 8.0 / 13
+ actual = ranking(x, axis=None)
+ assert_almost_equal(actual, desired)
class Test_geometric_mean(unittest.TestCase):
"Test farray.geometric_mean"
View
177 la/tests/deflarry_test.py
@@ -2982,7 +2982,7 @@ def test_move_sum_7(self):
"larry.move_sum_7"
t = np.array([[ nan, nan, nan, nan],
[ 2.0, 2.0, nan, 2.0],
- [ 1.0, 1.0, nan, 2.0]])
+ [ 1.0, 1.0, nan, 2.0]])
label = [[0, 1, 2], [0, 1, 2, 3]]
p = self.l2.move_sum(2, axis=0)
msg = printfail(t, p.x, 'x')
@@ -3105,12 +3105,8 @@ def test_ranking_1(self):
with np.errstate(invalid='ignore', divide='ignore'):
p = lx.ranking(axis=0)
label = [range(3), range(5)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_2(self):
"larry.ranking_2"
@@ -3124,12 +3120,8 @@ def test_ranking_2(self):
with np.errstate(invalid='ignore', divide='ignore'):
p = lx.ranking()
label = [range(3), range(5)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_3(self):
"larry.ranking_3"
@@ -3144,12 +3136,8 @@ def test_ranking_3(self):
[ 1.0, 0.0, 0.5, -0.5, -1.0]])
p = lx.ranking(axis=1)
label = [range(4), range(5)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_4(self):
"larry.ranking_4"
@@ -3158,12 +3146,8 @@ def test_ranking_4(self):
t = np.array([[1.0],[-1.0], [0.0]])
p = lx.ranking(axis=0)
label = [range(3), range(1)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_5(self):
"larry.ranking_5"
@@ -3173,12 +3157,8 @@ def test_ranking_5(self):
with np.errstate(invalid='ignore', divide='ignore'):
p = lx.ranking(axis=1)
label = [range(3), range(1)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_6(self):
"larry.ranking_6"
@@ -3194,12 +3174,8 @@ def test_ranking_6(self):
with np.errstate(invalid='ignore', divide='ignore'):
p = lx.ranking()
label = [range(4), range(5)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_7(self):
"larry.ranking_7"
@@ -3214,12 +3190,8 @@ def test_ranking_7(self):
[ 0.0, 1.0 , 0.0, 0.0 , -1.0]])
p = lx.ranking(1)
label = [range(4), range(5)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_8(self):
"larry.ranking_8"
@@ -3234,12 +3206,8 @@ def test_ranking_8(self):
[ 2.0/3, 1.0, 2.0/3, 1.0]])
p = lx.ranking(0)
label = [range(4), range(4)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_ranking_9(self):
"larry.ranking_9"
@@ -3256,12 +3224,111 @@ def test_ranking_9(self):
t = t.T
p = lx.ranking(1)
label = [range(4), range(4)]
- msg = printfail(t, p.x, 'x')
- t[np.isnan(t)] = self.nancode
- p[p.isnan()] = self.nancode
- self.assert_((abs(t - p.x) < self.tol).all(), msg)
- self.assert_(label == p.label, printfail(label, p.label, 'label'))
- self.assert_(noreference(p, lx), 'Reference found')
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_10(self):
+ "farray.ranking_10"
+ x = np.array([[ nan],
+ [ nan],
+ [ nan]])
+ lx = larry(x)
+ p = lx.ranking(0)
+ t = np.array([[ nan],
+ [ nan],
+ [ nan]])
+ label = [range(3), range(1)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_11(self):
+ "farray.ranking_11"
+ x = np.array([[ nan, nan],
+ [ nan, nan],
+ [ nan, nan]])
+ lx = larry(x)
+ p = lx.ranking(0)
+ t = np.array([[ nan, nan],
+ [ nan, nan],
+ [ nan, nan]])
+ label = [range(3), range(2)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_12(self):
+ "farray.ranking_12"
+ x = np.array([[ nan, nan, nan]])
+ lx = larry(x)
+ p = lx.ranking(1)
+ t = np.array([[ nan, nan, nan]])
+ label = [range(1), range(3)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_13(self):
+ "farray.ranking_13"
+ x = np.array([ 1.0, np.inf, 2.0])
+ lx = larry(x)
+ p = lx.ranking(0)
+ t = np.array([-1.0, 1.0, 0.0])
+ label = [range(3)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_14(self):
+ "farray.ranking_14"
+ x = np.array([ -np.inf, nan, 1.0, np.inf])
+ lx = larry(x)
+ p = lx.ranking(0)
+ t = np.array([-1.0, nan, 0.0, 1.0])
+ label = [range(4)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_15(self):
+ "farray.ranking_15"
+ x = np.array([ -np.inf, nan, 1.0, np.inf])
+ lx = larry(x)
+ p = lx.ranking(None)
+ t = np.array([-1.0, nan, 0.0, 1.0])
+ label = [range(4)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_16(self):
+ "farray.ranking_16"
+ x = np.array([[ 1.0, 1.0, 1.0, 1.0],
+ [ 1.0, 1.0, 2.0, 2.0],
+ [ 2.0, 2.0, 1.0, 2.0],
+ [ 2.0, 2.0, 1.0, 2.0]])
+ lx = larry(x)
+ p = lx.ranking(None)
+ t = np.array([[-1.0, -1.0, -1.0, -1.0],
+ [-1.0, -1.0, 1.0, 1.0],
+ [ 1.0, 1.0, -1.0, 1.0],
+ [ 1.0, 1.0, -1.0, 1.0]])
+ t *= 8.0 / 15
+ label = [range(4), range(4)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
+
+ def test_ranking_17(self):
+ "farray.ranking_17"
+ x = np.array([[ nan, 1.0, 1.0, 1.0],
+ [ 1.0, 1.5, 2.0, 2.0],
+ [ 2.0, nan, 1.0, 2.0],
+ [ 2.0, 1.5, 1.0, 2.0]])
+ lx = larry(x)
+ p = lx.ranking(None)
+ x = x.T
+ t = np.array([[ nan, -1.0, -1.0, -1.0],
+ [-1.0, 0.0, 1.0, 1.0],
+ [ 1.0, nan, -1.0, 1.0],
+ [ 1.0, 0.0, -1.0, 1.0]])
+ t *= 8.0 / 13
+ label = [range(4), range(4)]
+ t = la.larry(t, label)
+ ale(p, t, original=lx)
def test_movingrank_1(self):
"larry.movingrank_1"
Something went wrong with that request. Please try again.