def test_ExtractComponent(): seis = Seismogram() seis.live = 1 seis.data = dmatrix(np.random.rand(3, 6)) seis.npts = 6 ts = [] for i in range(3): ts.append(ExtractComponent(seis, i)) for i in range(3): assert (ts[i].data == seis.data[i]).all()
def get_live_seismogram(ts_size=255, sampling_rate=20.0): seis = Seismogram() seis.set_live() seis.set_as_origin("test", "0", "0", AtomicType.SEISMOGRAM) seis.dt = 1 / sampling_rate seis.npts = ts_size # seis.put('net', 'IU') seis.put("npts", ts_size) seis.put("sampling_rate", sampling_rate) seis.tref = TimeReferenceType.UTC seis.t0 = 0 seis["delta"] = 0.1 seis["calib"] = 0.1 seis["site_id"] = bson.objectid.ObjectId() seis["source_id"] = bson.objectid.ObjectId() seis.data = dmatrix(3, ts_size) for i in range(3): for j in range(ts_size): seis.data[i, j] = np.random.rand() return seis
def get_live_seismogram(): seis = Seismogram() seis.set_live() seis.set_as_origin('test', '0', '0', AtomicType.SEISMOGRAM) seis.dt = 1 / sampling_rate seis.npts = ts_size # seis.put('net', 'IU') seis.put('npts', ts_size) seis.put('sampling_rate', sampling_rate) seis.tref = TimeReferenceType.UTC seis.t0 = 0 seis['delta'] = 0.1 seis['calib'] = 0.1 seis['site_id'] = bson.objectid.ObjectId() seis['channel_id'] = [bson.objectid.ObjectId()] seis['source_id'] = bson.objectid.ObjectId() seis.data = dmatrix(3, ts_size) for i in range(3): for j in range(ts_size): seis.data[i, j] = np.random.rand() return seis
def test_Seismogram(): seis = Seismogram() seis.npts = 100 assert seis.data.rows() == 3 assert seis.data.columns() == 100 seis.t0 = 0.0 seis.dt = 0.001 seis.live = 1 seis.tref = TimeReferenceType.Relative seis.data = dmatrix(np.random.rand(3, 6)) assert seis.npts != 6 seis.sync_npts() assert seis.npts == 6 assert seis.npts == seis["npts"] seis.npts = 4 assert seis.data.columns() == 4 seis.npts = 10 assert (seis.data[0:3, 4:10] == 0).all() seis.data = dmatrix(np.random.rand(3, 100)) seis.sync_npts() seis_copy = pickle.loads(pickle.dumps(seis)) assert seis_copy.t0 == seis.t0 assert seis_copy.dt == seis.dt assert seis_copy.live == seis.live assert seis_copy.tref == seis.tref assert (seis_copy.data[:] == seis.data[:]).all() # test the += operator seis1 = Seismogram(seis) seis2 = Seismogram(seis) seis1 += seis2 assert (np.isclose(seis1.data[:], seis.data + seis.data)).all() seis.npts = 0 assert seis.data.rows() == 0 seis.npts = 100 for i in range(3): for j in range(100): if i == 0: seis.data[i, j] = 1.0 else: seis.data[i, j] = 0.0 seis.data[0, 1] = 1.0 seis.data[0, 2] = 1.0 seis.data[0, 3] = 0.0 seis.data[1, 1] = 1.0 seis.data[1, 2] = 1.0 seis.data[1, 3] = 0.0 seis.data[2, 1] = 1.0 seis.data[2, 2] = 0.0 seis.data[2, 3] = 1.0 sc = SphericalCoordinate() sc.phi = 0.0 sc.theta = np.pi / 4 seis.rotate(sc) assert all(np.isclose(seis.data[:, 3], [0, -0.707107, 0.707107])) seis.rotate_to_standard() assert all(seis.data[:, 3] == [0, 0, 1]) sc.phi = -np.pi / 4 seis.data[:, 3] = sc.unit_vector seis.rotate(sc) assert all(seis.data[:, 3] == [0, 0, 1]) seis.rotate_to_standard() assert all(np.isclose(seis.data[:, 3], [0.5, -0.5, 0.707107])) seis.data[:, 3] = [0, 0, 1] nu = [np.sqrt(3.0) / 3.0, np.sqrt(3.0) / 3.0, np.sqrt(3.0) / 3.0] seis.rotate(nu) assert (np.isclose( seis.tmatrix, np.array([ [0.70710678, -0.70710678, 0.0], [0.40824829, 0.40824829, -0.81649658], [0.57735027, 0.57735027, 0.57735027], ]), )).all() assert all(np.isclose(seis.data[:, 0], [0.707107, 0.408248, 0.57735])) assert all(np.isclose(seis.data[:, 1], [0, 0, 1.73205])) seis.rotate_to_standard() assert all(np.isclose(seis.data[:, 0], [1, 0, 0])) assert all(np.isclose(seis.data[:, 1], [1, 1, 1])) nu = [np.sqrt(3.0) / 3.0, np.sqrt(3.0) / 3.0, np.sqrt(3.0) / 3.0] seis.rotate(SphericalCoordinate(nu)) assert (np.isclose( seis.tmatrix, np.array([ [0.70710678, -0.70710678, 0.0], [0.40824829, 0.40824829, -0.81649658], [0.57735027, 0.57735027, 0.57735027], ]), )).all() assert all(np.isclose(seis.data[:, 0], [0.707107, 0.408248, 0.57735])) assert all(np.isclose(seis.data[:, 1], [0, 0, 1.73205])) seis.rotate_to_standard() assert all(np.isclose(seis.data[:, 0], [1, 0, 0])) assert all(np.isclose(seis.data[:, 1], [1, 1, 1])) sc.phi = np.pi / 4 sc.theta = 0.0 seis.rotate(sc) assert (np.isclose( seis.tmatrix, np.array([ [0.70710678, -0.70710678, 0.0], [0.70710678, 0.70710678, 0.0], [0.0, 0.0, 1.0], ]), )).all() assert all(np.isclose(seis.data[:, 0], [0.707107, 0.707107, 0])) assert all(np.isclose(seis.data[:, 1], [0, 1.41421, 1])) assert all(np.isclose(seis.data[:, 2], [0, 1.41421, 0])) assert all(np.isclose(seis.data[:, 3], [0, 0, 1])) seis.rotate_to_standard() # test for serialization of SphericalCoordinate sc_copy = pickle.loads(pickle.dumps(sc)) assert sc_copy.radius == sc.radius assert sc_copy.theta == sc.theta assert sc_copy.phi == sc.phi a = np.zeros((3, 3)) a[0][0] = 1.0 a[0][1] = 1.0 a[0][2] = 1.0 a[1][0] = -1.0 a[1][1] = 1.0 a[1][2] = 1.0 a[2][0] = 0.0 a[2][1] = -1.0 a[2][2] = 0.0 seis.transform(a) assert all(np.isclose(seis.data[:, 0], [1, -1, 0])) assert all(np.isclose(seis.data[:, 1], [3, 1, -1])) assert all(np.isclose(seis.data[:, 2], [2, 0, -1])) assert all(np.isclose(seis.data[:, 3], [1, 1, 0])) seis_copy = pickle.loads(pickle.dumps(seis)) seis_copy.rotate_to_standard() assert all(np.isclose(seis_copy.data[:, 0], [1, 0, 0])) assert all(np.isclose(seis_copy.data[:, 1], [1, 1, 1])) assert all(np.isclose(seis_copy.data[:, 2], [1, 1, 0])) assert all(np.isclose(seis_copy.data[:, 3], [0, 0, 1])) seis.rotate_to_standard() seis.rotate(np.pi / 4) seis.transform(a) assert (np.isclose( seis.tmatrix, np.array([[1.41421, 0.0, 1], [0.0, 1.41421, 1], [-0.707107, -0.707107, 0]]), )).all() assert all(np.isclose(seis.data[:, 0], [1.41421, 0, -0.707107])) assert all(np.isclose(seis.data[:, 1], [2.41421, 2.41421, -1.41421])) assert all(np.isclose(seis.data[:, 2], [1.41421, 1.41421, -1.41421])) assert all(np.isclose(seis.data[:, 3], [1, 1, 0])) seis.rotate_to_standard() assert all(np.isclose(seis.data[:, 0], [1, 0, 0])) assert all(np.isclose(seis.data[:, 1], [1, 1, 1])) assert all(np.isclose(seis.data[:, 2], [1, 1, 0])) assert all(np.isclose(seis.data[:, 3], [0, 0, 1])) uvec = SlownessVector() uvec.ux = 0.17085 # cos(-20deg)/5.5 uvec.uy = -0.062185 # sin(-20deg)/5.5 seis.free_surface_transformation(uvec, 5.0, 3.5) assert (np.isclose( seis.tmatrix, np.array([ [-0.171012, -0.469846, 0], [0.115793, -0.0421458, 0.445447], [-0.597975, 0.217647, 0.228152], ]), )).all() seis.tmatrix = a assert (seis.tmatrix == a).all() # test for serialization of SlownessVector uvec_copy = pickle.loads(pickle.dumps(uvec)) assert uvec_copy.ux == uvec.ux assert uvec_copy.uy == uvec.uy assert uvec_copy.azimuth() == uvec.azimuth()
def test_CoreSeismogram(): md = Metadata() md["delta"] = 0.01 md["starttime"] = 0.0 md["npts"] = 100 # test metadata constructor md["tmatrix"] = np.random.rand(3, 3) cseis = _CoreSeismogram(md, False) assert (cseis.tmatrix == md["tmatrix"]).all() md["tmatrix"] = dmatrix(np.random.rand(3, 3)) cseis = _CoreSeismogram(md, False) assert (cseis.tmatrix == md["tmatrix"]).all() md["tmatrix"] = np.random.rand(9) cseis = _CoreSeismogram(md, False) assert (cseis.tmatrix == md["tmatrix"].reshape(3, 3)).all() md["tmatrix"] = np.random.rand(1, 9) cseis = _CoreSeismogram(md, False) assert (cseis.tmatrix == md["tmatrix"].reshape(3, 3)).all() md["tmatrix"] = np.random.rand(9, 1) cseis = _CoreSeismogram(md, False) assert (cseis.tmatrix == md["tmatrix"].reshape(3, 3)).all() md["tmatrix"] = np.random.rand(3, 3).tolist() cseis = _CoreSeismogram(md, False) assert np.isclose(cseis.tmatrix, np.array(md["tmatrix"]).reshape(3, 3)).all() md["tmatrix"] = np.random.rand(9).tolist() cseis = _CoreSeismogram(md, False) assert np.isclose(cseis.tmatrix, np.array(md["tmatrix"]).reshape(3, 3)).all() # test whether the setter of tmatrix updates metadata correctly tm = np.random.rand(1, 9) cseis.tmatrix = tm assert (cseis.tmatrix == tm.reshape(3, 3)).all() assert np.isclose(cseis.tmatrix, np.array(cseis["tmatrix"]).reshape(3, 3)).all() tm = np.random.rand(9).tolist() cseis.tmatrix = tm assert np.isclose(cseis.tmatrix, np.array(tm).reshape(3, 3)).all() assert np.isclose(cseis.tmatrix, np.array(cseis["tmatrix"]).reshape(3, 3)).all() # test exceptions md["tmatrix"] = np.random.rand(4, 2) with pytest.raises(MsPASSError, match="should be a 3x3 matrix"): _CoreSeismogram(md, False) md["tmatrix"] = dmatrix(np.random.rand(2, 4)) with pytest.raises(MsPASSError, match="should be a 3x3 matrix"): _CoreSeismogram(md, False) md["tmatrix"] = 42 with pytest.raises(MsPASSError, match="not recognized"): _CoreSeismogram(md, False) md.erase("tmatrix") # tmatrix not defined is taken to default to tmatrix being an identity # matrix. We test that condition here cseis = _CoreSeismogram(md, False) assert np.isclose(cseis.tmatrix, np.eye(3)).all() md["tmatrix"] = {4: 2} with pytest.raises(MsPASSError, match="type is not recognized"): _CoreSeismogram(md, False) md["tmatrix"] = np.random.rand(9).tolist() md["tmatrix"][3] = "str" with pytest.raises(MsPASSError, match="should be float"): _CoreSeismogram(md, False) md["tmatrix"] = np.random.rand(3, 4).tolist() with pytest.raises(MsPASSError, match="should be a 3x3 list of list"): _CoreSeismogram(md, False) md["tmatrix"] = [1, 2, 3] with pytest.raises(MsPASSError, match="should be a 3x3 list of list"): _CoreSeismogram(md, False) md["tmatrix"] = np.random.rand(2, 2).tolist() with pytest.raises( MsPASSError, match="should be a list of 9 floats or a 3x3 list of list"): _CoreSeismogram(md, False) md["tmatrix"] = np.random.rand(3, 3).tolist() md["tmatrix"][1][1] = "str" with pytest.raises(MsPASSError, match="should be float"): _CoreSeismogram(md, False)
def test_dmatrix(): dm = dmatrix() assert dm.rows() == 0 dm = dmatrix(9, 4) assert dm.rows() == 9 assert dm.columns() == 4 assert dm.size == 4 * 9 assert len(dm) == 9 assert dm.shape == (9, 4) md = [array.array("l", (0 for _ in range(5))) for _ in range(3)] for i in range(3): for j in range(5): md[i][j] = i * 5 + j dm = dmatrix(md) assert np.equal(dm, md).all() dm_c = dmatrix(dm) assert (dm_c[:] == dm).all() dm_c.zero() assert not dm_c[:].any() md = np.zeros((7, 4), dtype=np.double, order="F") for i in range(7): for j in range(4): md[i][j] = i * 4 + j dm = dmatrix(md) assert (dm == md).all() assert (dm.transpose() == md.transpose()).all() assert (dm * 3.14 == md * 3.14).all() assert (2.17 * dm == 2.17 * md).all() assert (dm * dm.transpose() == np.matmul(md, md.transpose())).all() with pytest.raises(MsPASSError, match="size mismatch"): dm * dm dm_c = dmatrix(dm) dm += dm_c assert (dm == md + md).all() dm += md assert (dm == md + md + md).all() assert type(dm) == dmatrix dm -= dm_c dm -= dm_c dm -= md assert not dm[:].any() assert type(dm) == dmatrix dm_c = dmatrix(dm) md = np.zeros((7, 4), dtype=np.single, order="C") for i in range(7): for j in range(4): md[i][j] = i * 4 + j dm = dmatrix(md) assert (dm == md).all() md = np.zeros((7, 4), dtype=np.int, order="F") for i in range(7): for j in range(4): md[i][j] = i * 4 + j dm = dmatrix(md) assert (dm == md).all() md = np.zeros((7, 4), dtype=np.unicode_, order="C") for i in range(7): for j in range(4): md[i][j] = i * 4 + j dm = dmatrix(md) assert (dm == np.float_(md)).all() md = np.zeros((53, 37), dtype=np.double, order="C") for i in range(53): for j in range(37): md[i][j] = i * 37 + j dm = dmatrix(md) assert dm[17, 23] == md[17, 23] assert (dm[17] == md[17]).all() assert (dm[::] == md[::]).all() assert (dm[3::] == md[3::]).all() assert (dm[:5:] == md[:5:]).all() assert (dm[::7] == md[::7]).all() assert (dm[-3::] == md[-3::]).all() assert (dm[:-5:] == md[:-5:]).all() assert (dm[::-7] == md[::-7]).all() assert (dm[11:41:7] == md[11:41:7]).all() assert (dm[-11:-41:-7] == md[-11:-41:-7]).all() assert (dm[3::, 13] == md[3::, 13]).all() assert (dm[19, :5:] == md[19, :5:]).all() assert (dm[::-7, ::-11] == md[::-7, ::-11]).all() with pytest.raises(IndexError, match="out of bounds for axis 1"): dummy = dm[3, 50] with pytest.raises(IndexError, match="out of bounds for axis 0"): dummy = dm[80] with pytest.raises(IndexError, match="out of bounds for axis 1"): dm[3, 50] = 1.0 with pytest.raises(IndexError, match="out of bounds for axis 0"): dm[60, 50] = 1 dm[7, 17] = 3.14 assert dm[7, 17] == 3.14 dm[7, 17] = "6.28" assert dm[7, 17] == 6.28 dm[7] = 10 assert (dm[7] == 10).all() dm[::] = md assert (dm == md).all() dm[:, -7] = 3.14 assert (dm[:, -7] == 3.14).all() dm[17, :] = 3.14 assert (dm[17, :] == 3.14).all() dm[3:7, -19:-12] = 3.14 assert (dm[3:7, -19:-12] == 3.14).all()
def setup_function(function): ts_size = 255 sampling_rate = 20.0 function.dict1 = { "network": "IU", "station": "ANMO", "starttime": obspy.UTCDateTime(2019, 12, 31, 23, 59, 59, 915000), "npts": ts_size, "sampling_rate": sampling_rate, "channel": "BHE", "live": True, "_id": bson.objectid.ObjectId(), "jdate": obspy.UTCDateTime(2019, 12, 31, 23, 59, 59, 915000), "date_str": obspy.UTCDateTime(2019, 12, 31, 23, 59, 59, 915000), "not_defined_date": obspy.UTCDateTime(2019, 12, 31, 23, 59, 59, 915000), } function.dict2 = { "network": "IU", "station": "ANMO", "starttime": obspy.UTCDateTime(2019, 12, 31, 23, 59, 59, 915000), "npts": ts_size, "sampling_rate": sampling_rate, "channel": "BHN", } function.dict3 = { "network": "IU", "station": "ANMO", "starttime": obspy.UTCDateTime(2019, 12, 31, 23, 59, 59, 915000), "npts": ts_size, "sampling_rate": sampling_rate, "channel": "BHZ", } function.tr1 = obspy.Trace(data=np.random.randint(0, 1000, ts_size), header=function.dict1) function.tr2 = obspy.Trace(data=np.random.randint(0, 1000, ts_size), header=function.dict2) function.tr3 = obspy.Trace(data=np.random.randint(0, 1000, ts_size), header=function.dict3) function.stream = obspy.Stream( traces=[function.tr1, function.tr2, function.tr3]) function.md1 = Metadata() function.md1.put("network", "IU") function.md1.put("npts", ts_size) function.md1.put("sampling_rate", sampling_rate) function.md1.put("live", True) function.ts1 = TimeSeries() function.ts1.data = DoubleVector(np.random.rand(ts_size)) function.ts1.live = True function.ts1.dt = 1 / sampling_rate function.ts1.t0 = 0 function.ts1.npts = ts_size # TODO: need to bind the constructor that can do TimeSeries(md1) function.ts1.put("net", "IU") function.ts1.put("npts", ts_size) function.ts1.put("sampling_rate", sampling_rate) function.seismogram = Seismogram() # TODO: the default of seismogram.tref is UTC which is inconsistent with the default # for TimeSeries() # TODO: It would be nice to have dmatrix support numpy.ndarray as input function.seismogram.data = dmatrix(3, ts_size) for i in range(3): for j in range(ts_size): function.seismogram.data[i, j] = np.random.rand() function.seismogram.live = True function.seismogram.dt = 1 / sampling_rate function.seismogram.t0 = 0 function.seismogram.npts = ts_size # FIXME: if the following key is network, the Seismogram2Stream will error out # when calling TimeSeries2Trace internally due to the issue when mdef.is_defined(k) # returns True but k is an alias, the mdef.type(k) will error out. function.seismogram.put("net", "IU") function.seismogram.put("npts", ts_size) function.seismogram.put("sampling_rate", sampling_rate)
def test_CoreSeismogram(): md = Metadata() md['delta'] = 0.01 md['starttime'] = 0.0 md['npts'] = 100 # test metadata constructor md['tmatrix'] = np.random.rand(3, 3) cseis = _CoreSeismogram(md, False) assert (cseis.transformation_matrix == md['tmatrix']).all() md['tmatrix'] = dmatrix(np.random.rand(3, 3)) cseis = _CoreSeismogram(md, False) assert (cseis.transformation_matrix == md['tmatrix']).all() md['tmatrix'] = np.random.rand(9) cseis = _CoreSeismogram(md, False) assert (cseis.transformation_matrix == md['tmatrix'].reshape(3, 3)).all() md['tmatrix'] = np.random.rand(1, 9) cseis = _CoreSeismogram(md, False) assert (cseis.transformation_matrix == md['tmatrix'].reshape(3, 3)).all() md['tmatrix'] = np.random.rand(9, 1) cseis = _CoreSeismogram(md, False) assert (cseis.transformation_matrix == md['tmatrix'].reshape(3, 3)).all() md['tmatrix'] = np.random.rand(3, 3).tolist() cseis = _CoreSeismogram(md, False) assert np.isclose(cseis.transformation_matrix, np.array(md['tmatrix']).reshape(3, 3)).all() md['tmatrix'] = np.random.rand(9).tolist() cseis = _CoreSeismogram(md, False) assert np.isclose(cseis.transformation_matrix, np.array(md['tmatrix']).reshape(3, 3)).all() # test whether the setter of transformation_matrix updates metadata correctly tm = np.random.rand(1, 9) cseis.transformation_matrix = tm assert (cseis.transformation_matrix == tm.reshape(3, 3)).all() assert np.isclose(cseis.transformation_matrix, np.array(cseis['tmatrix']).reshape(3, 3)).all() tm = np.random.rand(9).tolist() cseis.transformation_matrix = tm assert np.isclose(cseis.transformation_matrix, np.array(tm).reshape(3, 3)).all() assert np.isclose(cseis.transformation_matrix, np.array(cseis['tmatrix']).reshape(3, 3)).all() # test exceptions md['tmatrix'] = np.random.rand(4, 2) with pytest.raises(MsPASSError, match="should be a 3x3 matrix"): _CoreSeismogram(md, False) md['tmatrix'] = dmatrix(np.random.rand(2, 4)) with pytest.raises(MsPASSError, match="should be a 3x3 matrix"): _CoreSeismogram(md, False) md['tmatrix'] = 42 with pytest.raises(MsPASSError, match="not recognized"): _CoreSeismogram(md, False) md.erase('tmatrix') with pytest.raises(MsPASSError, match="Error trying to extract"): _CoreSeismogram(md, False) md['tmatrix'] = {4: 2} with pytest.raises(MsPASSError, match="type is not recognized"): _CoreSeismogram(md, False) md['tmatrix'] = np.random.rand(9).tolist() md['tmatrix'][3] = 'str' with pytest.raises(MsPASSError, match="should be float"): _CoreSeismogram(md, False) md['tmatrix'] = np.random.rand(3, 4).tolist() with pytest.raises(MsPASSError, match="should be a 3x3 list of list"): _CoreSeismogram(md, False) md['tmatrix'] = [1, 2, 3] with pytest.raises(MsPASSError, match="should be a 3x3 list of list"): _CoreSeismogram(md, False) md['tmatrix'] = np.random.rand(2, 2).tolist() with pytest.raises( MsPASSError, match="should be a list of 9 floats or a 3x3 list of list"): _CoreSeismogram(md, False) md['tmatrix'] = np.random.rand(3, 3).tolist() md['tmatrix'][1][1] = 'str' with pytest.raises(MsPASSError, match="should be float"): _CoreSeismogram(md, False)