Skip to content

Commit 01d9eb5

Browse files
committed
add tests for encoder_kwargs_from
1 parent 2430c0a commit 01d9eb5

3 files changed

Lines changed: 53 additions & 18 deletions

File tree

src/MarkovChainMonteCarlo.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ function MCMCWrapper(
601601
prior::PD,
602602
em::EM;
603603
kwargs...,
604-
) where {AV <: AbstractVector, OB <: Observation, PD <: ParameterDistribution, EM <: Emulator}
604+
) where {OB <: Observation, PD <: ParameterDistribution, EM <: Emulator}
605605
return MCMCWrapper(mcmc_alg, get_obs(observation), prior, em; kwargs...)
606606
end
607607

@@ -611,7 +611,7 @@ function MCMCWrapper(
611611
prior::PD,
612612
em::EM;
613613
kwargs...,
614-
) where {AV <: AbstractVector, OS <: ObservationSeries, PD <: ParameterDistribution, EM <: Emulator}
614+
) where {OS <: ObservationSeries, PD <: ParameterDistribution, EM <: Emulator}
615615
observations = [get_obs(ob) for ob in get_observations(observation_series)]
616616
return MCMCWrapper(mcmc_alg, observations, prior, em; kwargs...)
617617
end

src/Utilities.jl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export create_encoder_schedule,
2727
encode_structure_matrix,
2828
decode_data,
2929
decode_structure_matrix,
30-
norm
30+
norm,
31+
encoder_kwargs_from
3132

3233

3334
const StructureMatrix = Union{UniformScaling, AbstractMatrix, AbstractVector, LinearMap} # The vector appears due to possible block-structured matrices (build=false)
@@ -71,32 +72,42 @@ end
7172

7273
# Using Observation Objects:
7374

75+
"""
76+
$(TYPEDSIGNATURES)
77+
78+
Extracts the relevant encoder kwargs from the observation as a NamedTuple. Contains,
79+
- `:obs_noise_cov` as (unbuilt) noise covariance
80+
- `:observation` as obs vector
81+
"""
7482
function encoder_kwargs_from(obs::OB) where {OB <: Observation}
75-
return (obs_noise_cov = get_covs(obs, build = false), observation = get_obs(obs))
83+
return (; obs_noise_cov = get_obs_noise_cov(obs, build = false), observation = get_obs(obs))
7684
end
7785

78-
function encoder_kwargs_from(os::OS) where {OS <: ObservationSeries}
79-
obs_vec = get_observations(os)
80-
return [encoder_kwargs_from(obs) for obs in obs_vec]
81-
end
86+
"""
87+
$(TYPEDSIGNATURES)
8288
83-
function encoder_kwargs_from(prior::PD; rng = Random.default_rng(), n_samples = 100) where {PD <: ParameterDistribution}
84-
return (prior_cov = cov(prior), prior_samples_in = sample(rng, prior, n_samples))
89+
Extracts the relevant encoder kwargs from the ObservationSeries as a NamedTuple. Assumes the same noise covariance for all observation vectors. Contains,
90+
- `:obs_noise_cov` as (unbuilt) noise covariance of FIRST observation
91+
- `:observation` as obs vector from all observations
92+
"""
93+
function encoder_kwargs_from(os::OS) where {OS <: ObservationSeries}
94+
observations = get_observations(os)
95+
obs_vec = [get_obs(obs) for obs in observations]
96+
obs_noise_cov = get_obs_noise_cov(observations[1], build = false)
97+
return (; obs_noise_cov = obs_noise_cov, observation = obs_vec)
8598
end
8699

87-
## multiplication with observation covariance objects without building
88100
"""
89101
$(TYPEDSIGNATURES)
90102
91-
Left-multiply `X` by structure matrix `A` without building it (if provided in a compact form).
92-
93-
This is useful when A is high dimensional and provided as an `SVD` or `SumOfCovariances` etc. object from EnsembleKalmanProcesses.
103+
Extracts the relevant encoder kwargs from the ParameterDistribution prior. Contains,
104+
- `:prior_cov` as prior covariance
94105
"""
95-
function lmul_compact(A, X::AVorM) where {AVorM <: AbstractVecOrMat}
96-
# A is presumed a vector, of (compact) matrix types.
97-
return isa(A, AbstractVector) ? EKP.lmul_without_build(A, X) : EKP.lmul_without_build([A], X)
106+
function encoder_kwargs_from(prior::PD) where {PD <: ParameterDistribution}
107+
return (; prior_cov = cov(prior))
98108
end
99109

110+
## multiplication with observation covariance objects without building
100111
"""
101112
$(TYPEDSIGNATURES)
102113
@@ -105,7 +116,7 @@ Produces a linear map of type `LinearMap` that can evaluates the stacked actions
105116
This compact map constructs the following form of the Linear map f:
106117
107118
1. get compact form svd-plus-d form "USVt + D" of the `blocks`
108-
2. create the f via stacking `A.U * A.S * A.Vt * x[block] + A.D * x[block] for (A,block) in blocks`
119+
2. create the f via stacking `A.U * A.S * A.Vt * xblock + A.D * xblock for (A,xblock) in (As, x)`
109120
110121
kwargs:
111122
------

test/Utilities/runtests.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using LinearAlgebra
77
using CalibrateEmulateSample.Utilities
88
using CalibrateEmulateSample.EnsembleKalmanProcesses
99
using CalibrateEmulateSample.DataContainers
10+
using CalibrateEmulateSample.ParameterDistributions
1011

1112
@testset "Utilities" begin
1213

@@ -49,6 +50,29 @@ end
4950
# Seed for pseudo-random number generator
5051
rng = Random.MersenneTwister(4154)
5152

53+
# test getting encoder kwargs from observations and series
54+
# from prior
55+
pd = constrained_gaussian("name", 0.0, 2.0, -10, Inf, repeat = 5)
56+
encoder_kwargs = encoder_kwargs_from(pd)
57+
@test isapprox(norm(encoder_kwargs.prior_cov - cov(pd)), 0, atol = 1e-12)
58+
59+
# from observation
60+
osample = [1.0, 2.0]
61+
ocov = 4.0 * I(2)
62+
obs = Observation(Dict("samples" => osample, "covariances" => ocov, "names" => "test"))
63+
encoder_kwargs = encoder_kwargs_from(obs)
64+
@test all(isapprox.(norm.(encoder_kwargs.obs_noise_cov - get_obs_noise_cov(obs, build = false)), 0.0, atol = 1e-12))
65+
@test all(isapprox.(encoder_kwargs.observation - get_obs(obs), 0.0, atol = 1e-12))
66+
67+
# from observation series
68+
obs2 = Observation(Dict("samples" => osample .+ 1.0, "covariances" => 2.0 .* ocov, "names" => "test"))
69+
obs_series = ObservationSeries([obs, obs2])
70+
encoder_kwargs = encoder_kwargs_from(obs_series)
71+
@test all(isapprox.(norm.(encoder_kwargs.obs_noise_cov - get_obs_noise_cov(obs, build = false)), 0, atol = 1e-12))
72+
diff = encoder_kwargs.observation - [get_obs(obs), get_obs(obs2)]
73+
@test all([all(isapprox.(dd, 0.0, atol = 1e-12)) for dd in diff])
74+
75+
5276
# Tests for get_structure_vec and get_structure_mat
5377
structure_vecs = Dict("a" => [1, 2, 3], "b" => [4, 5, 6])
5478
structure_mats = Dict("c" => [1 2; 3 4], "d" => [5 6; 7 8])

0 commit comments

Comments
 (0)