Skip to content

Commit 2b4c550

Browse files
committed
Improve/fix docstring for VarName (#213)
Couple of improvements in line with TuringLang/Turing.jl#1527.
1 parent fac4515 commit 2b4c550

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
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.10.6"
3+
version = "0.10.7"
44

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

src/varname.jl

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
"""
2-
```
3-
struct VarName{sym, T<:Tuple}
4-
indexing::T
5-
end
6-
```
2+
VarName(sym[, indexing=()])
3+
4+
A variable identifier for a symbol `sym` and indices `indexing` in the format
5+
returned by [`@vinds`](@ref).
6+
7+
The Julia variable in the model corresponding to `sym` can refer to a single value or to a
8+
hierarchical array structure of univariate, multivariate or matrix variables. The field `indexing`
9+
stores the indices requires to access the random variable from the Julia variable indicated by `sym`
10+
as a tuple of tuples. Each element of the tuple thereby contains the indices of one indexing
11+
operation.
12+
13+
`VarName`s can be manually constructed using the `VarName(sym, indexing)` constructor, or from an
14+
indexing expression through the [`@varname`](@ref) convenience macro.
15+
16+
# Examples
717
8-
A variable identifier. Every variable has a symbol `sym` and indices `indexing` in the format
9-
returned by [`@vinds`](@ref). The Julia variable in the model corresponding to `sym` can refer to a
10-
single value or to a hierarchical array structure of univariate, multivariate or matrix
11-
variables. `indexing` stores the indices that can access the random variable from the Julia
12-
variable.
18+
```jldoctest
19+
julia> vn = VarName(:x, ((Colon(), 1), (2,)))
20+
x[Colon(),1][2]
1321
14-
Examples:
22+
julia> vn.indexing
23+
((Colon(), 1), (2,))
1524
16-
- `x[1] ~ Normal()` will generate a `VarName` with `sym == :x` and `indexing == "((1,))"`.
17-
- `x[:,1] ~ MvNormal(zeros(2))` will generate a `VarName` with `sym == :x` and
18-
`indexing == ((Colon(), 1))"`.
19-
- `x[:,1][2] ~ Normal()` will generate a `VarName` with `sym == :x` and
20-
`indexing == ((Colon(), 1), (2,))`.
25+
julia> VarName(DynamicPPL.@vsym(x[:, 1][1+1]), DynamicPPL.@vinds(x[:, 1][1+1]))
26+
x[Colon(),1][2]
27+
```
2128
"""
2229
struct VarName{sym, T<:Tuple}
2330
indexing::T
@@ -26,7 +33,7 @@ end
2633
VarName(sym::Symbol, indexing::Tuple = ()) = VarName{sym, typeof(indexing)}(indexing)
2734

2835
"""
29-
VarName(vn::VarName, indexing)
36+
VarName(vn::VarName[, indexing=()])
3037
3138
Return a copy of `vn` with a new index `indexing`.
3239
"""
@@ -131,8 +138,29 @@ _issubrange(i::Colon, j::ConcreteIndex) = true
131138
"""
132139
@varname(expr)
133140
134-
A macro that returns an instance of `VarName` given the symbol or expression of a Julia variable,
135-
e.g. `@varname x[1,2][1+5][45][3]` returns `VarName{:x}(((1, 2), (6,), (45,), (3,)))`.
141+
A macro that returns an instance of [`VarName`](@ref) given a symbol or indexing expression `expr`.
142+
143+
The `sym` value is taken from the actual variable name, and the index values are put appropriately
144+
into the constructor (and resolved at runtime).
145+
146+
# Examples
147+
148+
```jldoctest
149+
julia> @varname(x).indexing
150+
()
151+
152+
julia> @varname(x[1]).indexing
153+
((1,),)
154+
155+
julia> @varname(x[:, 1]).indexing
156+
((Colon(), 1),)
157+
158+
julia> @varname(x[:, 1][2]).indexing
159+
((Colon(), 1), (2,))
160+
161+
julia> @varname(x[1,2][1+5][45][3]).indexing
162+
((1, 2), (6,), (45,), (3,))
163+
```
136164
137165
!!! compat "Julia 1.5"
138166
Using `begin` in an indexing expression to refer to the first index requires at least

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
44
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
55
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
66
DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c"
7+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
78
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
89
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
910
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
@@ -19,6 +20,7 @@ AbstractMCMC = "2.1"
1920
Bijectors = "0.8.2"
2021
Distributions = "0.24"
2122
DistributionsAD = "0.6.3"
23+
Documenter = "0.26.1"
2224
ForwardDiff = "0.10.12"
2325
MCMCChains = "4.0.4"
2426
MacroTools = "0.5.5"

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using AbstractMCMC
33
using Bijectors
44
using Distributions
55
using DistributionsAD
6+
using Documenter
67
using ForwardDiff
78
using MacroTools
89
using MCMCChains
@@ -59,4 +60,16 @@ include("test_util.jl")
5960
include(joinpath("turing", "runtests.jl"))
6061
end
6162
end
63+
64+
@testset "doctests" begin
65+
DocMeta.setdocmeta!(
66+
DynamicPPL,
67+
:DocTestSetup,
68+
quote
69+
using DynamicPPL
70+
end;
71+
recursive=true,
72+
)
73+
doctest(DynamicPPL; manual=false)
74+
end
6275
end

0 commit comments

Comments
 (0)