In [1]:
Copied!
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import obspy
import requests
sys.path.insert(0, os.path.abspath("../"))
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import obspy
import requests
sys.path.insert(0, os.path.abspath("../"))
In [2]:
Copied!
# DEEPDENOISER_API_URL = "http://127.0.0.1:8002"
# DEEPDENOISER_API_URL = "http://deepdenoiser.quakeflow.com" # gcp
# DEEPDENOISER_API_URL = "http://test.quakeflow.com:8003" # gcp
DEEPDENOISER_API_URL = "https://ai4eps-deepdenoiser.hf.space"
# DEEPDENOISER_API_URL = "http://131.215.74.195:8003" # local machine
# DEEPDENOISER_API_URL = "http://127.0.0.1:8002"
# DEEPDENOISER_API_URL = "http://deepdenoiser.quakeflow.com" # gcp
# DEEPDENOISER_API_URL = "http://test.quakeflow.com:8003" # gcp
DEEPDENOISER_API_URL = "https://ai4eps-deepdenoiser.hf.space"
# DEEPDENOISER_API_URL = "http://131.215.74.195:8003" # local machine
2. Prepare seismic waveforms¶
Find more details in obspy's tutorials:
In [3]:
Copied!
import obspy
stream = obspy.read()
stream.plot();
import obspy
stream = obspy.read()
stream.plot();
In [4]:
Copied!
## Extract 3-component data
stream = stream.sort()
assert(len(stream) == 3)
data = []
for trace in stream:
data.append(trace.data)
data = np.array(data).T
assert(data.shape[-1] == 3)
data_id = stream[0].get_id()[:-1]
timestamp = stream[0].stats.starttime.datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
## Add some noise
noisy_data = data + np.random.randn(*data.shape)*np.max(data)/20
## Extract 3-component data
stream = stream.sort()
assert(len(stream) == 3)
data = []
for trace in stream:
data.append(trace.data)
data = np.array(data).T
assert(data.shape[-1] == 3)
data_id = stream[0].get_id()[:-1]
timestamp = stream[0].stats.starttime.datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
## Add some noise
noisy_data = data + np.random.randn(*data.shape)*np.max(data)/20
3. Get denoised waveforms using DeepDenoiser¶
In [5]:
Copied!
req = {"id": [data_id],
"timestamp": [timestamp],
"vec": [noisy_data.tolist()]}
resp = requests.post(f'{DEEPDENOISER_API_URL}/predict', json=req)
print(resp)
denoised_data = np.array(resp.json()["vec"])
plt.figure(figsize=(10,4))
plt.subplot(331)
plt.plot(data[:,0], 'k', linewidth=0.5, label="E")
plt.legend()
plt.title("Raw signal")
plt.subplot(332)
plt.plot(noisy_data[:,0], 'k', linewidth=0.5, label="E")
plt.title("Nosiy signal")
plt.subplot(333)
plt.plot(denoised_data[0, :,0], 'k', linewidth=0.5, label="E")
plt.title("Denoised signal")
plt.subplot(334)
plt.plot(data[:,1], 'k', linewidth=0.5, label="N")
plt.legend()
plt.subplot(335)
plt.plot(noisy_data[:,1], 'k', linewidth=0.5, label="N")
plt.subplot(336)
plt.plot(denoised_data[0,:,1], 'k', linewidth=0.5, label="N")
plt.subplot(337)
plt.plot(data[:,2], 'k', linewidth=0.5, label="Z")
plt.legend()
plt.subplot(338)
plt.plot(noisy_data[:,2], 'k', linewidth=0.5, label="Z")
plt.subplot(339)
plt.plot(denoised_data[0,:,2], 'k', linewidth=0.5, label="Z")
plt.tight_layout()
plt.show();
req = {"id": [data_id],
"timestamp": [timestamp],
"vec": [noisy_data.tolist()]}
resp = requests.post(f'{DEEPDENOISER_API_URL}/predict', json=req)
print(resp)
denoised_data = np.array(resp.json()["vec"])
plt.figure(figsize=(10,4))
plt.subplot(331)
plt.plot(data[:,0], 'k', linewidth=0.5, label="E")
plt.legend()
plt.title("Raw signal")
plt.subplot(332)
plt.plot(noisy_data[:,0], 'k', linewidth=0.5, label="E")
plt.title("Nosiy signal")
plt.subplot(333)
plt.plot(denoised_data[0, :,0], 'k', linewidth=0.5, label="E")
plt.title("Denoised signal")
plt.subplot(334)
plt.plot(data[:,1], 'k', linewidth=0.5, label="N")
plt.legend()
plt.subplot(335)
plt.plot(noisy_data[:,1], 'k', linewidth=0.5, label="N")
plt.subplot(336)
plt.plot(denoised_data[0,:,1], 'k', linewidth=0.5, label="N")
plt.subplot(337)
plt.plot(data[:,2], 'k', linewidth=0.5, label="Z")
plt.legend()
plt.subplot(338)
plt.plot(noisy_data[:,2], 'k', linewidth=0.5, label="Z")
plt.subplot(339)
plt.plot(denoised_data[0,:,2], 'k', linewidth=0.5, label="Z")
plt.tight_layout()
plt.show();
<Response [200]>
In [ ]:
Copied!