Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning message for transformed data #217

Merged
merged 3 commits into from
Sep 29, 2024
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
2 changes: 1 addition & 1 deletion src/JuliaBUGS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function compute_data_transformation(
)
eval_env = create_eval_env(non_data_scalars, non_data_array_sizes, data)
has_new_val = true
pass = DataTransformation(eval_env, false)
pass = DataTransformation(eval_env, false, Ref(false))
while has_new_val
pass.new_value_added = false
analyze_block(pass, model_def)
Expand Down
15 changes: 14 additions & 1 deletion src/compiler_pass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,10 @@ to data.
mutable struct DataTransformation <: CompilerPass
env::NamedTuple
new_value_added::Bool
already_warned::Ref{Bool}
end

function analyze_statement(pass::DataTransformation, expr::Expr, loop_vars::NamedTuple)
function analyze_statement(pass::DataTransformation, expr::Expr, loop_vars)
if is_deterministic(expr)
lhs_expr, rhs_expr = expr.args[1], expr.args[2]
env = merge(pass.env, loop_vars)
Expand All @@ -510,6 +511,18 @@ function analyze_statement(pass::DataTransformation, expr::Expr, loop_vars::Name

rhs = evaluate(rhs_expr, env)
if is_resolved(rhs)
if !pass.already_warned[]
displayed_lhs =
lhs isa Symbol ? lhs : String(lhs[1]) * "[$(join(lhs[2:end], ", "))]"
@warn """
Variables that can be directly computed from data are considered 'transformed data' in BUGS.
These variables won't appear in the probabilistic graph.
In this model, '$(displayed_lhs)' is one such variable.
There may be others, but this warning is only shown once.
If you're not working with existing BUGS programs, consider pre-computing these variables in Julia for efficiency.
"""
pass.already_warned[] = true
end
pass.new_value_added = true
pass_env = pass.env
if lhs isa Symbol
Expand Down
6 changes: 3 additions & 3 deletions test/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ end

eval_env = JuliaBUGS.create_eval_env(non_data_scalars, non_data_array_sizes, data)

pass = DataTransformation(eval_env, false)
pass = DataTransformation(eval_env, false, Ref(false))
JuliaBUGS.analyze_block(pass, model_def)
(; new_value_added, env) = pass
@test new_value_added == true
@test env[:a] == 2

pass = DataTransformation(env, false)
pass = DataTransformation(env, false, Ref(true))
JuliaBUGS.analyze_block(pass, model_def)
(; new_value_added, env) = pass
@test new_value_added == true
@test env[:c] == 6

pass = DataTransformation(env, false)
pass = DataTransformation(env, false, Ref(true))
JuliaBUGS.analyze_block(pass, model_def)
(; new_value_added, env) = pass
@test new_value_added == false
Expand Down
Loading