Skip to content

Commit f13f769

Browse files
committed
Unify print and extract a function for indentation (both in characters as well as in headers)
1 parent 68e5a4d commit f13f769

12 files changed

Lines changed: 88 additions & 33 deletions

src/plans/cache.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,17 +1101,18 @@ function status_summary(smco::SimpleManifoldCachedObjective; context = :default)
11011101
length(s2) > 0 && (s2 = "$(s2)\n\n")
11021102
return "$(s2)$(s)"
11031103
end
1104-
function status_summary(mco::ManifoldCachedObjective)
1104+
function status_summary(mco::ManifoldCachedObjective; context = :default)
1105+
_is_inline(context) && (return repr(mco))
11051106
s = "## Cache\n"
1106-
s2 = status_summary(mco.objective)
1107-
(length(s2) > 0) && (s2 = "$(s2)")
1108-
length(mco.cache) == 0 && return "$(s) No caches active\n$(s2)"
1107+
s2 = status_summary(mco.objective; context = context)
1108+
(length(s2) > 0) && (s2 = "$(s2)\n\n")
1109+
length(mco.cache) == 0 && return "$(s2)$(s) No caches active"
11091110
longest_key_length = max(length.(["$k" for k in keys(mco.cache)])...)
11101111
cache_strings = [
11111112
" * :" *
11121113
rpad("$k", longest_key_length, " ") *
11131114
" : $(v.currentsize)/$(v.maxsize) entries of type $(valtype(v)) used" for
11141115
(k, v) in zip(keys(mco.cache), values(mco.cache))
11151116
]
1116-
return "$(s2)\n\n$(s)$(join(cache_strings, "\n"))\n"
1117+
return "$(s2)$(s)$(join(cache_strings, "\n"))\n"
11171118
end

src/plans/constrained_plan.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ function status_summary(cmo::ConstrainedManifoldObjective; context = :default)
270270
For verifications, the inequalities are checked with an absolute tolerance of `atol = $(cmo.atol)`
271271
272272
## Unconstrained Objective
273-
$(replace(s, "\n" => "\n$(_MANOPT_INDENT)", "\n#" => "\n$(_MANOPT_INDENT)##"))
273+
$(_in_str(s))
274274
275275
## Equality constrains
276-
$(replace(status_summary(cmo.equality_constraints; context = context), "\n" => "\n$(_MANOPT_INDENT)", "\n#" => "\n$(_MANOPT_INDENT)##"))
276+
$(_in_str(status_summary(cmo.equality_constraints; context = context)))
277277
278278
## Inequality constrains
279-
$(replace(status_summary(cmo.inequality_constraints; context = context), "\n" => "\n$(_MANOPT_INDENT)", "\n#" => "\n$(_MANOPT_INDENT)##"))"""
279+
$(_in_str(status_summary(cmo.inequality_constraints; context = context)))"""
280280
end
281281
function show(io::IO, cmo::ConstrainedManifoldObjective)
282282
print(io, "ConstrainedManifoldObjective("); print(io, cmo.objective)
@@ -388,10 +388,10 @@ function status_summary(cmp::ConstrainedManoptProblem; context = :default)
388388
A constrained optimization problem for Manopt.jl
389389
390390
## Manifold
391-
$(replace(repr(cmp.manifold), "\n#" => "\n$(_MANOPT_INDENT)##"))
391+
$(_in_str(repr(cmp.manifold); indent = 1, headers = 0))
392392
393393
## Objective
394-
$(replace(status_summary(cmp.objective, context = context), "\n#" => "\n$(_MANOPT_INDENT)##"))
394+
$(_in_str(status_summary(cmp.objective, context = context); indent = 1))
395395
396396
## Ranges
397397
* gradient equality range: $(_MANOPT_INDENT)$(cmp.grad_equality_range)

src/plans/debug.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function status_summary(de::DebugEvery; context = :default)
221221
return "[$s, $(de.every)]"
222222
end
223223
(context == :inline) && return "The Debug $(status_summary(de.debug; context = context)) only printed every $(de.every) iteration"
224-
return "a DebugAction wrapping the following DebugAction to only print it every $(de.every)th iteration.\n$(replace(status_summary(de.debug; context = context), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))"
224+
return "a DebugAction wrapping the following DebugAction to only print it every $(de.every)th iteration.\n$(_in_str(status_summary(de.debug; context = context)))"
225225
end
226226
function set_parameter!(de::DebugEvery, e::Symbol, args...)
227227
set_parameter!(de, Val(e), args...)

