def test_correct_instantiation(self): """ Verify that the data is loaded successfully """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() np.testing.assert_array_almost_equal(gsim.distances, self.hdf5["Distances"][:]) np.testing.assert_array_almost_equal(gsim.m_w, self.hdf5["Mw"][:]) self.assertEqual(gsim.distance_type, "rjb") self.assertSetEqual(gsim.REQUIRES_SITES_PARAMETERS, set(("vs30",))) self.assertSetEqual(gsim.REQUIRES_DISTANCES, set(("rjb",))) self.assertSetEqual( gsim.DEFINED_FOR_INTENSITY_MEASURE_TYPES, set((imt_module.PGA, imt_module.PGV, imt_module.SA))) self.assertSetEqual(gsim.DEFINED_FOR_STANDARD_DEVIATION_TYPES, set((const.StdDev.TOTAL,))) # Verify correctly parsed IMLs and standard deviations for iml in ["PGA", "PGV", "SA", "T"]: np.testing.assert_array_almost_equal( gsim.imls[iml], self.hdf5["IMLs/" + iml][:]) np.testing.assert_array_almost_equal( gsim.stddevs["Total"][iml], self.hdf5["Total/" + iml][:])
def test_get_mean_and_stddevs(self): """ Tests mean and standard deviations without amplification """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) rctx = RuptureContext() rctx.mag = 6.0 dctx = DistancesContext() # Test values at the given distances and those outside range dctx.rjb = np.array([0.5, 1.0, 10.0, 100.0, 500.0]) sctx = SitesContext() stddevs = [const.StdDev.TOTAL] expected_mean = np.array([2.0, 2.0, 1.0, 0.5, 1.0E-20]) # PGA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGA(), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.5 * np.ones(5), 5) # SA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.SA(1.0), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.8 * np.ones(5), 5) # PGV mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGV(), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), 10. * expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.5 * np.ones(5), 5)
def test_get_mean_and_stddevs_good(self): """ Tests the full execution of the GMPE tables for valid data """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) rctx = RuptureContext() rctx.mag = 6.0 rctx.rake = 90.0 dctx = DistancesContext() # Test values at the given distances and those outside range dctx.rjb = np.array([0.5, 1.0, 10.0, 100.0, 500.0]) sctx = SitesContext() stddevs = [const.StdDev.TOTAL] expected_mean = np.array([20.0, 20.0, 10.0, 5.0, 1.0E-19]) # PGA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGA(), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.25 * np.ones(5), 5) # SA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.SA(1.0), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.4 * np.ones(5), 5)
def test_get_mean_and_stddevs_good(self): """ Tests the full execution of the GMPE tables for valid data """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() rctx = RuptureContext() rctx.mag = 6.0 rctx.rake = 90.0 dctx = DistancesContext() # Test values at the given distances and those outside range dctx.rjb = np.array([0.5, 1.0, 10.0, 100.0, 500.0]) sctx = SitesContext() stddevs = [const.StdDev.TOTAL] expected_mean = np.array([20.0, 20.0, 10.0, 5.0, 1.0E-19]) # PGA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGA(), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.25 * np.ones(5), 5) # SA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.SA(1.0), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.4 * np.ones(5), 5)
def test_instantiation_without_file(self): """ Tests the case when the GMPE table file is missing """ with self.assertRaises(IOError) as ioe: GMPETable(gmpe_table=None) self.assertEqual(str(ioe.exception), "GMPE Table Not Defined!") GMPETable(gmpe_table='/do/not/exists/table.hdf5') # no error here
def test_instantiation(self): """ Tests instantiation without amplification """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() self.assertIsNone(gsim.amplification) self.assertSetEqual(gsim.REQUIRES_SITES_PARAMETERS, set(())) self.assertSetEqual(gsim.REQUIRES_RUPTURE_PARAMETERS, set(("mag", )))
def test_instantiation_without_file(self): """ Tests the case when the GMPE table file is missing """ with self.assertRaises(ValueError) as err: GMPETable(gmpe_table=None).init() self.assertEqual(str(err.exception), "You forgot to set GMPETable.GMPE_TABLE!") GMPETable(gmpe_table='/do/not/exists/table.hdf5') # no error here
def test_instantiation(self): """ Tests instantiation without amplification """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() self.assertIsNone(gsim.amplification) self.assertSetEqual(gsim.REQUIRES_SITES_PARAMETERS, set(())) self.assertSetEqual(gsim.REQUIRES_RUPTURE_PARAMETERS, set(("mag",)))
def test_retreival_tables_good_no_interp(self): """ Tests the retreival of the IML tables for 'good' conditions without applying magnitude interpolations """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() # PGA np.testing.assert_array_almost_equal( gsim._return_tables(6.0, imt_module.PGA(), "IMLs"), np.array([2., 1., 0.5])) # PGV np.testing.assert_array_almost_equal( gsim._return_tables(6.0, imt_module.PGV(), "IMLs"), np.array([20., 10., 5.]), 5) # SA(1.0) np.testing.assert_array_almost_equal( gsim._return_tables(6.0, imt_module.SA(1.0), "IMLs"), np.array([2.0, 1., 0.5])) # Also for standard deviations np.testing.assert_array_almost_equal( gsim._return_tables(6.0, imt_module.PGA(), "Total"), 0.5 * np.ones(3)) np.testing.assert_array_almost_equal( gsim._return_tables(6.0, imt_module.SA(1.0), "Total"), 0.8 * np.ones(3))
def test_retreival_tables_outside_mag_range(self): """ Tests that an error is raised when inputting a magnitude value outside the supported range """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) with self.assertRaises(ValueError) as ve: gsim._return_tables(4.5, imt_module.PGA(), "IMLs") self.assertEqual( str(ve.exception), "Magnitude 4.50 outside of supported range (5.00 to 7.00)")
def test_retreival_tables_outside_period_range(self): """ Tests that an error is raised when inputting a period value outside the supported range """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) with self.assertRaises(ValueError) as ve: gsim._return_tables(6.0, imt_module.SA(2.5), "IMLs") self.assertEqual( str(ve.exception), "Spectral period 2.500 outside of valid range (0.100 to 2.000)")
def test_instantiation_without_file(self): """ Tests the case when the GMPE table file is missing """ with self.assertRaises(ValueError) as err: GMPETable(gmpe_table=None) self.assertEqual(str(err.exception), "You forgot to set GMPETable.GMPE_TABLE!") with self.assertRaises(OSError) as err: GMPETable(gmpe_table='/do/not/exists/table.hdf5') self.assertIn("No such file or directory", str(err.exception))
def test_retreival_tables_outside_mag_range(self): """ Tests that an error is raised when inputting a magnitude value outside the supported range """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() with self.assertRaises(ValueError) as ve: gsim._return_tables(4.5, imt_module.PGA(), "IMLs") self.assertEqual( str(ve.exception), "Magnitude 4.50 outside of supported range (5.00 to 7.00)")
def test_instantiation(self): """ Tests instantiation of class """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() expected_rupture_set = set(("mag", "rake")) self.assertSetEqual(gsim.REQUIRES_RUPTURE_PARAMETERS, expected_rupture_set) self.assertEqual(gsim.amplification.parameter, "rake") self.assertEqual(gsim.amplification.element, "Rupture") self.assertSetEqual(gsim.REQUIRES_SITES_PARAMETERS, set())
def test_retreival_tables_outside_period_range(self): """ Tests that an error is raised when inputting a period value outside the supported range """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() with self.assertRaises(ValueError) as ve: gsim._return_tables(6.0, imt_module.SA(2.5), "IMLs") self.assertEqual( str(ve.exception), "Spectral period 2.500 outside of valid range (0.100 to 2.000)")
def test_correct_instantiation(self): """ Verify that the data is loaded successfully """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) np.testing.assert_array_almost_equal(gsim.distances, self.hdf5["Distances"][:]) np.testing.assert_array_almost_equal(gsim.m_w, self.hdf5["Mw"][:]) self.assertEqual(gsim.distance_type, "rjb") self.assertSetEqual(gsim.REQUIRES_SITES_PARAMETERS, set(("vs30",))) self.assertSetEqual(gsim.REQUIRES_DISTANCES, set(("rjb",))) self.assertSetEqual( gsim.DEFINED_FOR_INTENSITY_MEASURE_TYPES, set((imt_module.PGA, imt_module.PGV, imt_module.SA))) self.assertSetEqual(gsim.DEFINED_FOR_STANDARD_DEVIATION_TYPES, set((const.StdDev.TOTAL,))) # Verify correctly parsed IMLs and standard deviations for iml in ["PGA", "PGV", "SA", "T"]: np.testing.assert_array_almost_equal( gsim.imls[iml], self.hdf5["IMLs/" + iml][:]) np.testing.assert_array_almost_equal( gsim.stddevs["Total"][iml], self.hdf5["Total/" + iml][:])
def check_gsim_list(gsim_list): """ Check the GSIM models or strings in `gsim_list`, and return a dict of gsim names (str) mapped to their :class:`openquake.hazardlib.Gsim`. Raises error if any Gsim in the list is supported in OpenQuake. If a Gsim is passed as instance, its string representation is inferred from the class name and optional arguments. If a Gsim is passed as string, the associated class name is fetched from the OpenQuake available Gsims. :param gsim_list: list of GSIM names (str) or OpenQuake Gsims :return: a dict of GSIM names (str) mapped to the associated GSIM """ output_gsims = {} for gs in gsim_list: if isinstance(gs, GMPE): output_gsims[_get_gmpe_name(gs)] = gs # get name of GMPE instance elif gs in AVAILABLE_GSIMS: output_gsims[gs] = AVAILABLE_GSIMS[gs]() else: match = _gmpetable_regex.match(gs) # GMPETable ? if match: filepath = match.group(1).split("=")[1] # get table filename output_gsims[gs] = GMPETable(gmpe_table=filepath) else: raise ValueError('%s Not supported by OpenQuake' % gs) return output_gsims
def test_instantiation(self): """ Runs both instantiation checks The table file contains data for Inter and intra event standard deviation, as well as an IMT that is not recognised by OpenQuake """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() expected_stddev_set = set( (const.StdDev.TOTAL, const.StdDev.INTER_EVENT, const.StdDev.INTRA_EVENT)) self.assertSetEqual(gsim.DEFINED_FOR_STANDARD_DEVIATION_TYPES, expected_stddev_set) expected_imt_set = set((imt_module.PGA, imt_module.PGV, imt_module.SA)) self.assertSetEqual(gsim.DEFINED_FOR_INTENSITY_MEASURE_TYPES, expected_imt_set)
def test_missing_periods(self): """ Tests missing period information """ with self.assertRaises(ValueError) as ve: GMPETable(gmpe_table=self.TABLE_FILE).init() self.assertEqual( str(ve.exception), "Spectral Acceleration must be accompanied by periods")
def test_instantiation(self): """ Runs both instantiation checks The table file contains data for Inter and intra event standard deviation, as well as an IMT that is not recognised by OpenQuake """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() expected_stddev_set = set((const.StdDev.TOTAL, const.StdDev.INTER_EVENT, const.StdDev.INTRA_EVENT)) self.assertSetEqual(gsim.DEFINED_FOR_STANDARD_DEVIATION_TYPES, expected_stddev_set) expected_imt_set = set((imt_module.PGA, imt_module.PGV, imt_module.SA)) self.assertSetEqual(gsim.DEFINED_FOR_INTENSITY_MEASURE_TYPES, expected_imt_set)
def test_get_mean_stddevs_unsupported_stddev(self): """ Tests the execution of the GMPE with an unsupported standard deviation type """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) rctx = RuptureContext() rctx.mag = 6.0 dctx = DistancesContext() # Test values at the given distances and those outside range dctx.rjb = np.array([0.5, 1.0, 10.0, 100.0, 500.0]) sctx = SitesContext() sctx.vs30 = 1000. * np.ones(5) stddevs = [const.StdDev.TOTAL, const.StdDev.INTER_EVENT] with self.assertRaises(ValueError) as ve: gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGA(), stddevs) self.assertEqual(str(ve.exception), "Standard Deviation type Inter event not supported")
def test_retreival_tables_good_interp(self): """ Tests the retreival of the IML tables for 'good' conditions with magnitude interpolations """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) expected_table_pgv = np.array([midpoint(20., 40.), midpoint(10., 20.), midpoint(5., 10.)]) np.testing.assert_array_almost_equal( gsim._return_tables(6.5, imt_module.PGV(), "IMLs"), expected_table_pgv, 5) expected_table_sa1 = np.array([midpoint(2., 4.), midpoint(1., 2.), midpoint(0.5, 1.)]) np.testing.assert_array_almost_equal( gsim._return_tables(6.5, imt_module.SA(1.0), "IMLs"), expected_table_sa1)
def test_retreival_tables_good_interp(self): """ Tests the retreival of the IML tables for 'good' conditions with magnitude interpolations """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() expected_table_pgv = np.array([midpoint(20., 40.), midpoint(10., 20.), midpoint(5., 10.)]) np.testing.assert_array_almost_equal( gsim._return_tables(6.5, imt_module.PGV(), "IMLs"), expected_table_pgv, 5) expected_table_sa1 = np.array([midpoint(2., 4.), midpoint(1., 2.), midpoint(0.5, 1.)]) np.testing.assert_array_almost_equal( gsim._return_tables(6.5, imt_module.SA(1.0), "IMLs"), expected_table_sa1)
def test_get_mean_stddevs_unsupported_stddev(self): """ Tests the execution of the GMPE with an unsupported standard deviation type """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() rctx = RuptureContext() rctx.mag = 6.0 dctx = DistancesContext() # Test values at the given distances and those outside range dctx.rjb = np.array([0.5, 1.0, 10.0, 100.0, 500.0]) sctx = SitesContext() sctx.vs30 = 1000. * np.ones(5) stddevs = [const.StdDev.TOTAL, const.StdDev.INTER_EVENT] with self.assertRaises(ValueError) as ve: gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGA(), stddevs) self.assertEqual(str(ve.exception), "Standard Deviation type Inter event not supported")
def test_get_mean_and_stddevs(self): """ Tests mean and standard deviations without amplification """ gsim = GMPETable(gmpe_table=self.TABLE_FILE) gsim.init() rctx = RuptureContext() rctx.mag = 6.0 dctx = DistancesContext() # Test values at the given distances and those outside range dctx.rjb = np.array([0.5, 1.0, 10.0, 100.0, 500.0]) sctx = SitesContext() stddevs = [const.StdDev.TOTAL] expected_mean = np.array([2.0, 2.0, 1.0, 0.5, 1.0E-20]) # PGA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGA(), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.5 * np.ones(5), 5) # SA mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.SA(1.0), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.8 * np.ones(5), 5) # PGV mean, sigma = gsim.get_mean_and_stddevs(sctx, rctx, dctx, imt_module.PGV(), stddevs) np.testing.assert_array_almost_equal(np.exp(mean), 10. * expected_mean, 5) np.testing.assert_array_almost_equal(sigma[0], 0.5 * np.ones(5), 5)
def _check_gsim_list(gsim_list): """ Checks the list of GSIM models and returns an instance of the openquake.hazardlib.gsim class. Raises error if GSIM is not supported in OpenQuake :param list gsim_list: List of GSIM names (str) :returns: Ordered dictionary of GMPE names and instances """ output_gsims = [] for gsim in gsim_list: if gsim.startswith("GMPETable"): # Get filename match = re.match(r'^GMPETable\(([^)]+?)\)$', gsim) filepath = match.group(1).split("=")[1] gmpe_table = GMPETable(gmpe_table=filepath[1:-1]) output_gsims.append((_get_gmpe_name(gmpe_table), gmpe_table)) elif not gsim in GSIM_LIST.keys(): raise ValueError('%s Not supported by OpenQuake' % gsim) else: output_gsims.append((gsim, GSIM_LIST[gsim]())) return OrderedDict(output_gsims)