def test_length_checking(self): """Check that lengths are fixed""" st = read() templates = [] for i in range(20): template = Stream() for _tr in st: tr = _tr.copy() tr.stats.starttime += i * 100 tr.data = np.random.randn(200) template += tr templates.append(template) continuous_data = st for i, tr in enumerate(continuous_data): tr.data = np.random.randn((200 * 20) + ((i + 1) * 5)) tr.stats.starttime += (i + 1) * 5 min_start = min([tr.stats.starttime for tr in continuous_data]) max_end = max([tr.stats.endtime for tr in continuous_data]) data_len = ( (max_end - min_start) * continuous_data[0].stats.sampling_rate) + 1 continuous_data_prepped, templates = _prep_data_for_correlation( stream=continuous_data.copy(), templates=templates, force_stream_epoch=True) for tr in continuous_data_prepped: self.assertEqual(tr.stats.npts, data_len) self.assertEqual(tr.stats.starttime, min_start) self.assertEqual(tr.stats.endtime, max_end) continuous_data_prepped, templates = _prep_data_for_correlation( stream=continuous_data, templates=templates, force_stream_epoch=False) for tr in continuous_data_prepped: self.assertEqual(tr.stats.npts, (200 * 20) + (len(continuous_data) * 5))
def test_order_irrevelant(self): """ Early on the order of streams input was affecting output. """ st1 = self.stream_list[0].copy() ordered_streams = sorted(self.stream_list, key=lambda s: s[0].stats.starttime) shuffled_streams = deepcopy(ordered_streams) shuffle(shuffled_streams) order_prepped_st1, order_prepped_streams = _prep_data_for_correlation( stream=st1, templates=ordered_streams, force_stream_epoch=False) shuff_prepped_st1, shuff_prepped_streams = _prep_data_for_correlation( stream=st1, templates=shuffled_streams, force_stream_epoch=False) self.assertEqual(order_prepped_st1, shuff_prepped_st1) shuff_prepped_streams = sorted(shuff_prepped_streams, key=lambda s: s[0].stats.starttime) for ordered, shuffled in zip(order_prepped_streams, shuff_prepped_streams): for tr_ordered, tr_shuffled in zip(ordered, shuffled): self.assertEqual(tr_ordered, tr_shuffled)
def test_correct_padding(self): """check that nans are put where they are supposed to be """ _, prepped_streams = _prep_data_for_correlation( stream=self.stream_list[0], templates=deepcopy(self.stream_list), force_stream_epoch=False) for original, prepped in zip(self.stream_list, prepped_streams): for tr in prepped: original_tr = original.select(id=tr.id) if len(original_tr) == 0: self.assertTrue(np.all(np.isnan(tr.data))) else: self.assertEqual(tr, original_tr[0])
def test_duplicate_template_channels(self): """ Check that duplicate template channels result in duplicate template channels but not stream channels. """ templates = deepcopy(self.stream_list) templates.sort(key=lambda t: t[0].stats.starttime) stream = deepcopy(self.stream_list[0]) additional_channel = templates[0][0] additional_channel.stats.starttime += 30 templates[0] += additional_channel assert len(templates[0]) == 10 continuous_data, templates = _prep_data_for_correlation( stream=stream, templates=templates, force_stream_epoch=True) for template in templates: assert len(template) == 10 assert len(continuous_data) == 9
def test_continuous_data_removal(self): """Check that data that should be removed are.""" st = read() templates = [] for i in range(20): template = Stream() for _tr in st[1:]: tr = _tr.copy() tr.stats.starttime += i * 100 tr.data = np.random.randn(200) template += tr templates.append(template) continuous_data = st for tr in continuous_data: tr.data = np.random.randn(200 * 20) continuous_data, templates = _prep_data_for_correlation( stream=continuous_data, templates=templates, force_stream_epoch=True) self.assertEqual(len(continuous_data), 2) for template in templates: self.assertEqual(len(template), 2)
def match_filter(template_names, template_list, st, threshold, threshold_type, trig_int, plot=False, plotdir=None, xcorr_func=None, concurrency=None, cores=None, plot_format='png', output_cat=False, output_event=True, extract_detections=False, arg_check=True, full_peaks=False, peak_cores=None, spike_test=True, **kwargs): """ Main matched-filter detection function. Over-arching code to run the correlations of given templates with a day of seismic data and output the detections based on a given threshold. For a functional example see the tutorials. :type template_names: list :param template_names: List of template names in the same order as template_list :type template_list: list :param template_list: A list of templates of which each template is a :class:`obspy.core.stream.Stream` of obspy traces containing seismic data and header information. :type st: :class:`obspy.core.stream.Stream` :param st: A Stream object containing all the data available and required for the correlations with templates given. For efficiency this should contain no excess traces which are not in one or more of the templates. This will now remove excess traces internally, but will copy the stream and work on the copy, leaving your input stream untouched. :type threshold: float :param threshold: A threshold value set based on the threshold_type :type threshold_type: str :param threshold_type: The type of threshold to be used, can be MAD, absolute or av_chan_corr. See Note on thresholding below. :type trig_int: float :param trig_int: Minimum gap between detections from one template in seconds. If multiple detections occur within trig_int of one-another, the one with the highest cross-correlation sum will be selected. :type plot: bool :param plot: Turn plotting on or off :type plotdir: str :param plotdir: Path to plotting folder, plots will be output here, defaults to None, and plots are shown on screen. :type xcorr_func: str or callable :param xcorr_func: A str of a registered xcorr function or a callable for implementing a custom xcorr function. For more information see: :func:`eqcorrscan.utils.correlate.register_array_xcorr` :type concurrency: str :param concurrency: The type of concurrency to apply to the xcorr function. Options are 'multithread', 'multiprocess', 'concurrent'. For more details see :func:`eqcorrscan.utils.correlate.get_stream_xcorr` :type cores: int :param cores: Number of cores to use :type plot_format: str :param plot_format: Specify format of output plots if saved :type output_cat: bool :param output_cat: Specifies if matched_filter will output an obspy.Catalog class containing events for each detection. Default is False, in which case matched_filter will output a list of detection classes, as normal. :type output_event: bool :param output_event: Whether to include events in the Detection objects, defaults to True, but for large cases you may want to turn this off as Event objects can be quite memory intensive. :type extract_detections: bool :param extract_detections: Specifies whether or not to return a list of streams, one stream per detection. :type arg_check: bool :param arg_check: Check arguments, defaults to True, but if running in bulk, and you are certain of your arguments, then set to False. :type full_peaks: bool :param full_peaks: See :func: `eqcorrscan.utils.findpeaks.find_peaks_compiled` :type peak_cores: int :param peak_cores: Number of processes to use for parallel peak-finding (if different to `cores`). :type spike_test: bool :param spike_test: If set True, raise error when there is a spike in data. defaults to True. .. Note:: When using the "fftw" correlation backend the length of the fft can be set. See :mod:`eqcorrscan.utils.correlate` for more info. .. note:: **Returns:** If neither `output_cat` or `extract_detections` are set to `True`, then only the list of :class:`eqcorrscan.core.match_filter.Detection`'s will be output: :return: :class:`eqcorrscan.core.match_filter.Detection` detections for each detection made. :rtype: list If `output_cat` is set to `True`, then the :class:`obspy.core.event.Catalog` will also be output: :return: Catalog containing events for each detection, see above. :rtype: :class:`obspy.core.event.Catalog` If `extract_detections` is set to `True` then the list of :class:`obspy.core.stream.Stream`'s will also be output. :return: list of :class:`obspy.core.stream.Stream`'s for each detection, see above. :rtype: list .. note:: If your data contain gaps these must be padded with zeros before using this function. The `eqcorrscan.utils.pre_processing` functions will provide gap-filled data in the appropriate format. Note that if you pad your data with zeros before filtering or resampling the gaps will not be all zeros after filtering. This will result in the calculation of spurious correlations in the gaps. .. Note:: Detections are not corrected for `pre-pick`, the detection.detect_time corresponds to the beginning of the earliest template channel at detection. .. note:: **Data overlap:** Internally this routine shifts and trims the data according to the offsets in the template (e.g. if trace 2 starts 2 seconds after trace 1 in the template then the continuous data will be shifted by 2 seconds to align peak correlations prior to summing). Because of this, detections at the start and end of continuous data streams **may be missed**. The maximum time-period that might be missing detections is the maximum offset in the template. To work around this, if you are conducting matched-filter detections through long-duration continuous data, we suggest using some overlap (a few seconds, on the order of the maximum offset in the templates) in the continous data. You will then need to post-process the detections (which should be done anyway to remove duplicates). .. note:: **Thresholding:** **MAD** threshold is calculated as the: .. math:: threshold {\\times} (median(abs(cccsum))) where :math:`cccsum` is the cross-correlation sum for a given template. **absolute** threshold is a true absolute threshold based on the cccsum value. **av_chan_corr** is based on the mean values of single-channel cross-correlations assuming all data are present as required for the template, e.g: .. math:: av\_chan\_corr\_thresh=threshold \\times (cccsum\ /\ len(template)) where :math:`template` is a single template from the input and the length is the number of channels within this template. .. note:: The output_cat flag will create an :class:`obspy.core.event.Catalog` containing one event for each :class:`eqcorrscan.core.match_filter.Detection`'s generated by match_filter. Each event will contain a number of comments dealing with correlation values and channels used for the detection. Each channel used for the detection will have a corresponding :class:`obspy.core.event.Pick` which will contain time and waveform information. **HOWEVER**, the user should note that the pick times do not account for the prepick times inherent in each template. For example, if a template trace starts 0.1 seconds before the actual arrival of that phase, then the pick time generated by match_filter for that phase will be 0.1 seconds early. .. Note:: xcorr_func can be used as follows: .. rubric::xcorr_func argument example >>> import obspy >>> import numpy as np >>> from eqcorrscan.core.match_filter.matched_filter import ( ... match_filter) >>> from eqcorrscan.utils.correlate import time_multi_normxcorr >>> # define a custom xcorr function >>> def custom_normxcorr(templates, stream, pads, *args, **kwargs): ... # Just to keep example short call other xcorr function ... # in practice you would define your own function here ... print('calling custom xcorr function') ... return time_multi_normxcorr(templates, stream, pads) >>> # generate some toy templates and stream >>> random = np.random.RandomState(42) >>> template = obspy.read() >>> stream = obspy.read() >>> for num, tr in enumerate(stream): # iter st and embed templates ... data = tr.data ... tr.data = random.randn(6000) * 5 ... tr.data[100: 100 + len(data)] = data >>> # call match_filter ane ensure the custom function is used >>> detections = match_filter( ... template_names=['1'], template_list=[template], st=stream, ... threshold=.5, threshold_type='absolute', trig_int=1, ... plotvar=False, ... xcorr_func=custom_normxcorr) # doctest:+ELLIPSIS calling custom xcorr function... """ from eqcorrscan.core.match_filter.detection import Detection from eqcorrscan.utils.plotting import _match_filter_plot if "plotvar" in kwargs.keys(): Logger.warning("plotvar is depreciated, use plot instead") plot = kwargs.get("plotvar") if arg_check: # Check the arguments to be nice - if arguments wrong type the parallel # output for the error won't be useful if not isinstance(template_names, list): raise MatchFilterError('template_names must be of type: list') if not isinstance(template_list, list): raise MatchFilterError('templates must be of type: list') if not len(template_list) == len(template_names): raise MatchFilterError('Not the same number of templates as names') for template in template_list: if not isinstance(template, Stream): msg = 'template in template_list must be of type: ' + \ 'obspy.core.stream.Stream' raise MatchFilterError(msg) if not isinstance(st, Stream): msg = 'st must be of type: obspy.core.stream.Stream' raise MatchFilterError(msg) if str(threshold_type) not in [ str('MAD'), str('absolute'), str('av_chan_corr') ]: msg = 'threshold_type must be one of: MAD, absolute, av_chan_corr' raise MatchFilterError(msg) for tr in st: if not tr.stats.sampling_rate == st[0].stats.sampling_rate: raise MatchFilterError( 'Sampling rates are not equal %f: %f' % (tr.stats.sampling_rate, st[0].stats.sampling_rate)) for template in template_list: for tr in template: if not tr.stats.sampling_rate == st[0].stats.sampling_rate: raise MatchFilterError('Template sampling rate does not ' 'match continuous data') for template in template_list: for tr in template: if isinstance(tr.data, np.ma.core.MaskedArray): raise MatchFilterError( 'Template contains masked array, split first') if spike_test: Logger.info("Checking for spikes in data") _spike_test(st) if cores is not None: parallel = True else: parallel = False if peak_cores is None: peak_cores = cores # Copy the stream here because we will muck about with it Logger.info("Copying data to keep your input safe") stream = st.copy() templates = [t.copy() for t in template_list] _template_names = template_names.copy() # This can just be a shallow copy Logger.info("Reshaping templates") stream, templates, _template_names = _prep_data_for_correlation( stream=stream, templates=templates, template_names=_template_names) if len(templates) == 0: raise IndexError("No matching data") Logger.info('Starting the correlation run for these data') for template in templates: Logger.debug(template.__str__()) Logger.debug(stream.__str__()) multichannel_normxcorr = get_stream_xcorr(xcorr_func, concurrency) outtic = default_timer() [cccsums, no_chans, chans] = multichannel_normxcorr(templates=templates, stream=stream, cores=cores, **kwargs) if len(cccsums[0]) == 0: raise MatchFilterError('Correlation has not run, zero length cccsum') outtoc = default_timer() Logger.info( 'Looping over templates and streams took: {0:.4f}s'.format(outtoc - outtic)) Logger.debug('The shape of the returned cccsums is: {0}'.format( cccsums.shape)) Logger.debug('This is from {0} templates correlated with {1} channels of ' 'data'.format(len(templates), len(stream))) detections = [] if output_cat: det_cat = Catalog() if str(threshold_type) == str("absolute"): thresholds = [threshold for _ in range(len(cccsums))] elif str(threshold_type) == str('MAD'): thresholds = [ threshold * np.median(np.abs(cccsum)) for cccsum in cccsums ] else: thresholds = [threshold * no_chans[i] for i in range(len(cccsums))] if peak_cores is None: peak_cores = cores outtic = default_timer() all_peaks = multi_find_peaks(arr=cccsums, thresh=thresholds, parallel=parallel, trig_int=int(trig_int * stream[0].stats.sampling_rate), full_peaks=full_peaks, cores=peak_cores) outtoc = default_timer() Logger.info("Finding peaks took {0:.4f}s".format(outtoc - outtic)) for i, cccsum in enumerate(cccsums): if np.abs(np.mean(cccsum)) > 0.05: Logger.warning('Mean is not zero! Check this!') # Set up a trace object for the cccsum as this is easier to plot and # maintains timing if plot: _match_filter_plot(stream=stream, cccsum=cccsum, template_names=_template_names, rawthresh=thresholds[i], plotdir=plotdir, plot_format=plot_format, i=i) if all_peaks[i]: Logger.debug("Found {0} peaks for template {1}".format( len(all_peaks[i]), _template_names[i])) for peak in all_peaks[i]: detecttime = (stream[0].stats.starttime + peak[1] / stream[0].stats.sampling_rate) detection = Detection(template_name=_template_names[i], detect_time=detecttime, no_chans=no_chans[i], detect_val=peak[0], threshold=thresholds[i], typeofdet='corr', chans=chans[i], threshold_type=threshold_type, threshold_input=threshold) if output_cat or output_event: detection._calculate_event(template_st=templates[i]) detections.append(detection) if output_cat: det_cat.append(detection.event) else: Logger.debug("Found 0 peaks for template {0}".format( _template_names[i])) Logger.info("Made {0} detections from {1} templates".format( len(detections), len(templates))) if extract_detections: detection_streams = extract_from_stream(stream, detections) del stream, templates if output_cat and not extract_detections: return detections, det_cat elif not extract_detections: return detections elif extract_detections and not output_cat: return detections, detection_streams else: return detections, det_cat, detection_streams
def cross_chan_correlation(st1, streams, shift_len=0.0, allow_individual_trace_shifts=True, xcorr_func='fftw', concurrency="concurrent", cores=1, **kwargs): """ Calculate cross-channel correlation. Determine the cross-channel correlation between two streams of multichannel seismic data. :type st1: obspy.core.stream.Stream :param st1: Stream one :type streams: list :param streams: Streams to compare to. :type shift_len: float :param shift_len: How many seconds for templates to shift :type allow_individual_trace_shifts: bool :param allow_individual_trace_shifts: Controls whether templates are shifted by shift_len in relation to the picks as a whole, or whether each trace can be shifted individually. Defaults to True. :type xcorr_func: str, callable :param xcorr_func: The method for performing correlations. Accepts either a string or callable. See :func:`eqcorrscan.utils.correlate.register_array_xcorr` for more details :type concurrency: str :param concurrency: Concurrency for xcorr-func. :type cores: int :param cores: Number of threads to parallel over :returns: cross channel correlation, float - normalized by number of channels. locations of maximums :rtype: numpy.ndarray, numpy.ndarray .. Note:: If no matching channels were found then the coherance and index for that stream will be nan. """ # Cut all channels in stream-list to be the correct length (shorter than # st1 if stack = False by shift_len). allow_individual_trace_shifts = (allow_individual_trace_shifts and shift_len > 0) n_streams = len(streams) df = st1[0].stats.sampling_rate end_trim = int((shift_len * df) / 2) _streams = [] if end_trim > 0: for stream in streams: _stream = stream.copy() # Do not work on the users data for tr in _stream: tr.data = tr.data[end_trim:-end_trim] if tr.stats.sampling_rate != df: raise NotImplementedError("Sampling rates differ") _streams.append(_stream) streams = _streams else: # _prep_data_for_correlation works in place on data. # We need to copy it first. streams = [stream.copy() for stream in streams] # Check which channels are in st1 and match those in the stream_list st1_preped, prep_streams, stream_indexes = _prep_data_for_correlation( stream=st1.copy(), templates=streams, template_names=list(range(len(streams))), force_stream_epoch=False) # Run the correlations multichannel_normxcorr = get_stream_xcorr(xcorr_func, concurrency) [cccsums, no_chans, _] = multichannel_normxcorr(templates=prep_streams, stream=st1_preped, cores=cores, stack=False, **kwargs) # Find maximas, sum and divide by no_chans if allow_individual_trace_shifts: coherances = cccsums.max(axis=-1).sum(axis=-1) / no_chans else: cccsums = cccsums.sum(axis=1) coherances = cccsums.max(axis=-1) / no_chans # Subtract half length of correlogram and convert positions to seconds positions = (cccsums.argmax(axis=-1) - end_trim) / df # This section re-orders the coherences to correspond to the order of the # input streams _coherances = np.empty(n_streams) if allow_individual_trace_shifts: n_max_traces = max([len(st) for st in prep_streams]) # Set shifts for nan-traces to nan for i, tr in enumerate(st1_preped): if np.ma.is_masked(tr.data): positions[:, i] = np.nan else: positions = positions[:, np.newaxis] n_max_traces = 1 n_shifts_per_stream = positions.shape[1] _positions = np.empty([n_streams, n_max_traces]) _coherances.fill(np.nan) _positions.fill(np.nan) # Insert the correlations and shifts at the correct index for the templates _coherances[np.ix_(stream_indexes)] = coherances _positions[np.ix_(stream_indexes, range(n_shifts_per_stream))] = (positions) if not allow_individual_trace_shifts: # remove empty third axis from array _positions = _positions[:, ] return _coherances, _positions
def cross_chan_correlation(st1, streams, shift_len=0.0, xcorr_func='fftw', concurrency="concurrent", cores=1, **kwargs): """ Calculate cross-channel correlation. Determine the cross-channel correlation between two streams of multichannel seismic data. :type st1: obspy.core.stream.Stream :param st1: Stream one :type streams: list :param streams: Streams to compare to. :type shift_len: float :param shift_len: Seconds to shift, only used if `allow_shift=True` :type xcorr_func: str, callable :param xcorr_func: The method for performing correlations. Accepts either a string or callable. See :func:`eqcorrscan.utils.correlate.register_array_xcorr` for more details :type concurrency: str :param concurrency: Concurrency for xcorr-func. :type cores: int :param cores: Number of threads to parallel over :returns: cross channel correlation, float - normalized by number of channels. locations of maximums :rtype: numpy.ndarray, numpy.ndarray .. Note:: If no matching channels were found then the coherance and index for that stream will be nan. """ # Cut all channels in stream-list to be the correct length (shorter than # st1 if stack = False by shift_len). n_streams = len(streams) df = st1[0].stats.sampling_rate end_trim = int((shift_len * df) / 2) _streams = [] if end_trim > 0: for stream in streams: _stream = stream.copy() # Do not work on the users data for tr in _stream: tr.data = tr.data[end_trim: -end_trim] if tr.stats.sampling_rate != df: raise NotImplementedError("Sampling rates differ") _streams.append(_stream) streams = _streams # Check which channels are in st1 and match those in the stream_list st1, streams, stream_indexes = _prep_data_for_correlation( stream=st1, templates=streams, template_names=list(range(len(streams))), force_stream_epoch=False) # Run the correlations multichannel_normxcorr = get_stream_xcorr(xcorr_func, concurrency) [cccsums, no_chans, _] = multichannel_normxcorr( templates=streams, stream=st1, cores=cores, stack=False, **kwargs) # Find maximas, sum and divide by no_chans coherances = cccsums.max(axis=-1).sum(axis=-1) / no_chans positions = cccsums.argmax(axis=-1) # positions should probably have half the length of the correlogram # subtracted, and possibly be converted to seconds? _coherances = np.empty(n_streams) _positions = np.empty((n_streams, no_chans.max())) _coherances.fill(np.nan) _positions.fill(np.nan) for coh_ind, stream_ind in enumerate(stream_indexes): _coherances[stream_ind] = coherances[coh_ind] _positions[stream_ind] = positions[coh_ind] return _coherances, _positions