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

Splines and plot_erp #237

Merged
merged 68 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
6f64fee
new example_data with splines
vladdez Sep 12, 2024
b46dffd
categorical colors depricated, issues 235, 231
vladdez Sep 12, 2024
5abd763
remnants
vladdez Sep 12, 2024
44a9da5
Update src/plot_erp.jl
vladdez Sep 12, 2024
9e40960
Update test/test_erp.jl
vladdez Sep 12, 2024
80e88ea
Update src/plot_erp.jl
vladdez Sep 12, 2024
43b0cab
removing remnants from docs
vladdez Sep 12, 2024
2355bbd
Merge branch 'splines' of https://github.com/unfoldtoolbox/UnfoldMaki…
vladdez Sep 12, 2024
c07da9c
tiny
vladdez Sep 12, 2024
5642eaf
better p_vals
vladdez Sep 13, 2024
0923c3f
first working splie plot
vladdez Sep 13, 2024
b8a9147
bug
vladdez Sep 13, 2024
df0dc44
Update src/plot_parallelcoordinates.jl
vladdez Sep 13, 2024
69d4280
new link
vladdez Sep 13, 2024
01f761e
renaming and more configs
vladdez Sep 13, 2024
f6e0fc9
simplification of layout helper and issue 225
vladdez Sep 13, 2024
d2b6d6a
failed to do multilaine comment in Literate
vladdez Sep 13, 2024
01e8c1c
sterror issues 55 nad 238
vladdez Sep 16, 2024
e2d9f7d
additional parameters
vladdez Sep 16, 2024
d2e5bd8
bug
vladdez Sep 16, 2024
f760562
multiple splines
vladdez Sep 16, 2024
b092803
knots and contionous colors implemented
vladdez Sep 16, 2024
5dff5ac
spline docs
vladdez Sep 16, 2024
3dda76b
tiny cleaning
vladdez Sep 16, 2024
cc75a23
bug
vladdez Sep 16, 2024
fb3f288
Update src/plot_splines.jl
vladdez Sep 16, 2024
442d539
doc bug
vladdez Sep 16, 2024
d28a6ee
hide
vladdez Sep 16, 2024
7e3b0c6
formatting
vladdez Sep 17, 2024
3055711
bug in docstrings
vladdez Sep 17, 2024
15bcb06
simplification in plot_erp
vladdez Sep 17, 2024
248c168
test for issue 232
vladdez Sep 17, 2024
667eacc
better example2
vladdez Sep 17, 2024
d61c72c
buf for doctrsring
vladdez Sep 17, 2024
090ccd7
Update src/plot_splines.jl
vladdez Sep 18, 2024
ecfe965
compats
vladdez Sep 18, 2024
9175a59
rearrangment of doc pages
vladdez Sep 18, 2024
2f8ac9b
supportive axes for splines
vladdez Sep 18, 2024
42c9e28
more clever way to update supportive axes
vladdez Sep 18, 2024
656de32
same with superlabel
vladdez Sep 18, 2024
a5df6c4
now is correct
vladdez Sep 18, 2024
8d674e5
issue 172 and no_spline test
vladdez Sep 18, 2024
5ed10d1
compats
vladdez Sep 18, 2024
d7a7d90
renaming
vladdez Sep 18, 2024
3c6c7c4
Update src/plot_splines.jl
vladdez Sep 20, 2024
c7de87c
after comments
vladdez Sep 20, 2024
da27b27
we accidently lost colorbars
vladdez Sep 20, 2024
ca787bd
issue 239
vladdez Sep 20, 2024
e11fa1c
easy bug
vladdez Sep 20, 2024
87cf547
bug
vladdez Sep 20, 2024
3d1b902
minor changes in docs
vladdez Sep 20, 2024
577b3b8
indiv_docstrings - to show them in docs
vladdez Sep 20, 2024
a855d9c
also for plot_erpimage
vladdez Sep 20, 2024
120075e
same for buttergly
vladdez Sep 20, 2024
022e7c4
issue 236
vladdez Sep 23, 2024
0c48f13
issue 222
vladdez Sep 23, 2024
09ca287
customizable plot_erpgrid, issue 94
vladdez Sep 23, 2024
be27c4f
issue 205
vladdez Sep 23, 2024
211a444
add links to docstrings
vladdez Sep 23, 2024
a4660eb
issue 106
vladdez Sep 23, 2024
8cb4a8a
okay, reverse
vladdez Sep 23, 2024
7681bdd
issue 147 - indicator for topoplot
vladdez Sep 23, 2024
74ebd0c
supportive_defaults + docs
vladdez Sep 23, 2024
9f2ef72
format
vladdez Sep 23, 2024
c465e86
upd Readme
vladdez Sep 23, 2024
38c45f9
upd Readme2
vladdez Sep 24, 2024
9aa472b
the last edit
vladdez Sep 24, 2024
6d3dae6
version up
vladdez Sep 24, 2024
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
96 changes: 52 additions & 44 deletions docs/example_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,34 @@ using Random
"""
example_data(String)

Creates example data. Currently, 7 datasets are available.
- `TopoPlots.jl` (default) - tidy DataFrame from `TopoPlots.jl` with 2 conditions, 64 channels and 800 ms time range.
- `UnfoldLinearModel`
- `UnfoldLinearModelMultiChannel`
- `UnfoldLinearModelContinuousTime`
- `7channels`
- `UnfoldTimeExpanded`
- `sort_data` - includes DataFrame EEG recordings `dat` and `evts` with event variables occured during experiment. `evts` could be used for sorting EEG data in ERP image.
Creates example data or model. Currently, 3 datasets and 6 models are available.

Datasets:
- `TopoPlots.jl` (default) - 2 DataFrames from `TopoPlots.jl`:\\
- DataFrame with estimate, time, 64 channels, topopositions, sterror and pvalue and 800 ms time range.\\
- Posiions for 64 electrodes.
- `UnfoldLinearModelMultiChannel` - DataFrame with 5 channels, 3 coefnames, sterror, time and estimate.
- `sort_data` - 2 DataFrames:
- `dat` for EEG recordings and `evts` with event variables occured during experiment.\\
- `evts` could be used for sorting EEG data in ERP image.

Models:
- `UnfoldLinearModel` - Model with formula `1 + condition + continuous`.
- `UnfoldLinearModelContinuousTime` - Model with formula `timeexpand(1 + condition + continuous)` for times [0.0, 0.01 ... 0.5].
- `UnfoldLinearModelwith1Spline` - Model with formula `1 + condition + spl(continuous, 4)`.
- `UnfoldLinearModelwith2Splines` - Model with formula ` 1 + condition + spl(continuous, 4) + spl(continuous2, 6)`.
- `7channels` - Model with formula `timeexpand(1 + condA)` for times [-0.1, -0.09 ... 0.5].
- `UnfoldTimeExpanded` - Model with formula `timeexpand(1 + condition + continuous)` for times [-0.4, -0.39 ... 0.8].

**Return Value:** `DataFrame`.
"""
function example_data(example = "TopoPlots.jl")

