Giter Club home page Giter Club logo

mne-rsa's Introduction

Representational Similarity Analysis

unit_tests build_docs doi

This is a Python package for performing representational similarity analysis (RSA) using MNE-Python data structures. The RSA is computed using a “searchlight” approach.

Read more on RSA in the paper that introduced the technique:

Nikolaus Kriegeskorte, Marieke Mur and Peter Bandettini (2008). Representational similarity analysis - connecting the branches of systems neuroscience. Frontiers in Systems Neuroscience, 2(4). https://doi.org/10.3389/neuro.06.004.2008

https://raw.githubusercontent.com/wmvanvliet/mne-rsa/main/doc/rsa.png

Installation

The package can be installed either through PIP: pip install mne-rsa or through conda using the conda-forge channel: conda install -c conda-forge mne-rsa

Use cases

This is what the package can do for you:

  • Compute RDMs on arbitrary data
  • Compute RDMs in a searchlight across:
    • vertices/voxels and samples (source level)
    • sensors and samples (sensor level)
    • vertices/voxels only (source level)
    • sensors only (sensor level)
    • samples only (source and sensor level)
  • Use cross-validated distance metrics when computing RDMs
  • And of course: compute RSA between RDMs

Supported metrics for comparing RDMs:

  • Spearman correlation (the default)
  • Pearson correlation
  • Kendall’s Tau-A
  • Linear regression (when comparing multiple RDMs at once)
  • Partial correlation (when comparing multiple RDMs at once)

Juicy bits of the API

compute_rdm(data, metric='correlation', **kwargs)

rsa_stcs(stcs, rdm_model, src, spatial_radius=0.04, temporal_radius=0.1,
         stc_rdm_metric='correlation', stc_rdm_params=dict(),
         rsa_metric='spearman', y=None, n_folds=1, sel_vertices=None,
         tmin=None, tmax=None, n_jobs=1, verbose=False)

rsa_evokeds(evokeds, rdm_model, noise_cov=None, spatial_radius=0.04,
            temporal_radius=0.1, evoked_rdm_metric='correlation',
            evoked_rdm_params=dict(), rsa_metric='spearman', y=None,
            n_folds=1, picks=None, tmin=None, tmax=None, n_jobs=1,
            verbose=False)

rsa_epochs(epochs, rdm_model, noise_cov=None, spatial_radius=0.04,
           temporal_radius=0.1, epochs_rdm_metric='correlation',
           epochs_rdm_params=dict(), rsa_metric='spearman', y=None,
           n_folds=1, picks=None, tmin=None, tmax=None, n_jobs=1,
           verbose=False)

rsa_nifti(image, rdm_model, spatial_radius=0.01,
          image_rdm_metric='correlation', image_rdm_params=dict(),
          rsa_metric='spearman', y=None, n_folds=1, roi_mask=None,
          brain_mask=None, n_jobs=1, verbose=False)

Example usage

Basic example on the EEG “kiloword” data:

import mne
import rsa
data_path = mne.datasets.kiloword.data_path(verbose=True)
epochs = mne.read_epochs(data_path + '/kword_metadata-epo.fif')
# Compute the model RDM using all word properties
rdm_model = rsa.compute_rdm(epochs.metadata.iloc[:, 1:].values)
evoked_rsa = rsa.rsa_epochs(epochs, rdm_model,
                            spatial_radius=0.04, temporal_radius=0.01,
                            verbose=True)

Documentation

For quick guides on how to do specific things, see the examples.

Finally, there is the API reference documentation.

Integration with other packages

I mainly wrote this package to perform RSA analysis on MEG data. Hence, integration functions with MNE-Python are provided. There is also some integration with nipy for fMRI.

Performance

This package aims to be fast and memory efficient. An important design feature is that under the hood, everything operates on generators. The searchlight routines produce a generator of RDMs which are consumed by a generator of RSA values. Parallel processing is also supported, so you can use all of your CPU cores.

Development

Here is how to set up the package as a developer:

git clone [email protected]:wmvanvliet/mne-rsa.git
cd mne-rsa
python setup.py develop --user
.. toctree::
   :hidden:

   Examples <auto_examples/index>
   API Reference <api>

mne-rsa's People

Contributors

hoechenberger avatar sappelhoff avatar tshimizu97 avatar wmvanvliet avatar yuan-fang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mne-rsa's Issues

Mistake with the version number?

Hello @wmvanvliet, the latest release is tagged as version 0.91 … I don't think this was intended, and you were planning to do 0.9.1 instead? If yes, … you may want to kill that tag and pull the package off of PyPI
Or simply make the next release a 1.0 :D

Info (62) and data (1) must have same number of channels.

I planned to compute RSA in a searchlight pattern on evokeds with the function rsa_evokeds

