def test_pop_growth(): homedir = os.path.dirname( os.path.abspath(__file__)) # where is this script? print('Testing loading Population Growth from UN spreadsheet...') pg = PopulationGrowth.fromDefault() print('Passed loading Population Growth from UN spreadsheet...') print('Testing getting growth rates for the US...') rate = pg.getRate(840, 1963) assert rate == 1.373 / 100.0 allrates = np.array([ 1.581, 1.724, 1.373, 0.987, 0.885, 0.948, 0.945, 0.985, 1.035, 1.211, 0.915, 0.907, 0.754 ]) / 100.0 starts, usrates = pg.getRates(840) np.testing.assert_almost_equal(usrates, allrates) print('Passed getting growth rate for the US...') # three scenarios to test with regards to population growth rates # 1: time population data was "collected" is before the event time tpop = 2015 tevent = 2016 ccode = 840 # US pop = 1e6 newpop = pg.adjustPopulation(pop, ccode, tpop, tevent) tpop = 2007 tevent = 2016 ccode = 840 pop = 1e6 # TODO: hand-verify this result! newpop = pg.adjustPopulation(pop, ccode, tpop, tevent) # #2: time population data was "collected" is after the event time tpop = 2016 tevent = 2012 ccode = 840 # US pop = 1e6 newpop = pg.adjustPopulation(pop, ccode, tpop, tevent)
def test(): print('Testing Northridge exposure check (with GPW data).') events = ['northridge'] homedir = os.path.dirname( os.path.abspath(__file__)) # where is this script? excelfile = os.path.join(homedir, '..', 'data', 'WPP2015_POP_F02_POPULATION_GROWTH_RATE.xls') for event in events: shakefile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_grid.xml' % event) popfile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_gpw.flt' % event) isofile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_isogrid.bil' % event) growth = PopulationGrowth.fromDefault() exp = Exposure(popfile, 2012, isofile) results = exp.calcExposure(shakefile) cmpexposure = [ 0, 0, 1817, 1767260, 5840985, 5780298, 2738374, 1559657, 4094, 0 ] np.testing.assert_almost_equal(cmpexposure, results['TotalExposure']) print('Passed Northridge exposure check (with GPW data).')
def basic_test(): mmidata = np.array([[7, 8, 8, 8, 7], [8, 9, 9, 9, 8], [8, 9, 10, 9, 8], [8, 9, 9, 8, 8], [7, 8, 8, 6, 5]], dtype=np.float32) popdata = np.ones_like(mmidata) * 1e7 isodata = np.array( [[4, 4, 4, 4, 4], [4, 4, 4, 4, 4], [4, 4, 156, 156, 156], [156, 156, 156, 156, 156], [156, 156, 156, 156, 156]], dtype=np.int32) shakefile = get_temp_file_name() popfile = get_temp_file_name() isofile = get_temp_file_name() geodict = GeoDict({ 'xmin': 0.5, 'xmax': 4.5, 'ymin': 0.5, 'ymax': 4.5, 'dx': 1.0, 'dy': 1.0, 'nx': 5, 'ny': 5 }) layers = OrderedDict([ ('mmi', mmidata), ]) event_dict = { 'event_id': 'us12345678', 'magnitude': 7.8, 'depth': 10.0, 'lat': 34.123, 'lon': -118.123, 'event_timestamp': datetime.utcnow(), 'event_description': 'foo', 'event_network': 'us' } shake_dict = { 'event_id': 'us12345678', 'shakemap_id': 'us12345678', 'shakemap_version': 1, 'code_version': '4.5', 'process_timestamp': datetime.utcnow(), 'shakemap_originator': 'us', 'map_status': 'RELEASED', 'shakemap_event_type': 'ACTUAL' } unc_dict = {'mmi': (1, 1)} shakegrid = ShakeGrid(layers, geodict, event_dict, shake_dict, unc_dict) shakegrid.save(shakefile) popgrid = Grid2D(popdata, geodict.copy()) isogrid = Grid2D(isodata, geodict.copy()) write(popgrid, popfile, 'netcdf') write(isogrid, isofile, 'netcdf') ratedict = { 4: { 'start': [2010, 2012, 2014, 2016], 'end': [2012, 2014, 2016, 2018], 'rate': [0.01, 0.02, 0.03, 0.04] }, 156: { 'start': [2010, 2012, 2014, 2016], 'end': [2012, 2014, 2016, 2018], 'rate': [0.02, 0.03, 0.04, 0.05] } } popgrowth = PopulationGrowth(ratedict) popyear = datetime.utcnow().year exposure = Exposure(popfile, popyear, isofile, popgrowth=popgrowth) expdict = exposure.calcExposure(shakefile) modeldict = [ LognormalModel('AF', 11.613073, 0.180683, 1.0), LognormalModel('CN', 10.328811, 0.100058, 1.0) ] fatmodel = EmpiricalLoss(modeldict) # for the purposes of this test, let's override the rates # for Afghanistan and China with simpler numbers. fatmodel.overrideModel( 'AF', np.array([0, 0, 0, 0, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 0], dtype=np.float32)) fatmodel.overrideModel( 'CN', np.array([0, 0, 0, 0, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 0], dtype=np.float32)) print('Testing very basic fatality calculation...') fatdict = fatmodel.getLosses(expdict) # strictly speaking, the afghanistant fatalities should be 462,000 but floating point precision dictates otherwise. testdict = {'CN': 46111, 'AF': 461999, 'TotalFatalities': 508110} for key, value in fatdict.items(): assert value == testdict[key] print('Passed very basic fatality calculation...') print('Testing grid fatality calculations...') mmidata = exposure.getShakeGrid().getLayer('mmi').getData() popdata = exposure.getPopulationGrid().getData() isodata = exposure.getCountryGrid().getData() fatgrid = fatmodel.getLossGrid(mmidata, popdata, isodata) assert np.nansum(fatgrid) == 508111 print('Passed grid fatality calculations...') # Testing modifying rates and stuffing them back in... chile = LognormalModel('CL', 19.786773, 0.259531, 0.0) rates = chile.getLossRates(np.arange(5, 10)) modrates = rates * 2 # does this make event twice as deadly? # roughly the exposures from 2015-9-16 CL event expo_pop = np.array( [0, 0, 0, 1047000, 7314000, 1789000, 699000, 158000, 0, 0]) mmirange = np.arange(5, 10) chile_deaths = chile.getLosses(expo_pop[4:9], mmirange) chile_double_deaths = chile.getLosses(expo_pop[4:9], mmirange, rates=modrates) print('Chile model fatalities: %f' % chile_deaths) print('Chile model x2 fatalities: %f' % chile_double_deaths)
def test(): homedir = os.path.dirname(os.path.abspath( __file__)) # where is this script? fatfile = os.path.join(homedir, '..', 'data', 'fatality.xml') ecofile = os.path.join(homedir, '..', 'data', 'economy.xml') cityfile = os.path.join(homedir, '..', 'data', 'cities1000.txt') event = 'northridge' shakefile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_grid.xml' % event) popfile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_gpw.flt' % event) isofile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_isogrid.bil' % event) urbanfile = os.path.join(homedir, '..', 'data', 'eventdata', 'northridge', 'northridge_urban.bil') oceanfile = os.path.join( homedir, '..', 'data', 'eventdata', 'northridge', 'northridge_ocean.json') oceangridfile = os.path.join( homedir, '..', 'data', 'eventdata', 'northridge', 'northridge_ocean.bil') timezonefile = os.path.join( homedir, '..', 'data', 'eventdata', 'northridge', 'northridge_timezone.shp') invfile = os.path.join(homedir, '..', 'data', 'semi_inventory.hdf') colfile = os.path.join(homedir, '..', 'data', 'semi_collapse_mmi.hdf') casfile = os.path.join(homedir, '..', 'data', 'semi_casualty.hdf') workfile = os.path.join(homedir, '..', 'data', 'semi_workforce.hdf') tdir = tempfile.mkdtemp() basename = os.path.join(tdir, 'output') exp = Exposure(popfile, 2012, isofile) results = exp.calcExposure(shakefile) shakegrid = exp.getShakeGrid() popgrid = exp.getPopulationGrid() pdffile, pngfile, mapcities = draw_contour( shakegrid, popgrid, oceanfile, oceangridfile, cityfile, basename) shutil.rmtree(tdir) popyear = 2012 shake_tuple = getHeaderData(shakefile) tsunami = shake_tuple[1]['magnitude'] >= TSUNAMI_MAG_THRESH semi = SemiEmpiricalFatality.fromDefault() semi.setGlobalFiles(popfile, popyear, urbanfile, isofile) semiloss, resfat, nonresfat = semi.getLosses(shakefile) popgrowth = PopulationGrowth.fromDefault() econexp = EconExposure(popfile, 2012, isofile) fatmodel = EmpiricalLoss.fromDefaultFatality() expobject = Exposure(popfile, 2012, isofile, popgrowth) expdict = expobject.calcExposure(shakefile) fatdict = fatmodel.getLosses(expdict) econexpdict = econexp.calcExposure(shakefile) ecomodel = EmpiricalLoss.fromDefaultEconomic() ecodict = ecomodel.getLosses(expdict) shakegrid = econexp.getShakeGrid() pagerversion = 1 cities = Cities.loadFromGeoNames(cityfile) impact1 = '''Red alert level for economic losses. Extensive damage is probable and the disaster is likely widespread. Estimated economic losses are less than 1% of GDP of Italy. Past events with this alert level have required a national or international level response.''' impact2 = '''Orange alert level for shaking-related fatalities. Significant casualties are likely.''' structcomment = '''Overall, the population in this region resides in structures that are a mix of vulnerable and earthquake resistant construction. The predominant vulnerable building types are unreinforced brick with mud and mid-rise nonductile concrete frame with infill construction.''' histeq = [1, 2, 3] struct_comment = '''Overall, the population in this region resides in structures that are resistant to earthquake shaking, though some vulnerable structures exist.''' secondary_comment = '''Recent earthquakes in this area have caused secondary hazards such as landslides that might have contributed to losses.''' hist_comment = ''''A magnitude 7.1 earthquake 240 km east of this event struck Reventador: Ecuador on March 6, 1987 (UTC), with estimated population exposures of 14,000 at intensity VIII and 2,000 at intensity IX or greater, resulting in a reported 5,000 fatalities.'''.replace('\n', '') location = 'At the top of the world.' is_released = True doc = PagerData() eventcode = shakegrid.getEventDict()['event_id'] versioncode = eventcode doc.setInputs(shakegrid, timezonefile, pagerversion, versioncode, eventcode, tsunami, location, is_released) doc.setExposure(expdict, econexpdict) doc.setModelResults(fatmodel, ecomodel, fatdict, ecodict, semiloss, resfat, nonresfat) doc.setComments(impact1, impact2, struct_comment, hist_comment, secondary_comment) doc.setMapInfo(cityfile, mapcities) doc.validate() # let's test the property methods tdoc(doc, shakegrid, impact1, impact2, expdict, struct_comment, hist_comment) # see if we can save this to a bunch of files then read them back in try: tdir = tempfile.mkdtemp() doc.saveToJSON(tdir) newdoc = PagerData() newdoc.loadFromJSON(tdir) tdoc(newdoc, shakegrid, impact1, impact2, expdict, struct_comment, hist_comment) # test the xml saving method xmlfile = doc.saveToLegacyXML(tdir) except Exception as e: assert 1 == 2 finally: shutil.rmtree(tdir)
def test(): event = 'northridge' homedir = os.path.dirname( os.path.abspath(__file__)) #where is this script? xmlfile = os.path.join(homedir, '..', 'data', 'economy.xml') growthfile = os.path.join(homedir, '..', 'data', 'WPP2015_POP_F02_POPULATION_GROWTH_RATE.xls') gdpfile = os.path.join(homedir, '..', 'data', 'API_NY.GDP.PCAP.CD_DS2_en_excel_v2.xls') shakefile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_grid.xml' % event) popfile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_gpw.flt' % event) isofile = os.path.join(homedir, '..', 'data', 'eventdata', event, '%s_isogrid.bil' % event) shapefile = os.path.join(homedir, '..', 'data', 'eventdata', event, 'City_BoundariesWGS84', 'City_Boundaries.shp') print('Test loading economic exposure from inputs...') popgrowth = PopulationGrowth.fromDefault() econexp = EconExposure(popfile, 2012, isofile) print('Passed loading economic exposure from inputs...') print('Test loading empirical fatality model from XML file...') ecomodel = EmpiricalLoss.fromDefaultEconomic() print('Passed loading empirical fatality model from XML file.') print('Testing calculating probabilities for standard PAGER ranges...') expected = {'UK': 6819.883892 * 1e6} G = 2.5 probs = ecomodel.getProbabilities(expected, G) testprobs = { '0-1': 0.00020696841425738358, '1-10': 0.0043200811319132086, '10-100': 0.041085446477813294, '100-1000': 0.17564981840854255, '1000-10000': 0.33957681768639003, '10000-100000': 0.29777890303065313, '100000-10000000': 0.14138196485040311 } for key, value in probs.items(): np.testing.assert_almost_equal(value, testprobs[key]) print( 'Passed combining G values from all countries that contributed to losses...' ) print('Test retrieving economic model data from XML file...') model = ecomodel.getModel('af') testmodel = LognormalModel('dummy', 9.013810, 0.100000, 4.113200, alpha=15.065400) assert model == testmodel print('Passed retrieving economic model data from XML file.') print('Testing with known exposures/losses for 1994 Northridge EQ...') exposure = { 'xf': np.array([ 0, 0, 556171936.807, 718990717350.0, 2.40385709638e+12, 2.47073141687e+12, 1.2576210799e+12, 698888019337.0, 1913733716.16, 0.0 ]) } expodict = ecomodel.getLosses(exposure) testdict = {'xf': 25945225582} assert expodict['xf'] == testdict['xf'] print( 'Passed testing with known exposures/fatalities for 1994 Northridge EQ.' ) print('Testing calculating total economic losses for Northridge...') expdict = econexp.calcExposure(shakefile) ecomodel = EmpiricalLoss.fromDefaultEconomic() lossdict = ecomodel.getLosses(expdict) testdict = {'XF': 23172277187} assert lossdict['XF'] == testdict['XF'] print('Passed calculating total economic losses for Northridge...') print('Testing creating a economic loss grid...') mmidata = econexp.getShakeGrid().getLayer('mmi').getData() popdata = econexp.getEconPopulationGrid().getData() isodata = econexp.getCountryGrid().getData() ecogrid = ecomodel.getLossGrid(mmidata, popdata, isodata) ecosum = 23172275857.094917 assert np.nansum(ecogrid) == ecosum print('Passed creating a economic loss grid.') print('Testing assigning economic losses to polygons...') popdict = econexp.getPopulationGrid().getGeoDict() shapes = [] f = fiona.open(shapefile, 'r') for row in f: shapes.append(row) f.close() ecoshapes, toteco = ecomodel.getLossByShapes(mmidata, popdata, isodata, shapes, popdict) ecoshapes = sorted(ecoshapes, key=lambda shape: shape['properties']['dollars_lost'], reverse=True) lalosses = 17323352577 for shape in ecoshapes: if shape['id'] == '312': #Los Angeles cname = shape['properties']['CITY_NAME'] dollars = shape['properties']['dollars_lost'] assert lalosses == dollars assert cname == 'Los Angeles' print('Passed assigning economic losses to polygons...')
def test(): homedir = os.path.dirname(os.path.abspath(__file__)) #where is this script? fatfile = os.path.join(homedir,'..','data','fatality.xml') ecofile = os.path.join(homedir,'..','data','economy.xml') cityfile = os.path.join(homedir,'..','data','cities1000.txt') event = 'northridge' shakefile = os.path.join(homedir,'..','data','eventdata',event,'%s_grid.xml' % event) popfile = os.path.join(homedir,'..','data','eventdata',event,'%s_gpw.flt' % event) isofile = os.path.join(homedir,'..','data','eventdata',event,'%s_isogrid.bil' % event) urbanfile = os.path.join(homedir,'..','data','eventdata','northridge','northridge_urban.bil') oceanfile = os.path.join(homedir,'..','data','eventdata','northridge','northridge_ocean.json') invfile = os.path.join(homedir,'..','data','semi_inventory.hdf') colfile = os.path.join(homedir,'..','data','semi_collapse_mmi.hdf') casfile = os.path.join(homedir,'..','data','semi_casualty.hdf') workfile = os.path.join(homedir,'..','data','semi_workforce.hdf') tdir = tempfile.mkdtemp() outfile = os.path.join(tdir,'output.pdf') pngfile,mapcities = draw_contour(shakefile,popfile,oceanfile,cityfile,outfile,make_png=True) shutil.rmtree(tdir) popyear = 2012 semi = SemiEmpiricalFatality.fromDefault() semi.setGlobalFiles(popfile,popyear,urbanfile,isofile) semiloss,resfat,nonresfat = semi.getLosses(shakefile) popgrowth = PopulationGrowth.fromDefault() econexp = EconExposure(popfile,2012,isofile) fatmodel = EmpiricalLoss.fromDefaultFatality() expobject = Exposure(popfile,2012,isofile,popgrowth) expdict = expobject.calcExposure(shakefile) fatdict = fatmodel.getLosses(expdict) econexpdict = econexp.calcExposure(shakefile) ecomodel = EmpiricalLoss.fromDefaultEconomic() ecodict = ecomodel.getLosses(expdict) shakegrid = econexp.getShakeGrid() pagerversion = 1 cities = Cities.loadFromGeoNames(cityfile) impact1 = '''Red alert level for economic losses. Extensive damage is probable and the disaster is likely widespread. Estimated economic losses are less than 1% of GDP of Italy. Past events with this alert level have required a national or international level response.''' impact2 = '''Orange alert level for shaking-related fatalities. Significant casualties are likely.''' structcomment = '''Overall, the population in this region resides in structures that are a mix of vulnerable and earthquake resistant construction. The predominant vulnerable building types are unreinforced brick with mud and mid-rise nonductile concrete frame with infill construction.''' histeq = [1,2,3] struct_comment = '''Overall, the population in this region resides in structures that are resistant to earthquake shaking, though some vulnerable structures exist.''' secondary_comment = '''Recent earthquakes in this area have caused secondary hazards such as landslides that might have contributed to losses.''' hist_comment = ''''A magnitude 7.1 earthquake 240 km east of this event struck Reventador: Ecuador on March 6, 1987 (UTC), with estimated population exposures of 14,000 at intensity VIII and 2,000 at intensity IX or greater, resulting in a reported 5,000 fatalities.'''.replace('\n','') doc = PagerData() doc.setInputs(shakegrid,pagerversion,shakegrid.getEventDict()['event_id']) doc.setExposure(expdict,econexpdict) doc.setModelResults(fatmodel,ecomodel, fatdict,ecodict, semiloss,resfat,nonresfat) doc.setComments(impact1,impact2,struct_comment,hist_comment,secondary_comment) doc.setMapInfo(cityfile,mapcities) doc.validate() eventinfo = doc.getEventInfo() assert eventinfo['mag'] == shakegrid.getEventDict()['magnitude'] imp1,imp2 = doc.getImpactComments() assert imp1 == impact1 and imp2 == impact2 version = doc.getSoftwareVersion() elapsed = doc.getElapsed() exp = doc.getTotalExposure() assert np.isclose(np.array(exp),expdict['TotalExposure']).all() hist_table = doc.getHistoricalTable() assert hist_table[0]['EventID'] == '199206281505' scomm = doc.getStructureComment() assert scomm == struct_comment hcomm = doc.getHistoricalComment() assert hcomm == hist_comment citytable = doc.getCityTable() assert citytable.iloc[0]['name'] == 'Santa Clarita' summary = doc.getSummaryAlert() assert summary == 'yellow'