Skip to content

Commit 002a511

Browse files
committed
format
1 parent 472cda9 commit 002a511

6 files changed

Lines changed: 53 additions & 45 deletions

File tree

src/Emulator.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,7 @@ function encode_data(
253253
em_or_fmw::EorFMW,
254254
data,
255255
in_or_out::AS,
256-
) where {
257-
AS <: AbstractString,
258-
EorFMW <: Union{Emulator, ForwardMapWrapper},
259-
}
256+
) where {AS <: AbstractString, EorFMW <: Union{Emulator, ForwardMapWrapper}}
260257
return encode_data(get_encoder_schedule(em_or_fmw), data, in_or_out)
261258
end
262259

@@ -272,10 +269,7 @@ function decode_data(
272269
em_or_fmw::EorFMW,
273270
data,
274271
in_or_out::AS,
275-
) where {
276-
AS <: AbstractString,
277-
EorFMW <: Union{Emulator, ForwardMapWrapper},
278-
}
272+
) where {AS <: AbstractString, EorFMW <: Union{Emulator, ForwardMapWrapper}}
279273
return decode_data(get_encoder_schedule(em_or_fmw), data, in_or_out)
280274
end
281275

src/Utilities.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,11 @@ function _encode_data(proc::P, data::VV) where {P <: DataProcessor, VV <: Abstra
497497
end
498498

499499

500-
function _encode_data(proc::P, data::PDC, apply_to::AS) where {P <: DataProcessor, AS <: AbstractString, PDC <: PairedDataContainer}
500+
function _encode_data(
501+
proc::P,
502+
data::PDC,
503+
apply_to::AS,
504+
) where {P <: DataProcessor, AS <: AbstractString, PDC <: PairedDataContainer}
501505
input_data, output_data = get_data(data)
502506
if apply_to == "in"
503507
return _encode_data(proc, input_data)
@@ -518,7 +522,11 @@ function _decode_data(proc::P, data::VV) where {P <: DataProcessor, VV <: Abstra
518522
end
519523

520524

521-
function _decode_data(proc::P, data::PDC, apply_to::AS) where {P <: DataProcessor, AS <: AbstractString, PDC <: PairedDataContainer}
525+
function _decode_data(
526+
proc::P,
527+
data::PDC,
528+
apply_to::AS,
529+
) where {P <: DataProcessor, AS <: AbstractString, PDC <: PairedDataContainer}
522530
input_data, output_data = get_data(data)
523531

524532
if apply_to == "in"
@@ -777,7 +785,7 @@ function encode_data(
777785
in_or_out::AS,
778786
) where {AS <: AbstractString, VorMorDC <: Union{AbstractVector, AbstractMatrix, DataContainer}, VV <: AbstractVector}
779787
if isa(data, AbstractVector)
780-
return vec(get_data(encode_with_schedule(encoder_schedule, DataContainer(reshape(data,:,1)), in_or_out)))
788+
return vec(get_data(encode_with_schedule(encoder_schedule, DataContainer(reshape(data, :, 1)), in_or_out)))
781789
elseif isa(data, AbstractMatrix)
782790
return get_data(encode_with_schedule(encoder_schedule, DataContainer(data), in_or_out))
783791
else
@@ -900,7 +908,7 @@ function decode_data(
900908
in_or_out::AS,
901909
) where {AS <: AbstractString, VorMorDC <: Union{AbstractVector, AbstractMatrix, DataContainer}, VV <: AbstractVector}
902910
if isa(data, AbstractVector)
903-
return vec(get_data(decode_with_schedule(encoder_schedule, DataContainer(reshape(data,:,1)), in_or_out)))
911+
return vec(get_data(decode_with_schedule(encoder_schedule, DataContainer(reshape(data, :, 1)), in_or_out)))
904912
elseif isa(data, AbstractMatrix)
905913
return get_data(decode_with_schedule(encoder_schedule, DataContainer(data), in_or_out))
906914
else

src/Utilities/likelihood_informed.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ function initialize_processor!(
134134
end
135135

136136
# take samples from the appropriate distribution as prescribed by alpha
137-
samples_in, samples_out = if (:samples_in keys(input_structure_vectors)) && (:samples_out keys(output_structure_vectors))
138-
(
139-
get_structure_vec(input_structure_vectors, :samples_in)[it],
140-
get_structure_vec(output_structure_vectors, :samples_out)[it],
141-
)
142-
else
143-
@info "Structure vectors either not provided, else do not contain keys `:samples_in, :samples_out`. \n Continuing using input-output pairs as structure vectors"
144-
(in_data, out_data)
145-
end
137+
samples_in, samples_out =
138+
if (:samples_in keys(input_structure_vectors)) && (:samples_out keys(output_structure_vectors))
139+
(
140+
get_structure_vec(input_structure_vectors, :samples_in)[it],
141+
get_structure_vec(output_structure_vectors, :samples_out)[it],
142+
)
143+
else
144+
@info "Structure vectors either not provided, else do not contain keys `:samples_in, :samples_out`. \n Continuing using input-output pairs as structure vectors"
145+
(in_data, out_data)
146+
end
146147
samples_in_mean = mean(samples_in, dims = 2)
147148
samples_out_mean = mean(samples_out, dims = 2)
148149

test/Emulator/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ end
147147
# with out enc.
148148
fmw = forward_map_wrapper(G, prior, io_pairs, encoder_kwargs = (; obs_noise_cov = Σ))
149149
y_pred, y_cov = EM.predict(fmw, x_test, add_obs_noise_cov = true)
150-
@test all(isapprox(norm(yc - Σ), 0; atol = d * tol ) for yc in y_cov)
150+
@test all(isapprox(norm(yc - Σ), 0; atol = d * tol) for yc in y_cov)
151151
# try pass encoder for input
152152
new_schedule = (decorrelate_sample_cov(), "in") # for these i
153153
fmw = forward_map_wrapper(G, prior, io_pairs, encoder_schedule = new_schedule)

test/GaussianProcess/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ using CalibrateEmulateSample.Utilities
7575
Emulators.optimize_hyperparameters!(em1)
7676

7777
@test_throws ErrorException Emulators.optimize_hyperparameters!(10) # not an mlt
78-
78+
7979
μ1, σ1² = Emulators.predict(em1, new_inputs)
8080

8181
@test vec(μ1) [0.0, 1.0, 0.0, -1.0, 0.0] atol = 0.3

test/Utilities/runtests.jl

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using CalibrateEmulateSample.ParameterDistributions
1919
n_ens = 10
2020
dim_obs = 3
2121
dim_par = 2
22-
prior = constrained_gaussian("test", 1.0, 2.0, 0.0, Inf, repeats=dim_par)
22+
prior = constrained_gaussian("test", 1.0, 2.0, 0.0, Inf, repeats = dim_par)
2323
initial_ensemble = construct_initial_ensemble(rng, prior, n_ens) # params are cols
2424
y_obs = randn(rng, dim_obs)
2525
Γy = Matrix{Float64}(I, dim_obs, dim_obs)
@@ -47,33 +47,30 @@ using CalibrateEmulateSample.ParameterDistributions
4747
g_ens_final = randn(rng, dim_obs, n_ens) # add a final set out outputs.
4848
encoder_kwargs = encoder_kwargs_from(ekp, prior, final_samples_out = g_ens_final)
4949

50-
prior_kwargs = (; prior_cov=cov(prior))
51-
obs_kwargs = (; obs_noise_cov = [Γy], observation=[y_obs])
50+
prior_kwargs = (; prior_cov = cov(prior))
51+
obs_kwargs = (; obs_noise_cov = [Γy], observation = [y_obs])
5252
io_kwargs = (;
53-
input_structure_vecs = Dict(
54-
:dt => [0, get_algorithm_time(ekp)...],
55-
:samples_in => get_u(ekp),
56-
),
57-
output_structure_vecs = Dict(
58-
:dt => [0, get_algorithm_time(ekp)...],
59-
:samples_out => [get_g(ekp)..., g_ens_final],
60-
),
61-
)
53+
input_structure_vecs = Dict(:dt => [0, get_algorithm_time(ekp)...], :samples_in => get_u(ekp)),
54+
output_structure_vecs = Dict(
55+
:dt => [0, get_algorithm_time(ekp)...],
56+
:samples_out => [get_g(ekp)..., g_ens_final],
57+
),
58+
)
6259
test_kwargs = merge(prior_kwargs, obs_kwargs, io_kwargs)
63-
60+
6461
@test all(encoder_kwargs[key] == test_kwargs[key] for key in keys(encoder_kwargs))
6562
# remove final g
6663
encoder_reduced_kwargs = encoder_kwargs_from(ekp, prior)
6764
io_reduced_kwargs = (;
68-
input_structure_vecs = Dict(
69-
:dt => [0, get_algorithm_time(ekp)...][1:end-1],
70-
:samples_in => get_u(ekp)[1:end-1],
71-
),
72-
output_structure_vecs = Dict(
73-
:dt => [0, get_algorithm_time(ekp)...][1:end-1],
74-
:samples_out => [get_g(ekp)...],
75-
),
76-
)
65+
input_structure_vecs = Dict(
66+
:dt => [0, get_algorithm_time(ekp)...][1:(end - 1)],
67+
:samples_in => get_u(ekp)[1:(end - 1)],
68+
),
69+
output_structure_vecs = Dict(
70+
:dt => [0, get_algorithm_time(ekp)...][1:(end - 1)],
71+
:samples_out => [get_g(ekp)...],
72+
),
73+
)
7774
test_reduced_kwargs = merge(prior_kwargs, obs_kwargs, io_reduced_kwargs)
7875
@test all(encoder_reduced_kwargs[key] == test_reduced_kwargs[key] for key in keys(encoder_kwargs))
7976

@@ -490,6 +487,7 @@ end
490487
in_mat = in_dat * in_dat'
491488
out_mat = out_dat * out_dat'
492489
for (i_o, dat, mat) in (("in", in_dat, in_mat), ("out", out_dat, out_mat))
490+
493491
enc_dat = encode_data(encoder_schedule, dat, i_o)
494492
dec_dat = decode_data(encoder_schedule, enc_dat, i_o)
495493
enc_mat = Matrix(encode_structure_matrix(encoder_schedule, mat, i_o))
@@ -504,6 +502,13 @@ end
504502
@test isapprox(norm((D * enc_dat + b) - dec_dat), 0; atol = tol * size(D, 1))
505503
@test isapprox(norm(E * mat * E' - enc_mat), 0; atol = tol * size(E, 1)^2)
506504
@test isapprox(norm(D * enc_mat * D' - dec_mat), 0; atol = tol * size(D, 1)^2)
505+
506+
# test vec input
507+
enc_dat_vec = encode_data(encoder_schedule, vec(dat), i_o)
508+
dec_dat_vec = decode_data(encoder_schedule, enc_dat_vec, i_o)
509+
@test isapprox(norm(enc_dat_vec - vec(enc_dat)), 0; atol = tol)
510+
@test isapprox(norm(dec_dat_vec - vec(dec_dat)), 0; atol = tol)
511+
507512
end
508513
end
509514

0 commit comments

Comments
 (0)