def test_residuals_execution(self): """ Tests basic execution of residuals - not correctness of values - to be the same when created from sm_database.GroundMotionDatabase and sm_table.GroundMotionTable """ residuals1 = res.Residuals(self.gsims, self.imts) residuals1.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(residuals1.residuals) stats1 = residuals1.get_residual_statistics() # yes, we said we do not check for correctness of values. # However, this simple check warn us in case # code modification will unexpectedly change residuals calculations: expected_res_obs = { 'PGA': np.array([ 2.02129254e-04, 1.05953800e-03, 1.89631889e-04, 7.53992832e-05 ]), 'SA(1.0)': np.array([0.00012647, 0.00073836, 0.00019964, 0.00014166]) } for imtx in expected_res_obs: assert np.allclose(residuals1.contexts[0]['Observations'][imtx], expected_res_obs[imtx], rtol=.5e-3) residuals2 = res.Residuals(self.gsims, self.imts) residuals2.get_residuals(self.dbtable, component="Geometric") self._check_residual_dictionary_correctness(residuals2.residuals) stats2 = residuals2.get_residual_statistics() self._check_new_context_vs_old(residuals1.contexts, residuals2.contexts) self._cmp_objects(stats1, stats2)
def tests_residual_plotter(self, mock_pyplot, mock_pyplot_subplot): """ Tests basic execution of residual plot. Simply tests pyplot show is called by mocking its `show` method """ # setup a mock which will handle all calls to matplotlib Axes calls # (e.g., bar, plot or semilogx) so we can test what has been called: mocked_axes_obj = MagicMock() mock_pyplot_subplot.side_effect = lambda *a, **v: mocked_axes_obj residuals = res.Residuals(self.gsims, self.imts) residuals.get_residuals(self.database, component="Geometric") for gsim in self.gsims: for imt in self.imts: ResidualPlot(residuals, gsim, imt, bin_width=0.1) # assert we called pyplot show: self.assertTrue(mock_pyplot.show.call_count == 1) ResidualPlot(residuals, gsim, imt, bin_width=0.1, show=False) # assert we did NOT call pyplot show (call count still 1): self.assertTrue(mock_pyplot.show.call_count == 1) # reset mock: mock_pyplot.show.reset_mock() # assert we called the right matplotlib plotting functions: self.assertTrue(mocked_axes_obj.bar.called) self.assertTrue(mocked_axes_obj.plot.called) self.assertFalse(mocked_axes_obj.semilogx.called) # reset mock: mocked_axes_obj.reset_mock()
def test_edr_execution(self): """ Tests execution of EDR - not correctness of values - to be the same when created from sm_database.GroundMotionDatabase and sm_table.GroundMotionTable """ edr1 = res.Residuals(self.gsims, self.imts) edr1.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(edr1.residuals) edr1.get_edr_values() edr2 = res.Residuals(self.gsims, self.imts) edr2.get_residuals(self.dbtable, component="Geometric") self._check_residual_dictionary_correctness(edr2.residuals) edr2.get_edr_values() self._check_new_context_vs_old(edr1.contexts, edr2.contexts)
def test_llh_execution(self): """ Tests execution of LLH - not correctness of values - to be the same when created from sm_database.GroundMotionDatabase and sm_table.GroundMotionTable """ llh1 = res.Residuals(self.gsims, self.imts) llh1.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(llh1.residuals) llh1.get_loglikelihood_values(self.imts) llh2 = res.Residuals(self.gsims, self.imts) llh2.get_residuals(self.dbtable, component="Geometric") self._check_residual_dictionary_correctness(llh2.residuals) llh2.get_loglikelihood_values(self.imts) self._check_new_context_vs_old(llh1.contexts, llh2.contexts)
def tests_residuals_execution(self): """ Tests basic execution of residuals - not correctness of values """ residuals = res.Residuals(self.gsims, self.imts) residuals.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(residuals.residuals) _ = residuals.get_residual_statistics()
def test_edr_execution(self): """ Tests execution of EDR - not correctness of values """ edr = res.Residuals(self.gsims, self.imts) edr.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(edr.residuals) edr.get_edr_values()
def test_multivariate_llh_execution(self): """ Tests execution of multivariate llh - not correctness of values """ multi_llh = res.Residuals(self.gsims, self.imts) multi_llh.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(multi_llh.residuals) multi_llh.get_multivariate_loglikelihood_values()
def test_likelihood_execution(self): """ Tests basic execution of residuals - not correctness of values """ lkh = res.Residuals(self.gsims, self.imts) lkh.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(lkh.residuals) lkh.get_likelihood_values()
def tests_with_distance(self, mock_pyplot, mock_pyplot_subplot): """ Tests basic execution of residual with distance plots. Simply tests pyplot show is called by mocking its `show` method """ # setup a mock which will handle all calls to matplotlib Axes calls # (e.g., bar, plot or semilogx) so we can test what has been called: mocked_axes_obj = MagicMock() mock_pyplot_subplot.side_effect = lambda *a, **v: mocked_axes_obj residuals = res.Residuals(self.gsims, self.imts) residuals.get_residuals(self.database, component="Geometric") for gsim in self.gsims: for imt in self.imts: for dist in DISTANCES.keys(): if dist == 'r_x': # as for residual_plots_test, we should confirm # with scientific expertise that this is the case: with self.assertRaises(AttributeError): ResidualWithDistance(residuals, gsim, imt, distance_type=dist, show=True) continue ResidualWithDistance(residuals, gsim, imt, bin_width=0.1) # assert we called pyplot show: self.assertTrue(mock_pyplot.show.call_count == 1) ResidualWithDistance(residuals, gsim, imt, bin_width=0.1, show=False) # assert we did NOT call pyplot show (call count still 1): self.assertTrue(mock_pyplot.show.call_count == 1) # reset mock: mock_pyplot.show.reset_mock() # assert we called the right matplotlib plotting functions: self.assertFalse(mocked_axes_obj.bar.called) self.assertFalse(mocked_axes_obj.plot.called) self.assertTrue(mocked_axes_obj.semilogx.called) # check plot type: ResidualWithDistance(residuals, gsim, imt, plot_type='', bin_width=0.1, show=False) self.assertTrue(mocked_axes_obj.plot.called) # reset mock: mocked_axes_obj.reset_mock()
def test_multivariate_llh_execution(self): """ Tests execution of multivariate llh - not correctness of values - to be the same when created from sm_database.GroundMotionDatabase and sm_table.GroundMotionTable """ multi_llh1 = res.Residuals(self.gsims, self.imts) multi_llh1.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(multi_llh1.residuals) multi_llh1.get_multivariate_loglikelihood_values()
def test_likelihood_execution(self): """ Tests basic execution of residuals - not correctness of values - to be the same when created from sm_database.GroundMotionDatabase and sm_table.GroundMotionTable """ lkh1 = res.Residuals(self.gsims, self.imts) lkh1.get_residuals(self.database, component="Geometric") self._check_residual_dictionary_correctness(lkh1.residuals) lkh1.get_likelihood_values()
def test_multiple_metrics(self): """ Tests the execution running multiple metrics in one call """ residuals = res.Residuals(self.gsims, self.imts) residuals.get_residuals(self.database, component="Geometric") config = {} for key in [ "Residuals", "Likelihood", "LLH", "MultivariateLLH", "EDR" ]: _ = res.GSIM_MODEL_DATA_TESTS[key](residuals, config)
def test_multiple_metrics(self): """ Tests the execution running multiple metrics in one call with sm_table.GroundMotionTable instead of sm_database.GroundMotionDatabase """ # OLD CODE: # residuals = res.Residuals(self.gsims, self.imts) # residuals.get_residuals(self.database, component="Geometric") # config = {} # for key in ["Residuals", "Likelihood", "LLH", # "MultivariateLLH", "EDR"]: # _ = res.GSIM_MODEL_DATA_TESTS[key](residuals, config) residuals = res.Residuals(self.gsims, self.imts) residuals.get_residuals(self.dbtable, component="Geometric") config = {} for key in ["Residuals", "Likelihood", "LLH", "MultivariateLLH", "EDR"]: _ = res.GSIM_MODEL_DATA_TESTS[key](residuals, config)
def test_likelihood_density_distribution(self): """ Tests basic execution of Likelihood plot data. Does not test correctness of values """ residuals = res.Residuals(self.gsims, self.imts) residuals.get_residuals(self.database, component="Geometric") additional_keys = ['median'] for gsim in self.gsims: for imt in self.imts: for as_json in (True, False): bin_w1, bin_w2 = 0.1, 0.2 data1 = likelihood(residuals, gsim, imt, bin_width=bin_w1, as_json=as_json) self._plot_data_check(data1, as_json, "LH (%s)" % imt, "Frequency", additional_keys) data2 = likelihood(residuals, gsim, imt, bin_width=bin_w2, as_json=as_json) self._plot_data_check(data2, as_json, "LH (%s)" % imt, "Frequency", additional_keys) # assert histogram data is ok: self._hist_data_check(residuals, gsim, imt, data1, bin_w1) self._hist_data_check(residuals, gsim, imt, data2, bin_w2) # assert bin width did its job: for res_type in data1: self.assertTrue( len(data1[res_type]['x']) > len(data2[res_type]['x']))