11import numpy as np
2- import litebird_sim as lbs
32from astropy .time import Time
43
4+ import litebird_sim as lbs
5+
56
67def get_RNG_hierarchy (seed , num_of_dets ):
78 rng_hierarchy = lbs .RNGHierarchy (
@@ -61,10 +62,12 @@ def test_wrappers_linear_gain_drift(self, tmp_path):
6162 num_of_obs_per_detector = 1 ,
6263 )
6364
64- sim1 .observations [0 ].gain_2_self = np .ones_like (sim1 .observations [0 ].tod )
65- sim1 .observations [0 ].gain_2_obs = np .ones_like (sim1 .observations [0 ].tod )
66- sim1 .observations [0 ].gain_2_tod = np .ones_like (sim1 .observations [0 ].tod )
67- sim1 .observations [0 ].gain_2_det = np .ones_like (sim1 .observations [0 ].tod )
65+ sim1_obs = sim1 .observations [0 ]
66+
67+ sim1_obs .gain_2_self = np .ones_like (sim1_obs .tod )
68+ sim1_obs .gain_2_obs = np .ones_like (sim1_obs .tod )
69+ sim1_obs .gain_2_tod = np .ones_like (sim1_obs .tod )
70+ sim1_obs .gain_2_det = np .ones_like (sim1_obs .tod )
6871
6972 # Applying gain drift using four different functions
7073 sim1 .apply_gaindrift (
@@ -82,31 +85,28 @@ def test_wrappers_linear_gain_drift(self, tmp_path):
8285
8386 dets_random = get_dets_random (random_seed , len (self .dets ))
8487 lbs .apply_gaindrift_to_tod (
85- tod = sim1 . observations [ 0 ] .gain_2_tod ,
88+ tod = sim1_obs .gain_2_tod ,
8689 sampling_freq_hz = self .sampling_freq_Hz ,
8790 drift_params = self .drift_params ,
8891 dets_random = dets_random ,
8992 )
9093
9194 dets_random = get_dets_random (random_seed , len (self .dets ))
92- for idx , tod in enumerate (sim1 . observations [ 0 ] .gain_2_det ):
95+ for idx , tod in enumerate (sim1_obs .gain_2_det ):
9396 lbs .apply_gaindrift_for_one_detector (
9497 det_tod = tod ,
9598 sampling_freq_hz = self .sampling_freq_Hz ,
9699 drift_params = self .drift_params ,
97100 random = dets_random [idx ],
98101 )
99102
100- # Testing if the four gain drift tods are same
101- np .testing .assert_array_equal (
102- sim1 .observations [0 ].gain_2_self , sim1 .observations [0 ].gain_2_obs
103- )
104- np .testing .assert_array_equal (
105- sim1 .observations [0 ].gain_2_self , sim1 .observations [0 ].gain_2_tod
106- )
107- np .testing .assert_array_equal (
108- sim1 .observations [0 ].gain_2_self , sim1 .observations [0 ].gain_2_det
109- )
103+ # We use `assert np.allclose()` instead of
104+ # `np.testing.assert_array_equal` because the latter
105+ # does not work reliably with Python 3.14 and NumPy 2.
106+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_obs )
107+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_obs )
108+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_tod )
109+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_det )
110110
111111 sim1 .flush ()
112112
@@ -129,10 +129,12 @@ def test_wrapper_thermal_gain_drift(self, tmp_path):
129129 num_of_obs_per_detector = 1 ,
130130 )
131131
132- sim1 .observations [0 ].gain_2_self = np .ones_like (sim1 .observations [0 ].tod )
133- sim1 .observations [0 ].gain_2_obs = np .ones_like (sim1 .observations [0 ].tod )
134- sim1 .observations [0 ].gain_2_tod = np .ones_like (sim1 .observations [0 ].tod )
135- sim1 .observations [0 ].gain_2_det = np .ones_like (sim1 .observations [0 ].tod )
132+ sim1_obs = sim1 .observations [0 ]
133+
134+ sim1_obs .gain_2_self = np .ones_like (sim1_obs .tod )
135+ sim1_obs .gain_2_obs = np .ones_like (sim1_obs .tod )
136+ sim1_obs .gain_2_tod = np .ones_like (sim1_obs .tod )
137+ sim1_obs .gain_2_det = np .ones_like (sim1_obs .tod )
136138
137139 self .drift_params .drift_type = lbs .GainDriftType .THERMAL_GAIN
138140
@@ -152,17 +154,15 @@ def test_wrapper_thermal_gain_drift(self, tmp_path):
152154
153155 dets_random = get_dets_random (random_seed , len (self .dets ))
154156 lbs .apply_gaindrift_to_tod (
155- tod = sim1 . observations [ 0 ] .gain_2_tod ,
157+ tod = sim1_obs .gain_2_tod ,
156158 sampling_freq_hz = self .sampling_freq_Hz ,
157159 drift_params = self .drift_params ,
158- focalplane_attr = getattr (
159- sim1 .observations [0 ], self .drift_params .focalplane_group
160- ),
160+ focalplane_attr = getattr (sim1_obs , self .drift_params .focalplane_group ),
161161 dets_random = dets_random ,
162162 )
163163
164164 dets_random = get_dets_random (random_seed , len (self .dets ))
165- for idx , tod in enumerate (sim1 . observations [ 0 ] .gain_2_det ):
165+ for idx , tod in enumerate (sim1_obs .gain_2_det ):
166166 lbs .apply_gaindrift_for_one_detector (
167167 det_tod = tod ,
168168 sampling_freq_hz = self .sampling_freq_Hz ,
@@ -171,16 +171,10 @@ def test_wrapper_thermal_gain_drift(self, tmp_path):
171171 )
172172
173173 # Testing if the four gain drift tods are same
174- np .testing .assert_array_equal (
175- sim1 .observations [0 ].gain_2_self , sim1 .observations [0 ].gain_2_obs
176- )
177- np .testing .assert_array_equal (
178- sim1 .observations [0 ].gain_2_self , sim1 .observations [0 ].gain_2_tod
179- )
174+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_obs )
175+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_tod )
180176 if False : # This is expected to fail as it does not reproduce what happens in `Simulation`
181- np .testing .assert_array_equal (
182- sim1 .observations [0 ].gain_2_self , sim1 .observations [0 ].gain_2_det
183- )
177+ assert np .allclose (sim1_obs .gain_2_self , sim1_obs .gain_2_det )
184178
185179 sim1 .flush ()
186180
@@ -224,22 +218,24 @@ def test_linear_gain_drift(tmp_path):
224218 num_of_obs_per_detector = 1 ,
225219 )
226220
221+ sim1_obs = sim1 .observations [0 ]
222+
227223 # gain_wrapper stores the tod applied with the wrapper function and is
228224 # tested against gain_native where the gain is applied right within
229225 # this function
230- sim1 . observations [ 0 ]. gain_wrapper = np .ones_like (sim1 . observations [ 0 ] .tod )
231- sim1 . observations [ 0 ]. gain_native = np .ones_like (sim1 . observations [ 0 ] .tod )
226+ sim1_obs . gain_wrapper = np .ones_like (sim1_obs .tod )
227+ sim1_obs . gain_native = np .ones_like (sim1_obs .tod )
232228
233229 sim1 .apply_gaindrift (
234230 drift_params = drift_params ,
235231 component = "gain_wrapper" ,
236232 user_seed = 987654321 ,
237233 )
238234
239- tod_size = len (sim1 . observations [ 0 ] .gain_native [0 ])
235+ tod_size = len (sim1_obs .gain_native [0 ])
240236
241237 dets_random = get_dets_random (987654321 , len (dets ))
242- for idx , tod in enumerate (sim1 . observations [ 0 ] .gain_native ):
238+ for idx , tod in enumerate (sim1_obs .gain_native ):
243239 rng = dets_random [idx ]
244240
245241 rand = rng .normal (
@@ -264,9 +260,7 @@ def test_linear_gain_drift(tmp_path):
264260 tod [div * gain_arr_size :] *= gain_arr [:mod ]
265261
266262 # Testing if the two tods are same
267- np .testing .assert_array_equal (
268- sim1 .observations [0 ].gain_wrapper , sim1 .observations [0 ].gain_native
269- )
263+ assert np .allclose (sim1_obs .gain_wrapper , sim1_obs .gain_native )
270264
271265 sim1 .flush ()
272266
@@ -329,19 +323,21 @@ def test_thermal_gain_drift_with_mismatch(self, tmp_path):
329323 num_of_obs_per_detector = 1 ,
330324 )
331325
326+ sim1_obs = sim1 .observations [0 ]
327+
332328 # gain_wrapper stores the tod applied with the wrapper function
333329 # and is tested against gain_native where the gain is applied right
334330 # within this function
335- sim1 . observations [ 0 ]. gain_wrapper = np .ones_like (sim1 . observations [ 0 ] .tod )
336- sim1 . observations [ 0 ]. gain_native = np .ones_like (sim1 . observations [ 0 ] .tod )
331+ sim1_obs . gain_wrapper = np .ones_like (sim1_obs .tod )
332+ sim1_obs . gain_native = np .ones_like (sim1_obs .tod )
337333
338334 sim1 .apply_gaindrift (
339335 drift_params = drift_params ,
340336 component = "gain_wrapper" ,
341337 user_seed = 987654321 ,
342338 )
343339 dets_random = get_dets_random (987654321 , len (self .dets ))
344- for idx , tod in enumerate (sim1 . observations [ 0 ] .gain_native ):
340+ for idx , tod in enumerate (sim1_obs .gain_native ):
345341 rng = dets_random [idx ]
346342
347343 rand = rng .normal (loc = 0.7 , scale = 0.5 )
@@ -352,11 +348,8 @@ def test_thermal_gain_drift_with_mismatch(self, tmp_path):
352348 tod *= 1.0 + thermal_factor / drift_params .focalplane_Tbath_K
353349
354350 for i in np .arange (len (self .dets )):
355- assert (
356- sim1 .observations [0 ].gain_wrapper [i ]
357- < sim1 .observations [0 ].gain_native [i ]
358- ).all (), (
359- f"The assertion is failed for detector { sim1 .observations [0 ].name [i ]} "
351+ assert (sim1_obs .gain_wrapper [i ] < sim1_obs .gain_native [i ]).all (), (
352+ f"The assertion is failed for detector { sim1_obs .name [i ]} "
360353 )
361354
362355 sim1 .flush ()
@@ -387,16 +380,18 @@ def test_thermal_gain_drift_no_mismatch(self, tmp_path):
387380 num_of_obs_per_detector = 1 ,
388381 )
389382
390- sim1 .observations [0 ].gain_wrapper = np .ones_like (sim1 .observations [0 ].tod )
391- sim1 .observations [0 ].gain_native = np .ones_like (sim1 .observations [0 ].tod )
383+ sim1_obs = sim1 .observations [0 ]
384+
385+ sim1_obs .gain_wrapper = np .ones_like (sim1_obs .tod )
386+ sim1_obs .gain_native = np .ones_like (sim1_obs .tod )
392387
393388 sim1 .apply_gaindrift (
394389 drift_params = drift_params ,
395390 component = "gain_wrapper" ,
396391 user_seed = 987654321 ,
397392 )
398393 dets_random = get_dets_random (987654321 , len (self .dets ))
399- for idx , tod in enumerate (sim1 . observations [ 0 ] .gain_native ):
394+ for idx , tod in enumerate (sim1_obs .gain_native ):
400395 rng = dets_random [idx ]
401396
402397 rand = rng .normal (loc = 0.7 , scale = 0.5 )
@@ -407,11 +402,8 @@ def test_thermal_gain_drift_no_mismatch(self, tmp_path):
407402 tod *= 1.0 + thermal_factor / drift_params .focalplane_Tbath_K
408403
409404 for i in np .arange (len (self .dets )):
410- assert (
411- sim1 .observations [0 ].gain_wrapper [i ]
412- < sim1 .observations [0 ].gain_native [i ]
413- ).all (), (
414- f"The assertion is failed for detector { sim1 .observations [0 ].name [i ]} "
405+ assert (sim1_obs .gain_wrapper [i ] < sim1_obs .gain_native [i ]).all (), (
406+ f"The assertion is failed for detector { sim1_obs .name [i ]} "
415407 )
416408
417409 sim1 .flush ()
0 commit comments