Skip to content

Commit 5055391

Browse files
committed
Simplified map_window logic.
1 parent c120ad9 commit 5055391

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

spectral/algorithms/spatial.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_window_bounds(nrows, ncols, height, width, i, j):
8181
from the center of the widow.
8282
8383
For an alternate function that clips window pixels near the border of the
84-
image, see `get_clipped_window_bounds`.
84+
image, see `get_window_bounds_clipped`.
8585
'''
8686
if height > nrows or width > ncols:
8787
raise ValueError('Window size is too large for image dimensions.')
@@ -167,7 +167,7 @@ def get_window_bounds_clipped(nrows, ncols, height, width, i, j):
167167
return (rmin, rmax, cmin, cmax)
168168

169169
def map_window(func, image, window, rslice=(None,), cslice=(None,),
170-
border='shift', dtype=None ):
170+
border='shift', dtype=None):
171171
'''Applies a function over a rolling spatial window.
172172
173173
Arguments:
@@ -265,25 +265,14 @@ def map_window(func, image, window, rslice=(None,), cslice=(None,),
265265
rvals = list(range(*slice(*rslice).indices(nrows)))
266266
cvals = list(range(*slice(*cslice).indices(ncols)))
267267

268-
nrows_out = len(rvals)
269-
ncols_out = len(cvals)
270-
271-
# Call the function once to get output shape and dtype
272-
(r0, r1, c0, c1) = get_window(nrows, ncols, height, width,
273-
rvals[0], cvals[0])
274-
y = func(image[r0:r1, c0:c1], (rvals[0] - r0, cvals[0] - c0))
275-
if dtype is None:
276-
dtype = np.array(y).dtype
277-
out = np.empty((nrows_out, ncols_out) + np.shape(y), dtype=dtype)
278-
279-
for i in range(nrows_out):
280-
for j in range(ncols_out):
281-
(r0, r1, c0, c1) = get_window(nrows, ncols, height, width,
282-
rvals[i], cvals[j])
283-
out[i, j] = func(image[r0:r1, c0:c1],
284-
(rvals[i] - r0, cvals[j] - c0))
285-
return out
286-
268+
def get_val(i, j):
269+
(r0, r1, c0, c1) = get_window(nrows, ncols, height, width, i, j)
270+
return func(image[r0:r1, c0:c1],
271+
(i - r0, j - c0)).astype(dtype)
272+
273+
return np.array([[get_val(r, c) for c in cvals]
274+
for r in rvals]).astype(dtype)
275+
287276
def map_outer_window_stats(func, image, inner, outer, dim_out=1, cov=None,
288277
dtype=None, rslice=(None,), cslice=(None,)):
289278
'''Maps a function accepting `GaussianStats` over a rolling spatial window.

0 commit comments

Comments
 (0)