def test_autocorrelation(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) synth_trace1 = converter(create_random_trace(station='1',network='v',duration=20)) correlation = correlator(synth_trace1,synth_trace1) zero_target_index = correlation.data.shape[4]//2 zero_source_index = np.argmax(correlation.data[0,0,0,:][0]) assert zero_source_index == zero_target_index,'autocorrelation failed'
def test_convert_is_accurate(self): max_tau_shift = None correlator = XArrayXCorrelate(max_tau_shift=max_tau_shift) e2 = create_sinsoidal_trace_w_decay(decay=0.8, station='k', network='v', channel='e', duration=20) n2 = create_random_trace(station='k', network='v', channel='n', duration=20) z2 = create_sinsoidal_trace_w_decay(decay=0.3, station='k', network='v', channel='z', duration=20) b2 = create_random_trace(station='k', network='v', channel='b', duration=20) new_traces = e2.traces + n2.traces + z2.traces + b2.traces syth_trace2 = converter(new_traces) result_1 = syth_trace2.loc['e', :, :].data.ravel() - e2[0].data assert 0 == np.sum(result_1) result_1 = syth_trace2.loc['n', :, :].data.ravel() - n2[0].data assert 0 == np.sum(result_1) result_1 = syth_trace2.loc['z', :, :].data.ravel() - z2[0].data assert 0 == np.sum(result_1) result_1 = syth_trace2.loc['b', :, :].data.ravel() - b2[0].data assert 0 == np.sum(result_1)
def test_shift_trace_time_slice(self): max_tau_shift = 10.0 correlator = XArrayXCorrelate(max_tau_shift=max_tau_shift) time_shift = 4 synth_trace1 = converter( create_sinsoidal_trace_w_decay(decay=0.6, station='h', network='v', channel='z', duration=20)) synth_trace2 = converter( create_sinsoidal_trace_w_decay(decay=0.4, station='h', network='w', channel='z', duration=20)) synth_trace2 = xr.apply_ufunc(shift_trace, synth_trace2, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={ 'time': time_shift, 'delta': synth_trace2.attrs['delta'] }, keep_attrs=True) correlation = correlator(synth_trace1, synth_trace2) correlation /= correlation.max() time_array = correlation.coords['time'].values max_index = np.argmax(correlation.data) tau_shift = (time_array[max_index] - np.datetime64( UTCDateTime(0.0).datetime)) / np.timedelta64(1, 's') assert tau_shift == -time_shift, 'failed to correctly identify tau shift'
def test_array_is_real(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) syth_trace1 = converter(create_random_trace(station='1',network='v',duration=20)) correlation = correlator(syth_trace1,syth_trace1) assert correlation.data.dtype == np.float64,'improper data type'
def test_name(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) syth_trace1 = converter(create_random_trace(station='1',network='v',duration=20)) correlation = correlator(syth_trace1,syth_trace1) assert isinstance(correlation.name,str),'improper name type'
def test_autocorrelation_delta_attr(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) syth_trace1 = converter(create_random_trace(station='1',network='v',duration=20)) correlation = correlator(syth_trace1,syth_trace1) assert 'delta' in correlation.attrs['df'].columns,'did not propagate the delta value'
def test_one_stack(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) syth_trace1 = converter(create_random_trace(station='1', network='v', duration=20)) attr = correlator(syth_trace1, syth_trace1).attrs assert attr['df']['stacks'].values[0] == 1,'stacks assigned improper value'
def test_has_stacks(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) syth_trace1 = converter(create_random_trace(station='1', network='v', duration=20)) keys = correlator(syth_trace1, syth_trace1).attrs['df'].columns assert 'stacks' in keys, 'stacks not preserved through correlation'
def test_correlation_length(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) syth_trace1 = converter(create_random_trace(station='1', network='v')) syth_trace2 = converter(create_random_trace(station='2', network='v')) try: correlation = correlator(syth_trace1, syth_trace2) assert False, 'failed to catch exception' except Exception: assert True, 'failed to catch exception'
def test_correct_pair_ez24(self): correlator = XArrayXCorrelate(max_tau_shift=None) synth_trace_1, synth_trace_2 = create_example_xarrays_missing_channel() src_chan = 'z' rec_chan = 'e' correlation_source = correlator(synth_trace_1.copy(), synth_trace_2.copy()) test_correlation = correlator(synth_trace_1.sel(dict(channel=src_chan)).expand_dims('channel'), synth_trace_2.sel(dict(channel=rec_chan)).expand_dims('channel')) result_1 = correlation_source.loc[dict(src='v.h',rec='v.k',src_chan=src_chan, rec_chan=rec_chan)] - test_correlation assert 0 == np.sum(result_1.data)
def test_scipy_equivalent(self): max_tau_shift = 19.99 correlator = XArrayXCorrelate(max_tau_shift=max_tau_shift) synth_trace_1, synth_trace_2 = create_example_xarrays() src_chan = 'z' rec_chan = 'z' correlation_source = correlator(synth_trace_1.copy(), synth_trace_2.copy()) test_correlation = sig.correlate(np.asarray(synth_trace_1.sel(dict(channel=src_chan)).expand_dims('channel').data).squeeze(), np.asarray(synth_trace_2.sel(dict(channel=rec_chan)).expand_dims('channel').data).squeeze(),mode='full') corr_source = correlation_source.loc[dict(src='v.h',rec='v.k',src_chan=src_chan, rec_chan=rec_chan)].data np.testing.assert_allclose(test_correlation,corr_source,atol=1e-14)
def test_stacking_preserves_metadata(self): correlator = XArrayXCorrelate(max_tau_shift=5.0) combiner = XArrayCombine() syth_trace1 = converter( create_random_trace(station='1', network='v', duration=20)) result_1 = combiner(correlator(syth_trace1, syth_trace1), None) result_2 = combiner(correlator(syth_trace1, syth_trace1), None) stack = stacker(result_1, result_2) df = stack.attrs['df'] assert df['stacks'].values[0] == 2, 'unexpected stack length' assert 'operations' in df.columns, 'operations did not persist through stacking' assert 'delta' in df.columns, 'delta did not persist through stacking'
def __init__(self, window_length=None, **kwargs): self._single_station_include = [] self._station_pair_include = [] self._station_pair_exclude = [] self._single_station_exclude = [] self._window_length = window_length self._tasks = { 'data': DataLoader(**kwargs), 'xconvert': XArrayConverter(), 'process': {}, 'crosscorrelate': XArrayXCorrelate(), 'post-correlate': NullTask('post-correlate'), 'combine': XArrayCombine(), 'post-combine': NullTask('post-combine'), 'pre-stack': NullDualTask('pre-stack'), 'stack': XArrayStack(), 'post-stack': NullTask('post-stack') } self._process_order = []
def test_nonetype_in_out(self): correlator = XArrayXCorrelate() result = correlator(None, None, starttime=0, station=0) assert result == None
# relative import #from synthetic_trace_factory import create_random_trace, create_sinsoidal_trace from obspy.core import read from scipy.signal import correlate from anxcor.xarray_routines import XArrayRemoveMeanTrend import pytest from obspy.clients.fdsn import Client from obspy.core import UTCDateTime import numpy as np import xarray as xr whiten = XArrayWhiten(smoothing_window_ratio=0.025, freqmax=25.0, freqmin=0.001, order=2) convert = XArrayConverter() xarraycorrelate = XArrayXCorrelate(max_tau_shift=40) source_file = 'tests/test_data/test_teleseism/test_teleseism.BHE.SAC' def source_earthquake(): earthquake_trace = read(source_file, format='sac')[0] earthquake_trace.stats.data_type = 'eq' earthquake_trace.data /= max(earthquake_trace.data) earthquake_trace.stats.starttime = 0 earthquake_trace.detrend(type='linear') earthquake_trace.detrend(type='constant') earthquake_trace.taper(max_percentage=0.05) earthquake_trace.stats.channel = 'Z' earthquake_trace.stats.station = 'test' earthquake_trace.stats.network = ''