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(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_amplified(self):
     """
     Tests the full execution of the GMPE tables for valid data with
     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()
     sctx.vs30 = 100. * np.ones(5)
     stddevs = [const.StdDev.TOTAL]
     expected_mean = np.array([20., 20., 10., 5., 1.0E-19])
     expected_sigma = 0.25 * np.ones(5)
     # 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], expected_sigma, 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(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_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(7.5, imt_module.PGA(), "IMLs")
     self.assertEqual(
         str(ve.exception),
         "Magnitude 7.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_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(7.5, imt_module.PGA(), "IMLs")
     self.assertEqual(
         str(ve.exception),
         "Magnitude 7.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)")
Example #9
0
    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!")

        with self.assertRaises(IOError) as ioe:
            GMPETable(gmpe_table='/do/not/exists/table.hdf5')
        self.assertEqual(str(ioe.exception),
                         "Missing file '/do/not/exists/table.hdf5'")
Example #10
0
 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 test_instantiation(self):
     """
     Tests instantiation without amplification
     """
     gsim = GMPETable(gmpe_table=self.TABLE_FILE)
     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 no GMPE table file is coded into the GMPE, nor
     is any provided - should raise an error
     """
     with self.assertRaises(IOError) as ioe:
         GMPETable(gmpe_table=None)
     self.assertEqual(str(ioe.exception), "GMPE Table Not Defined!")
 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)
     # 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_missing_periods(self):
     """
     Tests missing period information
     """
     with self.assertRaises(ValueError) as ve:
         gsim = GMPETable(gmpe_table=self.TABLE_FILE)
     self.assertEqual(
         str(ve.exception),
         "Spectral Acceleration must be accompanied by periods")
 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_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")
Example #18
0
 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_instantiation(self):
     """
     Tests instantiation of class
     """
     gsim = GMPETable(gmpe_table=self.TABLE_FILE)
     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_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)
     # 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_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)
     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)
Example #22
0
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)
    """
    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]
            output_gsims.append(GMPETable(gmpe_table=filepath))
        elif not gsim in AVAILABLE_GSIMS.keys():
            raise ValueError('%s Not supported by OpenQuake' % gsim)
        else:
            output_gsims.append(AVAILABLE_GSIMS[gsim]())
    return output_gsims
Example #23
0
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)