Skip to content

Commit

Permalink
Merge branch 'master' into meta-model
Browse files Browse the repository at this point in the history
  • Loading branch information
rjplevin committed Nov 14, 2018
2 parents 91bf72f + 0b8cddb commit 75a814a
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 38 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Mimi.jl v0.5.1 Release Notes
* Disable topological ordering, remove offset keyword

# Mimi.jl v0.5.0 Release Notes
* Major redesign with lots of breaking changes

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Mimi is a package that provides a component model for integrated assessment mode

Also see the [OptiMimi](http://github.com/jrising/OptiMimi.jl) package for optimizing parameters within Mimi models.

[Julia 1.0](https://julialang.org/blog/2018/08/one-point-zero): Mimi has not yet been ported to Julia 1.0, and thus is not compatible with Julia 1.0 (or [Julia v0.7](https://docs.julialang.org/en/v0.7.0/NEWS/)which provides deprecation warnings for Julia 1.0). The next release of Mimi will provide this compatibility.
Porting to [Mimi 0.5.0](https://github.com/anthofflab/Mimi.jl/releases/tag/v0.5.1): If you are adapting models to the[Mimi 0.5.0](https://github.com/anthofflab/Mimi.jl/releases/tag/v0.5.1) breaking release or later, please use the [Integration Guide](http://anthofflab.berkeley.edu/Mimi.jl/latest/integrationguide.html) as guide to help port your models as easily as possible.

[Julia 1.0](https://julialang.org/blog/2018/08/one-point-zero): Mimi has not yet been ported to Julia 1.0, and thus is not compatible with Julia 1.0 (or [Julia v0.7](https://docs.julialang.org/en/v0.7.0/NEWS/) which provides deprecation warnings for Julia 1.0). The next release of Mimi will provide this compatibility.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ makedocs(
"Tutorial" => "tutorial.md",
"FAQ" => "faq.md",
"Reference" => "reference.md",
"Integration Guide" => "integrationguide.md"]
"Integration Guide: Port to v0.5.0" => "integrationguide.md"]
)

deploydocs(
Expand Down
5 changes: 0 additions & 5 deletions docs/src/integrationguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ In an effort to standardize the function naming protocol within Mimi, and to str

Changes to various optional keyword arguments:

- `connect_param!`: In the case that a component parameter is connected to a variable from a prior timestep, it is necessary to use the `offset` keyword argument to prevent a cycle. The offset value is an `Int` specifying the offset in terms of timesteps as below. Also notice the use of `=>` for readability, as opposed to all arguments being separated by commas. This old syntax will, however, still work.

```julia
connect_param!(mymodel, :TargetComponent=>:parametername, :SourceComponent=>:variablename, offset = 1)
```
- `add_comp!`: Previously the optional keyword arguments `start` and `stop` could be used to specify times for components that do not run for the full length of the model. These arguments are now `first` and `last` respectively.

```julia
Expand Down
5 changes: 2 additions & 3 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ set_param!(mymodel, :ComponentName, :parametername2, rand(351, 3)) # a two-dimen

```

To make an internal connection, the syntax is as follows. Note that there is an optional keyword argument offset, that should be used in the case that a component parameter is connected to a variable from a prior timestep to prevent a cycle. The offset value is an `Int` specifying the offset in terms of timesteps.
To make an internal connection, the syntax is as follows.

```julia
connect_param!(mymodel, :TargetComponent=>:parametername, :SourceComponent=>:variablename)
# Note: offset=1 => dependence is on on prior timestep, i.e., not a cycle
connect_param!(mymodel, :TargetComponent=>:parametername, :SourceComponent=>:variablename, offset = 1)
connect_param!(mymodel, :TargetComponent=>:parametername, :SourceComponent=>:variablename)
```

If you wish to delete a component that has already been added, do the following:
Expand Down
13 changes: 7 additions & 6 deletions src/core/connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ end
Bind the parameter `dst_par_name` of one component `dst_comp_name` of model `md`
to a variable `src_var_name` in another component `src_comp_name` of the same model
using `backup` to provide default values and the `ignoreunits` flag to indicate the need
to check match units between the two. The `offset` argument indicates the offset
between the destination and the source ie. the value would be `1` if the destination
component parameter should only be calculated for the second timestep and beyond.
to check match units between the two. The `offset` argument indicates the offset between the destination
and the source ie. the value would be `1` if the destination component parameter
should only be calculated for the second timestep and beyond.
"""
function connect_param!(md::ModelDef,
dst_comp_name::Symbol, dst_par_name::Symbol,
Expand Down Expand Up @@ -575,9 +575,10 @@ function comp_graph(md::ModelDef)
end
end

if is_cyclic(graph)
error("Component graph contains a cycle")
end
#TODO: for now we can allow cycles since we aren't using the offset
# if is_cyclic(graph)
# error("Component graph contains a cycle")
# end

return graph
end
Expand Down
2 changes: 1 addition & 1 deletion src/explorer/assets/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div id="vis" class="graphic"></div>

<script src="refresh.js"></script>

</body>

</html>
22 changes: 15 additions & 7 deletions src/explorer/assets/refresh.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
// Mimi UI

function refresh(speclist) {
function refresh(menu_item_list) {

// Loop over the things that we want to show in the list, add
// one button for each element as a child to the variablelist div

var element = document.getElementById("variablelist");

for (var i in speclist) {
for (var i in menu_item_list) {

var newButton = document.createElement("button");
newButton.setAttribute("class", "tab");

// Return a closure with a copy of the spec that's private to the function
// Set onclick for button
newButton.onclick = (function() {
var spec = speclist[i]["VLspec"];

var comp_name = menu_item_list[i]["comp_name"]
var item_name = menu_item_list[i]["item_name"]

return function() {
vegaEmbed("#vis", spec, {actions: false});
sendMessageToJulia({cmd: 'display_spec', comp_name: comp_name, item_name: item_name})
}
}())

newButton.appendChild(document.createTextNode(speclist[i]["name"]));
})()

newButton.appendChild(document.createTextNode(menu_item_list[i]["name"]));
element.appendChild(newButton);
}
}

function display(spec) {
vegaEmbed("#vis", spec["VLspec"], {actions: false});

}
31 changes: 24 additions & 7 deletions src/explorer/buildspecs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,40 @@ function _spec_for_item(m::Model, comp_name::Symbol, item_name::Symbol)

end

# Create VegaLite specs for each variable and parameter in the model
function spec_list(model::Model)
allspecs = []
function _menu_item(m::Model, comp_name::Symbol, item_name::Symbol)
dims = dimensions(m, comp_name, item_name)

if length(dims) == 0
value = m[comp_name, item_name]
name = "$comp_name : $item_name = $value"
elseif length(dims) > 2
@warn("$comp_name.$item_name has >2 graphing dims, not yet implemented in explorer")
return nothing
else
name = "$comp_name : $item_name" # the name is needed for the list label
end

menu_item = Dict("name" => name, "comp_name" => comp_name, "item_name" => item_name)
return menu_item
end

# Create the list of variables and parameters
function menu_item_list(model::Model)
all_menuitems = []

for comp_name in map(name, compdefs(model))
items = vcat(variable_names(model, comp_name), parameter_names(model, comp_name))

for item_name in items
spec = _spec_for_item(model, comp_name, item_name)
if spec !== nothing
push!(allspecs, spec)
menu_item = _menu_item(model, comp_name, item_name)
if menu_item !== nothing
push!(all_menuitems, menu_item)
end
end
end

# Return sorted list so that the UI list of items will be in alphabetical order
return sort(allspecs, by = x -> lowercase(x["name"]))
return sort(all_menuitems, by = x -> lowercase(x["name"]))
end

# So we can control these in one place...
Expand Down
18 changes: 13 additions & 5 deletions src/explorer/explore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ function explore(m::Model; title = "Electron")
error("A model must be run before it can be plotted")
end

#get variable data
speclist = spec_list(m)
speclistJSON = JSON.json(speclist)

#start Electron app
if app === nothing
global app = Application()
Expand All @@ -34,8 +30,20 @@ function explore(m::Model; title = "Electron")
slashes = Sys.iswindows() ? "///" : "//"
w = Window(app, URI("file:$(slashes)$(mainpath)"), options = windowopts)

#set async block to process messages
@async for msg in msgchannel(w)

spec = _spec_for_item(m, Symbol(msg["comp_name"]), Symbol(msg["item_name"]))
specJSON = JSON.json(spec)

run(w, "display($specJSON)")
end

#refresh variable list
result = run(w, "refresh($speclistJSON)")
menulist = menu_item_list(m)
menulistJSON = JSON.json(menulist)

result = run(w, "refresh($menulistJSON)")

return w

Expand Down
4 changes: 2 additions & 2 deletions test/test_explorer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Electron
import Mimi:
dataframe_or_scalar, createspec_singlevalue,
createspec_lineplot, createspec_multilineplot, createspec_barplot,
getmultiline, getline, getbar, _spec_for_item, spec_list, explore,
getmultiline, getline, getbar, _spec_for_item, menu_item_list, explore,
getdataframe, reset_compdefs

reset_compdefs()
Expand Down Expand Up @@ -56,7 +56,7 @@ run(m)
#TODO: createspec_singlevalue, createspec_multilineplot,
#createspec_lineplot, createspec_barplot, _spec_for_item

s = spec_list(m)
s = menu_item_list(m)
@test typeof(s) == Array{Any, 1}
@test length(s) == 7

Expand Down
33 changes: 33 additions & 0 deletions wip/Electron_callback_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Electron

app = Application();
w = Window(app);

# OLD 1

# function handle(message)
# @info message["cmd"]
# end

# Electron.set_msg_handler(handle,w)

# run(w, "const {ipcRenderer} = require('electron')") //done for me already
# run(w, "ipcRenderer.send('msg-for-julia-process', {cmd: 'message for Julia'})") //done for me already

# OLD 2

# @async begin
# while true
# m = read(w)
# println(m)
# end
# end

# CURRENT
@async for msg in msgchannel(w)
@info msg
end

run(w, "sendMessageToJulia({cmd: 'message for Julia'})")

read(w) #to get the message

0 comments on commit 75a814a

Please sign in to comment.