Skip to content

Commit 15f7a5f

Browse files
author
Yngve S. Kristiansen
committed
Adapt to new parameter setup
1 parent afc149b commit 15f7a5f

2 files changed

Lines changed: 37 additions & 42 deletions

File tree

src/semeio/workflows/ahm_analysis/ahmanalysis.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
SmootherSnapshot,
2222
smoother_update,
2323
)
24-
from ert.config import ESSettings, Field, GenKwConfig, ObservationSettings
24+
from ert.config import ESSettings, Field, ObservationSettings
2525
from ert.storage import Ensemble, Storage, open_storage
2626
from scipy.stats import ks_2samp
2727

@@ -200,39 +200,46 @@ def _replace(s: str) -> str:
200200
os.makedirs(ahmanalysis_reports_dir, exist_ok=True)
201201

202202
# create dataframe with observations vectors (1 by 1 obs and also all_obs)
203-
combinations = make_obs_groups(key_map)
203+
obs_group_to_obs_key = make_obs_groups(key_map)
204204

205205
field_parameters = [
206206
p.name
207207
for p in prior_experiment.parameter_configuration.values()
208208
if isinstance(p, Field)
209209
]
210-
gen_kws = [
210+
gen_kw_names = [
211211
p.name
212212
for p in prior_experiment.parameter_configuration.values()
213-
if isinstance(p, GenKwConfig)
213+
if p.type == "gen_kw"
214214
]
215215
if field_parameters:
216216
logger.warning(
217217
f"AHM_ANALYSIS will only evaluate scalar parameters, skipping: {field_parameters}"
218218
)
219219

220-
scalar_parameters = sorted(gen_kws)
221220
# identify the set of actual parameters that was updated for now just go
222221
# through scalar parameters but in future if easier access to field parameter
223222
# updates should also include field parameters
224-
dkeysf = get_updated_parameters(prior_data, scalar_parameters)
223+
# dkeysf = get_updated_parameters(prior_data, gen_kw_groups)
224+
updated_genkws = [
225+
p.name
226+
for p in prior_experiment.parameter_configuration.values()
227+
if p.update and p.type == "gen_kw"
228+
]
225229
# setup dataframe for calculated data
226230
kolmogorov_smirnov_data, active_obs, misfitval = (
227-
pd.DataFrame(sorted(dkeysf), columns=["Parameters"]),
231+
pd.DataFrame(sorted(updated_genkws), columns=["Parameters"]),
228232
pd.DataFrame(),
229233
pd.DataFrame(index=["misfit"]),
230234
)
235+
236+
active_obs_per_obs_group = {}
237+
misfits_per_obs_group = {}
231238
# loop over keys and calculate the KS matrix,
232239
# conditioning one parameter at the time.
233-
updated_combinations = deepcopy(combinations)
234-
for group_name, obs_group in combinations.items():
235-
print("Processing:", group_name)
240+
updated_combinations = deepcopy(obs_group_to_obs_key)
241+
for obs_group_name, obs_keys in obs_group_to_obs_key.items():
242+
print("Processing:", obs_group_name)
236243

237244
# Use localization to evaluate change of parameters for each observation
238245
# The order of the context managers is important, as we want to create a new
@@ -255,46 +262,49 @@ def _replace(s: str) -> str:
255262
update_log = _run_ministep(
256263
prior_storage=prior_ensemble,
257264
target_storage=target_ensemble,
258-
obs_group=obs_group,
259-
data_parameters=field_parameters + scalar_parameters,
265+
obs_group=obs_keys,
266+
data_parameters=field_parameters + gen_kw_names,
260267
observation_settings=observation_settings,
261268
es_settings=es_settings,
262269
random_seed=random_seed,
263270
)
264271
# Get the active vs total observation info
265272
df_update_log = make_update_log_df(update_log)
266273
except ErtAnalysisError:
267-
logger.error(f"Analysis failed for: {obs_group}")
268-
del updated_combinations[group_name]
274+
logger.error(f"Analysis failed for: {obs_keys}")
275+
del updated_combinations[obs_group_name]
269276
continue
270277
# Get the updated scalar parameter distributions
271278
target_ensemble.load_all_gen_kw_data().to_csv(
272-
ahmanalysis_reports_dir / f"{group_name}.csv"
279+
ahmanalysis_reports_dir / f"{obs_group_name}.csv"
273280
)
274281

275-
active_obs.at["ratio", group_name] = (
282+
active_obs.at["ratio", obs_group_name] = (
276283
str(count_active_observations(df_update_log))
277284
+ " active/"
278285
+ str(len(df_update_log.index))
279286
)
287+
active_obs_per_obs_group[obs_group_name] = count_active_observations(
288+
df_update_log
289+
)
290+
280291
# Get misfit values
281-
misfitval[group_name] = [
292+
misfitval[obs_group_name] = [
282293
calc_observationsgroup_misfit(
283-
group_name,
294+
obs_group_name,
284295
df_update_log,
285296
LibresFacade.load_all_misfit_data(prior_ensemble),
286297
)
287298
]
299+
300+
target_data = target_ensemble.load_all_gen_kw_data()
288301
# Calculate Ks matrix for scalar parameters
289-
kolmogorov_smirnov_data[group_name] = kolmogorov_smirnov_data[
290-
"Parameters"
291-
].map(
292-
calc_kolmogorov_smirnov(
293-
dkeysf,
294-
prior_data,
295-
target_ensemble.load_all_gen_kw_data(),
296-
)
297-
)
302+
303+
kolmogorov_smirnov_data[obs_group_name] = [
304+
ks_2samp(prior_data[dkey], target_data[dkey])[0]
305+
for dkey in sorted(updated_genkws)
306+
]
307+
298308
kolmogorov_smirnov_data.set_index("Parameters", inplace=True)
299309

300310
# save/export the Ks matrix, active_obs, misfitval and prior data

tests/workflows/ahm_analysis/test_ahm_analysis.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,6 @@ def test_make_obs_groups(input_map, expected_keys):
241241
@pytest.mark.parametrize(
242242
"prior_data, expected_result",
243243
[
244-
[
245-
{
246-
"SNAKE_OIL_PARAM:OP1_PERSISTENCE": [0, 1, 2],
247-
"SNAKE_OIL_PARAM:OP1_OCTAVES": [0, 1, 2],
248-
"SNAKE_OIL_PARAM:OP1_DIVERGENCE_SCALE": [0, 1, 2],
249-
"SNAKE_OIL_PARAM:OP1_OFFSET": [0, 0, 0],
250-
"SNAKE_OIL_PRES:BPR_138_PERSISTENCE": [0, 1, 2],
251-
},
252-
[
253-
"SNAKE_OIL_PARAM:OP1_PERSISTENCE",
254-
"SNAKE_OIL_PARAM:OP1_OCTAVES",
255-
"SNAKE_OIL_PARAM:OP1_DIVERGENCE_SCALE",
256-
"SNAKE_OIL_PRES:BPR_138_PERSISTENCE",
257-
],
258-
],
259244
[
260245
{
261246
"SNAKE_OIL_PARAM:OP1_PERSISTENCE": [0, 1, 2],

0 commit comments

Comments
 (0)