mne-connectivity icon indicating copy to clipboard operation
mne-connectivity copied to clipboard

plot_sensors_connectivity fails when strongest conns are spatially close

Open drammock opened this issue 9 months ago • 0 comments

Describe the bug

plot_sensors_connectivity() fails with a cryptic error message when all of the n_con strongest connections occur between sensor pairs that are less than 5cm apart.

Steps to reproduce

in this example:

https://mne.tools/mne-connectivity/dev/auto_examples/sensor_connectivity.html

and in the call to spectral_connectivity_epochs change method from pli to coh. Running the modified example will reproduce the error. Passing in n_con=50 will allow the plot to generate successfully.

Expected results

a plot of the most strongly connected sensors

Actual results

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /opt/mne/connectivity/examples/sensor_connectivity.py:59
     54 con = spectral_connectivity_epochs(
     55     epochs, method='coh', mode='multitaper', sfreq=sfreq, fmin=fmin, fmax=fmax,
     56     faverage=True, tmin=tmin, mt_adaptive=False, n_jobs=1)
     58 # Now, visualize the connectivity in 3D:
---> 59 plot_sensors_connectivity(
     60     epochs.info,
     61     con.get_data(output='dense')[:, :, 0])

File /opt/mne/connectivity/mne_connectivity/viz/_3d.py:90, in plot_sensors_connectivity(info, con, picks, cbar_label, n_con, cmap)
     87 con_val = np.array(con_val)
     89 # Show the connections as tubes between sensors
---> 90 vmax = np.max(con_val)
     91 vmin = np.min(con_val)
     92 for val, nodes in zip(con_val, con_nodes):

File <__array_function__ internals>:180, in amax(*args, **kwargs)

File /opt/mambaforge/envs/mnedev/lib/python3.10/site-packages/numpy/core/fromnumeric.py:2793, in amax(a, axis, out, keepdims, initial, where)
   2677 @array_function_dispatch(_amax_dispatcher)
   2678 def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   2679          where=np._NoValue):
   2680     """
   2681     Return the maximum of an array or maximum along an axis.
   2682 
   (...)
   2791     5
   2792     """
-> 2793     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
   2794                           keepdims=keepdims, initial=initial, where=where)

File /opt/mambaforge/envs/mnedev/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

Additional information

Possible fixes (or fix-like changes):

  1. filter the connections based on the 5cm threshold before reducing to the n_con strongest ones
  2. add the threshold to the function signature so users can be more lenient if they want to
  3. at a minimum, anticipate the error and raise a more informative warning.

crossref: https://mne.discourse.group/t/problem-with-different-methods-in-sensor-connectivity-example/7548/4

cc @olafhauk and @dasdiptyajit

drammock avatar Sep 22 '23 19:09 drammock