Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot_sensors_connectivity fails when strongest conns are spatially close #148

Open
drammock opened this issue Sep 22, 2023 · 0 comments
Open

Comments

@drammock
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant