### Install Required Python Packages Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb Installs essential Python libraries for the tutorial, including nilearn, mne, and mne_bids. This command uses pip for package management and is suitable for environments where direct package installation is permitted. Ensure you have pip installed. ```python !pip install nilearn mne mne_bids ``` -------------------------------- ### Install Dependencies for NLP Tutorial Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/03-features Installs necessary Python packages for natural language processing tasks, including PyTorch, transformers, spaCy, and scikit-learn. This is essential for running the subsequent code in the tutorial. ```shell # only run this cell in colab !pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124 !pip install accelerate transformers spacy scikit-learn ``` -------------------------------- ### Download and Install spaCy English Model Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/03-features This code snippet shows how to download and install the 'en_core-web-sm' model for spaCy. This model is necessary for enabling spaCy's English language processing capabilities, including part-of-speech tagging and dependency parsing. ```bash !python -m spacy download en_core-web-sm ``` -------------------------------- ### Install Required Libraries with Pip Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Installs the nilearn, mne, and mne_bids Python libraries. These are essential dependencies for processing and analyzing ECoG data within this tutorial. Ensure you have pip installed and a compatible Python environment. ```shell !pip install nilearn mne mne_bids ``` -------------------------------- ### Install MNE and MNE-BIDS Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/02-preprocessing Installs the necessary Python libraries, MNE and MNE-BIDS, for working with electrophysiological data and BIDS datasets. This is a prerequisite for the subsequent steps in the preprocessing pipeline. ```bash !pip install mne mne_bids ``` -------------------------------- ### Download ECoG Data File using Wget Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Downloads a specific ECoG data file from an S3 URL using the `wget` command. The `-nc` flag ensures that the file is only downloaded if it does not already exist. This is used to obtain the preprocessed high-gamma data file for the tutorial. ```shell !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$file_path ``` -------------------------------- ### Install MNE and MNE-BIDS Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/02-preprocessing.ipynb Installs the necessary Python libraries, MNE and MNE-BIDS, required for ECoG data processing. This command is typically run in a Python environment or notebook. ```python !pip install mne mne_bids ``` -------------------------------- ### Install Project Dependencies Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/04-encoding Installs necessary Python packages including PyTorch, MNE, Himalaya, scikit-learn, pandas, matplotlib, and nilearn. This is crucial for setting up the environment to run the tutorial's code. ```shell # only run this cell in colab !pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124 !pip install mne mne_bids himalaya scikit-learn pandas matplotlib nilearn ``` -------------------------------- ### Create Events for Word Onsets Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/04-encoding Creates an events array from the word start times. Each event is represented by its sample index, a zero channel, and an event ID (which is not explicitly set here, defaulting to 0). The start times (in seconds) are converted to sample indices by multiplying with the sampling frequency of the raw ECoG data. The shape of the resulting events array is printed. ```python events = np.zeros((len(df_word), 3), dtype=int) events[:, 0] = (df_word.start * raw.info['sfreq']).astype(int) print(f"Events array shape: {events.shape}") ``` -------------------------------- ### Install Dependencies - Python Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/05-atlases Installs necessary Python libraries for the tutorial, including mne, mne_bids, scipy, matplotlib, and nilearn. This step is crucial for ensuring all functionalities are available. ```python # only run this cell in colab !pip install mne mne_bids scipy matplotlib nilearn ``` -------------------------------- ### Construct and Print ECoG Data File Path Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Defines the file path for ECoG data using the BIDSPath class, specifying the root directory, subject, task, data type, description, suffix, and extension. It then prints the constructed path to verify its correctness. This method helps in programmatically accessing specific derivative files within a BIDS-compliant dataset. ```python file_path = BIDSPath(root="derivatives/ecogprep", subject="02", task="podcast", datatype="ieeg", description="highgamma", suffix="ieeg", extension="fif") print(f"File path within the dataset: {file_path}") ``` -------------------------------- ### MultipleKernelRidgeCV Configuration Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/06-banded-ridge.ipynb Demonstrates the setup for MultipleKernelRidgeCV, a cross-validation model for ridge regression with multiple kernels. It's configured with KFold cross-validation and precomputed kernels, suitable for advanced regression tasks. ```python MultipleKernelRidgeCV(cv=KFold(n_splits=5, random_state=None, shuffle=False), kernels='precomputed') ``` -------------------------------- ### Download and Load Electrode Data Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/05-atlases.ipynb Downloads an example Electrocorticography (ECoG) data file (`sub-03_task-podcast_desc-highgamma_ieeg.fif`) from an S3 bucket using mne. This file contains raw electrophysiological data including electrode positions. ```python # only run this cell in colab import pooch data_path = pooch.retrieve( url='https://s3.amazonaws.com/openneuro.org/ds005574/derivatives/ecogprep/sub-03/ieeg/sub-03_task-podcast_desc-highgamma_ieeg.fif', known_hash='d1d4d2a78406a18c75672a0833c29c1a89b41f3a5e22469e5e43779436e8f4d5', fname='sub-03_task-podcast_desc-highgamma_ieeg.fif', processor=pooch.பதி_file) raw = mne.io.read_raw_fif(data_path, preload=True) ``` -------------------------------- ### JavaScript: Interactive Data Visualization Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/05-atlases.ipynb This snippet provides a conceptual example of using JavaScript for interactive data visualization on a web page. It implies the use of a library like D3.js or Chart.js to render charts and handle user interactions. Requires a browser environment and potentially a charting library. ```javascript // Example using a hypothetical charting library // const chartData = [{ x: 1, y: 10 }, { x: 2, y: 15 }]; // const chart = new Chart("myCanvas", { type: "line", data: chartData }); ``` -------------------------------- ### Importing Plotting Function from Nilearn (Python) Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb This snippet imports the `plot_markers` function from the `nilearn.plotting` module. This function is essential for visualizing electrode locations on a brain surface, a key step in neuroimaging analysis. ```python from nilearn.plotting import plot_markers ``` -------------------------------- ### Import MNE and BIDSPath Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/02-preprocessing Imports the core MNE library for electrophysiological data analysis and the BIDSPath utility from MNE-BIDS for handling dataset organization. These are essential for loading and managing BIDS-compliant data. ```python import mne from mne_bids import BIDSPath ``` -------------------------------- ### Load Transcript Data with Pandas Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/03-features Loads transcript data into a pandas DataFrame. The transcript is expected to contain columns for words along with their start and end timestamps. This step is crucial for preparing the data for feature extraction. ```python bids_root = "" # if using a local dataset, set this variable accordingly ``` -------------------------------- ### Plot Channel Coordinates on Brain - Python Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb This code snippet visualizes the processed channel coordinates on a brain representation. It utilizes the Nilearn library for plotting, which expects coordinates in meters. The output is a projector object, indicating the successful generation of the plot. ```python from nilearn import plotting plotting.plot_points_on_brain(coords_are_brains=coords, **plotting_kwargs) ``` -------------------------------- ### Load and Inspect Transcript Data Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/03-features This code loads a transcript file into a pandas DataFrame and displays the first 10 rows. It assumes the transcript is in CSV format and requires the pandas library. The output shows columns like 'word', 'start', and 'end' times. ```python df = pd.read_csv(transcript_path) df.head(10) ``` -------------------------------- ### Define and Download ECoG Data Path Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/02-preprocessing.ipynb Constructs a BIDSPath object to specify the location and metadata of an ECoG data file. It includes functionality to download the data using wget if a local `bids_root` is not specified, often used in environments like Google Colab. ```python bids_root = "" # if using a local dataset, set this variable accordingly file_path = BIDSPath(root=bids_root, subject="01", task="podcast", datatype="ieeg", suffix="ieeg", extension=".edf") print(f"File path within the dataset: {file_path}") # You only need to run this if using Colab (i.e. if you did not set bids_root to a local directory) if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$file_path file_path = file_path.basename ``` -------------------------------- ### Extract ECoG Channel Names - JavaScript Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb This JavaScript code snippet demonstrates how to extract and display ECoG channel names from an alert box. It is triggered by a button click and displays a list of ECoG channels. This is useful for verifying channel labels in an ECoG experiment. ```javascript alert('Good ECoG:\n\nPO1, PO2, PO3, PO4, PO5, PO6, O1, O2, O3, O4, SFP1, SFP2, SFP3, SFP4, SFP5, LF1, LF2, LF3, LF4, LF5, LF6, LF7, LF8, PF2, PF3, PF4, PF5, PF6, MF1, MF2, MF3, MF4, MF5, MF6, MF7, MF8, IFP1, IFP2, IFP3, IFP4, IFP6, DI1, DI2, DI3, DI4, DI5, DI6, DI7, DI8, DI9, DI10, DLT1, DLT2, DLT3, DLT4, DLT5, DLT6, DLT7, DLT8, DLT9, DLT10, DMP1, DMP2, DMP3, DMP4, DMP5, DMP6, DMP7, DMF1, DMF2, DMF3, DMF4, DMF5, DMF6, DMF7, DMF8, DMF9, DMF10, DIF1, DIF2, DIF3, DIF4, DIF5, DIF6, DIF7, DIF8, DIF9, DIF10, DIF11, DIF12') ``` -------------------------------- ### Load Pretrained Causal LM and Display Config (Python) Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/03-features Loads a pretrained causal language model (like GPT-2) and prints its configuration details, including the number of layers, embedding dimension, and other model parameters. The model is then set to evaluation mode and moved to the appropriate device (e.g., GPU). Dependencies include the `transformers` library. ```python from transformers import AutoModelForCausalLM import torch # Assume modelname and device are defined elsewhere, e.g., modelname = "gpt2", device = "cuda" print("Loading model...") model = AutoModelForCausalLM.from_pretrained(modelname) print( f"Model : {modelname}" f"\nLayers: {model.config.num_hidden_layers}" f"\nEmbDim: {model.config.hidden_size}" f"\nConfig: {model.config}" ) model = model.eval() model = model.to(device) ``` -------------------------------- ### Download and Load Contextual Embeddings Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/04-encoding Downloads GPT-2 contextual word embeddings from an S3 bucket if not locally available, and then loads them using h5py. It prints the shape of the loaded embedding matrix. ```python bids_root = "" # if using a local dataset, set this variable accordingly # Download the transcript, if required embedding_path = f"{bids_root}stimuli/gpt2-xl/features.hdf5" if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$embedding_path embedding_path = "features.hdf5" print(f"Using embedding file path: {embedding_path}") ``` ```python modelname, layer = 'gpt2-xl', 24 with h5py.File(embedding_path, "r") as f: contextual_embeddings = f[f"layer-{layer}"][...] print(f"LLM embedding matrix has shape: {contextual_embeddings.shape}") ``` -------------------------------- ### Plot Markers with Coordinates and Values in Python Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb Generates a plot of markers at specified coordinates, with sizes and colors determined by input values. This function is useful for visualizing spatial data, such as sensor locations or experimental points. It supports various display modes and colormap customization. ```python import numpy as np # Assuming plot_markers function is available (e.g., from MNE-Python or similar) # from mne.viz import plot_markers # Placeholder for the actual plot_markers function definition if not imported def plot_markers(values, coords, node_size, display_mode, alpha, node_cmap, colorbar, node_vmin, node_vmax): # This is a mock implementation for demonstration purposes. # In a real scenario, this would call a plotting library function. print(f"Plotting {len(values)} markers with node_size={node_size}, display_mode='{display_mode}', alpha={alpha}, node_cmap='{node_cmap}', colorbar={colorbar}, node_vmin={node_vmin}, node_vmax={node_vmax}") print("
") # Mock output return "mock_figure_object" # Example Usage: coords = np.random.rand(10, 3) # Example coordinates (10 points, 3D) values = np.ones(len(coords)) # Example values (all ones) fig = plot_markers(values, coords, node_size=30, display_mode='lzr', alpha=1, node_cmap='Grays', colorbar=False, node_vmin=0, node_vmax=1) print(fig) ``` -------------------------------- ### Text Output from spaCy Processing Pipeline Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/03-features.ipynb This snippet displays the text output from a spaCy processing pipeline, likely from a pandas DataFrame. It shows tokenized words, their start and end character offsets, part-of-speech tags, dependency relations, and whether they are considered stop words. ```text word_idx word start end token pos dep stop 0 0 Act 3.710 3.790 Act NNP ROOT False 1 1 one, 3.990 4.190 one CD nummod True 2 1 one, 3.990 4.190 , , punct False 3 2 monkey 4.651 4.931 monkey NN appos False 4 3 in 4.951 5.011 in IN prep True 5 4 the 5.051 5.111 the DT det True 6 5 middle. 5.151 5.391 middle NN pobj False 7 5 middle. 5.151 5.391 . . punct False 8 6 So 6.592 6.732 So RB advmod True 9 7 there's 6.752 6.912 there EX expl True ``` -------------------------------- ### Setup MultipleKernelRidgeCV Model with Cross-Validation Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/06-banded-ridge Configures a MultipleKernelRidgeCV model for encoding analysis. It specifies precomputed kernels, uses a random search solver with defined iterations and alpha values, and sets up 5-fold inner cross-validation. This model is designed for scenarios with multiple interacting kernels. ```python n_iter = 20 alphas = np.logspace(1, 10, 10) # specify alpha values inner_cv = KFold(n_splits=5, shuffle=False) # inner 5-fold cross-validation setup solver_params = dict(n_iter=n_iter, alphas=alphas) mkr_model = MultipleKernelRidgeCV(kernels="precomputed", solver='random_search', solver_params=solver_params, cv=inner_cv) mkr_model ``` -------------------------------- ### Import Necessary Libraries for Data Handling Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb Imports core Python libraries used for neuroimaging data analysis. This includes `mne` for electrophysiological data processing, `numpy` for numerical operations, and `mne_bids` for handling data structured according to the Brain Imaging Data Structure (BIDS). ```python import mne import numpy as np from mne_bids import BIDSPath ``` -------------------------------- ### Load Preprocessed ECoG Data with MNE Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/04-encoding Loads preprocessed high-gamma ECoG data from a BIDS-compliant dataset using the MNE library. It specifies the file path using BIDSPath, reads the raw FIF file, picks specific channels using a regular expression, and returns the raw MNE object. This is the initial step for analyzing neural responses to speech. ```python file_path = BIDSPath(root=f"{bids_root}derivatives/ecogprep", subject="03", task="podcast", datatype="ieeg", description="highgamma", suffix="ieeg", extension=".fif") print(f"File path within the dataset: {file_path}") # You only need to run this if using Colab (i.e. if you did not set bids_root to a local directory) if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$file_path file_path = file_path.basename raw = mne.io.read_raw_fif(file_path, verbose=False) picks = mne.pick_channels_regexp(raw.ch_names, "LG[AB]*") raw = raw.pick(picks) raw ``` -------------------------------- ### Download and Load Podcast Transcript with Pandas Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/03-features This snippet demonstrates how to download a podcast transcript if it's not already present and then load it into a pandas DataFrame for initial inspection. It handles potential file existence and uses `wget` for downloading. ```python transcript_path = f"{bids_root}stimuli/podcast_transcript.csv" if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$transcript_path transcript_path = "podcast_transcript.csv" df = pd.read_csv(transcript_path) df.head(10) ``` -------------------------------- ### Prepare Electrode Coordinates for Plotting with Nilearn Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Extracts 3D coordinates for each ECoG electrode from the MNE Raw object's info. It converts these coordinates into a NumPy matrix suitable for nilearn's plotting functions, scaling them to meters. This step is crucial for visualizing electrode positions on a brain. ```python ch2loc = {ch['ch_name']: ch['loc'][:3] for ch in raw.info['chs']} coords = np.vstack([ch2loc[ch] for ch in raw.info['ch_names']]) coords *= 1000 # nilearn likes to plot in meters, not mm print("Coordinate matrix shape: ", coords.shape) ``` -------------------------------- ### Access and Print First ECoG Channel Name and Metadata with MNE Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Accesses and prints the name and metadata of the first ECoG channel from the loaded raw data. It specifically retrieves the channel name using raw.info.ch_names and detailed metadata, including location, for the first channel from raw.info['chs']. ```python print("First channel name is:", raw.info.ch_names[0]) print("Metadata associated with the first channel:") raw.info['chs'][0] ``` -------------------------------- ### Extract and Print ECoG Data Array with MNE Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Retrieves the actual electrophysiological data from an MNE Raw object using the get_data() method. It prints the type and shape of the resulting NumPy array, showing the number of channels and the total number of samples. This allows for direct numerical analysis. ```python data = raw.get_data() print(f"Data is a {type(data)} object and has a shape of: {data.shape}") ``` -------------------------------- ### Load and Prepare ECoG Data using MNE-Python Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/06-banded-ridge This snippet demonstrates loading raw ECoG data from a FIF file using MNE-Python, selecting specific channels based on a regular expression, and visualizing the raw data. It assumes the file path is correctly set and MNE-Python is installed. ```python if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$file_path file_path = file_path.basename raw = mne.io.read_raw_fif(file_path, verbose=False) picks = mne.pick_channels_regexp(raw.ch_names, "LG[AB]*") raw = raw.pick(picks) raw ``` -------------------------------- ### Load and Inspect Raw ECoG Data with MNE Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Loads raw ECoG data from a FIF file using MNE's read_raw_fif function. It then displays metadata including duration, sampling frequency, channel count, and filter details. This is a foundational step for analyzing ECoG data. ```python raw = mne.io.read_raw_fif(file_path.basename, verbose=False) raw ``` -------------------------------- ### Display Raw IEEG Data Information - Python Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb This snippet displays information about raw IEEG data read from a FIF file. It shows the file name, dimensions (channels x time points), duration, approximate size, and whether the data is loaded. Dependencies include the MNE-Python library. ```python import mne # Assuming file_path is defined and points to a .fif file # file_path = mne.io.open_info(file_path.basename) raw = mne.io.read_raw_fif(file_path.basename, verbose=False) raw ``` -------------------------------- ### Import ECoG Libraries and Define BIDS Path Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/00-intro Imports the mne, numpy, and BIDSPath from mne_bids libraries. BIDSPath is used to construct file paths according to the Brain Imaging Data Structure (BIDS) conventions, facilitating organized data access. This is crucial for locating specific ECoG data files. ```python import mne import numpy as np from mne_bids import BIDSPath ``` -------------------------------- ### Download Stimuli Transcripts Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/04-encoding Downloads the transcript file associated with the contextual word embeddings from an S3 bucket if it's not found locally. This transcript contains word, token, onset, and offset information. ```python # Download the transcript, if required transcript_path = f"{bids_root}stimuli/gpt2-xl/transcript.tsv" if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$transcript_path transcript_path = "transcript.tsv" ``` -------------------------------- ### HTML: Table for Acquisition Experimental Data Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/_sources/notebooks/00-intro.ipynb This HTML snippet defines a table row for displaying acquisition-related data, specifically the 'Duration' of the recording. It is part of a larger table structure, indicated by the class `repr-element` and a unique identifier associated with the 'Acquisition' section. The duration is presented in HH:MM:SS format. ```HTML Acquisition Duration 00:29:60 (HH:MM:SS) ``` -------------------------------- ### Download and Load GPT-2 Embeddings Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/06-banded-ridge Downloads GPT-2 contextual word embeddings from a specified S3 bucket if they are not found locally. It then loads the embeddings from an HDF5 file and prints the shape of the resulting matrix. ```python bids_root = "" # if using a local dataset, set this variable accordingly # Download the transcript, if required embedding_path = f"{bids_root}stimuli/gpt2-xl/features.hdf5" if not len(bids_root): !wget -nc https://s3.amazonaws.com/openneuro.org/ds005574/$embedding_path embedding_path = "features.hdf5" print(f"Using embedding file path: {embedding_path}") ``` -------------------------------- ### Import Core Libraries Source: https://hassonlab.github.io/podcast-ecog-tutorials/html/notebooks/04-encoding Imports essential Python libraries for data manipulation, machine learning, plotting, and neuroimaging analysis. These include mne, h5py, torch, numpy, pandas, matplotlib, nilearn, BIDSPath, Himalaya components, and scikit-learn modules. ```python import mne import h5py import torch import numpy as np import pandas as pd import matplotlib.pyplot as plt from nilearn.plotting import plot_markers from mne_bids import BIDSPath from himalaya.backend import set_backend, get_backend from himalaya.ridge import RidgeCV from himalaya.scoring import correlation_score from sklearn.model_selection import KFold from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler ```