Skip to content

Commit

Permalink
Make test_es_update.py pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Yngve S. Kristiansen committed Sep 13, 2024
1 parent 3ed8872 commit cbacea4
Show file tree
Hide file tree
Showing 6 changed files with 1,101 additions and 1,113 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

94 changes: 41 additions & 53 deletions tests/unit_tests/analysis/test_es_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from unittest.mock import patch

import numpy as np
import polars
import pytest
import scipy as sp
import xarray as xr
Expand Down Expand Up @@ -48,14 +49,16 @@ def uniform_parameter():


@pytest.fixture
def obs():
return xr.Dataset(
def obs() -> polars.DataFrame:
return polars.DataFrame(
{
"observations": (["report_step", "index"], [[1.0, 1.0, 1.0]]),
"std": (["report_step", "index"], [[0.1, 1.0, 10.0]]),
},
coords={"index": [0, 1, 2], "report_step": [0]},
attrs={"response": "RESPONSE"},
"response_key": "RESPONSE",
"observation_key": "OBSERVATION",
"report_step": polars.Series(np.full(3, 0), dtype=polars.UInt16),
"index": polars.Series([0, 1, 2], dtype=polars.UInt16),
"observations": [1.0, 1.0, 1.0],
"std": [0.1, 1.0, 10.0],
}
)


Expand Down Expand Up @@ -98,7 +101,7 @@ def test_update_report(
smoother_update(
prior_ens,
posterior_ens,
list(experiment.observation_keys),
experiment.observation_keys,
ert_config.ensemble_config.parameters,
UpdateSettings(auto_scale_observations=misfit_preprocess),
ESSettings(inversion="subspace"),
Expand Down Expand Up @@ -134,7 +137,7 @@ def test_update_report_with_exception_in_analysis_ES(
smoother_update(
prior_ens,
posterior_ens,
list(ert_config.observations.keys()),
experiment.observation_keys,
ert_config.ensemble_config.parameters,
UpdateSettings(alpha=0.0000000001),
ESSettings(inversion="subspace"),
Expand Down Expand Up @@ -177,7 +180,7 @@ def test_update_report_with_different_observation_status_from_smoother_update(
ss = smoother_update(
prior_ens,
posterior_ens,
list(ert_config.observations.keys()),
experiment.observation_keys,
ert_config.ensemble_config.parameters,
update_settings,
ESSettings(inversion="subspace"),
Expand Down Expand Up @@ -293,7 +296,7 @@ def test_update_snapshot(
prior_storage=prior_ens,
posterior_storage=posterior_ens,
sies_smoother=sies_smoother,
observations=list(ert_config.observations.keys()),
observations=experiment.observation_keys,
parameters=list(ert_config.ensemble_config.parameters),
update_settings=UpdateSettings(),
analysis_config=IESSettings(inversion="subspace_exact"),
Expand All @@ -305,7 +308,7 @@ def test_update_snapshot(
smoother_update(
prior_ens,
posterior_ens,
list(ert_config.observations.keys()),
experiment.observation_keys,
list(ert_config.ensemble_config.parameters),
UpdateSettings(),
ESSettings(inversion="subspace"),
Expand All @@ -324,7 +327,7 @@ def test_update_snapshot(
assert sim_gen_kw != target_gen_kw

# Check that posterior is as expected
assert target_gen_kw == pytest.approx(expected_gen_kw)
assert target_gen_kw == pytest.approx(expected_gen_kw, rel=1e-5)


@pytest.mark.usefixtures("use_tmpdir")
Expand Down Expand Up @@ -380,7 +383,7 @@ def test_smoother_snapshot_alpha(
experiment = storage.create_experiment(
parameters=[uniform_parameter],
responses=[resp],
observations={"OBSERVATION": obs},
observations={"gen_data": obs},
)
prior_storage = storage.create_ensemble(
experiment,
Expand All @@ -405,13 +408,15 @@ def test_smoother_snapshot_alpha(
data = rng.uniform(0.8, 1, 3)
prior_storage.save_response(
"gen_data",
xr.Dataset(
{"values": (["name", "report_step", "index"], [[data]])},
coords={
"name": ["RESPONSE"],
"index": range(len(data)),
"report_step": [0],
},
polars.DataFrame(
{
"response_key": "RESPONSE",
"report_step": polars.Series(
np.full(len(data), 0), dtype=polars.UInt16
),
"index": polars.Series(range(len(data)), dtype=polars.UInt16),
"values": data,
}
),
iens,
)
Expand Down Expand Up @@ -512,19 +517,15 @@ def g(X):
grid.to_file("MY_EGRID.EGRID", "egrid")

resp = GenDataConfig(keys=["RESPONSE"])
obs = xr.Dataset(
obs = polars.DataFrame(
{
"observations": (
["report_step", "index"],
observations.reshape((1, num_observations)),
),
"std": (
["report_step", "index"],
observation_noise.reshape(1, num_observations),
),
},
coords={"report_step": [0], "index": np.arange(len(observations))},
attrs={"response": "RESPONSE"},
"response_key": "RESPONSE",
"observation_key": "OBSERVATION",
"report_step": 0,
"index": np.arange(len(observations)),
"observations": observations,
"std": observation_noise,
}
)

param_group = "PARAM_FIELD"
Expand All @@ -544,7 +545,7 @@ def g(X):
experiment = storage.create_experiment(
parameters=[config],
responses=[resp],
observations={"OBSERVATION": obs},
observations={"gen_data": obs},
)

prior_ensemble = storage.create_ensemble(
Expand All @@ -570,13 +571,13 @@ def g(X):

prior_ensemble.save_response(
"gen_data",
xr.Dataset(
{"values": (["name", "report_step", "index"], [[Y[:, iens]]])},
coords={
"name": ["RESPONSE"],
polars.DataFrame(
{
"response_key": "RESPONSE",
"report_step": 0,
"index": range(len(Y[:, iens])),
"report_step": [0],
},
"values": Y[:, iens],
}
),
iens,
)
Expand Down Expand Up @@ -750,19 +751,6 @@ def test_temporary_parameter_storage_with_inactive_fields(
np.testing.assert_array_equal(ds["values"].values[0], fields[iens]["values"])


def test_that_observations_keep_sorting(snake_oil_case_storage, snake_oil_storage):
"""
The order of the observations influence the update as it affects the
perturbations, so we make sure we maintain the order throughout.
"""
ert_config = snake_oil_case_storage
experiment = snake_oil_storage.get_experiment_by_name("ensemble-experiment")
prior_ens = experiment.get_ensemble_by_name("default_0")
assert list(ert_config.observations.keys()) == list(
prior_ens.experiment.observations.keys()
)


def _mock_load_observations_and_responses(
S,
observations,
Expand Down

0 comments on commit cbacea4

Please sign in to comment.