Skip to content

Commit

Permalink
Merge pull request #269 from unfoldtoolbox/test
Browse files Browse the repository at this point in the history
topoplot up
  • Loading branch information
vladdez authored Nov 20, 2024
2 parents 3adc8d2 + 0779924 commit 88c52e5
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnfoldMakie"
uuid = "69a5ce3b-64fb-4f22-ae69-36dd4416af2a"
authors = ["Vladimir Mikheev", "Daniel Baumgartner", "Sören Döring", "Niklas Gärtner", "Furkan Lokman", "Benedikt Ehinger"]
version = "0.5.9"
version = "0.5.10"

[deps]
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
Expand Down Expand Up @@ -44,7 +44,7 @@ CoordinateTransformations = "0.6"
DataFrames = "1"
DataStructures = "0.18"
DocStringExtensions = "0.9"
GeometryBasics = "0.4"
GeometryBasics = "0.4, 0.5"
GridLayoutBase = "0.9, 0.10, 0.11"
ImageFiltering = "0.7"
Interpolations = "0.14, 0.15"
Expand Down
54 changes: 33 additions & 21 deletions docs/literate/tutorials/topoplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,39 @@ Example: `plot_topoplot(...; visual=(; label_text = true))`
Example: `plot_topoplot(...; visual=(; label_scatter = true))`
=#
f = Figure(size = (500, 500))
labs4 = ["O1", "F2", "F3", "P4"]
plot_topoplot!(
f[1, 1],
dat[1:4, 340, 1];
positions = positions[1:4],
visual = (; label_scatter = false),
labels = labs4,
axis = (; title = "no channel scatter"),
)

plot_topoplot!(
f[1, 2],
dat[1:4, 340, 1];
positions = positions[1:4],
visual = (; label_text = true, label_scatter = (markersize = 15, strokewidth = 2)),
labels = labs4,
axis = (; title = "channel scatter with text"),
mapping = (; labels = labs4),
)
f
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])
Expand Down
27 changes: 27 additions & 0 deletions docs/literate/tutorials/topoplotseries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,33 @@ plot_topoplotseries!(
)
f


# ## Channel labels

# Use `visual` to specify channel labelss and channels markers. `visual.label_text = true` makes channel names visible.

begin
f = Figure()
df_col = UnfoldMakie.eeg_array_to_dataframe(data[1:4, :, 1], string.(1:4))
labs4 = ["s1", "s2", "s3", "s4"]
plot_topoplotseries!(
f[1, 1:5],
df_col;
bin_num = 2,
positions = positions[4:7],
labels = labs4,
visual = (;
label_scatter = (
markersize = 15,
color = "white",
strokecolor = "green",
strokewidth = 2,
),
label_text = true,
),
)
f
end
# # Configurations of Topoplot series

#=
Expand Down
11 changes: 5 additions & 6 deletions src/eeg_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function eeg_topoplot_series!(
topo_axis = (;),
topo_attributes = (;),
positions,
labels = nothing,
)
topo_axis = update_axis(supportive_defaults(:topo_default_series); topo_axis...)