if example == "UnfoldLinearModel"

# load and generate a simulated Unfold Design
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
f = @formula 0 ~ 1 + condition + continuous
# generate ModelStruct
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)
return fit(
UnfoldModel,
[Any => (f, range(0, length = size(data, 2), step = 1 / 100))],
evts,
data;
solver = se_solver,
)
elseif example == "UnfoldLinearModelwith1Spline"
# load and generate a simulated Unfold Design
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
evts.continuous2 .=
log10.(6 .+ rand(MersenneTwister(1), length(evts.continuous))) .^ 2
f = @formula 0 ~ 1 + condition + spl(continuous, 4)
# generate ModelStruct
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)
return fit(
UnfoldModel,
[Any => (f, range(0, length = size(data, 2), step = 1 / 100))],
evts,
data;
solver = se_solver,
)
elseif example == "UnfoldLinearModelwith2Splines"
# load and generate a simulated Unfold Design
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
evts.continuous2 .=
log10.(6 .+ rand(MersenneTwister(1), length(evts.continuous))) .^ 2
f = @formula 0 ~ 1 + condition + spl(continuous, 4) + spl(continuous2, 6)
# generate ModelStruct
# generate ModelStruct
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)
return fit(
UnfoldModel,
Expand Down Expand Up @@ -105,6 +82,38 @@ function example_data(example = "TopoPlots.jl")
basis = firbasis([0, 0.5], 100)
# generate ModelStruct
return fit(UnfoldModel, [Any => (f, basis)], evts, data)
elseif example == "UnfoldLinearModelwith1Spline"
# load and generate a simulated Unfold Design
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
evts.continuous2 .=
log10.(6 .+ rand(MersenneTwister(1), length(evts.continuous))) .^ 2
f = @formula 0 ~ 1 + condition + spl(continuous, 4)
# generate ModelStruct
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)
return fit(
UnfoldModel,
[Any => (f, range(0, length = size(data, 2), step = 1 / 100))],
evts,
data;
solver = se_solver,
)
elseif example == "UnfoldLinearModelwith2Splines"
# load and generate a simulated Unfold Design
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
evts.continuous2 .=
log10.(6 .+ rand(MersenneTwister(1), length(evts.continuous))) .^ 2
f = @formula 0 ~ 1 + condition + spl(continuous, 4) + spl(continuous2, 6)
# generate ModelStruct
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)
return fit(
UnfoldModel,
[Any => (f, range(0, length = size(data, 2), step = 1 / 100))],
evts,
data;
solver = se_solver,
)
elseif example == "7channels"
design =
SingleSubjectDesign(conditions = Dict(:condA => ["levelA", "levelB"])) |>
Expand All @@ -122,7 +131,6 @@ function example_data(example = "TopoPlots.jl")
f = @formula 0 ~ 1 + condA
bf_dict = [Any => (f, basisfunction)]
return fit(UnfoldModel, bf_dict, evnts, df)

