@@ -36,6 +36,51 @@ def test__psf_weighted_noise_imaging_from():
3636 )
3737
3838
39+ def test__psf_weighted_data_from__unmasked_pixels_on_array_edge ():
40+ """
41+ Regression test: an unmasked pixel within `kernel_shape // 2` of the array
42+ edge drives the kernel off the weight map.
43+
44+ numba `@jit()` does not bounds-check array reads, so those positions
45+ silently returned uninitialized memory (values of order 1e299) rather than
46+ raising, poisoning `psf_weighted_data` and the data vector built from it.
47+ Because the values read depend on whatever the allocator left next to the
48+ weight map, the corruption was heap-state dependent: deterministic on the
49+ first call after a cold-cache compile, and intermittent in forked
50+ multiprocessing workers.
51+
52+ The zero-padded numpy implementation is the reference — kernel positions
53+ off the array contribute zero. Every other test in this module masks a
54+ one-pixel border, so none of them exercise this path.
55+ """
56+
57+ image = np .arange (1.0 , 26.0 ).reshape (5 , 5 )
58+ noise_map = np .ones ((5 , 5 ))
59+
60+ kernel = np .array ([[0.0 , 1.0 , 2.0 ], [3.0 , 4.0 , 1.0 ], [2.0 , 0.0 , 1.0 ]])
61+
62+ # Every pixel unmasked, so the border pixels push the kernel off the array.
63+ native_index_for_slim_index = np .array (
64+ [[y , x ] for y in range (5 ) for x in range (5 )]
65+ )
66+
67+ psf_weighted_data = aa .util .inversion_imaging_numba .psf_weighted_data_from (
68+ image_native = image ,
69+ noise_map_native = noise_map ,
70+ kernel_native = kernel ,
71+ native_index_for_slim_index = native_index_for_slim_index ,
72+ )
73+
74+ psf_weighted_data_numpy = aa .util .inversion_imaging .psf_weighted_data_from (
75+ weight_map_native = image / noise_map ** 2.0 ,
76+ kernel_native = kernel ,
77+ native_index_for_slim_index = native_index_for_slim_index ,
78+ )
79+
80+ assert np .all (np .isfinite (psf_weighted_data ))
81+ assert psf_weighted_data == pytest .approx (psf_weighted_data_numpy , 1.0e-8 )
82+
83+
3984def test__psf_weighted_data_from ():
4085
4186 mask = aa .Mask2D (
0 commit comments