Commits

Chris Mutel committed 09a4a90

Move tests for array proxies so Nose can find them

Comments (0)

Files changed (5)

bw2data/proxies/array.py

 # -*- coding: utf-8 -*
 import numpy as np
-import unittest
 
 
 class DifferentIndexTypesError(StandardError):
     """
 Provides a dictionary-based interface from database row ids to array indices.
 
-ArrayProxy provides a matrix or array whose indices are translated through a
-lookup dictionary. Slices are not generally supported.
+ArrayProxy provides a matrix or array whose indices are translated through a lookup dictionary. Slices are not generally supported.
     """
     def __init__(self, data, row_dict, col_dict=None):
         super(ArrayProxy, self).__init__()
             if obj == slice(None):
                 return obj
             else:
-                # Could support for slicing using Django's SortedDict
-                # (http://code.djangoproject.com/browser/django/trunk/django/utils/datastructures.py)
-                # or other sorted dictionary implementations, e.g.
+                # Could support for  sorted dictionary implementations, e.g.
                 # http://code.activestate.com/recipes/496761/ or
                 # http://www.voidspace.org.uk/downloads/odict.py
                 # However, this is not desperately needed...
-                raise NotImplementedError("Slices are not supported, " + \
-                    "because their meaning is unclear when translated into" + \
-                    " array indices. If you are confident, you can call " + \
+                raise NotImplementedError("Slices are not supported, "
+                    "because their meaning is unclear when translated into"
+                    " array indices. If you are confident, you can call "
                     "slices directly on self.data.")
         elif isinstance(obj, (list, tuple)):
             return tuple([self.translate(_obj, type) for _obj in obj])
-        else:  # No longer test for integer keys; subclasses can use strings
+        else:
+            # No longer test for integer keys; subclasses can use strings
             try:
                 if type == "row":
                     return self.row_dict[obj]
 
 class ListArrayProxy(object):
     """
-An interface to a list of objects that translates lookeups from foo[bar,baz] to
-foo.indices.index(bar)[baz]. If baz is a slice, returns a generator.
+An interface to a list of objects that translates lookeups from foo[bar,baz] to foo.indices.index(bar)[baz]. If baz is a slice, returns a generator.
 
-Takes list_, the list of objects, and optionally index_objs, which is an
-iterable of objects used as indices to list_. index_objs must all be of the
-same type.
+Takes list_, the list of objects, and optionally index_objs, which is an iterable of objects used as indices to list_. index_objs must all be of the same type.
     """
 
     __slots__ = ["indices", "list", "index_type"]
 
     def __repr__(self):
         return self.__unicode__()
-
-
-class ArrayProxyTest(unittest.TestCase):
-    def test_array_proxy(self):
-        a = np.zeros((3, 3))
-        for x in range(3):
-            for y in range(3):
-                a[x, y] = x + 3 * y
-        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
-        self.assertEqual(a[10, 20], 0)
-        self.assertTrue(np.allclose(a[11, (20, 21)], np.array((1, 4))))
-
-    def test_array_proxy_object(self):
-        class Dummy(object):
-            def __init__(self, id):
-                self.id = id
-
-        a = np.zeros((3, 3))
-        for x in range(3):
-            for y in range(3):
-                a[x, y] = x + 3 * y
-        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
-        obj = Dummy(10)
-        self.assertEqual(a[obj, 20], 0)
-
-    def test_array_proxy_key_error(self):
-        a = np.zeros((3, 3))
-        for x in range(3):
-            for y in range(3):
-                a[x, y] = x + 3 * y
-        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
-        self.assertRaises(KeyError, a.__getitem__, (20, 10))
-
-        class Vanilla(object):
-            pass
-        v = Vanilla()
-        self.assertRaises(KeyError, a.__getitem__, (v, 20))
-
-    def test_array_proxy_slices(self):
-        a = np.zeros((3, 3))
-        for x in range(3):
-            for y in range(3):
-                a[x, y] = x + 3 * y
-        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
-        self.assertRaises(NotImplementedError, a.__getitem__, (slice(None, 11,
-            None), 20))
-        # Note: np slices don't preserve shape!
-        self.assertTrue(np.allclose(a[:, 21], np.array((3, 4, 5,))))
-        self.assertTrue(np.allclose(a[11, :], np.array((1, 4, 7))))
-
-    def test_reverse_dict(self):
-        a = np.zeros((3, 3))
-        for x in range(3):
-            for y in range(3):
-                a[x, y] = x + 3 * y
-        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
-        self.assertFalse(hasattr(a, "_row_dict_rev"))
-        self.assertFalse(hasattr(a, "_col_dict_rev"))
-        self.assertTrue(a.row_dict_rev)
-        self.assertTrue(a.col_dict_rev)
-        self.assertEquals(a.row_dict_rev[0], 10)
-        self.assertEquals(a.col_dict_rev[0], 20)
-
-    def test_one_dimensional_proxy(self):
-        b = np.zeros((3,))
-        for x in range(3):
-            b[x] = x + 3
-        b = OneDimensionalArrayProxy(b, {10: 0, 11: 1, 12: 2})
-        self.assertEqual(b[11], 4)
-        self.assertTrue(np.allclose(b[:], np.array((3, 4, 5))))
-        b[11] = 13
-        self.assertEqual(b[11], 13)
-        self.assertTrue(np.allclose(b[:], np.array((3, 13, 5))))
-        b = np.zeros((3, 3))
-        self.assertRaises(AttributeError, OneDimensionalArrayProxy, b, {})
-        self.assertRaises(TypeError, OneDimensionalArrayProxy, b, {}, {})
-
-
-class ListArrayProxyTest(unittest.TestCase):
-    def test_list_array_proxy_without_indices(self):
-        l = ListArrayProxy((range(10), range(10)))
-        self.assertEqual(l[0], range(10))
-        self.assertEqual(list(l[:, 1]), [1, 1])
-        self.assertEqual(list(l[:, :]), [range(10), range(10)])
-        self.assertEqual(list(l[:, 4:6]), [range(10)[4:6], range(10)[4:6]])
-
-    def test_objects_as_indices(self):
-        class A(object):
-            pass
-        m = A()
-        n = A()
-        l = ListArrayProxy((range(5), range(5, 10)), [m, n])
-        self.assertEqual(l[n], range(5, 10))
-        self.assertEqual(l.sum(), sum(range(10)))
-        self.assertEqual(l[n, :2], [5, 6])
-
-    def test_mismatched_index_length(self):
-        self.assertRaises(ValueError, ListArrayProxy, range(5), range(4))
-
-    def test_list_array_proxy_with_indices(self):
-        class A(object):
-            pass
-
-        class B(object):
-            pass
-        m = A()
-        o = B()
-        self.assertRaises(DifferentIndexTypesError, ListArrayProxy, ((), ()),
-            [m, o])
-
-    def test_slices_with_indices(self):
-        l = ListArrayProxy((range(3), range(5), range(7)),
-            (3, 5, 7))
-        self.assertEqual(l[:], (range(3), range(5), range(7)))
-        self.assertRaises(InconsistentSlicingError, l.__getitem__, slice(3, 4))
-        self.assertFalse(l[:3])
-        self.assertEqual(l[:5], (range(3),))
-        self.assertEqual([x for x in l[::2]], [range(3), range(7)])
-        self.assertEqual([x for x in l[5:]], [range(5), range(7)])

bw2data/proxies/sparse.py

 # -*- coding: utf-8 -*
-import numpy as np
-import scipy.sparse
-import unittest
 from . import ArrayProxy
 
 
 class SparseMatrixProxy(ArrayProxy):
     """
-Provides a dictionary-based interface from database row ids to array indices 
-for sparse matrices. Does not assume a certain sparsity structure.
+Provides a dictionary-based interface from database row ids to array indices for sparse matrices. Does not assume a certain sparsity structure.
     """
     def __init__(self, data, row_dict, col_dict, *args, **kwargs):
         self.row_dict = row_dict
         y = self.translate(args[0][1], "col")
 
         if x == slice(None) or y == slice(None):
-            raise NotImplementedError("SparseMatrixProxy doesn't support " + \
+            raise NotImplementedError("SparseMatrixProxy doesn't support "
                 "slices; use CompressedSparseMatrixProxy for slices")
         return self.data.__getitem__((x, y),)
 
         y = self.translate(args[0][1], "col")
 
         if x == slice(None) or y == slice(None):
-            raise NotImplementedError, "SparseMatrixProxy doesn't support " +\
-                "assignment by slice"
-        self.data.__setitem__((x,y), args[1])
+            raise NotImplementedError("SparseMatrixProxy doesn't support "
+                "assignment by slice")
+        self.data.__setitem__((x, y), args[1])
 
     def get_row_as_dict(self, row):
-        raise NotImplementedError, "Use CompressedSparseMatrixProxy for " + \
-            "dictionary matrix slices"
+        raise NotImplementedError("Use CompressedSparseMatrixProxy for "
+            "dictionary matrix slices")
 
     def get_col_as_dict(self, col):
-        raise NotImplementedError, "Use CompressedSparseMatrixProxy for " + \
-            "dictionary matrix slices"
+        raise NotImplementedError("Use CompressedSparseMatrixProxy for "
+            "dictionary matrix slices")
 
     def toarray(self):
         return self.data.toarray()
 
 class CompressedSparseMatrixProxy(SparseMatrixProxy):
     """
-Subclass of SparseMatrixProxy for CSR (comma separated row) or CSC (comma 
-separated column) matrices.
+Subclass of SparseMatrixProxy for CSR (comma separated row) or CSC (comma separated column) matrices.
 
-This class will create, on demand, the complementary matrix type (e.g. CSC for 
-CSR) for efficient slicing and multiple assignment. If a matrix is changed, a 
-*dirty* flag is set that tells the class to re-create the complementary matrix 
-before accessing its information. All updates to complementary matrices are 
-lazy.
+This class will create, on demand, the complementary matrix type (e.g. CSC for CSR) for efficient slicing and multiple assignment. If a matrix is changed, a *dirty* flag is set that tells the class to re-create the complementary matrix before accessing its information. All updates to complementary matrices are lazy.
     """
 
     __slots__ = ['row_dict', 'col_dict', 'format', 'dirty', '_csr', '_csc']
         y = self.translate(args[0][1], "col")
 
         if isinstance(x, slice) and isinstance(y, slice):
-            raise NotImplementedError, \
-                "Convert to dense matrix to do slices on multiple dimensions"
+            raise NotImplementedError(
+                "Convert to dense matrix to do slices on multiple dimensions")
         elif isinstance(x, slice):
             if self.dirty and self.format == "csr":
                 self._csc = self._csr.tocsc()
-            return self._csc.__getitem__((x,y),)
+            return self._csc.__getitem__((x, y),)
         elif isinstance(y, slice):
             if self.dirty and self.format == "csc":
                 self._csr = self._csc.tocsr()
-            return self._csr.__getitem__((x,y),)
+            return self._csr.__getitem__((x, y),)
         else:
-            return self.data.__getitem__((x,y),)
+            return self.data.__getitem__((x, y),)
 
     def _get_data(self):
         if self.format == "csr":
         super(CompressedSparseMatrixProxy, self).__setitem__(*args)
 
     def get_row_as_dict(self, row):
-        obj = self.__getitem__((row,slice(None)),)
-        return dict([(self.row_dict_rev[obj.indices[index]], obj.data[index] 
+        obj = self.__getitem__((row, slice(None)),)
+        return dict([(self.row_dict_rev[obj.indices[index]], obj.data[index]
             ) for index in xrange(obj.indices.shape[0])])
 
     def get_col_as_dict(self, col):
-        obj = self.__getitem__((slice(None),col),)
-        return dict([(self.col_dict_rev[obj.indices[index]], obj.data[index] 
+        obj = self.__getitem__((slice(None), col),)
+        return dict([(self.col_dict_rev[obj.indices[index]], obj.data[index]
             ) for index in xrange(obj.indices.shape[0])])
-
-
-class SparseMatrixProxyTest(unittest.TestCase):
-    def test_sparse_matrix_proxy(self):
-        mat = scipy.sparse.lil_matrix((3,3))
-        for x in range(3):
-            for y in range(3):
-                if x == y: continue
-                mat[x,y] = x+3*y
-        mat = SparseMatrixProxy(mat, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 
-            22: 2})
-        self.assertEqual(mat[10, 20], 0)
-        self.assertEqual(mat[11, 22], 7)
-        self.assertEqual(mat.nnz, 6)
-        mat[11,21] = 3
-        self.assertEqual(mat[11,21], 3)
-        self.assertRaises(NotImplementedError, mat.__getitem__, (slice(None), 
-            21))
-        self.assertRaises(NotImplementedError, mat.__setitem__, (slice(None), 
-            21), 1)
-
-    def test_compressed_sparse_matrix_proxy(self):
-        c = scipy.sparse.lil_matrix((3,3))
-        for x in range(3):
-            for y in range(3):
-                if x == y: continue
-                c[x,y] = x+3*y
-        c = CompressedSparseMatrixProxy(c, {10: 0, 11: 1, 12: 2}, 
-            {20: 0, 21: 1, 22: 2})
-        self.assertEqual(c[10, 20], 0)
-        self.assertEqual(c[11, 22], 7)
-        self.assertEqual(c.nnz, 6)
-        self.assertTrue(isinstance(c.data, scipy.sparse.csr.csr_matrix))
-        self.assertTrue(np.allclose(c[:,21].todense(), np.array(((3,),(
-            0,),(5,)))))
-        self.assertTrue(np.allclose(c[11,:].todense(), np.array((1,0,7))))
-        c[11,21] = 3
-        assert c[11,21] == 3

bw2data/tests/__init__.py

 from .database import DatabaseTest
 from .geo import GeoTest
 from .utils import UtilsTest
+from .array import ArrayProxyTest, ListArrayProxyTest
+from .sparse import SparseMatrixProxyTest

bw2data/tests/array.py

+import unittest
+from ..proxies.array import ArrayProxy, OneDimensionalArrayProxy, \
+    ListArrayProxy, DifferentIndexTypesError, InconsistentSlicingError
+import numpy as np
+
+
+class ArrayProxyTest(unittest.TestCase):
+    def test_array_proxy(self):
+        a = np.zeros((3, 3))
+        for x in range(3):
+            for y in range(3):
+                a[x, y] = x + 3 * y
+        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
+        self.assertEqual(a[10, 20], 0)
+        self.assertTrue(np.allclose(a[11, (20, 21)], np.array((1, 4))))
+
+    def test_array_proxy_object(self):
+        class Dummy(object):
+            def __init__(self, id):
+                self.id = id
+
+        a = np.zeros((3, 3))
+        for x in range(3):
+            for y in range(3):
+                a[x, y] = x + 3 * y
+        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
+        obj = Dummy(10)
+        self.assertEqual(a[obj, 20], 0)
+
+    def test_array_proxy_key_error(self):
+        a = np.zeros((3, 3))
+        for x in range(3):
+            for y in range(3):
+                a[x, y] = x + 3 * y
+        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
+        self.assertRaises(KeyError, a.__getitem__, (20, 10))
+
+        class Vanilla(object):
+            pass
+        v = Vanilla()
+        self.assertRaises(KeyError, a.__getitem__, (v, 20))
+
+    def test_array_proxy_slices(self):
+        a = np.zeros((3, 3))
+        for x in range(3):
+            for y in range(3):
+                a[x, y] = x + 3 * y
+        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
+        self.assertRaises(NotImplementedError, a.__getitem__, (slice(None, 11,
+            None), 20))
+        # Note: np slices don't preserve shape!
+        self.assertTrue(np.allclose(a[:, 21], np.array((3, 4, 5,))))
+        self.assertTrue(np.allclose(a[11, :], np.array((1, 4, 7))))
+
+    def test_reverse_dict(self):
+        a = np.zeros((3, 3))
+        for x in range(3):
+            for y in range(3):
+                a[x, y] = x + 3 * y
+        a = ArrayProxy(a, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1, 22: 2})
+        self.assertFalse(hasattr(a, "_row_dict_rev"))
+        self.assertFalse(hasattr(a, "_col_dict_rev"))
+        self.assertTrue(a.row_dict_rev)
+        self.assertTrue(a.col_dict_rev)
+        self.assertEquals(a.row_dict_rev[0], 10)
+        self.assertEquals(a.col_dict_rev[0], 20)
+
+    def test_one_dimensional_proxy(self):
+        b = np.zeros((3,))
+        for x in range(3):
+            b[x] = x + 3
+        b = OneDimensionalArrayProxy(b, {10: 0, 11: 1, 12: 2})
+        self.assertEqual(b[11], 4)
+        self.assertTrue(np.allclose(b[:], np.array((3, 4, 5))))
+        b[11] = 13
+        self.assertEqual(b[11], 13)
+        self.assertTrue(np.allclose(b[:], np.array((3, 13, 5))))
+        b = np.zeros((3, 3))
+        self.assertRaises(AttributeError, OneDimensionalArrayProxy, b, {})
+        self.assertRaises(TypeError, OneDimensionalArrayProxy, b, {}, {})
+
+
+class ListArrayProxyTest(unittest.TestCase):
+    def test_list_array_proxy_without_indices(self):
+        l = ListArrayProxy((range(10), range(10)))
+        self.assertEqual(l[0], range(10))
+        self.assertEqual(list(l[:, 1]), [1, 1])
+        self.assertEqual(list(l[:, :]), [range(10), range(10)])
+        self.assertEqual(list(l[:, 4:6]), [range(10)[4:6], range(10)[4:6]])
+
+    def test_objects_as_indices(self):
+        class A(object):
+            pass
+        m = A()
+        n = A()
+        l = ListArrayProxy((range(5), range(5, 10)), [m, n])
+        self.assertEqual(l[n], range(5, 10))
+        self.assertEqual(l.sum(), sum(range(10)))
+        self.assertEqual(l[n, :2], [5, 6])
+
+    def test_mismatched_index_length(self):
+        self.assertRaises(ValueError, ListArrayProxy, range(5), range(4))
+
+    def test_list_array_proxy_with_indices(self):
+        class A(object):
+            pass
+
+        class B(object):
+            pass
+        m = A()
+        o = B()
+        self.assertRaises(DifferentIndexTypesError, ListArrayProxy, ((), ()),
+            [m, o])
+
+    def test_slices_with_indices(self):
+        l = ListArrayProxy((range(3), range(5), range(7)),
+            (3, 5, 7))
+        self.assertEqual(l[:], (range(3), range(5), range(7)))
+        self.assertRaises(InconsistentSlicingError, l.__getitem__, slice(3, 4))
+        self.assertFalse(l[:3])
+        self.assertEqual(l[:5], (range(3),))
+        self.assertEqual([x for x in l[::2]], [range(3), range(7)])
+        self.assertEqual([x for x in l[5:]], [range(5), range(7)])

bw2data/tests/sparse.py

+# -*- coding: utf-8 -*
+import numpy as np
+import scipy.sparse
+import unittest
+from ..proxies.sparse import SparseMatrixProxy, CompressedSparseMatrixProxy
+
+
+class SparseMatrixProxyTest(unittest.TestCase):
+    def test_sparse_matrix_proxy(self):
+        mat = scipy.sparse.lil_matrix((3, 3))
+        for x in range(3):
+            for y in range(3):
+                if x == y:
+                    continue
+                mat[x, y] = x + 3 * y
+        mat = SparseMatrixProxy(mat, {10: 0, 11: 1, 12: 2}, {20: 0, 21: 1,
+            22: 2})
+        self.assertEqual(mat[10, 20], 0)
+        self.assertEqual(mat[11, 22], 7)
+        self.assertEqual(mat.nnz, 6)
+        mat[11, 21] = 3
+        self.assertEqual(mat[11, 21], 3)
+        self.assertRaises(NotImplementedError, mat.__getitem__, (slice(None),
+            21))
+        self.assertRaises(NotImplementedError, mat.__setitem__, (slice(None),
+            21), 1)
+
+    def test_compressed_sparse_matrix_proxy(self):
+        c = scipy.sparse.lil_matrix((3, 3))
+        for x in range(3):
+            for y in range(3):
+                if x == y:
+                    continue
+                c[x, y] = x + 3 * y
+        c = CompressedSparseMatrixProxy(c, {10: 0, 11: 1, 12: 2},
+            {20: 0, 21: 1, 22: 2})
+        self.assertEqual(c[10, 20], 0)
+        self.assertEqual(c[11, 22], 7)
+        self.assertEqual(c.nnz, 6)
+        self.assertTrue(isinstance(c.data, scipy.sparse.csr.csr_matrix))
+        self.assertTrue(np.allclose(
+            c[:, 21].todense(),
+            np.array(((3,), (0,), (5,)))
+            ))
+        self.assertTrue(np.allclose(
+            c[11, :].todense(),
+            np.array((1, 0, 7))
+            ))
+        c[11, 21] = 3
+        assert c[11, 21] == 3