Skip to content

Commit

Permalink
Merge #259
Browse files Browse the repository at this point in the history
259: implement Base.show for core types r=haakon-e a=haakon-e

See #257 for details.

With this implementation, combining all the examples [here](https://clima.github.io/EnsembleKalmanProcesses.jl/dev/parameter_distributions/#ParameterDistribution-constructor) gives a vector that displays like this:
```julia
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
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)
```

Co-authored-by: Haakon Ludvig Langeland Ervik <[email protected]>
  • Loading branch information
bors[bot] and haakon-e committed Mar 19, 2023
2 parents 3e7494e + 496881f commit 69bcca5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ParameterDistributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ abstract type NoConstraint <: ConstraintType end
abstract type BoundedBelow <: ConstraintType end
abstract type BoundedAbove <: ConstraintType end
abstract type Bounded <: ConstraintType end
BasicConstraints = Union{BoundedBelow, BoundedAbove, Bounded, NoConstraint}

"""
Constraint{T} <: ConstraintType
Expand Down Expand Up @@ -119,6 +120,23 @@ struct Constraint{T} <: ConstraintType
bounds::Union{Dict, Nothing}
end

function Base.show(io::IO, ::MIME"text/plain", cons::Constraint{T}) where {T <: BasicConstraints} # verbose
bounds = isnothing(cons.bounds) ? Dict() : cons.bounds
lb = get(bounds, "lower_bound", "-∞")
ub = get(bounds, "upper_bound", "")
print(io, "Constraint{$(T)} with bounds ($(lb), $(ub))")
end
function Base.show(io::IO, cons::Constraint{T}) where {T}
suffix = isnothing(cons.bounds) ? "" : " with characterization $(tuple(cons.bounds...))"
print(io, "Constraint{$(T)}" * suffix)
end
function Base.show(io::IO, cons::Constraint{<:BasicConstraints}) # shorthand, e.g. in parameter distributions
bounds = isnothing(cons.bounds) ? Dict() : cons.bounds
lb = get(bounds, "lower_bound", "-∞")
ub = get(bounds, "upper_bound", "")
print(io, "Bounds: ($(lb), $(ub))")
end

"""
no_constraint()
Expand Down Expand Up @@ -418,6 +436,19 @@ function ParameterDistribution(
return ParameterDistribution(distribution, constraint, name)
end

function Base.show(io::IO, distributions::ParameterDistribution)
n = length(distributions.name)
out = "ParameterDistribution with $n entries: \n"
for (i, inds) in enumerate(batch(distributions))
dist = distributions.distribution[i]
dist_string = replace("$dist", "\n" => " ") # hack to remove `\n` from `Parameterized(FullNormal(...))`
cons = distributions.constraint[inds]
nam = distributions.name[i]
out *= "'$(nam)' with $(cons) over distribution $dist_string \n"
end
print(io, out)
end

## Functions

"""
Expand Down

0 comments on commit 69bcca5

Please sign in to comment.