src/plans/embedded_objective.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,5 @@ function status_summary(emo::EmbeddedManifoldObjective{P, T}; context = :default
386386
An embedded objective
387387
388388
## Objective
389-
$(_MANOPT_INDENT)$(replace(status_summary(emo.objective, context = context), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))$(pX_str)"""
389+
$(_in_str(status_summary(emo.objective, context = context)))$(pX_str)"""
390390
end

src/plans/first_order_plan.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ function get_cost_and_gradient!(
511511
return error("$mfo seems to either have no access to a cost or a gradient")
512512
end
513513
function status_summary(mfo::ManifoldFirstOrderObjective; context = :default)
514-
_is_inline(context) && (return "A first order objective with functions for $(join(keys(mfo.functions), ", ", (length(mfo.functions) > 2 ? "," : "") * " and "))")
514+
_is_inline(context) && (return repr(mfo))
515515
return "A first order objective with $(length(mfo.functions)) provided functions.\n\n" * join([ "* $k:$(_MANOPT_INDENT) $(v)" for (k, v) in zip(keys(mfo.functions), mfo.functions) ], "\n")
516516
end
517517
function show(io::IO, mfo::ManifoldFirstOrderObjective{E}) where {E}

src/plans/interior_point_Newton_plan.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function status_summary(CKKTvf::CondensedKKTVectorField; context = :default)
356356
_is_inline(context) && (return repr(CKKTvf))
357357
return """
358358
The condensed KKT vector field for the constrained objective
359-
$(_MANOPT_INDENT)$(replace(status_summary(CKKTvf.cmo; context = context), "\n" => "\n$(_MANOPT_INDENT)"))
359+
$(_in_str(status_summary(CKKTvf.cmo; context = context); indent = 1))
360360
with μ=$(CKKTvf.μ) s=$(CKKTvf.s) β=$(CKKTvf.β)"""
361361
end
362362
function show(io::IO, CKKTvf::CondensedKKTVectorField)
@@ -467,7 +467,7 @@ function status_summary(CKKTvfJ::CondensedKKTVectorFieldJacobian; context = :def
467467
_is_inline(context) && (return repr(CKKTvfJ))
468468
return """
469469
The Jacobian of the condensed KKT vector field for the constrained objective
470-
$(_MANOPT_INDENT)$(replace(status_summary(CKKTvfJ.cmo; context = context), "\n" => "\n$(_MANOPT_INDENT)"))
470+
$(_in_str(status_summary(CKKTvfJ.cmo; context = context); indent = 1))
471471
with μ=$(CKKTvfJ.μ) s=$(CKKTvfJ.s) β=$(CKKTvfJ.β)"""
472472
end
473473
function show(io::IO, CKKTvfJ::CondensedKKTVectorFieldJacobian)

src/plans/plan.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,25 @@ status_summary(e; context::Symbol = :default)
3030
function status_summary(io::IO, e; context = :default)
3131
return print(io, status_summary(e; context = context))
3232
end
33+
#
34+
#
35+
# status_summary string format helper
36+
# ---
37+
# check whether a context is inline or less
3338
_is_inline(c) = (c == :inline || c == :short)
34-
39+
# ind_str - indent a string for use within another one
40+
# * `indent = false` raise indentation by `indent_str` (`_MANOPT_INDENT` by default)
41+
# * `headers = true` increase headers also on Headers that are indented with `indent_str`
42+
function _in_str(s::String; indent = 0, headers = 1, indent_str = _MANOPT_INDENT)
43+
t = s
44+
for _ in 1:indent
45+
t = replace("$(indent_str)$t", "\n" => "\n$(indent_str)")
46+
end
47+
for i in 1:headers
48+
t = replace(t, Regex("(?m)^($(indent_str)*)(#+)") => s"\1#\2")
49+
end
50+
return t
51+
end
3552
"""
3653
set_parameter!(f, element::Symbol , args...)
3754

src/plans/primal_dual_plan.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ function status_summary(tmp::TwoManifoldProblem; context = :default)
3131
An optimization problem for Manopt.jl requiring a primal and a dual manifold
3232
3333
## Manifolds
34-
* $(replace(repr(tmp.first_manifold), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))
35-
* $(replace(repr(tmp.second_manifold), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))
34+
* $(_in_str(repr(tmp.first_manifold), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))
35+
* $(_in_str(repr(tmp.second_manifold); indent = 1))
3636
3737
## Objective
38-
$(_MANOPT_INDENT)$(replace(status_summary(tmp.objective, context = context), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))"""
38+
$(_in_str(status_summary(tmp.objective, context = context); indent = 1))"""
3939
end
4040

4141
@doc """

src/plans/problem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function status_summary(dmp::DefaultManoptProblem; context = :default)
5151
$(_MANOPT_INDENT)$(replace(repr(dmp.manifold), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))
5252
5353
## Objective
54-
$(_MANOPT_INDENT)$(replace(status_summary(dmp.objective, context = context), "\n#" => "\n$(_MANOPT_INDENT)##", "\n" => "\n$(_MANOPT_INDENT)"))"""
54+
$(_in_str(status_summary(dmp.objective, context = context); indent = 1))"""
5555
end
5656

5757
"""

src/plans/proximal_gradient_plan.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ Generate the proximal gradient objective given the total cost ``f = g + h``, smo
3030
* `evaluation=`[`AllocatingEvaluation`](@ref): whether the gradient and proximal map
3131
is given as an allocation function or an in-place ([`InplaceEvaluation`](@ref)).
3232
"""
33-
struct ManifoldProximalGradientObjective{E <: AbstractEvaluationType, TC, TG, TGG, TP} <:
34-
AbstractManifoldCostObjective{E, TC}
33+
struct ManifoldProximalGradientObjective{E <: AbstractEvaluationType, TC, TG, TGG, TP} <: AbstractManifoldCostObjective{E, TC}
3534
cost::TC # f = g + h
3635
cost_smooth::TG # smooth part
3736
gradient_g!!::TGG
@@ -77,6 +76,29 @@ function get_gradient!(
7776
return X
7877
end
7978

79+
function Base.show(io::IO, mpgo::ManifoldProximalGradientObjective{E}) where {E}
80+
print(io, "ManifoldProximalGradientObjective(")
81+
print(io, mpgo.cost); print(io, ", ")
82+
print(io, mpgo.cost_smooth); print(io, ", ")
83+
print(io, mpgo.gradient_g!!); print(io, ", ")
84+
print(io, mpgo.proximal_map_h!!); print(io, "; "); print(io, _to_kw(E))
85+
return print(io, ")")
86+
end
87+
88+
function status_summary(mpgo::ManifoldProximalGradientObjective{E}; context = :default) where {E}
89+
(context === :short) && repr(mpgo)
90+
s = "A proximal gradient objective `f = g + h`, where `g` is smooth and `h` is possibly nonsmooth."
91+
(context === :inline) && (return s)
92+
e = (E === AllocatingEvaluation ? " (allocating)" : " (in-place)")
93+
return """
94+
$s
95+
96+
# Components
97+
* `f`: $(mpgo.cost)
98+
* `g`: $(mpgo.cost_smooth)
99+
* `gradient_g`: $(mpgo.gradient_g!!)$e
100+
* `prox_h`: $(mpgo.proximal_map_h!!)$e"""
101+
end
80102
"""
81103
get_cost_smooth(M::AbstractManifold, objective, p)
82104
@@ -102,11 +124,7 @@ function get_proximal_map(
102124
end
103125

104126
function get_proximal_map!(
105-
M::AbstractManifold,
106-
q,
107-
mpgo::ManifoldProximalGradientObjective{AllocatingEvaluation},
108-
λ,
109-
p,
127+
M::AbstractManifold, q, mpgo::ManifoldProximalGradientObjective{AllocatingEvaluation}, λ, p,
110128
)
111129
copyto!(M, q, mpgo.proximal_map_h!!(M, λ, p))
112130
return q

0 commit comments

Comments
 (0)