Skip to content

Commit

Permalink
a few changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed82008 committed Sep 13, 2021
1 parent 7a06663 commit d411f55
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 29 deletions.
26 changes: 25 additions & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ git-tree-sha1 = "ed720e2622820bf584d4ad90e6fcb93d95170b44"
uuid = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"
version = "0.1.3"

[[CSV]]
deps = ["Dates", "Mmap", "Parsers", "PooledArrays", "SentinelArrays", "Tables", "Unicode"]
git-tree-sha1 = "b83aa3f513be680454437a0eee21001607e5d983"
uuid = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
version = "0.8.5"

[[ChainRules]]
deps = ["ChainRulesCore", "Compat", "LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "0902fc7f416c8f1e3b1e014786bb65d0c2241a9b"
Expand Down Expand Up @@ -269,6 +275,12 @@ git-tree-sha1 = "bec2532f8adb82005476c141ec23e921fc20971b"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.8.0"

[[DataFrames]]
deps = ["Compat", "DataAPI", "Future", "InvertedIndices", "IteratorInterfaceExtensions", "LinearAlgebra", "Markdown", "Missings", "PooledArrays", "PrettyTables", "Printf", "REPL", "Reexport", "SortingAlgorithms", "Statistics", "TableTraits", "Tables", "Unicode"]
git-tree-sha1 = "d785f42445b63fc86caa08bb9a9351008be9b765"
uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "1.2.2"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "7d9d316f04214f7efdbb6398d545446e246eff02"
Expand Down Expand Up @@ -835,6 +847,12 @@ git-tree-sha1 = "371a19bb801c1b420b29141750f3a34d6c6634b9"
uuid = "1d0040c9-8b98-4ee7-8388-3f51789ca0ad"
version = "0.1.0"

[[PooledArrays]]
deps = ["DataAPI", "Future"]
git-tree-sha1 = "a193d6ad9c45ada72c14b731a318bedd3c2f00cf"
uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
version = "1.3.0"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a"
Expand Down Expand Up @@ -959,6 +977,12 @@ git-tree-sha1 = "9c1a0dea3b442024c54ca6a318e8acf842eab06f"
uuid = "30f210dd-8aff-4c5f-94ba-8e64358c1161"
version = "2.2.0"

[[SentinelArrays]]
deps = ["Dates", "Random"]
git-tree-sha1 = "54f37736d8934a12a200edea2f9206b03bdf3159"
uuid = "91c51154-3ec4-41a3-a24f-3f23e20d615c"
version = "1.3.7"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

Expand Down Expand Up @@ -1119,7 +1143,7 @@ uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.18.0"

[[TuringDash]]
deps = ["Dash", "DashBootstrapComponents", "DashCoreComponents", "DashHtmlComponents", "PlotlyJS", "Turing"]
deps = ["CSV", "Dash", "DashBootstrapComponents", "DashCoreComponents", "DashHtmlComponents", "DataFrames", "PlotlyJS", "Random", "Turing"]
path = "TuringDash"
uuid = "ff11d773-741b-488a-a49d-ae0d842bee15"
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
TuringDash = "ff11d773-741b-488a-a49d-ae0d842bee15"
22 changes: 12 additions & 10 deletions TuringDash/src/TuringDash.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module TuringDash


include("render_funcs.jl")
include("utils.jl")
using Dash, DashBootstrapComponents
Expand Down Expand Up @@ -31,7 +30,8 @@ function make_app()
y_graph1 = []
itr = 0
df = nothing

nperiteration = 1

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],
suppress_callback_exceptions=true)
Expand Down Expand Up @@ -70,30 +70,32 @@ function make_app()
if (user_funcs == "") || (funcs_init == "")
throw(PreventUpdate())
end

x_graph = []
y_graph = []
y_graph1 = []
itr = 0

for colname in Symbol.(names(df))
@eval $colname = df.$colname
end
#evealuate model
end
# evealuate model
eval(Meta.parse(user_funcs))

#evaluate eval_definition
model = eval(Meta.parse(funcs_init))

