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

implement Base.show for core types #259

Merged
merged 1 commit into from
Mar 19, 2023
Merged

implement Base.show for core types #259

merged 1 commit into from
Mar 19, 2023

Conversation

haakon-e
Copy link
Member

@haakon-e haakon-e commented Mar 16, 2023

See #257 for details.

With this implementation, combining all the examples here gives a vector that displays like this:

julia> combine_distributions([u; prior])
ParameterDistribution with 7 entries: 
'constrained_mvnormal' with Constraint[Bounds: (0, ∞), Bounds: (0, ∞), Bounds: (0, ∞)] over distribution Parameterized(FullNormal( dim: 3 μ: [1.0, 1.0, 1.0] Σ: [0.5 0.25 0.0; 0.25 0.5 0.25; 0.0 0.25 0.5] ) ) 
'constrained_sampled' with Constraint[Bounds: (10, 15), Bounds: (-∞, ∞)] over distribution Samples{Float64}([1.0 5.0 9.0 13.0; 3.0 7.0 11.0 15.0]) 
'Beta' with Constraint[Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞), Bounds: (-∞, ∞)] over distribution VectorOfParameterized{Beta{Float64}}(Beta{Float64}[Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0), Beta{Float64}(α=2.0, β=2.0)]) 
'point_seven' with Constraint[Bounds: (0.0, 1.0)] over distribution Parameterized(Normal{Float64}=0.9581731745582243, σ=0.7851841275859747)) 
'upper bound' with Constraint[Bounds: (-∞, 5.0)] over distribution Parameterized(Normal{Float64}=1.458006955602075, σ=0.03487311564634986)) 
'lower bound' with Constraint[Bounds: (-5.0, ∞)] over distribution Parameterized(Normal{Float64}=1.740120034293624, σ=0.026311235124201148)) 
'no bound' with Constraint[Bounds: (-∞, ∞)] over distribution Parameterized(Normal{Float64}=0.7, σ=0.15)) 

Individual constraints render like this:

julia> no_constraint()
Constraint{NoConstraint} with bounds (-∞, ∞)

julia> bounded_below(-5)
Constraint{BoundedBelow} with bounds (-5, ∞)

julia> bounded_above(-5)
Constraint{BoundedAbove} with bounds (-∞, -5)

julia> bounded(-5,5)
Constraint{Bounded} with bounds (-5, 5)

@haakon-e haakon-e added the documentation Improvements or additions to documentation label Mar 16, 2023
@haakon-e haakon-e self-assigned this Mar 16, 2023
@haakon-e haakon-e linked an issue Mar 16, 2023 that may be closed by this pull request
@haakon-e
Copy link
Member Author

What do you think @odunbar? One downside of this implementation, particularly for bounds, is that new, custom structs may not contain enough information. Consider e.g. the affine constraint type from examples:

julia> d2 = Samples([1.0 5.0 9.0 13.0; 3.0 7.0 11.0 15.0]) # 4 samples of 2D parameter space
Samples{Float64}([1.0 5.0 9.0 13.0; 3.0 7.0 11.0 15.0])

julia> transform = (x -> 3 * x + 14)
#13 (generic function with 1 method)

julia> jac_transform = (x -> 3)
#15 (generic function with 1 method)

julia> inverse_transform = (x -> (x - 14) / 3)
#17 (generic function with 1 method)

julia> abstract type Affine <: ConstraintType end

julia> c2 = [bounded(10, 15),
             Constraint{Affine}(transform, jac_transform, inverse_transform, nothing)]
2-element Vector{Constraint}:
 Constraint{Bounded} with bounds (10, 15)
 Constraint{Affine} with bounds (-∞, ∞)

julia> name2 = "constrained_sampled"
"constrained_sampled"

julia> u2 = ParameterDistribution(d2, c2, name2)
ParameterDistribution with 1 entries: 
'constrained_sampled' with Constraint[Bounds: (10, 15), Bounds: (-∞, ∞)] over distribution Samples{Float64}([1.0 5.0 9.0 13.0; 3.0 7.0 11.0 15.0]) 

Should I explicitly check for NoConstraint, BoundedBelow, BoundedAbove and Bounded when printing, and then print something more comprehensive if it's some other type?

@haakon-e
Copy link
Member Author

btw, I got an error using some neat >v1.6 syntax:

(; distribution, constraint, name) = distributions

ERROR: LoadError: LoadError: syntax: invalid assignment location "; distribution, constraint, name" around /home/runner/work/EnsembleKalmanProcesses.jl/EnsembleKalmanProcesses.jl/src/ParameterDistributions.jl:435
[...]

so I changed it to be compatible... but maybe we should think about updating the version of our runners? Or are we explicitly wanting to support Julia 1.6 still?

@odunbar
Copy link
Collaborator

odunbar commented Mar 17, 2023

Looks Great!

  1. If users implement their own constraints, they have the option to add a Dict meta-data (currently the nothing in your constructor example). Unhelpfully we call this "bounds", but we could maybe have an option that if it not a predefined constraint, then display this. E.g
    Constraint{Affine}(transform, jac_transform, inverse_transform, Dict("linear" => 3, "const" => 14)
    Would display something like
    Constraint{Affine} with characterization (linear = 3, const = 14)
    We are providing a small convenience here, I wouldn't try to anticipate these unknown functions beyond what users wish to provide. Especially as I am yet to see a use-case where someone may want to do this.

  2. Thanks for noting the Julia 1.6 issue, I guess there is no explicit reason to support 1.6, except that we do. On the other hand, this is the first instance I've seen where we have a support problem, so perhaps we can leave it compatible for now until future refactors.

@haakon-e
Copy link
Member Author

With your suggestion, the following is now outputted:

julia> c2 = [bounded(10, 15), Constraint{Affine}(transform, jac_transform, inverse_transform, Dict("linear" => 3, "const" => 14))]
2-element Vector{Constraint}:
 Constraint{Bounded} with bounds (10, 15)
 Constraint{Affine} with characterization ("linear" => 3, "const" => 14)

@haakon-e haakon-e requested a review from odunbar March 18, 2023 01:47
Copy link
Collaborator

@odunbar odunbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

We can ignore codecov as it is not a sensible, so squash and merge, thanks!

* types: `Constraint`, `ParameterDistribution`
@haakon-e
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 19, 2023

Build succeeded:

@bors bors bot merged commit 69bcca5 into main Mar 19, 2023
@bors bors bot deleted the base-show branch March 19, 2023 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Base.show for EKP.jl types
2 participants