def test_source_inside(self): sensors = [self.sensor_0, self.sensor_6] # _source_inside -> True source = swprocess.Source(x=3, y=0, z=0) array = swprocess.Array1D(sensors, source) self.assertTrue(array._source_inside) # _source_inside -> False source = swprocess.Source(x=-10, y=0, z=0) array = swprocess.Array1D(sensors, source) self.assertFalse(array._source_inside)
def test_flip_required(self): sensors = [self.sensor_0, self.sensor_1] # _flip_required -> True source = swprocess.Source(x=3, y=0, z=0) array = swprocess.Array1D(sensors, source) self.assertTrue(array._flip_required) # _flip_required -> False source = swprocess.Source(x=-5, y=0, z=0) array = swprocess.Array1D(sensors, source) self.assertFalse(array._flip_required)
def test_eq(self): source_0 = swprocess.Source(0, 0, 0) source_1 = swprocess.Source(1, 0, 0) array_a = swprocess.Array1D([self.sensor_0, self.sensor_1], source_0) array_b = "array1d" array_c = swprocess.Array1D([self.sensor_0], source_0) array_d = swprocess.Array1D([self.sensor_0, self.sensor_1], source_1) array_e = swprocess.Array1D([self.sensor_5, self.sensor_6], source_0) array_f = swprocess.Array1D([self.sensor_0, self.sensor_1], source_0) self.assertFalse(array_a == array_b) self.assertFalse(array_a == array_c) self.assertFalse(array_a == array_d) self.assertFalse(array_a == array_e) self.assertFalse(array_a != array_f)
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_manual_pick_first_arrivals(self): # Replace self._ginput_session edist, etime = [0, 1, 2], [2, 1, 0] class DummyArray1D(swprocess.Array1D): def _ginput_session(ax, initial_adjustment=True, ask_to_continue=True, npts=None): return (edist, etime) array = DummyArray1D([self.sensor_0, self.sensor_5, self.sensor_6], swprocess.Source(x=-5, y=0, z=0)) # time_ax = "y" for waterfall_kwargs in [dict(time_ax="y"), None]: rdist, rtime = array.manual_pick_first_arrivals( waterfall_kwargs=waterfall_kwargs) self.assertListEqual(edist, rdist) self.assertListEqual(etime, rtime) # time_ax = "x" rtime, rdist = array.manual_pick_first_arrivals(waterfall_kwargs=dict( time_ax="x")) self.assertListEqual(edist, rdist) self.assertListEqual(etime, rtime)
def test_timeseriesmatrix(self): source = swprocess.Source(x=-5, y=0, z=0) sensors = [self.sensor_0, self.sensor_1, self.sensor_5, self.sensor_6] array = swprocess.Array1D(sensors=sensors, source=source) base = np.array([[-.1] * 9 + [-0.11] + [-.1] * 9, [0.2] * 9 + [+0.25] + [0.2] * 9, [1.0] * 9 + [+1.05] + [1.0] * 9, [2.0] * 9 + [+2.02] + [2.0] * 9]) # detrend=False, normalize="none" expected = base returned = array.timeseriesmatrix(detrend=False, normalize="none") self.assertArrayEqual(expected, returned) # detrend=False, normalize="each" expected = base / np.array([0.11, 0.25, 1.05, 2.02]).reshape(4, 1) returned = array.timeseriesmatrix(detrend=False, normalize="each") self.assertArrayEqual(expected, returned) # detrend=False, normalize="all" expected = base / 2.02 returned = array.timeseriesmatrix(detrend=False, normalize="all") self.assertArrayEqual(expected, returned) # detrend=True, normalize="none" expected = base - np.array([-.1, 0.2, 1, 2]).reshape(4, 1) returned = array.timeseriesmatrix(detrend=True, normalize="none") self.assertArrayAlmostEqual(expected, returned, places=2)
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 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_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_array1d(self): source = swprocess.Source(1, 0, 0) # Non-normalized sensors = [self.sensor_1, self.sensor_5] expected = swprocess.Array1D(sensors, source) returned = swprocess.Array1D.from_array1d(expected) self.assertEqual(expected, returned)
def test_offsets(self): source = swprocess.Source(x=-5, y=0, z=0) sensors = [self.sensor_1, self.sensor_5, self.sensor_6] array = swprocess.Array1D(sensors=sensors, source=source) # simple returned = array.offsets expected = [5 + 1, 5 + 5, 5 + 6] self.assertListEqual(expected, returned)
def test_position(self): source = swprocess.Source(x=-5, y=0, z=0) sensors = [self.sensor_1, self.sensor_5, self.sensor_6] array = swprocess.Array1D(sensors=sensors, source=source) # normalize=False returned = array.position(normalize=False) expected = [1, 5, 6] self.assertListEqual(expected, returned) # normalize=True returned = array.position(normalize=True) expected = [0, 4, 5] self.assertListEqual(expected, returned)
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 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 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)
def test_spacing(self): # constant spacing for spacing in [1, 2, 5.5]: array = self.dummy_array(amp=[0, 0, 0], dt=1, nstacks=1, delay=0, nsensors=3, spacing=spacing, source_x=-5) self.assertEqual(spacing, array.spacing) # non-constant spacing source = swprocess.Source(x=-10, y=0, z=0) sensors = [self.sensor_0, self.sensor_5, self.sensor_6] array = swprocess.Array1D(sensors=sensors, source=source) try: array.spacing except ValueError: raised_error = True else: raised_error = False finally: self.assertTrue(raised_error)
def test_interactive_mute(self): # Replace self._ginput_session class DummyArray1D(swprocess.Array1D): def set_xy_before(self, xs, ys): self.xs_before = xs self.ys_before = ys def set_xy_after(self, xs, ys): self.xs_after = xs self.ys_after = ys def _my_generator(self): xs = [self.xs_before, self.xs_after] ys = [self.ys_before, self.ys_after] for x, y in zip(xs, ys): yield (x, y) def _opt1_ginput_session(self, *args, **kwargs): if not getattr(self, "mygen", False): self.mygen = self._my_generator() return next(self.mygen) def _opt2_ginput_session(self, *args, **kwargs): return (self.xs_after, self.ys_after) def interactive_mute(self, mute_location="both", window_kwargs=None, waterfall_kwargs=None): if mute_location == "after": self._ginput_session = self._opt2_ginput_session else: self._ginput_session = self._opt1_ginput_session return super().interactive_mute( mute_location=mute_location, window_kwargs=window_kwargs, waterfall_kwargs=waterfall_kwargs) # Create dummy array source = swprocess.Source(x=-5, y=0, z=0) sensors = [self.sensor_0, self.sensor_5, self.sensor_6] sensors = [swprocess.Sensor1C.from_sensor1c(x) for x in sensors] array = DummyArray1D(sensors=sensors, source=source) # Set mute locations -> time_ax = "y" xs_b, ys_b = (1, 5), (1, 3) array.set_xy_before(xs_b, ys_b) xs_a, ys_a = (1, 6), (4, 7) array.set_xy_after(xs_a, ys_a) # mute_location = "before" & time_ax = "y" start, end = array.interactive_mute(mute_location="before") self.assertTupleEqual(((xs_b[0], ys_b[0]), (xs_b[1], ys_b[1])), start) self.assertTrue(end is None) delattr(array, "mygen") # mute_location = "after" & time_ax = "y" start, end = array.interactive_mute(mute_location="after") self.assertTrue(start is None) self.assertTupleEqual(((xs_a[0], ys_a[0]), (xs_a[1], ys_a[1])), end) # mute_location = "both" & time_ax = "y" start, end = array.interactive_mute(mute_location="both") self.assertTupleEqual(((xs_b[0], ys_b[0]), (xs_b[1], ys_b[1])), start) self.assertTupleEqual(((xs_a[0], ys_a[0]), (xs_a[1], ys_a[1])), end) delattr(array, "mygen") # Set mute locations -> time_ax = "x" array.set_xy_before(ys_b, xs_b) array.set_xy_after(ys_a, xs_a) # mute_location = "before" & time_ax = "x" start, end = array.interactive_mute(mute_location="before", waterfall_kwargs=dict(time_ax="x")) self.assertTupleEqual(((xs_b[0], ys_b[0]), (xs_b[1], ys_b[1])), start) self.assertTrue(end is None) delattr(array, "mygen") # mute_location = "after" & time_ax = "x" start, end = array.interactive_mute(mute_location="after", waterfall_kwargs=dict(time_ax="x")) self.assertTrue(start is None) self.assertTupleEqual(((xs_a[0], ys_a[0]), (xs_a[1], ys_a[1])), end) # mute_location = "both" & time_ax = "x" start, end = array.interactive_mute(mute_location="both", waterfall_kwargs=dict(time_ax="x")) self.assertTupleEqual(((xs_b[0], ys_b[0]), (xs_b[1], ys_b[1])), start) self.assertTupleEqual(((xs_a[0], ys_a[0]), (xs_a[1], ys_a[1])), end) delattr(array, "mygen")