elseif example == "UnfoldTimeExpanded"
df, evts = UnfoldSim.predef_eeg()
f = @formula 0 ~ 1 + condition + continuous
Expand Down
2 changes: 0 additions & 2 deletions docs/literate/how_to/mult_vis_in_fig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ plot_erp!(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous => nonnumeric),
legend = (; nbanks = 2),
layout = (; show_legend = true, legend_position = :right),
)

plot_parallelcoordinates(
f[3, 2:3],
uf_5chan;
mapping = (; color = :coefname),
layout = (; legend_position = :right),
)
vladdez marked this conversation as resolved.
Show resolved Hide resolved

plot_erpimage!(f[1, 4:5], times, d_singletrial)
Expand Down
5 changes: 4 additions & 1 deletion docs/literate/tutorials/erp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ res_effects = effects(Dict(:continuous => -5:0.5:5), m);
# ## Figure plotting
plot_erp(results)

# To change legend title use `mapping.color`:
plot_erp(results, mapping = (; color = :coefname => "Conditions"))

# # Additional features

# ## Effect plot
Expand All @@ -53,7 +56,7 @@ plot_erp(results)
plot_erp(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous),
layout = (; show_legend = false),
layout = (; use_colorbar = false),
)

# ## Significance lines
Expand Down
17 changes: 14 additions & 3 deletions src/docstring_template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ function _docstring(cfg_symb::Symbol)
:figure => "use `kwargs...` of [`Makie.Figure`](https://docs.makie.org/stable/explanations/figure/)",
:axis => "use `kwargs...` of [`Makie.Axis`](https://docs.makie.org/stable/reference/blocks/axis/)",
:legend => "use `kwargs...` of [`Makie.Legend`](https://docs.makie.org/stable/reference/blocks/legend/)",
:colorbar => "use `kwargs...` of $cbarstring",
:layout => "check this [page](https://unfoldtoolbox.github.io/UnfoldMakie.jl/dev/generated/how_to/hide_deco/)",
:mapping => "use any mapping from [`AlgebraOfGraphics`](https://aog.makie.org/stable/layers/mapping/)",
:visual => "use `kwargs...` of [$(visuallink[cfg_symb])]($(visuallink2[cfg_symb]))",
:colorbar => "use `kwargs...` of $cbarstring",
)
for k = 1:length(fn)
namedtpl = string(Base.getfield(cfg, fn[k]))
Expand All @@ -53,7 +55,7 @@ function _docstring(cfg_symb::Symbol)

