def test_variable_type(self): # first, make a noise trace and shift it by tau * sampling rate\ file = source_file duration = 400 shift = 30 noise_loc_1 = create_random_trace(sampling_rate=40, duration=duration) noise_loc_1[0].data += create_random_trace(sampling_rate=40, duration=duration)[0].data noise_loc_1 = convert(noise_loc_1) noise_loc_2 = xr.apply_ufunc(shift_trace, noise_loc_1, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={ 'time': shift, 'delta': noise_loc_1.attrs['delta'] }, keep_attrs=True) # next, add an eq teleseism from file to both noise streams noise_loc_1_eq = noise_loc_1.copy() down = XArrayResample(10) t_norm = XArrayTemporalNorm(time_window=2.0) noise_loc_2_eq = down(noise_loc_1_eq) noise_loc_2_eq_tnorm = t_norm(noise_loc_2_eq) assert noise_loc_2_eq_tnorm.dtype == np.float64
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_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_filter_frequency_in_band(self): process = XArrayBandpass(freqmin=0.5, freqmax=20.0) trace = converter(create_random_trace(sampling_rate=100)) trace = process(trace, starttime=0, station=0) source = self._get_frequency_of_trace(trace, sample_point=5) target = 0.1 assert source > target, "bandpass filter removing desired frequency"
def test_filter_frequency_out_of_band(self): process = XArrayBandpass(freqmin=0.5, freqmax=20.0) trace = converter(create_random_trace(sampling_rate=100)) trace = process(trace, starttime=0, station=0) source = self._get_frequency_of_trace(trace, sample_point=40.0) target = 0 assert round(abs(source - target), 1) == 0, "frequency not removed"
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_increase_earthquake_shift_time_xcorr_func(self): # first, make a noise trace and shift it by tau * sampling rate\ file = source_file duration = 400 shift = 3 noise_loc_1 = create_random_trace(sampling_rate=40, duration=duration) noise_loc_1[0].data+= create_random_trace(sampling_rate=40, duration=duration)[0].data noise_loc_1 = convert(noise_loc_1,starttime=0,station=0) noise_loc_2 = xr.apply_ufunc(shift_trace, noise_loc_1, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={'time': shift, 'delta': noise_loc_1.attrs['delta']}, keep_attrs=True) # next, add an eq teleseism from file to both noise streams noise_loc_1_eq = noise_loc_1.copy() noise_loc_2_eq = noise_loc_2.copy() attrs = noise_loc_1.attrs source_eq = source_earthquake() noise_loc_1_eq[:, :, :source_eq.data.shape[2]] += source_eq.data[:, :, :] noise_loc_2_eq[:, :, :source_eq.data.shape[2]] += source_eq.data[:, :, :] noise_loc_1.attrs = attrs noise_loc_2.attrs = attrs noise_loc_2_eq.attrs = attrs noise_loc_1_eq.attrs = attrs # downsample both to 10hz sampling rate down = XArrayResample(10) t_norm =XArrayTemporalNorm(time_window=2.0) noise_loc_1_eq = down(noise_loc_1_eq) noise_loc_2_eq = down(noise_loc_2_eq) noise_loc_2_eq_tnorm = t_norm(noise_loc_2_eq.copy()) noise_loc_1_eq_tnorm = t_norm(noise_loc_1_eq.copy()) x_corr_eq = self.max_corr_norm(noise_loc_1_eq, noise_loc_2_eq) x_corr_eq_tnorm= self.max_corr_norm(noise_loc_1_eq_tnorm, noise_loc_2_eq_tnorm) zero_index = len(x_corr_eq)//2 assert x_corr_eq[zero_index+int(40*shift)] < x_corr_eq_tnorm[zero_index+int(40*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_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'