Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/ert/run_models/_create_run_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
logger = logging.getLogger(__name__)


def _conditionally_format_float(num: float) -> str:
str_num = str(num)
if "." not in str_num:
return str_num
float_parts = str_num.split(".")
formatted_str = f"{num:.6f}" if len(float_parts[1]) >= 6 else str(num)
return formatted_str.rstrip("0").rstrip(".")


def _backup_if_existing(path: Path) -> None:
if not path.exists():
return
Expand All @@ -59,11 +68,19 @@ def _value_export_txt(
with path.open("w") as f:
for key, param_map in values.items():
for param, value in param_map.items():
if isinstance(value, (int | float)):
if isinstance(value, (int)):
if key == DESIGN_MATRIX_GROUP:
print(f"{param} {value:g}", file=f)
print(f"{param} {value}", file=f)
else:
print(f"{key}:{param} {value:g}", file=f)
print(f"{key}:{param} {value}", file=f)
elif isinstance(value, (float)):
if key == DESIGN_MATRIX_GROUP:
print(f"{param} {_conditionally_format_float(value)}", file=f)
else:
print(
f"{key}:{param} {_conditionally_format_float(value)}",
file=f,
)
elif key == DESIGN_MATRIX_GROUP:
print(f"{param} {value}", file=f)
else:
Expand Down Expand Up @@ -203,10 +220,11 @@ def _make_param_substituter(
param_substituter = deepcopy(substituter)
for values in param_data.values():
for param_name, value in values.items():
if isinstance(value, (int, float)):
formatted_value = f"{value:.6g}"
else:
formatted_value = str(value)
formatted_value = (
_conditionally_format_float(value)
if isinstance(value, float)
else str(value)
)
param_substituter[f"<{param_name}>"] = formatted_value
return param_substituter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Realization,FOPR,WOPR_OP1_108,WOPR_OP1_144,WOPR_OP1_190,WOPR_OP1_36,WOPR_OP1_72,WOPR_OP1_9,WPR_DIFF_1
0,1572.4551,4.6631575,1.2280039,24.150873,0.16579537,16.603199,0.5786172,17.52338
1,564.73254,4.3687825,32.65306,2.25,7.5132375,7.502955,4.0,3.917212
2,760.21344,0.67585987,0.04954808,0.8789783,0.53148407,10.315068,0.56918764,21.326956
3,762.2886,0.057372905,2.0035706,89.3921,1.0729967,0.2363393,1.9635296,4.454344
4,978.6854,0.60999763,11.165132,2.3617368,0.51387596,41.033993,0.04177265,27.461798
0,1572.4546,4.6631527,1.2280097,24.150967,0.16579399,16.603184,0.57861996,17.523544
1,564.73285,4.3688073,32.65306,2.25,7.513266,7.503007,4.0,3.9172916
2,760.21466,0.6758533,0.0495441,0.8790346,0.5314902,10.31503,0.56919664,21.326956
3,762.2892,0.05737214,2.0035608,89.392296,1.0730002,0.23634046,1.9635346,4.45436
4,978.6881,0.6100113,11.165296,2.3617568,0.5138796,41.034237,0.041773625,27.461823
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Realization,MISFIT:FOPR,MISFIT:WOPR_OP1_108,MISFIT:WOPR_OP1_144,MISFIT:WOPR_OP1_190,MISFIT:WOPR_OP1_36,MISFIT:WOPR_OP1_72,MISFIT:WOPR_OP1_9,MISFIT:WPR_DIFF_1,MISFIT:TOTAL
0,1572.4551,4.6631575,1.2280039,24.150873,0.16579537,16.603199,0.5786172,17.52338,1637.3682
1,564.73254,4.3687825,32.65306,2.25,7.513238,7.502955,4.0,3.9172122,626.9378
2,760.21344,0.67585987,0.04954808,0.8789783,0.53148407,10.315068,0.56918764,21.326956,794.5605
3,762.2885,0.0573729,2.0035706,89.3921,1.0729967,0.2363393,1.9635296,4.454344,861.46875
4,978.6855,0.60999763,11.165132,2.3617368,0.51387596,41.033993,0.04177265,27.461798,1061.8737
0,1572.4546,4.6631527,1.2280097,24.150967,0.16579399,16.603184,0.57861996,17.523544,1637.3679
1,564.73285,4.3688073,32.65306,2.25,7.513266,7.503007,4.0,3.9172914,626.9383
2,760.21466,0.67585325,0.0495441,0.8790346,0.5314902,10.31503,0.56919664,21.326956,794.56177
3,762.2892,0.05737214,2.0035608,89.39229,1.0730002,0.23634046,1.9635346,4.45436,861.46954
4,978.6881,0.6100113,11.165296,2.3617568,0.5138796,41.034237,0.04177362,27.46182,1061.8767
108 changes: 105 additions & 3 deletions tests/ert/unit_tests/test_run_path_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import orjson
import polars as pl
import pytest
import xtgeo

Expand All @@ -19,14 +20,20 @@
)
from ert.plugins import get_site_plugins
from ert.run_arg import create_run_arguments
from ert.run_models._create_run_path import _make_param_substituter, create_run_path
from ert.run_models._create_run_path import (
_conditionally_format_float,
_make_param_substituter,
create_run_path,
)
from ert.run_models.model_factory import _merge_parameters
from ert.runpaths import Runpaths
from ert.sample_prior import sample_prior
from ert.storage import (
RealizationStorageState,
load_realization_parameters_and_responses,
)
from ert.substitutions import Substitutions
from tests.ert.conftest import _create_design_matrix
from tests.ert.unit_tests.config.summary_generator import simple_smspec, simple_unsmry


Expand Down Expand Up @@ -1059,7 +1066,102 @@ def test_that_parameters_as_magic_strings_are_substituted():
{
"GROUP1": {"a": 1.01},
"GROUP2": {"b": "value"},
"GROUP3": {"c": 4213131313},
},
).substitute("<a> and <b>")
== "1.01 and value"
).substitute("<a> and <b> and <c>")
== "1.01 and value and 4213131313"
)