So I have a list of evoked object for each item. Data of each evoked object is an array of 62 channels * 450 timepoints. Since I have 120 items in my data, the length of the evoked list is 120. And I have a model DSM of 120*120.

However, when performing RSA, I got an error:

'ValueError: Info (62) and data (1) must have same number of channels.'

This is my script:

######################################################################################################
epochlist = []

subs = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]

for j in subs:

  sub_epoch = all_epochs[j-1].copy()
  
  epochlist.append(sub_epoch)

epoch_concat = mne.concatenate_epochs(epochlist)

evoked_list = []

for n in items['itemid_s']:

  item_epoch = epoch_concat[epoch_concat.metadata['itemid']==n]
  
  item_evoke = item_epoch.average()
  
  evoked_list.append(item_evoke)

rsa_result = rsa.rsa_evokeds(
evoked_list,
phonology_dist[np.triu_indices(120,k=1)],
evoked_dsm_metric='sqeuclidean',
rsa_metric='kendall-tau-a',
spatial_radius=0.45,
temporal_radius=0.02,
tmin=0.15, tmax=0.25,
n_jobs=3,
n_folds=None,
verbose=False)

######################################################################################################

Could you help me on this error? Many thanks~

image

rsa_epochs on single sensor without spatial searchlight

Hi!

I am currently trying to analyze some data of mine using the mne_rsa package. I am analyzing iEEG data and therefore, we are mainly doing single sensor analysis at the moment, without spatial searchlight that is.

In order to understand how to do so, I have played around with the tutorial found here: https://users.aalto.fi/~vanvlm1/mne-rsa/auto_examples/plot_sensor_level.html#sphx-glr-auto-examples-plot-sensor-level-py

I have noticed that when setting the spatial_radius to None, as described in the API documentation, like here:

rsa_result = mne_rsa.rsa_epochs(
    epochs,                           # The EEG data
    dsm_vis,                          # The model DSM
    epochs_dsm_metric='sqeuclidean',  # Metric to compute the EEG DSMs
    rsa_metric='kendall-tau-a',       # Metric to compare model and EEG DSMs
    spatial_radius=None,                # Spatial radius of the searchlight patch
    temporal_radius=0.05,             # Temporal radius of the searchlight path
    tmin=0.15, tmax=0.25,             # To save time, only analyze this time interval
    n_jobs=1,                         # Only use one CPU core. Increase this for more speed.
    n_folds=None,
    verbose=False
    picks="Fz")                    # Set to True to display a progress bar

I get the error:

