-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvariable_ops.jl
More file actions
330 lines (272 loc) · 9.46 KB
/
Copy pathvariable_ops.jl
File metadata and controls
330 lines (272 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
$(SIGNATURES)
Add a VariableDFG to a DFG.
Implement `addVariable!(dfg::AbstractDFG, variable::AbstractGraphVariable)`
"""
function addVariable! end
"""
$(SIGNATURES)
Add a Vector{VariableDFG} to a DFG.
Implement `addVariables!(dfg::AbstractDFG, variables::Vector{<:AbstractGraphVariable})`
"""
function addVariables! end
"""
$(SIGNATURES)
Get a VariableDFG from a DFG using its label.
Implement `getVariable(dfg::AbstractDFG, label::Symbol)`
"""
function getVariable end
"""
$(SIGNATURES)
Get the variables in the DFG as a Vector, supporting various filters.
Keyword arguments
- `whereSolvable`: Optional function to filter on the `solvable` property, eg `>=(1)`.
- `whereLabel`: Optional function to filter on label e.g., `contains(r"x1")`.
- `whereTags`: Optional function to filter on tags, eg. `⊇([:POSE])`.
- `whereType`: Optional function to filter on the variable type.
Returns
- `Vector{<:AbstractGraphVariable}` matching the filters.
See also: [`listVariables`](@ref), [`ls`](@ref)
"""
function getVariables end
"""
$(SIGNATURES)
Merge a variable into the DFG. If a variable with the same label exists, it will be overwritten;
otherwise, the variable will be added to the graph.
Implement `mergeVariable!(dfg::AbstractDFG, variable::AbstractGraphVariable)`
"""
function mergeVariable! end
"""
$(SIGNATURES)
Merge a vector of variables into the DFG. If a variable with the same label exists, it will be overwritten; otherwise, the variable will be added to the graph.
Implement `mergeVariables!(dfg::AbstractDFG, variables::Vector{<:AbstractGraphVariable})`
"""
function mergeVariables! end
"""
$(SIGNATURES)
Delete a VariableDFG from the DFG.
Implement `deleteVariable!(dfg::AbstractDFG, label::Symbol)`
"""
function deleteVariable! end
"""
$(SIGNATURES)
Get a list of labels of the DFGVariables in the graph.
Supports optional arguments to filter the variables returned.
Notes
- Returns `::Vector{Symbol}`
Example
```julia
listVariables(dfg)
```
See also: [`ls`](@ref)
"""
function listVariables end
"""
$(SIGNATURES)
True if the variable exists in the graph.
Implement `hasVariable(dfg::AbstractDFG, label::Symbol)`
"""
function hasVariable end
# ==============================================================================
"""
$(SIGNATURES)
Get a VariableSummary from a DFG.
"""
function getVariableSummary end
"""
$(SIGNATURES)
Get the variables from a DFG as a Vector{VariableSummary}.
"""
function getVariablesSummary end
"""
$(SIGNATURES)
Get a VariableSkeleton from a DFG.
"""
function getVariableSkeleton end
"""
$(SIGNATURES)
Get the variables from a DFG as a Vector{VariableSkeleton}.
"""
function getVariablesSkeleton end
# =============================================================================
function getVariables(dfg::AbstractDFG, labels::Vector{Symbol})
return map(label -> getVariable(dfg, label), labels)
end
function deleteVariable!(dfg::AbstractDFG, variable::AbstractGraphVariable)
return deleteVariable!(dfg, variable.label)
end
function deleteVariables!(dfg::AbstractDFG, labels::Vector{Symbol})
counts = asyncmap(labels) do l
return deleteVariable!(dfg, l)
end
return sum(counts; init = 0)
end
function deleteVariables!(dfg::AbstractDFG; kwargs...)
labels = listVariables(dfg; kwargs...)
return deleteVariables!(dfg, labels)
end
# =============================================================================
# TODO SORT OUT BELLOW
# =============================================================================
"""
$(SIGNATURES)
Get the kind of the variable's state, eg. `Pose2`, `Point3`, etc. as an instance of `StateType`.
"""
getStateKind(::VariableDFG{T}) where {T} = T()
getStateKind(::State{T}) where {T} = T()
getStateKind(dfg::AbstractDFG, lbl::Symbol) = getStateKind(getVariable(dfg, lbl))
#TODO why this convert? rather enforce explicit use of getManifold
function Base.convert(
::Type{<:AbstractManifold},
::Union{<:T, Type{<:T}},
) where {T <: StateType}
#TODO Deprecate v0.29
Base.depwarn(
"convert(AbstractManifold, StateType) is deprecated, use getManifold instead",
:convert,
)
return getManifold(T)
end
"""
$SIGNATURES
Interface function to return the `<:ManifoldsBase.AbstractManifold` object of `variableType<:StateType`.
"""
getManifold(::T) where {T <: StateType} = getManifold(T)
getManifold(vari::VariableDFG) = getStateKind(vari) |> getManifold
getManifold(state::State) = getStateKind(state) |> getManifold
# covers both <:StateType and <:AbstractObservation
getManifold(dfg::AbstractDFG, lbl::Symbol) = getManifold(dfg[lbl])
"""
$SIGNATURES
Interface function to return the `variableType` dimension of an StateType, extend this function for all Types<:StateType.
"""
function getDimension end
getDimension(::Type{T}) where {T <: StateType} = manifold_dimension(getManifold(T))
getDimension(::T) where {T <: StateType} = manifold_dimension(getManifold(T))
getDimension(M::ManifoldsBase.AbstractManifold) = manifold_dimension(M)
getDimension(p::Distributions.Distribution) = length(p)
getDimension(var::VariableDFG) = getDimension(getStateKind(var))
"""
$SIGNATURES
Interface function to return the manifold point type of an StateType, extend this function for all Types<:StateType.
"""
function getPointType end
getPointType(::T) where {T <: StateType} = getPointType(T)
"""
$SIGNATURES
Interface function to return the user provided identity point for this StateType manifold, extend this function for all Types<:StateType.
Notes
- Used in transition period for Serialization. This function will likely be changed or deprecated entirely.
"""
function getPointIdentity end
getPointIdentity(::T) where {T <: StateType} = getPointIdentity(T)
##------------------------------------------------------------------------------
## solvedCount
##------------------------------------------------------------------------------
"""
$SIGNATURES
Get the number of times a variable has been inferred -- i.e. `solvedCount`.
Related
isSolved, setSolvedCount!
"""
getSolvedCount(v::State) = v.solves
function getSolvedCount(v::VariableDFG, solveKey::Symbol = :default)
if hasState(v, solveKey)
return getState(v, solveKey) |> getSolvedCount
else
return 0
end
end
function getSolvedCount(dfg::AbstractDFG, sym::Symbol, solveKey::Symbol = :default)
return getSolvedCount(getVariable(dfg, sym), solveKey)
end
"""
$SIGNATURES
Update/set the `solveCount` value.
Related
getSolved, isSolved
"""
setSolvedCount!(v::State, val::Int) = v.solves = val
function setSolvedCount!(v::VariableDFG, val::Int, solveKey::Symbol = :default)
return setSolvedCount!(getState(v, solveKey), val)
end
function setSolvedCount!(
dfg::AbstractDFG,
sym::Symbol,
val::Int,
solveKey::Symbol = :default,
)
return setSolvedCount!(getVariable(dfg, sym), val, solveKey)
end
"""
$SIGNATURES
Boolean on whether the variable has been solved.
Related
getSolved, setSolved!
"""
isSolved(v::State) = 0 < v.solves
function isSolved(v::VariableDFG, solveKey::Symbol = :default)
if hasState(v, solveKey)
return getState(v, solveKey) |> isSolved
else
return false
end
end
function isSolved(dfg::AbstractDFG, sym::Symbol, solveKey::Symbol = :default)
return isSolved(getVariable(dfg, sym), solveKey)
end
##------------------------------------------------------------------------------
## initialized
##------------------------------------------------------------------------------
"""
$SIGNATURES
Returns state of variable data `.initialized` flag.
Notes:
- used by both factor graph variable and Bayes tree clique logic.
"""
function isInitialized(var::VariableDFG, key::Symbol = :default)
return getState(var, key).initialized
end
function isInitialized(dfg::AbstractDFG, label::Symbol, key::Symbol = :default)
return isInitialized(getVariable(dfg, label), key)::Bool
end
"""
$SIGNATURES
Return `::Bool` on whether this variable has been marginalized.
Notes:
- State default `solveKey=:default`
"""
function isMarginalized(vert::VariableDFG, solveKey::Symbol = :default)
return getState(vert, solveKey).marginalized
end
function isMarginalized(dfg::AbstractDFG, sym::Symbol, solveKey::Symbol = :default)
return isMarginalized(DFG.getVariable(dfg, sym), solveKey)
end
"""
$SIGNATURES
Mark a variable as marginalized `true` or `false`.
"""
function setMarginalized!(vnd::State, val::Bool)
return vnd.marginalized = val
end
function setMarginalized!(vari::VariableDFG, val::Bool, solveKey::Symbol = :default)
return setMarginalized!(getState(vari, solveKey), val)
end
function setMarginalized!(
dfg::AbstractDFG,
sym::Symbol,
val::Bool,
solveKey::Symbol = :default,
)
return setMarginalized!(getVariable(dfg, sym), val, solveKey)
end
##==============================================================================
## Variables
##==============================================================================
#
# | | label | tags | timestamp | variableTypeName | solvable | solverData | smallData | dataEntries |
# |---------------------|:-----:|:----:|:---------:|:----------------:|:--------:|:----------:|:---------:|:-----------:|
# | VariableSkeleton | X | X | | | | | | |
# | VariableSummary | X | X | X | X | | | | X |
# | VariableDFG | X | X | x | | X | X | X | X |
#