def setUp(self): ''' Set up test class ''' filename = os.path.join(BASE_DATA_PATH,'completeness_test_cat.csv') parser0 = CsvCatalogueParser(filename) self.catalogue = parser0.read_file() self.config = {'b-value': 1.0, 'input_mmin': 5.0, 'input_mmax': None, 'tolerance': 0.001, 'maximum_iterations': 1000} self.model = KijkoSellevolFixedb()
def setUp(self): ''' Set up test class ''' parser0 = CsvCatalogueParser(TEST_CAT_1) self.catalogue = parser0.read_file() self.config = {'b-value': 1.0, 'input_mmin': 5.0, 'input_mmax': None, 'tolerance': 0.001, 'maximum_iterations': 1000} self.model = KijkoSellevolFixedb()
# Plot magnitude time density from hmtk.plotting.seismicity.catalogue_plots import plot_magnitude_time_density magnitude_bin = 0.2 time_bin = 10.0 plot_magnitude_time_density(catalogue, magnitude_bin, time_bin) # In[ ]: mmax_config = {'b-value': 1.0, 'input_mmin': 4.5, 'input_mmax': None, 'input_mmax_uncertainty': 0.5} mmax_ks = KijkoSellevolFixedb() mmax, mmax_sigma = mmax_ks.get_mmax(catalogue, mmax_config) print 'Mmax = %8.3f +/- %8.3f' %(mmax, mmax_sigma) # In[ ]: mmax_config = {'b-value': 1.0, 'sigma-b': 0.05, 'input_mmin': 4.5, 'input_mmax': None, 'input_mmax_uncertainty': 0.5} mmax_ksb = KijkoSellevolBayes()
class TestKijkoSellevolFixedb(unittest.TestCase): ''' Test suite for the Kijko & Sellevol fixed b-value estimator of Mmax ''' def setUp(self): ''' Set up test class ''' parser0 = CsvCatalogueParser(TEST_CAT_1) self.catalogue = parser0.read_file() self.config = {'b-value': 1.0, 'input_mmin': 5.0, 'input_mmax': None, 'tolerance': 0.001, 'maximum_iterations': 1000} self.model = KijkoSellevolFixedb() def test_integral_function(self): ''' Tests the integral of the Kijko & Sellevol fixed-b estimator define in Equation 6 of Kijko (2004) ''' # Simple test case 1 - all good parameters mmax = 8.5 mmin = 5.0 mval = 6.5 beta = np.log(10.) neq = 100. self.assertAlmostEqual(self.model._ks_intfunc(mval, neq, mmax, mmin, beta), 0.04151379) # Simple test case 2 - Mmin == Mmax (returns inf) mmin = 6.0 mmax = 6.0 self.assertTrue(np.isinf(self.model._ks_intfunc(mval, neq, mmax, mmin, beta))) # Test case 3 - Mmin > MMax (raises value Error) mmin = 6.1 with self.assertRaises(ValueError) as ae: _ = self.model._ks_intfunc(mval, neq, mmax, mmin, beta) self.assertEqual(ae.exception.message, 'Maximum magnitude smaller than minimum magnitude' ' in Kijko & Sellevol (Fixed-b) integral') # Test case 4 - Number of earthquakes is 0 mmax = 8.5 mmin = 5.0 neq = 0. self.assertAlmostEqual(1.0, self.model._ks_intfunc(mval, neq, mmax, mmin, beta)) # Test case 5 - beta is negative neq = 100. self.assertAlmostEqual(0.0, self.model._ks_intfunc(mval, neq, mmax, mmin, -0.5)) def test_get_mmin(self): ''' Tests the main method to calculate Mmax BEHAVIOUR NOTE 1: the estimator of mmax is dependent on the mmin If mmin < mmin_observed then the integral will not reach stability (or give rubbish) therefore if the mmin specified in the config is less than mmin_obs it will be overwritten by mmin_observed BEHAVIOUR NOTE 2: Negative or very small b-values (< 1E-16) will result in immediate stability of the integral, thus giving mmax == mmax_obs. If b-value == 0 then will give a divide by zero warning ''' # Test good working case b = 1, mmin = 5.0 mmax, sigma_mmax = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax, 7.6994981) self.assertAlmostEqual(sigma_mmax, 0.31575163) # Test case with mmin < min(magnitude) (b = 1) # Gives very high value of mmax self.config['input_mmin'] = 4.0 mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, 8.35453605) self.assertAlmostEqual(sigma_mmax_1, 0.959759906) self.config['input_mmin'] = 3.0 mmax_2, sigma_mmax_2 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, mmax_2) # Test case with b-value = 0 self.config['input_mmin'] = 5.0 self.config['b-value'] = 0.0 mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, 7.4) self.assertAlmostEqual(sigma_mmax_1, 0.1) # With b < 0, results should be the same as b = 0. self.config['b-value'] = 0.0 mmax_2, sigma_mmax_2 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_2, mmax_1) self.assertAlmostEqual(sigma_mmax_2, sigma_mmax_1) # Case where the maximum magnitude is overriden self.config['input_mmax'] = 7.6 self.config['b-value'] = 1.0 self.config['input_mmax_uncertainty'] = 0.2 mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, 8.1380422) self.assertAlmostEqual(sigma_mmax_1, 0.57401164)
class TestKijkoSellevolFixedb(unittest.TestCase): ''' Test suite for the Kijko & Sellevol fixed b-value estimator of Mmax ''' def setUp(self): ''' Set up test class ''' filename = os.path.join(BASE_DATA_PATH, 'completeness_test_cat.csv') parser0 = CsvCatalogueParser(filename) self.catalogue = parser0.read_file() self.config = { 'b-value': 1.0, 'input_mmin': 5.0, 'input_mmax': None, 'tolerance': 0.001, 'maximum_iterations': 1000 } self.model = KijkoSellevolFixedb() def test_integral_function(self): # Tests the integral of the Kijko & Sellevol fixed-b estimator # define in Equation 6 of Kijko (2004) # Simple test case 1 - all good parameters mmax = 8.5 mmin = 5.0 mval = 6.5 beta = np.log(10.) neq = 100. self.assertAlmostEqual( self.model._ks_intfunc(mval, neq, mmax, mmin, beta), 0.04151379) # Test case 4 - Number of earthquakes is 0 mmax = 8.5 mmin = 5.0 neq = 0. self.assertAlmostEqual( 1.0, self.model._ks_intfunc(mval, neq, mmax, mmin, beta)) # Test case 5 - beta is negative neq = 100. self.assertAlmostEqual( 0.0, self.model._ks_intfunc(mval, neq, mmax, mmin, -0.5)) def test_get_mmin(self): # Tests the main method to calculate Mmax # BEHAVIOUR NOTE 1: the estimator of mmax is dependent on the mmin # If mmin < mmin_observed then the integral will not reach stability # (or give rubbish) therefore if the mmin specified in the config # is less than mmin_obs it will be overwritten by mmin_observed # BEHAVIOUR NOTE 2: Negative or very small b-values (< 1E-16) will # result in immediate stability of the integral, thus giving # mmax == mmax_obs. # If b-value == 0 then will give a divide by zero warning # Test good working case b = 1, mmin = 5.0 mmax, sigma_mmax = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax, 7.6994981) self.assertAlmostEqual(sigma_mmax, 0.31575163) # Test case with mmin < min(magnitude) (b = 1) # Gives very high value of mmax self.config['input_mmin'] = 4.0 mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, 8.35453605) self.assertAlmostEqual(sigma_mmax_1, 0.959759906) self.config['input_mmin'] = 3.0 mmax_2, sigma_mmax_2 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, mmax_2) # Case where the maximum magnitude is overriden # self.config['input_mmax'] = 7.6 # self.config['b-value'] = 1.0 # self.config['input_mmax_uncertainty'] = 0.2 # mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) # self.assertAlmostEqual(mmax_1, 8.1380422) # self.assertAlmostEqual(sigma_mmax_1, 0.57401164) def test_raise_runTimeWarning(self): """Test case with b-value = 0 """ self.config['input_mmin'] = 5.0 self.config['b-value'] = 0.0 with warnings.catch_warnings(record=True) as cm: self.model.get_mmax(self.catalogue, self.config) assert len(cm) > 0 def test_raise_valueError(self): # Simple test case 2 - Mmin == Mmax (returns inf) mmin = 6.0 mmax = 6.0 mval = 6.5 beta = np.log(10.) neq = 100. with self.assertRaises(ValueError) as cm: self.model._ks_intfunc(mval, neq, mmax, mmin, beta) self.assertEqual( str(cm.exception), 'Maximum magnitude smaller than minimum magnitude' ' in Kijko & Sellevol (Fixed-b) integral') def test_raise_valueError_1(self): # Test case 3 - Mmin > MMax (raises value Error) mmin = 6.2 mmax = 6.0 mval = 6.5 beta = np.log(10.) neq = 100. with self.assertRaises(ValueError) as ae: self.model._ks_intfunc(mval, neq, mmax, mmin, beta) exception = ae.exception self.assertEqual( str(exception), 'Maximum magnitude smaller than minimum magnitude' ' in Kijko & Sellevol (Fixed-b) integral')
# Plot magnitude time density from hmtk.plotting.seismicity.catalogue_plots import plot_magnitude_time_density magnitude_bin = 0.2 time_bin = 10.0 plot_magnitude_time_density(catalogue, magnitude_bin, time_bin) # In[ ]: mmax_config = { 'b-value': 1.0, 'input_mmin': 4.5, 'input_mmax': None, 'input_mmax_uncertainty': 0.5 } mmax_ks = KijkoSellevolFixedb() mmax, mmax_sigma = mmax_ks.get_mmax(catalogue, mmax_config) print 'Mmax = %8.3f +/- %8.3f' % (mmax, mmax_sigma) # In[ ]: mmax_config = { 'b-value': 1.0, 'sigma-b': 0.05, 'input_mmin': 4.5, 'input_mmax': None, 'input_mmax_uncertainty': 0.5 }
class TestKijkoSellevolFixedb(unittest.TestCase): ''' Test suite for the Kijko & Sellevol fixed b-value estimator of Mmax ''' def setUp(self): ''' Set up test class ''' filename = os.path.join(BASE_DATA_PATH, 'completeness_test_cat.csv') parser0 = CsvCatalogueParser(filename) self.catalogue = parser0.read_file() self.config = {'b-value': 1.0, 'input_mmin': 5.0, 'input_mmax': None, 'tolerance': 0.001, 'maximum_iterations': 1000} self.model = KijkoSellevolFixedb() def test_integral_function(self): # Tests the integral of the Kijko & Sellevol fixed-b estimator # define in Equation 6 of Kijko (2004) # Simple test case 1 - all good parameters mmax = 8.5 mmin = 5.0 mval = 6.5 beta = np.log(10.) neq = 100. self.assertAlmostEqual(self.model._ks_intfunc(mval, neq, mmax, mmin, beta), 0.04151379) # Test case 4 - Number of earthquakes is 0 mmax = 8.5 mmin = 5.0 neq = 0. self.assertAlmostEqual(1.0, self.model._ks_intfunc(mval, neq, mmax, mmin, beta)) # Test case 5 - beta is negative neq = 100. self.assertAlmostEqual( 0.0, self.model._ks_intfunc(mval, neq, mmax, mmin, -0.5)) def test_get_mmin(self): # Tests the main method to calculate Mmax # BEHAVIOUR NOTE 1: the estimator of mmax is dependent on the mmin # If mmin < mmin_observed then the integral will not reach stability # (or give rubbish) therefore if the mmin specified in the config # is less than mmin_obs it will be overwritten by mmin_observed # BEHAVIOUR NOTE 2: Negative or very small b-values (< 1E-16) will # result in immediate stability of the integral, thus giving # mmax == mmax_obs. # If b-value == 0 then will give a divide by zero warning # Test good working case b = 1, mmin = 5.0 mmax, sigma_mmax = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax, 7.6994981) self.assertAlmostEqual(sigma_mmax, 0.31575163) # Test case with mmin < min(magnitude) (b = 1) # Gives very high value of mmax self.config['input_mmin'] = 4.0 mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, 8.35453605) self.assertAlmostEqual(sigma_mmax_1, 0.959759906) self.config['input_mmin'] = 3.0 mmax_2, sigma_mmax_2 = self.model.get_mmax(self.catalogue, self.config) self.assertAlmostEqual(mmax_1, mmax_2) # Case where the maximum magnitude is overriden # self.config['input_mmax'] = 7.6 # self.config['b-value'] = 1.0 # self.config['input_mmax_uncertainty'] = 0.2 # mmax_1, sigma_mmax_1 = self.model.get_mmax(self.catalogue, self.config) # self.assertAlmostEqual(mmax_1, 8.1380422) # self.assertAlmostEqual(sigma_mmax_1, 0.57401164) def test_raise_runTimeWarning(self): """Test case with b-value = 0 """ self.config['input_mmin'] = 5.0 self.config['b-value'] = 0.0 with warnings.catch_warnings(record=True) as cm: self.model.get_mmax(self.catalogue, self.config) assert len(cm) > 0 def test_raise_valueError(self): # Simple test case 2 - Mmin == Mmax (returns inf) mmin = 6.0 mmax = 6.0 mval = 6.5 beta = np.log(10.) neq = 100. with self.assertRaises(ValueError) as cm: self.model._ks_intfunc(mval, neq, mmax, mmin, beta) self.assertEqual(str(cm.exception), 'Maximum magnitude smaller than minimum magnitude' ' in Kijko & Sellevol (Fixed-b) integral') def test_raise_valueError_1(self): # Test case 3 - Mmin > MMax (raises value Error) mmin = 6.2 mmax = 6.0 mval = 6.5 beta = np.log(10.) neq = 100. with self.assertRaises(ValueError) as ae: self.model._ks_intfunc(mval, neq, mmax, mmin, beta) exception = ae.exception self.assertEqual(str(exception), 'Maximum magnitude smaller than minimum magnitude' ' in Kijko & Sellevol (Fixed-b) integral')