Traceback (most recent call last):
  File "C:\Users\alexander.lepauvre\Anaconda3\envs\mne\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-e87dfff10dfc>", line 1, in <module>
    rsa_result = mne_rsa.rsa_epochs(
  File "C:\Users\alexander.lepauvre\Anaconda3\envs\mne\lib\site-packages\mne_rsa\sensor_level.py", line 380, in rsa_epochs
    return mne.EvokedArray(data, info, tmin, comment='RSA',
  File "<decorator-gen-203>", line 24, in __init__
  File "C:\Users\alexander.lepauvre\Anaconda3\envs\mne\lib\site-packages\mne\evoked.py", line 856, in __init__
    raise ValueError('Data must be a 2D array of shape (n_channels, '
ValueError: Data must be a 2D array of shape (n_channels, n_samples), got shape (12,)

I am guessing that this is not the intended behavior right? I have tried to look at the code, as the error mentions this lies in the data being fed into the evoked ,mne function has wrong dimensions, but I wasn't able to fix it. I have however noticed that when replacing the None by a zero it works, though I am not sure the same happens as with None.

How to reproduce:

import mne
import mne_rsa

data_path = mne.datasets.kiloword.data_path(verbose=True)
epochs = mne.read_epochs(data_path + '/kword_metadata-epo.fif')
epochs = epochs.resample(100)

epochs.metadata.sample(10)

dsm_vis = mne_rsa.compute_dsm(epochs.metadata[['NumberOfLetters']],
                              metric='euclidean')
mne_rsa.plot_dsms(dsm_vis)

rsa_result = mne_rsa.rsa_epochs(
    epochs,                           # The EEG data
    dsm_vis,                          # The model DSM
    epochs_dsm_metric='sqeuclidean',  # Metric to compute the EEG DSMs
    rsa_metric='kendall-tau-a',       # Metric to compare model and EEG DSMs
    spatial_radius=None,                # Spatial radius of the searchlight patch
    temporal_radius=0.05,             # Temporal radius of the searchlight path
    tmin=0.15, tmax=0.25,             # To save time, only analyze this time interval
    n_jobs=1,                         # Only use one CPU core. Increase this for more speed.
    n_folds=None,
    verbose=False)                    # Set to True to display a progress bar

Thanks in advances for your help,

Kind regards,

Alex

Doesn't build on Windows

setup.py doesn't run on Windows:

  Traceback (most recent call last):
    File "<string>", line 2, in <module>
    File "<pip-setuptools-caller>", line 34, in <module>
    File "C:\bld\mne-rsa_1646066101996\work\setup.py", line 21, in <module>
      long_description = fid.read()
    File "C:\bld\mne-rsa_1646066101996\_h_env\lib\encodings\cp1252.py", line 23, in decode
      return codecs.charmap_decode(input,self.errors,decoding_table)[0]
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 704: character maps to <undefined>

The reason is that you don't specify an encoding when loading README.rst, so Windows defaults to cp1252:

mne-rsa/setup.py

Lines 20 to 21 in d375f21

with open('README.rst', 'r') as fid:
long_description = fid.read()

Question about setting the spatial radius

Hello,

Thank you for developing this library. I am considering using it to analyze MEG data (recorded using the MEGIN system).

I see in the API that the spatial radius should be set in meters.
"spatial_radius floats | None
The spatial radius of the searchlight patch in meters. All sensors within this radius will belong to the searchlight patch. Set to None to only perform the searchlight over time, flattening across sensors. Defaults to 0.04."

But later in one the example Plot 'sensor-level DSMs' the spatial radius is set to 45 (I am guessing this is not 45 meters)
"# This will create a generator for the DSMs
dsms = mne_rsa.dsm_epochs(
epochs, # The EEG data
dist_metric='correlation', # Metric to compute the EEG DSMs
spatial_radius=45, # Spatial radius of the searchlight patch
temporal_radius=None, # Perform only spatial searchlight
tmin=0.15, tmax=0.25, # To save time, only analyze this time interval
)"

I am not sure how to set the spatial radius for the MEGIN system. Is there a way to see which sensors are included within each searchlight?

Many thanks in advance for any information and tips in using the library!

Ana P

ModuleNotFoundError: No module named 'mne_rsa'

Hi! I'm so exciting to found this wonderful package, but after I installed mne-rsa via "pip install mne-rsa", it showed that
Requirement already satisfied: mne-rsa in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (0.4)
Requirement already satisfied: matplotlib in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (3.3.2)
Requirement already satisfied: mne in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (0.22.dev0)
Requirement already satisfied: numpy in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (1.19.4)
Requirement already satisfied: scipy in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (1.5.2)
Requirement already satisfied: cycler>=0.10 in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from matplotlib->mne-rsa) (0.10.0)
Requirement already satisfied: pillow>=6.2.0 in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from matplotlib->mne-rsa) (8.0.1)
Requirement already satisfied: python-dateutil>=2.1 in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from matplotlib->mne-rsa) (2.8.1)
Requirement already satisfied: certifi>=2020.06.20 in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from matplotlib->mne-rsa) (2020.12.5)
Requirement already satisfied: numpy in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (1.19.4)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from matplotlib->mne-rsa) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from matplotlib->mne-rsa) (1.3.0)
Requirement already satisfied: six in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from cycler>=0.10->matplotlib->mne-rsa) (1.15.0)
Requirement already satisfied: scipy in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (1.5.2)
Requirement already satisfied: numpy in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (1.19.4)
Requirement already satisfied: six in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from cycler>=0.10->matplotlib->mne-rsa) (1.15.0)
Requirement already satisfied: numpy in c:\program files (x86)\microsoft visual studio\shared\anaconda3_64\envs\mne\lib\site-packages (from mne-rsa) (1.19.4)
But, I cannot import this package no matter I typied "import rsa" or import "import mne_rsa", there always was prompt showing that ModuleNotFoundError: No module named 'rsa' or ModuleNotFoundError: No module named 'mne_rsa'. But when I install other packages like mne-bids or picard, it worked fine.
Could you please tell me what can I do to solve this problem? I appreciate very much! Thank you!

issue with sliding time window

Hi, I think I found a small bug when trying to run "sliding time-window" style RSA with spatial_radius=None.

Error:

