Skip to content

Commit 93bfe0f

Browse files
committed
Start with the generic part on actions.
1 parent 90c6e67 commit 93bfe0f

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

src/plans/debug.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The original options can still be accessed using the [`get_state`](@ref) functio
2727
# Fields
2828
2929
* `options`: the options that are extended by debug information
30-
* `debugDictionary`: a `Dict{Symbol,DebugAction}` to keep track of Debug for different actions
30+
* `debug_dictionary`: a `Dict{Symbol,DebugAction}` to keep track of Debug for different actions
3131
3232
# Constructors
3333
DebugSolverState(o,dA)
@@ -40,7 +40,7 @@ construct debug decorated options, where `dD` can be
4040
"""
4141
mutable struct DebugSolverState{S <: AbstractManoptSolverState} <: AbstractManoptSolverState
4242
state::S
43-
debugDictionary::Dict{Symbol, <:DebugAction}
43+
debug_dictionary::Dict{Symbol, <:DebugAction}
4444
function DebugSolverState{S}(
4545
st::S, dA::Dict{Symbol, <:DebugAction}
4646
) where {S <: AbstractManoptSolverState}
@@ -75,10 +75,10 @@ end
7575
"""
7676
set_parameter!(ams::DebugSolverState, ::Val{:Debug}, args...)
7777
78-
Set certain values specified by `args...` into the elements of the `debugDictionary`
78+
Set certain values specified by `args...` into the elements of the `debug_dictionary`
7979
"""
8080
function set_parameter!(dss::DebugSolverState, ::Val{:Debug}, args...)
81-
for d in values(dss.debugDictionary)
81+
for d in values(dss.debug_dictionary)
8282
set_parameter!(d, args...)
8383
end
8484
return dss
@@ -96,17 +96,19 @@ function get_parameter(dss::DebugSolverState, v::Val{T}, args...) where {T}
9696
end
9797

9898
function status_summary(dst::DebugSolverState)
99-
if length(dst.debugDictionary) > 0
99+
if length(dst.debug_dictionary) > 0
100100
s = ""
101-
for (k, v) in dst.debugDictionary
101+
for (k, v) in dst.debug_dictionary
102102
s = "$s\n :$k = $(status_summary(v))"
103103
end
104104
return "$(status_summary(dst.state))\n\n## Debug$s"
105105
else # if the dictionary has no entries, there is no actual debug in pretty print
106106
return status_summary(dst.state)
107107
end
108108
end
109-
109+
function show(io::IO, dst::DebugSolverState)
110+
return print(io, "DebugSolverState($(dst.state), $(dst.debug_dictionary))")
111+
end
110112

111113
dispatch_state_decorator(::DebugSolverState) = Val(true)
112114

@@ -391,7 +393,7 @@ end
391393
function show(io::IO, di::DebugDivider)
392394
return print(io, "DebugDivider(; divider=\"$(escape_string(di.divider))\", at_init=$(di.at_init))")
393395
end
394-
status_summary(di::DebugDivider) = "\"$(escape_string(di.divider))\""
396+
status_summary(di::DebugDivider; inline = true) = (inline ? "\"$(escape_string(di.divider))\"" : "A Debug printing the String “$(escape_string(di.divider))” as a divider.")
395397

396398
@doc """
397399
DebugEntry <: DebugAction