alg = MH()
alg = MH(
[0.25 0.05;
0.05 0.50]
)
rng = Random.GLOBAL_RNG

nperiteration = 5
nsamples = 1000
@async begin
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
for _ in (nperiteration + 1):nperiteration:nsamples
r = Turing.Inference.resume(r, nperiteration, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
end
Expand All @@ -114,7 +116,7 @@ function make_app()
global x_graph, y_graph, y_graph1, chn, itr
if isready(chn)
val = take!(chn)
itr += 5
itr += nperiteration
append!(x_graph, itr)
append!(y_graph, val[1])
append!(y_graph1, val[2])
Expand Down
20 changes: 10 additions & 10 deletions TuringDash/src/render_funcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function render_all()
dbc_row([
render_func_init(),
dbc_col([
dbc_button("Sample !", color="success", id="dtu-btn-turing")
dbc_button("Sample", color="success", id="dtu-btn-turing")
], md =2),
render_plots()
])
Expand All @@ -17,12 +17,12 @@ function render_info()
dbc_jumbotron([
html_h1("Dash Turing App", className="dtu-display-3"),
html_p(
"Web app for Bayesian inference using Dash.jl and Turing.jl ",
"Web app for Bayesian inference using Dash.jl and Turing.jl ",
className="lead",
),
html_hr(className="my-2"),
html_p(
"User can define Turing model",
"User can define a Turing model. The dataset's column names are available as variable names.",
)
])
])
Expand All @@ -39,18 +39,18 @@ end
function render_function()
return dbc_card([
dbc_cardbody([
html_h4("Model specification", className="card-title"),
html_h4("Model specification:", className="card-title"),
html_div([
dbc_textarea(
id = "dtu-user-funcs",
bs_size="lg",
className="mb-3",
rows = 6,
value="""
@model turingmodel(x, y) = begin
@model regression_model(x, y) = begin
a ~ Normal()
b ~ Normal()
for i in 1:length(x)
for i in 1:10
y[i] ~ Normal(a + b * x[i], 1.0)
end
end
Expand All @@ -63,13 +63,13 @@ end
function render_init()
return dbc_card([
dbc_cardbody([
html_h4("Conditional Model", className="card-title"),
html_h4("Conditional model:", className="card-title"),
html_div([
dbc_input(
id = "dtu-user-func-init",
bs_size="lg",
className="mb-3",
value="turingmodel(x, y)",
value="regression_model(x, y)",
)
]),
])
Expand All @@ -83,7 +83,7 @@ function render_uploader()
html_div(
id="dtu-def-upload-data",
[
html_label("Select a default dataset:")
html_label("Select a dataset:")
dcc_dropdown(
id="dtu-def-data-drop",
options =[
Expand All @@ -96,7 +96,7 @@ function render_uploader()
)
],
),
html_h3("OR"),
html_h3("or"),
dcc_upload(
id="dtu-upload-datan",
children=html_div([
Expand Down
21 changes: 13 additions & 8 deletions precompile_funcs.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
module PrecompileFuncs
using TuringDash
using TuringDash.Turing
using TuringDash.Random
using TuringDash.CSV, TuringDash.DataFrames

using TuringDash
using TuringDash.Turing
using TuringDash.Random
using TuringDash.CSV, TuringDash.DataFrames

function precompile_turing()
chn = Base.Channel{Vector{Float64}}(Inf)
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
@model regression_model(x, y) = begin
a ~ Normal()
b ~ Normal()
for i in 1:length(x)
for i in 1:10
y[i] ~ Normal(a + b * x[i], 1.0)
end
end
"""
model_str2 = "turingmodel(x, y)"
model_str2 = "regression_model(x, y)"
for colname in Symbol.(names(df1))
@eval $colname = df1.$colname
end

eval(Meta.parse(model_str1))
model = eval(Meta.parse(model_str2))

alg = MH()
alg = MH(
[0.25 0.05;
0.05 0.50]
)
rng = Random.GLOBAL_RNG

nperiteration = 50
Expand All @@ -40,4 +44,5 @@ function precompile_turing()
end
return "success"
end

end

0 comments on commit d411f55

Please sign in to comment.