def test_scalar(self): # indexing with scalar A = self.A X = [A, A, A] S = slice(None, None, None) # index rows (single scalar index for all list items) Y = vfuncs._idx(X, ([0], S)) for y in Y: self.assertTrue(y.tolist() == A[0].tolist()) # index rows (separate scalar index per list items) Y = vfuncs._idx(X, ([0, 0, 0], S)) for y in Y: self.assertTrue(y.tolist() == A[0].tolist()) # index columns (single scalar index for all list items) Y = vfuncs._idx(X, (S, [0])) for y in Y: self.assertTrue(y.tolist() == A[:, 0].tolist()) # index columns (separate scalar index per list items) Y = vfuncs._idx(X, (S, [0, 0, 0])) for y in Y: self.assertTrue(y.tolist() == A[:, 0].tolist())
def test_mixed(self): A = self.A S = self.S J = [ self.B, [1, 4, 5], [True, 2.0, False, 6], np.array([2, 3, 9], dtype=np.int32), np.array([-3, 9]) ] J_1 = [ self.B, [1, 4, 5], [1, 2, 0, 6], np.array([2, 3, 9], dtype=np.int32), np.array([-3, 9]) ] J_2 = [ np.r_[self.B, False], [1, 4, 5], [1, 2, 0, 6], np.array([2, 3, 9], dtype=np.int32), np.array([-3, 9]) ] X = [A, A, A, A, A] Y = vfuncs._idx(X, (J, S)) for i, y in enumerate(Y): self.assertTrue(y.tolist() == A[J_1[i]].tolist()) Y = vfuncs._idx(X, (S, J)) for i, y in enumerate(Y): self.assertTrue(y.tolist() == A[:, J_2[i]].tolist())
def test_slicing(self): A = self.A J = self.J S = self.S X = [A, A, A] # out of bound slicing results in empty array S1 = slice(12, None, None) Y = vfuncs._idx(X, (S1, S)) for y in Y: self.assertTrue(y.shape == A[S1].shape and y.tolist() == A[S1].tolist()) Y = vfuncs._idx(X, (S, S1)) for y in Y: self.assertTrue(y.shape == A[:, S1].shape and y.tolist() == A[:, S1].tolist()) # slices with negative indices S2 = slice(-2, None, None) Y = vfuncs._idx(X, (S2, S)) for y in Y: self.assertTrue(y.tolist() == A[S2].tolist()) Y = vfuncs._idx(X, (S, S2)) for y in Y: self.assertTrue(y.tolist() == A[:, S2].tolist()) # simultaneous row and column slicing Y = vfuncs._idx(X, (slice(None, None, 2), slice(None, None, 3))) for y in Y: self.assertTrue(y.tolist() == A[::2, ::3].tolist())
def test_pylist(self): # indexing with python list of indices A = self.A S = self.S X = [A, A, A] # indexing with list containing non-int()-able item # should raise exception class Foo: pass J = [1, Foo(), 2] with self.assertRaises(TypeError): vfuncs._idx(X, ([J], S))
def test_boolean(self): # indexing with array of booleans A = self.A S = self.S B = self.B B2 = np.r_[B, False] X = [A, A, A] Y = vfuncs._idx(X, ([B], S)) for y in Y: self.assertTrue(y.tolist() == A[B].tolist()) Y = vfuncs._idx(X, (S, [B2])) for y in Y: self.assertTrue(y.tolist() == A[:, B2].tolist()) Y = vfuncs._idx(X, ([B, B, B], S)) for y in Y: self.assertTrue(y.tolist() == A[B, :].tolist()) Y = vfuncs._idx(X, (S, [B2, B2, B2])) for y in Y: self.assertTrue(y.tolist() == A[:, B2].tolist()) # length of boolean array allowed to exceed that of "indexee" # as long as exceeding entries are all False Y = vfuncs._idx(X, ([B[:3]], S)) for y in Y: self.assertTrue(y.tolist() == A[:3, :][B[:3], :].tolist()) B2 = np.hstack((B, np.array([False, False, False]))) Y = vfuncs._idx(X, ([B2], S)) for y in Y: self.assertTrue(y.tolist() == A[B, :].tolist())
def test_intarray(self): A = self.A S = self.S X = [A, A, A] # indexing with array of integers # negative indices # out of bound indices should raise exception J = np.array([42]) with self.assertRaises(IndexError): vfuncs._idx(X, ([J], S)) with self.assertRaises(IndexError): vfuncs._idx(X, (S, [J])) J = np.array([-13]) with self.assertRaises(IndexError): vfuncs._idx(X, ([J], S)) with self.assertRaises(IndexError): vfuncs._idx(X, (S, [J]))
def test_check_inputs(self): A = self.A J = self.J S = self.S X = [A, A, A] # simultaneous indexing of rows and columns disallowed with self.assertRaises(TypeError): vfuncs._idx(X, ([J], [J])) # indexing just rows should not give error Y = vfuncs._idx(X, ([J], S)) for y in Y: self.assertTrue(y.tolist() == A[J].tolist()) # indexing just columns should not give error Y = vfuncs._idx(X, (S, [J])) for y in Y: self.assertTrue(y.tolist() == A[:, J].tolist()) # indexing X=[A, A, A] with [J, J] should give error with self.assertRaises(ValueError): vfuncs._idx(X, ([J, J], S)) # index [A] with [J] Y = vfuncs._idx([A], ([J], S)) for y in Y: self.assertTrue(y.tolist() == A[J, :].tolist()) Y = vfuncs._idx([A], (S, [J])) for y in Y: self.assertTrue(y.tolist() == A[:, J].tolist()) # index [A] with [J, J, J] Y = vfuncs._idx([A], ([J, J, J], S)) for y in Y: self.assertTrue(y.tolist() == A[J, :].tolist()) Y = vfuncs._idx([A], (S, [J, J, J])) for y in Y: self.assertTrue(y.tolist() == A[:, J].tolist()) # index [A, A, A] with [J] Y = vfuncs._idx([A, A, A], ([J], S)) for y in Y: self.assertTrue(y.tolist() == A[J, :].tolist()) Y = vfuncs._idx([A, A, A], (S, [J])) for y in Y: self.assertTrue(y.tolist() == A[:, J].tolist()) # index [A, A, A] with [J, J, J] Y = vfuncs._idx([A, A, A], ([J, J, J], S)) for y in Y: self.assertTrue(y.tolist() == A[J, :].tolist()) Y = vfuncs._idx([A, A, A], (S, [J, J, J])) for y in Y: self.assertTrue(y.tolist() == A[:, J].tolist()) # indexing [] with [J] should give error with self.assertRaises(ValueError): vfuncs._idx([], ([J], S)) with self.assertRaises(ValueError): vfuncs._idx([], (S, [J])) # indexing [] with [J, J] should give error with self.assertRaises(ValueError): vfuncs._idx([], ([J, J], S)) with self.assertRaises(ValueError): vfuncs._idx([], (S, [J, J])) # indexing [A] with [] should give error with self.assertRaises(ValueError): vfuncs._idx([A], ([], S)) with self.assertRaises(ValueError): vfuncs._idx([A], (S, [])) # indexing [A, A] with [] should give error with self.assertRaises(ValueError): vfuncs._idx([A, A], ([], S)) with self.assertRaises(ValueError): vfuncs._idx([A, A], (S, []))