def test_write_to_txt(self): tks = [[1, 2, 0], [2, 0], [5, 0], [1, 0]] vps = [[300, 400, 500], [300, 600], [600, 1000], [800, 1000]] vss = [[100, 200, 300], [200, 300], [300, 500], [400, 600]] rhs = [[2000] * 3, [2000] * 2, [2000] * 2, [2000] * 2] ids = [1, 2, 3] misfits = [1, 0.5, 0.3] gm = swprepost.GroundModel(tks[0], vps[0], vss[0], rhs[0], identifier=ids[0], misfit=misfits[0]) suite = swprepost.GroundModelSuite(gm) for tk, vs, vp, rh, cid, ms in zip(tks[1:], vss[1:], vps[1:], rhs[1:], ids[1:], misfits[1:]): gm = swprepost.GroundModel(tk, vp, vs, rh, identifier=cid, misfit=ms) suite.append(gm) fname = "text.txt" suite.write_to_txt(fname) mysuite = swprepost.GroundModelSuite.from_geopsy(fname) for gm_a, gm_b in zip(suite.gms, mysuite.gms): self.assertEqual(gm_a, gm_b) os.remove(fname)
def test_gm2(self): # Simple Test tk1 = [5, 0] vp1 = [200, 250] vs1 = [100, 125] rh1 = [2000] * 2 mygm = swprepost.GroundModel(thickness=tk1, vp=vp1, vs=vs1, density=rh1) self.assertListEqual([0, 5, 5, 9999.0], mygm.depth) self.assertListEqual([200, 200, 250, 250], mygm.vp2) self.assertListEqual([100, 100, 125, 125], mygm.vs2) self.assertListEqual([2000] * 4, mygm.rh2) # Setup thks = [1, 3, 5, 7, 0] vss = [100, 300, 500, 700, 900] vps = [200, 600, 1000, 1400, 1800] rho = [2000] * 5 mygm = swprepost.GroundModel(thks, vps, vss, rho) depth2 = mygm.gm2(parameter="depth") vp2 = mygm.gm2(parameter="vp") vs2 = mygm.gm2(parameter="vs") rho2 = mygm.gm2(parameter="rh") # True depth2_true = [0, 1, 1, 4, 4, 9, 9, 16, 16, 9999.] vs2_true = [100, 100, 300, 300, 500, 500, 700, 700, 900, 900] vp2_true = [200, 200, 600, 600, 1000, 1000, 1400, 1400, 1800, 1800] rho2_true = rho + rho # Different way to get to the same value deptha = mygm.depth vp2a = mygm.vp2 vs2a = mygm.vs2 rho2a = mygm.rh2 # Check two lists are indeed the same self.assertListEqual(depth2, deptha) self.assertListEqual(vp2, vp2a) self.assertListEqual(vs2, vs2a) self.assertListEqual(rho2, rho2a) # Go value by value returned_vals = [depth2, vp2, vs2, rho2] expected_vals = [depth2_true, vp2_true, vs2_true, rho2_true] for expected, returned in zip(expected_vals, returned_vals): self.assertListAlmostEqual(expected, returned) # Check Poisson's Ratio returned = mygm.pr2 expected = swprepost.GroundModel.calc_pr(vp2, vs2) self.assertListEqual(expected, returned)
def test_init(self): # List thk = [1, 5, 3.3, 10] vps = [300, 450, 1500.1, 2000.1] vss = [100, 150, 201., 300] rho = [2000, 3000, 2250., 2300.] mygm = swprepost.GroundModel(thickness=thk, vp=vps, vs=vss, density=rho) self.assertListEqual(mygm.tk, thk) self.assertListEqual(mygm.vp, vps) self.assertListEqual(mygm.vs, vss) self.assertListEqual(mygm.rh, rho) # ndarray thk = np.array([1, 5, 3.3, 10]) vps = np.array([300, 450, 1500.1, 2000.1]) vss = np.array([100, 150, 201., 300]) rho = np.array([2000, 3000, 2250., 2300.]) mygm = swprepost.GroundModel(thickness=thk, vp=vps, vs=vss, density=rho) self.assertListEqual(thk.tolist(), mygm.tk) self.assertListEqual(vps.tolist(), mygm.vp) self.assertListEqual(vss.tolist(), mygm.vs) self.assertListEqual(rho.tolist(), mygm.rh) # Bad Type x = ["this", "that", "these", "those"] self.assertRaises(TypeError, swprepost.GroundModel, x, x, x, x) # Bad Value - Less than zero x = [-1] * 3 self.assertRaises(ValueError, swprepost.GroundModel, x, x, x, x) # Bad Value - Length x = [1] * 3 y = [1] * 4 self.assertRaises(ValueError, swprepost.GroundModel, x, x, x, y) # Bad Value - Vp < Vs tk = [1] * 2 vp = [100, 200] vs = [200, 300] rh = [2000] * 2 self.assertRaises(ValueError, swprepost.GroundModel, tk, vp, vs, rh)
def test_from_array(self): tks = np.array([[1, 2, 3], [0, 0, 0]]) vps = np.array([[100, 200, 300], [200, 400, 600]]) vss = np.array([[50, 75, 100], [100, 200, 300]]) rhs = np.array([[2000, 2200, 2250], [2100, 2300, 2300]]) misfits = np.array([1., 2., 3.]) ids = np.array([2, 7, 9]) gms = [] for col in range(tks.shape[1]): gm = swprepost.GroundModel(tks[:, col], vps[:, col], vss[:, col], rhs[:, col], identifier=ids[col], misfit=misfits[col]) gms.append(gm) suite = swprepost.GroundModelSuite.from_array(tks, vps, vss, rhs, ids, misfits) for expected, returned in zip(gms, suite): self.assertEqual(expected, returned) self.assertListEqual(misfits.tolist(), suite.misfits) self.assertListEqual(ids.tolist(), suite.identifiers)
def test_median(self): tks = [[1, 5, 0], [2, 4, 0], [5, 10, 0]] vss = [[100, 200, 300], [150, 275, 315], [100, 300, 200]] vps = [[300, 500, 350], [600, 700, 800], [300, 1000, 400]] rhs = [[2000] * 3, [2300] * 3, [2200] * 3] gm = swprepost.GroundModel(tks[0], vps[0], vss[0], rhs[0]) suite = swprepost.GroundModelSuite(gm) for tk, vs, vp, rh in zip(tks[1:], vss[1:], vps[1:], rhs[1:]): gm = swprepost.GroundModel(tk, vp, vs, rh) suite.append(gm) calc_med_gm = suite.median(nbest=3) med_tks = [2., 5., 0.] med_vss = [100., 275., 300.] med_vps = [300., 700., 400.] med_rhs = [2200.] * 3 med_gm = swprepost.GroundModel(med_tks, med_vps, med_vss, med_rhs) self.assertTrue(med_gm == calc_med_gm)
def test_str_and_repr(self): tk = [1, 2, 0] vp = [100, 200, 300] vs = [50, 100, 150] rh = [2000] * 3 gm = swprepost.GroundModel(tk, vp, vs, rh) # str self.assertEqual(gm.txt_repr, gm.__str__()) # repr expected = f"GroundModel(thickness={gm.tk}, vp={gm.vp}, vs={gm.vs}, density={gm.rh})" self.assertEqual(expected, gm.__repr__())
def test_properties(self): tk = [1, 2, 0] vp = [100, 200, 300] vs = [50, 100, 150] rh = [2000] * 3 gm = swprepost.GroundModel(tk, vp, vs, rh) self.assertListEqual(tk, gm.thickness) self.assertListEqual(vp, gm.vp) self.assertListEqual(vs, gm.vs) self.assertListEqual(rh, gm.density)
def make_gm(v, gm_dict): for key, value in gm_dict.items(): setattr(cls, key + v, value) setattr( cls, "gm" + v, swprepost.GroundModel( *[gm_dict[attr] for attr in ["tk", "vp", "vs", "rh"]], **{ key: gm_dict[attr] for key, attr in zip(["identifier", "misfit"], ["id", "mf"]) }))
def test_write_to_txt(self): tk = [2, 4, 0] vp = [100, 200, 300] vs = [50, 100, 200] rh = [2000] * 3 expected = swprepost.GroundModel(tk, vp, vs, rh) fname = "test_write_to_txt" expected.write_to_txt(fname) returned = swprepost.GroundModel.from_geopsy(fname) self.assertEqual(expected, returned) os.remove(fname)
def test_equal(self): tk = [1, 2, 0] vp = [100, 200, 300] vs = [50, 100, 150] rh = [2000] * 3 gm_a = swprepost.GroundModel(tk, vp, vs, rh) # Equal gm_b = swprepost.GroundModel(tk, vp, vs, rh) self.assertEqual(gm_a, gm_b) # Not Equal - Wrong Length x = [1, 2] y = [2, 4] gm_b = swprepost.GroundModel(x, y, x, x) self.assertNotEqual(gm_a, gm_b) # Not Equal - Wrong Value x = [1, 2, 3] y = [2, 4, 6] gm_b = swprepost.GroundModel(x, y, x, x) self.assertNotEqual(gm_a, gm_b)
def test_sigma_ln(self): tk = [1, 5, 0] vss = [[100, 200, 300], [150, 275, 315], [100, 300, 200]] vp = [200, 400, 600] rh = [2000] * 3 gm = swprepost.GroundModel(tk, vp, vss[0], rh) suite = swprepost.GroundModelSuite(gm) for vs in vss[1:]: gm = swprepost.GroundModel(tk, vp, vs, rh) suite.append(gm) dmax = 10 dy = 0.5 depth, sigln = suite.sigma_ln(nbest=3, dmax=dmax, dy=dy, parameter='vs') self.assertListEqual(depth, list(np.arange(0, dmax + dy, dy))) self.assertListEqual(sigln, ([np.std(np.log([100, 150, 100]), ddof=1)] * 3 + [np.std(np.log([200, 275, 300]), ddof=1)] * 10 + [np.std(np.log([300, 315, 200]), ddof=1)] * 8))
def test_write_to_mat(self): thick = [5., 5., 10., 10., 50., 0.] vp = [1500.] * len(thick) vs = [100., 200., 300., 400., 500., 600.] density = [2000.] * len(thick) mygm = swprepost.GroundModel(thick, vp, vs, density) fname = "test" mygm.write_to_mat(fname) data = loadmat(fname) self.assertListEqual(data["thickness"].tolist()[0], thick) self.assertListEqual(data["vp1"].tolist()[0], vp) self.assertListEqual(data["vs1"].tolist()[0], vs) self.assertListEqual(data["rho1"].tolist()[0], density) os.remove(f"{fname}.mat")
def test_vs30(self): # nbest="all" thk = [5, 20, 0] vps = [300, 600, 800] vss = [150, 300, 400] rho = [2000] * 3 gm = swprepost.GroundModel(thk, vps, vss, rho) suite = swprepost.GroundModelSuite(gm) for _ in range(5): suite.append(gm) self.assertListEqual(suite.vs30(), [266.6666666666666666666] * 6) # nbest=3 self.assertListEqual(suite.vs30(nbest=3), [266.6666666666666666666] * 3)
def test_init(self): # One GroundModel gm = swprepost.GroundModel(self.tk_0, self.vp_0, self.vs_0, self.rh_0, identifier=self.id_0, misfit=self.mf_0) returned = swprepost.GroundModelSuite(gm) expected = swprepost.GroundModelSuite(self.gm_0) self.assertEqual(expected, returned) # Bad Value - Wrong Type gm = ["GroundModel"] self.assertRaises(TypeError, swprepost.GroundModelSuite, gm)
def test_str(self): x = [1, 2, 3] y = [2, 4, 5] gm = swprepost.GroundModel(x, y, x, x) suite = swprepost.GroundModelSuite(gm) for _ in range(3): suite.append(gm) expected = "GroundModelSuite with 4 GroundModels." returned = suite.__str__() self.assertEqual(expected, returned) # TODO (jpv): Add a more serious test for slice get_item suite = suite[1:3] expected = "GroundModelSuite with 2 GroundModels." returned = suite.__str__() self.assertEqual(expected, returned)
def test_vs30(self): # One thick layer thick = [50, 0] vp = [600] * len(thick) vs = [100, 100] density = [0] * len(thick) test_val = swprepost.GroundModel(thick, vp, vs, density).vs30 know_val = 100 self.assertEqual(test_val, know_val) # Two layers, same velocity thick = [15, 15, 0] vp = [600] * len(thick) vs = [100, 100, 0] density = [0] * len(thick) test_val = swprepost.GroundModel(thick, vp, vs, density).vs30 know_val = 100 self.assertEqual(test_val, know_val) # One layer, exactly 30m thick = [30, 0] vp = [600] * len(thick) vs = [100, 200] density = [0] * len(thick) test_val = swprepost.GroundModel(thick, vp, vs, density).vs30 know_val = 100 self.assertEqual(test_val, know_val) # Two layers, exactly 30m thick = [15, 15, 0] vp = [600] * len(thick) vs = [100, 200, 300] density = [0] * len(thick) test_val = swprepost.GroundModel(thick, vp, vs, density).vs30 know_val = 133.33 self.assertAlmostEqual(test_val, know_val, places=2) # Two layers, less than 30m thick = [5, 10, 0] vp = [600] * len(thick) vs = [100, 200, 300] density = [0] * len(thick) test_val = swprepost.GroundModel(thick, vp, vs, density).vs30 know_val = 200. self.assertAlmostEqual(test_val, know_val, places=2) # Two layers, less than 30m, with velocity reversal thick = [10, 10, 0] vp = [600] * len(thick) vs = [200, 50, 300] density = [0] * len(thick) test_val = swprepost.GroundModel(thick, vp, vs, density).vs30 know_val = 105.88 self.assertAlmostEqual(test_val, know_val, places=2)
def setUpClass(self): # GroundModelSuite tk = [1, 2, 3] vs = [100, 200, 300] vp = [200, 400, 600] rh = [2000] * 3 gms = [] for _id, _mf in enumerate( [0.5, 0.8, 1, 0.3, 0.4, 0.6, 0.7, 0.1, 0.2, 0.1]): gms.append( swprepost.GroundModel(tk, vp, vs, rh, identifier=_id, misfit=_mf)) self.gm_suite = swprepost.GroundModelSuite.from_list(gms)
def test_simplify(self): tk = [1, 3, 1, 5, 0] vp = [200, 200, 500, 500, 600] vs = [100, 100, 100, 300, 300] rh = [2000] * 5 mygm = swprepost.GroundModel(tk, vp, vs, rh) simp_tk, simp_vp = mygm.simplify(parameter='vp') self.assertListEqual(simp_tk, [1, 3, 6, 0]) self.assertListEqual(simp_vp, [200, 200, 500, 600]) simp_tk, simp_vs = mygm.simplify(parameter='vs') self.assertListEqual(simp_tk, [1, 4, 0]) self.assertListEqual(simp_vs, [100, 100, 300]) simp_tk, simp_rh = mygm.simplify(parameter='rh') self.assertListEqual(simp_tk, [1, 0]) self.assertListEqual(simp_rh, [2000, 2000])
def test_from_geopsy(self): # Single Model tk = [0.68, 9.69, 0.018, 22.8, 43.9, 576.4, 0] vp = [196.7, 295.8, 1600.2, 1600.2, 1600.2, 4232.5, 4232.5] vs = [120.3, 120.3, 120., 231.9, 840.9, 840.9, 2095.3] rh = [2000.] * 7 _id = 149698 mf = 0.766485 expected_0 = swprepost.GroundModel(thickness=tk, vp=vp, vs=vs, density=rh, identifier=_id, misfit=mf) fname = self.full_path + "data/test_gm_mod1.txt" returned_0 = swprepost.GroundModelSuite.from_geopsy(fname=fname)[0] self.assertEqual(expected_0, returned_0) # Two Models tk1 = [0.7, 9.1, 0.1, 21.9, 61.0, 571.8, 0] vp1 = [196.7, 281.4, 1392.1, 1392.1, 1392.1, 4149.1, 4149.1] vs1 = [120.3, 120.3, 120.3, 225.1, 840.9, 840.9, 2202.1] rh1 = [2000.] * 7 _id = 147185 mf = 0.767484 expected_1 = swprepost.GroundModel(thickness=tk1, vp=vp1, vs=vs1, density=rh1, identifier=_id, misfit=mf) fname = self.full_path + "data/test_gm_mod2.txt" returned_1 = swprepost.GroundModelSuite.from_geopsy(fname=fname) self.assertEqual(expected_0, returned_1[0]) self.assertEqual(expected_1, returned_1[1]) # Randomly check the 10th profile (index=9) fname = self.full_path + "data/test_gm_mod100.txt" suite = swprepost.GroundModelSuite.from_geopsy(fname=fname, nmodels=10) tk = [ 0.77397930357999966677, 9.4057659375340758601, 0.10720244308314619275, 22.132593746915929955, 27.312477738315664055, 586.97428362212974662, 0 ] vp = [ 196.72222021325231367, 307.83304440876798935, 1492.6139621303491367, 1492.6139621303491367, 1492.6139621303491367, 4149.1243500998343734, 4149.1243500998343734 ] vs = [ 120.30018967392834384, 120.30018967392834384, 120.30018967392834384, 227.42292146971948341, 832.63107566976702856, 832.63107566976702856, 2116.2608747684203081 ] rh = [2000] * 7 _id = 149535 mf = 0.770783 expected_9 = swprepost.GroundModel(thickness=tk, vp=vp, vs=vs, density=rh, identifier=_id, misfit=mf) self.assertEqual(expected_9, suite[9])
def test_from_geopsy(self): # Single Model tk = [0.68, 9.69, 0.018, 22.8, 43.9, 576.4, 0] vp = [196.7, 295.8, 1600.2, 1600.2, 1600.2, 4232.5, 4232.5] vs = [120.3, 120.3, 120., 231.9, 840.9, 840.9, 2095.3] rh = [2000.] * 7 _id = 149698 mf = 0.766485 expected_0 = swprepost.GroundModel(thickness=tk, vp=vp, vs=vs, density=rh, identifier=_id, misfit=mf) fname = self.path / "data/gm/test_gm_mod1.txt" returned_0 = swprepost.GroundModelSuite.from_geopsy(fname=fname)[0] self.assertEqual(expected_0, returned_0) # Two Models tk1 = [0.7, 9.1, 0.1, 21.9, 61.0, 571.8, 0] vp1 = [196.7, 281.4, 1392.1, 1392.1, 1392.1, 4149.1, 4149.1] vs1 = [120.3, 120.3, 120.3, 225.1, 840.9, 840.9, 2202.1] rh1 = [2000.] * 7 _id = 147185 mf = 0.767484 expected_1 = swprepost.GroundModel(thickness=tk1, vp=vp1, vs=vs1, density=rh1, identifier=_id, misfit=mf) fname = self.path / "data/gm/test_gm_mod2.txt" returned_1 = swprepost.GroundModelSuite.from_geopsy(fname=fname) self.assertEqual(expected_0, returned_1[0]) self.assertEqual(expected_1, returned_1[1]) # Randomly check the 10th profile (index=9) fname = self.path / "data/gm/test_gm_mod100.txt" suite = swprepost.GroundModelSuite.from_geopsy(fname=fname, nmodels=10) tk = [ 0.77397930357999966677, 9.4057659375340758601, 0.10720244308314619275, 22.132593746915929955, 27.312477738315664055, 586.97428362212974662, 0 ] vp = [ 196.72222021325231367, 307.83304440876798935, 1492.6139621303491367, 1492.6139621303491367, 1492.6139621303491367, 4149.1243500998343734, 4149.1243500998343734 ] vs = [ 120.30018967392834384, 120.30018967392834384, 120.30018967392834384, 227.42292146971948341, 832.63107566976702856, 832.63107566976702856, 2116.2608747684203081 ] rh = [2000] * 7 _id = 149535 mf = 0.770783 expected_9 = swprepost.GroundModel(thickness=tk, vp=vp, vs=vs, density=rh, identifier=_id, misfit=mf) self.assertEqual(expected_9, suite[9]) # test 100 models suite = swprepost.GroundModelSuite.from_geopsy( self.path / "data/gm/test_gm_mod100.txt") self.assertTrue(len(suite) == 100) # Request more models than are available. with warnings.catch_warnings(): warnings.simplefilter("ignore") suite = swprepost.GroundModelSuite.from_geopsy( self.path / "data/gm/test_gm_mod100.txt", nmodels=101) self.assertTrue(len(suite) == 100) # Real Examples # ------------- for version in swprepost.meta.SUPPORTED_GEOPSY_VERSIONS: for tar, par in zip(["tar1", "tar12"], ["ln3", "ln7"]): fname = self.path / \ f"data/gm/{tar}_{par}_v{version.replace('.','')}_m100_gm.txt" suite = swprepost.GroundModelSuite.from_geopsy(fname) gm = swprepost.GroundModel.from_geopsy(fname) self.assertEqual(gm, suite[0]) self.assertTrue(len(suite) == 100)
def test_discretize(self): thick = [2., 2., 0.] vp = [1500.] * len(thick) vs = [100., 200., 300.] density = [2000.] * len(thick) mygm = swprepost.GroundModel(thick, vp, vs, density) disc_depth, disc_par = mygm.discretize(5, dy=1.25) self.assertListEqual([0., 1.25, 2.5, 3.75, 5.], disc_depth) self.assertListEqual([100., 100., 200., 200., 300.], disc_par) thick = [2., 2., 0.] vp = [1500.] * len(thick) vs = [100., 200., 300.] density = [2000.] * len(thick) mygm = swprepost.GroundModel(thick, vp, vs, density) disc_depth, disc_par = mygm.discretize(5, dy=1) self.assertListEqual([0., 1., 2., 3., 4., 5.], disc_depth) self.assertListEqual([100., 100., 100., 200., 200., 300.], disc_par) thick = [1.5, 1.5, 0] vp = [1500.] * len(thick) vs = [100., 200., 300.] density = [2000.] * len(thick) mygm = swprepost.GroundModel(thick, vp, vs, density) disc_depth, disc_par = mygm.discretize(5, dy=1) self.assertListEqual([0., 1., 2., 3., 4., 5.], disc_depth) self.assertListEqual([100., 100., 200., 200., 300., 300.], disc_par) thick = [1, 1, 0] vp = [1500.] * len(thick) vs = [100., 200., 300.] density = [2000.] * len(thick) mygm = swprepost.GroundModel(thick, vp, vs, density) disc_depth, disc_par = mygm.discretize(3, dy=0.5) self.assertListEqual([0., 0.5, 1., 1.5, 2., 2.5, 3.], disc_depth) self.assertListEqual([100., 100., 100., 200., 200., 300., 300.], disc_par) thick = [1, 1, 0] vp = [1500.] * len(thick) vs = [100., 200., 300.] density = [2000.] * len(thick) mygm = swprepost.GroundModel(thick, vp, vs, density) disc_depth, disc_par = mygm.discretize(3, dy=0.25) expected = [ 0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2., 2.25, 2.5, 2.75, 3. ] self.assertListEqual(expected, disc_depth) expected = [ 100., 100., 100., 100., 100, 200., 200., 200., 200., 300., 300., 300., 300. ] self.assertListEqual(expected, disc_par) # Check pr thick = [0.75, 0] vp = [200, 400] vs = [100, 200] density = [2000] * 2 mygm = swprepost.GroundModel(thick, vp, vs, density) disc_depth, disc_par = mygm.discretize(1.5, dy=0.5, parameter="pr") self.assertListEqual([0., 0.5, 1., 1.5], disc_depth) self.assertListEqual([0.3333333333333333] * 4, disc_par) # Half-space tk = [0] vp = [1500] vs = [100] rh = [2000] gm = swprepost.GroundModel(tk, vp, vs, rh) disc_depth, disc_vs = gm.discretize(dmax=5, dy=1) expected = [0, 1, 2, 3, 4, 5] self.assertListEqual(expected, disc_depth) expected = [100] * 6 self.assertListEqual(expected, disc_vs) # Fine discretization tk = [5.3, 12.5, 0] vp = [100, 200, 300] vs = [50, 100, 150] rh = [2000] * 3 gm = swprepost.GroundModel(tk, vp, vs, rh) disc_depth, disc_vs = gm.discretize(dmax=50, dy=0.01) expected = np.arange(0, int(50 / 0.01) + 1, 1) * 0.01 self.assertListEqual(expected.tolist(), disc_depth) self.assertEqual(len(disc_depth), len(disc_vs))