Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ run(mymodel)

![Explorer Model Example](figs/explorer_model_example.png)

Alternatively, in order to view just one parameter or variable, call the function `explore` as below to return a plot object and automatically display the plot in a viewer, assuming `explore` is the last command executed. This call will return the type `VegaLite.VLSpec`, which you may interact with using the API described in the [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) documentation. For example, [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) plots can be saved as [PNG](https://en.wikipedia.org/wiki/Portable_Network_Graphics), [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics), [PDF](https://en.wikipedia.org/wiki/PDF) and [EPS](https://en.wikipedia.org/wiki/Encapsulated_PostScript) files. You may save a plot using the `save` function. Note that saving an interactive plot in a non-interactive file format, such as .pdf or .svg will result in a warning `WARN Can not resolve event source: window`, but the plot will be saved as a static image. If you wish to preserve interactive capabilities, you may save it using the .vegalite file extension. If you then open this file in Jupyter lab, the interactive aspects will be preserved.
Alternatively, in order to view just one parameter or variable, call the function `explore` as below to return a plot object and automatically display the plot in a viewer, assuming `explore` is the last command executed. This call will return the type `VegaLite.VLSpec`, which you may interact with using the API described in the [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) documentation. For example, [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) plots can be saved as [PNG](https://en.wikipedia.org/wiki/Portable_Network_Graphics), [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics), [PDF](https://en.wikipedia.org/wiki/PDF) and [EPS](https://en.wikipedia.org/wiki/Encapsulated_PostScript) files. You may save a plot using the `save` function. Note that while `explore(m)` returns interactive plots for line graphs, `explore(m, :foo, :bar)` will return only static plots.

```julia
using VegaLite
Expand All @@ -168,8 +168,6 @@ p = explore(mymodel, component1, parameter1)
save("figure.svg", p)
```

![Explorer Single Plot Example](figs/explorer_single_plot_example.png)

## Sensitivity Analysis (SA) Support

Mimi includes a host of routines which support running various sensitivity analysis methods on Mimi models. The best current documentation on the SA API is the internals documentation [here](internals/montecarlo.md), which provides a working, although informal, description of the SA support of Mimi. This file should be used in conjunction with the examples in [Tutorial 4: Sensitivity Analysis (SA) Support](@ref), since the documentation covers more advanced options such as non-stochastic scenarios and running multiple models, which are not yet included in this tutorial.
Expand Down
79 changes: 73 additions & 6 deletions src/explorer/buildspecs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function dataframe_or_scalar(m::Model, comp_name::Symbol, item_name::Symbol)
end

# Generate the VegaLite spec for a variable or parameter
function _spec_for_item(m::Model, comp_name::Symbol, item_name::Symbol)
function _spec_for_item(m::Model, comp_name::Symbol, item_name::Symbol; interactive::Bool=true)
dims = dimensions(m, comp_name, item_name)

# Control flow logic selects the correct plot type based on dimensions
Expand All @@ -32,9 +32,9 @@ function _spec_for_item(m::Model, comp_name::Symbol, item_name::Symbol)
# a 'time' field necessitates a line plot
elseif dffields[1] == "time"
if length(dffields) > 2
spec = createspec_multilineplot(name, df, dffields)
spec = createspec_multilineplot(name, df, dffields, interactive=interactive)
else
spec = createspec_lineplot(name, df, dffields)
spec = createspec_lineplot(name, df, dffields, interactive=interactive)
end

#otherwise we are dealing with a barplot
Expand Down Expand Up @@ -88,7 +88,11 @@ global const _plot_width = 450
global const _plot_height = 410
global const _slider_height = 90

function createspec_lineplot(name, df, dffields)
function createspec_lineplot(name, df, dffields; interactive::Bool=true)
interactive ? createspec_lineplot_interactive(name, df, dffields) : createspec_lineplot_static(name, df, dffields)
end

function createspec_lineplot_interactive(name, df, dffields)
datapart = getdatapart(df, dffields, :line) #returns JSONtext type
spec = Dict(
"name" => name,
Expand Down Expand Up @@ -141,7 +145,40 @@ function createspec_lineplot(name, df, dffields)
return spec
end

function createspec_multilineplot(name, df, dffields)
function createspec_lineplot_static(name, df, dffields)
datapart = getdatapart(df, dffields, :line) #returns JSONtext type
spec = Dict(
"name" => name,
"type" => "line",
"VLspec" => Dict(
"\$schema" => "https://vega.github.io/schema/vega-lite/v2.0.json",
"description" => "plot for a specific component variable pair",
"title" => name,
"data"=> Dict("values" => datapart),
"mark" => Dict("type" => "line"),
"encoding" => Dict(
"x" => Dict(
"field" => dffields[1],
"type" => "temporal",
"timeUnit" => "utcyear",
),
"y" => Dict(
"field" => dffields[2],
"type" => "quantitative",
)
),
"width" => _plot_width,
"height" => _plot_height,
)
)
return spec
end

function createspec_multilineplot(name, df, dffields; interactive::Bool=true)
interactive ? createspec_multilineplot_interactive(name, df, dffields) : createspec_multilineplot_static(name, df, dffields)
end

function createspec_multilineplot_interactive(name, df, dffields)
datapart = getdatapart(df, dffields, :multiline) #returns JSONtext type
spec = Dict(
"name" => name,
Expand Down Expand Up @@ -197,6 +234,37 @@ function createspec_multilineplot(name, df, dffields)
return spec
end

function createspec_multilineplot_static(name, df, dffields)
datapart = getdatapart(df, dffields, :multiline) #returns JSONtext type
spec = Dict(
"name" => name,
"VLspec" => Dict(
"\$schema" => "https://vega.github.io/schema/vega-lite/v2.0.json",
"description" => "plot for a specific component variable pair",
"title" => name,
"data" => Dict("values" => datapart),

"mark" => Dict("type" => "line"),
"encoding" => Dict(
"x" => Dict(
"field" => dffields[1],
"type" => "temporal",
"timeUnit" => "utcyear",
),
"y" => Dict(
"field" => dffields[3],
"type" => "quantitative",
),
"color" => Dict("field" => dffields[2], "type" => "nominal",
"scale" => Dict("scheme" => "category20")),
),
"width" => _plot_width,
"height" => _plot_height
),
)
return spec
end

function createspec_barplot(name, df, dffields)
datapart = getdatapart(df, dffields, :bar) #returns JSONtext type
spec = Dict(
Expand Down Expand Up @@ -253,7 +321,6 @@ function getdatapart(df, dffields, plottype::Symbol)
return JSON.JSONText(datapart)
end


function getmultiline(cols, dffields)
datasb = StringBuilder()
numrows = length(cols[1])
Expand Down
4 changes: 2 additions & 2 deletions src/explorer/explore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function explore(m::Model; title = "Electron")
end

"""
explore(m::Model, comp_name::Symbol, datum_name::Symbol)
explore(m::Model, comp_name::Symbol, datum_name::Symbol; interactive::String=true)

Plot a specific `datum_name` (a `variable` or `parameter`) of Model `m`.
"""
Expand All @@ -62,7 +62,7 @@ function explore(m::Model, comp_name::Symbol, datum_name::Symbol)
error("A model must be run before it can be plotted")
end

spec = Mimi._spec_for_item(m, comp_name, datum_name)["VLspec"]
spec = Mimi._spec_for_item(m, comp_name, datum_name, interactive=false)["VLspec"]
spec === nothing && error("Spec cannot be built.")

return VegaLite.VLSpec{:plot}(spec)
Expand Down
1 change: 1 addition & 0 deletions test/test_explorer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ s = menu_item_list(m)
#4. explore(m::Model, title = "Electron")
w = explore(m, title = "Testing Window")
@test typeof(w) == Electron.Window
close(w)

#5. explore(m::Model, comp_name::Symbol, datum_name::Symbol;
# dim_name::Union{Nothing, Symbol} = nothing)
Expand Down