Fastest way to crop a 3D array inside a 3D array with Python
up vote
2
down vote
favorite
I have a 3D array and a list of 3D indexes. My aim is to isolate a small 3D volume of a specific size (3x3x3 or 5x5x5 or whatever) for every index (with the index lying in the middle of the volume).
At the moment, I do this:
1) Group five 2D arrays (with the interested one in the middle, following the indexes). So having a 5xNxN array.
2) For a 5x5x5 volume, for each 2D array (0,N,N; 1,N,N..etc) of my 5xNxN array, I crop a 5x5 array around the same index.
3) Stack these five 5x5 2D arrays to obtain my small 3D volume.
Is there a fastest way to do this job?
Here an explanatory code:
arr = np.zeros((7,7,7)) #Just a 3D array
ind = [3, 3, 3] #My index
for el in range(arr.shape[0]):
if el==ind[0]:
group = arr[el-2:el+3] #it isolates a 3D volume with arr[ind[0]] in the middle
volume_3d =
for i in group:
volume_2d = i[ind[1]-2:ind[1]+3, ind[2]-2:ind[2]+3]
volume_3d.append (volume_2d) #it builds the 3D volume
Thanks
python arrays numpy
add a comment |
up vote
2
down vote
favorite
I have a 3D array and a list of 3D indexes. My aim is to isolate a small 3D volume of a specific size (3x3x3 or 5x5x5 or whatever) for every index (with the index lying in the middle of the volume).
At the moment, I do this:
1) Group five 2D arrays (with the interested one in the middle, following the indexes). So having a 5xNxN array.
2) For a 5x5x5 volume, for each 2D array (0,N,N; 1,N,N..etc) of my 5xNxN array, I crop a 5x5 array around the same index.
3) Stack these five 5x5 2D arrays to obtain my small 3D volume.
Is there a fastest way to do this job?
Here an explanatory code:
arr = np.zeros((7,7,7)) #Just a 3D array
ind = [3, 3, 3] #My index
for el in range(arr.shape[0]):
if el==ind[0]:
group = arr[el-2:el+3] #it isolates a 3D volume with arr[ind[0]] in the middle
volume_3d =
for i in group:
volume_2d = i[ind[1]-2:ind[1]+3, ind[2]-2:ind[2]+3]
volume_3d.append (volume_2d) #it builds the 3D volume
Thanks
python arrays numpy
Your outer loop seems useless. You could just start withel = ind[0]
and execute everything inside theif
.
– coldspeed
Nov 10 at 4:05
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a 3D array and a list of 3D indexes. My aim is to isolate a small 3D volume of a specific size (3x3x3 or 5x5x5 or whatever) for every index (with the index lying in the middle of the volume).
At the moment, I do this:
1) Group five 2D arrays (with the interested one in the middle, following the indexes). So having a 5xNxN array.
2) For a 5x5x5 volume, for each 2D array (0,N,N; 1,N,N..etc) of my 5xNxN array, I crop a 5x5 array around the same index.
3) Stack these five 5x5 2D arrays to obtain my small 3D volume.
Is there a fastest way to do this job?
Here an explanatory code:
arr = np.zeros((7,7,7)) #Just a 3D array
ind = [3, 3, 3] #My index
for el in range(arr.shape[0]):
if el==ind[0]:
group = arr[el-2:el+3] #it isolates a 3D volume with arr[ind[0]] in the middle
volume_3d =
for i in group:
volume_2d = i[ind[1]-2:ind[1]+3, ind[2]-2:ind[2]+3]
volume_3d.append (volume_2d) #it builds the 3D volume
Thanks
python arrays numpy
I have a 3D array and a list of 3D indexes. My aim is to isolate a small 3D volume of a specific size (3x3x3 or 5x5x5 or whatever) for every index (with the index lying in the middle of the volume).
At the moment, I do this:
1) Group five 2D arrays (with the interested one in the middle, following the indexes). So having a 5xNxN array.
2) For a 5x5x5 volume, for each 2D array (0,N,N; 1,N,N..etc) of my 5xNxN array, I crop a 5x5 array around the same index.
3) Stack these five 5x5 2D arrays to obtain my small 3D volume.
Is there a fastest way to do this job?
Here an explanatory code:
arr = np.zeros((7,7,7)) #Just a 3D array
ind = [3, 3, 3] #My index
for el in range(arr.shape[0]):
if el==ind[0]:
group = arr[el-2:el+3] #it isolates a 3D volume with arr[ind[0]] in the middle
volume_3d =
for i in group:
volume_2d = i[ind[1]-2:ind[1]+3, ind[2]-2:ind[2]+3]
volume_3d.append (volume_2d) #it builds the 3D volume
Thanks
python arrays numpy
python arrays numpy
edited Nov 10 at 4:02
coldspeed
111k17101170
111k17101170
asked Nov 10 at 3:40
Sav
357
357
Your outer loop seems useless. You could just start withel = ind[0]
and execute everything inside theif
.
– coldspeed
Nov 10 at 4:05
add a comment |
Your outer loop seems useless. You could just start withel = ind[0]
and execute everything inside theif
.
– coldspeed
Nov 10 at 4:05
Your outer loop seems useless. You could just start with
el = ind[0]
and execute everything inside the if
.– coldspeed
Nov 10 at 4:05
Your outer loop seems useless. You could just start with
el = ind[0]
and execute everything inside the if
.– coldspeed
Nov 10 at 4:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Numpy supports slicing like this quite easily:
dim = 5
x = dim // 2
i,j,k = ind
volume_3d = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)].copy()
# Your implementation.
dim = 5
x = dim // 2
arr = np.random.randn(7, 7, 7)
el = ind[0]
group = arr[el-x:el+(dim-x)]
volume_3d =
for i in group:
volume_2d = i[ind[1]-x:ind[1]+(dim-x), ind[2]-x:ind[2]+(dim-x)]
volume_3d.append (volume_2d)
# Proposed in this post.
i,j,k = ind
volume_3d_2 = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)]
print(np.array_equal(volume_3d, volume_3d_2))
True
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Numpy supports slicing like this quite easily:
dim = 5
x = dim // 2
i,j,k = ind
volume_3d = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)].copy()
# Your implementation.
dim = 5
x = dim // 2
arr = np.random.randn(7, 7, 7)
el = ind[0]
group = arr[el-x:el+(dim-x)]
volume_3d =
for i in group:
volume_2d = i[ind[1]-x:ind[1]+(dim-x), ind[2]-x:ind[2]+(dim-x)]
volume_3d.append (volume_2d)
# Proposed in this post.
i,j,k = ind
volume_3d_2 = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)]
print(np.array_equal(volume_3d, volume_3d_2))
True
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
add a comment |
up vote
1
down vote
accepted
Numpy supports slicing like this quite easily:
dim = 5
x = dim // 2
i,j,k = ind
volume_3d = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)].copy()
# Your implementation.
dim = 5
x = dim // 2
arr = np.random.randn(7, 7, 7)
el = ind[0]
group = arr[el-x:el+(dim-x)]
volume_3d =
for i in group:
volume_2d = i[ind[1]-x:ind[1]+(dim-x), ind[2]-x:ind[2]+(dim-x)]
volume_3d.append (volume_2d)
# Proposed in this post.
i,j,k = ind
volume_3d_2 = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)]
print(np.array_equal(volume_3d, volume_3d_2))
True
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Numpy supports slicing like this quite easily:
dim = 5
x = dim // 2
i,j,k = ind
volume_3d = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)].copy()
# Your implementation.
dim = 5
x = dim // 2
arr = np.random.randn(7, 7, 7)
el = ind[0]
group = arr[el-x:el+(dim-x)]
volume_3d =
for i in group:
volume_2d = i[ind[1]-x:ind[1]+(dim-x), ind[2]-x:ind[2]+(dim-x)]
volume_3d.append (volume_2d)
# Proposed in this post.
i,j,k = ind
volume_3d_2 = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)]
print(np.array_equal(volume_3d, volume_3d_2))
True
Numpy supports slicing like this quite easily:
dim = 5
x = dim // 2
i,j,k = ind
volume_3d = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)].copy()
# Your implementation.
dim = 5
x = dim // 2
arr = np.random.randn(7, 7, 7)
el = ind[0]
group = arr[el-x:el+(dim-x)]
volume_3d =
for i in group:
volume_2d = i[ind[1]-x:ind[1]+(dim-x), ind[2]-x:ind[2]+(dim-x)]
volume_3d.append (volume_2d)
# Proposed in this post.
i,j,k = ind
volume_3d_2 = arr[i-x:i+(dim-x), j-x:j+(dim-x), k-x:k+(dim-x)]
print(np.array_equal(volume_3d, volume_3d_2))
True
edited Nov 10 at 4:05
answered Nov 10 at 3:54
coldspeed
111k17101170
111k17101170
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
add a comment |
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
It works perfectly. Thanks
– Sav
Nov 12 at 2:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235813%2ffastest-way-to-crop-a-3d-array-inside-a-3d-array-with-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your outer loop seems useless. You could just start with
el = ind[0]
and execute everything inside theif
.– coldspeed
Nov 10 at 4:05