77
88from imap_processing import imap_module_directory
99from imap_processing .ialirt .l0 .process_swapi import (
10+ FILLVAL_FLOAT32 ,
11+ Consts ,
1012 count_rate ,
1113 geometric_mean ,
1214 optimize_pseudo_parameters ,
@@ -184,8 +186,8 @@ def test_count_rate():
184186 """Use random realistic values to test for expected output of count_rate()."""
185187
186188 actual_result = count_rate (1370 , * [550 , 5.27 , 1e5 ])
187- expected_result = 3073.023325893161
188- assert actual_result == expected_result , (
189+ expected_result = 3073.023325893161 * Consts . temporary_density_factor
190+ assert np . isclose ( actual_result , expected_result ) , (
189191 f"The actual result of count_rate()"
190192 f" { actual_result } does not "
191193 f"match the expected result "
@@ -202,24 +204,24 @@ def test_optimize_parameters():
202204 "file_name" : "ialirt_test_data_u_sw_550_n_sw_5_T_sw_100000_v2.csv" ,
203205 "expected_values" : { # expected output and acceptable tolerance
204206 "pseudo_speed" : (550 , 0.01 ),
205- "pseudo_density" : (5 , 0.14 ),
206- "pseudo_temperature" : (1e5 , 0.2 ),
207+ "pseudo_density" : (5 / Consts . temporary_density_factor , 0.14 ),
208+ "pseudo_temperature" : (1e5 , 0.25 ),
207209 },
208210 },
209211 "test_set_2" : {
210212 "file_name" : "ialirt_test_data_u_sw_650_n_sw_3.0_T_sw_120000_v2.csv" ,
211213 "expected_values" : { # expected output and acceptable tolerance
212214 "pseudo_speed" : (650 , 0.01 ),
213- "pseudo_density" : (3 , 0.3 ),
215+ "pseudo_density" : (3 / Consts . temporary_density_factor , 0.3 ),
214216 "pseudo_temperature" : (1.2e5 , 0.28 ),
215217 },
216218 },
217219 "test_set_3" : {
218220 "file_name" : "ialirt_test_data_u_sw_400_n_sw_6.0_T_sw_80000_v2.csv" ,
219221 "expected_values" : { # expected output and acceptable tolerance
220222 "pseudo_speed" : (400 , 0.01 ),
221- "pseudo_density" : (6 , 0.39 ),
222- "pseudo_temperature" : (8e4 , 0.15 ),
223+ "pseudo_density" : (6 / Consts . temporary_density_factor , 0.39 ),
224+ "pseudo_temperature" : (8e4 , 0.2 ),
223225 },
224226 },
225227 }
@@ -259,6 +261,120 @@ def test_optimize_parameters():
259261 )
260262
261263
264+ def test_optimize_parameters_exception_handling ():
265+ """Test that the optimize_pseudo_parameters() function reports
266+ speed only when given data that causes curve_fit to fail."""
267+
268+ expected_speed = 557.279273 # peak passband speed
269+ file_name = "ialirt_test_data_u_sw_550_n_sw_5_T_sw_100000_v2.csv"
270+
271+ calibration_test_file = pd .read_csv (
272+ f"{ imap_module_directory } /tests/ialirt/data/l0/swapi_ialirt_energy_steps.csv"
273+ )
274+ energy_passbands = calibration_test_file ["Energy" ][0 :63 ].to_numpy ().astype (float )
275+
276+ energy_data = pd .read_csv (
277+ f"{ imap_module_directory } /tests/ialirt/data/l0/{ file_name } "
278+ )
279+ count_rates = energy_data ["Count Rates [Hz]" ].to_numpy ()
280+ count_rates [0 ] = 0.0
281+ count_rates = np .tile (count_rates , (2 , 1 ))
282+ count_rates_errors = energy_data ["Count Rates Error [Hz]" ].to_numpy ()
283+
284+ """
285+ code to select the random seed:
286+ for i in range(100):
287+ np.random.seed(i)
288+ result = optimize_pseudo_parameters(count_rates *
289+ np.abs(np.random.standard_normal(size=count_rates.shape)),
290+ count_rates_errors, energy_passbands)
291+ if np.isclose(result['pseudo_speed'][0], expected_speed,
292+ rtol=1e-6) and np.isnan(result['pseudo_density'][0]):
293+ print(i)
294+ """
295+ np .random .seed (14 )
296+ speed , density , temperature = optimize_pseudo_parameters (
297+ count_rates * np .abs (np .random .standard_normal (size = count_rates .shape )),
298+ count_rates_errors ,
299+ energy_passbands ,
300+ )
301+
302+ np .testing .assert_allclose (speed , expected_speed , rtol = 1e-6 )
303+ np .testing .assert_allclose (density , FILLVAL_FLOAT32 )
304+ np .testing .assert_allclose (temperature , FILLVAL_FLOAT32 )
305+
306+
307+ def test_optimize_parameters_bad_fit_handling ():
308+ """Test that the optimize_pseudo_parameters() function
309+ reports speed only when the fit is too poor."""
310+
311+ file_name = "ialirt_test_data_u_sw_550_n_sw_5_T_sw_100000_v2.csv"
312+
313+ calibration_test_file = pd .read_csv (
314+ f"{ imap_module_directory } /tests/ialirt/data/l0/swapi_ialirt_energy_steps.csv"
315+ )
316+ energy_passbands = calibration_test_file ["Energy" ][0 :63 ].to_numpy ().astype (float )
317+
318+ energy_data = pd .read_csv (
319+ f"{ imap_module_directory } /tests/ialirt/data/l0/{ file_name } "
320+ )
321+ count_rates = energy_data ["Count Rates [Hz]" ].to_numpy ()
322+ count_rates [0 ] = 0.0
323+ count_rates_errors = energy_data ["Count Rates Error [Hz]" ].to_numpy ()
324+
325+ # add high-amplitude randomness to the count rates to make the fit poor
326+ np .random .seed (0 )
327+ count_rates = count_rates + np .abs (
328+ np .random .standard_normal (size = count_rates .shape ) * count_rates .max ()
329+ )
330+
331+ speed , density , temperature = optimize_pseudo_parameters (
332+ count_rates , count_rates_errors , energy_passbands
333+ )
334+
335+ expected_speed = (
336+ np .sqrt (energy_passbands [count_rates .argmax (axis = - 1 )]) * Consts .speed_coeff
337+ )
338+
339+ np .testing .assert_allclose (speed , expected_speed , rtol = 1e-6 )
340+ np .testing .assert_allclose (density , FILLVAL_FLOAT32 )
341+ np .testing .assert_allclose (temperature , FILLVAL_FLOAT32 )
342+
343+
344+ def test_optimize_parameters_bad_covariance_handling ():
345+ """Test that the optimize_pseudo_parameters() function
346+ reports speed only when output covariance is nonsensical."""
347+
348+ file_name = "ialirt_test_data_u_sw_550_n_sw_5_T_sw_100000_v2.csv"
349+
350+ calibration_test_file = pd .read_csv (
351+ f"{ imap_module_directory } /tests/ialirt/data/l0/swapi_ialirt_energy_steps.csv"
352+ )
353+ energy_passbands = calibration_test_file ["Energy" ][0 :63 ].to_numpy ().astype (float )
354+
355+ energy_data = pd .read_csv (
356+ f"{ imap_module_directory } /tests/ialirt/data/l0/{ file_name } "
357+ )
358+ count_rates = energy_data ["Count Rates [Hz]" ].to_numpy ()
359+ count_rates [0 ] = 0.0
360+ count_rates_errors = energy_data ["Count Rates Error [Hz]" ].to_numpy ()
361+
362+ # setting errors to 0 results in infinite covariance
363+ count_rates_errors *= 0
364+
365+ speed , density , temperature = optimize_pseudo_parameters (
366+ count_rates , count_rates_errors , energy_passbands
367+ )
368+
369+ expected_speed = (
370+ np .sqrt (energy_passbands [count_rates .argmax (axis = - 1 )]) * Consts .speed_coeff
371+ )
372+
373+ np .testing .assert_allclose (speed , expected_speed , rtol = 1e-6 )
374+ np .testing .assert_allclose (density , FILLVAL_FLOAT32 )
375+ np .testing .assert_allclose (temperature , FILLVAL_FLOAT32 )
376+
377+
262378def test_geometric_mean ():
263379 """Test geometric_mean function."""
264380
0 commit comments