Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: pretty-printing #48

Open
st-- opened this issue Nov 11, 2021 · 1 comment
Open

RFC: pretty-printing #48

st-- opened this issue Nov 11, 2021 · 1 comment

Comments

@st--
Copy link
Member

st-- commented Nov 11, 2021

ParameterHandling.jl is a nice framework for organising parameters into nested NamedTuples. Whilst working with them in JuliaGaussianProcesses/AbstractGPs.jl#240, what I was missing is a nice way of printing this parameter structure. It seems like the best place to sort this out is within ParameterHandling directly, as it needs to know about the various constraints to print them properly.

So far, I've got the following stub:

using Printf

function show_params(nt::NamedTuple, pre=0)
    res = ""
    for (s, v) in pairs(nt)
        if typeof(v) <: NamedTuple
            res *= join(fill(" ", pre)) * "$(s):\n" * show_params(v, pre+4)
        else
            res *= join(fill(" ", pre)) * "$s = $(@sprintf("%.3f", v))\n"
        end
    end
    return res
end

which then gets called as print(show_params(ParameterHandling.value(param_struct))).

Ideally, I would like to avoid the call to value to remove all the constraints, and instead add the corresponding information (e.g. scale = 1.345 (positive)).

And of course would have to think a bit harder to make it work well for vectors/matrices etc... so I thought it better to first start a discussion about what might be reasonable:)

Update: Oh, and of course instead of directly printing, it should implement show so it works well both in REPL and notebooks.

@scheidan
Copy link

Maybe simply some pretty printing for the types would already be a good first step.

For example:

import base.show

function show(io::Base.IO, t::PH.Fixed)
    print(io, PH.value(t), " [fixed]")
end

function show(io::Base.IO, t::PH.Positive)
    print(io, PH.value(t), " ∈ ℝ+")
end

function show(io::Base.IO, t::PH.Bounded)
    print(io, PH.value(t),
          " ∈ [$(t.lower_bound), $(t.upper_bound)]")
end

this results in:

julia> params_template = (
    k1 = (
        a = fixed(0.9),        
        b = positive(1.0),
    ),
    k2 = (
        a = bounded(0.15, 0.1, 0.2),
        b = -3.0,
    ),
    c = 22.0,
)

julia> params_template
(k1 = (a = 0.9 [fixed], b = 1.0 +), k2 = (a = 0.15  [0.1, 0.2], b = -3.0), c = 22.0)

We would need to think a bit more carefully how to print the types in a meaningful way.
Maybe print them as we type them? E.g. bounded(0.15,0.1, 0.2) would be printed simply as bounded(0.15, 0.1, 0.2)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants