3030from climada .hazard .test .test_base import hazard_kwargs
3131
3232
33- @pytest .fixture
33+ @pytest .fixture ( scope = "module" )
3434def haz_kwargs ():
3535 return hazard_kwargs ()
3636
@@ -40,14 +40,18 @@ def hazard(haz_kwargs):
4040 return Hazard (** haz_kwargs )
4141
4242
43- @pytest .fixture
44- def lead_time (haz_kwargs ):
45- return pd .timedelta_range ("1h" , periods = len (haz_kwargs ["event_id" ])).to_numpy ()
43+ @pytest .fixture (scope = "module" , params = [1 , 2 ])
44+ def lead_time (request , haz_kwargs ):
45+ base_range = pd .timedelta_range (
46+ "1h" , periods = len (haz_kwargs ["event_id" ]) // request .param
47+ )
48+ return np .tile (base_range .to_numpy (), request .param )
4649
4750
48- @pytest .fixture
49- def member (haz_kwargs ):
50- return np .arange (len (haz_kwargs ["event_id" ]))
51+ @pytest .fixture (scope = "module" , params = [1 , 2 ])
52+ def member (request , haz_kwargs ):
53+ base_range = np .arange (len (haz_kwargs ["event_id" ]) // request .param )
54+ return np .tile (base_range , request .param )
5155
5256
5357@pytest .fixture
@@ -260,55 +264,55 @@ def test_hazard_forecast_mean_min_max(haz_fc, attr):
260264 npt .assert_array_equal (haz_fcst_reduced .orig , [True ])
261265
262266
263- def test_hazard_forecast_mean_min_max_dim (haz_fc ):
267+ @pytest .mark .parametrize ("attr" , ["min" , "mean" , "max" ])
268+ def test_hazard_forecast_mean_min_max_member (haz_fc , attr ):
264269 """Check mean, min, and max methods for ImpactForecast with dim argument"""
265- for attr in ["min" , "mean" , "max" ]:
266- for dim , unique_vals in zip (
267- ["member" , "lead_time" ],
268- [np .unique (haz_fc .member ), np .unique (haz_fc .lead_time )],
269- ):
270- haz_fcst_reduced = getattr (haz_fc , attr )(dim = dim )
271- # Assert sparse matrices
272- expected_intensity = []
273- expected_fraction = []
274- for val in unique_vals :
275- mask = getattr (haz_fc , dim ) == val
276- expected_intensity .append (
277- getattr (haz_fc .intensity .todense ()[mask ], attr )(axis = 0 )
278- )
279- expected_fraction .append (
280- getattr (haz_fc .fraction .todense ()[mask ], attr )(axis = 0 )
281- )
282- npt .assert_array_equal (
283- haz_fcst_reduced .intensity .todense (),
284- np .vstack (expected_intensity ),
270+
271+ for dim , unique_vals in zip (
272+ ["member" , "lead_time" ],
273+ [np .unique (haz_fc .member ), np .unique (haz_fc .lead_time )],
274+ ):
275+ haz_fcst_reduced = getattr (haz_fc , attr )(dim = dim )
276+ # Assert sparse matrices
277+ expected_intensity = []
278+ expected_fraction = []
279+ for val in unique_vals :
280+ mask = getattr (haz_fc , dim ) == val
281+ expected_intensity .append (
282+ getattr (haz_fc .intensity .todense ()[mask ], attr )(axis = 0 )
285283 )
284+ expected_fraction .append (
285+ getattr (haz_fc .fraction .todense ()[mask ], attr )(axis = 0 )
286+ )
287+ npt .assert_array_equal (
288+ haz_fcst_reduced .intensity .todense (),
289+ np .vstack (expected_intensity ),
290+ )
291+ npt .assert_array_equal (
292+ haz_fcst_reduced .fraction .todense (),
293+ np .vstack (expected_fraction ),
294+ )
295+ # Check that attributes where reduced correctly
296+ if dim == "member" :
297+ npt .assert_array_equal (haz_fcst_reduced .member , unique_vals )
286298 npt .assert_array_equal (
287- haz_fcst_reduced .fraction . todense () ,
288- np .vstack ( expected_fraction ),
299+ haz_fcst_reduced .lead_time ,
300+ np .array ([ np . timedelta64 ( "NaT" )] * len ( unique_vals ) ),
289301 )
290-
291- # Check that attributes where reduced correctly
292- if dim == "member" :
293- npt .assert_array_equal (haz_fcst_reduced .member , unique_vals )
294- npt .assert_array_equal (
295- haz_fcst_reduced .lead_time ,
296- np .array ([np .timedelta64 ("NaT" )] * len (unique_vals )),
297- )
298- else : # dim == "lead_time"
299- npt .assert_array_equal (haz_fcst_reduced .lead_time , unique_vals )
300- npt .assert_array_equal (
301- haz_fcst_reduced .member ,
302- np .array ([- 1 ] * len (unique_vals )),
303- )
302+ else : # dim == "lead_time"
303+ npt .assert_array_equal (haz_fcst_reduced .lead_time , unique_vals )
304304 npt .assert_array_equal (
305- haz_fcst_reduced .event_name ,
306- np .array ([attr ] * len (unique_vals )),
305+ haz_fcst_reduced .member ,
306+ np .array ([- 1 ] * len (unique_vals )),
307307 )
308- npt .assert_array_equal (haz_fcst_reduced .event_id , [0 ] * len (unique_vals ))
309- npt .assert_array_equal (haz_fcst_reduced .frequency , [1 ] * len (unique_vals ))
310- npt .assert_array_equal (haz_fcst_reduced .date , [0 ] * len (unique_vals ))
311- npt .assert_array_equal (haz_fcst_reduced .orig , [True ] * len (unique_vals ))
308+ npt .assert_array_equal (
309+ haz_fcst_reduced .event_name ,
310+ np .array ([attr ] * len (unique_vals )),
311+ )
312+ npt .assert_array_equal (haz_fcst_reduced .event_id , [0 ] * len (unique_vals ))
313+ npt .assert_array_equal (haz_fcst_reduced .frequency , [1 ] * len (unique_vals ))
314+ npt .assert_array_equal (haz_fcst_reduced .date , [0 ] * len (unique_vals ))
315+ npt .assert_array_equal (haz_fcst_reduced .orig , [True ] * len (unique_vals ))
312316 # TODO add test in case no reduction happens (e.g., all values along dim are unique)
313317
314318
0 commit comments