def __getitem__(self, indices): """Return ``self[indices]``. Parameters ---------- indices : index expression Object determining which parts of the partition to extract. ``None`` (new axis) and empty axes are not supported. Examples -------- >>> intvp = odl.IntervalProd([-1, 1, 4, 2], [3, 6, 5, 7]) >>> grid = odl.RectGrid([-1, 0, 3], [2, 4], [5], [2, 4, 7]) >>> part = odl.RectPartition(intvp, grid) >>> part nonuniform_partition( [-1.0, 0.0, 3.0], [2.0, 4.0], [5.0], [2.0, 4.0, 7.0], min_pt=[-1.0, 1.0, 4.0, 2.0], max_pt=[3.0, 6.0, 5.0, 7.0] ) Take an advanced slice (every second along the first axis, the last in the last axis and everything in between): >>> part[::2, ..., -1] nonuniform_partition( [-1.0, 3.0], [2.0, 4.0], [5.0], [7.0], min_pt=[-1.0, 1.0, 4.0, 5.5], max_pt=[3.0, 6.0, 5.0, 7.0] ) Too few indices are filled up with an ellipsis from the right: >>> part[1] nonuniform_partition( [0.0], [2.0, 4.0], [5.0], [2.0, 4.0, 7.0], min_pt=[-0.5, 1.0, 4.0, 2.0], max_pt=[1.5, 6.0, 5.0, 7.0] ) Colons etc work as expected: >>> part[:] == part True >>> part[:, :, :] == part True >>> part[...] == part True """ # Special case of index list: slice along first axis if isinstance(indices, list): if indices == []: new_min_pt = new_max_pt = [] else: new_min_pt = [self.cell_boundary_vecs[0][:-1][indices][0]] new_max_pt = [self.cell_boundary_vecs[0][1:][indices][-1]] for cvec in self.cell_boundary_vecs[1:]: new_min_pt.append(cvec[0]) new_max_pt.append(cvec[-1]) new_intvp = IntervalProd(new_min_pt, new_max_pt) new_grid = self.grid[indices] return RectPartition(new_intvp, new_grid) indices = normalized_index_expression(indices, self.shape, int_to_slice=True) # Build the new partition new_min_pt, new_max_pt = [], [] for cvec, idx in zip(self.cell_boundary_vecs, indices): # Determine the subinterval min_pt and max_pt vectors. Take the # first min_pt as new min_pt and the last max_pt as new max_pt. sub_min_pt = cvec[:-1][idx] sub_max_pt = cvec[1:][idx] new_min_pt.append(sub_min_pt[0]) new_max_pt.append(sub_max_pt[-1]) new_intvp = IntervalProd(new_min_pt, new_max_pt) new_grid = self.grid[indices] return RectPartition(new_intvp, new_grid)
def __getitem__(self, indices): """Return ``self[indices]``. Parameters ---------- indices : index expression Object determining which parts of the grid to extract. ``None`` (new axis) and empty axes are not supported. Examples -------- Indexing with integers along all axes produces an array (a point): >>> g = RectGrid([-1, 0, 3], [2, 4, 5], [5], [2, 4, 7]) >>> g[0, 0, 0, 0] array([-1., 2., 5., 2.]) Otherwise, a new RectGrid is returned: >>> g[:, 0, 0, 0] RectGrid( [-1., 0., 3.], [ 2.], [ 5.], [ 2.] ) >>> g[0, ..., 1:] RectGrid( [-1.], [ 2., 4., 5.], [ 5.], [ 4., 7.] ) >>> g[::2, ..., ::2] RectGrid( [-1., 3.], [ 2., 4., 5.], [ 5.], [ 2., 7.] ) Too few indices are filled up with an ellipsis from the right: >>> g[0] RectGrid( [-1.], [ 2., 4., 5.], [ 5.], [ 2., 4., 7.] ) >>> g[0] == g[0, :, :, :] == g[0, ...] True """ if isinstance(indices, list): if indices == []: new_coord_vecs = [] else: new_coord_vecs = [self.coord_vectors[0][indices]] new_coord_vecs += self.coord_vectors[1:] return RectGrid(*new_coord_vecs) indices = normalized_index_expression(indices, self.shape, int_to_slice=False) # If all indices are integers, return an array (a point). Otherwise, # create a new grid. if all(np.isscalar(idx) for idx in indices): return np.fromiter( (v[int(idx)] for idx, v in zip(indices, self.coord_vectors)), dtype=float) else: new_coord_vecs = [vec[idx] for idx, vec in zip(indices, self.coord_vectors)] return RectGrid(*new_coord_vecs)
def __getitem__(self, indices): """Return ``self[indices]``. Parameters ---------- indices : index expression Object determining which parts of the partition to extract. ``None`` (new axis) and empty axes are not supported. Examples -------- >>> intvp = odl.IntervalProd([-1, 1, 4, 2], [3, 6, 5, 7]) >>> grid = odl.RectGrid([-1, 0, 3], [2, 4], [5], [2, 4, 7]) >>> part = odl.RectPartition(intvp, grid) >>> part nonuniform_partition( [-1.0, 0.0, 3.0], [2.0, 4.0], [5.0], [2.0, 4.0, 7.0], min_pt=[-1.0, 1.0, 4.0, 2.0], max_pt=[3.0, 6.0, 5.0, 7.0] ) Take an advanced slice (every second along the first axis, the last in the last axis and everything in between): >>> part[::2, ..., -1] nonuniform_partition( [-1.0, 3.0], [2.0, 4.0], [5.0], [7.0], min_pt=[-1.0, 1.0, 4.0, 5.5], max_pt=[3.0, 6.0, 5.0, 7.0] ) Too few indices are filled up with an ellipsis from the right: >>> part[1] nonuniform_partition( [0.0], [2.0, 4.0], [5.0], [2.0, 4.0, 7.0], min_pt=[-0.5, 1.0, 4.0, 2.0], max_pt=[1.5, 6.0, 5.0, 7.0] ) Colons etc work as expected: >>> part[:] == part True >>> part[:, :, :] == part True >>> part[...] == part True """ # Special case of index list: slice along first axis if isinstance(indices, list): new_min_pt = [self.cell_boundary_vecs[0][:-1][indices][0]] new_max_pt = [self.cell_boundary_vecs[0][1:][indices][-1]] for cvec in self.cell_boundary_vecs[1:]: new_min_pt.append(cvec[0]) new_max_pt.append(cvec[-1]) new_intvp = IntervalProd(new_min_pt, new_max_pt) new_grid = self.grid[indices] return RectPartition(new_intvp, new_grid) indices = normalized_index_expression(indices, self.shape, int_to_slice=True) # Build the new partition new_min_pt, new_max_pt = [], [] for cvec, idx in zip(self.cell_boundary_vecs, indices): # Determine the subinterval min_pt and max_pt vectors. Take the # first min_pt as new min_pt and the last max_pt as new max_pt. sub_min_pt = cvec[:-1][idx] sub_max_pt = cvec[1:][idx] new_min_pt.append(sub_min_pt[0]) new_max_pt.append(sub_max_pt[-1]) new_intvp = IntervalProd(new_min_pt, new_max_pt) new_grid = self.grid[indices] return RectPartition(new_intvp, new_grid)
def __getitem__(self, indices): """Return ``self[indices]``. Parameters ---------- indices : index expression Object determining which parts of the grid to extract. ``None`` (new axis) and empty axes are not supported. Examples -------- Indexing with integers along all axes produces an array (a point): >>> g = RectGrid([-1, 0, 3], [2, 4, 5], [5], [2, 4, 7]) >>> g[0, 0, 0, 0] array([-1., 2., 5., 2.]) Otherwise, a new RectGrid is returned: >>> g[:, 0, 0, 0] RectGrid( [-1.0, 0.0, 3.0], [2.0], [5.0], [2.0] ) >>> g[0, ..., 1:] RectGrid( [-1.0], [2.0, 4.0, 5.0], [5.0], [4.0, 7.0] ) >>> g[::2, ..., ::2] RectGrid( [-1.0, 3.0], [2.0, 4.0, 5.0], [5.0], [2.0, 7.0] ) Too few indices are filled up with an ellipsis from the right: >>> g[0] RectGrid( [-1.0], [2.0, 4.0, 5.0], [5.0], [2.0, 4.0, 7.0] ) >>> g[0] == g[0, :, :, :] == g[0, ...] True """ if isinstance(indices, list): new_coord_vecs = [self.coord_vectors[0][indices]] new_coord_vecs += self.coord_vectors[1:] return RectGrid(*new_coord_vecs) indices = normalized_index_expression(indices, self.shape, int_to_slice=False) # If all indices are integers, return an array (a point). Otherwise, # create a new grid. if all(np.isscalar(idx) for idx in indices): return np.fromiter( (v[int(idx)] for idx, v in zip(indices, self.coord_vectors)), dtype=float) else: new_coord_vecs = [vec[idx] for idx, vec in zip(indices, self.coord_vectors)] return RectGrid(*new_coord_vecs)