Skip to content

Commit 93cb6b3

Browse files
Fix create_pixel_dim (#567)
Co-authored-by: Walter Simson <walter.a.simson@gmail.com>
1 parent f7a7f2a commit 93cb6b3

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

kwave/utils/mapgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ def create_pixel_dim(Nx: int, origin_size: float, shift: float) -> Tuple[np.ndar
935935

936936
# pixel numbering has a double centre point
937937
else:
938-
nx = np.hstack([np.arange(-Nx / 2 + 1, 0 + 1, 1), np.arange(0, -Nx / 2 - 1 + 1, 1)])
938+
nx = np.hstack([np.arange(-Nx / 2 + 1, 0 + 1, 1), np.arange(0, Nx / 2 - 1 + 1, 1)])
939939

940940
# grid dimension has an odd number of points
941941
else:

tests/test_create_pixel_dim.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pytest
2+
import numpy as np
3+
4+
from kwave.utils.mapgen import create_pixel_dim
5+
6+
7+
def test_even_single_shift0():
8+
Nx = 4
9+
origin_size = "single"
10+
shift = 0
11+
12+
result = create_pixel_dim(Nx, origin_size, shift)
13+
expected = np.array([-1, 0, 1, 2]) # from the code path
14+
np.testing.assert_array_equal(result, expected)
15+
16+
17+
def test_even_single_shift1():
18+
Nx = 4
19+
origin_size = "single"
20+
shift = 1
21+
22+
result = create_pixel_dim(Nx, origin_size, shift)
23+
expected = np.array([-2, -1, 0, 1]) # from code path
24+
np.testing.assert_array_equal(result, expected)
25+
26+
27+
def test_even_double_shift_any():
28+
Nx = 4
29+
origin_size = "double"
30+
shift = 999 # any shift, code doesn't branch on shift for Nx even
31+
32+
result = create_pixel_dim(Nx, origin_size, shift)
33+
expected = np.array([-1, 0, 0, 1]) # based on code as-is
34+
np.testing.assert_array_equal(result, expected)
35+
36+
37+
def test_odd_single():
38+
Nx = 5
39+
origin_size = "single"
40+
shift = 0 # or shift=1 doesn't matter for this branch
41+
42+
result = create_pixel_dim(Nx, origin_size, shift)
43+
expected = np.array([-2, -1, 0, 1, 2])
44+
np.testing.assert_array_equal(result, expected)
45+
46+
47+
def test_odd_double_shift0():
48+
Nx = 5
49+
origin_size = "double"
50+
shift = 0
51+
52+
result = create_pixel_dim(Nx, origin_size, shift)
53+
expected = np.array([-1, 0, 0, 1, 2]) # consistent with code
54+
np.testing.assert_array_equal(result, expected)
55+
56+
57+
def test_odd_double_shift1():
58+
Nx = 5
59+
origin_size = "double"
60+
shift = 1
61+
62+
result = create_pixel_dim(Nx, origin_size, shift)
63+
expected = np.array([-2, -1, 0, 0, 1])
64+
np.testing.assert_array_equal(result, expected)

0 commit comments

Comments
 (0)