'Fit Xdawn from epochs.
Parameters
epochs : Epochs object
An instance of Epoch on which Xdawn filters will be fitted.
y : ndarray | None (default None)
If None, used epochs.events[:, 2].
Returns
self : Xdawn instance
The Xdawn instance.'

def fit(self, epochs, y=None):

    if (not isinstance(epochs, BaseEpochs)):
        raise ValueError('epochs    must    be    an    Epochs    object.')
    X = epochs.get_data()
    X = X[:, _pick_data_channels(epochs.info), :]
    y = (epochs.events[:, 2] if (y is None) else y)
    self.event_id_ = epochs.event_id
    correct_overlap = self.correct_overlap
    if (correct_overlap == 'auto'):
        isi = np.diff(np.sort(epochs.events[:, 0]))
        window = int(((epochs.tmax - epochs.tmin) * epochs.info['sfreq']))
        correct_overlap = (isi.min() < window)
    if (epochs.baseline and correct_overlap):
        raise ValueError('Cannot    apply    correct_overlap    if    epochs    were    baselined.')
    (events, tmin, sfreq) = (None, 0.0, 1.0)
    if correct_overlap:
        events = epochs.events
        tmin = epochs.tmin
        sfreq = epochs.info['sfreq']
    self.correct_overlap_ = correct_overlap
    n_components = X.shape[1]
    (filters, patterns, evokeds) = _fit_xdawn(X, y, n_components=n_components, reg=self.reg, signal_cov=self.signal_cov, events=events, tmin=tmin, sfreq=sfreq)
    filters = filters.reshape((-1), n_components, filters.shape[(-1)])
    patterns = patterns.reshape((-1), n_components, patterns.shape[(-1)])
    (self.filters_, self.patterns_, self.evokeds_) = (dict(), dict(), dict())
    idx = np.argsort([value for (_, value) in iteritems(epochs.event_id)])
    for (eid, this_filter, this_pattern, this_evo) in zip(epochs.event_id, filters[idx], patterns[idx], evokeds[idx]):
        self.filters_[eid] = this_filter.T
        self.patterns_[eid] = this_pattern.T
        n_events = len(epochs[eid])
        evoked = EvokedArray(this_evo, epochs.info, tmin=epochs.tmin, comment=eid, nave=n_events)
        self.evokeds_[eid] = evoked
    return self
