def test_cumulative_absolute_velocity(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) acc_signal.generate_cumulative_stats() true_cav = 8.53872 assert np.isclose(acc_signal.cav, true_cav, rtol=0.0001)
def test_arias_intensity(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) acc_signal.generate_cumulative_stats() true_arias_intensity = 0.63398 assert np.isclose(acc_signal.arias_intensity, true_arias_intensity, rtol=0.0001)
def show_test_motion(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) acc_signal.generate_displacement_and_velocity_series() bf, sp = plt.subplots(3) sp[0].plot(acc_signal.time, acc_signal.values) sp[1].plot(acc_signal.time, acc_signal.velocity) sp[2].plot(acc_signal.time, acc_signal.displacement) plt.show()
def test_duration_stats(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) acc_signal.generate_duration_stats() assert np.isclose(acc_signal.t_595, 20.99) # eqsig==0.5.0 assert np.isclose(acc_signal.t_b01, 38.27) # eqsig==0.5.0 assert np.isclose(acc_signal.t_b05, 15.41) # eqsig==0.5.0 assert np.isclose(acc_signal.t_b10, 8.41) # eqsig==0.5.0
def test_stockwell_transform_then_inverse(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename, skiprows=2) acc_signal = AccSignal(rec, motion_step) acc2_signal = interp_to_approx_dt(acc_signal, 0.1) acc2_signal.swtf = stockwell.transform(acc2_signal.values) assert len(acc2_signal.swtf[0]) == acc2_signal.npts inv_signal = AccSignal(stockwell.itransform(acc2_signal.swtf), acc2_signal.dt) norm_abs_error = np.sum(abs(acc2_signal.values - inv_signal.values)) / acc2_signal.npts assert norm_abs_error < 0.008
def test_peak_values(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) acc_signal.generate_peak_values() true_pga = 1.41 true_pgv = 0.26006 true_pgd = 0.07278134 # eqsig==0.4.12 assert np.isclose(acc_signal.pga, true_pga, rtol=0.001) assert np.isclose(acc_signal.pgv, true_pgv, rtol=0.0001), acc_signal.pgv assert np.isclose(acc_signal.pgd, true_pgd, rtol=0.0001), acc_signal.pgd
def __init__(self, values, dt, names=[], master_index=0, stypes="custom", **kwargs): self.freq_range = np.array(kwargs.get('freq_range', [0.1, 20])) lvt = np.log10(1.0 / self.freq_range) if stypes == "custom" or stypes == "acc": stypes = [stypes] * len(values) self.response_times = kwargs.get('resp_times', np.logspace(lvt[1], lvt[0], 31, base=10)) if master_index < 0: raise ValueError("master_index must be positive") if master_index > len(values) - 1: raise ValueError("master_index: %i, out of bounds, maximum value: %i" % (master_index, len(signals) - 1)) self.master_index = master_index # if len(signals) != len(steps): # raise ValueError("Length of signals: %i, must match length of steps: %i" % (len(signals), len(steps))) # self.master = Record(master_motion, master_step, response_times=self.response_times) shortage = len(values) - len(names) self.names = list(names) for i in range(shortage): self.names.append("m%i" % (len(names) + i)) self.master = self.names[master_index] self.dt = dt self.signals = OrderedDict() for s in range(len(values)): if stypes[s] == "acc": self.signals[self.names[s]] = AccSignal(values[s], dt, self.names[s], response_times=self.response_times) else: self.signals[self.names[s]] = Signal(values[s], dt, self.names[s])
def test_fourier_spectra_with_motion(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_dt = 0.01 rec = np.loadtxt(record_path + record_filename, skiprows=2) rec2 = np.zeros(2**13) rec2[:len(rec)] = rec asig = AccSignal(-rec, motion_dt) nfreq = len(asig.fa_spectrum) test_filename = 'test_motion_true_fourier_spectra.csv' data = np.loadtxt(record_path + test_filename, skiprows=1, delimiter=",") freqs = data[:nfreq - 1, 0] fa = data[:nfreq - 1, 1] phase = data[:nfreq - 1, 2] fa_eqsig = abs(asig.fa_spectrum) freq_eqsig = asig.fa_frequencies org_phases = np.angle(asig.fa_spectrum) ss_phases = np.angle(np.fft.rfft(rec2))[:len(org_phases)] + 0.0001 assert np.isclose(freqs[0], freq_eqsig[1], rtol=0.001), freqs[0] assert np.isclose(freqs[20], freq_eqsig[21], rtol=0.0001) assert np.isclose(freqs[-1], freq_eqsig[-1], rtol=0.001) for i in range(len(fa)): assert np.isclose(fa[i], fa_eqsig[i + 1], atol=0.00001), i
def fas2signal(fas, dt, stype="signal"): """ Convert a fourier spectrum to time series signal Parameters ---------- fas: array_like of img floats Positive part only dt: float time step of time series stype: str If 'signal' then return Signal, else return AccSignal """ from eqsig.single import Signal, AccSignal n = 2 * len(fas) a = np.zeros(2 * len(fas), dtype=complex) a[1:n // 2] = fas[1:] a[n // 2 + 1:] = np.flip(np.conj(fas[1:]), axis=0) a /= dt s = np.fft.ifft(a) npts = int(2 ** (np.log(n) / np.log(2))) s = s[:npts] if stype == 'signal': return Signal(s, dt) else: return AccSignal(s, dt)
def show_response_spectra_at_high_frequencies(): record_path = TEST_DATA_DIR test_filename = 'test_motion_true_spectra_acc.csv' data = np.loadtxt(record_path + test_filename, skiprows=1, delimiter=",") times = data[:40, 0] ss_s_a = data[:40, 1] record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) # acc_signal = AccSignal(rec, motion_step, response_times=times) # s_a = acc_signal.s_a # # a_times = acc_signal.response_times # s_d, s_v, s_a = dh.pseudo_response_spectra(rec, motion_step, times, xi=0.05) # s_d, s_v, s_a = dh.true_response_spectra(rec, motion_step, times, xi=0.05) acc_signal = AccSignal(rec, motion_step, response_times=times) s_a = acc_signal.s_a s_a_in_g = s_a / 9.81 # srss1 = sum(abs(s_a_in_g - ss_s_a)) plt.plot(times, s_a_in_g, label="eqsig") plt.plot(times, ss_s_a, label="true-ish") plt.legend() plt.show()
def load_asig(ffp, load_label=False, m=1.0): """ Loads an ``AccSignal`` that was saved in eqsig input format. Parameters ---------- ffp: str Full file path to output file load_label: bool if true then get label from file m: float (default=1.0) Scale factor to apply to time series data when loading Returns ------- asig: eqsig.AccSignal """ vals, dt = load_values_and_dt(ffp) if load_label: a = open(ffp) label = a.read().splitlines()[0] a.close() else: label = 'm1' return AccSignal(vals * m, dt, label=label)
def test_fourier_spectra_stable_against_aliasing(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename, skiprows=2) rec2 = np.zeros(2**13) rec2[:len(rec)] = rec org_signal = AccSignal(rec, motion_step) extended_signal = AccSignal(rec2, motion_step) rec_split = [] for i in range(int(len(rec2) / 2)): rec_split.append(rec2[i * 2]) acc_split = AccSignal(rec_split, motion_step * 2) org_fa = abs(org_signal.fa_spectrum) split_fa = abs(acc_split.fa_spectrum) ext_fa = abs(extended_signal.fa_spectrum) org_freq = abs(org_signal.fa_frequencies) split_freq = abs(acc_split.fa_frequencies) ext_freq = abs(extended_signal.fa_frequencies) for i in range(len(org_signal.fa_spectrum)): if i > 1830: abs_tol = 0.03 else: abs_tol = 0.02 assert np.isclose(org_freq[i], ext_freq[i]) assert np.isclose(org_fa[i], ext_fa[i]) if i < 2048: assert np.isclose(org_freq[i], split_freq[i]) assert np.isclose(org_fa[i], split_fa[i], atol=abs_tol), i
def show_fourier_spectra_stable_against_aliasing(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) rec2 = np.zeros(2**13) rec2[:len(rec)] = rec org_signal = AccSignal(rec, motion_step) extended_signal = AccSignal(rec2, motion_step) rec_split = [] for i in range(int(len(rec2) / 2)): rec_split.append(rec2[i * 2]) acc_split = AccSignal(rec_split, motion_step * 2) bf, sp = plt.subplots(2) sp[0].plot(org_signal.time, org_signal.values) sp[0].plot(extended_signal.time, extended_signal.values) sp[0].plot(acc_split.time, acc_split.values) sp[1].plot(org_signal.fa_frequencies, abs(org_signal.fa_spectrum), lw=0.7, label="original") sp[1].plot(acc_split.fa_frequencies, abs(acc_split.fa_spectrum), lw=0.7, label="split") sp[1].plot(extended_signal.fa_frequencies, abs(extended_signal.fa_spectrum), lw=0.7, label="full") plt.legend() plt.show()
def load_test_record_from_file(record_path, record_filename, scale=1): a = open(record_path + record_filename, 'r') b = a.readlines() a.close() acc = [] motion_step = float(b[0].split("=")[1]) print('values dt: ', motion_step) for i in range(len(b)): if i > 3: dat = b[i].split() for j in range(len(dat)): acc.append(float(dat[j]) * scale) rec = AccSignal(acc, motion_step) return rec
def rewrite_fourier_spectra_test_file(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) fa_amplitudes = abs(acc_signal.fa_spectrum) fa_phases = np.angle(acc_signal.fa_spectrum) paras = [] for i in range(len(acc_signal.fa_frequencies)): paras.append("%.5f,%.5f,%.5f" % (acc_signal.fa_frequencies[i], fa_amplitudes[i], fa_phases[i])) outfile_name = record_path + "test_motion_dt0p01_fas.txt" outfile = open(outfile_name, "w") outfile.write("\n".join(paras)) outfile.close()
def test_response_spectra_at_high_frequencies(): record_path = TEST_DATA_DIR test_filename = 'test_motion_true_spectra_acc.csv' data = np.loadtxt(record_path + test_filename, skiprows=1, delimiter=",") times = data[:40, 0] ss_s_a = data[:40, 1] record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step, response_times=times) s_a = acc_signal.s_a s_a_in_g = s_a / 9.81 a_times = acc_signal.response_times assert len(times) == len(a_times) srss1 = sum(abs(s_a_in_g - ss_s_a)) assert srss1 < 0.01 * 40, srss1
def rewrite_response_spectra_eqsig_test_file(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step, response_times=(0.1, 5.)) s_a = acc_signal.s_a s_d = acc_signal.s_d times = acc_signal.response_times paras = [] for i in range(len(times)): paras.append("%.5f,%.5f,%.5f" % (times[i], s_a[i], s_d[i])) outfile_name = record_path + "test_motion_dt0p01_rs.txt" outfile = open(outfile_name, "w") outfile.write("\n".join(paras)) outfile.close()
def test_fourier_spectra(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) fa_amplitudes = abs(acc_signal.fa_spectrum) fa_phases = np.angle(acc_signal.fa_spectrum) paras = [] for i in range(len(acc_signal.fa_frequencies)): paras.append("%.5f,%.5f,%.5f" % (acc_signal.fa_frequencies[i], fa_amplitudes[i], fa_phases[i])) testfile_name = record_path + "test_motion_dt0p01_fas.txt" testfile = open(testfile_name, "r") test_lines = testfile.readlines() for i, line in enumerate(test_lines): line = line.replace("\n", "") assert line == paras[i], i
def fas2signal(fas, dt, stype="signal"): """ Convert a fourier spectrum to time series signal :param fas: positive part only :param dt: time step of time series :return: """ n = 2 * len(fas) a = np.zeros(2 * len(fas), dtype=complex) a[1:n // 2] = fas[1:] a[n // 2 + 1:] = np.flip(np.conj(fas[1:]), axis=0) a /= dt s = np.fft.ifft(a) npts = int(2**(np.log(n) / np.log(2))) s = s[:npts] if stype == 'signal': return Signal(s, dt) else: return AccSignal(s, dt)
def test_response_spectra_versus_old_eqsig_version(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) s_a = acc_signal.s_a s_d = acc_signal.s_d times = acc_signal.response_times paras = [] for i in range(len(times)): paras.append("%.5f,%.5f,%.5f" % (times[i], s_a[i], s_d[i])) testfile_name = record_path + "test_motion_dt0p01_rs.txt" testfile = open(testfile_name, "r") test_lines = testfile.readlines() for i, line in enumerate(test_lines): line = line.replace("\n", "") assert line == paras[i], i
def test_displacement_velocity(): record_path = TEST_DATA_DIR record_filename = 'test_motion_dt0p01.txt' motion_step = 0.01 rec = np.loadtxt(record_path + record_filename) acc_signal = AccSignal(rec, motion_step) # Compare time series test_filename = 'test_motion_avd.csv' data = np.loadtxt(record_path + test_filename, skiprows=1, delimiter=",") time = data[:, 0] velocity = data[:, 2] displacement = data[:, 3] assert len(time) == len(acc_signal.time) abs_velocity_diff = abs(acc_signal.velocity - velocity) cum_velocity_diff = sum(abs_velocity_diff) max_velocity_diff = max(abs_velocity_diff) assert cum_velocity_diff < 0.03, cum_velocity_diff assert max_velocity_diff < 0.00006, max_velocity_diff abs_disp_diff = abs(acc_signal.displacement - displacement) cum_disp_diff = sum(abs_disp_diff) max_disp_diff = max(abs_disp_diff) assert cum_disp_diff < 0.02, cum_disp_diff assert max_disp_diff < 0.00002, max_disp_diff # Compare time series versus true test_filename = 'test_motion_avd.csv' data = np.loadtxt(record_path + test_filename, skiprows=1, delimiter=",") time = data[:, 0] velocity = data[:, 2] displacement = data[:, 3] assert len(time) == len(acc_signal.time) abs_velocity_diff = abs(acc_signal.velocity - velocity) cum_velocity_diff = sum(abs_velocity_diff) max_velocity_diff = max(abs_velocity_diff) assert cum_velocity_diff < 0.03, cum_velocity_diff assert max_velocity_diff < 0.00006, max_velocity_diff abs_disp_diff = abs(acc_signal.displacement - displacement) cum_disp_diff = sum(abs_disp_diff) max_disp_diff = max(abs_disp_diff) assert cum_disp_diff < 0.02, cum_disp_diff assert max_disp_diff < 0.00002, max_disp_diff
def combine_at_angle(acc_sig_ns, acc_sig_we, angle): off_rad = np.radians(angle) combo = acc_sig_ns.values * np.cos(off_rad) + acc_sig_we.values * np.sin( off_rad) new_sig = AccSignal(combo, acc_sig_ns.dt) return new_sig
def load_signal(ffp, astype='sig'): vals, dt = load_values_and_dt(ffp) if astype == "signal": return Signal(vals, dt) elif astype == "acc_sig": return AccSignal(vals, dt)