Skip to content

Commit 48c0a6f

Browse files
committed
refactor to PrettyTables
1 parent 01b76ab commit 48c0a6f

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.3.37"
66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
88
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
9+
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
910
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
1011
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
1112
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
@@ -15,6 +16,7 @@ Accessors = "0.1.36"
1516
Aqua = "0.8"
1617
ArrayInterface = "7.9"
1718
Pkg = "1"
19+
PrettyTables = "2.4.0"
1820
RuntimeGeneratedFunctions = "0.5.12"
1921
SafeTestsets = "0.0.1"
2022
StaticArrays = "1.9"

src/SymbolicIndexingInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using RuntimeGeneratedFunctions
44
import StaticArraysCore: MArray, similar_type
55
import ArrayInterface
66
using Accessors: @reset
7-
import Term: Table # for pretty-printing
7+
using PrettyTables # for pretty printing
88

99
RuntimeGeneratedFunctions.init(@__MODULE__)
1010

src/parameter_indexing_proxy.jl

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,52 @@ function Base.setindex!(p::ParameterIndexingProxy, val, idx)
1818
return setp(p.wrapped, idx)(p.wrapped, val)
1919
end
2020

21-
function Base.show(io::IO, pip::ParameterIndexingProxy; kwargs...)
21+
function Base.show(io::IO, ::MIME"text/plain", pip::ParameterIndexingProxy)
22+
showparams(io, pip; num_rows = 20, show_all = false, scalarize = true)
23+
end
24+
25+
"""
26+
showparams(io::IO, pip::ParameterIndexingProxy; num_rows = 20, show_all = false, scalarize = true, kwargs...)
27+
28+
Method for customizing the table output. Keyword args:
29+
- num_rows
30+
- show_all: whether to show all parameters
31+
- scalarize: whether to scalarize array symbolics in the table output.
32+
- kwargs... are passed to the pretty_table call.
33+
"""
34+
function showparams(io::IO, pip::ParameterIndexingProxy; num_rows = 20, show_all = false, scalarize = true, kwargs...)
2235
params = Any[]
2336
vals = Any[]
2437
for p in parameter_symbols(pip.wrapped)
25-
push!(params, p)
26-
val = getp(pip.wrapped, p)(pip.wrapped)
27-
push!(vals, val)
38+
if symbolic_type(p) === ArraySymbolic() && scalarize
39+
val = getp(pip.wrapped, p)(pip.wrapped)
40+
for (_p, _v) in zip(collect(p), val)
41+
push!(params, _p)
42+
push!(vals, _v)
43+
end
44+
else
45+
push!(params, p)
46+
val = getp(pip.wrapped, p)(pip.wrapped)
47+
push!(vals, val)
48+
end
2849
end
2950

30-
print(
31-
Table([params vals];
32-
box=:SIMPLE,
33-
header=["Parameter", "Value"],
34-
kwargs...)
35-
)
51+
num_shown = if show_all
52+
length(params)
53+
else
54+
if num_rows > length(params)
55+
length(params)
56+
else
57+
num_rows
58+
end
59+
end
60+
61+
pretty_table(io, [params[1:num_shown] vals[1:num_shown]];
62+
header=["Parameter", "Value"],
63+
kwargs...)
64+
65+
if num_shown < length(params)
66+
println(io,
67+
"$num_shown of $(length(params)) params shown. To show all the parameters, call `showparams(io, ps, show_all = true)`. Adjust the number of rows with the num_rows kwarg. Consult `showparams` docstring for more options.")
68+
end
3669
end

0 commit comments

Comments
 (0)