Problem with different methods in sensor_connectivity example

I’m running the example sensor_connectivity.ipynb (“Compute all-to-all connectivity in sensor space”) in version 1.4 (Centos 7). When I change method='pli' to method='coh' in spectral_connectivity_epochs(), I get the following error message (from plot_connectivity_epochs()) which I don’t understand.

ValueError Traceback (most recent call last)
Cell In[34], line 34
29 con = spectral_connectivity_epochs(
30 epochs, method=‘coh’, mode=‘multitaper’, sfreq=sfreq, fmin=fmin, fmax=fmax,
31 faverage=True, tmin=tmin, mt_adaptive=False, n_jobs=1)
33 # Now, visualize the connectivity in 3D:
—> 34 plot_sensors_connectivity(
35 epochs.info,
36 con.get_data(output=‘dense’)[:, :, 0])

File /imaging/local/software/mne_python/mne1.4.0_1/lib/python3.10/site-packages/mne_connectivity/viz/_3d.py:84, in plot_sensors_connectivity(info, con, picks, cbar_label)
81 con_val = np.array(con_val)
83 # Show the connections as tubes between sensors
—> 84 vmax = np.max(con_val)
85 vmin = np.min(con_val)
86 for val, nodes in zip(con_val, con_nodes):

File <array_function internals>:5, in amax(*args, **kwargs)

File /imaging/local/software/mne_python/mne1.4.0_1/lib/python3.10/site-packages/numpy/core/fromnumeric.py:2754, in amax(a, axis, out, keepdims, initial, where)
2638 @array_function_dispatch(_amax_dispatcher)
2639 def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
2640 where=np._NoValue):
2641 “”"
2642 Return the maximum of an array or maximum along an axis.
2643
(…)
2752 5
2753 “”"
→ 2754 return _wrapreduction(a, np.maximum, ‘max’, axis, None, out,
2755 keepdims=keepdims, initial=initial, where=where)

File /imaging/local/software/mne_python/mne1.4.0_1/lib/python3.10/site-packages/numpy/core/fromnumeric.py:86, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
83 else:
84 return reduction(axis=axis, out=out, **passkwargs)
—> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

ValueError: zero-size array to reduction operation maximum which has no identity

@olafhauk hi,

I can replicate the problem. I had a quick look at the mne codes, it seems like a threshold issue while plotting the connectivity matrix.

API: mne_connectivity.viz.plot_sensors_connectivity computes the threshold value using 20 strongest connections (default). Afterwards, only those sensors are kept that are less than 5cm apart.

Here is the code snippet:

    # Get the strongest connections
    n_con = 20  # show up to 20 connections
    min_dist = 0.05  # exclude sensors that are less than 5cm apart

    threshold = np.sort(con, axis=None)[-n_con]
    print('threshold', threshold)
    ii, jj = np.where(con >= threshold)

    # Remove close connections
    con_nodes = list()
    con_val = list()
    for i, j in zip(ii, jj):
        if np.linalg.norm(sens_loc[i] - sens_loc[j]) > min_dist:
            con_nodes.append((i, j))
            con_val.append(con[i, j])

    con_val = np.array(con_val)

if you use method=coh, no sensor is significant with the default threshold. If you decrease the threshold it should work. The API should prompt a warring in this ca!se

@drammock is it possible to assign the threshold by the user? Maybe improving the API would be nice too.

Best,
Dip

1 Like

I completely agree the threshold criteria should be available to the user. I actually thought it should always produce the 20 strongest connections, but I probably got that wrong. It would be great to be able to run this example with all available connectivity methods.

This is a typo; sensor pairs more than the threshold distance are kept.

I can reproduce it too. what is going on is that with coh the 20 strongest sensor pairs are < 5cm apart. I’ll open a GH issue to discuss possible fixes.

If you use the development version of MNE-Connectivity (pip install -U --no-deps git+https://github.com/mne-tools/mne-connectivity@main), you can pass in n_con (default is still 20). If you pass in n_con=50 you’ll get a plot with just a few connections.