Skip to content

Commit 4c37acf

Browse files
committed
do not truncate numpy strings
1 parent b5a430c commit 4c37acf

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/py21cmwedge/uvgridder.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,16 @@ def uvw_to_dict(self):
240240
"""
241241

242242
def to_str(arr):
243-
return f"{arr[0]:.3f},{arr[1]:.3f}"
243+
print(f"{arr=:} {arr[0]:.3f},{arr[1]:.3f}")
244+
return np.array(f"{arr[0]:.3f},{arr[1]:.3f}", dtype=object)
244245

245246
uv_bins, counts = np.unique(
246-
np.apply_along_axis(to_str, 0, self.uvw_array), return_counts=True
247+
np.apply_along_axis(
248+
to_str,
249+
0,
250+
self.uvw_array,
251+
),
252+
return_counts=True,
247253
)
248254

249255
for uv_bin, count in zip(uv_bins, counts):

tests/test_uvgridder.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,43 @@ def test_read_antpos():
5555
np.testing.assert_allclose(test_antpos, test_obj.antpos)
5656

5757

58+
def test_calc_uvw():
59+
"""Antenna Position file to uvws test.
60+
61+
Read in antenna positions, convert to uvws,
62+
check equality with predefined uvw file.
63+
"""
64+
test_obj = UVGridder()
65+
test_obj.read_antpos(
66+
os.path.join(TEST_DATA_PATH, "test_antpos.txt"), skiprows=1, delimiter=","
67+
)
68+
test_antpos = np.array(
69+
[
70+
[0, 0, 0],
71+
[1, 0, 0],
72+
[0, 1, 0],
73+
[-1, 0, 0],
74+
[0, -1, 0],
75+
[1, 1, 0],
76+
[-1, -1, 0],
77+
[1, -1, 0],
78+
[-1, 1, 0],
79+
]
80+
).T
81+
82+
u = (
83+
np.expand_dims(test_antpos[0], 0).astype(np.float64)
84+
- np.expand_dims(test_antpos[0], 1).astype(np.float64)
85+
).ravel()
86+
v = (
87+
np.expand_dims(test_antpos[1], 0).astype(np.float64)
88+
- np.expand_dims(test_antpos[1], 1).astype(np.float64)
89+
).ravel()
90+
91+
np.testing.assert_allclose(u, test_obj.uvw_array[0])
92+
np.testing.assert_allclose(v, test_obj.uvw_array[1])
93+
94+
5895
def test_freq_from_int():
5996
"""Create frequency array from integer input."""
6097
test_obj = UVGridder()
@@ -142,19 +179,26 @@ def test_zero_uvbin():
142179
test_uvbin = {}
143180
test_obj.set_uvw_array(test_uvw)
144181
test_obj.uvw_to_dict()
182+
145183
assert test_obj.uvbins == test_uvbin
146184

147185

148186
def test_uvbin_is_dict():
149187
"""Test uvbins get saved as dict."""
150188
test_obj = UVGridder()
151-
test_uvw = np.zeros((3, 200)) + np.array([[14.6], [0], [0]])
152-
test_uvbin = {"14.600,0.000": 200}
189+
test_uvw = np.concatenate(
190+
[
191+
np.tile([14.6, 0, 0], (1, 1)),
192+
np.tile([0, 14.6, 0], (2, 1)),
193+
np.tile([14.6, 14.6, 0], (3, 1)),
194+
],
195+
axis=0,
196+
).T
197+
test_uvbin = {"14.600,0.000": 1, "0.000,14.600": 2, "14.600,14.600": 3}
198+
test_uvbin = {np.str_(key): np.int64(val) for (key, val) in test_uvbin.items()}
153199
test_obj.set_uvw_array(test_uvw)
154200
test_obj.uvw_to_dict()
155-
print(f"{test_uvbin=:}")
156-
print(f"{test_obj.uvbins=:}")
157-
test_obj.uvbins == test_uvbin
201+
np.testing.assert_equal(test_obj.uvbins, test_uvbin)
158202

159203

160204
def test_gauss_sum():

0 commit comments

Comments
 (0)