Skip to content

Commit

Permalink
implement Base.show for core types
Browse files Browse the repository at this point in the history
* types: `Constraint`, `ParameterDistribution`
  • Loading branch information
haakon-e committed Mar 19, 2023
1 parent 3e7494e commit 496881f
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 496881f

Please sign in to comment.