Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erpgrid #92

Merged
merged 36 commits into from
Nov 10, 2023
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3bb1869
Update plot_erp.jl
vladdez Sep 19, 2023
72e6d4b
new tests, combined plot in docs
vladdez Sep 20, 2023
28687f8
del
vladdez Sep 20, 2023
20cfa16
additional docs problems
vladdez Sep 20, 2023
dc7fd3d
little detail
vladdez Sep 20, 2023
fd13482
dead links
vladdez Sep 21, 2023
bc73746
docstring warnings
vladdez Sep 21, 2023
47c4b19
cross-refs again
vladdez Sep 21, 2023
2bbc565
test
vladdez Sep 21, 2023
fa8a8ec
pp_plot fixed
vladdez Sep 21, 2023
28eafd0
docs errors should be resolved
vladdez Sep 21, 2023
39f43d5
Update hide_deco.md
vladdez Sep 21, 2023
ea756cb
colorbar docs
vladdez Sep 29, 2023
b4687d5
liting
vladdez Sep 29, 2023
a236bb0
add Voltage label
vladdez Sep 29, 2023
2a3383d
Merge branch 'main' into erpPlotDocumentation
vladdez Sep 29, 2023
f1ece45
linting + name consistency
vladdez Oct 2, 2023
85f17d6
Merge branch 'erpPlotDocumentation' of https://github.com/unfoldtoolb…
vladdez Oct 2, 2023
8f0f9c0
formatter
vladdez Oct 13, 2023
aaef662
no extra for circular
vladdez Oct 24, 2023
bb67ed3
no extra toposeries
vladdez Oct 24, 2023
534c0bd
test for design matrix and no extra
vladdez Oct 24, 2023
0313cf7
test
vladdez Oct 24, 2023
dd8769e
no extra for erp
vladdez Oct 24, 2023
f8afc64
del extra in erpimage and parplot
vladdez Oct 24, 2023
1dc2b35
g
vladdez Oct 24, 2023
b6ffa34
Merge branch 'main' into erpPlotDocumentation
vladdez Oct 24, 2023
6966866
citability
vladdez Oct 24, 2023
62f2b37
failed, but I will save it
vladdez Oct 27, 2023
8295681
fixed
vladdez Oct 31, 2023
ac6be39
fixed colorbar in toposeries
vladdez Nov 9, 2023
00db349
small test
vladdez Nov 9, 2023
7a8e13a
merge with main
behinger Nov 10, 2023
1c032f1
review of current test
vladdez Nov 10, 2023
e0eb9f9
Merge branch 'main' into erpgrid
vladdez Nov 10, 2023
4d6daa1
del chanNum argument
vladdez Nov 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
no extra for erp
vladdez committed Oct 24, 2023
commit dd8769e709240eabec990b46418c51e37fbb4211
12 changes: 6 additions & 6 deletions docs/src/literate/tutorials/erp.jl
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ results = coeftable(m)
res_effects = effects(Dict(:continuous => -5:0.5:5), m);

# ## Plot the results
plot_erp(results; extra = (:stderror => true,))
plot_erp(results; :stderror => true,)



@@ -48,8 +48,8 @@ plot_erp(results; extra = (:stderror => true,))

# # Configuration for Line Plots

# ## extra
# `plot_erp(...;extra=(;<name>=<value>,...)`.
# ## key values
# `plot_erp(...; <name>=<value>,...)`.
# - categoricalColor (boolean, true) - in case of numeric `:color` column, is color a continuous or categorical variable?
# - categoricalGroup (boolean, true) - in case of numeric `:group` column, treat `:group` as categorical variable by default
# - stderror (boolean, false) - add an error-ribbon based on the `:stderror` column
@@ -60,9 +60,9 @@ plot_erp(results; extra = (:stderror => true,))
plot_erp(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous),
extra = (; showLegend = true, categoricalColor = false, categoricalGroup = true),
legend = (; nbanks = 2),
layout = (; legendPosition = :right),
showLegend = true, categoricalColor = false, categoricalGroup = true,
)


