@@ -305,113 +305,6 @@ def test_calc_impact_RF_pass(self):
305305 # fmt: on
306306 check_impact (self , impact , haz , exp , aai_agg , eai_exp , at_event , imp_mat_array )
307307
308- def test_impactForecast (self ):
309- """Test that ImpactForecast is returned correctly"""
310- lead_time = pd .timedelta_range ("1h" , periods = 6 ).to_numpy ()
311- member = np .arange (6 )
312- _haz = Hazard .from_hdf5 (get_test_file ("test_hazard_US_flood_random_locations" ))
313- haz_fc = HazardForecast .from_hazard (_haz , lead_time = lead_time , member = member )
314-
315- exp = Exposures .from_hdf5 (
316- get_test_file ("test_exposure_US_flood_random_locations" )
317- )
318- impf_set = ImpactFuncSet .from_excel (
319- Path (__file__ ).parent / "data" / "flood_imp_func_set.xls"
320- )
321- icalc = ImpactCalc (exp , impf_set , haz_fc )
322- impact = icalc .impact (assign_centroids = False )
323- aai_agg = 161436.05112960344
324- eai_exp = np .array (
325- [
326- 1.61159701e05 ,
327- 1.33742847e02 ,
328- 0.00000000e00 ,
329- 4.21352988e-01 ,
330- 1.42185609e02 ,
331- 0.00000000e00 ,
332- 0.00000000e00 ,
333- 0.00000000e00 ,
334- ]
335- )
336- at_event = np .array (
337- [
338- 0.00000000e00 ,
339- 0.00000000e00 ,
340- 9.85233619e04 ,
341- 3.41245461e04 ,
342- 7.73566566e07 ,
343- 0.00000000e00 ,
344- 0.00000000e00 ,
345- ]
346- )
347- # fmt: off
348- imp_mat_array = np .array (
349- [
350- [
351- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
352- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
353- ],
354- [
355- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
356- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
357- ],
358- [
359- 0.00000000e00 , 6.41965663e04 , 0.00000000e00 , 2.02249434e02 ,
360- 3.41245461e04 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
361- ],
362- [
363- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
364- 3.41245461e04 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
365- ],
366- [
367- 7.73566566e07 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
368- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
369- ],
370- [
371- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
372- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
373- ],
374- [
375- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
376- 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
377- ],
378- ]
379- )
380- # fmt: on
381- check_impact (
382- self , impact , haz_fc , exp , aai_agg , eai_exp , at_event , imp_mat_array
383- )
384-
385- # additional test to check that impact is indeed ImpactForecast
386- self .assertIsInstance (impact , ImpactForecast )
387- np .testing .assert_array_equal (impact .lead_time , lead_time )
388- self .assertIs (impact .lead_time .dtype , lead_time .dtype )
389- np .testing .assert_array_equal (impact .member , member )
390-
391- def test_impact_forecast_empty_impmat_error (self ):
392- """Test that error is raised when trying to compute impact forecast
393- without saving impact matrix
394- """
395- lead_time = pd .timedelta_range ("1h" , periods = 6 ).to_numpy ()
396- member = np .arange (6 )
397- _haz = Hazard .from_hdf5 (get_test_file ("test_hazard_US_flood_random_locations" ))
398- haz_fc = HazardForecast .from_hazard (_haz , lead_time = lead_time , member = member )
399-
400- exp = Exposures .from_hdf5 (
401- get_test_file ("test_exposure_US_flood_random_locations" )
402- )
403- impf_set = ImpactFuncSet .from_excel (
404- Path (__file__ ).parent / "data" / "flood_imp_func_set.xls"
405- )
406- icalc = ImpactCalc (exp , impf_set , haz_fc )
407- with self .assertRaises (ValueError ) as cm :
408- icalc .impact (assign_centroids = False , save_mat = False )
409- the_exception = cm .exception
410- self .assertEqual (
411- the_exception .args [0 ],
412- "Saving impact matrix is required when using HazardForecast." ,
413- )
414-
415308 def test_empty_impact (self ):
416309 """Check that empty impact is returned if no centroids match the exposures"""
417310 exp = ENT .exposures .copy ()
@@ -716,6 +609,118 @@ def test_single_exp_zero_mdr(self):
716609 check_impact (self , imp , haz , exp , aai_agg , eai_exp , at_event , at_event )
717610
718611
612+ class TestImpactCalcForecast (unittest .TestCase ):
613+ """Test impact calc for forecast hazard"""
614+
615+ def test_impactForecast (self ):
616+ """Test that ImpactForecast is returned correctly"""
617+ lead_time = pd .timedelta_range ("1h" , periods = 6 ).to_numpy ()
618+ member = np .arange (6 )
619+ _haz = Hazard .from_hdf5 (get_test_file ("test_hazard_US_flood_random_locations" ))
620+ haz_fc = HazardForecast .from_hazard (_haz , lead_time = lead_time , member = member )
621+
622+ exp = Exposures .from_hdf5 (
623+ get_test_file ("test_exposure_US_flood_random_locations" )
624+ )
625+ impf_set = ImpactFuncSet .from_excel (
626+ Path (__file__ ).parent / "data" / "flood_imp_func_set.xls"
627+ )
628+ icalc = ImpactCalc (exp , impf_set , haz_fc )
629+ impact = icalc .impact (assign_centroids = False )
630+ aai_agg = 161436.05112960344
631+ eai_exp = np .array (
632+ [
633+ 1.61159701e05 ,
634+ 1.33742847e02 ,
635+ 0.00000000e00 ,
636+ 4.21352988e-01 ,
637+ 1.42185609e02 ,
638+ 0.00000000e00 ,
639+ 0.00000000e00 ,
640+ 0.00000000e00 ,
641+ ]
642+ )
643+ at_event = np .array (
644+ [
645+ 0.00000000e00 ,
646+ 0.00000000e00 ,
647+ 9.85233619e04 ,
648+ 3.41245461e04 ,
649+ 7.73566566e07 ,
650+ 0.00000000e00 ,
651+ 0.00000000e00 ,
652+ ]
653+ )
654+ # fmt: off
655+ imp_mat_array = np .array (
656+ [
657+ [
658+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
659+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
660+ ],
661+ [
662+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
663+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
664+ ],
665+ [
666+ 0.00000000e00 , 6.41965663e04 , 0.00000000e00 , 2.02249434e02 ,
667+ 3.41245461e04 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
668+ ],
669+ [
670+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
671+ 3.41245461e04 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
672+ ],
673+ [
674+ 7.73566566e07 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
675+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
676+ ],
677+ [
678+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
679+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
680+ ],
681+ [
682+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
683+ 0.00000000e00 , 0.00000000e00 , 0.00000000e00 , 0.00000000e00 ,
684+ ],
685+ ]
686+ )
687+ # fmt: on
688+ check_impact (
689+ self , impact , haz_fc , exp , aai_agg , eai_exp , at_event , imp_mat_array
690+ )
691+
692+ # additional test to check that impact is indeed ImpactForecast
693+ self .assertIsInstance (impact , ImpactForecast )
694+ np .testing .assert_array_equal (impact .lead_time , lead_time )
695+ self .assertIs (impact .lead_time .dtype , lead_time .dtype )
696+ np .testing .assert_array_equal (impact .member , member )
697+
698+ def test_impact_forecast_empty_impmat_error (self ):
699+ """Test that error is raised when trying to compute impact forecast
700+ without saving impact matrix
701+ """
702+ lead_time = pd .timedelta_range ("1h" , periods = 6 ).to_numpy ()
703+ member = np .arange (6 )
704+ _haz = Hazard .from_hdf5 (get_test_file ("test_hazard_US_flood_random_locations" ))
705+ haz_fc = HazardForecast .from_hazard (_haz , lead_time = lead_time , member = member )
706+
707+ exp = Exposures .from_hdf5 (
708+ get_test_file ("test_exposure_US_flood_random_locations" )
709+ )
710+ impf_set = ImpactFuncSet .from_excel (
711+ Path (__file__ ).parent / "data" / "flood_imp_func_set.xls"
712+ )
713+ icalc = ImpactCalc (exp , impf_set , haz_fc )
714+ with self .assertRaises (ValueError ) as cm :
715+ icalc .impact (assign_centroids = False , save_mat = False )
716+ no_impmat_exception = cm .exception
717+ self .assertEqual (
718+ no_impmat_exception .args [0 ],
719+ "Saving impact matrix is required when using HazardForecast."
720+ "Please set save_mat=True." ,
721+ )
722+
723+
719724class TestImpactMatrixCalc (unittest .TestCase ):
720725 """Verify the computation of the impact matrix"""
721726
@@ -1011,6 +1016,7 @@ def test_skip_mat(self, from_eih_mock):
10111016# Execute Tests
10121017if __name__ == "__main__" :
10131018 TESTS = unittest .TestLoader ().loadTestsFromTestCase (TestImpactCalc )
1019+ TESTS .addTests (unittest .TestLoader ().loadTestsFromTestCase (TestImpactCalcForecast ))
10141020 TESTS .addTests (unittest .TestLoader ().loadTestsFromTestCase (TestReturnImpact ))
10151021 TESTS .addTests (unittest .TestLoader ().loadTestsFromTestCase (TestImpactMatrix ))
10161022 TESTS .addTests (unittest .TestLoader ().loadTestsFromTestCase (TestImpactMatrixCalc ))
0 commit comments