Skip to content
This repository was archived by the owner on Dec 15, 2020. It is now read-only.

Commit 3eafeb6

Browse files
committed
Merge pull request #42 from adonath/fix_for_#40
Fix for #40, #29 and #31
2 parents c7981b8 + b11d926 commit 3eafeb6

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎imageutils/array_utils.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import copy
99

1010
__all__ = ['extract_array_2d', 'add_array_2d', 'subpixel_indices',
11-
'mask_to_mirrored_num']
11+
'mask_to_mirrored_num', 'get_slices']
1212

1313

14-
def _get_slices(large_array_shape, small_array_shape, position):
14+
def get_slices(large_array_shape, small_array_shape, position):
1515
"""
1616
Get slices for the overlapping part of a small and a large array.
1717
@@ -43,10 +43,10 @@ def _get_slices(large_array_shape, small_array_shape, position):
4343
Slice in x direction for the small array.
4444
"""
4545
# Get edge coordinates
46-
y_min = int(position[1] + 0.5) - small_array_shape[0] // 2
47-
x_min = int(position[0] + 0.5) - small_array_shape[1] // 2
48-
y_max = int(position[1] + 0.5) + small_array_shape[0] // 2 + 1
49-
x_max = int(position[0] + 0.5) + small_array_shape[1] // 2 + 1
46+
y_min = int(position[1] + 0.5 - small_array_shape[0] / 2.)
47+
x_min = int(position[0] + 0.5 - small_array_shape[1] / 2.)
48+
y_max = int(position[1] + 0.5 + small_array_shape[0] / 2.)
49+
x_max = int(position[0] + 0.5 + small_array_shape[1] / 2.)
5050

5151
# Set up slices in x direction
5252
s_x = slice(max(0, x_min), min(large_array_shape[1], x_max))
@@ -76,21 +76,21 @@ def extract_array_2d(array_large, shape, position):
7676
7777
Examples
7878
--------
79-
We consider a large array of zeros with the shape 11x10 and a small
80-
array of ones with a shape of 3x4:
79+
We consider a large array with the shape 11x10, from which we extract
80+
a small array of shape 3x5:
8181
8282
>>> import numpy as np
8383
>>> from imageutils.array_utils import extract_array_2d
8484
>>> large_array = np.arange(110).reshape((11, 10))
8585
>>> large_array[4:9, 4:9] = np.ones((5, 5))
86-
>>> extract_array_2d(large_array, (3, 4), (7, 7))
86+
>>> extract_array_2d(large_array, (3, 5), (7, 7))
8787
array([[ 1, 1, 1, 1, 69],
8888
[ 1, 1, 1, 1, 79],
8989
[ 1, 1, 1, 1, 89]])
9090
"""
9191
# Check if larger array is really larger
9292
if array_large.shape >= shape:
93-
s_y, s_x, _, _ = _get_slices(array_large.shape, shape, position)
93+
s_y, s_x, _, _ = get_slices(array_large.shape, shape, position)
9494
return array_large[s_y, s_x]
9595
else:
9696
raise Exception("Can't extract array. Shape too large.")
@@ -128,7 +128,7 @@ def add_array_2d(array_large, array_small, position):
128128
"""
129129
# Check if larger array is really larger
130130
if array_large.shape >= array_small.shape:
131-
s_y, s_x, b_y, b_x = _get_slices(array_large.shape,
131+
s_y, s_x, b_y, b_x = get_slices(array_large.shape,
132132
array_small.shape, position)
133133
array_large[s_y, s_x] += array_small[b_y, b_x]
134134
return array_large

‎imageutils/tests/test_array_utils.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_extract_array_2d():
3333
assert np.all(extracted_array == small_test_array)
3434

3535

36-
def test_add_array_2d():
36+
def test_add_array_2d_odd_shape():
3737
"""
3838
Test add_array_2D utility function.
3939
@@ -48,6 +48,21 @@ def test_add_array_2d():
4848
assert np.all(added_array == large_test_array_ref)
4949

5050

51+
def test_add_array_2d_even_shape():
52+
"""
53+
Test add_array_2D utility function.
54+
55+
Test by adding an array of ones out of an array of zeros.
56+
"""
57+
large_test_array = np.zeros((11, 11))
58+
small_test_array = np.ones((4, 4))
59+
large_test_array_ref = large_test_array.copy()
60+
large_test_array_ref[0:2, 0:2] += small_test_array[2:4, 2:4]
61+
62+
added_array = add_array_2d(large_test_array, small_test_array, (0, 0))
63+
assert np.all(added_array == large_test_array_ref)
64+
65+
5166
@pytest.mark.parametrize(('position', 'subpixel_index'),
5267
zip(test_positions, test_position_indices))
5368
def test_subpixel_indices(position, subpixel_index):

0 commit comments

Comments
 (0)