return """## Shared plot configuration options
The shared plot options can be used as follows: `type = (; key = value, ...))`.\\
For example, `plot_x(...; layout = (; show_legend = true, legend_position = :right))`.\\
For example, `plot_x(...; colorbar = (; vertical = true, label = "Test"))`.\\
Multiple defaults will be cycled until match.

Placing `;` is important!
Expand All @@ -63,7 +65,7 @@ function _docstring(cfg_symb::Symbol)
end

function indiv_docstrings(cfg_symb::Symbol)
behinger marked this conversation as resolved.
Show resolved Hide resolved
# plot__splines
# plot_splines
if cfg_symb == :spline_default
return (;
ylabel = "Spline value",
Expand Down Expand Up @@ -94,5 +96,14 @@ function indiv_docstrings(cfg_symb::Symbol)
)
elseif cfg_symb == :sortplot_default
return (; ylabelvisible = true, yticklabelsvisible = false)
# plot_erpgrid
elseif cfg_symb == :hlines_grid_default
return (; color = :gray, linewidth = 0.5)
elseif cfg_symb == :vlines_grid_default
return (; color = :gray, linewidth = 0.5, ymin = 0.2, ymax = 0.8)
elseif cfg_symb == :lines_grid_default
return (; color = :deepskyblue3)
elseif cfg_symb == :labels_grid_default
return (; color = :gray, fontsize = 12, align = (:left, :top), space = :relative)
end
end
2 changes: 1 addition & 1 deletion src/plot_butterfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function plot_butterfly!(
topomarkersize = 10,
topopositions_to_color = x -> pos_to_color_RomaO(x),
topo_axis = (;),
mapping = (;), # WTf??
mapping = (;),
kwargs...,
)

Expand Down
6 changes: 2 additions & 4 deletions src/plot_circular_topoplots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ function plot_circular_topoplots!(
# setting the colorbar to the bottom right of the box.
# Relative values got determined by checking what subjectively looks best
Colorbar(
f[1, 2],
colormap = config.colorbar.colormap,
f[1, 2];
colorrange = (min, max),
label = config.colorbar.label,
config.colorbar...,
height = @lift Fixed($(pixelarea(ax.scene)).widths[2])
)
plot_topo_plots!(
Expand All @@ -102,7 +101,6 @@ function plot_circular_topoplots!(
)

apply_layout_settings!(config; ax = ax)

# set the scene's background color according to config
#set_theme!(Theme(backgroundcolor = config.axisData.backgroundcolor))
return f
Expand Down
56 changes: 40 additions & 16 deletions src/plot_erpgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ Plot an ERP image.
Electrode positions.
- `ch_names::Vector{String}`\\
Vector with channel names.
- `hlines_grid_axis::NamedTuple = (;)`\\
vladdez marked this conversation as resolved.
Show resolved Hide resolved
Here you can flexibly change configurations of the hline axis.\\
To see all options just type `?hlines` in REPL.\\
Defaults: $(indiv_docstrings(:hlines_grid_default))
- `vlines_grid_axis::NamedTuple = (;)`\\
Here you can flexibly change configurations of the vline axis.\\
To see all options just type `?vlines` in REPL.\\
Defaults: $(indiv_docstrings(:vlines_grid_default))
- `lines_grid_axis::NamedTuple = (;)`\\
Here you can flexibly change configurations of the lines axis.\\
To see all options just type `?lines` in REPL.\\
Defaults: $(indiv_docstrings(:lines_grid_default))
- `labels_grid_axis::NamedTuple = (;)`\\
Here you can flexibly change configurations of the labels axis.\\
To see all options just type `?text` in REPL.\\
Defaults: $(indiv_docstrings(:labels_grid_default))

## Keyword arguments (kwargs)
- `drawlabels::Bool = false`\\
Expand Down Expand Up @@ -48,6 +64,10 @@ function plot_erpgrid!(
ch_names::Vector{String};
drawlabels = false,
times = -1:size(data, 2)-2, #arbitrary
labels_grid_axis = (;),
hlines_grid_axis = (;),
vlines_grid_axis = (;),
lines_grid_axis = (;),
kwargs...,
)
config = PlotConfig(:erpgrid)
Expand All @@ -72,6 +92,8 @@ function plot_erpgrid!(

axlist = []
rel_zeropoint = argmin(abs.(times)) ./ length(times)
labels_grid_axis =
update_axis(indiv_docstrings(:labels_grid_default); labels_grid_axis...)
for (ix, p) in enumerate(eachcol(positions))
x = p[1]
y = p[2]
Expand All @@ -83,29 +105,25 @@ function plot_erpgrid!(
valign = y,
)
if drawlabels
text!(
ax,
rel_zeropoint + 0.1,
1,
color = :gray,
fontsize = 12,
text = ch_names[ix],
align = (:left, :top),
space = :relative,
)
text!(ax, rel_zeropoint + 0.1, 1; text = ch_names[ix], labels_grid_axis...)
end
# todo: add label if not nothing

push!(axlist, ax)
end
# todo: make optional + be able to specify the linewidth + color
hlines!.(axlist, Ref([0.0]), color = :gray, linewidth = 0.5)
vlines!.(axlist, Ref([0.0]), color = :gray, linewidth = 0.5)
hlines_grid_axis =
update_axis(indiv_docstrings(:hlines_grid_default); hlines_grid_axis...)
vlines_grid_axis =
update_axis(indiv_docstrings(:vlines_grid_default); vlines_grid_axis...)
lines_grid_axis = update_axis(indiv_docstrings(:lines_grid_default); lines_grid_axis...)

hlines!.(axlist, Ref([0.0]); hlines_grid_axis...)
vlines!.(axlist, Ref([0.0]); vlines_grid_axis...)

times = isnothing(times) ? (1:size(data, 2)) : times

# todo: add customizable kwargs
h = lines!.(axlist, Ref(times), eachrow(data))
h = lines!.(axlist, Ref(times), eachrow(data); lines_grid_axis...)

linkaxes!(axlist...)
hidedecorations!.(axlist)
Expand All @@ -119,14 +137,20 @@ function plot_erpgrid!(
xstart = [Point2f(0), Point2f(0)]
xdir = [Vec2f(0, 0.1), Vec2f(0.1, 0)]
arrows!(xstart, xdir, arrowsize = 10)
text!(0.02, 0, text = config.axis.xlabel, align = (:left, :top), fontsize = 12)
text!(
0.02,
0,
text = config.axis.xlabel,
fontsize = config.axis.fontsize,
align = (:left, :top),
)
text!(
-0.008,
0.01,
text = config.axis.ylabel,
fontsize = config.axis.fontsize,
align = (:left, :baseline),
rotation = π / 2,
fontsize = 12,
)
f
end
4 changes: 2 additions & 2 deletions src/plot_splines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using BSplineKit, Unfold
Visualization of spline terms in an UnfoldModel. Two subplots are generated for each spline term:\\
1) the basis function of the spline; 2) the density of the underlying covariate.\\

Multiple spline terms are arranged across columns.
Dashed lines indicate spline knots.
Multiple spline terms are arranged across columns.\\
Dashed lines indicate spline knots.

## Arguments:

Expand Down
2 changes: 1 addition & 1 deletion src/plot_topoplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function plot_topoplot!(
@warn """The min and max of the value represented by the color are the same, it seems that the data values are identical.
We disable the color bar in this figure.
Note: The identical min and max may cause an interpolation error when plotting the topoplot."""
config_kwargs!(config, layout = (; use_colorbar = false, show_legend = false))
config_kwargs!(config, layout = (; use_colorbar = false))
else
config_kwargs!(config, colorbar = (; limits = clims))
end
Expand Down
Loading
Loading