Save and load Unfold models
Unfold.jl allows storing Unfold models in a memory-efficient way using (compressed) .jld2 files.
Simulate EEG data and fit an Unfold model
Click to expand
Simulate some example data using UnfoldSim.jl
using UnfoldSim
data, events = UnfoldSim.predef_eeg(; n_repeats = 10)
first(events, 5)
5×3 DataFrame
Row | continuous | condition | latency |
---|---|---|---|
Float64 | String | Int64 | |
1 | 2.77778 | car | 62 |
2 | -5.0 | face | 132 |
3 | -1.66667 | car | 196 |
4 | -5.0 | car | 249 |
5 | 5.0 | car | 303 |
Fit an Unfold model
using Unfold
basisfunction = firbasis(τ = (-0.5, 1.0), sfreq = 100, name = "stimulus")
f = @formula 0 ~ 1 + condition + continuous
bfDict = Dict(Any => (f, basisfunction))
m = fit(UnfoldModel, bfDict, events, data);
┌ Warning: using `Dict(:A=>(@formula,times/basisfunction))` is deprecated, please use `[:A=>(@formula,times/basisfunction)]` from now on
└ @ Unfold ~/work/Unfold.jl/Unfold.jl/src/fit.jl:74
Save and load the fitted Unfold model
The following code saves the model in a compressed .jld2 file. The default option of the save
function is compress=false
. For memory efficiency the designmatrix is set to missing. If needed, it can be reconstructed when loading the model.
save_path = mktempdir(; cleanup = false) # create a temporary directory for the example
save(joinpath(save_path, "m_compressed.jld2"), m; compress = true);
The load
function allows to retrieve the model again. By default, the designmatrix is reconstructed. If it is not needed set generate_Xs=false
` which improves time-efficiency.
m_loaded = load(joinpath(save_path, "m_compressed.jld2"), UnfoldModel, generate_Xs = true);
This page was generated using Literate.jl.