Skip to content

Commit 00dfbd9

Browse files
committed
account for frequency dimension in nearest gridder
1 parent 4f43d98 commit 00dfbd9

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/py21cmwedge/uvgridder.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ def uv_weights(self, u, v, spatial_function="triangle"):
265265
# weights = 1. - (np.abs(uv - grid)/np.diff(grid)[0])**2
266266
# weights = np.exp( - (uv - grid)**2/(2*np.diff(grid)[0]**2))
267267
# weights = np.exp( - abs(uv - grid)/(np.diff(grid)[0]))
268-
_range = np.arange(self.uv_size) - (self.uv_size - 1) / 2.0
269-
_range *= self.uv_delta
270268
match spatial_function.casefold():
271269
case "triangle":
270+
_range = np.arange(self.uv_size) - (self.uv_size - 1) / 2.0
271+
_range *= self.uv_delta
272272
x, y = np.meshgrid(_range, _range)
273273
x.shape += (1,)
274274
y.shape += (1,)
@@ -280,11 +280,22 @@ def uv_weights(self, u, v, spatial_function="triangle"):
280280
weights /= np.sum(weights, axis=(0, 1))
281281
weights = np.transpose(weights, [2, 0, 1])
282282
case "nearest":
283-
u_index = np.argmin(np.abs(u - _range))
284-
v_index = np.argmin(np.abs(v - _range))
283+
_range = np.arange(self.uv_size) - (self.uv_size - 1) / 2.0
284+
_range *= self.uv_delta
285+
x, y = _range, _range
286+
x.shape += (1,)
287+
y.shape += (1,)
288+
x = u - x
289+
y = v - y
290+
u_index = np.argmin(np.abs(x), axis=0)
291+
v_index = np.argmin(np.abs(y), axis=0)
292+
293+
print(f"{u_index.shape=:}, {v_index.shape=:}")
285294
print(f"{u_index=:}, {v_index=:}")
286-
weights = np.zeros((1, self.uv_size, self.uv_size), dtype=complex)
287-
weights[0, u_index, v_index] = 1.0
295+
weights = np.zeros(
296+
(x.shape[-1], self.uv_size, self.uv_size), dtype=complex
297+
)
298+
weights[:, u_index, v_index] = 1.0
288299
case _:
289300
raise ValueError(
290301
f"Unknown value for 'spatial_function': {spatial_function}"

tests/test_uvgridder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ def test_grid_uv_deltas():
326326
np.testing.assert_allclose(test_obj.uvf_cube[0, 33, 18], 10)
327327

328328

329+
def test_grid_uv_deltas_multi_freq():
330+
"""Test grid_uv sets up complex type."""
331+
test_obj = UVGridder()
332+
test_obj.uv_delta = 0.5
333+
test_obj.set_freqs([50e6, 150e6])
334+
test_uvw = np.zeros((3, 10)) + np.array([[14.6], [0], [0]])
335+
test_obj.set_uvw_array(test_uvw)
336+
test_obj.uvw_to_dict()
337+
test_obj.grid_uvw(convolve_beam=False, spatial_function="nearest")
338+
339+
np.testing.assert_allclose(test_obj.uvf_cube[1, 33, 18], 10)
340+
np.testing.assert_allclose(test_obj.uvf_cube[0, 23, 18], 10)
341+
342+
329343
def test_grid_uv_error():
330344
"""Test grid_uv sets up complex type."""
331345
test_obj = UVGridder()

0 commit comments

Comments
 (0)