Git Product home page Git Product logo

stepeter / naturalistic_arm_movements_ecog Goto Github PK

View Code? Open in Web Editor NEW
1.0 6.0 10.0 8 MB

Analysis of human behavioral and neural variability during naturalistic arm movements. Replicates the findings in our preprint: https://www.biorxiv.org/content/10.1101/2020.04.17.047357v2

License: MIT License

Jupyter Notebook 98.91% Python 0.98% MATLAB 0.11%
naturalistic-arm-movements ecog regression-model spectrograms

naturalistic_arm_movements_ecog's Introduction

Behavioral and neural variability of naturalistic arm movements

This repository contains scripts used to analyze the behavioral and neural variability of 12 human subjects as they performed spontaneous arm movements across multiple days. Neural data was monitored intracranially using electrocorticography (ECoG); wrist trajectories extracted from simultaneously recorded video was used to generate behavioral data. This repository is meant to be used with our published dataset to replicate our analysis and generate most of the figures from our preprint. Such analyses include ECoG spectral power computation and projection to pre-defined regions of interest, comparison of naturalistic reach data to cued hand clench data, and regression of spectral power on behavioral and environmental features.

Getting started

This code was primarily run using Python 3.6.8, with one optional part requiring Matlab 2018b. Note that Matlab and Python code are separated into separate folders. Analysis is heavily reliant on MNE for data analysis and saving. Some specific package versions used:

  • Python: Matplotlib 3.0.2, MNE 0.19.0, Nilearn 0.5.0, Numpy 1.15.4, Pandas 0.25.3

  • Matlab: EEGLAB 13.5.4b, Measure Projection Toolbox 1.661

Analysis steps

All Python scripts can be found in compute_power_gen_figs_python. This folder includes Jupyter notebooks to generate plots, scripts to perform longer analyses such as computing spectral power and fitting regression models, and utility scripts. There is also a config file that contains many parameters that may be shared by multiple scripts or do not need to be changed.

Step 1: Computing spectral power (compute_spectral_power.py)

This script will compute spectral power via Morlet wavelet transform for ECoG segments aligned to movement onset events (FIF files, which are of type mne.Epochs). Requires inputs for the load and save directories. This process takes several hours.

python compute_spectral_power.py -eclp <load_path> -sp <save_path>

Step 2: Visualize behavioral and environmental features used for regression analysis (Behavioral_features_Fig2.ipynb)

Here, we extract behavioral and environmental metadata based on the time when each reach began (day of recording, time of day), how the contralateral wrist moved during the reach (reach duration, magnitude, angle, and onset speed), whether people were speaking during movement initiation (speech ratio), and how much both wrists moved during each movement (bimanual ratio, overlap, and class). These 10 features are later used to model changes in neural spectral power via multiple linear regression. This notebook, however, plots the normalized distributions of these 10 features for each subject. Be sure to set tfr_lp to be the directory with the spectral power data (TFR files). These files include the behavioral feature data in their metadata attribute.

(Optional) Step 3: Create projection matrices (project_ecog_to_rois.m)

To visualize the spectral power of multiple subjects, we project the power across electrodes into pre-defined regions of interest using the Measure Projection Toolbox. The projection matrices are included with the dataset, but can be generated using the Matlab code in project_data_rois_matlab. First, open set_environment_paths.m and update the path names. Note that other versions of EEGLAB may cause errors.

Next, run project_ecog_to_rois.m, setting save_proj_matrix and save_plots to 1 to save the results. The plot will show all regions of interest on the left hemisphere, with the 8 regions of interests we used shown in color.

Step 4: Visualize projected spectral power (Projected_spectral_power_Figs3_4.ipynb)

This notebook will plot projected power averaged across subjects for all 8 regions, along with projected power at 1 region showing each individual subject. Be sure to specify the directories where the spectral power results (TFR files) are saved using tfr_lp and where the projection matrices (CSV files) are saved using roi_proj_loadpath.

Step 5: Plot inter-event variability in low/high-frequency band spectral power (Plot_banded_power_Fig5.ipynb)

This notebook generates Fig. 5 from the paper, which shows the event-by-event variability in spectral power for each subject, separated across recording days. Be sure to set the pathnames for tfr_lp and roi_proj_loadpath.

Step 6: Fit regression models (fit_regression_models.py)

We now want to see how well the behavioral metadata from the previous step explains changes in the neural activity during movement initiation. This script performs separate multiple regression models for each ECoG recording electrode, performing several random train/test splits to minimize bias. For each fit model, the R2 score is computed on the withheld test data to measure how well the model generalizes to new data.

Additionally, a delta R2 score is computed for each feature, which involves randomizing that feature’s training data, fitting a new model and computing a new R2 score, and subtracting this new R2 score from the R2 score computed using non-randomized training data. Higher delta R2 scores indicate more explanatory features.

To run this script, just specify the load path (with TFR files) and save path for regression output. The type of regression model can also be specified as linear or rf (random forest). Note that this script will need to run for several hours, especially if the number of permutations is high.

python fit_regression_models.py -tflp <TFR_load_path> -sp <save_path>

Step 7: Visualize regression results (Regression_results_Fig6.ipynb)

This notebook visualizes the R2 scores and coefficients obtained from the previous step. Specify the directories of the regression output (reg_lp) and spectral power files (tfr_lp). In addition, plot_sd_coef allows for plotting the standard deviation (True) or average (False) coefficient value across permutations.

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.