STY: PEP8 and other clean up.

This commit is contained in:
Tony S Yu
2012-08-09 00:21:59 -04:00
parent 70153abb83
commit e4dd658daf
2 changed files with 22 additions and 27 deletions
+19 -24
View File
@@ -87,62 +87,57 @@ def reconstruction(image, mask, selem=None, offset=None):
raise ImportError("_greyreconstruct extension not available.")
if selem is None:
selem = np.ones([3]*image.ndim, bool)
selem = np.ones([3]*image.ndim, dtype=bool)
else:
selem = selem.copy()
if offset == None:
assert all([d % 2 == 1 for d in selem.shape]),\
"Footprint dimensions must all be odd"
offset = np.array([d/2 for d in selem.shape])
if not all([d % 2 == 1 for d in selem.shape]):
ValueError("Footprint dimensions must all be odd")
offset = np.array([d / 2 for d in selem.shape])
# Cross out the center of the selem
selem[[slice(d,d+1) for d in offset]] = False
#
selem[[slice(d, d + 1) for d in offset]] = False
# Construct an array that's padded on the edges so we can ignore boundaries
# The array is a dstack of the image and the mask; this lets us interleave
# image and mask pixels when sorting which makes list manipulations easier
#
padding = (np.array(selem.shape)/2).astype(int)
dims = np.zeros(image.ndim+1,int)
padding = (np.array(selem.shape) / 2).astype(int)
dims = np.zeros(image.ndim + 1, dtype=int)
dims[1:] = np.array(image.shape)+2*padding
dims[0] = 2
inside_slices = [slice(p,-p) for p in padding]
values = np.ones(dims)*np.min(image)
values[[0]+inside_slices] = image
values[[1]+inside_slices] = mask
#
values = np.ones(dims) * np.min(image)
values[[0] + inside_slices] = image
values[[1] + inside_slices] = mask
# Create a list of strides across the array to get the neighbors
# within a flattened array
#
value_stride = np.array(values.strides[1:]) / values.dtype.itemsize
image_stride = values.strides[0] / values.dtype.itemsize
selem_mgrid = np.mgrid[[slice(-o,d - o)
for d,o in zip(selem.shape,offset)]]
selem_offsets = selem_mgrid[:,selem].transpose()
selem_mgrid = np.mgrid[[slice(-o, d - o)
for d, o in zip(selem.shape, offset)]]
selem_offsets = selem_mgrid[:, selem].transpose()
strides = np.array([np.sum(value_stride * selem_offset)
for selem_offset in selem_offsets], np.int32)
values = values.flatten()
value_sort = np.lexsort([-values]).astype(np.int32)
#
# Make a linked list of pixels sorted by value. -1 is the list terminator.
#
prev = -np.ones(len(values), np.int32)
next = -np.ones(len(values), np.int32)
prev[value_sort[1:]] = value_sort[:-1]
next[value_sort[:-1]] = value_sort[1:]
#
# Create a rank-order value array so that the Cython inner-loop
# can operate on a uniform data type
#
values, value_map = rank_order(values)
current = value_sort[0]
reconstruction_loop(values, prev, next, strides, current, image_stride)
#
# Reshape the values array to the shape of the padded image
# and return the unpadded portion of that result
#
values = value_map[values[:image_stride]]
values.shape = np.array(image.shape)+2*padding
values.shape = np.array(image.shape) + 2 * padding
return values[inside_slices]
@@ -26,7 +26,7 @@ def test_image_less_than_mask():
"""Test reconstruction where the image is uniform and less than mask"""
image = np.ones((5, 5))
mask = np.ones((5, 5)) * 2
assert np.all(reconstruction(image,mask) == 1)
assert np.all(reconstruction(image, mask) == 1)
def test_one_image_peak():
@@ -34,7 +34,7 @@ def test_one_image_peak():
image = np.ones((5, 5))
image[2, 2] = 2
mask = np.ones((5, 5)) * 3
assert np.all(reconstruction(image,mask) == 2)
assert np.all(reconstruction(image, mask) == 2)
def test_two_image_peaks():
@@ -59,7 +59,7 @@ def test_two_image_peaks():
[1, 1, 1, 1, 1, 3, 3, 3],
[1, 1, 1, 1, 1, 3, 3, 3],
[1, 1, 1, 1, 1, 3, 3, 3]])
assert np.all(reconstruction(image,mask) == expected)
assert np.all(reconstruction(image, mask) == expected)
def test_zero_image_one_mask():