Beispiel #1
0
 def test_orthogonal_indexer(self):
     x = np.random.randn(10, 11, 12, 13, 14)
     y = np.arange(5)
     I = ReturnItem()
     # orthogonal and numpy indexing should be equivalent, because we only
     # use at most one array and it never in between two slice objects
     # (i.e., we try to avoid numpy's mind-boggling "partial indexing"
     # http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)
     for i in [
             I[:], I[0], I[0, 0], I[:5], I[5:], I[2:5], I[3:-3], I[::-1],
             I[::-2], I[5::-2], I[:3:-2], I[2:5:-1], I[7:3:-2], I[:3, :4],
             I[:3, 0, :4], I[:3, 0, :4, 0], I[y], I[:, y], I[0, y],
             I[:2, :3, y], I[0, y, :, :4, 0]
     ]:
         j = indexing.orthogonal_indexer(i, x.shape)
         self.assertArrayEqual(x[i], x[j])
         self.assertArrayEqual(self.set_to_zero(x, i),
                               self.set_to_zero(x, j))
     # for more complicated cases, check orthogonal indexing is still
     # equivalent to slicing
     z = np.arange(2, 8, 2)
     for i, j, shape in [(I[y, y], I[:5, :5], (5, 5, 12, 13, 14)),
                         (I[y, z], I[:5, 2:8:2], (5, 3, 12, 13, 14)),
                         (I[0, y, y], I[0, :5, :5], (5, 5, 13, 14)),
                         (I[y, 0, z], I[:5, 0, 2:8:2], (5, 3, 13, 14)),
                         (I[y, :, z], I[:5, :, 2:8:2], (5, 11, 3, 13, 14)),
                         (I[0, :, z], I[0, :, 2:8:2], (11, 3, 13, 14)),
                         (I[0, :2, y, y, 0], I[0, :2, :5, :5,
                                               0], (2, 5, 5)),
                         (I[0, :, y, :, 0], I[0, :, :5, :, 0], (11, 5, 13)),
                         (I[:, :, y, :, 0], I[:, :, :5, :,
                                              0], (10, 11, 5, 13)),
                         (I[:, :, y, z, :], I[:, :, :5,
                                              2:8:2], (10, 11, 5, 3, 14))]:
         k = indexing.orthogonal_indexer(i, x.shape)
         self.assertEqual(shape, x[k].shape)
         self.assertArrayEqual(x[j], x[k])
         self.assertArrayEqual(self.set_to_zero(x, j),
                               self.set_to_zero(x, k))
     # standard numpy (non-orthogonal) indexing doesn't work anymore
     with self.assertRaisesRegexp(ValueError, 'only supports 1d'):
         indexing.orthogonal_indexer(x > 0, x.shape)
     with self.assertRaisesRegexp(ValueError, 'invalid subkey'):
         print(indexing.orthogonal_indexer((1.5 * y, 1.5 * y), x.shape))
Beispiel #2
0
 def test_orthogonal_indexer(self):
     x = np.random.randn(10, 11, 12, 13, 14)
     y = np.arange(5)
     I = ReturnItem()
     # orthogonal and numpy indexing should be equivalent, because we only
     # use at most one array and it never in between two slice objects
     # (i.e., we try to avoid numpy's mind-boggling "partial indexing"
     # http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)
     for i in [I[:], I[0], I[0, 0], I[:5], I[5:], I[2:5], I[3:-3], I[::-1],
               I[::-2], I[5::-2], I[:3:-2], I[2:5:-1], I[7:3:-2], I[:3, :4],
               I[:3, 0, :4], I[:3, 0, :4, 0], I[y], I[:, y], I[0, y],
               I[:2, :3, y], I[0, y, :, :4, 0]]:
         j = indexing.orthogonal_indexer(i, x.shape)
         self.assertArrayEqual(x[i], x[j])
         self.assertArrayEqual(self.set_to_zero(x, i),
                               self.set_to_zero(x, j))
     # for more complicated cases, check orthogonal indexing is still
     # equivalent to slicing
     z = np.arange(2, 8, 2)
     for i, j, shape in [
             (I[y, y], I[:5, :5], (5, 5, 12, 13, 14)),
             (I[y, z], I[:5, 2:8:2], (5, 3, 12, 13, 14)),
             (I[0, y, y], I[0, :5, :5], (5, 5, 13, 14)),
             (I[y, 0, z], I[:5, 0, 2:8:2], (5, 3, 13, 14)),
             (I[y, :, z], I[:5, :, 2:8:2], (5, 11, 3, 13, 14)),
             (I[0, :, z], I[0, :, 2:8:2], (11, 3, 13, 14)),
             (I[0, :2, y, y, 0], I[0, :2, :5, :5, 0], (2, 5, 5)),
             (I[0, :, y, :, 0], I[0, :, :5, :, 0], (11, 5, 13)),
             (I[:, :, y, :, 0], I[:, :, :5, :, 0], (10, 11, 5, 13)),
             (I[:, :, y, z, :], I[:, :, :5, 2:8:2], (10, 11, 5, 3, 14))]:
         k = indexing.orthogonal_indexer(i, x.shape)
         self.assertEqual(shape, x[k].shape)
         self.assertArrayEqual(x[j], x[k])
         self.assertArrayEqual(self.set_to_zero(x, j),
                               self.set_to_zero(x, k))
     # standard numpy (non-orthogonal) indexing doesn't work anymore
     with self.assertRaisesRegexp(ValueError, 'only supports 1d'):
         indexing.orthogonal_indexer(x > 0, x.shape)
     with self.assertRaisesRegexp(ValueError, 'invalid subkey'):
         print(indexing.orthogonal_indexer((1.5 * y, 1.5 * y), x.shape))
Beispiel #3
0
 def __getitem__(self, key):
     return self.array[indexing.orthogonal_indexer(key, self.shape)]
Beispiel #4
0
 def __getitem__(self, key):
     return self.array[indexing.orthogonal_indexer(key, self.shape)]