@@ -84,15 +84,15 @@ plot_erp(
# coefname=["(Intercept)","condition: face"] # if coefname not specified, line should be black
# )
#
# plot_erp(results;extra= (;:pvalue=>pvals))
# plot_erp(results; :pvalue=>pvals)
# ### stderror (boolean)
# Indicating whether the plot should show a colored band showing lower and higher estimates based on the stderror.
# Default is `false`.
#
# previously we showed `:stderror`- but low/high is possible as well`
results.se_low = results.estimate .- 0.5
results.se_high = results.estimate .+ 0.15
plot_erp(select(results, Not(:stderror)); extra = (; stderror = true))
plot_erp(select(results, Not(:stderror)); stderror = true)

# !!! note
# as in the above code,`:stderror` has precedence over `:se_low`/`:se_high`
13 changes: 7 additions & 6 deletions docs/src/literate/tutorials/erp.md
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ nothing #hide
## Plot the results

````@example erp
plot_erp(results; extra=(:stderror=>true,))
plot_erp(results; :stderror=>true,)
````

## Column Mappings for Line Plots
@@ -59,8 +59,8 @@ There are multiple default values, that are checked in that order if they exist

# Configuration for Line Plots

## extra
`plot_erp(...;extra=(;<name>=<value>,...)`.
## key variables
`plot_erp(...; <name>=<value>,...)`.
- categoricalColor (boolean, true) - in case of numeric `:color` column, is color a continuous or categorical variable?
- categoricalGroup (boolean, true) - in case of numeric `:group` column, treat `:group` as categorical variable by default
- stderror (boolean, false) - add an error-ribbon based on the `:stderror` column
@@ -72,9 +72,9 @@ Using some general configurations we can pretty up the default visualization. He
plot_erp(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous),
extra = (; showLegend = true, categoricalColor = false, categoricalGroup = true),
legend = (; nbanks = 2),
layout = (; legendPosition = :right),
showLegend = true, categoricalColor = false, categoricalGroup = true,
)
````

