# Buggy code
def _onselect(
    eclick,
    erelease,
    tfr,
    pos,
    ch_type,
    itmin,
    itmax,
    ifmin,
    ifmax,
    cmap,
    fig,
    layout=None,
):
    """Callback called from topomap for drawing average tfr over channels."""
    import matplotlib.pyplot as plt

    pos, _ = _check_outlines(pos, outlines="head", head_pos=None)
    ax = eclick.inaxes
    xmin = min(eclick.xdata, erelease.xdata)
    xmax = max(eclick.xdata, erelease.xdata)
    ymin = min(eclick.ydata, erelease.ydata)
    ymax = max(eclick.ydata, erelease.ydata)
    indices = [
        i
        for i in range(len(pos))
        if pos[i][0] < xmax
        and pos[i][0] > xmin
        and pos[i][1] < ymax
        and pos[i][1] > ymin
    ]
    for idx, circle in enumerate(ax.artists):
        if idx in indices:
            circle.set_color("r")
        else:
            circle.set_color("black")
    plt.gcf().canvas.draw()
    if not indices:
        return
    data = tfr.data
    if ch_type == "mag":
        picks = pick_types(tfr.info, meg=ch_type, ref_meg=False)
        data = np.mean(data[indices, ifmin:ifmax, itmin:itmax], axis=0)
        chs = [tfr.ch_names[picks[x]] for x in indices]
    elif ch_type == "grad":
        picks = pick_types(tfr.info, meg=ch_type, ref_meg=False)
        from ..channels.layout import _pair_grad_sensors

        grads = _pair_grad_sensors(tfr.info, layout=layout, topomap_coords=False)
        idxs = list()
        for idx in indices:
            idxs.append(grads[idx * 2])
            idxs.append(grads[idx * 2 + 1])  # pair of grads
        data = np.mean(data[idxs, ifmin:ifmax, itmin:itmax], axis=0)
        chs = [tfr.ch_names[x] for x in idxs]
    elif ch_type == "eeg":
        picks = pick_types(tfr.info, meg=False, eeg=True, ref_meg=False)
        data = np.mean(data[indices, ifmin:ifmax, itmin:itmax], axis=0)
        chs = [tfr.ch_names[picks[x]] for x in indices]
    logger.info("Averaging TFR over channels " + str(chs))
    if len(fig) == 0:
        fig.append(figure_nobar())
    if not plt.fignum_exists(fig[0].number):
        fig[0] = figure_nobar()
    ax = fig[0].add_subplot(111)
    itmax = min(itmax, len(tfr.times) - 1)
    ifmax = min(ifmax, len(tfr.freqs) - 1)
    extent = (
        tfr.times[itmin] * 1e3,
        tfr.times[itmax] * 1e3,
        tfr.freqs[ifmin],
        tfr.freqs[ifmax],
    )

    title = "Average over %d %s channels." % (len(chs), ch_type)
    ax.set_title(title)
    ax.set_xlabel("Time (ms)")
    ax.set_ylabel("Frequency (Hz)")
    img = ax.imshow(data, extent=extent, aspect="auto", origin="lower", cmap=cmap)
    if len(fig[0].get_axes()) < 2:
        fig[0].get_axes()[1].cbar = fig[0].colorbar(mappable=img)
    else:
        fig[0].get_axes()[1].cbar.on_mappable_changed(mappable=img)
    fig[0].canvas.draw()
    plt.figure(fig[0].number)
    plt_show(True)


# Traceback (ValueError)
n [15]: Traceback (most recent call last):
File "/tsi/doctorants/mainak/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 291, in mouseReleaseEvent
FigureCanvasBase.button_release_event(self, x, y, button)
File "/tsi/doctorants/mainak/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1891, in button_release_event
self.callbacks.process(s, event)
File "/tsi/doctorants/mainak/anaconda/lib/python2.7/site-packages/matplotlib/cbook.py", line 533, in process
proxy(*args, **kwargs)
File "/tsi/doctorants/mainak/anaconda/lib/python2.7/site-packages/matplotlib/cbook.py", line 408, in __call__
return mtd(*args, **kwargs)
File "/tsi/doctorants/mainak/anaconda/lib/python2.7/site-packages/matplotlib/widgets.py", line 1250, in release
self.onselect(vmin, vmax)
File "/tsi/doctorants/mainak/mne-python/mne/viz/evoked.py", line 90, in _butterfly_onselect
layout=None)
File "/tsi/doctorants/mainak/mne-python/mne/viz/topomap.py", line 49, in _prepare_topo_plot
picks, pos = _pair_grad_sensors(info, layout)
File "/tsi/doctorants/mainak/mne-python/mne/channels/layout.py", line 689, in _pair_grad_sensors
raise ValueError("No 'grad' channel pairs found.")
ValueError: No 'grad' channel pairs found.

