InteractModels

InteractModels.InteractModelsModule

InteractModels

Stable Dev CI Coverage

InteractModels is a subpackage of ModelParameters.jl that provides an interactive web interface that can run in Atom, Jupyter notebooks, Electron apps or be served on the web.

It's separated out to avoid loading the web stack when it isn't needed.

source
InteractModels.InteractModelType
InteractModel(f, model)

An AbstractModel that generates its own Interact.jl interface. Each model Param has a slider generated for it to update the model. After any slider updates the user-defined function f is passed the updated model to generate a new output - anything that will display in a WebIO.jl node, like a Plots.jl plot

After any slider changes, the parent model is updated, so that parent(model) will return it's latest state.

Arguments

  • f: a function that take the model object as an argument, but with Param fields replaced with their values. Usually a do block.
  • model: any object with Params objects in some fields.

Param fields

Param objects in the model need to include a range or bounds field to define the slider range - holding an AbsractRange or NTuple{2}, respectively.

Optionally, the Params can also include:

  • A label field to use instead of field names
  • A desciption field to use in mouse hover text

Keyword Arguments

  • title: "" set a window title, if you need it.
  • submodel: Nothing. Type or Union that will group sliders into labeled subsections.
  • throttle: 0.1. Slider throttle, in seconds. Adjust to improve performance.
  • layout: vbox. This can be any three-argument function that will combine title, output, and sliders (all WebIO nodes) into a combined WebIO node. You can use this method to do any additional layout you need.

Example

This is a simple example where the model is a NamedTuple, adapted from an Interact.jl example. It will display in atoms plot pane:

using InteractModels, Interact, ColorSchemes, Colors

color(i) = Colors.hex(colors[i%length(colors)+1])
colors = ColorSchemes.viridis
width, height = 700, 300
nsamples = 256

model = (;
    sample_step=Param(val=0.05, range=0.01:0.001:0.1, label="Sample step"),
    phase=Param(val=0.0, range=0:0.1:2pi, label="Phase"),
    radii=Param(val=20,range=0:0.1:60, label="Radus")
)

ui = InteractModel(model; submodel=Nothing, throttle=0.1) do m
    cxs_unscaled = [i * m.sample_step + m.phase for i in 1:nsamples]
    cys = sin.(cxs_unscaled) .* height/3 .+ height/2
    cxs = cxs_unscaled .* width/4pi
    c = (dom"svg:circle[cx=$(cxs[i]), cy=$(cys[i]), r=$(m.radii), fill=#$(color(i))]"()
         for i in 1:nsamples)
    return dom"svg:svg[width=$width, height=$height]"(c...)
end
source
InteractModels.attach_sliders!Method
attach_sliders!(f, model::AbstractModel; submodel=false, throttle=0.1)
attach_sliders!(model::AbstractModel; submodel=false, throttle=0.1, f=identity)

Internal method that may be useful for creating custom interfaces like InteractModel, without actually using InteracModel directly. This interface will be less stable than InteractModel.

Create sliders and attach them to the model so it will be updated when they are moved.

Arguments

  • f: a function that accepts the model, stripped of Param wrappers, with a return value that sets the observable obs. Usually this converts it to a plot or other web output.
  • model: a AbstractModel

Keyword Arguments

  • throttle: 0.1 - sliders response time, in seconds.
  • submodel: Nothing. Type or Union that will group sliders into labeled subsections.
  • obs: An optional observable to be updated when sliders change

Returns a vbox holding the slider widgets.

source