Skip to content

Commit 9e5dd21

Browse files
[Backport version-14.6] Match numeric suffixes only in experiment names
Match numeric suffixes only in experiment names Prevents crashes when names like 'es_mda_adaptive' exist (cherry picked from commit 1485aa7) Co-authored-by: Feda Curic <feda.curic@gmail.com>
1 parent 2b93885 commit 9e5dd21

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

src/ert/storage/local_storage.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logging
66
import os
7+
import re
78
import shutil
89
from collections.abc import Generator, MutableSequence
910
from datetime import datetime
@@ -546,21 +547,17 @@ def get_unique_experiment_name(self, experiment_name: str) -> str:
546547
if experiment_name not in [e.name for e in self.experiments]:
547548
return experiment_name
548549

549-
if (
550-
len(
551-
same_prefix := [
552-
e.name
553-
for e in self.experiments
554-
if e.name.startswith(experiment_name + "_")
555-
]
556-
)
557-
> 0
558-
):
559-
return (
560-
experiment_name
561-
+ "_"
562-
+ str(max(int(e[e.rfind("_") + 1 :]) for e in same_prefix) + 1)
563-
)
550+
# Only match names that follow the pattern: experiment_name_<digits>
551+
pattern = re.escape(experiment_name) + r"_(\d+)$"
552+
553+
numeric_suffixes = []
554+
for e in self.experiments:
555+
match = re.match(pattern, e.name)
556+
if match:
557+
numeric_suffixes.append(int(match.group(1)))
558+
559+
if numeric_suffixes:
560+
return experiment_name + "_" + str(max(numeric_suffixes) + 1)
564561
else:
565562
return experiment_name + "_0"
566563

tests/ert/unit_tests/storage/test_local_storage.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,25 @@ def test_get_unique_experiment_name(snake_oil_storage):
476476
assert snake_oil_storage.get_unique_experiment_name("") == "default_0"
477477

478478

479+
def test_get_unique_experiment_name_ignores_non_numeric_suffixes(tmp_path):
480+
"""
481+
Regression test for bug where non-numeric suffixes caused ValueError.
482+
483+
When an experiment like 'es_mda_adaptive' existed, attempting to generate
484+
a unique name for 'es_mda' would incorrectly try to parse 'adaptive' as
485+
an integer, causing: ValueError: invalid literal for int() with base 10: 'adaptive'
486+
487+
The fix ensures only numeric suffixes (e.g., 'es_mda_0', 'es_mda_1') are
488+
considered when generating new unique names.
489+
"""
490+
with open_storage(tmp_path, mode="w") as storage:
491+
storage.create_experiment(name="es_mda")
492+
storage.create_experiment(name="es_mda_adaptive")
493+
494+
# Should return es_mda_0, not crash on "adaptive"
495+
assert storage.get_unique_experiment_name("es_mda") == "es_mda_0"
496+
497+
479498
def add_to_name(prefix: str):
480499
def _inner(params):
481500
for param in params:

0 commit comments

Comments
 (0)