# Fixed code
def _onselect(
    eclick,
    erelease,
    tfr,
    pos,
    ch_type,
    itmin,
    itmax,
    ifmin,
    ifmax,
    cmap,
    fig,
    layout=None,
):
    """Callback called from topomap for drawing average tfr over channels."""
    import matplotlib.pyplot as plt

    pos, _ = _check_outlines(pos, outlines="head", head_pos=None)
    ax = eclick.inaxes
    xmin = min(eclick.xdata, erelease.xdata)
    xmax = max(eclick.xdata, erelease.xdata)
    ymin = min(eclick.ydata, erelease.ydata)
    ymax = max(eclick.ydata, erelease.ydata)
    indices = [
        i
        for i in range(len(pos))
        if pos[i][0] < xmax
        and pos[i][0] > xmin
        and pos[i][1] < ymax
        and pos[i][1] > ymin
    ]
    for idx, circle in enumerate(ax.artists):
        if idx in indices:
            circle.set_color("r")
        else:
            circle.set_color("black")
    plt.gcf().canvas.draw()
    if not indices:
        return
    data = tfr.data
    if ch_type == "mag":
        picks = pick_types(tfr.info, meg=ch_type, ref_meg=False)
        data = np.mean(data[indices, ifmin:ifmax, itmin:itmax], axis=0)
        chs = [tfr.ch_names[picks[x]] for x in indices]
    elif ch_type == "grad":
        from ..channels.layout import _pair_grad_sensors

        grads = _pair_grad_sensors(tfr.info, layout=layout, topomap_coords=False)
        idxs = list()
        for idx in indices:
            idxs.append(grads[idx * 2])
            idxs.append(grads[idx * 2 + 1])  # pair of grads
        data = np.mean(data[idxs, ifmin:ifmax, itmin:itmax], axis=0)
        chs = [tfr.ch_names[x] for x in idxs]
    elif ch_type == "eeg":
        picks = pick_types(tfr.info, meg=False, eeg=True, ref_meg=False)
        data = np.mean(data[indices, ifmin:ifmax, itmin:itmax], axis=0)
        chs = [tfr.ch_names[picks[x]] for x in indices]
    logger.info("Averaging TFR over channels " + str(chs))
    if len(fig) == 0:
        fig.append(figure_nobar())
    if not plt.fignum_exists(fig[0].number):
        fig[0] = figure_nobar()
    ax = fig[0].add_subplot(111)
    itmax = len(tfr.times) - 1 if itmax is None else min(itmax, len(tfr.times) - 1)
    ifmax = len(tfr.freqs) - 1 if ifmax is None else min(ifmax, len(tfr.freqs) - 1)
    if itmin is None:
        itmin = 0
    if ifmin is None:
        ifmin = 0
    extent = (
        tfr.times[itmin] * 1e3,
        tfr.times[itmax] * 1e3,
        tfr.freqs[ifmin],
        tfr.freqs[ifmax],
    )

    title = "Average over %d %s channels." % (len(chs), ch_type)
    ax.set_title(title)
    ax.set_xlabel("Time (ms)")
    ax.set_ylabel("Frequency (Hz)")
    img = ax.imshow(data, extent=extent, aspect="auto", origin="lower", cmap=cmap)
    if len(fig[0].get_axes()) < 2:
        fig[0].get_axes()[1].cbar = fig[0].colorbar(mappable=img)
    else:
        fig[0].get_axes()[1].cbar.on_mappable_changed(mappable=img)
    fig[0].canvas.draw()
    plt.figure(fig[0].number)
    plt_show(True)