Skip to content
Browse files

ENH misc.fromlists() now raises on overlapping input

  • Loading branch information...
1 parent f3a353d commit 0a711b12de872cfd73e4c7e261cc99286ea57081 @kwgoodman committed
Showing with 66 additions and 40 deletions.
  1. +2 −2 README.rst
  2. +1 −0 RELEASE.rst
  3. +42 −36 la/util/misc.py
  4. +21 −2 la/util/tests/misc_test.py
View
4 README.rst
@@ -120,9 +120,9 @@ After you have installed ``la``, run the suite of unit tests::
>>> import la
>>> la.test()
<snip>
- Ran 3009 tests in 12.225s
+ Ran 3011 tests in 12.225s
OK
- <nose.result.TextTestResult run=3009 errors=0 failures=0>
+ <nose.result.TextTestResult run=3011 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
1 RELEASE.rst
@@ -30,6 +30,7 @@ la 0.7
- Added setitem ability to label indexing (lix)
- la.IO no longer keeps the archive connection open between calls
- C files generated with Cython 0.19.1 instead of 0.15.1
+- misc.fromlists() now raises on overlapping input data
**Breakage from la 0.6**
View
78 la/util/misc.py
@@ -6,33 +6,36 @@
import numpy as np
from la.flabel import list2index
-
+
C = string.ascii_letters + string.digits
+
+
def randstring(n):
"Random characters string selected from lower, upper letters and digits."
s = []
nc = len(C) - 1
for i in range(n):
s.append(C[random.randint(0, nc)])
- return ''.join(s)
-
+ return ''.join(s)
+
+
def isint(x):
"""Returns True if input is an integer; False otherwise.
-
+
Parameters
----------
x : any
Input can be of any type.
-
+
Returns
-------
y : bool
True is `x` is an integer, False otherwise.
-
+
Notes
-----
A table showing what isint returns for various types:
-
+
========== =======
type isint
========== =======
@@ -45,7 +48,7 @@ def isint(x):
complex False
str False
bool False
-
+
Examples
--------
>>> isint(1)
@@ -57,28 +60,29 @@ def isint(x):
>>> isint(1j)
False
>>> isint('a')
- False
-
+ False
+
"""
return np.issubdtype(type(x), int)
-
+
+
def isfloat(x):
"""Returns True if input is a float; False otherwise.
-
+
Parameters
----------
x : any
Input can be of any type.
-
+
Returns
-------
y : bool
True is `x` is a float, False otherwise.
-
+
Notes
-----
A table showing what isfloat returns for various types:
-
+
========== =======
type isint
========== =======
@@ -91,7 +95,7 @@ def isfloat(x):
complex False
str False
bool False
-
+
Examples
--------
>>> isfloat(1)
@@ -103,28 +107,29 @@ def isfloat(x):
>>> isfloat(1.1j)
False
>>> isfloat('a')
- False
-
+ False
+
"""
return np.issubdtype(type(x), float)
-
+
+
def isscalar(x):
"""Returns True if input is a scalar; False otherwise.
-
+
Parameters
----------
x : any
Input can be of any type.
-
+
Returns
-------
y : bool
True is `x` is a scalar, False otherwise.
-
+
Notes
-----
A table showing what isscalar returns for various types:
-
+
========== =======
type isint
========== =======
@@ -137,7 +142,7 @@ def isscalar(x):
complex False
str False
bool False
-
+
Examples
--------
>>> isscalar(1)
@@ -149,19 +154,21 @@ def isscalar(x):
>>> isscalar(1j)
False
>>> isscalar('a')
- False
-
+ False
+
"""
return isfloat(x) or isint(x)
-
+
+
def isstring(s):
"Return True if input is a str or np.string_."
return issubclass(type(s), str)
+
def fromlists(xs, labels):
"""
Convert list of values and list of label tuples to larry label and x.
-
+
Parameters
----------
xs : {tuple, list}
@@ -170,14 +177,14 @@ def fromlists(xs, labels):
labels : list of tuples
A list of tuples, one tuple per dimension of the output array, that
give the label (coordinates) of the corresponding elements in `xs`.
-
+
Returns
-------
x : Numpy ndarray
A Numpy array with order and shape given by `labels`.
label : list
The label that corresponds to `x`.
-
+
Examples
--------
>>> from la.util.misc import fromlists
@@ -189,10 +196,7 @@ def fromlists(xs, labels):
[ 3., 4.]])
>>> label
[['a', 'b'], ['a', 'b']]
- >>> x
- array([[ 1., 2.],
- [ 3., 4.]])
-
+
"""
if (len(xs) == 0) and (len(labels) == 0):
x = np.array([])
@@ -206,7 +210,9 @@ def fromlists(xs, labels):
shape.append(len(label_unique))
index.append(labelidx)
label.append(label_unique)
+ if len(set(zip(*index))) != len(index[0]):
+ raise ValueError('Overlap in data')
x = np.empty(shape)
x.fill(np.nan)
- x[index] = xs
- return x, label
+ x[index] = xs
+ return x, label
View
23 la/util/tests/misc_test.py
@@ -3,9 +3,9 @@
import unittest
import numpy as np
-from numpy.testing import assert_equal
+from numpy.testing import assert_equal, assert_raises
-from la.util.misc import randstring, isint, isfloat, isscalar
+from la.util.misc import randstring, isint, isfloat, isscalar, fromlists
class Test_misc(unittest.TestCase):
@@ -16,6 +16,7 @@ def test_randstring_1(self):
rs = randstring(4)
self.assertTrue(len(rs) == 4, 'Wrong length string.')
+
def test_isa():
"util.misc.isint, isfloat, isscalar"
t = {}
@@ -38,3 +39,21 @@ def test_isa():
yield assert_equal, isfloat(key), value[1], msg
msg = '\nisscalar(' + str(key) + ')'
yield assert_equal, isscalar(key), (value[0] or value[1]), msg
+
+
+def test_fromlists_1():
+ "misc.fromlists #1"
+ xs = [1, 2, 3, 4]
+ labels = [('a', 'a', 'b', 'b'), ('a', 'b', 'a', 'b')]
+ ax, alabel = fromlists(xs, labels)
+ dx = np.array([[1., 2.], [ 3., 4.]])
+ dlabel = [['a', 'b'], ['a', 'b']]
+ assert_equal(ax, dx, err_msg='arrays do not match')
+ assert_equal(alabel, dlabel, err_msg='labels do not match')
+
+
+def test_fromlists_2():
+ "misc.fromlists #2"
+ xs = [1, 2, 3, 4]
+ labels = [('a', 'a', 'b', 'b'), ('a', 'a', 'a', 'b')]
+ assert_raises(ValueError, fromlists, xs, labels)

0 comments on commit 0a711b1

Please sign in to comment.
Something went wrong with that request. Please try again.