@pytest.mark.usefixtures("use_tmpdir")
def test_design_matrix_scalars_are_not_exported_with_scientific_notation(
run_args, storage
):
"""
This is a regression test to make sure that the integers and floats will remain full
numbers in parameters.txt, paramaters.json, and template files, when
exporting values from the design matrix.
"""
design_matrix_filename = "dm.xlsx"

design_sheet = pl.DataFrame(
{"REAL": [0], "my_big_value": [4294912121], "my_small_value": [1.00000377]}
)
_create_design_matrix(design_matrix_filename, design_sheet)

run_template_file_name = "my_file.tmpl"
run_template_output_file_name = "my_file.txt"
Path(run_template_file_name).write_text(
"big is <my_big_value>\nsmall is <my_small_value>", encoding="utf-8"
)

config = ErtConfig.from_file_contents(
"NUM_REALIZATIONS 1\n"
f"DESIGN_MATRIX {design_matrix_filename} DESIGN_SHEET:DesignSheet\n"
f"RUN_TEMPLATE {run_template_file_name} {run_template_output_file_name}"
)

_, design_matrix = _merge_parameters(
config.analysis_config.design_matrix,
config.ensemble_config.parameter_configuration,
)
experiment_id = storage.create_experiment(
parameters=config.parameter_configurations_with_design_matrix,
templates=config.ert_templates,
)
prior_ensemble = storage.create_ensemble(
experiment_id, name="prior", ensemble_size=1
)
sample_prior(prior_ensemble, [0], 123, design_matrix_df=design_matrix.to_polars())
runargs = run_args(config, prior_ensemble, 1)
runpaths = Runpaths.from_config(config)
create_run_path(
run_args=runargs,
ensemble=prior_ensemble,
user_config_file=config.user_config_file,
forward_model_steps=config.forward_model_steps,
env_vars=config.env_vars,
env_pr_fm_step=config.env_pr_fm_step,
substitutions=config.substitutions,
parameters_file="parameters",
runpaths=runpaths,
)

experiment_path = Path("simulations/realization-0/iter-0")
assert experiment_path.exists()

parameters_text = experiment_path / "parameters.txt"
assert parameters_text.exists()
assert (
parameters_text.read_text(encoding="utf-8").strip()
== "my_big_value 4294912121\nmy_small_value 1.000004"
)

parameters_json = experiment_path / "parameters.json"
assert parameters_json.exists()
parameters_json_dict = orjson.loads(parameters_json.read_text(encoding="utf-8"))
assert str(parameters_json_dict["my_big_value"]["value"]) == "4294912121"
assert str(parameters_json_dict["my_small_value"]["value"]) == "1.00000377"

run_template_output = experiment_path / run_template_output_file_name
assert run_template_output.exists()
assert (
run_template_output.read_text(encoding="utf-8")
== "big is 4294912121\nsmall is 1.000004"
)


@pytest.mark.parametrize(
"input_float, expected_string",
[
(10, "10"),
(10.0, "10"),
(0.10, "0.1"),
(0.101, "0.101"),
(12.1111114, "12.111111"),
(0.100100100, "0.1001"),
(0.100100900, "0.100101"),
],
)
def test_run_path_parameters_format_floats_correctly(input_float, expected_string):
assert _conditionally_format_float(input_float) == expected_string
Loading