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

Base.show(::ParameterDistribution) error with Gaussian Random Fields #300

Closed
haakon-e opened this issue Jun 19, 2023 · 2 comments · Fixed by #304
Closed

Base.show(::ParameterDistribution) error with Gaussian Random Fields #300

haakon-e opened this issue Jun 19, 2023 · 2 comments · Fixed by #304

Comments

@haakon-e
Copy link
Member

To reproduce (from ParameterDistributions test (link):

import GaussianRandomFields
const GRF = GaussianRandomFields
using EnsembleKalmanProcesses.ParameterDistributions
dim = 1
dofs = 10
pts = collect(0:0.01:1)
pkg = GRFJL()
grf = GRF.GaussianRandomField(GRF.CovarianceFunction(dim, GRF.Matern(0.05, 1)), GRF.KarhunenLoeve(dofs), pts)
d5 = GaussianRandomFieldInterface(grf, pkg)
c5 = bounded(-5, 5)
name5 = "grf_in_min5_5"
u5 = ParameterDistribution(d5, c5, name5)

gives this error:

julia> u5 = ParameterDistribution(d5, c5, name5)
Error showing value of type ParameterDistribution{GaussianRandomFieldInterface, Constraint{Bounded}, String}:
ERROR: BoundsError: attempt to access 1-element Vector{Constraint{Bounded}} at index [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
Stacktrace:
   [1-3]  internal
       @ Base, Unknown
     [4] getindex(A::Vector{Constraint{Bounded}}, I::Vector{Int64})
       @ Base ./abstractarray.jl:1294
     [5] show(io::IOContext{Base.TTY}, distributions::ParameterDistribution{GaussianRandomFieldInterface, Constraint{Bounded}, …})
       @ EnsembleKalmanProcesses.ParameterDistributions ~/Documents/CliMA/EnsembleKalmanProcesses.jl/src/ParameterDistributions.jl:447
  [6-22]  internal
       @ Base.Multimedia, REPL, AbbreviatedStackTraces, REPL.LineEdit, Unknown
Use `err` to retrieve the full stack trace.
@haakon-e
Copy link
Member Author

Base.show() currently looks like this:

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

The error seems to be that I try to index into the contraints:

cons = distributions.constraint[inds]

where

inds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
distributions.constraint
1-element Vector{Constraint{Bounded}}:
 Constraint{Bounded} with bounds (-5, 5)

i.e., I am trying to index into a 1-vector with the indices 1:10, which obviously won't work.

@odunbar do you have any insight into GRFs that may be useful to resolve this?
Perhaps we should loop over distributions in some other way for GRFs?
And more generally, what is useful information to be printed about GRFs?

@haakon-e haakon-e changed the title Base.show() error with Gaussian Random Fields Base.show(::ParameterDistribution) error with Gaussian Random Fields Jun 19, 2023
@odunbar
Copy link
Collaborator

odunbar commented Jun 28, 2023

I think this is resolved by using the keyword option function_parameter_opt=constraint in the call to batch() in the loop. This will return 1 for inds always as required.

More generally I agree that for (1) long VectorOfParameterized distributions, and (2) GaussianRandomFieldInterface distributions there may be more readable summary information that can be provided

@bors bors bot closed this as completed in 7b6149c Jul 5, 2023
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

Successfully merging a pull request may close this issue.

2 participants