def setUp(self):
     """
     Open the hdf5 file
     """
     self.fle = h5py.File(self.TABLE_FILE)
     self.amp_table = AmplificationTable(self.fle["Amplification"],
                                         self.fle["Mw"][:],
                                         self.fle["Distances"][:])
 def setUp(self):
     """
     Open the hdf5 file
     """
     self.fle = h5py.File(self.TABLE_FILE)
     self.amp_table = AmplificationTable(self.fle["Amplification"],
                                         self.fle["Mw"][:],
                                         self.fle["Distances"][:])
 def test_unsupported_parameter(self):
     """
     Tests instantiation with a bad input
     """
     with self.assertRaises(ValueError) as ve:
         AmplificationTable(self.fle["Amplification"], None, None)
     self.assertEqual(str(ve.exception),
                      "Amplification parameter Bad Value not recognised!")
class AmplificationTableSiteTestCase(unittest.TestCase):
    """
    Tests the amplification tables for a site parameter
    """
    TABLE_FILE = os.path.join(BASE_DATA_PATH, "model_amplification_site.hdf5")
    IDX = 0

    def setUp(self):
        """
        Open the hdf5 file
        """
        self.fle = h5py.File(self.TABLE_FILE)
        self.amp_table = AmplificationTable(self.fle["Amplification"],
                                            self.fle["Mw"][:],
                                            self.fle["Distances"][:])

    def test_instantiation(self):
        """
        Tests the setup and loading of data from file to memory
        """

        # Check setup
        # 1. Shape
        self.assertTupleEqual(self.amp_table.shape, (10, 3, 5, 2))
        # 2. Parameter
        self.assertEqual(self.amp_table.parameter, "vs30")
        # 3. Element
        self.assertEqual(self.amp_table.element, "Sites")
        # 4. Interpolation values
        np.testing.assert_array_almost_equal(self.amp_table.values,
                                             np.array([400.0, 1000.0]))
        # 5. Periods
        np.testing.assert_array_almost_equal(self.amp_table.periods,
                                             np.array([0.1, 0.5, 1.0]))
        # 6. Means and Standard Deviations
        expected_mean, expected_sigma = self._build_mean_and_stddev_table()
        for key in self.amp_table.mean:
            np.testing.assert_array_almost_equal(self.amp_table.mean[key],
                                                 expected_mean[key])
            np.testing.assert_array_almost_equal(
                self.amp_table.sigma["Total"][key],
                expected_sigma["Total"][key])

    def _build_mean_and_stddev_table(self):
        """
        Builds the expected mean and standard deviation tables
        """
        expected_means = {
            "PGA": np.ones([10, 1, 5, 2]),
            "PGV": np.ones([10, 1, 5, 2]),
            "SA": np.ones([10, 3, 5, 2])
        }
        # For second level revise values
        expected_means["PGA"][:, :, :, self.IDX] *= 1.5
        expected_means["PGV"][:, :, :, self.IDX] *= 0.5
        expected_means["SA"][:, 0, :, self.IDX] *= 1.5
        expected_means["SA"][:, 1, :, self.IDX] *= 2.0
        expected_means["SA"][:, 2, :, self.IDX] *= 0.5
        expected_sigma = {
            const.StdDev.TOTAL: {
                "PGA": np.ones([10, 1, 5, 2]),
                "PGV": np.ones([10, 1, 5, 2]),
                "SA": np.ones([10, 3, 5, 2])
            }
        }
        expected_sigma[const.StdDev.TOTAL]["PGA"][:, :, :, self.IDX] *= 0.8
        expected_sigma[const.StdDev.TOTAL]["PGV"][:, :, :, self.IDX] *= 0.8
        expected_sigma[const.StdDev.TOTAL]["SA"][:, :, :, self.IDX] *= 0.8
        return expected_means, expected_sigma

    def test_get_set(self):
        """
        Test that the set function operates correctly
        """
        self.assertSetEqual(self.amp_table.get_set(), {"vs30"})

    def test_get_mean_table(self, idx=0):
        """
        Test the retrieval of the mean amplification tables for a given
        magnitude and IMT
        """
        rctx = RuptureContext()
        rctx.mag = 6.0
        # PGA
        expected_table = np.ones([10, 2])
        expected_table[:, self.IDX] *= 1.5
        np.testing.assert_array_almost_equal(
            self.amp_table.get_mean_table(imt_module.PGA(), rctx),
            expected_table)
        # SA
        expected_table[:, self.IDX] = 2.0 * np.ones(10)
        np.testing.assert_array_almost_equal(
            self.amp_table.get_mean_table(imt_module.SA(0.5), rctx),
            expected_table)
        # SA (period interpolation)
        interpolator = interp1d(np.log10(self.amp_table.periods),
                                np.log10(np.array([1.5, 2.0, 0.5])))
        period = 0.3
        expected_table[:, self.IDX] = (10.0**interpolator(
            np.log10(period))) * np.ones(10.)
        np.testing.assert_array_almost_equal(
            self.amp_table.get_mean_table(imt_module.SA(period), rctx),
            expected_table)

    def test_get_sigma_table(self):
        """
        Test the retrieval of the standard deviation modification tables
        for a given magnitude and IMT
        """
        rctx = RuptureContext()
        rctx.mag = 6.0
        # PGA
        expected_table = np.ones([10, 2])
        expected_table[:, self.IDX] *= 0.8
        stddevs = ["Total"]
        pga_table = self.amp_table.get_sigma_tables(imt_module.PGA(), rctx,
                                                    stddevs)[0]
        np.testing.assert_array_almost_equal(pga_table, expected_table)
        # SA (for coverage)
        sa_table = self.amp_table.get_sigma_tables(imt_module.SA(0.3), rctx,
                                                   stddevs)[0]
        np.testing.assert_array_almost_equal(sa_table, expected_table)

    def test_get_amplification_factors(self):
        """
        Tests the amplification tables
        """
        rctx = RuptureContext()
        rctx.mag = 6.0
        dctx = DistancesContext()
        # Takes distances at the values found in the table (not checking
        # distance interpolation)
        dctx.rjb = np.copy(self.amp_table.distances[:, 0, 0])
        # Test Vs30 is 700.0 m/s midpoint between the 400 m/s and 1000 m/s
        # specified in the table
        sctx = SitesContext()
        sctx.vs30 = 700.0 * np.ones_like(dctx.rjb)
        stddevs = [const.StdDev.TOTAL]
        expected_mean = np.ones_like(dctx.rjb)
        expected_sigma = np.ones_like(dctx.rjb)
        # Check PGA and PGV
        mean_amp, sigma_amp = self.amp_table.get_amplification_factors(
            imt_module.PGA(), sctx, rctx, dctx.rjb, stddevs)
        np.testing.assert_array_almost_equal(
            mean_amp,
            midpoint(1.0, 1.5) * expected_mean)
        np.testing.assert_array_almost_equal(sigma_amp[0], 0.9 * expected_mean)
        mean_amp, sigma_amp = self.amp_table.get_amplification_factors(
            imt_module.PGV(), sctx, rctx, dctx.rjb, stddevs)
        np.testing.assert_array_almost_equal(
            mean_amp,
            midpoint(1.0, 0.5) * expected_mean)
        np.testing.assert_array_almost_equal(sigma_amp[0], 0.9 * expected_mean)
        # Sa (0.5)
        mean_amp, sigma_amp = self.amp_table.get_amplification_factors(
            imt_module.SA(0.5), sctx, rctx, dctx.rjb, stddevs)
        np.testing.assert_array_almost_equal(
            mean_amp,
            midpoint(1.0, 2.0) * expected_mean)
        np.testing.assert_array_almost_equal(sigma_amp[0], 0.9 * expected_mean)

    def tearDown(self):
        """
        Close the hdf5 file
        """
        self.fle.close()
