Topoplot
Topoplot (aka topography plot) is a plot type for visualisation of EEG activity in a specific time stemp or time interval. It can fully represent channel and channel location dimensions using contour lines.
The topoplot is a 2D projection and interpolation of the 3D distributed sensor activity. The name stems from physical geography, but instead of height, the contour lines represent voltage levels.
Setup
Package loading
using Unfold
using UnfoldMakie
using DataFrames
using CairoMakie
using TopoPlots
using DataFrames
Data loading
dat, positions = TopoPlots.example_data();
The size of data
is 64×400×3. This means:
- 64 channels;
- 400 timepoints in range from -0.3 to 0.5 mseconds;
- Estimates of 3 averaging functions. Instead of displaying the EEG data for all subjects, here we aggregate the data using (1) mean, (2) standard deviation and (3) p-value within t-tests.
While position
consist of 64 x and y coordinates of each channels on a scalp.
Plot Topoplots
Here we select a time point in 340 msec and the mean estimate.
plot_topoplot(dat[1:4, 340, 1]; positions = positions[1:4])
df = DataFrame(:estimate => dat[:, 340, 1])
plot_topoplot(df; positions = positions)
Setting sensor positions
The plot_topoplot()
needs the sensor positions to be specified. There are several ways to do this:
- Specify positions directly:
plot_topoplot(...; positions=[...])
- Specify the sensor labels:
plot_topoplot(...; labels=[...])
To get the positions from the labels we use a database.
Column Mappings for Topoplots
When using topoplots with a DataFrame
as input, the library needs to know the names of the columns used for plotting. This is specified using the mapping=(;)
kwargs.
While there are several default values that will be checked in order if they exist in the DataFrame
, a custom name may need to be chosen:
Note that only one of positions
or labels
needs to be set to draw a topoplot. If both are set, positions takes precedence, labels can be used to label electrodes in TopoPlots.jl.
The default columns of mapping could be seen usign this code:
configs_default = UnfoldMakie.PlotConfig()
configs_default.mapping.y
(:estimate, :yhat, :y)
Labelling
label_text
draws labels next to their positions.
Example: plot_topoplot(...; visual=(; label_text = true))
label_scatter (boolean)
draws the markers at the given positions.
Example: plot_topoplot(...; visual=(; label_scatter = true))
begin
f = Figure(size = (500, 500))
labs4 = ["s1", "s2", "s3", "s4"]
plot_topoplot!(
f[1, 1],
dat[1:4, 340, 1];
positions = positions[1:4],
visual = (; label_scatter = false),
labels = labs4,
axis = (; xlabel = "", title = "No markers"),
colorbar = (; height = 100,),
)
plot_topoplot!(
f[2, 1],
dat[1:4, 340, 1];
positions = positions[1:4],
visual = (;
label_text = true,
label_scatter = (
markersize = 15,
color = "white",
strokecolor = "green",
strokewidth = 2,
),
),
labels = labs4,
axis = (; xlabel = "340 ms", title = "Markers with channel labels"),
mapping = (; labels = labs4),
colorbar = (; height = 100),
)
f
end
Highlighting channels
plot_topoplot(dat[:, 50, 1]; positions, high_chan = [1, 2])
Horizontal colorbars
Just switch colorbar.vertical
to false
plot_topoplot(
dat[:, 50, 1];
positions,
axis = (; xlabel = "50 ms"),
colorbar = (; vertical = false, width = 180, label = "Voltage estimate"),
)
Configurations of Topoplot
UnfoldMakie.plot_topoplot
— Functionplot_topoplot!(f::Union{GridPosition, GridLayout, Figure}, data::Union{<:Observable{<:DataFrame},<:AbstractDataFrame,<:AbstractVector}; positions::Vector, labels = nothing, kwargs...)
using Interpolations: positions plot_topoplot(data::Union{<:Observable{<:DataFrame},<:AbstractDataFrame,<:AbstractVector}; position::Vector, labels = nothing, kwargs...)
Plot a topoplot.
Arguments
f::Union{GridPosition, GridLayout, Figure}
Figure
,GridLayout
, orGridPosition
to draw the plot.data::Union{<:Observable{<:DataFrame},<:AbstractDataFrame,<:AbstractVector}
Data for the plot visualization.positions::Vector{Point{2, Float32}}
Positions used ifdata
is not aDataFrame
. Positions are generated fromlabels
ifpositions = nothing
.labels::Vector{String} = nothing
Labels used ifdata
is not a DataFrame.high_chan = nothing
- channnel(s) to highlight by color.high_color = :darkgreen
- color for highlighting.topo_axis::NamedTuple = (;)
Here you can flexibly change configurations of the topoplot axis.
To see all options just type?Axis
in REPL.
Defaults: (width = Relative(1.0f0), height = Relative(1.0f0), halign = 0.05, valign = 0.95, aspect = DataAspect())topo_attributes::NamedTuple = (;)
Here you can flexibly change configurations of the topoplot interoplation.
To see all options just type?Topoplot.topoplot
in REPL.
Defaults: interp_resolution = (128, 128), interpolation = CloughTocher()
Shared plot configuration options
The shared plot options can be used as follows: type = (; key = value, ...))
.
For example, plot_x(...; colorbar = (; vertical = true, label = "Test"))
.
Multiple defaults will be cycled until match.
Placing ;
is important!
figure = NamedTuple() - use kwargs...
of Makie.Figure
axis = (xlabel = "Time", aspect = Makie.DataAspect()) - use kwargs...
of Makie.Axis
layout = (show_legend = true, use_colorbar = true, hidespines = (), hidedecorations = Dict{Symbol, Bool}(:label => 0)) - check this page
mapping = (x = nothing, y = (:estimate, :yhat, :y), positions = (:pos, :positions, :position, nothing), labels = (:labels, :label, :sensor, nothing)) - use any mapping from AlgebraOfGraphics
visual = (colormap = Makie.Reverse{Symbol}(:RdBu), contours = (color = :white, linewidth = 2), enlarge = 1, label_scatter = true, label_text = false, bounding_geometry = GeometryBasics.Circle) - use kwargs...
of Topoplot.eeg_topoplot
legend = (orientation = :vertical, tellwidth = true, tellheight = false, halign = :right, valign = :center) - use kwargs...
of Makie.Legend
colorbar = (vertical = true, tellwidth = true, tellheight = false, labelrotation = -1.5707963267948966, flipaxis = true, label = "Voltage") - use kwargs...
of Makie.Colorbar
Return Value: Figure
displaying the Topoplot.
This page was generated using Literate.jl.