Skip to content

Commit

Permalink
Fix decluster for detections at the same time (eqcorrscan#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
calum-chamberlain authored Aug 28, 2018
1 parent 9b8ab34 commit 570502a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Fix bug where `set_xcorr` as context manager did not correctly reset
stream_xcorr methods.
* Correct test-script (`test_eqcorrscan.py`) to find paths properly.
* BUG-FIX in `Party.decluster` when detections made at exactly the same time
the first, rather than the highest of these was taken.

## 0.3.2
* Implement reading Party objects from multiple files, including wildcard
Expand Down
12 changes: 8 additions & 4 deletions eqcorrscan/core/match_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,16 @@ def decluster(self, trig_int, timing='detect', metric='avg_cor'):
detect_times = np.array([
_total_microsec(d[0].datetime, min_det.datetime)
for d in detect_info])
# Trig_int must be converted from seconds to micro-seconds
peaks_out = decluster(
peaks=detect_vals, index=detect_times, trig_int=trig_int * 10 ** 6)
# Trig_int must be converted from seconds to micro-seconds
declustered_detections = [
all_detections[np.where(detect_times == ind[1])[0][0]]
for ind in peaks_out]
# Need to match both the time and the detection value
declustered_detections = []
for ind in peaks_out:
matching_time_indeces = np.where(detect_times == ind[-1])[0]
matches = matching_time_indeces[
np.where(detect_vals[matching_time_indeces] == ind[0])[0][0]]
declustered_detections.append(all_detections[matches])
# Convert this list into families
template_names = list(set([d.template_name
for d in declustered_detections]))
Expand Down
25 changes: 25 additions & 0 deletions eqcorrscan/tests/match_filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,31 @@ def test_party_decluster(self):
self.party.copy().decluster(
trig_int=trig_int, timing='origin', metric=metric)

def test_party_decluster_same_times(self):
"""
Test that the correct detection is associated with the peak.
Tests for the case where two detections from different templates are
made at the same time - the peak-finding finds the best, but decluster
did not always correctly associate the correct detection with that
peak.
"""
# Test insertion before
test_party = self.party.copy()
det = test_party[0][0].copy()
det.detect_time = test_party[1][0].detect_time
det.detect_val = 4
test_party[0].detections.append(det)
test_party.decluster(1)
assert det not in [d for f in test_party for d in f]
# Tes insertion after
test_party = self.party.copy()
det = test_party[1][0].copy()
det.detect_time = test_party[0][0].detect_time
det.detect_val = 4
test_party[1].detections.append(det)
test_party.decluster(1)
assert det not in [d for f in test_party for d in f]

def test_party_rethreshold(self):
"""Make sure that rethresholding removes the events we want it to."""
party = self.party.copy()
Expand Down

0 comments on commit 570502a

Please sign in to comment.