'Detect ECG related components using correlation.
.. note:: If no ECG channel is available, routine attempts to create
an artificial ECG based on cross-channel averaging.
Parameters
inst : instance of Raw, Epochs or Evoked
Object to compute sources from.
ch_name : str
The name of the channel to use for ECG peak detection.
The argument is mandatory if the dataset contains no ECG
channels.
threshold : float
The value above which a feature is classified as outlier. If
method is \'ctps\', defaults to 0.25, else defaults to 3.0.
start : int | float | None
First sample to include. If float, data will be interpreted as
time in seconds. If None, data will be used from the first sample.
stop : int | float | None
Last sample to not include. If float, data will be interpreted as
time in seconds. If None, data will be used to the last sample.
l_freq : float
Low pass frequency.
h_freq : float
High pass frequency.
method : {\'ctps\', \'correlation\'}
The method used for detection. If \'ctps\', cross-trial phase
statistics [1] are used to detect ECG related components.
Thresholding is then based on the significance value of a Kuiper
statistic.
If \'correlation\', detection is based on Pearson correlation
between the filtered data and the filtered ECG channel.
Thresholding is based on iterative z-scoring. The above
threshold components will be masked and the z-score will
be recomputed until no supra-threshold component remains.
Defaults to \'ctps\'.
reject_by_annotation : bool
If True, data annotated as bad will be omitted. Defaults to True.
.. versionadded:: 0.14.0
verbose : bool, str, int, or None
If not None, override default verbose level (see
:func:`mne.verbose` and :ref:`Logging documentation <tut_logging>`
for more). Defaults to self.verbose.
Returns
ecg_idx : list of int
The indices of ECG related components.
scores : np.ndarray of float, shape (``n_components_``)
The correlation scores.
See Also
find_bads_eog
References
[1] Dammers, J., Schiek, M., Boers, F., Silex, C., Zvyagintsev,
M., Pietrzyk, U., Mathiak, K., 2008. Integration of amplitude
and phase statistics for complete artifact removal in independent
components of neuromagnetic recordings. Biomedical
Engineering, IEEE Transactions on 55 (10), 2353-2362.'

@verbose
def find_bads_ecg(self, inst, ch_name=None, threshold=None, start=None, stop=None, l_freq=8, h_freq=16, method='ctps', reject_by_annotation=True, verbose=None):

    if (verbose is None):
        verbose = self.verbose
    idx_ecg = _get_ecg_channel_index(ch_name, inst)
    if (idx_ecg is None):
        if (verbose is not None):
            verbose = self.verbose
        (ecg, times) = _make_ecg(inst, start, stop, reject_by_annotation=reject_by_annotation, verbose=verbose)
    else:
        ecg = inst.ch_names[idx_ecg]
    if (method == 'ctps'):
        if (threshold is None):
            threshold = 0.25
        if isinstance(inst, BaseRaw):
            sources = self.get_sources(create_ecg_epochs(inst, ch_name, keep_ecg=False, reject_by_annotation=reject_by_annotation)).get_data()
            if (sources.shape[0] == 0):
                warn('No    ECG    activity    detected.    Consider    changing    the    input    parameters.')
        elif isinstance(inst, BaseEpochs):
            sources = self.get_sources(inst).get_data()
        else:
            raise ValueError('With    `ctps`    only    Raw    and    Epochs    input    is    supported')
        (_, p_vals, _) = ctps(sources)
        scores = p_vals.max((-1))
        ecg_idx = np.where((scores >= threshold))[0]
    elif (method == 'correlation'):
        if (threshold is None):
            threshold = 3.0
        scores = self.score_sources(inst, target=ecg, score_func='pearsonr', start=start, stop=stop, l_freq=l_freq, h_freq=h_freq, reject_by_annotation=reject_by_annotation, verbose=verbose)
        ecg_idx = find_outliers(scores, threshold=threshold)
    else:
        raise ValueError(('Method    "%s"    not    supported.' % method))
    ecg_idx = ecg_idx[np.abs(scores[ecg_idx]).argsort()[::(-1)]]
    self.labels_['ecg'] = list(ecg_idx)
    if (ch_name is None):
        ch_name = 'ECG-MAG'
    self.labels_[('ecg/%s' % ch_name)] = list(ecg_idx)
    return (self.labels_['ecg'], scores)
