Skip to content

Commit d33bebc

Browse files
committed
iterator_ij should not receive an image data argument.
1 parent 65ef3f1 commit d33bebc

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

spectral/algorithms/algorithms.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,16 @@ def iterator(image, mask=None, index=None):
149149
return ImageIterator(image)
150150

151151

152-
def iterator_ij(image, mask=None, index=None):
152+
def iterator_ij(mask, index=None):
153153
'''
154154
Returns an iterator over image pixel coordinates for a given mask.
155155
156156
Arguments:
157157
158-
`image` (ndarray or :class:`spectral.Image`):
159-
160-
An image over whose pixels will be iterated.
161-
162158
`mask` (ndarray) [default None]:
163159
164-
An array of integers that specify over which pixels in `image`
165-
iteration should be performed.
160+
An array of integers that specify which coordinates should
161+
be returned.
166162
167163
`index` (int) [default None]:
168164
@@ -173,29 +169,19 @@ def iterator_ij(image, mask=None, index=None):
173169
An iterator over image pixel coordinates. Each returned item is a
174170
2-tuple of the form (row, col).
175171
176-
If neither `mask` nor `index` are defined, iteration is performed over all
177-
pixels. If `mask` (but not `index`) is defined, iteration is performed
178-
over all pixels for which `mask` is nonzero. If both `mask` and `index`
179-
are defined, iteration is performed over all pixels `image[i,j]` for which
180-
`mask[i,j] == index`.
172+
If `index` is not defined, iteration is performed over all non-zero
173+
elements. If `index` is defined, iteration is performed over all
174+
coordinates for whch `mask[i,j] == index`.
181175
'''
182176

183-
if mask is None and index is None:
184-
# Iterate over all pixels
185-
(nrows, ncols) = image.shape[:2]
186-
for r in range(nrows):
187-
for c in range(ncols):
188-
yield (r, c)
177+
if mask.ndim != 2:
178+
raise ValueError('Invalid mask shape.')
179+
if index is None:
180+
mask = mask != 0
189181
else:
190-
if mask.shape != image.shape[:len(mask.shape)]:
191-
raise ValueError('Mask shape does not match image.')
192-
193-
if index is None:
194-
mask = mask != 0
195-
else:
196-
mask = mask == index
197-
for rc in np.argwhere(mask):
198-
yield tuple(rc)
182+
mask = mask == index
183+
for rc in np.argwhere(mask):
184+
yield tuple(rc)
199185

200186

201187
def mean_cov(image, mask=None, index=None):

spectral/tests/iterators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ def test_iterator_ij_nonzero(self):
8585
classes = self.gt.ravel()
8686
pixels = data.reshape((-1, data.shape[-1]))
8787
sum = np.sum(pixels[classes > 0], 0)
88-
itsum = np.sum(np.array([data[ij] for ij in iterator_ij(data,
89-
self.gt)]), 0)
88+
itsum = np.sum(np.array([data[ij] for ij in iterator_ij(self.gt)]), 0)
9089
assert_allclose(sum, itsum)
9190

9291
def test_iterator_ij_index(self):
@@ -97,7 +96,9 @@ def test_iterator_ij_index(self):
9796
classes = self.gt.ravel()
9897
pixels = data.reshape((-1, data.shape[-1]))
9998
sum = np.sum(pixels[classes == cls], 0)
100-
itsum = np.sum(np.array([data[ij] for ij in iterator_ij(data, self.gt, cls)]), 0)
99+
itsum = np.sum(np.array([data[ij] for ij in iterator_ij(self.gt,
100+
cls)]),
101+
0)
101102
assert_allclose(sum, itsum)
102103

103104
def test_iterator_spyfile(self):

0 commit comments

Comments
 (0)