src/plans/solver_state.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ for example within the [`DebugSolverState`](@ref) or within the [`RecordSolverSt
313313
"""
314314
abstract type AbstractStateAction end
315315

316+
status_summary(asa::AbstractStateAction; inline = true) = repr(asa)
317+
status_summary(io::IO, asa::AbstractStateAction; inline = true) = show(io, status_summary(asa; inline = inline))
318+
319+
Base.show(io::IO, ::MIME"text/plain", asa::AbstractStateAction) = status_summary(io::IO, asa; inline = false)
320+
316321
mutable struct StorageRef{T}
317322
x::T
318323
end

src/solvers/debug_solver.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Extend the `i`th step of the solver by a hook to run debug prints,
2626
that were added to the `:BeforeIteration` and `:Iteration` entries of the debug lists.
2727
"""
2828
function step_solver!(amp::AbstractManoptProblem, dss::DebugSolverState, k)
29-
get(dss.debugDictionary, :BeforeIteration, DebugDivider(""))(amp, get_state(dss), k)
29+
get(dss.debug_dictionary, :BeforeIteration, DebugDivider(""))(amp, get_state(dss), k)
3030
step_solver!(amp, dss.state, k)
31-
get(dss.debugDictionary, :Iteration, DebugDivider(""))(amp, get_state(dss), k)
31+
get(dss.debug_dictionary, :Iteration, DebugDivider(""))(amp, get_state(dss), k)
3232
return dss
3333
end
3434

src/solvers/gradient_descent.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function Base.show(io::IO, gds::GradientDescentState)
103103
)
104104
end
105105

106-
function status_summary(gds::GradientDescentState)
106+
function status_summary(gds::GradientDescentState; inline = false)
107107
i = get_count(gds, :Iterations)
108108
Iter = (i > 0) ? "After $i iterations\n" : ""
109109
Conv = indicates_convergence(gds.stop) ? "Yes" : "No"
@@ -117,7 +117,8 @@ function status_summary(gds::GradientDescentState)
117117
$(gds.stepsize)
118118
119119
## Stopping criterion
120-
$(status_summary(gds.stop))
120+
$(status_summary(gds.stop; inline = true))
121+
121122
This indicates convergence: $Conv"""
122123
return s
123124
end

test/plans/test_debug.jl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function ManifoldsBase.default_inverse_retraction_method(::TestPolarManifold)
88
end
99

1010
struct TestDebugAction <: DebugAction end
11+
Base.show(io::IO, ::TestDebugAction) = print(io, "TestDebugAction()")
12+
1113

1214
struct TestMessageState <: AbstractManoptSolverState end
1315
Manopt.get_message(::TestMessageState) = "DebugTest"
@@ -34,18 +36,25 @@ Manopt.get_parameter(d::TestDebugParameterState, ::Val{:value}) = d.value
3436
)
3537
f(M, q) = distance(M, q, p) .^ 2
3638
grad_f(M, q) = -2 * log(M, q, p)
37-
# summary fallback to show
39+
tda = TestDebugAction()
40+
# summary fallback to show - inherited from AbstractStateAction(s)
3841
@test Manopt.status_summary(TestDebugAction()) === "TestDebugAction()"
42+
show(io, tda)
43+
@test String(take!(io)) === "TestDebugAction()"
3944
mp = DefaultManoptProblem(M, ManifoldGradientObjective(f, grad_f))
4045
a1 = DebugDivider("|"; io = io)
41-
@test Manopt.dispatch_state_decorator(DebugSolverState(st, a1)) === Val{true}()
46+
dst = DebugSolverState(st, a1)
47+
@test Manopt.dispatch_state_decorator(dst) === Val{true}()
4248
# constructors
43-
@test DebugSolverState(st, a1).debugDictionary[:Iteration] == a1
44-
@test DebugSolverState(st, [a1]).debugDictionary[:Iteration].group[1] == a1
45-
@test DebugSolverState(st, Dict(:A => a1)).debugDictionary[:A] == a1
46-
@test DebugSolverState(st, ["|"]).debugDictionary[:Iteration].divider == a1.divider
47-
@test endswith(repr(DebugSolverState(st, a1)), "\"|\"")
48-
@test repr(DebugSolverState(st, Dict{Symbol, DebugAction}())) == repr(st)
49+
@test DebugSolverState(st, a1).debug_dictionary[:Iteration] == a1
50+
@test DebugSolverState(st, [a1]).debug_dictionary[:Iteration].group[1] == a1
51+
@test DebugSolverState(st, Dict(:A => a1)).debug_dictionary[:A] == a1
52+
@test DebugSolverState(st, ["|"]).debug_dictionary[:Iteration].divider == a1.divider
53+
@test endswith(Manopt.status_summary(dst), "\"|\"")
54+
@test Manopt.status_summary(a1; inline = true) == "\"|\""
55+
@test Manopt.status_summary(a1; inline = false) == "A Debug printing the String “|” as a divider."
56+
empty_dbg = Dict{Symbol, DebugAction}()
57+
@test repr(DebugSolverState(st, empty_dbg)) == "DebugSolverState($(repr(st)), $(repr(empty_dbg)))"
4958
# Passthrough
5059
dss = DebugSolverState(st, a1)
5160
Manopt.set_parameter!(dss, :StoppingCriterion, :MaxIteration, 20)

0 commit comments

Comments
 (0)