Plotting activity on the brain

This is a quick demo of a function to plot electrode activity on the brain.

In [1]:
# To make sure moviepy loads
from imageio.plugins import ffmpeg

# This is because I'm lazy and haven't made a pip package yet...
import sys

# Now regular imports
import ecogtools as et
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mne
%matplotlib inline

/Users/choldgraf/anaconda/lib/python2.7/site-packages/sklearn/ DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
/Users/choldgraf/anaconda/lib/python2.7/site-packages/matplotlib/ UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

In [2]:
melec = pd.read_csv('../data/2d_brain/meta_elec.csv')
im = plt.imread('../data/2d_brain/brain.png')
x, y = melec[['x_2d', 'y_2d']].values.T

In [3]:
fig, ax = plt.subplots()

First simulate some activity

In [8]:
sfreq_activity = 100
duration = 5
activity = np.random.randn(melec.shape[0], sfreq_activity * duration)
time = np.arange(activity.shape[-1]) / float(sfreq_activity)

# Add a random burst of activity
ixs_activity = np.array([sfreq_activity * 2.5, sfreq_activity * 1.], dtype=int)
dur_activity = .5
for ix in ixs_activity:
    activity[:, ix:ix + int(dur_activity * sfreq_activity)] = 3
activity = mne.filter.filter_data(activity, sfreq_activity, None, 5)

In [9]:
# Activity is of shape (n_channels, n_times)

# Here's what the activity looks like over time
fig, ax = plt.subplots()
_ = ax.plot(time, activity.T)
_ = ax.set(xlabel='Time (s)')

(32, 500)

We can display a single frame of activity like this

In [10]:
et.plot_activity_on_brain(x, y, activity[:, 0], im,
                          smin=10, smax=100, vmin=-1, vmax=1)

<matplotlib.axes._subplots.AxesSubplot at 0x12e114250>

Alternatively, we can create a movie if we pass a 2-D array as activity

In [11]:
clip = et.plot_activity_on_brain(x, y, activity, im,
                                 smax=100, vmin=-3, vmax=3)
# This is now a moviepy video clip

< instance at 0x12e0cac20>

In [12]:

100%|█████████▉| 500/501 [00:06<00:00, 60.20it/s]     | 2/501 [00:00<00:29, 17.04it/s]