def test_transform(self): amp = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0] dt = 1 expected_frq = np.array([0.00000000000000000, 0.09090909090909091, 0.18181818181818182, 0.2727272727272727, 0.36363636363636365, 0.4545454545454546]) expected_amp = np.array([25.0+0.0*1j, -11.843537519677056+-3.477576385886737*1j, 0.22844587066117938+0.14681324646918337*1j, -0.9486905697966428+-1.0948472814948405*1j, 0.1467467171062613+0.3213304885841657*1j, -0.08296449829374097+-0.5770307602665046*1j]) expected_amp *= 2/len(amp) # TimeSeries tseries = sigpropy.TimeSeries(amp, dt) sensor = hvsrpy.Sensor3c(tseries, tseries, tseries) fft = sensor.transform() for val in fft.values(): for expected, returned in [(expected_frq, val.frq), (expected_amp, val.amp)]: self.assertArrayAlmostEqual(expected, returned) # WindowedTimeSeries amps = np.array([amp, amp]) tseries = sigpropy.WindowedTimeSeries(amps, dt) sensor = hvsrpy.Sensor3c(tseries, tseries, tseries) fft = sensor.transform() for val in fft.values(): for expected, returned in [(expected_frq, val.frq), (expected_amp, val.amp[0])]: self.assertArrayAlmostEqual(expected, returned) # Bad TimeSeries sensor.ew = "bad TimeSeries" self.assertRaises(NotImplementedError, sensor.transform)
def test_from_timeseries(self): # TODO (jpv): Replace these tests with something more meaningful. # Early example. amplitude = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0] dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) fft = sigpropy.FourierTransform.from_timeseries(tseries) true_frq = np.array([0.00000000000000000, 0.09090909090909091, 0.18181818181818182, 0.2727272727272727, 0.36363636363636365, 0.4545454545454546]) true_amp = np.array([25.0+0.0*1j, -11.843537519677056+-3.477576385886737*1j, 0.22844587066117938+0.14681324646918337*1j, -0.9486905697966428+-1.0948472814948405*1j, 0.1467467171062613+0.3213304885841657*1j, -0.08296449829374097+-0.5770307602665046*1j]) true_amp *= 2/len(amplitude) for expected, returned in [(true_frq, fft.frequency), (true_amp, fft.amplitude)]: self.assertArrayAlmostEqual(expected, returned) # Load and transform ambient noise record. fname = "data/a2/UT.STN11.A2_C50.miniseed" trace = obspy.read(self.full_path+fname)[0] tseries = sigpropy.TimeSeries.from_trace(trace) tseries.split(windowlength=120) fsuite = sigpropy.FourierTransform.from_timeseries(tseries) returned = fsuite.amplitude.shape expected = (tseries.nseries, fsuite.frequency.size) self.assertTupleEqual(expected, returned) # Bad timeseries. self.assertRaises(TypeError, sigpropy.FourierTransform.from_timeseries, "bad timeseries")
def test_cosine_taper(self): ## nseries = 1 # 0% Window - (i.e., no taper) amp = np.ones(10) dt = 1 tseries = sigpropy.TimeSeries(amp, dt) tseries.cosine_taper(0) returned = tseries.amplitude expected = amp self.assertArrayEqual(expected, returned) # 50% window amp = np.ones(10) dt = 1 tseries = sigpropy.TimeSeries(amp, dt) tseries.cosine_taper(0.5) returned = tseries.amplitude expected = np.array([ 0.000000000000000e+00, 4.131759111665348e-01, 9.698463103929542e-01, 1.000000000000000e+00, 1.000000000000000e+00, 1.000000000000000e+00, 1.000000000000000e+00, 9.698463103929542e-01, 4.131759111665348e-01, 0.000000000000000e+00 ]) self.assertArrayAlmostEqual(expected, returned, places=6) # 100% Window amp = np.ones(10) dt = 1 tseries = sigpropy.TimeSeries(amp, dt) tseries.cosine_taper(1) returned = tseries.amplitude expected = np.array([ 0.000000000000000e+00, 1.169777784405110e-01, 4.131759111665348e-01, 7.499999999999999e-01, 9.698463103929542e-01, 9.698463103929542e-01, 7.500000000000002e-01, 4.131759111665350e-01, 1.169777784405111e-01, 0.000000000000000e+00 ]) self.assertArrayAlmostEqual(expected, returned, places=6) ## nseries = 3 # 0% Window - (i.e., no taper) amplitude = np.ones(30) dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) tseries.split(windowlength=9) tseries.cosine_taper(0) expected = np.ones(10) for returned in tseries.amplitude: self.assertArrayAlmostEqual(expected, returned, places=6) # 50% window amplitude = np.ones(30) dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) tseries.split(windowlength=9) tseries.cosine_taper(0.5) expected = np.array([ 0.000000000000000e+00, 4.131759111665348e-01, 9.698463103929542e-01, 1.000000000000000e+00, 1.000000000000000e+00, 1.000000000000000e+00, 1.000000000000000e+00, 9.698463103929542e-01, 4.131759111665348e-01, 0.000000000000000e+00 ]) for returned in tseries.amplitude: self.assertArrayAlmostEqual(expected, returned, places=6) # 100% Window amplitude = np.ones(30) dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) tseries.split(windowlength=9) tseries.cosine_taper(1) expected = np.array([ 0.000000000000000e+00, 1.169777784405110e-01, 4.131759111665348e-01, 7.499999999999999e-01, 9.698463103929542e-01, 9.698463103929542e-01, 7.500000000000002e-01, 4.131759111665350e-01, 1.169777784405111e-01, 0.000000000000000e+00 ]) for returned in tseries.amplitude: self.assertArrayAlmostEqual(expected, returned, places=6)
def test_trim(self): # nseries = 1 tseries = sigpropy.TimeSeries(amplitude=[0, 1, 2, 3, 4], dt=0.5) tseries.trim(0, 1) self.assertListEqual([0, 1, 2], tseries.amplitude.tolist()) self.assertEqual(3, tseries.nsamples) self.assertEqual(0, min(tseries.time)) self.assertEqual(1, max(tseries.time)) # nseries = 3, no trim amplitude = np.array([[0., 1, 2, 3], [3., 4, 5, 6], [6., 7, 8, 9]]) tseries = sigpropy.TimeSeries(amplitude=amplitude, dt=1) with warnings.catch_warnings(): warnings.simplefilter("ignore") tseries.trim(0, 9) expected = amplitude returned = tseries.amplitude self.assertArrayEqual(expected, returned) # nseries = 3, remove last window for end_time in [6, 7, 8]: amplitude = np.array([[0., 1, 2, 3], [3., 4, 5, 6], [6., 7, 8, 9]]) tseries = sigpropy.TimeSeries(amplitude=amplitude, dt=1) with warnings.catch_warnings(): warnings.simplefilter("ignore") tseries.trim(0, end_time) returned = tseries.amplitude expected = amplitude[:-1] self.assertArrayEqual(expected, returned) # nseries = 3, leave only the middle window. for end_time in [6, 7, 8]: amplitude = np.array([[0., 1, 2, 3], [3., 4, 5, 6], [6., 7, 8, 9]]) tseries = sigpropy.TimeSeries(amplitude=amplitude, dt=1) with warnings.catch_warnings(): warnings.simplefilter("ignore") tseries.trim(3, end_time) returned = tseries.amplitude expected = amplitude[1:2] self.assertArrayEqual(expected, returned) # nseries = 3, leave only the last window. amplitude = np.array([[0., 1, 2, 3], [3., 4, 5, 6], [6., 7, 8, 9]]) tseries = sigpropy.TimeSeries(amplitude=amplitude, dt=1) with warnings.catch_warnings(): warnings.simplefilter("ignore") tseries.trim(6, 9) returned = tseries.amplitude expected = amplitude[2:3] self.assertArrayEqual(expected, returned) # nseries = 3, leave only the first window. for end_time in [3, 4, 5]: amplitude = np.array([[0., 1, 2, 3], [3., 4, 5, 6], [6., 7, 8, 9]]) tseries = sigpropy.TimeSeries(amplitude=amplitude, dt=1) with warnings.catch_warnings(): warnings.simplefilter("ignore") tseries.trim(0, end_time) returned = tseries.amplitude expected = amplitude[0:1] self.assertArrayEqual(expected, returned) # Illogical start_time (start_time < 0). amplitude = list(range(5)) dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) self.assertRaises(IndexError, tseries.trim, start_time=-1, end_time=2) # Illogical start_time (start_time > end_time). amplitude = list(range(5)) dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) self.assertRaises(IndexError, tseries.trim, start_time=2, end_time=2) # Illogical end_time (end_time > max_time). amplitude = list(range(5)) dt = 1 tseries = sigpropy.TimeSeries(amplitude, dt) self.assertRaises(IndexError, tseries.trim, start_time=1, end_time=5)
def new_sensor(): ns = sigpropy.TimeSeries(np.ones(10), dt=1) ew = sigpropy.TimeSeries(np.ones(10), dt=1) vt = sigpropy.TimeSeries(np.ones(10), dt=1) return hvsrpy.Sensor3c(ns, ew, vt)
def new_sensor(): component = sigpropy.TimeSeries(np.ones(10), dt=1) return hvsrpy.Sensor3c(component, component, component)