8000 MAINT: Keep API compatibility and don't modify input args · scikit-image/scikit-image@14e164a · GitHub
[go: up one dir, main page]

Skip to content

Commit 14e164a

Browse files
committed
MAINT: Keep API compatibility and don't modify input args
- Don't break API: argument `selem` must be second argument to be backwards compatible. - Don't modify input in _offset_to_raveled_neighbors. - Use `structure` instead of `selem` (where possible) which is closer to SciPy's terminology.
1 parent c98a33e commit 14e164a

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

skimage/morphology/extrema.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def _resolve_neighborhood(selem, connectivity, ndim):
328328
return selem
329329

330330

331-
def local_maxima(image, connectivity=None, selem=None, indices=False,
331+
def local_maxima(image, selem=None, connectivity=None, indices=False,
332332
allow_borders=True):
333333
"""Find local maxima of n-dimensional array.
334334
@@ -340,16 +340,16 @@ def local_maxima(image, connectivity=None, selem=None, indices=False,
340340
----------
341341
image : ndarray
342342
An n-dimensional array.
343+
selem : ndarray, optional
344+
A structuring element used to determine the neighborhood of each
345+
evaluated pixel. It must only contain 1's and 0's, have the same number
346+
of dimensions as `image`. If not given, all adjacent pixels are
347+
considered as part of the neighborhood.
343348
connectivity : int, optional
344349
A number used to determine the neighborhood of each evaluated pixel.
345350
Adjacent pixels whose squared distance from the center is larger or
346-
equal to `connectivity` are considered neighbors. If not given, all
347-
adjacent pixels are considered as part of the neighborhood. Ignored if
351+
equal to `connectivity` are considered neighbors. Ignored if
348352
`selem` is not None.
349-
selem : ndarray, optional
350-
A structuring element used to determine the neighborhood of each
351-
evaluated pixel. It must only contain 1's and 0's, have the same number
352-
of dimensions as `image`.
353353
indices : bool, optional
354354
If True, the output will be an array representing indices of local
355355
maxima. If False, the output will be an array of 0's and 1's with the
@@ -468,7 +468,7 @@ def local_maxima(image, connectivity=None, selem=None, indices=False,
468468
return flags
469469

470470

471-
def local_minima(image, connectivity=None, selem=None, indices=False,
471+
def local_minima(image, selem=None, connectivity=None, indices=False,
472472
allow_borders=True):
473473
"""Find local minima of n-dimensional array.
474474
@@ -480,16 +480,16 @@ def local_minima(image, connectivity=None, selem=None, indices=False,
480480
----------
481481
image : ndarray
482482
An n-dimensional array.
483+
selem : ndarray, optional
484+
A structuring element used to determine the neighborhood of each
485+
evaluated pixel. It must only contain 1's and 0's, have the same number
486+
of dimensions as `image`. If not given, all adjacent pixels are
487+
considered as part of the neighborhood.
483488
connectivity : int, optional
484489
A number used to determine the neighborhood of each evaluated pixel.
485490
Adjacent pixels whose squared distance from the center is larger or
486-
equal to `connectivity` are considered neighbors. If not given, all
487-
adjacent pixels are considered as part of the neighborhood. Ignored if
491+
equal to `connectivity` are considered neighbors. Ignored if
488492
`selem` is not None.
489-
selem : ndarray, optional
490-
A structuring element used to determine the neighborhood of each
491-
evaluated pixel. It must only contain 1's and 0's, have the same number
492-
of dimensions as `image`.
493493
indices : bool, optional
494494
If True, the output will be an array representing indices of local
495495
minima. If False, the output will be an array of 0's and 1's with the
@@ -568,8 +568,8 @@ def local_minima(image, connectivity=None, selem=None, indices=False,
568568
"""
569569
return local_maxima(
570570
image=invert(image),
571-
connectivity=connectivity,
572571
selem=selem,
572+
connectivity=connectivity,
573573
indices=indices,
574574
allow_borders=allow_borders
575575
)

skimage/morphology/watershed.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ def _validate_connectivity(image_dim, connectivity, offset):
120120
return c_connectivity, offset
121121

122122

123-
def _offsets_to_raveled_neighbors(image_shape, selem, center):
123+
def _offsets_to_raveled_neighbors(image_shape, structure, center):
124124
"""Compute offsets to a samples neighbors if the image would be raveled.
125125
126126
Parameters
127127
----------
128128
image_shape : tuple
129129
The shape of the image for which the offsets are computed.
130-
selem : ndarray
130+
structure : ndarray
131131
A structuring element determining the neighborhood expressed as an
132132
n-D array of 1's and 0's.
133133
center : sequence
@@ -144,8 +144,9 @@ def _offsets_to_raveled_neighbors(image_shape, selem, center):
144144
>>> _offsets_to_raveled_neighbors((4, 5), np.ones((4, 3)), (1, 1))
145145
array([-5, -1, 1, 5, -6, -4, 4, 6, 10, 9, 11])
146146
"""
147-
selem[tuple(center)] = 0 # Ignore the center; it's not a neighbor
148-
connection_indices = np.transpose(np.nonzero(selem))
147+
structure = structure.copy() # Don't modify original input
148+
structure[tuple(center)] = 0 # Ignore the center; it's not a neighbor
149+
connection_indices = np.transpose(np.nonzero(structure))
149150
offsets = (np.ravel_multi_index(connection_indices.T, image_shape) -
150151
np.ravel_multi_index(center, image_shape))
151152
squared_distances = np.sum((connection_indices - center) ** 2, axis=1)

0 commit comments

Comments
 (0)
0