def test_wind_v3_template(self): # Test running an end to end cyclone test based # on a wind config template. # The output file f = tempfile.NamedTemporaryFile( suffix='.npz', prefix='HAZIMPt_wind_scenarios_test_const', delete=False) wind_dir = os.path.join(misc.EXAMPLE_DIR, 'wind') exp_filename = os.path.join(wind_dir, 'syn_small_exposure_tcrm.csv') wind_filename = os.path.join(wind_dir, 'gust01.txt') a_config = [{TEMPLATE: WINDV3}, {LOADCSVEXPOSURE: {'file_name': exp_filename, 'exposure_latitude': 'LATITUDE', 'exposure_longitude': 'LONGITUDE'}}, {LOADWINDTCRM: [wind_filename]}, {CALCSTRUCTLOSS: {REP_VAL_NAME: 'REPLACEMENT_VALUE'}}, {SAVE: f.name}] context = main.start(config_list=a_config) self.assertTrue(allclose( context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = numpy.load(f.name) self.assertTrue(allclose(exp_dict['structural_loss'], exp_dict['calced-loss'])) os.remove(f.name)
def test_startII(self): # The config file f = tempfile.NamedTemporaryFile(suffix='.yaml', prefix='HAZIMPt_test_hazimp', delete=False, mode='w+t') print(' - ' + templates.TEMPLATE + ': ' + templates.DEFAULT, file=f) print(' - constant : ', file=f) print(' var : c_test', file=f) print(' value : 7 ', file=f) print(' - add_test:', file=f) print(' - multiply_test:', file=f) f.close() a_test = 5 b_test = 2 cont_in = context.Context() cont_in.exposure_long = scipy.asarray([11.0]) cont_in.exposure_att = {'a_test': a_test, 'b_test': b_test} cont_in = main.start(config_file=f.name, cont_in=cont_in) os.remove(f.name) self.assertEqual(cont_in.exposure_att['d_test'], 35) # 7 * 5 self.assertEqual(cont_in.exposure_att['c_test'], 7) # 5 + 2
def test_wind_v3_template_list_csv(self): # Test running an end to end cyclone test based # on a wind config template. # The output file f = tempfile.NamedTemporaryFile( suffix='.csv', prefix='HAZIMPt_wind_scenarios_test_const', delete=False) wind_dir = os.path.join(misc.EXAMPLE_DIR, 'wind') exp_filename = os.path.join(wind_dir, 'syn_small_exposure_tcrm.csv') vul_filename = os.path.join(misc.RESOURCE_DIR, 'synthetic_domestic_wind_vul_curves.xml') wind_filename = os.path.join(wind_dir, 'gust01.txt') a_config = [{ TEMPLATE: WINDV3 }, { LOADCSVEXPOSURE: { 'file_name': exp_filename, 'exposure_latitude': 'LATITUDE', 'exposure_longitude': 'LONGITUDE' } }, { VULNFILE: vul_filename }, { VULNSET: 'domestic_wind_2012' }, { LOADWINDTCRM: [wind_filename] }, { CALCSTRUCTLOSS: { REP_VAL_NAME: 'REPLACEMENT_VALUE' } }, { SAVE: f.name }] context = main.start(config_list=a_config) self.assertTrue( allclose(context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = misc.csv2dict(f.name) self.assertTrue( allclose(exp_dict['structural_loss'], exp_dict['calced-loss'])) # Failing this shows how versions of numpy # less than 1.8 reduce the # number of significant figures in the output self.assertTrue( allclose(exp_dict['exposure_latitude'], [-22.99, -23.01, -22.99, -23.99, -23]))
def test_wind_yaml_v3_list(self): # Test running an end to end cyclone test based # on a wind config template. wind_dir = os.path.join(misc.EXAMPLE_DIR, 'wind') exp_filename = os.path.join(wind_dir, 'syn_small_exposure_tcrm.csv') wind_filename = os.path.join(wind_dir, 'gust01.txt') # The output file f_out = tempfile.NamedTemporaryFile( suffix='.npz', prefix='HAZIMPt_wind_scenarios_test_const', delete=False) # The config file f = tempfile.NamedTemporaryFile( suffix='.yaml', prefix='HAZIMP_wind_scenarios_test_const', delete=False) print(' - ' + TEMPLATE + ': ' + WINDV3, file=f) print(' - ' + LOADCSVEXPOSURE + ': ', file=f) print(' file_name: ' + exp_filename, file=f) print(' exposure_latitude: LATITUDE', file=f) print(' exposure_longitude: LONGITUDE', file=f) print( ' - ' + LOADWINDTCRM + ': [' + wind_filename + ']', file=f) print(' - ' + CALCSTRUCTLOSS + ': ', file=f) print(' ' + REP_VAL_NAME + ': ' + 'REPLACEMENT_VALUE', file=f) print(' - ' + SAVE + ': ' + f_out.name, file=f) f.close() context = main.start(config_file=f.name) self.assertTrue(allclose( context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = numpy.load(f_out.name) self.assertTrue(allclose(exp_dict['structural_loss'], exp_dict['calced-loss'])) os.remove(f.name) os.remove(f_out.name)
def test_exposure_and_vuln_functions(self): # Create the files building_vulnerability = build_example_building_vulnerability() contents_vulnerability = build_example_contents_vulnerability() file_exp, lat_name, long_name = build_example_exposure() the_config = [{'template': 'default'}, {jobs.LOADCSVEXPOSURE: {'file_name': file_exp, 'exposure_latitude': lat_name, 'exposure_longitude': long_name}}, {jobs.LOADXMLVULNERABILITY: {'file_name': [building_vulnerability, contents_vulnerability]}}, {jobs.SIMPLELINKER: {'vul_functions_in_exposure': {"EQ_building": 'building', "EQ_contents": 'contents'}}}, {jobs.SELECTVULNFUNCTION: {'variability_method': {"EQ_building": 'mean', "EQ_contents": 'mean'}}}, {jobs.LOOKUP: None}] context = main.start(config_list=the_config) # SW1 loss ratio # SW1 4 MMI - 0.4 building_loss , 0.004 contents_loss # SW2 5 MMI - 0.05 building_loss 0.0005 contents_loss if parallel.STATE.size == 1: results = [0.4, 0.05] actual = context.exposure_att['building_loss'] self.assertTrue(allclose(actual, results), 'actual:' + str(actual) + '\n results:' + str(results)) else: if parallel.STATE.rank == 0: results = [0.4] actual = context.exposure_att['building_loss'] self.assertTrue(allclose(actual, results), 'actual:' + str(actual) + '\n results:' + str(results)) elif parallel.STATE.rank == 1: results = [0.05] actual = context.exposure_att['building_loss'] self.assertTrue(allclose(actual, results), 'actual:' + str(actual) + '\n results:' + str(results)) os.remove(building_vulnerability) os.remove(contents_vulnerability) os.remove(file_exp)
def test_const_test(self): # First test running an end to end wind test based # on a config list, no template # The output file f = tempfile.NamedTemporaryFile( suffix='.npz', prefix='HAZIMPt_wind_scenarios_test_const', delete=False) wind_dir = os.path.join(misc.EXAMPLE_DIR, 'wind') exp_filename = os.path.join(wind_dir, 'syn_small_exposure_tcrm.csv') wind_filename = os.path.join(wind_dir, 'gust01.txt') vul_filename = os.path.join(misc.RESOURCE_DIR, 'synthetic_domestic_wind_vul_curves.xml') sim_at = {'domestic_wind_2012': 'WIND_VULNERABILITY_FUNCTION_ID'} slv_at = {'domestic_wind_2012': 'mean'} a_config = [ {TEMPLATE: DEFAULT}, {LOADCSVEXPOSURE: {'file_name': exp_filename, 'exposure_latitude': 'LATITUDE', 'exposure_longitude': 'LONGITUDE'}}, {LOADRASTER: {'file_list': [wind_filename], 'attribute_label': '0.2s gust at 10m height m/s'}}, {LOADXMLVULNERABILITY: {'file_name': vul_filename}}, {SIMPLELINKER: {'vul_functions_in_exposure': sim_at}}, {SELECTVULNFUNCTION: {'variability_method': slv_at}}, {LOOKUP: None}, {calcs.STRUCT_LOSS: None}, {SAVEALL: {'file_name': f.name}}] context = main.start(config_list=a_config) self.assertTrue(allclose( context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = numpy.load(f.name) self.assertTrue(allclose(exp_dict['structural_loss'], exp_dict['calced-loss'])) os.remove(f.name)
def test_flood_struct_yaml_list(self): # Test running an end to end flood test based # on a flood config template. # IF YOU CHANGE THIS CHANGE list_flood_v2.yaml as well flood_dir = os.path.join(misc.EXAMPLE_DIR, 'flood') exp_filename = os.path.join(flood_dir, 'small_exposure.csv') flood_filename = os.path.join(flood_dir, 'depth_small_synthetic.txt') # The output file f_out = tempfile.NamedTemporaryFile( suffix='.npz', prefix='HAZIMPt_flood_scenarios_test_const', delete=False) # The config file f = tempfile.NamedTemporaryFile( suffix='.yaml', prefix='HAZIMP_flood_scenarios_test_const', delete=False) print(' - ' + TEMPLATE + ': ' + FLOODFABRICV2, file=f) print(' - ' + LOADCSVEXPOSURE + ': ', file=f) print(' file_name: ' + exp_filename, file=f) print(' exposure_latitude: LATITUDE', file=f) print(' exposure_longitude: LONGITUDE', file=f) print(' - ' + FLOOR_HEIGHT + ': .3', file=f) a_str = ' - ' + LOADFLOODASCII + ': [' + flood_filename + ']' print(a_str, file=f) print(' - ' + CALCSTRUCTLOSS + ': ', file=f) print(' ' + REP_VAL_NAME + ': ' + 'REPLACEMENT_VALUE', file=f) print(' - ' + SAVE + ': ' + f_out.name, file=f) f.close() context = main.start(config_file=f.name) self.assertTrue( allclose(context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = numpy.load(f_out.name) self.assertTrue( allclose(exp_dict['structural_loss'], exp_dict['calced-loss'])) os.remove(f.name) os.remove(f_out.name)
def test_wind_yaml_v3_list(self): # Test running an end to end cyclone test based # on a wind config template. wind_dir = os.path.join(misc.EXAMPLE_DIR, 'wind') exp_filename = os.path.join(wind_dir, 'syn_small_exposure_tcrm.csv') vul_filename = os.path.join(misc.RESOURCE_DIR, 'synthetic_domestic_wind_vul_curves.xml') wind_filename = os.path.join(wind_dir, 'gust01.txt') # The output file f_out = tempfile.NamedTemporaryFile( suffix='.npz', prefix='HAZIMPt_wind_scenarios_test_const', delete=False) # The config file f = tempfile.NamedTemporaryFile( mode='w', suffix='.yaml', prefix='HAZIMP_wind_scenarios_test_const', delete=False) f.write(f" - {TEMPLATE}: {WINDV3}\n") f.write(f" - {VULNFILE}: {vul_filename}\n") f.write(f" - {VULNSET}: domestic_wind_2012\n") f.write(f" - {LOADCSVEXPOSURE}:\n") f.write(f" file_name: {exp_filename}\n") f.write(" exposure_latitude: LATITUDE\n") f.write(" exposure_longitude: LONGITUDE\n") f.write(f" - {LOADWINDTCRM}: [{wind_filename}]\n") f.write(f" - {CALCSTRUCTLOSS}: \n") f.write(f" {REP_VAL_NAME}: REPLACEMENT_VALUE\n") f.write(f" - {SAVE}: {f_out.name}\n") f.close() context = main.start(config_file=f.name) self.assertTrue( allclose(context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = numpy.load(f_out.name) self.assertTrue( allclose(exp_dict['structural_loss'], exp_dict['calced-loss']))
def test_flood_fabric_v2_template_list(self): # Test running an end to end test based # on a config template. # The output file f = tempfile.NamedTemporaryFile( suffix='.csv', prefix='HAZIMP_flood_scenarios_test_const', delete=False) resource_dir = os.path.join(misc.EXAMPLE_DIR, 'flood') exp_filename = os.path.join(resource_dir, 'small_exposure.csv') haz_filename = os.path.join(resource_dir, 'depth_small_synthetic.txt') config = [{ TEMPLATE: FLOODFABRICV2 }, { LOADCSVEXPOSURE: { 'file_name': exp_filename, 'exposure_latitude': 'LATITUDE', 'exposure_longitude': 'LONGITUDE' } }, { FLOOR_HEIGHT: .3 }, { LOADFLOODASCII: [haz_filename] }, { CALCSTRUCTLOSS: { REP_VAL_NAME: 'REPLACEMENT_VALUE' } }, { SAVE: f.name }] context = main.start(config_list=config) self.assertTrue( allclose(context.exposure_att['structural_loss'], context.exposure_att['calced-loss'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = misc.csv2dict(f.name) self.assertTrue( allclose(exp_dict['structural_loss'], exp_dict['calced-loss'])) os.remove(f.name)
def test_start(self): config_list = [{ templates.TEMPLATE: templates.DEFAULT }, { 'constant': { 'var': 'c_test', 'value': 7 } }, { 'add_test': None }, { 'multiply_test': None }] a_test = 5 b_test = 2 cont_in = context.Context() cont_in.exposure_att = {'a_test': a_test, 'b_test': b_test} cont_in.exposure_long = scipy.asarray([11.0]) cont_in = main.start(config_list=config_list, cont_in=cont_in) self.assertEqual(cont_in.exposure_att['d_test'], 35) self.assertEqual(cont_in.exposure_att['c_test'], 7)
def test_flood_contents_v2_template_list(self): # Test running an end to end test based # on a config template. # Note, removing randomness for testing purposes # The output file f = tempfile.NamedTemporaryFile( suffix='.csv', prefix='HAZIMP_flood_scenarios_test_const', delete=False) resource_dir = os.path.join(misc.EXAMPLE_DIR, 'flood') exp_filename = os.path.join(resource_dir, 'small_exposure.csv') haz_filename = os.path.join(resource_dir, 'depth_small_synthetic.txt') config = [{ TEMPLATE: FLOODCONTENTSV2 }, { LOADCSVEXPOSURE: { 'file_name': exp_filename, 'exposure_latitude': 'LATITUDE', 'exposure_longitude': 'LONGITUDE' } }, { FLOOR_HEIGHT: .3 }, { LOADFLOODASCII: [haz_filename] }, { flood_conts.INSURE_PROB: { flood_conts.INSURED: 1.0, flood_conts.UNINSURED: 0.0 } }, { flood_conts.CONT_ACTIONS: { flood_conts.SAVE_CONT: 0.0, flood_conts.NO_ACTION_CONT: 0.0, flood_conts.EXPOSE_CONT: 1.0 } }, { CALCCONTLOSS: { REP_VAL_NAME: 'REPLACEMENT_VALUE' } }, { SAVE: f.name }] context = main.start(config_list=config) # These don't work on parallelised tests # if they are wrong the error will flow # on and be caught in the contents_loss # self.assertTrue(allclose( # context.exposure_att['calced_haz'][[0, 1, 3]], # context.exposure_att['water_depth'][[0, 1, 3]])) # # index = 'water depth above ground floor (m)' # self.assertTrue(allclose( # context.exposure_att['calced_floor_depth'][[0, 1, 3]], # context.exposure_att[index][[0, 1, 3]])) # print ('file name', f.name) # self.assertTrue(numpy.array_equal( # context.exposure_att['calced_CONTENTS_FLOOD_FUNCTION_ID'], # context.exposure_att['CONTENTS_FLOOD_FUNCTION_ID'])) self.assertTrue( allclose(context.exposure_att['calced_contents_loss_ratio'], context.exposure_att['contents_loss_ratio'])) # Only the head node writes a file if parallel.STATE.rank == 0: exp_dict = misc.csv2dict(f.name) self.assertTrue( allclose(exp_dict['contents_loss'], exp_dict['calced_contents_loss'])) os.remove(f.name)