def test_cross_chan_coherence_non_matching_sampling_rates(self): """Initial test to ensure cross_chan_coherence runs.""" st2 = self.st2.copy() for tr in st2: tr.stats.sampling_rate += 20 with self.assertRaises(AssertionError): cross_chan_correlation(st1=self.st1.copy(), streams=[st2])
def test_cross_chan_different_order(self): """ Check that the order of streams doesn't matter. """ from random import shuffle for master in self.stream_list: shuffled_stream_list = [st.copy() for st in self.stream_list] shuffle(shuffled_stream_list) cccoh, _ = cross_chan_correlation(st1=master.copy(), streams=self.stream_list) cccoh_shuffled, _ = cross_chan_correlation( st1=master.copy(), streams=shuffled_stream_list) cccoh.sort() cccoh_shuffled.sort() assert np.allclose(cccoh, cccoh_shuffled, atol=0.0001)
def test_inverted_coherence(self): """Reverse channels and ensure we get -1""" st2 = self.st2.copy() for tr in st2: tr.data *= -1 cccoh, _ = cross_chan_correlation(st1=self.st1.copy(), streams=[st2]) self.assertEqual(cccoh[0], -1)
def test_known_coherence(self): """Test for a real stream case""" st1 = read(os.path.join(self.testing_path, 'WAV', 'TEST_', '2013-09-01-0410-35.DFDPC_024_00')) st2 = read(os.path.join(self.testing_path, 'WAV', 'TEST_', '2013-09-01-2040-11.DFDPC_039_00')) st1 = st1.resample(100) st2 = st2.resample(100) for tr in st1: tr.data = tr.data[0:8000] for tr in st2: tr.data = tr.data[0:8000] cccoh, _ = cross_chan_correlation(st1=st1, streams=[st2]) self.assertTrue(cccoh[0] < 0.01)
def cc_coh_dets(streams, events, length, corr_prepick, shift): # Loop over detections and return list of ccc with template # Trim all wavs to desired length # Assumes the first entry is template for i, st in enumerate(streams): ev = events[i] for tr in st: pk = [pk for pk in ev.picks if pk.waveform_id.get_seed_string() == tr.id][0] strt = pk.time - corr_prepick - (shift / 2.) tr.trim(starttime=strt, endtime=strt + length + (shift / 2.), nearest_sample=True) # Clean out traces of different lengths len = Counter([(tr.id, tr.stats.npts) for st in streams for tr in st]).most_common(1)[0][0][1] for st in streams: rms = [tr for tr in st if tr.stats.npts != len] for rm in rms: st.traces.remove(rm) coh, i = cross_chan_correlation(st1=streams[0], streams=streams[1:], shift_len=shift) return coh
def test_cross_chan_coherence_shifted(self): """Initial test to ensure cross_chan_coherence runs.""" cccoh, _ = cross_chan_correlation(st1=self.st1.copy(), streams=[self.st2.copy()], shift_len=0.2) self.assertEqual(round(cccoh[0], 6), 1)
def test_cross_chan_coherence(self): """Initial test to ensure cross_chan_coherence runs.""" cccoh, _ = cross_chan_correlation(st1=self.st1.copy(), streams=[self.st2.copy()]) self.assertEqual(cccoh[0], 1)