Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for spikes and raise error if found. #77

Merged
merged 11 commits into from
Mar 22, 2017
27 changes: 26 additions & 1 deletion eqcorrscan/core/match_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3758,7 +3758,7 @@ def match_filter(template_names, template_list, st, threshold,
raise MatchFilterError(
'Template sampling rate does not '
'match continuous data')

_spike_test(st, 0.99, 1e6)
# Copy the stream here because we will muck about with it
stream = st.copy()
templates = copy.deepcopy(template_list)
Expand Down Expand Up @@ -4023,6 +4023,31 @@ def match_filter(template_names, template_list, st, threshold,
return detections, det_cat, detection_streams


def _spike_test(stream, percent=0.99, multiplier=1e6):
"""
Check for very large spikes in data and raise an error if found.

:param stream: Stream to look for spikes in.
:type stream: :class:`obspy.core.stream.Stream`
:param percent: Percentage as a decimal to calcualte range for.
:type percent: float
:param multiple: Multiplier of range to define a spike.
:type multiple: float
"""
for tr in stream:
if (tr.data > 2 * np.max(
np.sort(np.abs(
tr))[0:int(percent * len(tr.data))]) * multiplier).sum() > 0:
msg = ('Spikes above ' + str(multiplier) +
' of the range of ' + str(percent) +
' of the data present, check. \n ' +
'This would otherwise likely result in an issue during ' +
'FFT prior to cross-correlation.\n' +
'If you think this spike is real please report ' +
'this as a bug.')
raise MatchFilterError(msg)


if __name__ == "__main__":
import doctest
doctest.testmod()
Expand Down
9 changes: 8 additions & 1 deletion eqcorrscan/tests/match_filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from eqcorrscan.core.match_filter import read_detections, get_catalog
from eqcorrscan.core.match_filter import write_catalog, extract_from_stream
from eqcorrscan.core.match_filter import Tribe, Template, Party, Family
from eqcorrscan.core.match_filter import read_party, read_tribe
from eqcorrscan.core.match_filter import read_party, read_tribe, _spike_test
from eqcorrscan.tutorials.get_geonet_events import get_geonet_events
from eqcorrscan.utils import pre_processing, catalog_utils

Expand Down Expand Up @@ -134,6 +134,13 @@ def test_failed_normxcorr(self):
ccc = normxcorr2(template=[0, 1, 2, 3, 4], image='bob')
self.assertEqual(ccc, 'NaN')

def test_spike_test(self):
"""Check that an error is raised!"""
stream = read()
stream[0].data[100] = 1e20
with self.assertRaises(MatchFilterError):
_spike_test(stream)


class TestSynthData(unittest.TestCase):
def test_debug_range(self):
Expand Down