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










share|improve this question























  • 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














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










share|improve this question























  • 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












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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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















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












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





share|improve this answer






















  • It works perfectly. Thanks
    – Sav
    Nov 12 at 2:06










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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





share|improve this answer






















  • It works perfectly. Thanks
    – Sav
    Nov 12 at 2:06














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





share|improve this answer






















  • It works perfectly. Thanks
    – Sav
    Nov 12 at 2:06












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20