class AmplificationTableSiteTestCase(unittest.TestCase):
    """
    Tests the amplification tables for a site parameter
    """
    TABLE_FILE = os.path.join(BASE_DATA_PATH, "model_amplification_site.hdf5")
    IDX = 0

    def setUp(self):
        """
        Open the hdf5 file
        """
        self.fle = h5py.File(self.TABLE_FILE)
        self.amp_table = AmplificationTable(self.fle["Amplification"],
                                            self.fle["Mw"][:],
                                            self.fle["Distances"][:])

    def test_instantiation(self):
        """
        Tests the setup and loading of data from file to memory
        """

        # Check setup
        # 1. Shape
        self.assertTupleEqual(self.amp_table.shape, (10, 3, 5, 2))
        # 2. Parameter
        self.assertEqual(self.amp_table.parameter, "vs30")
        # 3. Element
        self.assertEqual(self.amp_table.element, "Sites")
        # 4. Interpolation values
        np.testing.assert_array_almost_equal(self.amp_table.values,
                                             np.array([400.0, 1000.0]))
        # 5. Periods
        np.testing.assert_array_almost_equal(self.amp_table.periods,
                                             np.array([0.1, 0.5, 1.0]))
        # 6. Means and Standard Deviations
        expected_mean, expected_sigma = self._build_mean_and_stddev_table()
        for key in self.amp_table.mean:
            np.testing.assert_array_almost_equal(self.amp_table.mean[key],
                                                 expected_mean[key])
            np.testing.assert_array_almost_equal(
                self.amp_table.sigma["Total"][key],
                expected_sigma["Total"][key])

    def _build_mean_and_stddev_table(self):
        """
        Builds the expected mean and standard deviation tables
        """
        expected_means = {
            "PGA": np.ones([10, 1, 5, 2]),
            "PGV": np.ones([10, 1, 5, 2]),
            "SA": np.ones([10, 3, 5, 2])
            }
        # For second level revise values
        expected_means["PGA"][:, :, :, self.IDX] *= 1.5
        expected_means["PGV"][:, :, :, self.IDX] *= 0.5
        expected_means["SA"][:, 0, :, self.IDX] *= 1.5
        expected_means["SA"][:, 1, :, self.IDX] *= 2.0
        expected_means["SA"][:, 2, :, self.IDX] *= 0.5
        expected_sigma = {const.StdDev.TOTAL: {
            "PGA": np.ones([10, 1, 5, 2]),
            "PGV": np.ones([10, 1, 5, 2]),
            "SA": np.ones([10, 3, 5, 2])
            }}
        expected_sigma[const.StdDev.TOTAL]["PGA"][:, :, :, self.IDX] *= 0.8
        expected_sigma[const.StdDev.TOTAL]["PGV"][:, :, :, self.IDX] *= 0.8
        expected_sigma[const.StdDev.TOTAL]["SA"][:, :, :, self.IDX] *= 0.8
        return expected_means, expected_sigma

    def test_get_set(self):
        """
        Test that the set function operates correctly
        """
        self.assertSetEqual(self.amp_table.get_set(), {"vs30"})

    def test_get_mean_table(self, idx=0):
        """
        Test the retrieval of the mean amplification tables for a given
        magnitude and IMT
        """
        rctx = RuptureContext()
        rctx.mag = 6.0
        # PGA
        expected_table = np.ones([10, 2])
        expected_table[:, self.IDX] *= 1.5
        np.testing.assert_array_almost_equal(
            self.amp_table.get_mean_table(imt_module.PGA(), rctx),
            expected_table)
        # SA
        expected_table[:, self.IDX] = 2.0 * np.ones(10)
        np.testing.assert_array_almost_equal(
            self.amp_table.get_mean_table(imt_module.SA(0.5), rctx),
            expected_table)
        # SA (period interpolation)
        interpolator = interp1d(np.log10(self.amp_table.periods),
                                np.log10(np.array([1.5, 2.0, 0.5])))
        period = 0.3
        expected_table[:, self.IDX] = (
            10.0 ** interpolator(np.log10(period))) * np.ones(10.)
        np.testing.assert_array_almost_equal(
            self.amp_table.get_mean_table(imt_module.SA(period), rctx),
            expected_table)

    def test_get_sigma_table(self):
        """
        Test the retrieval of the standard deviation modification tables
        for a given magnitude and IMT
        """
        rctx = RuptureContext()
        rctx.mag = 6.0
        # PGA
        expected_table = np.ones([10, 2])
        expected_table[:, self.IDX] *= 0.8
        stddevs = ["Total"]
        pga_table = self.amp_table.get_sigma_tables(imt_module.PGA(),
                                                    rctx,
                                                    stddevs)[0]
        np.testing.assert_array_almost_equal(pga_table, expected_table)
        # SA (for coverage)
        sa_table = self.amp_table.get_sigma_tables(imt_module.SA(0.3),
                                                   rctx,
                                                   stddevs)[0]
        np.testing.assert_array_almost_equal(sa_table, expected_table)

    def test_get_amplification_factors(self):
        """
        Tests the amplification tables
        """
        rctx = RuptureContext()
        rctx.mag = 6.0
        dctx = DistancesContext()
        # Takes distances at the values found in the table (not checking
        # distance interpolation)
        dctx.rjb = np.copy(self.amp_table.distances[:, 0, 0])
        # Test Vs30 is 700.0 m/s midpoint between the 400 m/s and 1000 m/s
        # specified in the table
        sctx = SitesContext()
        sctx.vs30 = 700.0 * np.ones_like(dctx.rjb)
        stddevs = [const.StdDev.TOTAL]
        expected_mean = np.ones_like(dctx.rjb)
        expected_sigma = np.ones_like(dctx.rjb)
        # Check PGA and PGV
        mean_amp, sigma_amp = self.amp_table.get_amplification_factors(
            imt_module.PGA(), sctx, rctx, dctx.rjb, stddevs)
        np.testing.assert_array_almost_equal(
            mean_amp,
            midpoint(1.0, 1.5) * expected_mean)
        np.testing.assert_array_almost_equal(
            sigma_amp[0],
            0.9 * expected_mean)
        mean_amp, sigma_amp = self.amp_table.get_amplification_factors(
            imt_module.PGV(), sctx, rctx, dctx.rjb, stddevs)
        np.testing.assert_array_almost_equal(
            mean_amp,
            midpoint(1.0, 0.5) * expected_mean)
        np.testing.assert_array_almost_equal(
            sigma_amp[0],
            0.9 * expected_mean)
        # Sa (0.5)
        mean_amp, sigma_amp = self.amp_table.get_amplification_factors(
            imt_module.SA(0.5), sctx, rctx, dctx.rjb, stddevs)
        np.testing.assert_array_almost_equal(
            mean_amp,
            midpoint(1.0, 2.0) * expected_mean)
        np.testing.assert_array_almost_equal(
            sigma_amp[0],
            0.9 * expected_mean)

    def tearDown(self):
        """
        Close the hdf5 file
        """
        self.fle.close()