taking a 2D uneven surface slice
I'm trying to do something that at first glance I think should be simple but I can't quite figure out how to do it. The problem is as follows: I have a 3D grid Values[Nx, Ny, Nz] I want to slice Values at a 2D surface in the Z dimension specified by Z_index[Nx, Ny] and return a 2D slice[Nx, Ny]. It is not as simple as Values[:,:,Z_index]. I tried this:
values.shape (4, 5, 6) coords.shape (4, 5) slice = values[:,:,coords] slice.shape (4, 5, 4, 5) slice = np.take(values, coords, axis=2) slice.shape (4, 5, 4, 5)
Obviously I could create an empty 2D slice and then fill it by using np.ndenumerate to fill it point by point by selecting values[i, j, Z_index[i, j]]. This just seems too inefficient and not very pythonic.
Hi Bryan: On Tue, Apr 16, 2013 at 1:21 PM, Bryan Woods <bwoods@aer.com> wrote:
I'm trying to do something that at first glance I think should be simple but I can't quite figure out how to do it. The problem is as follows:
I have a 3D grid Values[Nx, Ny, Nz]
I want to slice Values at a 2D surface in the Z dimension specified by Z_index[Nx, Ny] and return a 2D slice[Nx, Ny].
It is not as simple as Values[:,:,Z_index].
I tried this:
values.shape (4, 5, 6) coords.shape (4, 5) slice = values[:,:,coords] slice.shape (4, 5, 4, 5) slice = np.take(values, coords, axis=2) slice.shape (4, 5, 4, 5)
Obviously I could create an empty 2D slice and then fill it by using np.ndenumerate to fill it point by point by selecting values[i, j, Z_index[i, j]]. This just seems too inefficient and not very pythonic.
The following should work:
values.shape (4,5,6) coords.shape (4,5) values[np.arange(values.shape[0])[:,None], ... np.arange(values.shape[1])[None,:], ... coords].shape (4, 5)
Essentially we extract the values we want by values[I,J,K] where the indices I, J and K are each of shape (4,5) [or broadcast-able to that shape].
A slightly different way to look at it (I don't think it is exactly the same problem, but the description reminded me of it): http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html (and I think there are some things that can be done to make that faster, but I don't recall it right now) Ben Root On Tue, Apr 16, 2013 at 4:35 PM, Bradley M. Froehle <brad.froehle@gmail.com> wrote:
Hi Bryan:
On Tue, Apr 16, 2013 at 1:21 PM, Bryan Woods <bwoods@aer.com> wrote:
I'm trying to do something that at first glance I think should be simple but I can't quite figure out how to do it. The problem is as follows:
I have a 3D grid Values[Nx, Ny, Nz]
I want to slice Values at a 2D surface in the Z dimension specified by Z_index[Nx, Ny] and return a 2D slice[Nx, Ny].
It is not as simple as Values[:,:,Z_index].
I tried this:
values.shape (4, 5, 6) coords.shape (4, 5) slice = values[:,:,coords] slice.shape (4, 5, 4, 5) slice = np.take(values, coords, axis=2) slice.shape (4, 5, 4, 5)
Obviously I could create an empty 2D slice and then fill it by using np.ndenumerate to fill it point by point by selecting values[i, j, Z_index[i, j]]. This just seems too inefficient and not very pythonic.
The following should work:
values.shape (4,5,6) coords.shape (4,5) values[np.arange(values.shape[0])[:,None], ... np.arange(values.shape[1])[None,:], ... coords].shape (4, 5)
Essentially we extract the values we want by values[I,J,K] where the indices I, J and K are each of shape (4,5) [or broadcast-able to that shape].
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
I am sorry, I meant to post this in a different thread... On Wed, Dec 3, 2014 at 8:32 PM, Benjamin Root <ben.root@ou.edu> wrote:
A slightly different way to look at it (I don't think it is exactly the same problem, but the description reminded me of it):
http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html
(and I think there are some things that can be done to make that faster, but I don't recall it right now)
Ben Root
On Tue, Apr 16, 2013 at 4:35 PM, Bradley M. Froehle < brad.froehle@gmail.com> wrote:
Hi Bryan:
On Tue, Apr 16, 2013 at 1:21 PM, Bryan Woods <bwoods@aer.com> wrote:
I'm trying to do something that at first glance I think should be simple but I can't quite figure out how to do it. The problem is as follows:
I have a 3D grid Values[Nx, Ny, Nz]
I want to slice Values at a 2D surface in the Z dimension specified by Z_index[Nx, Ny] and return a 2D slice[Nx, Ny].
It is not as simple as Values[:,:,Z_index].
I tried this:
values.shape (4, 5, 6) coords.shape (4, 5) slice = values[:,:,coords] slice.shape (4, 5, 4, 5) slice = np.take(values, coords, axis=2) slice.shape (4, 5, 4, 5)
Obviously I could create an empty 2D slice and then fill it by using np.ndenumerate to fill it point by point by selecting values[i, j, Z_index[i, j]]. This just seems too inefficient and not very pythonic.
The following should work:
values.shape (4,5,6) coords.shape (4,5) values[np.arange(values.shape[0])[:,None], ... np.arange(values.shape[1])[None,:], ... coords].shape (4, 5)
Essentially we extract the values we want by values[I,J,K] where the indices I, J and K are each of shape (4,5) [or broadcast-able to that shape].
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Benjamin Root
-
Bradley M. Froehle
-
Bryan Woods