def test_tutorial_template_gen(self): """Test template generation from tutorial, uses from_client method. Checks that the tutorial generates the templates we expect it to! """ from obspy import read from eqcorrscan.tutorials.template_creation import mktemplates import os import numpy as np from obspy.clients.fdsn.header import FDSNException import warnings testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') try: mktemplates(plot=False) except FDSNException: warnings.warn('FDSN error, is server down?') return for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') expected_template = read(os.path.join(testing_path, 'tutorial_template_' + str(template_no) + '.ms')) for tr in template: expected_tr = expected_template.select(station=tr.stats. station, channel=tr.stats. channel)[0] self.assertTrue((expected_tr.data.astype(np.float32) == tr.data.astype(np.float32)).all()) del(template) os.remove('tutorial_template_' + str(template_no) + '.ms')
def test_delay_grouping(self): """Test grouping by delays""" from obspy import read import glob import os from eqcorrscan.utils.clustering import group_delays from eqcorrscan.tutorials.template_creation import mktemplates from obspy.clients.fdsn.header import FDSNException import warnings testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data', 'WAV', 'TEST_') stream_files = glob.glob(os.path.join(testing_path, '*')) stream_list = [read(stream_file) for stream_file in stream_files] groups = group_delays(stream_list=stream_list) self.assertEqual(len(groups), 1) # All have same lag-times # Make some templates try: mktemplates(plot=False) except FDSNException: warnings.warn('FDSN exception raised, is server down?') return stream_list = [] for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') stream_list.append(template) groups = group_delays(stream_list=stream_list) self.assertEqual(len(groups), 4) # None have same lag-times # Cleanup the templates templates = glob.glob('tutorial_template_?.ms') for template in templates: os.remove(template)
def test_match_filter(self): """Test the match_filter tutorial, generates templates too.""" from eqcorrscan.tutorials.template_creation import mktemplates from eqcorrscan.tutorials.match_filter import run_tutorial from eqcorrscan.core.match_filter import read_detections import os import glob from obspy import read # Run mktemplates first to set-up for match_filter mktemplates(plot=False) for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') self.assertTrue(len(template) > 1) del (template) # Run the matched-filter tutorial_detections = run_tutorial(plot=False) # It should make 20 detections in total... testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') fname = os.path.join(testing_path, 'expected_tutorial_detections.txt') expected_detections = read_detections(fname) # Annoyingly something doesn't match, event when writing out detections # then reading them back in and comparing them to themselves in memory. expected_times = [ detection.detect_time for detection in expected_detections ] for expected_time in expected_times: expected_time.precision = 3 # Lower the precision slightly # expected_correlations = [round(detection.detect_val, 4) for detection # in expected_detections] for detection in tutorial_detections: detection.detect_time.precision = 3 self.assertIn(detection.detect_time, expected_times, msg='Detection at %s is not in expected detections' % detection.detect_time) if len(expected_detections) > len(tutorial_detections): # This is a fail but we are trying to debug actual_times = [ tutorial_detection.detect_time for tutorial_detection in tutorial_detections ] for detection in expected_detections: self.assertIn(detection.detect_time, actual_times, msg='Expected detection at %s was not made' % detection.detect_time) self.assertEqual(len(tutorial_detections), 22) # Cleanup the templates templates = glob.glob('tutorial_template_?.ms') for template in templates: os.remove(template)
def test_match_filter(self): """Test the match_filter tutorial, generates templates too.""" from eqcorrscan.tutorials.template_creation import mktemplates from eqcorrscan.tutorials.match_filter import run_tutorial from eqcorrscan.core.match_filter import read_detections import os import glob from obspy import read # Run mktemplates first to set-up for match_filter mktemplates(plot=False) for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') self.assertTrue(len(template) > 1) del(template) # Run the matched-filter tutorial_detections = run_tutorial(plot=False) # It should make 20 detections in total... testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') fname = os.path.join(testing_path, 'expected_tutorial_detections.txt') expected_detections = read_detections(fname) # Annoyingly something doesn't match, event when writing out detections # then reading them back in and comparing them to themselves in memory. expected_times = [detection.detect_time for detection in expected_detections] for expected_time in expected_times: expected_time.precision = 3 # Lower the precision slightly # expected_correlations = [round(detection.detect_val, 4) for detection # in expected_detections] for detection in tutorial_detections: detection.detect_time.precision = 3 self.assertIn(detection.detect_time, expected_times, msg='Detection at %s is not in expected detections' % detection.detect_time) # self.assertIn(round(detection.detect_val, 4), # expected_correlations, # msg='Detection with cross-correlation value %s not' + # ' in expected detections' % detection.detect_val) if len(expected_detections) > len(tutorial_detections): # This is a fail but we are trying to debug actual_times = [tutorial_detection.detect_time for tutorial_detection in tutorial_detections] for detection in expected_detections: self.assertIn(detection.detect_time, actual_times, msg='Expected detection at %s was not made' % detection.detect_time) self.assertEqual(len(tutorial_detections), 23) # Cleanup the templates templates = glob.glob('tutorial_template_?.ms') for template in templates: os.remove(template)
def test_templates_and_match(self): """Call the template creation then the matched-filter tests.""" print("Making templates") # Some output for travis to stop it from stalling mktemplates(plot=False) print("Made templates") for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') expected_template = read( os.path.join(self.testing_path, 'tutorial_template_' + str(template_no) + '.ms')) # self.assertTrue(len(template) > 1) self.assertEqual(len(template), len(expected_template)) # Run the matched-filter print("Running the match-filter") tutorial_detections = match_filter.run_tutorial(plot=False, num_cores=1) print("Match-filter ran") # It should make 20 detections in total... fname = os.path.join(self.testing_path, 'expected_tutorial_detections.txt') expected_detections = read_detections(fname) expected_times = [ detection.detect_time for detection in expected_detections ] for expected_time in expected_times: expected_time.precision = 3 # Lower the precision slightly # expected_correlations = [round(detection.detect_val, 4) for detection # in expected_detections] for detection in tutorial_detections: assert (detection.detect_val < detection.no_chans) detection.detect_time.precision = 3 self.assertIn(detection.detect_time, expected_times, msg='Detection at %s is not in expected detections' % detection.detect_time) if len(expected_detections) > len(tutorial_detections): # This is a fail but we are trying to debug actual_times = [ tutorial_detection.detect_time for tutorial_detection in tutorial_detections ] for detection in expected_detections: self.assertIn(detection.detect_time, actual_times, msg='Expected detection at %s was not made' % detection.detect_time) self.assertEqual(len(tutorial_detections), 22) for template_no in range(4): if os.path.isfile('tutorial_template_' + str(template_no) + '.ms'): os.remove('tutorial_template_' + str(template_no) + '.ms')
def test_tutorial_template_gen(self): """Test template generation from tutorial, uses from_client method. Checks that the tutorial generates the templates we expect it to! """ testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') mktemplates(plot=False) for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') expected_template = read( os.path.join(testing_path, 'tutorial_template_' + str(template_no) + '.ms')) for tr in template: expected_tr = expected_template.select( station=tr.stats.station, channel=tr.stats.channel)[0] self.assertTrue((expected_tr.data.astype( np.float32) == tr.data.astype(np.float32)).all()) del (template) os.remove('tutorial_template_' + str(template_no) + '.ms')
def test_delay_grouping(self): """Test grouping by delays""" testing_path = os.path.join(self.testing_path, 'WAV', 'TEST_') stream_files = glob.glob(os.path.join(testing_path, '*DFDPC*')) stream_list = [read(stream_file) for stream_file in stream_files] groups = group_delays(stream_list=stream_list) self.assertEqual(len(groups), 1) # All have same lag-times # Make some templates try: mktemplates(plot=False) except FDSNException: return stream_list = [] for template_no in range(4): template = read('tutorial_template_' + str(template_no) + '.ms') stream_list.append(template) groups = group_delays(stream_list=stream_list) self.assertEqual(len(groups), 4) # None have same lag-times # Cleanup the templates templates = glob.glob('tutorial_template_?.ms') for template in templates: os.remove(template)