def test_read_matlab_output(self): self.hyp_file() events = parse_hyp('hyptest.hyp') event = events[0] mdict, sdict = full_pdf_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'ln_pdf': np.matrix([0, 0.7]), 'dV': 1, 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 }) MATLAB_output([mdict, sdict], 'hyptest.mat') event, stations = read_matlab_output('hyptest.mat') self.assertAlmostEqual(event, mdict['Events'], 10) self.assertAlmostEqual(stations['azimuth'], mdict['Stations'][:, 1], 10) self.assertAlmostEqual(stations['takeoff_angle'], mdict['Stations'][:, 2], 10) self.assertAlmostEqual(stations['polarity'], mdict['Stations'][:, 3], 10)
def test_parse_hyp(self): self.hyp_file() events = parse_hyp('hyptest.hyp') self.assertEqual(len(events), 1) self.assertTrue('PPolarity' in events[0].keys()) self.assertTrue('UID' in events[0].keys()) self.assertTrue('hyp_file' in events[0].keys()) self.assertEqual(events[0]['UID'], "20150126222324275") self.assertEqual(len(events[0].keys()), 3)
def test_read_binary_output(self): self.hyp_file() events = parse_hyp('hyptest.hyp') event = events[0] x = { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'dkl': 1.2, 'ln_pdf': np.matrix([0, 0.7]), 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 } a, b, c = hyp_output_dicts(event, False, x) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass self.assertFalse(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertFalse(os.path.exists('MTfitOUTPUTTEST.mt')) fid, out_str = hyp_output([a, b], fid='MTfitOUTPUTTEST.hyp') y = read_binary_output('MTfitOUTPUTTEST.mt') self.assertTrue(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertTrue(os.path.exists('MTfitOUTPUTTEST.mt')) self.assertEqual(sorted(y[0].keys()), sorted(x.keys())) self.assertTrue( (y[0]['moment_tensor_space'] == x['moment_tensor_space']).all()) self.assertEqual(y[0]['dkl'], x['dkl']) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass x = { 'moment_tensor_space': np.matrix([[1., -0.51969334, 3.], [2., 0.22610635, 3.], [1., 0.29358698, 3.], [2., 0.58532165, 3.], [1., -0.27015115, 3.], [2., -0.42073549, 3.]]), 'probability': np.matrix([[1., 2., 1.]]), 'dkl': 2.4, 'ln_pdf': np.matrix([0, 0.7, 0]), 'total_number_samples': 400, 'g': np.array([0.1, 0.2, 0.3]), 'd': np.array([0.2, 0.2, 0.2]), 'k': np.array([0.2, 0.2, 0.2]), 'h': np.array([0.2, 0.2, 0.2]), 's': np.array([0.2, 0.2, 0.2]), 'S1': np.array([0.2, 0.2, 0.2]), 'D1': np.array([0.2, 0.2, 0.2]), 'R1': np.array([0.2, 0.2, 0.2]), 'u': np.array([0.2, 0.2, 0.2]), 'v': np.array([0.2, 0.2, 0.2]), 'S2': np.array([0.2, 0.2, 0.2]), 'D2': np.array([0.2, 0.2, 0.2]), 'R2': np.array([0.2, 0.2, 0.2]), 'ln_bayesian_evidence': 1. + 10. } a, b, c = hyp_output_dicts(event, False, x) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass self.assertFalse(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertFalse(os.path.exists('MTfitOUTPUTTEST.mt')) fid, out_str = hyp_output([a, b], fid='MTfitOUTPUTTEST.hyp') y = read_binary_output('MTfitOUTPUTTEST.mt') self.assertTrue(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertTrue(os.path.exists('MTfitOUTPUTTEST.mt')) self.assertEqual(sorted(y[0].keys()), sorted(x.keys())) self.assertTrue((y[0]['g'] == x['g']).all()) self.assertEqual(y[0]['ln_bayesian_evidence'], x['ln_bayesian_evidence']) self.assertEqual(y[0]['dkl'], x['dkl']) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass
def test_hyp_output(self): self.hyp_file() events = parse_hyp('hyptest.hyp') event = events[0] a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 }) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass self.assertFalse(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertFalse(os.path.exists('MTfitOUTPUTTEST.mt')) fid, out_str = hyp_output([a, b], fid='MTfitOUTPUTTEST.hyp') self.assertTrue(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertTrue(os.path.exists('MTfitOUTPUTTEST.mt')) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass event['hyp_file'].pop(14) a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., -0.51969334, 3.], [2., 0.22610635, 3.], [1., 0.29358698, 3.], [2., 0.58532165, 3.], [1., -0.27015115, 3.], [2., -0.42073549, 3.]]), 'probability': np.matrix([[1., 2., 1.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'total_number_samples': 400, 'g': np.array([0.2, 0.2, 0.2]), 'd': np.array([0.2, 0.2, 0.2]), 'k': np.array([0.2, 0.2, 0.2]), 'h': np.array([0.2, 0.2, 0.2]), 's': np.array([0.2, 0.2, 0.2]), 'S1': np.array([0.2, 0.2, 0.2]), 'D1': np.array([0.2, 0.2, 0.2]), 'R1': np.array([0.2, 0.2, 0.2]), 'u': np.array([0.2, 0.2, 0.2]), 'v': np.array([0.2, 0.2, 0.2]), 'S2': np.array([0.2, 0.2, 0.2]), 'D2': np.array([0.2, 0.2, 0.2]), 'R2': np.array([0.2, 0.2, 0.2]), 'ln_bayesian_evidence': 1. + 10 }) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass self.assertFalse(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertFalse(os.path.exists('MTfitOUTPUTTEST.mt')) fid, out_str = hyp_output([a, b], fid='MTfitOUTPUTTEST.hyp') self.assertTrue(os.path.exists('MTfitOUTPUTTEST.hyp')) self.assertTrue(os.path.exists('MTfitOUTPUTTEST.mt')) try: os.remove('MTfitOUTPUTTEST.hyp') except Exception: pass try: os.remove('MTfitOUTPUTTEST.mt') except Exception: pass
def test_hyp_output_dicts(self): self.hyp_file() events = parse_hyp('hyptest.hyp') event = events[0] a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'ln_pdf': np.matrix([0, 0.7]), 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 }) self.assertEqual(len(a.split('\n')), len(event['hyp_file']) + 1) self.assertTrue('MOMENTTENSOR' in a) self.assertEqual(float(a.split()[a.split().index('MTNN') + 1]), 2.0) self.assertEqual(len(b), 169) events = parse_hyp('hyptest.hyp') event = events[0] a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2., 3.], [2., 1., 3.], [1., 2., 3.], [2., 1., 3.], [1., 2., 3.], [2., 1., 3.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'probability': np.matrix([[1., 2., 1.]]), 'total_number_samples': 400 }) self.assertEqual(len(a.split('\n')), len(event['hyp_file']) + 1) self.assertTrue('MOMENTTENSOR' in a) self.assertEqual(float(a.split()[a.split().index('MTNN') + 1]), 2.0) self.assertEqual(len(b), 233) events = parse_hyp('hyptest.hyp') event = events[0] event['hyp_file'].pop(14) a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2., 3.], [2., 1., 3.], [1., 2., 3.], [2., 1., 3.], [1., 2., 3.], [2., 1., 3.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'probability': np.matrix([[1., 2., 1.]]), 'total_number_samples': 400 }) self.assertEqual(len(a.split('\n')), len(event['hyp_file']) + 2) self.assertTrue('MOMENTTENSOR' in a) self.assertEqual(float(a.split()[a.split().index('MTNN') + 1]), 2.0) self.assertEqual(len(b), 233) event.pop('hyp_file') a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2., 3.], [2., 1., 3.], [1., 2., 3.], [2., 1., 3.], [1., 2., 3.], [2., 1., 3.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'probability': np.matrix([[1., 2., 1.]]), 'total_number_samples': 400 }) self.assertEqual(len(a.split('\n')), 36) self.assertTrue('MOMENTTENSOR' in a) self.assertEqual(float(a.split()[a.split().index('MTNN') + 1]), 2.0) self.assertEqual(len(b), 233) events = parse_hyp('hyptest.hyp') event = events[0] event['hyp_file'].pop(14) a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., -0.51969334, 3.], [2., 0.22610635, 3.], [1., 0.29358698, 3.], [2., 0.58532165, 3.], [1., -0.27015115, 3.], [2., -0.42073549, 3.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'probability': np.matrix([[1., 2., 1.]]), 'total_number_samples': 400 }) self.assertEqual(len(a.split('\n')), len(event['hyp_file']) + 2) self.assertFalse('MOMENTTENSOR' in a) self.assertTrue('FOCALMECH' in a) self.assertAlmostEqual(float(a.split()[a.split().index('Mech') + 1]), 0.5 * 180 / np.pi, 5) self.assertEqual(len(b), 233) a, b, c = hyp_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., -0.51969334, 3.], [2., 0.22610635, 3.], [1., 0.29358698, 3.], [2., 0.58532165, 3.], [1., -0.27015115, 3.], [2., -0.42073549, 3.]]), 'probability': np.matrix([[1., 2., 1.]]), 'ln_pdf': np.matrix([0, 0.7, 0]), 'total_number_samples': 400, 'g': np.array([0.2, 0.2, 0.2]), 'd': np.array([0.2, 0.2, 0.2]), 'k': np.array([0.2, 0.2, 0.2]), 'h': np.array([0.2, 0.2, 0.2]), 's': np.array([0.2, 0.2, 0.2]), 'S1': np.array([0.2, 0.2, 0.2]), 'D1': np.array([0.2, 0.2, 0.2]), 'R1': np.array([0.2, 0.2, 0.2]), 'u': np.array([0.2, 0.2, 0.2]), 'v': np.array([0.2, 0.2, 0.2]), 'S2': np.array([0.2, 0.2, 0.2]), 'D2': np.array([0.2, 0.2, 0.2]), 'R2': np.array([0.2, 0.2, 0.2]), 'ln_bayesian_evidence': (1, 10) }) self.assertEqual(len(a.split('\n')), len(event['hyp_file']) + 2) self.assertFalse('MOMENTTENSOR' in a) self.assertTrue('FOCALMECH' in a) self.assertAlmostEqual(float(a.split()[a.split().index('Mech') + 1]), 0.5 * 180 / np.pi, 5) self.assertEqual(len(b), 545)
def test_full_pdf_output_dicts(self): self.hyp_file() events = parse_hyp('hyptest.hyp') event = events[0] mdict, sdict = full_pdf_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'ln_pdf': np.matrix([0, 0.7]), 'dV': 1, 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 }) self.assertTrue('Other' in mdict.keys()) self.assertTrue('Events' in mdict.keys()) self.assertTrue('Stations' in mdict.keys()) self.assertTrue('MTSpace' in mdict['Events'].keys()) self.assertTrue('Probability' in mdict['Events'].keys()) self.assertTrue('ln_pdf' in mdict['Events'].keys()) self.assertTrue('UID' in mdict['Events'].keys()) self.assertTrue('NSamples' in mdict['Events'].keys()) self.assertEqual(mdict['Stations'].shape, (20, 4)) event['PPolarityProbability'] = event['PPolarity'].copy() event['PPolarityProbability']['Measured'] = np.matrix([[0.6, 0.4], [0.7, 0.3], [0.8, 0.2], [0.67, 0.33], [0.94, 0.06], [0.32, 0.68], [0.96, 0.04], [0.76, 0.24], [0.82, 0.18], [0.12, 0.88], [0.57, 0.43], [0.68, 0.32], [0.51, 0.49], [0.68, 0.32], [0.50, 0.50], [0.02, 0.98], [0.6, 0.4], [0.7, 0.3], [0.8, 0.2], [0.67, 0.33]]) event.pop('PPolarity') mdict, sdict = full_pdf_output_dicts( event, ['PPolarityProbability'], { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'ln_pdf': np.matrix([0, 0.7]), 'dV': 1, 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 }) self.assertTrue('Other' in mdict.keys()) self.assertTrue('Events' in mdict.keys()) self.assertTrue('Stations' in mdict.keys()) self.assertTrue('MTSpace' in mdict['Events'].keys()) self.assertTrue('Probability' in mdict['Events'].keys()) self.assertTrue('ln_pdf' in mdict['Events'].keys()) self.assertTrue('UID' in mdict['Events'].keys()) self.assertTrue('NSamples' in mdict['Events'].keys()) self.assertEqual(mdict['Stations'].shape, (20, 4)) self.assertTrue( all(mdict['Stations'][:, 3] == np.array([ 0.6, 0.7, 0.8, 0.67, 0.94, -0.68, 0.96, 0.76, 0.82, -0.88, 0.57, 0.68, 0.51, 0.68, 0.5, -0.98, 0.6, 0.7, 0.8, 0.67 ]))) mdict, sdict = full_pdf_output_dicts( event, False, { 'moment_tensor_space': np.matrix([[1., 2.], [2., 1.], [1., 2.], [2., 1.], [1., 2.], [2., 1.]]), 'ln_pdf': np.matrix([0, 0.7]), 'dV': 1, 'probability': np.matrix([[1., 2.]]), 'total_number_samples': 400 }) self.assertTrue('Other' in mdict.keys()) self.assertTrue('Events' in mdict.keys()) self.assertTrue('Stations' in mdict.keys()) self.assertTrue('MTSpace' in mdict['Events'].keys()) self.assertTrue('Probability' in mdict['Events'].keys()) self.assertTrue('ln_pdf' in mdict['Events'].keys()) self.assertTrue('UID' in mdict['Events'].keys()) self.assertTrue('NSamples' in mdict['Events'].keys()) self.assertEqual(mdict['Stations'].shape, (20, 4))