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 61 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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.5.7"

[deps]
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
BSplineKit = "093aae92-e908-43d7-9660-e50ee39d5a0a"
vladdez marked this conversation as resolved.
Show resolved Hide resolved
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
Expand Down Expand Up @@ -34,6 +35,7 @@ UnfoldMakiePyMNEExt = "PyMNE"

[compat]
AlgebraOfGraphics = "0.7, 0.8"
BSplineKit = "0.16, 0.17"
CategoricalArrays = "0.10"
ColorSchemes = "3"
ColorTypes = "0.11"
Expand Down
4 changes: 3 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
BSplineKit = "093aae92-e908-43d7-9660-e50ee39d5a0a"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand All @@ -23,4 +24,5 @@ UnfoldSim = "ed8ae6d2-84d3-44c6-ab46-0baf21700804"
XML2_jll = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"

[compat]
AlgebraOfGraphics = "0.7, 0.8"
AlgebraOfGraphics = "0.7, 0.8"
BSplineKit = "0.16, 0.17"
63 changes: 52 additions & 11 deletions docs/example_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +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
# generate ModelStruct
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true)
return fit(
UnfoldModel,
Expand Down Expand Up @@ -72,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 @@ -89,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: 1 addition & 1 deletion docs/literate/explanations/positions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnfoldMakie
using CairoMakie
using TopoPlots
using PyMNE
using PyMNE;

# # Get positions from MNE

Expand Down
6 changes: 2 additions & 4 deletions docs/literate/how_to/hide_deco.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ plot_butterfly!(
data;
positions = pos,
topomarkersize = 10,
topoheight = 0.4,
topowidth = 0.4,
topo_axis = (; height = Relative(0.4), width = Relative(0.4)),
axis = (; title = "With decorations"),
)
plot_butterfly!(
f[2, 1],
data;
positions = pos,
topomarkersize = 10,
topoheight = 0.4,
topowidth = 0.4,
topo_axis = (; height = Relative(0.4), width = Relative(0.4)),
axis = (; title = "Without decorations"),
layout = (; hidedecorations = (:label => true, :ticks => true, :ticklabels => true)),
)
Expand Down
18 changes: 3 additions & 15 deletions docs/literate/how_to/mult_vis_in_fig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ pvals = DataFrame(
to = [0.2, 0.5], # if coefname not specified, line should be black
coefname = ["(Intercept)", "category: face"],
)
plot_erp!(
f[2, 1:2],
results,
categorical_color = false,
categorical_group = false,
significance = pvals,
stderror = true,
)
plot_erp!(f[2, 1:2], results, significance = pvals, stderror = true)

plot_designmatrix!(f[2, 3], designmatrix(uf))

Expand All @@ -94,18 +87,14 @@ res_effects = effects(Dict(:continuous => -5:0.5:5), uf_deconv)
plot_erp!(
f[2, 4:5],
res_effects;
categorical_color = false,
categorical_group = true,
mapping = (; y = :yhat, color = :continuous, group = :continuous),
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 Expand Up @@ -157,8 +146,7 @@ plot_butterfly!(
d_topo;
positions = pos,
topomarkersize = 10,
topoheight = 0.4,
topowidth = 0.4,
topo_axis = (; height = Relative(0.4), width = Relative(0.4)),
)
hlines!(0, color = :gray, linewidth = 1)
vlines!(0, color = :gray, linewidth = 1)
Expand Down
17 changes: 9 additions & 8 deletions docs/literate/how_to/position2color.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# You want to change the colors of the lines and markers on the inserted topoplot.
# To do that you need to change the color scheme (aka color map) of the butterfly plot.
# You can find th elist of colormaps for Makie [here](https://docs.makie.org/v0.21/explanations/colors).

# # Setup

Expand All @@ -19,15 +20,15 @@ plot_butterfly(results; positions = positions)
# # Color schemes
# ## MNE style

#=
We can change the color scale by specifying a function that maps from an `(x, y)` tuple to a color. UnfoldMakie currently provides three different color scales:
- `pos2colorRGB` (same as MNE-Python),
- `pos2colorHSV` (HSV color space),
- `pos2colorRomaO`.
# We can change the color scale by specifying a function that maps from an `(x, y)` tuple to a color.
# `UnfoldMakie` currently provides three different color scales:
# - `pos2colorRGB` (same as MNE-Python),
# - `pos2colorHSV` (HSV color space),
# - `pos2colorRomaO`.

# While `RGB` & `HSV` have the advantage of being 2D color maps, `Roma0` has the advantage of being perceptually uniform.
# Also you can specify a uniform color.

While `RGB` & `HSV` have the advantage of being 2D color maps, `Roma0` has the advantage of being perceptually uniform.
Also you can specify a uniform color.
=#

plot_butterfly(
results;
Expand Down
12 changes: 6 additions & 6 deletions docs/literate/intro/code_principles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

# Here we will write about principles which we developed through our publication.

#- Code should be clear and concise
#- Variables inside the code should have meaningful names
#- Every function exposed to the user should have documentation that specifies all parameters, types, input and output arguments.
#- Most people will not look at the defaults, so it is very important to nudge users to show important details with a picture or text.
#- Function naming should be based on some theory and naming conventions.
#- You should avoid functions longer 50 lines
# - Code should be clear and concise.
# - Variables inside the code should have meaningful names.
# - Every function exposed to the user should have documentation that specifies all parameters, types, input and output arguments.
# - Most people will not look at the defaults, so it is very important to nudge users to label important details of the plot.
# - Function naming should be based on some theory and naming conventions.
# - You should avoid functions longer 50 lines.
7 changes: 6 additions & 1 deletion docs/literate/tutorials/butterfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ plot_butterfly(df; positions = pos)

# You want to change size of topomarkers and size of topoplot:

plot_butterfly(df; positions = pos, topomarkersize = 10, topoheight = 0.4, topowidth = 0.4)
plot_butterfly(
df;
positions = pos,
topomarkersize = 10,
topo_axis = (; height = Relative(0.4), width = Relative(0.4)),
)

# You want to add vline and hline:

Expand Down
2 changes: 1 addition & 1 deletion docs/literate/tutorials/designmatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using CairoMakie
# Data

include("../../../example_data.jl")
uf = example_data("UnfoldLinearModel")
uf = example_data("UnfoldLinearModel");

# # Plot Designmatrices

Expand Down
15 changes: 8 additions & 7 deletions docs/literate/tutorials/erp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using CairoMakie
using DataFramesMeta
using UnfoldSim
using UnfoldMakie
include("../../../example_data.jl")
include("../../../example_data.jl");

# Data generation

Expand All @@ -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,9 +56,7 @@ plot_erp(results)
plot_erp(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous),
layout = (; show_legend = false),
categorical_color = false, # perceives color (here: continuous) as contionus
categorical_group = true, # separates lines, if `false` all lines will be connected
layout = (; use_colorbar = false),
)

# ## Significance lines
Expand All @@ -68,8 +69,8 @@ plot_erp(
m = example_data("UnfoldLinearModel")
results = coeftable(m)
significancevalues = DataFrame(
from = [0.1, 0.3],
to = [0.5, 0.7],
from = [0.01, 0.2],
to = [0.3, 0.4],
coefname = ["(Intercept)", "condition: face"], # if coefname not specified, line should be black
)
plot_erp(results; :significance => significancevalues)
Expand Down Expand Up @@ -110,7 +111,7 @@ text!(0.98, 0.2, text = "* Confidence\nintervals", align = (:right, :top))
f

# There are two ways to implement it.
# First is using `:stderror = true' after `;`.
# First is using `:stderror = true` after `;`.

results.se_low = results.estimate .- 0.5
results.se_high = results.estimate .+ 0.15
Expand Down
31 changes: 31 additions & 0 deletions docs/literate/tutorials/splines.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# # [Spline plot](@id spline_vis)
# **Spline plot** is a plot type for visualisation of 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.

# # Setup
# Package and data loading

using Unfold, UnfoldMakie
using BSplineKit, DataFrames


include("../../../example_data.jl")
df, pos = example_data("TopoPlots.jl")
m1 = example_data("UnfoldLinearModelwith1Spline");
m2 = example_data("UnfoldLinearModelwith2Splines");


# Spline plot with one spline term:
plot_splines(m1)

# Spline plot with two spline terms:
plot_splines(m2)

# # Configurations of Spline plot

# ```@docs
# plot_splines
# ```
2 changes: 1 addition & 1 deletion docs/literate/tutorials/topoplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ f

# ```@docs
# plot_topoplot
# ```
# ```
14 changes: 12 additions & 2 deletions docs/literate/tutorials/topoplotseries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ nothing #hide
# `bin_width` - specify the interval between topoplots

bin_width = 80
plot_topoplotseries(df; bin_width, positions = positions)
plot_topoplotseries(
df;
bin_width,
positions = positions,
axis = (; xlabel = "Time windows [s]"),
)

# `bin_num` - specify the number of topoplots

plot_topoplotseries(df; bin_num = 5, positions = positions)
plot_topoplotseries(
df;
bin_num = 5,
positions = positions,
axis = (; xlabel = "Time windows [s]"),
)

# # Categorical and contionous x-values
# By deafult x-value is `time`, but it could be any contionous (i.g. saccade amplitude) or categorical (any experimental variable) value.
Expand Down
Loading
Loading