3D Head Visualizations

3D topographic head map

The 3D topographic head map provides a view of voltage measurements as a heatmap imposed on a 3D skull shape. It can be generated as an animation to view changes over time or as a standalone plot.

General Setup

Import required modules

from simpl_eeg import topomap_3d_head, eeg_objects

Create epoched data

For additional options see Creating EEG Objects section.

experiment_folder = "../../data/927"
nth_epoch = 0

epochs = eeg_objects.Epochs(experiment_folder)
epoch = epochs.get_epoch(nth_epoch)
Reading /Users/mpin/Documents/MDS/capstone/simpl_eeg_capstone/data/927/fixica.fdt
/Users/mpin/Documents/MDS/capstone/simpl_eeg_capstone/simpl_eeg/eeg_objects.py:199: RuntimeWarning: Data file name in EEG.data (927 fix ica correct.fdt) is incorrect, the file name must have changed on disk, using the correct file name (fixica.fdt).
  raw = mne.io.read_raw_eeglab(data_path)
loaded raw from ../../data/927/fixica.set
Not setting metadata
Not setting metadata
33 matching events found
Setting baseline interval to [-0.2998046875, 0.0] sec
Applying baseline correction (mode: mean)
0 projection items activated
Loading data for 33 events and 2049 original time points ...
0 bad epochs dropped

Create a 3D topographic animation

Define parameters

A detailed description of all parameters can be found in the topomap_3d_head.animate_3d_head docstring:

help(topomap_3d_head.animate_3d_head)
Help on function animate_3d_head in module simpl_eeg.topomap_3d_head:

animate_3d_head(epoch, data_df=None, plot_title='', color_title='EEG MicroVolt', vmin=-50, vmax=50, colormap='Bluered')
df = None 
vmin = -40
vmax = 40
colormap = "RdBu_r"
time_stamp = -300

Generating the animation

topo_3d_head = topomap_3d_head.animate_3d_head(
    epoch,
    data_df = df, # if you want to animate a data frame not values of epoch, you would still need to input the epoch for node locations
    color_title="EEG MicroVolt",
    vmin=vmin,
    vmax=vmax,
    colormap=colormap,
)
topo_3d_head.show()

Saving the animation

Save as html

html_file_path = "examples/topo_3d.html"
topo_3d_head.write_html(html_file_path)

Save as gif

topomap_3d_head.save_gif(epoch, gifname="topo_3d_head_ani", duration=200)

Save as mp4

Note

You need to save the file as gif first and then convert it into mp4 file.

import moviepy.editor as mp

clip = mp.VideoFileClip("topo_3d_head_ani.gif")
clip.write_videofile("examples/topo_3d_head_ani.mp4")

Create a 3D topographic plot

Define parameters

A detailed description of all animation parameters can be found in the topomap_3d_head.topo_3d_map docstring:

help(topomap_3d_head.topo_3d_map)
Help on function topo_3d_map in module simpl_eeg.topomap_3d_head:

topo_3d_map(epoch, time_stamp, data_df=None, color_title='EEG MicroVolt', vmin=-50, vmax=50, colormap='Bluered')
    Plot a topographic map in a 3D head shape for a single time stamp
    
    Parameters:
        epoch: mne.epochs.Epochs
            An epoched file for the EEGLab data
        time_stamp: int
            The time stamp that is of interest if using epoched
            data, the row number that is of interest if using
            data frame
        data_df: pd.core.frame.DataFrame
            A data frame of EEG data if epoched data is not of
            interest
        color_title: str (optional)
            The title of the color bar. Defaults to "EEG MicroVolt".
        vmin: int (optional)
            The minimum EEG voltage value to be shown on the color bar.
            Defaults to -50.
        vmax: int (optional)
            The maximum EEG voltage value to be shown on the color bar.
            Defaults to 50.
        colormap: str (optional)
            The colour scheme to use. Defaults to Bluered.
    
    Returns:
        plotly.graph_objs._figure.Figure
            A topographic map in a 3D head shape
df = None 
vmin = -40
vmax = 40
colormap = "RdBu_r"
time_stamp = -300

Generating a standalone plot

topo_3d__head_static = topomap_3d_head.topo_3d_map(
    epoch,
    time_stamp,
    data_df = df,
    color_title="EEG MicroVolt",
    vmin=vmin,
    vmax=vmax,
    colormap=colormap,
)
topo_3d__head_static.show()

Saving the plot

You can change the file to different formats by changing the format argument in the function. It supports png, pdf, svg.

Save as svg

static_file_path = "examples/topo_3d_static.svg"
topo_3d__head_static.write_image(static_file_path, engine="kaleido")

Save as png

static_file_path_png = "examples/topo_3d_static.png"

# no need to specify engine if not saving as svg file
topo_3d__head_static.write_image(static_file_path_png)