def test_auto_pick_first_arrivals(self): s1 = swprocess.Sensor1C(np.concatenate( (np.zeros(100), np.array([0.1, 0, 0]), np.zeros(100))), dt=1, x=1, y=0, z=0) s2 = swprocess.Sensor1C(np.concatenate( (np.zeros(100), np.array([0, 0.2, 0]), np.zeros(100))), dt=1, x=2, y=0, z=0) source = swprocess.Source(0, 0, 0) array = swprocess.Array1D([s1, s2], source) # algorithm = "threshold" position, times = array.auto_pick_first_arrivals(algorithm="threshold") self.assertListEqual(array.position(), position) self.assertListEqual([100, 101], times) # algorithm = "bad" self.assertRaises(NotImplementedError, array.auto_pick_first_arrivals, algorithm="bad")
def test_from_array1ds(self): # Define source. source = swprocess.Source(x=-5, y=0, z=0) # Create signal array. s_sensors = [] for n in range(5): amp = 5 * np.random.random(100) s_sensors.append( swprocess.Sensor1C(amp, dt=0.01, x=2 * n, y=0, z=0)) signal = swprocess.Array1D(s_sensors, source) # Create noise array. n_sensors = [] for n in range(5): amp = np.random.random(100) n_sensors.append( swprocess.Sensor1C(amp, dt=0.01, x=2 * n, y=0, z=0)) noise = swprocess.Array1D(n_sensors, source) # Calculate SNR fmin, fmax = 5, 50 snr = swprocess.snr.SignaltoNoiseRatio.from_array1ds(signal, noise, fmin=fmin, fmax=fmax) expected = np.arange(fmin, fmax + signal[0].df, signal[0].df) returned = snr.frequencies self.assertArrayAlmostEqual(expected, returned) # Fail due to unequal samples noise.trim(0, 0.25) self.assertRaises(IndexError, swprocess.snr.SignaltoNoiseRatio.from_array1ds, signal, noise, fmin=fmin, fmax=fmax) # Pass after padding snr = swprocess.snr.SignaltoNoiseRatio.from_array1ds( signal, noise, fmin=fmin, fmax=fmax, pad_snr=True, df_snr=signal[0].df) expected = np.arange(fmin, fmax + signal[0].df, signal[0].df) returned = snr.frequencies self.assertArrayAlmostEqual(expected, returned)
def test_init(self): # Basic source = swprocess.Source(x=-5, y=0, z=0) sensors = [self.sensor_0, self.sensor_1] array = swprocess.Array1D(sensors=sensors, source=source) self.assertEqual(array.source, source) self.assertListEqual(array.sensors, sensors) # Bad: Invalid sensors self.assertRaises(ValueError, swprocess.Array1D, sensors=[self.sensor_5, self.sensor_5], source=source) # Bad: Incompatable sensors sensor_bad = swprocess.Sensor1C(amplitude=[1, 2, 3, 4], dt=1, x=7, y=0, z=0, nstacks=1, delay=0) self.assertRaises(ValueError, swprocess.Array1D, sensors=[self.sensor_5, sensor_bad], source=source)
def test_to_and_from_su(self): spacing = 2 sensors = [] nsamples = 1000 time = np.arange(0, 1, 1 / nsamples) for n in range(24): sensor = swprocess.Sensor1C(amplitude=np.sin(2 * np.pi * n * time), dt=1 / nsamples, x=spacing * n, y=0, z=0, nstacks=1, delay=0) sensors.append(sensor) source = swprocess.Source(x=-10, y=0, z=0) expected = swprocess.Array1D(sensors, source) # To and from : SU fname = "to_file.su" expected.to_file(fname) returned = swprocess.Array1D.from_files(fname) self.assertEqual(expected, returned) os.remove(fname) # Bad : format self.assertRaises(ValueError, expected.to_file, fname, ftype="seg2")
def test_plot(self): # Basic case (near-side, 2m spacing) fname = self.wghs_path + "1.dat" swprocess.Array1D.from_files(fname).plot() # Non-linear spacing sensors = [ swprocess.Sensor1C( [1, 2, 3], dt=1, x=x, y=0, z=0, ) for x in [0, 1, 3] ] source = swprocess.Source(x=-5, y=0, z=0) array = swprocess.Array1D(sensors=sensors, source=source) array.plot() # Basic case (far-side, 2m spacing) fname = self.wghs_path + "20.dat" swprocess.Array1D.from_files(fname).plot() plt.show(block=False) plt.close("all")
def dummy_array(amp, dt, nstacks, delay, nsensors, spacing, source_x): """Make simple dummy array from timeseries for testing.""" sensors = [] for i in range(nsensors): sensor = swprocess.Sensor1C(amp, dt, x=i * spacing, y=0, z=0, nstacks=nstacks, delay=delay) sensors.append(sensor) source = swprocess.Source(x=source_x, y=0, z=0) return swprocess.Array1D(sensors=sensors, source=source)
def setUpClass(cls): cls.full_path = get_full_path(__file__) cls.wghs_path = cls.full_path + "../examples/masw/data/wghs/" cls.sensor_0 = swprocess.Sensor1C(amplitude=[-.1] * 9 + [-0.11] + [-.1] * 9, dt=1, x=0, y=0, z=0, nstacks=1, delay=0) cls.sensor_1 = swprocess.Sensor1C(amplitude=[0.2] * 9 + [+0.25] + [0.2] * 9, dt=1, x=1, y=0, z=0, nstacks=1, delay=0) cls.sensor_5 = swprocess.Sensor1C(amplitude=[1.0] * 9 + [+1.05] + [1.0] * 9, dt=1, x=5, y=0, z=0, nstacks=1, delay=0) cls.sensor_6 = swprocess.Sensor1C(amplitude=[2.0] * 9 + [+2.02] + [2.0] * 9, dt=1, x=6, y=0, z=0, nstacks=1, delay=0)
def test_from_array(self): class SubEmpty(swprocess.wavefieldtransforms.EmptyWavefieldTransform): @classmethod def _create_vector(cls, *args, **kwargs): cls._create_vector_args = args cls._create_vector_kwargs = kwargs sensors = [swprocess.Sensor1C([0]*100, 0.01, 2*n, 0, 0) for n in range(5)] source = swprocess.Source(-5, 0, 0) array = swprocess.Array1D(sensors, source) settings = dict(fmin=5, fmax=50, vmin=75, vmax=300, nvel=30, vspace="lin") empty = SubEmpty.from_array(array, settings) self.assertArrayEqual(np.arange(5., 50+1, 1), empty.frequencies) expected = (settings["vmin"], settings["vmax"], settings["nvel"], settings["vspace"]) returned = empty._create_vector_args self.assertTupleEqual(expected, returned) self.assertDictEqual({}, empty._create_vector_kwargs)