Expand Down Expand Up @@ -175,24 +176,22 @@ function eeg_topoplot_series!(
if isempty(to_value(single_y)) # exits the loop if there is no data for a new topoplot.
break
end

ax = Axis(
fig[:, :][r, c];
topo_axis...,
xlabel = label_management(cat_or_cont_columns, df_single, col),
)
# select labels
labels = to_value(df_single)[:, label]
# select data

# select data
topo_attributes = scatter_management(
single_y,
topo_attributes,
highlight_scatter,
interactive_scatter,
)
single_topoplot =
eeg_topoplot!(ax, to_value(single_y), labels; positions, topo_attributes...)
eeg_topoplot!(ax, to_value(single_y); positions, labels, topo_attributes...)

if rasterize_heatmaps
single_topoplot.plots[1].plots[1].rasterize = true
end
Expand Down Expand Up @@ -227,7 +226,7 @@ function topoplot_subselection(data_mean, col, row, select_col, select_row, r, c
return df_single
end

function scatter_management( # should be chekec and simplified
function scatter_management( # should be cheked and simplified
single_y,
topo_attributes,
highlight_scatter,
Expand Down
8 changes: 4 additions & 4 deletions src/plot_topoplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ function plot_topoplot!(
clist = [:gray, high_color][Int.(x .+ 1)] #color for highlighting
eeg_topoplot!(
inner_axis,
data,
labels;
data;
labels = labels,
positions,
config.visual...,
topo_attributes...,
Expand All @@ -92,8 +92,8 @@ function plot_topoplot!(
else
eeg_topoplot!(
inner_axis,
data,
labels;
data;
labels = labels,
positions,
config.visual...,
topo_attributes...,
Expand Down
17 changes: 12 additions & 5 deletions src/plot_topoplotseries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ Multiple miniature topoplots in regular distances.
This is typically what you want, otherwise you get ~128x128 vectors per topoplot, which makes everything very slow.
- `col_labels::Bool`, `row_labels::Bool = true`\\
Shows column and row labels for categorical values.
- `labels::Vector{String} = nothing`\\
Show labels for each electrode.
- `positions::Vector{Point{2, Float32}} = nothing`\\
Specify channel positions. Requires the list of x and y positions for all unique electrode.
- `labels::Vector{String} = nothing`\\
Show labels for each electrode.
- `interactive_scatter = nothing`\\
Enable interactive mode.\\
If you create `obs_tuple = Observable((0, 0, 0))` and pass it into `interactive_scatter` you can update the observable tuple with the indices of the clicked topoplot markers.\\
Expand All @@ -48,7 +48,6 @@ Multiple miniature topoplots in regular distances.
Here you can flexibly change configurations of the topoplot interoplation.\\
To see all options just type `?Topoplot.topoplot` in REPL.\\
Defaults: $(supportive_defaults(:topo_default_attributes))
Code description:
-
$(_docstring(:topoplotseries))
Expand All @@ -64,8 +63,8 @@ function plot_topoplotseries!(
bin_width = nothing,
bin_num = nothing,
positions = nothing,
labels = nothing,
nrows = 1,
labels = nothing, # rename to channel_labels?
combinefun = mean,
col_labels = false,
row_labels = true,
Expand All @@ -90,6 +89,11 @@ function plot_topoplotseries!(
config.visual...,
)

if !isnothing(labels) && length(labels) != length(positions)
error(
"The length of `labels` differs from the length of `position`. Please make sure they are the same length.",
)
end
# resolve columns with data
config.mapping = resolve_mappings(to_value(data), config.mapping)
data_copy = deepcopy(to_value(data)) # deepcopy prevents overwriting initial data
Expand Down Expand Up @@ -125,7 +129,8 @@ function plot_topoplotseries!(
axis = (; xlabel = string(config.mapping.col)),
)
config_kwargs!(config; kwargs...) #add the user specified once more, just if someone specifies the xlabel manually
# overkll as we would only need to check the xlabel ;)
# overkill as we would only need to check the xlabel ;)

ftopo, axlist, colorrange = eeg_topoplot_series!(
f,
data_row;
Expand All @@ -142,6 +147,7 @@ function plot_topoplotseries!(
topo_axis = topo_axis,
topo_attributes = topo_attributes,
positions,
labels,
)
config_kwargs!(
config,
Expand All @@ -156,6 +162,7 @@ function plot_topoplotseries!(
if config.layout.use_colorbar == true
Colorbar(f[1, 2]; colormap = config.visual.colormap, config.colorbar...)
end

apply_layout_settings!(config; fig = f, ax = ax)
return f
end
Expand Down
2 changes: 1 addition & 1 deletion src/plotconfig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function PlotConfig(T::Val{:topoplot})
contours = (color = :white, linewidth = 2),
enlarge = 1,
label_scatter = true,
label_text = true,
label_text = false,
bounding_geometry = Circle,
colormap = Reverse(:RdBu),
),
Expand Down
9 changes: 8 additions & 1 deletion test/test_butterfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,17 @@ end
)
end


#TO DO
# not working
#= @testset "butterfly: with two size highlighted channels" begin
df.highlight = in.(df.channel, Ref([10, 12]))
plot_butterfly(df; positions = pos, mapping = (; linesize = :highlight))
end =#

@testset "butterfly: topoplot channel names" begin
plot_butterfly(
df;
positions = pos,
topo_attributes = (; label_text = true, label_scatter = (; markersize = 10)),
)
end
13 changes: 11 additions & 2 deletions test/test_topoplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ end

@testset "topoplot: labels" begin
labels = ["s$i" for i = 1:size(dat[:, 150, 1], 1)]
plot_topoplot(dat[:, 150, 1], positions = positions; labels = labels)
plot_topoplot(
dat[:, 150, 1],
positions = positions;
labels = labels,
visual = (; label_text = true),
)
end

@testset "topoplot: GridSubposition" begin
Expand All @@ -64,7 +69,11 @@ end
end

@testset "topoplot: positions through labels" begin
plot_topoplot(dat[1:19, 50, 1]; labels = TopoPlots.CHANNELS_10_20)
plot_topoplot(
dat[1:19, 50, 1];
labels = TopoPlots.CHANNELS_10_20,
visual = (; label_text = true),
)
end

@testset "topoplot: change interpolation" begin
Expand Down
46 changes: 43 additions & 3 deletions test/test_toposeries1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,49 @@ end
)
end

@testset "toposeries: channel names" begin
plot_topoplotseries(df; bin_width = 80, positions = positions, labels = raw_ch_names)
end # doesnt work rn
@testset "toposeries: channel names true" begin
plot_topoplotseries(
df;
bin_width = 80,
nrows = 3,
positions = positions,
visual = (; label_text = true),
)
end

@testset "toposeries: channel names given by user" begin
df_30 = UnfoldMakie.eeg_array_to_dataframe(dat[1:30, :, 1], string.(1:30))

plot_topoplotseries(
df_30;
bin_width = 80,
nrows = 3,
positions = positions[1:30],
labels = raw_ch_names,
visual = (; label_text = true),
)
end

@testset "error checking: different length of channel names gand positions" begin
err1 = nothing
t() = error(
plot_topoplotseries(
df;
bin_width = 80,
nrows = 3,
positions = positions,
labels = raw_ch_names,
visual = (; label_text = true),
),
)
try
t()
catch err1
end
@test err1 == ErrorException(
"The length of `labels` differs from the length of `position`. Please make sure they are the same length.",
)
end

@testset "toposeries: xlabel" begin
f = Figure()
Expand Down

0 comments on commit 88c52e5

Please sign in to comment.