def test_output(self): self.test_append() self.assertTrue((self.Sample.output()[0]['moment_tensor_space'] == np.matrix([[2, 2, 3, 2], [1, 1, 5, 1], [2, 2, 1, 2], [1, 1, 1, 1], [2, 2, 2, 2], [1, 1, 1, 1]])).all()) output_ln_pdf = np.sum(np.exp(np.matrix([[1, 1, 0, 1], [2, 2, 1, 2]])), 0) output_ln_pdf /= np.sum(output_ln_pdf) self.assertAlmostEqual(self.Sample.output()[0]['probability'][0], output_ln_pdf[0, 0]) self.assertAlmostEqual(self.Sample.output()[0]['probability'][1], output_ln_pdf[0, 1]) self.assertAlmostEqual(self.Sample.output()[0]['probability'][2], output_ln_pdf[0, 2]) self.assertAlmostEqual(self.Sample.output()[0]['probability'][3], output_ln_pdf[0, 3]) self.assertEqual(self.Sample.output()[0]['dV'], 1) output_ln_pdf = np.sum(np.exp(np.matrix([[1, 1, 0, 1], [2, 2, 1, 2]])), 0) self.assertAlmostEqual(self.Sample.output(False)[0]['probability'][0, 0], output_ln_pdf[0, 0]) self.assertAlmostEqual(self.Sample.output(False)[0]['probability'][0, 1], output_ln_pdf[0, 1]) self.assertAlmostEqual(self.Sample.output(False)[0]['probability'][0, 2], output_ln_pdf[0, 2]) self.assertAlmostEqual(self.Sample.output(False)[0]['probability'][0, 3], output_ln_pdf[0, 3]) self.assertEqual(self.Sample.output(False)[0]['dV'], 1) self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[3], [1], [2], [1], [2], [1]])] ln_pdf = LnPDF(np.matrix([[1]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue((self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [3], [1], [2], [1], [2], [1]])).all()) self.assertTrue((self.Sample.output()[0]['moment_tensor_space_1'] == np.matrix([[2], [1], [2], [1], [2], [1]])).all()) self.assertTrue((self.Sample.output()[0]['moment_tensor_space_2'] == np.matrix([[3], [1], [2], [1], [2], [1]])).all()) self.assertTrue((self.Sample.output()[0]['probability'] == np.matrix([[1]])).all(), str(self.Sample.output()[0]['probability'])) self.assertEqual(self.Sample.output()[0]['dV'], 1) self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[2], [1], [2], [1], [2], [1]])] ln_pdf = LnPDF(np.matrix([[1]])) scale_factor = np.array([{'mu': np.array([[[2, 3], [3, 2]]]), 'sigma': np.array([[1, 1], [1, 1]])}]) self.assertFalse(self.Sample.__dict__.__contains__('scale_factor')) self.Sample.append(moment_tensors, ln_pdf, 1, scale_factor) self.assertTrue((self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [2], [1], [2], [1], [2], [1]])).all()) self.assertTrue(self.Sample.__dict__.__contains__('scale_factor')) self.assertTrue((self.Sample.scale_factor[0]['mu'] == np.array([[2, 3], [3, 2]])).all()) self.assertTrue((self.Sample.output()[0]['scale_factors'][0]['mu'] == np.array([[[2, 3], [3, 2]]])).all(), str(self.Sample.output()[0]['scale_factors'][0]['mu'])) self.tearDown() self.setUp() self.test_append() out, out_str = self.Sample.output(convert=True) self.assertTrue('dV' in out.keys()) self.assertTrue('moment_tensor_space' in out.keys()) self.assertTrue('probability' in out.keys()) self.assertTrue('ln_pdf' in out.keys()) self.assertTrue('g' in out.keys())
def test_append(self): moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[1], [2]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue((self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1]])).all()) self.assertEqual(self.Sample.n, 1) self.assertEqual(self.Sample.ln_pdf[0, 0], 1.) moment_tensors = np.matrix([[2, 3], [1, 5], [2, 1], [1, 1], [2, 2], [1, 1]]) ln_pdf = LnPDF(np.matrix([[1, 0], [2, 1]])) self.Sample.append(moment_tensors, ln_pdf, 2) self.assertTrue((self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[0], [0]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue((self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) self.Sample.append(np.matrix([[], [], [], [], [], []]), np.matrix([[]]), 1) self.assertTrue((self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) self.assertEqual(self.Sample.n, 5) # test multiple events self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[2], [1], [2], [1], [2], [1]])] ln_pdf = LnPDF(np.matrix([[1]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue((self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [2], [1], [2], [1], [2], [1]])).all()) # test scale_factor - multiple events self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[2], [1], [2], [1], [2], [1]])] ln_pdf = LnPDF(np.matrix([[1]])) scale_factor = np.array([{'mu': np.array([[[2, 3], [3, 2]]]), 'sigma': np.array([[1, 1], [1, 1]])}]) self.assertFalse(self.Sample.__dict__.__contains__('scale_factor')) self.Sample.append(moment_tensors, ln_pdf, 1, scale_factor) self.assertTrue((self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [2], [1], [2], [1], [2], [1]])).all()) self.assertTrue(self.Sample.__dict__.__contains__('scale_factor')) self.assertTrue((self.Sample.scale_factor[0]['mu'] == np.array([[2, 3], [3, 2]])).all()) self.tearDown() self.Sample = Sample(initial_sample_size=3) # test initial samples moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[1], [2]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue((self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1]])).all()) self.assertEqual(self.Sample.n, 1) self.assertEqual(self.Sample.ln_pdf[0, 0], 1.) moment_tensors = np.matrix([[2, 3], [1, 5], [2, 1], [1, 1], [2, 2], [1, 1]]) ln_pdf = LnPDF(np.matrix([[1, 0], [2, 1]])) self.Sample.append(moment_tensors, ln_pdf, 2) self.assertTrue((self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[1], [2]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue((self.Sample.moment_tensors[:, 0:4] == np.matrix([[2, 2, 3, 2], [1, 1, 5, 1], [2, 2, 1, 2], [1, 1, 1, 1], [2, 2, 2, 2], [1, 1, 1, 1]])).all())
def setUp(self): self.Sample = Sample()
class SampleTestCase(unittest.TestCase): def setUp(self): self.Sample = Sample() def tearDown(self): del self.Sample def test_append(self): moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[1], [2]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue( (self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1]])).all()) self.assertEqual(self.Sample.n, 1) self.assertEqual(self.Sample.ln_pdf[0, 0], 1.) moment_tensors = np.matrix([[2, 3], [1, 5], [2, 1], [1, 1], [2, 2], [1, 1]]) ln_pdf = LnPDF(np.matrix([[1, 0], [2, 1]])) self.Sample.append(moment_tensors, ln_pdf, 2) self.assertTrue( (self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[0], [0]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue( (self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) self.Sample.append(np.matrix([[], [], [], [], [], []]), np.matrix([[]]), 1) self.assertTrue( (self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) self.assertEqual(self.Sample.n, 5) # test multiple events self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [ np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[2], [1], [2], [1], [2], [1]]) ] ln_pdf = LnPDF(np.matrix([[1]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue( (self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [2], [1], [2], [1], [2], [1]])).all()) # test scale_factor - multiple events self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [ np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[2], [1], [2], [1], [2], [1]]) ] ln_pdf = LnPDF(np.matrix([[1]])) scale_factor = np.array([{ 'mu': np.array([[[2, 3], [3, 2]]]), 'sigma': np.array([[1, 1], [1, 1]]) }]) self.assertFalse(self.Sample.__dict__.__contains__('scale_factor')) self.Sample.append(moment_tensors, ln_pdf, 1, scale_factor) self.assertTrue( (self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [2], [1], [2], [1], [2], [1]])).all()) self.assertTrue(self.Sample.__dict__.__contains__('scale_factor')) self.assertTrue( (self.Sample.scale_factor[0]['mu'] == np.array([[2, 3], [3, 2]])).all()) self.tearDown() self.Sample = Sample(initial_sample_size=3) # test initial samples moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[1], [2]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue( (self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1]])).all()) self.assertEqual(self.Sample.n, 1) self.assertEqual(self.Sample.ln_pdf[0, 0], 1.) moment_tensors = np.matrix([[2, 3], [1, 5], [2, 1], [1, 1], [2, 2], [1, 1]]) ln_pdf = LnPDF(np.matrix([[1, 0], [2, 1]])) self.Sample.append(moment_tensors, ln_pdf, 2) self.assertTrue( (self.Sample.moment_tensors[:, 0:3] == np.matrix([[2, 2, 3], [1, 1, 5], [2, 2, 1], [1, 1, 1], [2, 2, 2], [1, 1, 1]])).all()) moment_tensors = np.matrix([[2], [1], [2], [1], [2], [1]]) ln_pdf = LnPDF(np.matrix([[1], [2]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue( (self.Sample.moment_tensors[:, 0:4] == np.matrix([[2, 2, 3, 2], [1, 1, 5, 1], [2, 2, 1, 2], [1, 1, 1, 1], [2, 2, 2, 2], [1, 1, 1, 1]])).all()) def test_output(self): self.test_append() self.assertTrue( (self.Sample.output()[0]['moment_tensor_space'] == np.matrix( [[2, 2, 3, 2], [1, 1, 5, 1], [2, 2, 1, 2], [1, 1, 1, 1], [2, 2, 2, 2], [1, 1, 1, 1]])).all()) output_ln_pdf = np.sum( np.exp(np.matrix([[1., 1., 0., 1.], [2., 2., 1., 2.]])), 0) output_ln_pdf /= np.sum(output_ln_pdf) self.assertAlmostEqual(self.Sample.output()[0]['probability'][0], output_ln_pdf[0, 0]) self.assertAlmostEqual(self.Sample.output()[0]['probability'][1], output_ln_pdf[0, 1]) self.assertAlmostEqual(self.Sample.output()[0]['probability'][2], output_ln_pdf[0, 2]) self.assertAlmostEqual(self.Sample.output()[0]['probability'][3], output_ln_pdf[0, 3]) self.assertEqual(self.Sample.output()[0]['dV'], 1) output_ln_pdf = np.sum( np.exp(np.matrix([[1., 1., 0., 1.], [2., 2., 1., 2.]])), 0) self.assertAlmostEqual( self.Sample.output(False)[0]['probability'][0], output_ln_pdf[0, 0]) self.assertAlmostEqual( self.Sample.output(False)[0]['probability'][1], output_ln_pdf[0, 1]) self.assertAlmostEqual( self.Sample.output(False)[0]['probability'][2], output_ln_pdf[0, 2]) self.assertAlmostEqual( self.Sample.output(False)[0]['probability'][3], output_ln_pdf[0, 3]) self.assertEqual(self.Sample.output(False)[0]['dV'], 1) self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [ np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[3], [1], [2], [1], [2], [1]]) ] ln_pdf = LnPDF(np.matrix([[1]])) self.Sample.append(moment_tensors, ln_pdf, 1) self.assertTrue( (self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [3], [1], [2], [1], [2], [1]])).all()) self.assertTrue( (self.Sample.output()[0]['moment_tensor_space_1'] == np.matrix( [[2], [1], [2], [1], [2], [1]])).all()) self.assertTrue( (self.Sample.output()[0]['moment_tensor_space_2'] == np.matrix( [[3], [1], [2], [1], [2], [1]])).all()) self.assertTrue( (self.Sample.output()[0]['probability'] == (np.array([1]))).all(), str(self.Sample.output()[0]['probability'])) self.assertEqual(self.Sample.output()[0]['dV'], 1) self.tearDown() self.Sample = Sample(initial_sample_size=1, number_events=2) moment_tensors = [ np.matrix([[2], [1], [2], [1], [2], [1]]), np.matrix([[2], [1], [2], [1], [2], [1]]) ] ln_pdf = LnPDF(np.matrix([[1.]])) scale_factor = np.array([{ 'mu': np.array([[[2, 3], [3, 2]]]), 'sigma': np.array([[1, 1], [1, 1]]) }]) self.assertFalse(self.Sample.__dict__.__contains__('scale_factor')) self.Sample.append(moment_tensors, ln_pdf, 1, scale_factor) self.assertTrue( (self.Sample.moment_tensors[:, 0] == np.matrix([[2], [1], [2], [1], [2], [1], [2], [1], [2], [1], [2], [1]])).all()) self.assertTrue(self.Sample.__dict__.__contains__('scale_factor')) self.assertTrue( (self.Sample.scale_factor[0]['mu'] == np.array([[2, 3], [3, 2]])).all()) self.assertTrue( (self.Sample.output()[0]['scale_factors'][0]['mu'] == np.array( [[[2, 3], [3, 2]]])).all(), str(self.Sample.output()[0]['scale_factors'][0]['mu'])) self.tearDown() self.setUp() self.test_append() out, out_str = self.Sample.output(convert=True) self.assertTrue('dV' in out.keys()) self.assertTrue('moment_tensor_space' in out.keys()) self.assertTrue('probability' in out.keys()) self.assertTrue('ln_pdf' in out.keys()) self.assertTrue('g' in out.keys())