File "/opt/anaconda3/lib/python3.6/site-packages/mne/evoked.py", line 705, in init
raise ValueError('Data must be a 2D array of shape (n_channels, '

ValueError: Data must be a 2D array of shape (n_channels, n_samples)

Cause:

This is caused by the fact that mne.EvokedArray expects (chan x times) array and lets (time, n)

Fix:
Just making it a 1 x list here fixes it:

if spatial_radius is not None:
    info = epochs.info
    info = mne.pick_info(info, picks)
else:
    info = mne.create_info(['rsa'], epochs.info['sfreq'])
    **data=[data]**#!!! Here

ImportError: dlopen: cannot load any more object with static TLS

Hi!
I installed mne-rsa using pip install mne-rsa in CentOS release 6.5 (Final). However, when I import mne_rsa in ipython, there is an error message: ImportError: dlopen: cannot load any more object with static TLS. I encountered this error before, at that time I only need to import mne_rsa before MNE and this error will disappear, but now it didn't work.
Here is the specific error information:
I'm really confused how to solve this:

In [2]: import mne_rsa
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 import mne_rsa

File ~/.conda/envs/mne/lib/python3.9/site-packages/mne_rsa/__init__.py:2, in <module>
      1 __version__ = '0.6'
----> 2 from .source_level import rsa_stcs, dsm_stcs, rsa_nifti, dsm_nifti
      3 from .sensor_level import rsa_evokeds, rsa_epochs, dsm_evokeds, dsm_epochs
      4 from .searchlight import searchlight

File ~/.conda/envs/mne/lib/python3.9/site-packages/mne_rsa/source_level.py:25, in <module>
     22 from scipy.linalg import block_diag
     23 import nibabel as nib
---> 25 from .dsm import _n_items_from_dsm, dsm_array
     26 from .rsa import rsa_array
     27 from .searchlight import searchlight

File ~/.conda/envs/mne/lib/python3.9/site-packages/mne_rsa/dsm.py:9, in <module>
      6 import numpy as np
      7 from scipy.spatial import distance
----> 9 from .folds import _create_folds
     10 from .searchlight import searchlight
     13 def compute_dsm(data, metric='correlation', **kwargs):

File ~/.conda/envs/mne/lib/python3.9/site-packages/mne_rsa/folds.py:3, in <module>
      1 from mne.utils import logger
      2 import numpy as np
----> 3 from sklearn.model_selection import StratifiedKFold
      4 from sklearn.preprocessing import OneHotEncoder
      7 def _create_folds(X, y, n_folds=None):

File ~/.conda/envs/mne/lib/python3.9/site-packages/sklearn/__init__.py:83, in <module>
     81     from . import __check_build  # noqa: F401
     82     from .base import clone
---> 83     from .utils._show_versions import show_versions
     85     __all__ = [
     86         "calibration",
     87         "cluster",
   (...)
    128         "show_versions",
    129     ]
    132 def setup_module(module):

File ~/.conda/envs/mne/lib/python3.9/site-packages/sklearn/utils/_show_versions.py:12, in <module>
      9 import sys
     10 import importlib
---> 12 from ._openmp_helpers import _openmp_parallelism_enabled
     15 def _get_sys_info():
     16     """System information
     17 
     18     Returns
   (...)
     22 
     23     """

ImportError: dlopen: cannot load any more object with static TLS

BTW, my sklearn version is 1.0.2, do I need to downgrade the sklearn? if so, what version should I install?
Best

ValueError: Info (61) and data (1) must have same number of channels.

rsa_result = mne_rsa.rsa_evokeds(
list_evoked,
dsm_model=RDM_model_fuyin_xw_1d,
rsa_metric='spearman',
spatial_radius= 0.04,
temporal_radius=0.05,
tmin=0.4, tmax=0.5,
n_jobs=6,
n_folds=None,
verbose=True)

when I run the code, I got this error:
ValueError: Info (61) and data (1) must have same number of channels.

I debug the function and find out there maybe a mistake in the source code of .rsa_evokeds:


    if one_model:
        return mne.EvokedArray(np.atleast_2d(data[..., 0]), info, tmin,
                               comment='RSA', nave=len(evokeds))

I think it should be like:


    if one_model:
        return mne.EvokedArray(np.atleast_2d(data), info, tmin,
                               comment='RSA', nave=len(evokeds))

some methods questions & combining efforts?

Hi @wmvanvliet,

I meant to contact you for a while regarding this cool project of your's. I'm not sure if the issue format
is the best to discuss my questions, but as it's completely open it's of course a favourable one.

I would like to ask two methods related questions:

  1. Are you thinking about supporting multivariate noise normalization during computation of the RDMs as outlined and implemented here & here?
  2. Is anything planned regarding significance testing (e.g. permutation and/or bootstrap tests)?

Something a little off, but over at urial I played a bit around with RSA focusing fMRI, as I needed it for parts of my PhD. As you mentioned contributions regarding fMRI are welcome, I wondered if it might be worth talking about combining our efforts?

It would be cool to hear your thoughts on any of the topics I mentioned.

Cheers, Peer

support RSA on time-frequency data type

Hi,

As we discussed in MEGNord 2023, it would be great for mne-rsa to support the time-frequency data type in MNE Python. Specifically, the AverageTFR and EpochsTFR object at sensor level. For the source level, I think currently MNE python does not have a TF object.

Weiyong

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.