Skip to content

Commit 37ec450

Browse files
committed
Save user-provided name in Model (#175)
This PR implements the functionality requested in TuringLang/Turing.jl#1429 and saves the user-provided name of a model in `Model`. It can be retrieved from a `Model` instance with `Base.nameof`.
1 parent 6547969 commit 37ec450

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.9.3"
3+
version = "0.9.4"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/compiler.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ function build_output(modelinfo)
343343
modeldef[:body] = quote
344344
$evaluator = $(combinedef_anonymous(evaluatordef))
345345
return $(DynamicPPL.Model)(
346-
$evaluator, $allargs_namedtuple, $defaults_namedtuple
346+
$(QuoteNode(modeldef[:name])),
347+
$evaluator,
348+
$allargs_namedtuple,
349+
$defaults_namedtuple,
347350
)
348351
end
349352

src/model.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
struct Model{F,argnames,defaultnames,missings,Targs,Tdefaults}
3+
name::Symbol
34
f::F
45
args::NamedTuple{argnames,Targs}
56
defaults::NamedTuple{defaultnames,Tdefaults}
@@ -32,39 +33,44 @@ Model{typeof(f),(:x, :y),(:x,),(:y,),Tuple{Float64,Float64},Tuple{Int64}}(f, (x
3233
```
3334
"""
3435
struct Model{F,argnames,defaultnames,missings,Targs,Tdefaults} <: AbstractModel
36+
name::Symbol
3537
f::F
3638
args::NamedTuple{argnames,Targs}
3739
defaults::NamedTuple{defaultnames,Tdefaults}
3840

3941
"""
40-
Model{missings}(f, args::NamedTuple, defaults::NamedTuple)
42+
Model{missings}(name::Symbol, f, args::NamedTuple, defaults::NamedTuple)
4143
42-
Create a model with evaluation function `f` and missing arguments overwritten by `missings`.
44+
Create a model of name `name` with evaluation function `f` and missing arguments
45+
overwritten by `missings`.
4346
"""
4447
function Model{missings}(
48+
name::Symbol,
4549
f::F,
4650
args::NamedTuple{argnames,Targs},
4751
defaults::NamedTuple{defaultnames,Tdefaults},
4852
) where {missings,F,argnames,Targs,defaultnames,Tdefaults}
49-
return new{F,argnames,defaultnames,missings,Targs,Tdefaults}(f, args, defaults)
53+
return new{F,argnames,defaultnames,missings,Targs,Tdefaults}(name, f, args, defaults)
5054
end
5155
end
5256

5357
"""
54-
Model(f, args::NamedTuple[, defaults::NamedTuple = ()])
58+
Model(name::Symbol, f, args::NamedTuple[, defaults::NamedTuple = ()])
5559
56-
Create a model with evaluation function `f` and missing arguments deduced from `args`.
60+
Create a model of name `name` with evaluation function `f` and missing arguments deduced
61+
from `args`.
5762
5863
Default arguments `defaults` are used internally when constructing instances of the same
5964
model with different arguments.
6065
"""
6166
@generated function Model(
67+
name::Symbol,
6268
f::F,
6369
args::NamedTuple{argnames,Targs},
6470
defaults::NamedTuple = NamedTuple(),
6571
) where {F,argnames,Targs}
6672
missings = Tuple(name for (name, typ) in zip(argnames, Targs.types) if typ <: Missing)
67-
return :(Model{$missings}(f, args, defaults))
73+
return :(Model{$missings}(name, f, args, defaults))
6874
end
6975

7076
"""
@@ -174,6 +180,13 @@ getmissings(model::Model{_F,_a,_d,missings}) where {missings,_F,_a,_d} = missing
174180
getmissing(model::Model) = getmissings(model)
175181
@deprecate getmissing(model) getmissings(model)
176182

183+
"""
184+
nameof(model::Model)
185+
186+
Get the name of the `model` as `Symbol`.
187+
"""
188+
Base.nameof(model::Model) = model.name
189+
177190
"""
178191
logjoint(model::Model, varinfo::AbstractVarInfo)
179192

src/prob_macro.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ end
171171
# `missings` is splatted into a tuple at compile time and inserted as literal
172172
return quote
173173
$(warnings...)
174-
Model{$(Tuple(missings))}(model.f, $(to_namedtuple_expr(argnames, argvals)),
175-
model.defaults)
174+
Model{$(Tuple(missings))}(
175+
model.name, model.f, $(to_namedtuple_expr(argnames, argvals)), model.defaults,
176+
)
176177
end
177178
end
178179

@@ -229,6 +230,7 @@ end
229230

230231
# `args` is inserted as properly typed NamedTuple expression;
231232
# `missings` is splatted into a tuple at compile time and inserted as literal
232-
return :(Model{$(Tuple(missings))}(model.f, $(to_namedtuple_expr(argnames, argvals)),
233-
model.defaults))
233+
return :(Model{$(Tuple(missings))}(
234+
model.name, model.f, $(to_namedtuple_expr(argnames, argvals)), model.defaults,
235+
))
234236
end

test/model.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,18 @@ Random.seed!(1234)
148148
# Ensure that they're not all the same (some can be, because rejected samples)
149149
@test any(res34[1:end - 1] .!= res34[2:end])
150150
end
151+
152+
@testset "nameof" begin
153+
@model function test1(x)
154+
m ~ Normal(0, 1)
155+
x ~ Normal(m, 1)
156+
end
157+
@model test2(x) = begin
158+
m ~ Normal(0, 1)
159+
x ~ Normal(m, 1)
160+
end
161+
162+
@test nameof(test1(rand())) == :test1
163+
@test nameof(test2(rand())) == :test2
164+
end
151165
end

0 commit comments

Comments
 (0)