mne-tools / mne-connectivity

Connectivity algorithms that leverage the MNE-Python API.
https://mne.tools/mne-connectivity/dev/index.html
BSD 3-Clause "New" or "Revised" License
68 stars 34 forks source link

Has the method for computing coherence been changed? #83

Closed fishbacp closed 2 years ago

fishbacp commented 2 years ago

The program I'm working on required me to update to mne 1.0dev0. Module not found errors resulted for mne-bids, mne-connectivity, and mne-hfo, which I had used previously with no issues, so I updated them to their development versions as well:

mne 1.0.dev0 mne-bids 0.10.dev0 mne-connectivity 0.3.dev0 mne-hfo 0.3.dev0

Previously I had computed coherence using something along the lines of the following, where data was my data epoch having size 1-by-n_channels -by - n_times,. The duration is very short due to stationarity assumptions, usually about .5 seconds.

from mne_connectivity import spectral_connectivity
S,freqs,times,n_epochs,n_tapers=spectral_connectivity(data,sfreq=500, method='coh', mode='multitaper', fmin=5, fmax=20, faverage=True )

My coherence matrix was then S[:,:,0] which had dimensions n_channels by n_channels since I had averaged over the frequency band.

Now I can import mne_connectivity okay, but I receive an error message stating I can't import spectral_connectivity from mne_connectivity.

The mne_connectivity directory contains a script, spectral.py, where the CohEst class is defined. Should I instantiate this class to get my coherence values? If so, what does con_idx denote? Or should I be using SpectralConnectivity as defined at https://mne.tools/mne-connectivity/stable/generated/mne_connectivity.SpectralConnectivity.html#mne_connectivity.SpectralConnectivity ?

It's not clear how I should now be computing coherence.

fishbacp commented 2 years ago

I believe I've tracked down the source of the error. In my main code, when I try to import spectral_connectivity, I obtain the following:

    from mne_connectivity import spectral_connectivity
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/__init__.py", line 11, in <module>
    from .base import (Connectivity, EpochConnectivity, EpochSpectralConnectivity,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/base.py", line 10, in <module>
    from mne_connectivity.utils import fill_doc
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/utils/__init__.py", line 1, in <module>
    from .docs import fill_doc
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/utils/docs.py", line 7, in <module>
    from mne.externals.doccer import indentcount_lines
ModuleNotFoundError: No module named 'mne.externals'

I've poured over the mne files on github and can't find mne.externals.

adam2392 commented 2 years ago

spectral_connectivity was renamed on main branch for upcoming v0.3 to make it more explicit that it was operating over epochs and does not preserve epochs usually (i.e. some sort of averaging over epochs occurs).

See: https://mne.tools/mne-connectivity/dev/whats_new.html

fishbacp commented 2 years ago

So is my understanding correct that I'll be using spectral_connectivity_epochs from mne_connectivity as opposed to spectral_connectivity in 0.3.dev0?

I double checked that I have 0.3dev0 installed by typing pip3 list at the terminal.

However, now typing import mne_connectivity leads to the following:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import mne_connectivity
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/__init__.py", line 11, in <module>
    from .base import (Connectivity, EpochConnectivity, EpochSpectralConnectivity,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/base.py", line 11, in <module>
    from mne_connectivity.utils import (
ImportError: cannot import name 'fill_doc' from 'mne_connectivity.utils' (/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne_connectivity/utils.py)

Which version of mne should I be working with? Currently I have mne 1.0.dev0 due to a bug I posted last week. Should I go back and install mne .24?

adam2392 commented 2 years ago

v0.3dev should work with mne 1.0dev because the CIs explicitly check this.

Not sure why that error is coming up since the codebase shows fill_doc is fine. Probably something to do w/ your virtual environment I suspect.

fishbacp commented 2 years ago

I'm doing all of this on a Mac running OS 11.6.1. I have a Windows 10 vm using vmWare Fusion but am not using it for any of this.

fishbacp commented 2 years ago

I compared how I have things set up on my Mac as opposed to my Windows VM. Perhaps you could clarify whether I've put things in the correct locations.

Mac: simply typed this at the terminal:

pip3 install mne-connectivity==.3

In the folder where my packages are located, I see mne_connectivity listed on its own. Not surprisingly, because of its location, import mne_connectivity results in the error above.

Windows VM: connectivity exists as a folder in mne. I used

from mne.connectivity import spectral_connectivity,

which worked fine. Would it work for me to download a mne_connectivity v0.3 .zip to mne and then run the setup?

adam2392 commented 2 years ago

V0.3 is not released on PIP yet. So you're just installing v0.2. Which is probably leading to your error. See installation instructions.

As for locations of files, no correct answer but most likely there's recommended ways on StackOverflow or Python official page.

fishbacp commented 2 years ago

It could be that I'm not correctly remembering how I did the installation. I did type pip3 list and got the following for my mne-related packages versions:

mne 1.0.dev0 mne-bids 0.10.dev0 mne-connectivity 0.3.dev0 mne-hfo 0.3.dev0

When you refer to the .3dev0 installation instructions, I explored the link you provided above, and all I could find regarding the development version was the following:

pip3 install --user -U https://api.github.com/repos/mne-tools/mne-connectivity/zipball/master

This led to the error

  ERROR: HTTP error 404 while getting https://api.github.com/repos/mne-tools/mne-connectivity/zipball/master
ERROR: Could not install requirement https://api.github.com/repos/mne-tools/mne-connectivity/zipball/master because of HTTP error 404 Client Error: Not Found for url: https://codeload.github.com/mne-tools/mne-connectivity/legacy.zip/master for URL https://api.github.com/repos/mne-tools/mne-connectivity/zipball/master

I'm very sorry for creating such an inconvenience.

larsoner commented 2 years ago

You need main instead of master at the end there

fishbacp commented 2 years ago

That was it!

from mne_connectivity import spectral_connectivity_epochs

now works fine.

Thank you so very, very much!

adam2392 commented 2 years ago

I'll need to update the docs for that.

adam2392 commented 2 years ago

Updated the docs in #86