Skip to content

Commit 0c5ca24

Browse files
authored
Merge pull request #2 from joulebit/overhaul
6% faster with better cropped cube
2 parents 2059b9f + 7dd61a5 commit 0c5ca24

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

libraries/resizing.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ def crop_cube(cube: np.ndarray) -> np.ndarray:
1515
np.array: Cropped 3D Numpy byte array equivalent to cube, but with no zero padding
1616
1717
"""
18-
for i in range(cube.ndim):
19-
cube = np.swapaxes(cube, 0, i)
20-
nonzero_indices = np.any(cube != 0, axis=tuple(range(1, cube.ndim)))
21-
cube = cube[nonzero_indices]
22-
cube = np.swapaxes(cube, 0, i)
23-
return cube
18+
nonzero_indices = np.where(cube != 0)
19+
cropped_cube = cube[np.min(nonzero_indices[0]):np.max(nonzero_indices[0]) + 1,
20+
np.min(nonzero_indices[1]):np.max(nonzero_indices[1]) + 1,
21+
np.min(nonzero_indices[2]):np.max(nonzero_indices[2]) + 1]
22+
return cropped_cube
2423

2524

2625
def expand_cube(cube: np.ndarray) -> Generator[np.ndarray, None, None]:

0 commit comments

Comments
 (0)