Skip to content

Commit a45f33f

Browse files
committed
Fix raw formatting for big numbers in parameter export and templating
This makes sure that we treat integers in raw format, while floats will remain with the existing scientific notation.
1 parent a29328d commit a45f33f

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

src/ert/run_models/_create_run_path.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ def _value_export_txt(
6060
with path.open("w") as f:
6161
for key, param_map in values.items():
6262
for param, value in param_map.items():
63-
if isinstance(value, (int | float)):
64-
if key == DESIGN_MATRIX_GROUP:
65-
print(f"{param} {value:g}", file=f)
66-
else:
67-
print(f"{key}:{param} {value:g}", file=f)
68-
elif key == DESIGN_MATRIX_GROUP:
69-
print(f"{param} {value}", file=f)
63+
if isinstance(value, int):
64+
value_str = str(value)
65+
elif isinstance(value, float):
66+
value_str = f"{value:g}"
7067
else:
71-
print(f"{key}:{param} {value}", file=f)
68+
value_str = str(value)
69+
70+
if key == DESIGN_MATRIX_GROUP:
71+
print(f"{param} {value_str}", file=f)
72+
else:
73+
print(f"{key}:{param} {value_str}", file=f)
7274

7375

7476
def _value_export_json(
@@ -214,11 +216,15 @@ def _make_param_substituter(
214216
param_substituter = deepcopy(substituter)
215217
for values in param_data.values():
216218
for param_name, value in values.items():
217-
if isinstance(value, (int, float)):
218-
formatted_value = f"{value:.6g}"
219+
if isinstance(value, int):
220+
formatted_value = str(value)
221+
elif isinstance(value, float):
222+
formatted_value = f"{value:g}"
219223
else:
220224
formatted_value = str(value)
225+
221226
param_substituter[f"<{param_name}>"] = formatted_value
227+
222228
return param_substituter
223229

224230

tests/ert/ui_tests/cli/analysis/test_design_matrix.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def test_run_poly_example_with_design_matrix_and_genkw_merge(default_values):
9494
"REAL": list(range(num_realizations)),
9595
"a": a_values,
9696
"category": 5 * ["cat1"] + 5 * ["cat2"],
97+
"big_integer": [1e10 + i for i in range(num_realizations)],
9798
}
9899
),
99100
pl.DataFrame(default_values, orient="row"),
@@ -117,10 +118,11 @@ def test_run_poly_example_with_design_matrix_and_genkw_merge(default_values):
117118
encoding="utf-8",
118119
)
119120

120-
# This adds a dummy category parameter to COEFFS GENKW
121-
# which will be overridden by the design matrix catagorical entries
121+
# This adds a dummy category and big_numbers parameter to COEFFS GENKW
122+
# which will be overridden by the design matrix entries
122123
with open("coeff_priors", "a", encoding="utf-8") as f:
123-
f.write("category UNIFORM 0 1")
124+
f.write("category UNIFORM 0 1\n")
125+
f.write("big_numbers UNIFORM 0 1\n")
124126

125127
Path("poly_eval.py").write_text(
126128
dedent(
@@ -153,6 +155,7 @@ def _evaluate(coeffs, x):
153155
b: <b>
154156
c: <c>
155157
category: <category>
158+
big_integer: <big_integer>
156159
"""
157160
),
158161
encoding="utf-8",
@@ -185,6 +188,9 @@ def _evaluate(coeffs, x):
185188
np.testing.assert_array_equal(
186189
params["category"].to_list(), 5 * ["cat1"] + 5 * ["cat2"]
187190
)
191+
np.testing.assert_array_equal(
192+
params["big_integer"].to_list(), [1e10 + i for i in range(10)]
193+
)
188194
np.testing.assert_array_equal(params["b"].to_list(), 10 * [1])
189195
np.testing.assert_array_equal(params["c"].to_list(), 10 * [2])
190196
with open("poly_out/realization-0/iter-0/my_output", encoding="utf-8") as f:
@@ -193,12 +199,14 @@ def _evaluate(coeffs, x):
193199
assert output[1] == "b: 1"
194200
assert output[2] == "c: 2"
195201
assert output[3] == "category: cat1"
202+
assert output[4] == "big_integer: 10000000000"
196203
with open("poly_out/realization-5/iter-0/my_output", encoding="utf-8") as f:
197204
output = [line.strip() for line in f]
198205
assert output[0] == "a: 5"
199206
assert output[1] == "b: 1"
200207
assert output[2] == "c: 2"
201208
assert output[3] == "category: cat2"
209+
assert output[4] == "big_integer: 10000000005"
202210

203211

204212
@pytest.mark.usefixtures(

tests/ert/unit_tests/config/test_gen_kw_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def test_gen_kw_config_get_priors(spec, expected):
177177
+ r"LOG10_KW_NAME:MY_KEYWORD "
178178
+ number_regex,
179179
),
180+
("CONST 1.54", False, "KW_NAME:MY_KEYWORD 1.54\n"),
180181
("CONST 1.0", False, "KW_NAME:MY_KEYWORD 1\n"),
181182
("DUNIF 5 1 5", False, r"KW_NAME:MY_KEYWORD " + number_regex),
182183
("ERRF 1 2 0.1 0.1", False, r"KW_NAME:MY_KEYWORD " + number_regex),

0 commit comments

Comments
 (0)