Skip to content

Commit

Permalink
read me
Browse files Browse the repository at this point in the history
  • Loading branch information
efmanu committed Sep 13, 2021
1 parent 88208e1 commit 7a06663
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 85 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
# Dash Optim App
Web app to optimize various functions with the help of Dash.jl and Optim.jl
# Turing Dash App
Web app for Bayesian inference using Dash.jl and Turing.jl

NB: Only functions with 2 parameters

### Run DashOptim App
### Run TuringDash App
From the project folder, enter the following commands in Julia REPL
```julia
julia>using Pkg
julia>Pkg.activate(".")
julia>include("run.jl")
```
##### Step 1: Start Julia with project environment
![1](https://user-images.githubusercontent.com/22251968/132993635-4f1d52ce-07f7-404b-b442-3b98edb06f43.PNG)
##### Step 2: Run the app
![2](https://user-images.githubusercontent.com/22251968/132993640-8be6b1f4-1120-48b7-a422-ab123efb0b60.PNG)
##### Step 2: Open App in browser
![3](https://user-images.githubusercontent.com/22251968/132993642-e3b82795-1b00-4369-8115-4aad2e44f27b.PNG)
##### Step 2: Configurations for optimization
![Animation](https://user-images.githubusercontent.com/22251968/132994748-f696aee1-dc4c-43b4-8275-e78f1c5ce061.gif)

##### Step 2: Configurations for sampling
![Animation4](https://user-images.githubusercontent.com/22251968/133113047-30aae141-bd81-417d-bde5-832ac4de3797.gif)

### Deploy in Herokuapp
From the project folder, enter the following commands in command line/terminal etc.
Expand Down
77 changes: 42 additions & 35 deletions TuringDash/src/TuringDash.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using PlotlyJS
using Turing
using Random
using CSV, DataFrames
using Base64

#Initialize channel to get data during optimization
chn = Base.Channel{Vector{Float64}}(Inf)
Expand All @@ -18,6 +19,7 @@ y_graph1 = []
itr = 0
eval_f = nothing
range_value = []
df = nothing

function make_app()
global chn, x_graph, y_graph, itr, eval_f
Expand All @@ -28,6 +30,7 @@ function make_app()
y_graph = []
y_graph1 = []
itr = 0
df = nothing

external_stylesheets = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
app = dash(external_stylesheets=[dbc_themes.BOOTSTRAP,external_stylesheets],
Expand All @@ -45,34 +48,37 @@ function make_app()
State("dtu-user-func-init", "value"),
State("dtu-upload-datan", "filename")
) do n_clicks, contents, drop_val, user_funcs, funcs_init, filename
global chn
global chn, x_graph, y_graph, y_graph1, itr, df
if (contents isa Nothing) && (drop_val == "0")
throw(PreventUpdate())
else
if contents isa Nothing
df = load_default(drop_val)
else
df = parse_contents(contents, filename)
end
end
ctx = callback_context()
if isempty(ctx.triggered)
throw(PreventUpdate())
end
if !(ctx.triggered[1].prop_id == "dtu-btn-turing.n_clicks")
throw(PreventUpdate())
end
if (contents isa Nothing) && (drop_val == "0")
throw(PreventUpdate())
end

if (user_funcs == "") || (funcs_init == "")
throw(PreventUpdate())
end
global chn
global chn, x_graph, y_graph, y_graph1, itr

x_graph = []
y_graph = []
y_graph1 = []
itr = 0
if contents isa Nothing
df = load_default(drop_val)
else
df = parse_contents(contents, filename)
end

for colname in Symbol.(names(df))
@eval $colname = df.$colname
end

end
#evealuate model
eval(Meta.parse(user_funcs))

Expand All @@ -85,15 +91,15 @@ function make_app()
nperiteration = 5
nsamples = 1000
@async begin
r = sample(rng, model, spl, nperiteration; chain_type=MCMCChains.Chains, save_state=true)
r = sample(rng, model, alg, nperiteration; chain_type=MCMCChains.Chains, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
for i in (nperiteration + 1):nperiteration:nsamples
r = Turing.Inference.resume(r, nperiteration, save_state=true)
r = Turing.Inference.resume(r, nperiteration, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
end
end
@show nsamples
return "render_liveupdates()"
# @show nsamples
return render_liveupdates()
end

callback!(app,
Expand All @@ -108,31 +114,32 @@ function make_app()
global x_graph, y_graph, y_graph1, chn, itr
if isready(chn)
val = take!(chn)
itr += 1
itr += 5
append!(x_graph, itr)
append!(y_graph, val[1])
append!(y_graph1, val[2])
else
st = true
end
return Dict(
"data" => [
Dict(
"x" => x_graph,
"y" => y_graph,
"mode" => "line",
)
]
),
Dict(
"data" => [
Dict(
"x" => x_graph,
"y" => y_graph1,
"mode" => "line",
)
]
), st
scatter_plt1 = scatter(
;x=x_graph, y=y_graph,
mode="lines", line_color="blue"
)
scatter_plt2 = scatter(
;x=x_graph, y=y_graph1,
mode="lines", line_color="blue"
)
fig1 = PlotlyJS.plot([scatter_plt1], Layout(
title="Trace Plot: Var 1",
xaxis_title="Iteration",
yaxis_title="Value",
))
fig2 = PlotlyJS.plot([scatter_plt2], Layout(
title="Trace Plot: Var 2",
xaxis_title="Iteration",
yaxis_title="Value",
))
return fig1, fig2, st
end
return app
end
Expand Down
10 changes: 5 additions & 5 deletions TuringDash/src/render_funcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ function render_uploader()
id="dtu-def-data-drop",
options =[
Dict("label" => "Select Data", "value" => "0"),
Dict("label" => "Real Estate", "value" => "1"),
Dict("label" => "Solar", "value" => "2"),
Dict("label" => "House Price", "value" => "3")
Dict("label" => "Data 1", "value" => "1"),
Dict("label" => "Data 2", "value" => "2"),
Dict("label" => "Data 3", "value" => "3")
],
value="0"
)
Expand Down Expand Up @@ -128,11 +128,11 @@ function render_liveupdates()
html_h3("Plots"),
dcc_interval(
id="interval-component",
interval=1000, # in milliseconds
interval=500, # in milliseconds
n_intervals=0,
disabled=false
),
dcc_graph(id="dtu-params-state2",
dcc_graph(id="dtu-params-state1",
figure=Dict(
"data" => [
Dict("x" => x_graph, "y" => y_graph),
Expand Down
6 changes: 3 additions & 3 deletions TuringDash/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ function parse_contents(contents, filename)

function load_default(val)
if val == "1"
df = CSV.read(download("https://raw.githubusercontent.com/efmanu/fluxdash/master/FluxDash/datasets/real_estate.csv"), DataFrame)
df = CSV.read(download("https://raw.githubusercontent.com/efmanu/TuringDashApp.jl/master/TuringDash/datasets/data1.csv"), DataFrame)
elseif val =="2"
df = CSV.read(download("https://raw.githubusercontent.com/efmanu/fluxdash/master/FluxDash/datasets/real_estate.csv"), DataFrame)
df = CSV.read(download("https://raw.githubusercontent.com/efmanu/TuringDashApp.jl/master/TuringDash/datasets/data2.csv"), DataFrame)
else
df = CSV.read(download("https://raw.githubusercontent.com/efmanu/fluxdash/master/FluxDash/datasets/real_estate.csv"), DataFrame)
df = CSV.read(download("https://raw.githubusercontent.com/efmanu/TuringDashApp.jl/master/TuringDash/datasets/data3.csv"), DataFrame)
end
return df
end
63 changes: 34 additions & 29 deletions precompile_funcs.jl
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
module PrecompileFuncs
using OptimDash
using OptimDash.Optim
using OptimDash.PlotlyJS
using TuringDash
using TuringDash.Turing
using TuringDash.Random
using TuringDash.CSV, TuringDash.DataFrames

function precompile_optim()
function precompile_turing()
chn = Base.Channel{Vector{Float64}}(Inf)
user_funcs = "f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2"
funcs_init = "[0.0, 0.0]"
lb_str = "[-2.0, -2.0]"
ub_str = "[2.0, 2.0]"
eval_f, init_f, lb_f, ub_f = OptimDash.eval_definition(
user_funcs, funcs_init, lb_str, ub_str
)
function g(f)
function _g(x)
put!(chn, x)
return Base.invokelatest(f, x)
df1 = nothing
global df1
df1 = CSV.read(download("https://raw.githubusercontent.com/efmanu/TuringDashApp.jl/master/TuringDash/datasets/data1.csv"), DataFrame)
model_str1 = """
@model turingmodel(x, y) = begin
a ~ Normal()
b ~ Normal()
for i in 1:length(x)
y[i] ~ Normal(a + b * x[i], 1.0)
end
return _g
end
results = optimize(g(eval_f), init_f)
"""
model_str2 = "turingmodel(x, y)"
for colname in Symbol.(names(df1))
@eval $colname = df1.$colname
end

bound_range = (ub_f - lb_f)./1000
range_value = [collect(1:10) for _ in 1:2]
z = rand(1:10,10,10)
heat_plt = heatmap(
x = range_value[1], y = range_value[2],
z = z
)
# scatter_plt = scatter(
# ;x=x_graph, y=y_graph,
# mode="lines", line_color="black"
# )
fig = OptimDash.PlotlyJS.plot(heat_plt)
eval(Meta.parse(model_str1))
model = eval(Meta.parse(model_str2))

alg = MH()
rng = Random.GLOBAL_RNG

nperiteration = 50
nsamples = 20

r = sample(rng, model, alg, nperiteration; chain_type=MCMCChains.Chains, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
for i in (nperiteration + 1):nperiteration:nsamples
r = Turing.Inference.resume(r, nperiteration, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
end
return "success"
end
end
2 changes: 1 addition & 1 deletion run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using TuringDash

# using .PrecompileFuncs
# PrecompileFuncs.precompile_optim()
# PrecompileFuncs.precompile_turing()

app = TuringDash.make_app()

Expand Down

0 comments on commit 7a06663

Please sign in to comment.