@@ -97,7 +97,8 @@ pvals = DataFrame(
# )
````

plot_erp(results;extra= (;:pvalue=>pvals))
plot_erp(results; :pvalue=>pvals)

### stderror (boolean)
Indicating whether the plot should show a colored band showing lower and higher estimates based on the stderror.
Default is `false`.
@@ -107,7 +108,7 @@ previously we showed `:stderror`- but low/high is possible as well`
````@example erp
results.se_low = results.estimate .- 0.5
results.se_high = results.estimate .+ 0.15
plot_erp(select(results, Not(:stderror)); extra= (;stderror=true))
plot_erp(select(results, Not(:stderror)); stderror=true)
````

!!! note
8 changes: 4 additions & 4 deletions docs/src/tutorials/butterfly.md
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ We filter the data to make it more clearly represented:
```@example main
include("../../example_data.jl")
df, pos = example_data("TopoPlots.jl")
first(df,3)
first(df, 3)
```

## Plot Butterfly Plots
@@ -37,7 +37,7 @@ plot_butterfly(df; positions=pos)

## Column Mappings for Butterfly Plots

Since butterfly plots use a `DataFrame` as input, the library needs to know the names of the columns used for plotting. You can set these mapping values by calling `plot_butterfly(...; mapping=(;:x=:time,))`, that is, by specifying a `NamedTuple` (note the `;` right after the opening parentheses).
Since butterfly plots use a `DataFrame` as input, the library needs to know the names of the columns used for plotting. You can set these mapping values by calling `plot_butterfly(...; mapping=(; :x=:time,))`, that is, by specifying a `NamedTuple` (note the `;` right after the opening parentheses).

For more information on mapping values, see the [Mapping Data](@ref config_mapping) section of the documentation.

@@ -56,8 +56,8 @@ Default is `(:labels, :label, :topoLabels, :sensor, :nothing)`

## Configurations for Butterfly Plots

Here we look into possible options for configuring the butterfly plot visualization using `(...; extra=(<name>=<value>, ...)`.
This is the list of unique configuration (extraData):
Here we look into possible options for configuring the butterfly plot visualization using `(...; <name>=<value>, ...)`.
This is the list of unique configuration (key values):

- topoLegend (boolean)

74 changes: 44 additions & 30 deletions src/plot_erp.jl
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ Plot an ERP plot.
- `plotData::DataFrame`: Data for the line plot visualization.
- `kwargs...`: Additional styling behavior. Often used: `plot_erp(df; mapping=(; color=:coefname, col=:conditionA))`

## Extra Data Behavior (...; extra = (; [key]=value)):
## kwargs (...; ...):

- `categoricalColor` (bool, `true`) - Indicates whether the column referenced in mapping.color should be used nonnumerically.
- `categoricalGroup` (bool, `true`) - Indicates whether the column referenced in mapping.group should be used nonnumerically.
@@ -34,7 +34,7 @@ Plot Butterfly

See `plot_erp` for all specifications

## Extra Data Behavior (...; extra=(; [key]=value)):
## kwargs: (...; ...):
`markersize` (Real, `10`) - change the size of the markers, topoplot-inlay electrodes
`topowidth` (Real, `0.25`) - change the size of the inlay topoplot width
`topoheigth` (Real, `0.25`) - change the size of the inlay topoplot height
@@ -43,12 +43,19 @@ See `plot_erp` for all specifications
"""
plot_butterfly(plotData::DataFrame; kwargs...) =
plot_butterfly!(Figure(), plotData; kwargs...)
plot_butterfly!(
f::Union{GridPosition,<:Figure},
plotData::DataFrame;
extra = (;),
kwargs...,
) = plot_erp!(f, plotData, ; extra = merge((; butterfly = true), extra), kwargs...)

plot_butterfly!(f::Union{GridPosition,<:Figure}, plotData::DataFrame; kwargs...) =
plot_erp!(
f,
plotData;
butterfly = true,
topoLegend = true,
markersize = 10,
topowidth = 0.25,
topoheigth = 0.25,
topoPositionToColorFunction = x -> posToColorRomaO(x),
kwargs...,
)



@@ -57,11 +64,21 @@ function plot_erp!(
plotData::DataFrame;
positions = nothing,
labels = nothing,
categoricalColor = true,
categoricalGroup = true,
stderror = false, # XXX if it exists, should be plotted
pvalue = [],
butterfly = false,
topoLegend = nothing,
markersize = nothing,
topowidth = nothing,
topoheigth = nothing,
topoPositionToColorFunction = nothing,
kwargs...,
)
config = PlotConfig(:erp)
config_kwargs!(config; kwargs...)
if config.extra.butterfly
if butterfly
config = PlotConfig(:butterfly)
config_kwargs!(config; kwargs...)
end
@@ -86,35 +103,35 @@ function plot_erp!(
end

# check if stderror values exist and create new collumsn with high and low band
if "stderror" ∈ names(plotData) && config.extra.stderror
if "stderror" ∈ names(plotData) && stderror
plotData.stderror = plotData.stderror .|> a -> isnothing(a) ? 0.0 : a
plotData[!, :se_low] = plotData[:, config.mapping.y] .- plotData.stderror
plotData[!, :se_high] = plotData[:, config.mapping.y] .+ plotData.stderror
end

# Get topocolors for butterfly
if (config.extra.butterfly)
if (butterfly)
if isnothing(positions) && isnothing(labels)
config.extra = merge(config.extra, (; topoLegend = false))
#colors =config.visual.colormap# get(colorschemes[config.visual.colormap],range(0,1,length=nrow(plotData)))
topoLegend = false
#colors = config.visual.colormap# get(colorschemes[config.visual.colormap],range(0,1,length=nrow(plotData)))
colors = nothing
#config.mapping = merge(config.mapping,(;color=config.))
else
allPositions = getTopoPositions(; positions = positions, labels = labels)
colors = getTopoColor(allPositions, config)
colors = getTopoColor(allPositions, topoPositionToColorFunction)
end


end
# Categorical mapping
# convert color column into string, so no wrong grouping happens
if config.extra.categoricalColor && (:color ∈ keys(config.mapping))
if categoricalColor && (:color ∈ keys(config.mapping))
config.mapping =
merge(config.mapping, (; color = config.mapping.color => nonnumeric))
end

# converts group column into string
if config.extra.categoricalGroup && (:group ∈ keys(config.mapping))
if categoricalGroup && (:group ∈ keys(config.mapping))
config.mapping =
merge(config.mapping, (; group = config.mapping.group => nonnumeric))
end
@@ -140,35 +157,35 @@ function plot_erp!(

basic = visual(Lines; config.visual...) * xy_mapp
# add band of sdterrors
if config.extra.stderror
if stderror
m_se = mapping(config.mapping.x, :se_low, :se_high)
basic = basic + visual(Band, alpha = 0.5) * m_se
end

basic = basic * data(plotData)

# add the pvalues
if !isempty(config.extra.pvalue)
if !isempty(pvalue)
basic = basic + addPvalues(plotData, config)
end

plotEquation = basic * mapp

f_grid = f[1, 1]
# butterfly plot is drawn slightly different
if config.extra.butterfly
if butterfly
# add topoLegend

if (config.extra.topoLegend)
if (topoLegend)
topoAxis = Axis(
f_grid,
width = Relative(config.extra.topowidth),
height = Relative(config.extra.topoheigth),
width = Relative(topowidth),
height = Relative(topoheigth),
halign = 0.05,
valign = 0.95,
aspect = 1,
)
topoplotLegend(config, topoAxis, allPositions)
topoplotLegend(topoAxis, markersize, topoPositionToColorFunction, allPositions)
end
# no extra legend
mainAxis = Axis(f_grid; config.axis...)
@@ -218,17 +235,14 @@ function eegHeadMatrix(positions, center, radius)
)
end

function topoplotLegend(config, axis, allPositions)
function topoplotLegend(axis, markersize, topoPositionToColorFunction, allPositions)
allPositions = unique(allPositions)

topoMatrix = eegHeadMatrix(allPositions, (0.5, 0.5), 0.5)

# colorscheme where first entry is 0, and exactly length(positions)+1 entries
specialColors = ColorScheme(
vcat(
RGB(1, 1, 1.0),
[config.extra.topoPositionToColorFunction(pos) for pos in allPositions]...,
),
vcat(RGB(1, 1, 1.0), [topoPositionToColorFunction(pos) for pos in allPositions]...),
)

xlims!(low = -0.2, high = 1.2)
@@ -242,7 +256,7 @@ function topoplotLegend(config, axis, allPositions)
colorrange = (0, length(allPositions)), # add the 0 for the white-first color
colormap = specialColors,
head = (color = :black, linewidth = 1, model = topoMatrix),
label_scatter = (markersize = config.extra.markersize, strokewidth = 0.5),
label_scatter = (markersize = markersize, strokewidth = 0.5),
)

hidedecorations!(current_axis())
@@ -252,7 +266,7 @@ function topoplotLegend(config, axis, allPositions)
end

function addPvalues(plotData, config)
p = deepcopy(config.extra.pvalue)
p = deepcopy(pvalue)

# for now, add them to the fixed effect
if "group" ∉ names(p)
14 changes: 0 additions & 14 deletions src/plotconfig.jl
Original file line number Diff line number Diff line change
@@ -169,13 +169,6 @@ function PlotConfig(T::Val{:butterfly})
config_kwargs!(
cfg;
layout = (; showLegend = false),
extra = (;
topoLegend = true,
markersize = 10,
topowidth = 0.25,
topoheigth = 0.25,
topoPositionToColorFunction = x -> posToColorRomaO(x),
),
mapping = (;
color = (:channel, :channels, :trial, :trials),
positions = (:pos, :positions, :position, :topoPositions, :x, nothing),
@@ -190,13 +183,6 @@ function PlotConfig(T::Val{:erp})
cfg;
mapping = (; color = (:color, :coefname, nothing)),
layout = (; showLegend = true, hidespines = (:r, :t)),
extra = (;
butterfly = false,
categoricalColor = true,
categoricalGroup = true,
stderror = false, # XXX if it exists, should be plotted
pvalue = [],
),
)

return cfg
4 changes: 1 addition & 3 deletions src/topo_color.jl
Original file line number Diff line number Diff line change
@@ -8,9 +8,7 @@ function getTopoPositions(; labels=nothing, positions=nothing)
return positions .|> (p -> Point2f(p[1], p[2]))
end

function getTopoColor(positions, config)
posToColor = config.extra.topoPositionToColorFunction
# positions = getTopoPositions(plotData,config)
function getTopoColor(positions, posToColor)
if isnothing(positions)
return nothing
end
15 changes: 14 additions & 1 deletion test/test_butterfly.jl
Original file line number Diff line number Diff line change
@@ -2,5 +2,18 @@
@testset "markersize change" begin
include("../docs/example_data.jl")
data, pos = example_data("TopoPlots.jl")
plot_butterfly(data; positions=pos, extra=(markersize = 10, topoheigth=0.4, topowidth=0.4))
plot_butterfly(
data;
positions = pos,
markersize = 10,
topoheigth = 0.4,
topowidth = 0.4,
)
end


@testset "markersize change" begin
include("../docs/example_data.jl")
data, pos = example_data("TopoPlots.jl")
plot_butterfly(data; positions = pos)
end
48 changes: 48 additions & 0 deletions test/test_erp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@testset "testing standart" begin
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
f = @formula 0 ~ 1 + condition + continuous
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)

m = fit(
UnfoldModel,
Dict(Any => (f, range(0, step = 1 / 100, length = size(data, 2)))),
evts,
data,
solver = se_solver,
)
results = coeftable(m)
res_effects = effects(Dict(:continuous => -5:0.5:5), m)

# ## Plot the results
plot_erp(results; :stderror => true)
end


@testset "testing standart with more arguments" begin
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
f = @formula 0 ~ 1 + condition + continuous
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)

m = fit(
UnfoldModel,
Dict(Any => (f, range(0, step = 1 / 100, length = size(data, 2)))),
evts,
data,
solver = se_solver,
)
results = coeftable(m)
res_effects = effects(Dict(:continuous => -5:0.5:5), m)

# ## Plot the results
plot_erp(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous),
legend = (; nbanks = 2),
layout = (; legendPosition = :right),
showLegend = true,
categoricalColor = false,
categoricalGroup = true,
)
end