def test_iter(self): """PairMatrix __iter__ should iterate over rows.""" p = self.ab_pairs c = PairMatrix([1, 2, 3, 4], p) l = list(c) self.assertEqual(len(l), 2) self.assertEqual(list(l[0]), [1, 2]) self.assertEqual(list(l[1]), [3, 4])
def test_getitem(self): """PairMatrix __getitem__ should translate indices and get from array""" n = self.named self.assertEqual(n['a'], array([1, 2])) self.assertEqual(n['b'], array([3, 4])) self.assertEqual(n['a', 'a'], 1) self.assertEqual(n['a', 'b'], 2) self.assertEqual(n['b', 'a'], 3) self.assertEqual(n['b', 'b'], 4) #WARNING: m[a][b] doesn't work b/c indices not translated! #must access as m[a,b] instead. filterwarnings("ignore", "using a non-integer") try: x = n['a']['b'] except (ValueError, IndexError): pass #should work even if SubAlphabets not the same a = Alphabet('ab') x = Alphabet('xyz') j = a * x m = PairMatrix([1, 2, 3, 4, 5, 6], j) self.assertEqual(m['a', 'x'], 1) self.assertEqual(m['a', 'y'], 2) self.assertEqual(m['a', 'z'], 3) self.assertEqual(m['b', 'x'], 4) self.assertEqual(m['b', 'y'], 5) self.assertEqual(m['b', 'z'], 6) #should work even if SubAlphabets are different types a = Alphabet([1, 2, 3]) b = Alphabet(['abc', 'xyz']) j = a * b m = PairMatrix([1, 2, 3, 4, 5, 6], j) self.assertEqual(m[1, 'abc'], 1) self.assertEqual(m[1, 'xyz'], 2) self.assertEqual(m[2, 'abc'], 3) self.assertEqual(m[2, 'xyz'], 4) self.assertEqual(m[3, 'abc'], 5) self.assertEqual(m[3, 'xyz'], 6) self.assertEqual(list(m[2]), [3, 4]) #gives KeyError if single item not present in first level self.assertRaises(KeyError, m.__getitem__, 'x')
def test_init(self): """PairMatrix init requires data and alphabet""" #should only care about number of elements, not shape p = PairMatrix([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8], RnaPairs) assert p.Alphabet is RnaPairs self.assertEqual(len(p._data), 4) self.assertEqual(len(p._data.flat), 16) self.assertEqual(p._data[0], array([1, 2, 3, 4])) self.assertEqual(p._data[1], array([5, 6, 7, 8]))
def test_ne(self): """PairMatrix test for inequality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) c = PairMatrix([1,2,3,4], p) d = PairMatrix([1,2,3,4], p) assert a != c assert a == b assert c == d #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x*x y = PairMatrix([1,2,3,4], x) assert y == c #should check all elements, not just first c = PairMatrix([1,1,1,1], p) d = PairMatrix([1,1,1,4], p) assert c != d
def test_eq(self): """Pairmatrix test for equality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) assert a is not b self.assertEqual(a, b) c = PairMatrix([1,2,3,4], p) d = PairMatrix([1,2,3,4], p) assert c is not d self.assertEqual(c, d) self.assertNotEqual(a, c) #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x*x y = PairMatrix([1,2,3,4], x) self.assertEqual(y, c) #should check all elements, not just first c = PairMatrix([1,1,1,1], p) d = PairMatrix([1,1,1,4], p) assert c is not d self.assertNotEqual(c, d)
def test_ne(self): """PairMatrix test for inequality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) c = PairMatrix([1, 2, 3, 4], p) d = PairMatrix([1, 2, 3, 4], p) assert a != c assert a == b assert c == d #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x * x y = PairMatrix([1, 2, 3, 4], x) assert y == c #should check all elements, not just first c = PairMatrix([1, 1, 1, 1], p) d = PairMatrix([1, 1, 1, 4], p) assert c != d
def test_eq(self): """Pairmatrix test for equality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) assert a is not b self.assertEqual(a, b) c = PairMatrix([1, 2, 3, 4], p) d = PairMatrix([1, 2, 3, 4], p) assert c is not d self.assertEqual(c, d) self.assertNotEqual(a, c) #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x * x y = PairMatrix([1, 2, 3, 4], x) self.assertEqual(y, c) #should check all elements, not just first c = PairMatrix([1, 1, 1, 1], p) d = PairMatrix([1, 1, 1, 4], p) assert c is not d self.assertNotEqual(c, d)
def test_empty(self): """PairMatrix empty classmethod should produce correct class""" p = PairMatrix.empty(self.ab_pairs) self.assertEqual(p._data.flat, array([0,0,0,0])) self.assertEqual(p._data, array([[0,0],[0,0]])) self.assertEqual(p._data.shape, (2,2))
def setUp(self): """Define standard alphabet and matrices for tests.""" self.ab = Alphabet('ab') self.ab_pairs = self.ab*self.ab self.empty = PairMatrix([0,0,0,0], self.ab_pairs) self.named = PairMatrix([[1,2],[3,4]], self.ab_pairs, 'name')
class PairMatrixTests(TestCase): """Tests of the PairMatrix base class.""" def setUp(self): """Define standard alphabet and matrices for tests.""" self.ab = Alphabet('ab') self.ab_pairs = self.ab*self.ab self.empty = PairMatrix([0,0,0,0], self.ab_pairs) self.named = PairMatrix([[1,2],[3,4]], self.ab_pairs, 'name') def test_init(self): """PairMatrix init requires data and alphabet""" #should only care about number of elements, not shape p = PairMatrix([1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8], RnaPairs) assert p.Alphabet is RnaPairs self.assertEqual(len(p._data), 4) self.assertEqual(len(p._data.flat), 16) self.assertEqual(p._data[0], array([1,2,3,4])) self.assertEqual(p._data[1], array([5,6,7,8])) def test_init_bad(self): """PairMatrix init should fail if data wrong length""" self.assertRaises(ValueError, PairMatrix, [1,2,3,4], RnaPairs) #should also require alphabet self.assertRaises(TypeError, PairMatrix, [1,2,3,4]) def test_toMatlab(self): """PairMatrix toMatlab should return correct format string""" self.assertEqual(self.empty.toMatlab(), "m=[0.0 0.0;\n0.0 0.0];\n") self.assertEqual(self.named.toMatlab(), \ "name=[1.0 2.0;\n3.0 4.0];\n") def test_str(self): """PairMatrix __str__ should return string correpsonding to data""" self.assertEqual(str(self.named), str(self.named._data)) def test_repr(self): """PairMatrix __repr__ should return reconstructable string""" self.assertEqual(repr(self.named), \ 'PairMatrix('+ repr(self.named._data) + ',' +\ repr(self.ab_pairs)+",'name')") def test_getitem(self): """PairMatrix __getitem__ should translate indices and get from array""" n = self.named self.assertEqual(n['a'], array([1,2])) self.assertEqual(n['b'], array([3,4])) self.assertEqual(n['a','a'], 1) self.assertEqual(n['a','b'], 2) self.assertEqual(n['b','a'], 3) self.assertEqual(n['b','b'], 4) #WARNING: m[a][b] doesn't work b/c indices not translated! #must access as m[a,b] instead. try: x = n['a']['b'] except ValueError: pass #should work even if SubAlphabets not the same a = Alphabet('ab') x = Alphabet('xyz') j = a * x m = PairMatrix([1,2,3,4,5,6], j) self.assertEqual(m['a','x'], 1) self.assertEqual(m['a','y'], 2) self.assertEqual(m['a','z'], 3) self.assertEqual(m['b','x'], 4) self.assertEqual(m['b','y'], 5) self.assertEqual(m['b','z'], 6) #should work even if SubAlphabets are different types a = Alphabet([1,2,3]) b = Alphabet(['abc', 'xyz']) j = a * b m = PairMatrix([1,2,3,4,5,6], j) self.assertEqual(m[1,'abc'], 1) self.assertEqual(m[1,'xyz'], 2) self.assertEqual(m[2,'abc'], 3) self.assertEqual(m[2,'xyz'], 4) self.assertEqual(m[3,'abc'], 5) self.assertEqual(m[3,'xyz'], 6) self.assertEqual(list(m[2]), [3,4]) #gives KeyError if single item not present in first level self.assertRaises(KeyError, m.__getitem__, 'x') def test_empty(self): """PairMatrix empty classmethod should produce correct class""" p = PairMatrix.empty(self.ab_pairs) self.assertEqual(p._data.flat, array([0,0,0,0])) self.assertEqual(p._data, array([[0,0],[0,0]])) self.assertEqual(p._data.shape, (2,2)) def test_eq(self): """Pairmatrix test for equality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) assert a is not b self.assertEqual(a, b) c = PairMatrix([1,2,3,4], p) d = PairMatrix([1,2,3,4], p) assert c is not d self.assertEqual(c, d) self.assertNotEqual(a, c) #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x*x y = PairMatrix([1,2,3,4], x) self.assertEqual(y, c) #should check all elements, not just first c = PairMatrix([1,1,1,1], p) d = PairMatrix([1,1,1,4], p) assert c is not d self.assertNotEqual(c, d) def test_ne(self): """PairMatrix test for inequality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) c = PairMatrix([1,2,3,4], p) d = PairMatrix([1,2,3,4], p) assert a != c assert a == b assert c == d #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x*x y = PairMatrix([1,2,3,4], x) assert y == c #should check all elements, not just first c = PairMatrix([1,1,1,1], p) d = PairMatrix([1,1,1,4], p) assert c != d def test_iter(self): """PairMatrix __iter__ should iterate over rows.""" p = self.ab_pairs c = PairMatrix([1,2,3,4], p) l = list(c) self.assertEqual(len(l), 2) self.assertEqual(list(l[0]), [1,2]) self.assertEqual(list(l[1]), [3,4]) def test_len(self): """PairMatrix __len__ should return number of rows""" p = self.ab_pairs c = PairMatrix([1,2,3,4], p) self.assertEqual(len(c), 2)
def test_len(self): """PairMatrix __len__ should return number of rows""" p = self.ab_pairs c = PairMatrix([1, 2, 3, 4], p) self.assertEqual(len(c), 2)
def test_empty(self): """PairMatrix empty classmethod should produce correct class""" p = PairMatrix.empty(self.ab_pairs) self.assertEqual(p._data.flat, array([0, 0, 0, 0])) self.assertEqual(p._data, array([[0, 0], [0, 0]])) self.assertEqual(p._data.shape, (2, 2))
def setUp(self): """Define standard alphabet and matrices for tests.""" self.ab = Alphabet('ab') self.ab_pairs = self.ab * self.ab self.empty = PairMatrix([0, 0, 0, 0], self.ab_pairs) self.named = PairMatrix([[1, 2], [3, 4]], self.ab_pairs, 'name')
class PairMatrixTests(TestCase): """Tests of the PairMatrix base class.""" def setUp(self): """Define standard alphabet and matrices for tests.""" self.ab = Alphabet('ab') self.ab_pairs = self.ab * self.ab self.empty = PairMatrix([0, 0, 0, 0], self.ab_pairs) self.named = PairMatrix([[1, 2], [3, 4]], self.ab_pairs, 'name') def test_init(self): """PairMatrix init requires data and alphabet""" #should only care about number of elements, not shape p = PairMatrix([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8], RnaPairs) assert p.Alphabet is RnaPairs self.assertEqual(len(p._data), 4) self.assertEqual(len(p._data.flat), 16) self.assertEqual(p._data[0], array([1, 2, 3, 4])) self.assertEqual(p._data[1], array([5, 6, 7, 8])) def test_init_bad(self): """PairMatrix init should fail if data wrong length""" self.assertRaises(ValueError, PairMatrix, [1, 2, 3, 4], RnaPairs) #should also require alphabet self.assertRaises(TypeError, PairMatrix, [1, 2, 3, 4]) def test_toMatlab(self): """PairMatrix toMatlab should return correct format string""" self.assertEqual(self.empty.toMatlab(), "m=[0.0 0.0;\n0.0 0.0];\n") self.assertEqual(self.named.toMatlab(), \ "name=[1.0 2.0;\n3.0 4.0];\n") def test_str(self): """PairMatrix __str__ should return string correpsonding to data""" self.assertEqual(str(self.named), str(self.named._data)) def test_repr(self): """PairMatrix __repr__ should return reconstructable string""" self.assertEqual(repr(self.named), \ 'PairMatrix('+ repr(self.named._data) + ',' +\ repr(self.ab_pairs)+",'name')") def test_getitem(self): """PairMatrix __getitem__ should translate indices and get from array""" n = self.named self.assertEqual(n['a'], array([1, 2])) self.assertEqual(n['b'], array([3, 4])) self.assertEqual(n['a', 'a'], 1) self.assertEqual(n['a', 'b'], 2) self.assertEqual(n['b', 'a'], 3) self.assertEqual(n['b', 'b'], 4) #WARNING: m[a][b] doesn't work b/c indices not translated! #must access as m[a,b] instead. filterwarnings("ignore", "using a non-integer") try: x = n['a']['b'] except (ValueError, IndexError): pass #should work even if SubAlphabets not the same a = Alphabet('ab') x = Alphabet('xyz') j = a * x m = PairMatrix([1, 2, 3, 4, 5, 6], j) self.assertEqual(m['a', 'x'], 1) self.assertEqual(m['a', 'y'], 2) self.assertEqual(m['a', 'z'], 3) self.assertEqual(m['b', 'x'], 4) self.assertEqual(m['b', 'y'], 5) self.assertEqual(m['b', 'z'], 6) #should work even if SubAlphabets are different types a = Alphabet([1, 2, 3]) b = Alphabet(['abc', 'xyz']) j = a * b m = PairMatrix([1, 2, 3, 4, 5, 6], j) self.assertEqual(m[1, 'abc'], 1) self.assertEqual(m[1, 'xyz'], 2) self.assertEqual(m[2, 'abc'], 3) self.assertEqual(m[2, 'xyz'], 4) self.assertEqual(m[3, 'abc'], 5) self.assertEqual(m[3, 'xyz'], 6) self.assertEqual(list(m[2]), [3, 4]) #gives KeyError if single item not present in first level self.assertRaises(KeyError, m.__getitem__, 'x') def test_empty(self): """PairMatrix empty classmethod should produce correct class""" p = PairMatrix.empty(self.ab_pairs) self.assertEqual(p._data.flat, array([0, 0, 0, 0])) self.assertEqual(p._data, array([[0, 0], [0, 0]])) self.assertEqual(p._data.shape, (2, 2)) def test_eq(self): """Pairmatrix test for equality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) assert a is not b self.assertEqual(a, b) c = PairMatrix([1, 2, 3, 4], p) d = PairMatrix([1, 2, 3, 4], p) assert c is not d self.assertEqual(c, d) self.assertNotEqual(a, c) #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x * x y = PairMatrix([1, 2, 3, 4], x) self.assertEqual(y, c) #should check all elements, not just first c = PairMatrix([1, 1, 1, 1], p) d = PairMatrix([1, 1, 1, 4], p) assert c is not d self.assertNotEqual(c, d) def test_ne(self): """PairMatrix test for inequality should check all elements""" p = self.ab_pairs a = PairMatrix.empty(p) b = PairMatrix.empty(p) c = PairMatrix([1, 2, 3, 4], p) d = PairMatrix([1, 2, 3, 4], p) assert a != c assert a == b assert c == d #Note: still compare equal if alphabets are different x = Alphabet('xy') x = x * x y = PairMatrix([1, 2, 3, 4], x) assert y == c #should check all elements, not just first c = PairMatrix([1, 1, 1, 1], p) d = PairMatrix([1, 1, 1, 4], p) assert c != d def test_iter(self): """PairMatrix __iter__ should iterate over rows.""" p = self.ab_pairs c = PairMatrix([1, 2, 3, 4], p) l = list(c) self.assertEqual(len(l), 2) self.assertEqual(list(l[0]), [1, 2]) self.assertEqual(list(l[1]), [3, 4]) def test_len(self): """PairMatrix __len__ should return number of rows""" p = self.ab_pairs c = PairMatrix([1, 2, 3, 4], p) self.assertEqual(len(c), 2)