|
1 | 1 | """
|
2 |
| - struct Model{G, Targs<:NamedTuple, Tmissings <: Val} |
3 |
| - args::Targs |
4 |
| - missings::Tmissings |
| 2 | + struct Model{G, argnames, missings, Targs} |
| 3 | + args::NamedTuple{argnames, Targs} |
5 | 4 | end
|
6 | 5 |
|
7 |
| -A `Model` struct with arguments `args`, model generator type `G` and |
8 |
| -missing data `missings`. `missings` is a `Val` instance, e.g. `Val{(:a, :b)}()`. An |
9 |
| -argument in `args` with a value `missing` will be in `missings` by default. However, in |
10 |
| -non-traditional use-cases `missings` can be defined differently. All variables in |
11 |
| -`missings` are treated as random variables rather than observations. |
| 6 | +A `Model` struct with model generator type `G`, arguments names `argnames`, arguments types `Targs`, |
| 7 | +and missing arguments `missings`. `argnames` and `missings` are tuples of symbols, e.g. `(:a, |
| 8 | +:b)`. An argument with a type of `Missing` will be in `missings` by default. However, in |
| 9 | +non-traditional use-cases `missings` can be defined differently. All variables in `missings` are |
| 10 | +treated as random variables rather than observations. |
| 11 | +
|
| 12 | +# Example |
| 13 | +
|
| 14 | +```julia |
| 15 | +julia> Model{typeof(gdemo)}((x = 1.0, y = 2.0)) |
| 16 | +Model{typeof(gdemo),(),(:x, :y),Tuple{Float64,Float64}}((x = 1.0, y = 2.0)) |
| 17 | +
|
| 18 | +julia> Model{typeof(gdemo), (:y,)}((x = 1.0, y = 2.0)) |
| 19 | +Model{typeof(gdemo),(:y,),(:x, :y),Tuple{Float64,Float64}}((x = 1.0, y = 2.0)) |
| 20 | +``` |
12 | 21 | """
|
13 |
| -struct Model{G, Targs<:NamedTuple, Tmissings<:Val} <: AbstractModel |
14 |
| - args::Targs |
15 |
| - missings::Tmissings |
| 22 | +struct Model{G, argnames, missings, Targs} <: AbstractModel |
| 23 | + args::NamedTuple{argnames, Targs} |
| 24 | + |
| 25 | + Model{G, missings}(args::NamedTuple{argnames, Targs}) where {G, argnames, missings, Targs} = |
| 26 | + new{G, argnames, missings, Targs}(args) |
| 27 | +end |
| 28 | + |
| 29 | +@generated function Model{G}(args::NamedTuple{argnames, Targs}) where {G, argnames, Targs} |
| 30 | + missings = Tuple(name for (name, typ) in zip(argnames, Targs.types) if typ <: Missing) |
| 31 | + return :(Model{G, $missings}(args)) |
16 | 32 | end
|
17 | 33 |
|
18 |
| -Model{G}(args::Targs, missings::Tmissings) where {G, Targs, Tmissings} = |
19 |
| - Model{G, Targs, Tmissings}(args, missings) |
20 | 34 |
|
21 | 35 | (model::Model)(vi) = model(vi, SampleFromPrior())
|
22 | 36 | (model::Model)(vi, spl) = model(vi, spl, DefaultContext())
|
23 | 37 |
|
24 | 38 |
|
25 | 39 | """
|
26 |
| - getgenerator(model::Model) |
| 40 | + getargnames(model::Model) |
27 | 41 |
|
28 |
| -Get the generator (the function defined by the `@model` macro) of a certain model instance. |
| 42 | +Get a tuple of the argument names of the `model`. |
29 | 43 | """
|
30 |
| -function getgenerator end |
| 44 | +getargnames(model::Model) = getargnames(typeof(model)) |
| 45 | +getargnames(::Type{<:Model{_G, argnames} where {_G}}) where {argnames} = argnames |
| 46 | + |
31 | 47 |
|
32 | 48 | """
|
33 |
| - getmodeltype(::typeof(modelgen)) |
| 49 | + getmissings(model::Model) |
34 | 50 |
|
35 |
| -Get the associated model type for a model generating function. |
| 51 | +Get a tuple of the names of the missing arguments of the `model`. |
36 | 52 | """
|
37 |
| -function getmodeltype end |
| 53 | +getmissings(model::Model{_G, _a, missings}) where {missings, _G, _a} = missings |
| 54 | + |
| 55 | +getmissing(model::Model) = getmissings(model) |
| 56 | +@deprecate getmissing(model) getmissings(model) |
| 57 | + |
38 | 58 |
|
39 | 59 | """
|
40 |
| - getdefaults(::typeof(modelgen)) |
| 60 | + getgenerator(model::Model) |
41 | 61 |
|
42 |
| -Get a named tuple of the default argument values defined for a model defined by a generating function. |
| 62 | +Get the generator (the function defined by the `@model` macro) of a certain model instance. |
43 | 63 | """
|
44 |
| -function getdefaults end |
| 64 | +getgenerator(model::Model) = getgenerator(typeof(model)) |
| 65 | + |
45 | 66 |
|
46 | 67 | """
|
47 |
| - getargnames(::typeof(modelgen)) |
| 68 | + getdefaults(model::Model) |
48 | 69 |
|
49 |
| -Get a tuple of the argument names of the model defined by a generating function. |
| 70 | +Get a named tuple of the default argument values defined for a model defined by a generating function. |
50 | 71 | """
|
51 |
| -function getargnames end |
| 72 | +getdefaults(model::Model) = getdefaults(typeof(model)) |
52 | 73 |
|
53 | 74 |
|
54 |
| -getmissing(model::Model) = model.missings |
55 |
| -@generated function getmissing(args::NamedTuple{names, ttuple}) where {names, ttuple} |
56 |
| - length(names) == 0 && return :(Val{()}()) |
57 |
| - minds = filter(1:length(names)) do i |
58 |
| - ttuple.types[i] == Missing |
59 |
| - end |
60 |
| - mnames = names[minds] |
61 |
| - return :(Val{$mnames}()) |
62 |
| -end |
| 75 | +""" |
| 76 | + getmodeltype(::typeof(modelgen)) |
| 77 | +
|
| 78 | +Get the associated model type for a model generator (the function defined by the `@model` macro). |
| 79 | +""" |
| 80 | +getmodeltype(model::Model) = typeof(model) |
0 commit comments