Skip to content

Commit 3402f78

Browse files
committed
finish dep graph docs
1 parent c44a35f commit 3402f78

File tree

2 files changed

+96
-26
lines changed

2 files changed

+96
-26
lines changed

docs/src/systems/DependencyGraphs.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ BipartiteGraph
99
```@docs
1010
equation_dependencies
1111
asgraph
12+
variable_dependencies
13+
asdigraph
14+
eqeq_dependencies
15+
varvar_dependencies
1216
```

src/systems/dependency_graphs.jl

+92-26
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ rxs = [Reaction(β, [S,I], [I], [1,1], [2]),
2323
rs = ReactionSystem(rxs, t, [S,I,R], [β,γ,κ,η])
2424
2525
# ODEs:
26-
os = convert(ODESystem, rs)
26+
odesys = convert(ODESystem, rs)
2727
2828
# dependency of each ODE on state variables
29-
equation_dependencies(os)
29+
equation_dependencies(odesys)
3030
3131
# dependency of each ODE on parameters
32-
equation_dependencies(os, variables=parameters(os))
32+
equation_dependencies(odesys, variables=parameters(odesys))
3333
3434
# Jumps
35-
js = convert(JumpSystem, rs)
35+
jumpsys = convert(JumpSystem, rs)
3636
3737
# dependency of each jump rate function on state variables
38-
equation_dependencies(js)
38+
equation_dependencies(jumpsys)
3939
4040
# dependency of each jump rate function on parameters
41-
equation_dependencies(js, variables=parameters(js))
41+
equation_dependencies(jumpsys, variables=parameters(jumpsys))
4242
```
4343
"""
4444
function equation_dependencies(sys::AbstractSystem; variables=states(sys))
@@ -104,7 +104,7 @@ asgraph(eqdeps, vtois)
104104
```
105105
106106
Convert a collection of equation dependencies, for example as returned by
107-
`equation_dependencies`, to a `BipartiteGraph`.
107+
`equation_dependencies`, to a [`BipartiteGraph`](@ref).
108108
109109
Notes:
110110
- `vtois` should provide `Dict` like mapping from variable dependency in `eqdeps`
@@ -113,7 +113,7 @@ Notes:
113113
Example:
114114
Continuing the example started in [`equation_dependencies`](@ref)
115115
```julia
116-
digr = asgraph(equation_dependencies(os), Dict(s => i for (i,s) in enumerate(states(os))))
116+
digr = asgraph(equation_dependencies(odesys), Dict(s => i for (i,s) in enumerate(states(odesys))))
117117
```
118118
"""
119119
function asgraph(eqdeps, vtois)
@@ -140,35 +140,47 @@ asgraph(sys::AbstractSystem; variables=states(sys),
140140
variablestoids=Dict(convert(Variable, v) => i for (i,v) in enumerate(variables)))
141141
```
142142
143-
Convert an `AbstractSystem` to a `BipartiteGraph` mapping equations
143+
Convert an `AbstractSystem` to a [`BipartiteGraph`](@ref) mapping equations
144144
to variables they depend on.
145145
146146
Notes:
147147
- Defaults for kwargs creating a mapping from `equations(sys)` to `states(sys)`
148148
they depend on.
149149
- `variables` should provide the list of variables to use for generating
150150
the dependency graph.
151-
- `variablestoids` should provide `Dict` like mapping from a variable to its
152-
integer index within `variables`.
151+
- `variablestoids` should provide `Dict` like mapping from a `Variable` to its
152+
`Int` index within `variables`.
153153
154154
Example:
155155
Continuing the example started in [`equation_dependencies`](@ref)
156156
```julia
157-
digr = asgraph(os)
157+
digr = asgraph(odesys)
158158
```
159159
"""
160160
function asgraph(sys::AbstractSystem; variables=states(sys),
161161
variablestoids=Dict(convert(Variable, v) => i for (i,v) in enumerate(variables)))
162162
asgraph(equation_dependencies(sys, variables=variables), variablestoids)
163163
end
164-
# function asgraph(sys::AbstractSystem; variables=nothing, variablestoids=nothing)
165-
# vs = isnothing(variables) ? states(sys) : variables
166-
# eqdeps = equation_dependencies(sys, variables=vs)
167-
# vtois = isnothing(variablestoids) ? Dict(convert(Variable, v) => i for (i,v) in enumerate(vs)) : variablestoids
168-
# asgraph(eqdeps, vtois)
169-
# end
170-
171-
# for each variable determine the equations that modify it
164+
165+
"""
166+
```julia
167+
variable_dependencies(sys::AbstractSystem; variables=states(sys), variablestoids=nothing)
168+
```
169+
170+
For each variable determine the equations that modify it and return as a [`BipartiteGraph`](@ref).
171+
172+
Notes:
173+
- Dependencies are returned as a [`BipartiteGraph`](@ref) mapping variable
174+
indices to the indices of equations that map to them.
175+
- `variables` denotes the list of variables to determine dependencies for.
176+
- `variablestoids` denotes a `Dict` mapping `Variable`s to `Int`s.
177+
178+
Example:
179+
Continuing the example of [`equation_dependencies`](@ref)
180+
```julia
181+
variable_dependencies(odesys)
182+
```
183+
"""
172184
function variable_dependencies(sys::AbstractSystem; variables=states(sys), variablestoids=nothing)
173185
eqs = equations(sys)
174186
vtois = isnothing(variablestoids) ? Dict(convert(Variable, v) => i for (i,v) in enumerate(variables)) : variablestoids
@@ -192,12 +204,30 @@ function variable_dependencies(sys::AbstractSystem; variables=states(sys), varia
192204
end
193205

194206
"""
207+
```julia
208+
asdigraph(g::BipartiteGraph, sys::AbstractSystem; variables = states(sys), equationsfirst = true)
209+
```
210+
211+
Convert a [`BipartiteGraph`](@ref) to a `LightGraph.SimpleDiGraph`.
212+
213+
Notes:
195214
- The resulting `SimpleDiGraph` unifies the two sets of vertices (equations
196-
and then states in the case `eqdeps` comes from `equation_dependencies`), producing
197-
one ordered set of integer vertices (as `SimpleDiGraph` does not support two distinct
198-
collections of nodes.
215+
and then states in the case it comes from [`asgraph`](@ref)), producing one
216+
ordered set of integer vertices (`SimpleDiGraph` does not support two distinct
217+
collections of vertices so they must be merged).
218+
- `variables` gives the variables that `g` is associated with (usually the
219+
`states` of a system).
220+
- `equationsfirst` (default is `true`) gives whether the [`BipartiteGraph`](@ref)
221+
gives a mapping from equations to variables they depend on (`true`), as calculated
222+
by [`asgraph`](@ref), or whether it gives a mapping from variables to the equations
223+
that modify them, as calculated by [`variable_dependencies`](@ref).
224+
225+
Example:
226+
Continuing the example in [`asgraph`](@ref)
227+
```julia
228+
dg = asdigraph(digr)
229+
```
199230
"""
200-
# convert BipartiteGraph to LightGraph.SimpleDiGraph
201231
function asdigraph(g::BipartiteGraph, sys::AbstractSystem; variables = states(sys), equationsfirst = true)
202232
neqs = length(equations(sys))
203233
nvars = length(variables)
@@ -217,7 +247,25 @@ function asdigraph(g::BipartiteGraph, sys::AbstractSystem; variables = states(sy
217247
SimpleDiGraph(g.ne, fadjlist, badjlist)
218248
end
219249

220-
# maps the i'th eq to equations that depend on it
250+
"""
251+
```julia
252+
eqeq_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer}
253+
```
254+
255+
Calculate a `LightGraph.SimpleDiGraph` that maps each equation to equations they depend on.
256+
257+
Notes:
258+
- The `fadjlist` of the `SimpleDiGraph` maps from an equation to the equations that
259+
modify variables it depends on.
260+
- The `badjlist` of the `SimpleDiGraph` maps from an equation to equations that
261+
depend on variables it modifies.
262+
263+
Example:
264+
Continuing the example of `equation_dependencies`
265+
```julia
266+
eqeqdep = eqeq_dependencies(asgraph(odesys), variable_dependencies(odesys))
267+
```
268+
"""
221269
function eqeq_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer}
222270
g = SimpleDiGraph{T}(length(eqdeps.fadjlist))
223271

@@ -232,5 +280,23 @@ function eqeq_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}
232280
g
233281
end
234282

235-
# maps the i'th variable to variables that depend on it
283+
"""
284+
```julia
285+
varvar_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer} = eqeq_dependencies(vardeps, eqdeps)
286+
```
287+
288+
Calculate a `LightGraph.SimpleDiGraph` that maps each variable to variables they depend on.
289+
290+
Notes:
291+
- The `fadjlist` of the `SimpleDiGraph` maps from a variable to the variables that
292+
depend on it.
293+
- The `badjlist` of the `SimpleDiGraph` maps from a variable to variables on which
294+
it depends.
295+
296+
Example:
297+
Continuing the example of `equation_dependencies`
298+
```julia
299+
varvardep = varvar_dependencies(asgraph(odesys), variable_dependencies(odesys))
300+
```
301+
"""
236302
varvar_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer} = eqeq_dependencies(vardeps, eqdeps)

0 commit comments

Comments
 (0)