def test_phase_misfit(self): """ Tests all tf misfits with a signal that has phase misfit """ s1 = self.s1 s1p = self.s1p t = self.t dt = self.dt fmin = self.fmin fmax = self.fmax nf = self.nf tfem_11p_ref = np.loadtxt(self.path + os.sep + 'TFEM_11p.dat') tfpm_11p_ref = np.loadtxt(self.path + os.sep + 'TFPM_11p.dat') tem_11p_ref = np.loadtxt(self.path + os.sep + 'TEM_11p.dat') fem_11p_ref = np.loadtxt(self.path + os.sep + 'FEM_11p.dat') fpm_11p_ref = np.loadtxt(self.path + os.sep + 'FPM_11p.dat') tpm_11p_ref = np.loadtxt(self.path + os.sep + 'TPM_11p.dat') em_11p_ref = np.loadtxt(self.path + os.sep + 'EM_11p.dat') pm_11p_ref = np.loadtxt(self.path + os.sep + 'PM_11p.dat') tfem_11p = tfem(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tfpm_11p = tfpm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tem_11p = tem(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) fem_11p = fem(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) fpm_11p = fpm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tpm_11p = tpm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) em_11p = em(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) pm_11p = pm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tol = 1e-5 atol_min = 1e-15 self.assertTrue(np.allclose(tfem_11p, tfem_11p_ref, rtol=tol, atol=np.abs(tfem_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(tfpm_11p, tfpm_11p_ref, rtol=tol, atol=np.abs(tfpm_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(tem_11p, tem_11p_ref, rtol=tol, atol=np.abs(tem_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(fem_11p, fem_11p_ref, rtol=tol, atol=np.abs(fem_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(fpm_11p, fpm_11p_ref, rtol=tol, atol=np.abs(fpm_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(tpm_11p, tpm_11p_ref, rtol=tol, atol=np.abs(tpm_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(em_11p, em_11p_ref, rtol=tol, atol=np.abs(em_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(pm_11p, pm_11p_ref, rtol=tol, atol=np.abs(pm_11p_ref).max() * tol + atol_min))
def test_phase_misfit(self): """ Tests all tf misfits with a signal that has phase misfit """ S1 = self.S1 s1p = self.s1p t = self.t dt = self.dt fmin = self.fmin fmax = self.fmax nf = self.nf TFEM_11p_ref = np.loadtxt(self.path + os.sep + 'TFEM_11p.dat') TFPM_11p_ref = np.loadtxt(self.path + os.sep + 'TFPM_11p.dat') TEM_11p_ref = np.loadtxt(self.path + os.sep + 'TEM_11p.dat') FEM_11p_ref = np.loadtxt(self.path + os.sep + 'FEM_11p.dat') FPM_11p_ref = np.loadtxt(self.path + os.sep + 'FPM_11p.dat') TPM_11p_ref = np.loadtxt(self.path + os.sep + 'TPM_11p.dat') EM_11p_ref = np.loadtxt(self.path + os.sep + 'EM_11p.dat') PM_11p_ref = np.loadtxt(self.path + os.sep + 'PM_11p.dat') TFEM_11p = tfem(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) TFPM_11p = tfpm(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) TEM_11p = tem(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) FEM_11p = fem(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) FPM_11p = fpm(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) TPM_11p = tpm(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) EM_11p = em(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) PM_11p = pm(s1p, S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tol = 1e-5 atol_min = 1e-15 self.assertTrue(np.allclose(TFEM_11p, TFEM_11p_ref, rtol=tol, atol=np.abs(TFEM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(TFPM_11p, TFPM_11p_ref, rtol=tol, atol=np.abs(TFPM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(TEM_11p, TEM_11p_ref, rtol=tol, atol=np.abs(TEM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(FEM_11p, FEM_11p_ref, rtol=tol, atol=np.abs(FEM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(FPM_11p, FPM_11p_ref, rtol=tol, atol=np.abs(FPM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(TPM_11p, TPM_11p_ref, rtol=tol, atol=np.abs(TPM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(EM_11p, EM_11p_ref, rtol=tol, atol=np.abs(EM_11p_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(PM_11p, PM_11p_ref, rtol=tol, atol=np.abs(PM_11p_ref).max() * tol + atol_min))
def test_envelope_misfit(self): """ Tests all tf misfits with a signal that has envelope misfit """ S1 = self.S1 S1a = self.S1a t = self.t dt = self.dt fmin = self.fmin fmax = self.fmax nf = self.nf TFEM_11a_ref = np.loadtxt(self.path + os.sep + "TFEM_11a.dat") TFPM_11a_ref = np.loadtxt(self.path + os.sep + "TFPM_11a.dat") TEM_11a_ref = np.loadtxt(self.path + os.sep + "TEM_11a.dat") FEM_11a_ref = np.loadtxt(self.path + os.sep + "FEM_11a.dat") FPM_11a_ref = np.loadtxt(self.path + os.sep + "FPM_11a.dat") TPM_11a_ref = np.loadtxt(self.path + os.sep + "TPM_11a.dat") EM_11a_ref = np.loadtxt(self.path + os.sep + "EM_11a.dat") PM_11a_ref = np.loadtxt(self.path + os.sep + "PM_11a.dat") TFEM_11a = tfem(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) TFPM_11a = tfpm(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) TEM_11a = tem(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) TPM_11a = tpm(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) FEM_11a = fem(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) FPM_11a = fpm(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) EM_11a = em(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) PM_11a = pm(S1a(t), S1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tol = 1e-5 atol_min = 1e-15 self.assertTrue(np.allclose(TFEM_11a, TFEM_11a_ref, rtol=tol, atol=np.abs(TFEM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(TFPM_11a, TFPM_11a_ref, rtol=tol, atol=np.abs(TFPM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(TEM_11a, TEM_11a_ref, rtol=tol, atol=np.abs(TEM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(FEM_11a, FEM_11a_ref, rtol=tol, atol=np.abs(FEM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(FPM_11a, FPM_11a_ref, rtol=tol, atol=np.abs(FPM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(TPM_11a, TPM_11a_ref, rtol=tol, atol=np.abs(TPM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(EM_11a, EM_11a_ref, rtol=tol, atol=np.abs(EM_11a_ref).max() * tol + atol_min)) self.assertTrue(np.allclose(PM_11a, PM_11a_ref, rtol=tol, atol=np.abs(PM_11a_ref).max() * tol + atol_min))
def phase_misfit(tr1, tr2): """ Single valued phase misfit after Kristekova 2009. """ # XXX: This needs some way to configure it! # Go from 5 times Nyquist to 100 seconds. nyquist = tr1.stats.sampling_rate * 0.5 f_max = nyquist / 5 f_min = 1.0 / 100.0 value = tf_misfit.pm(tr1.data, tr2.data, tr1.stats.delta, fmin=f_min, nf=10, fmax=f_max) return { "name": "phase_misfit", "pretty_name": "Time Frequency Phase Misfit", "logarithmic_plot": False, "value": value, "minimizing_misfit": True }
def comp_obspy_tf_misfit(model, site_name, dt, comps='XYZ', fmin=0.15, fmax=5, nf=128, vel=None, vel_rec=None, plot=False): """See doc in `comp_obspy_tf_misfits` for details """ if vel is None: with open('results/vel_syn.pickle', 'rb') as fid: vel_syn = pickle.load(fid) vel = vel_syn[model][site_name] vel_rec = vel_syn["rec"][site_name] resize(vel, vel_rec, dt) res = {} res['dt'] = dt res['f'] = np.logspace(np.log10(fmin), np.log10(fmax), nf) for comp in comps: res[comp] = {} res[comp]['syn'] = vel[comp] res[comp]['rec'] = vel_rec[comp] res[comp]['em'] = em(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['pm'] = pm(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['tfem'] = tfem(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['tfpm'] = tfpm(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['tem'] = tem(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['tpm'] = tpm(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['fem'] = fem(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res[comp]['fpm'] = fpm(vel[comp], vel_rec[comp], dt, fmin, fmax, nf) res['t'] = np.arange(res[comp]['tem'].shape[-1]) * dt if plot: plot_obspy_tf_misfit(res, comps=comps) return res
def test_phase_misfit(self, state): """ Tests all tf misfits with a signal that has phase misfit """ s1 = state['s1'] s1p = state['s1p'] t = state['t'] dt = state['dt'] fmin = state['fmin'] fmax = state['fmax'] nf = state['nf'] tfem_11p_ref = np.loadtxt(self.path + os.sep + 'TFEM_11p.dat') tfpm_11p_ref = np.loadtxt(self.path + os.sep + 'TFPM_11p.dat') tem_11p_ref = np.loadtxt(self.path + os.sep + 'TEM_11p.dat') fem_11p_ref = np.loadtxt(self.path + os.sep + 'FEM_11p.dat') fpm_11p_ref = np.loadtxt(self.path + os.sep + 'FPM_11p.dat') tpm_11p_ref = np.loadtxt(self.path + os.sep + 'TPM_11p.dat') em_11p_ref = np.loadtxt(self.path + os.sep + 'EM_11p.dat') pm_11p_ref = np.loadtxt(self.path + os.sep + 'PM_11p.dat') tfem_11p = tfem(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tfpm_11p = tfpm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tem_11p = tem(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) fem_11p = fem(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) fpm_11p = fpm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tpm_11p = tpm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) em_11p = em(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) pm_11p = pm(s1p, s1(t), dt=dt, fmin=fmin, fmax=fmax, nf=nf) tol = 1e-5 atol_min = 1e-15 assert np.allclose(tfem_11p, tfem_11p_ref, rtol=tol, atol=np.abs(tfem_11p_ref).max() * tol + atol_min) assert np.allclose(tfpm_11p, tfpm_11p_ref, rtol=tol, atol=np.abs(tfpm_11p_ref).max() * tol + atol_min) assert np.allclose(tem_11p, tem_11p_ref, rtol=tol, atol=np.abs(tem_11p_ref).max() * tol + atol_min) assert np.allclose(fem_11p, fem_11p_ref, rtol=tol, atol=np.abs(fem_11p_ref).max() * tol + atol_min) assert np.allclose(fpm_11p, fpm_11p_ref, rtol=tol, atol=np.abs(fpm_11p_ref).max() * tol + atol_min) assert np.allclose(tpm_11p, tpm_11p_ref, rtol=tol, atol=np.abs(tpm_11p_ref).max() * tol + atol_min) assert np.allclose(em_11p, em_11p_ref, rtol=tol, atol=np.abs(em_11p_ref).max() * tol + atol_min) assert np.allclose(pm_11p, pm_11p_ref, rtol=tol, atol=np.abs(pm_11p_ref).max() * tol + atol_min)
def _process(self, inputs): self.log("In process Misfit") ip = inputs["input"] syn = ip["synthetic_trace"] obs = ip["data_trace"] param = ip["parameters"] self.log("Starting misfit %s, freqmin %s freqmax %s" % (obs.id, param["min_period"], param["max_period"])) nf = 100 # Plot. fig = plotTfMisfits( obs.data, syn.data, dt=obs.stats.delta, nf=nf, fmin=1.0 / param["max_period"], fmax=1.0 / param["min_period"], w0=param["wavelet_parameter"], show=False) # Calculate misfit values again. pm_inf = pm( obs.data, syn.data, dt=obs.stats.delta, nf=nf, fmin=1.0 / param["max_period"], fmax=1.0 / param["min_period"], w0=param["wavelet_parameter"]) em_inf = em( obs.data, syn.data, dt=obs.stats.delta, nf=nf, fmin=1.0 / param["max_period"], fmax=1.0 / param["min_period"], w0=param["wavelet_parameter"]) data = ip["data_trace"] station_id = "%s.%s" % (data.stats.network, data.stats.station) component = data.stats.channel[-1] # Metadata for Alessandro! meta = [{ "type": "time_frequency_misfit", "station_id": station_id, "component": component, "phase_misfit": pm_inf, "envelope_misfit": em_inf }] # self.write("misfit_values", { # "record_type": "time_frequency_misfit", # "output_folder": ip["output_folder"], # "station_id": station_id, # "component": component, # "phase_misfit": pm_inf, # "envelope_misfit": em_inf},metadata=metadata) fig.suptitle("Component:" + component, fontsize=15) with io.BytesIO() as buf: fig.savefig(buf) buf.seek(0, 0) # Not Python 3 compatible but who cares. image_string = buf.read() plt.close(fig) image_type = "time_frequency_misfit" self.write( "image", ({"image_string": image_string, "component": component, "output_folder": ip["output_folder"]}, image_type, station_id),metadata=meta)