Skip to content

Commit

Permalink
Rename substitution_list
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Oct 23, 2024
1 parent 2c94d39 commit a867d62
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 120 deletions.
48 changes: 24 additions & 24 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from typing_extensions import Self

from ert.plugins import ErtPluginManager
from ert.substitution_list import SubstitutionList
from ert.substitutions import Substitutions

from ._get_num_cpu import get_num_cpu_from_data_file
from .analysis_config import AnalysisConfig
Expand Down Expand Up @@ -89,7 +89,7 @@ class ErtConfig:
DEFAULT_RUNPATH_FILE: ClassVar[str] = ".ert_runpath_list"
PREINSTALLED_FORWARD_MODEL_STEPS: ClassVar[Dict[str, ForwardModelStep]] = {}

substitution_list: SubstitutionList = field(default_factory=SubstitutionList)
substitutions: Substitutions = field(default_factory=Substitutions)
ensemble_config: EnsembleConfig = field(default_factory=EnsembleConfig)
ens_path: str = DEFAULT_ENSPATH
env_vars: Dict[str, str] = field(default_factory=dict)
Expand Down Expand Up @@ -233,13 +233,13 @@ def from_file_contents(

@classmethod
def from_dict(cls, config_dict) -> Self:
substitution_list = _substitution_list_from_dict(config_dict)
substitutions = _substitutions_from_dict(config_dict)
runpath_file = config_dict.get(
ConfigKeys.RUNPATH_FILE, ErtConfig.DEFAULT_RUNPATH_FILE
)
substitution_list["<RUNPATH_FILE>"] = runpath_file
config_dir = substitution_list.get("<CONFIG_PATH>", "")
config_file = substitution_list.get("<CONFIG_FILE>", "no_config")
substitutions["<RUNPATH_FILE>"] = runpath_file
config_dir = substitutions.get("<CONFIG_PATH>", "")
config_file = substitutions.get("<CONFIG_FILE>", "no_config")
config_file_path = path.join(config_dir, config_file)

errors = cls._validate_dict(config_dict, config_file)
Expand All @@ -257,9 +257,9 @@ def from_dict(cls, config_dict) -> Self:
model_config = ModelConfig.from_dict(config_dict)
runpath = model_config.runpath_format_string
eclbase = model_config.eclbase_format_string
substitution_list["<RUNPATH>"] = runpath
substitution_list["<ECL_BASE>"] = eclbase
substitution_list["<ECLBASE>"] = eclbase
substitutions["<RUNPATH>"] = runpath
substitutions["<ECL_BASE>"] = eclbase
substitutions["<ECLBASE>"] = eclbase
except ConfigValidationError as e:
errors.append(e)
except PydanticValidationError as err:
Expand All @@ -270,7 +270,7 @@ def from_dict(cls, config_dict) -> Self:

try:
workflow_jobs, workflows, hooked_workflows = cls._workflows_from_dict(
config_dict, substitution_list
config_dict, substitutions
)
except ConfigValidationError as e:
errors.append(e)
Expand Down Expand Up @@ -340,7 +340,7 @@ def from_dict(cls, config_dict) -> Self:
env_vars[key] = val

return cls(
substitution_list=substitution_list,
substitutions=substitutions,
ensemble_config=ensemble_config,
ens_path=config_dict.get(ConfigKeys.ENSPATH, ErtConfig.DEFAULT_ENSPATH),
env_vars=env_vars,
Expand All @@ -355,7 +355,7 @@ def from_dict(cls, config_dict) -> Self:
installed_forward_model_steps=installed_forward_model_steps,
forward_model_steps=cls._create_list_of_forward_model_steps_to_run(
installed_forward_model_steps,
substitution_list,
substitutions,
config_dict,
),
model_config=model_config,
Expand Down Expand Up @@ -551,7 +551,7 @@ def _validate_dict(
def _create_list_of_forward_model_steps_to_run(
cls,
installed_steps: Dict[str, ForwardModelStep],
substitution_list: SubstitutionList,
substitutions: Substitutions,
config_dict,
) -> List[ForwardModelStep]:
errors = []
Expand All @@ -562,7 +562,7 @@ def _create_list_of_forward_model_steps_to_run(
else:
unsubstituted_step_name = fm_step_description[0]
args = []
fm_step_name = substitution_list.substitute(unsubstituted_step_name)
fm_step_name = substitutions.substitute(unsubstituted_step_name)
try:
fm_step = copy.deepcopy(installed_steps[fm_step_name])

Expand All @@ -577,7 +577,7 @@ def _create_list_of_forward_model_steps_to_run(
)
)
continue
fm_step.private_args = SubstitutionList()
fm_step.private_args = Substitutions()
for key, val in args:
fm_step.private_args[key] = val

Expand Down Expand Up @@ -617,7 +617,7 @@ def _create_list_of_forward_model_steps_to_run(
try:
substituted_json = cls._create_forward_model_json(
run_id=None,
context=substitution_list,
context=substitutions,
forward_model_steps=[fm_step],
skip_pre_experiment_validation=True,
)
Expand Down Expand Up @@ -654,7 +654,7 @@ def forward_model_data_to_json(
if context_env is None:
context_env = {}
return self._create_forward_model_json(
context=self.substitution_list,
context=self.substitutions,
forward_model_steps=self.forward_model_steps,
user_config_file=self.user_config_file,
env_vars={**self.env_vars, **context_env},
Expand All @@ -666,7 +666,7 @@ def forward_model_data_to_json(
@classmethod
def _create_forward_model_json(
cls,
context: SubstitutionList,
context: Substitutions,
forward_model_steps: List[ForwardModelStep],
run_id: Optional[str],
iens: int = 0,
Expand All @@ -688,7 +688,7 @@ def __init__(self, fm_step):
f"parsing forward model step `FORWARD_MODEL {fm_step_description}` - "
"reconstructed, with defines applied during parsing"
)
self.copy_private_args = SubstitutionList()
self.copy_private_args = Substitutions()
for key, val in fm_step.private_args.items():
self.copy_private_args[key] = context.substitute_real_iter(
val, iens, itr
Expand Down Expand Up @@ -805,7 +805,7 @@ def handle_default(fm_step: ForwardModelStep, arg: str) -> str:
def _workflows_from_dict(
cls,
content_dict,
substitution_list,
substitutions,
):
workflow_job_info = content_dict.get(ConfigKeys.LOAD_WORKFLOW_JOB, [])
workflow_job_dir_info = content_dict.get(ConfigKeys.WORKFLOW_JOB_DIRECTORY, [])
Expand Down Expand Up @@ -875,7 +875,7 @@ def _workflows_from_dict(
existed = filename in workflows
workflow = Workflow.from_file(
work[0],
substitution_list,
substitutions,
workflow_jobs,
)
for job, args in workflow:
Expand Down Expand Up @@ -967,7 +967,7 @@ def _installed_forward_model_steps_from_dict(

@property
def preferred_num_cpu(self) -> int:
return int(self.substitution_list.get(f"<{ConfigKeys.NUM_CPU}>", 1))
return int(self.substitutions.get(f"<{ConfigKeys.NUM_CPU}>", 1))

def _create_observations(
self,
Expand Down Expand Up @@ -1057,8 +1057,8 @@ def _get_files_in_directory(job_path, errors):
return files


def _substitution_list_from_dict(config_dict) -> SubstitutionList:
subst_list = SubstitutionList()
def _substitutions_from_dict(config_dict) -> Substitutions:
subst_list = Substitutions()

for key, val in config_dict.get("DEFINE", []):
subst_list[key] = val
Expand Down
6 changes: 3 additions & 3 deletions src/ert/config/forward_model_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing_extensions import NotRequired, Unpack

from ert.config.parsing.config_errors import ConfigWarning
from ert.substitution_list import SubstitutionList
from ert.substitutions import Substitutions

from .parsing import (
SchemaItemType,
Expand Down Expand Up @@ -164,7 +164,7 @@ class ForwardModelStep:
environment: dict[str, Union[int, str]] = field(default_factory=dict)
exec_env: dict[str, Union[int, str]] = field(default_factory=dict)
default_mapping: dict[str, Union[int, str]] = field(default_factory=dict)
private_args: SubstitutionList = field(default_factory=SubstitutionList)
private_args: Substitutions = field(default_factory=Substitutions)

default_env: ClassVar[dict[str, str]] = {
"_ERT_ITERATION_NUMBER": "<ITER>",
Expand Down Expand Up @@ -251,7 +251,7 @@ def __init__(
environment=environment,
exec_env=exec_env,
default_mapping=default_mapping,
private_args=SubstitutionList(),
private_args=Substitutions(),
)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions src/ert/config/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .parsing import ConfigValidationError, ErrorInfo, init_workflow_schema, parse

if TYPE_CHECKING:
from ert.substitution_list import SubstitutionList
from ert.substitutions import Substitutions

from .workflow_job import WorkflowJob

Expand Down Expand Up @@ -89,7 +89,7 @@ def _parse_command_list(
def from_file(
cls,
src_file: str,
context: Optional[SubstitutionList],
context: Optional[Substitutions],
job_dict: Dict[str, WorkflowJob],
) -> "Workflow":
if not os.path.exists(src_file):
Expand Down
6 changes: 3 additions & 3 deletions src/ert/enkf_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ def create_run_path(
if context_env is None:
context_env = {}
t = time.perf_counter()
substitution_list = ert_config.substitution_list
substitutions = ert_config.substitutions
runpaths.set_ert_ensemble(ensemble.name)
for run_arg in run_args:
run_path = Path(run_arg.runpath)
if run_arg.active:
run_path.mkdir(parents=True, exist_ok=True)
for source_file, target_file in ert_config.ert_templates:
target_file = substitution_list.substitute_real_iter(
target_file = substitutions.substitute_real_iter(
target_file, run_arg.iens, ensemble.iteration
)
try:
Expand All @@ -207,7 +207,7 @@ def create_run_path(
f"Unsupported non UTF-8 character found in file: {source_file}"
) from e

result = substitution_list.substitute_real_iter(
result = substitutions.substitute_real_iter(
file_content,
run_arg.iens,
ensemble.iteration,
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/tools/export/export_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
description = "The CSV export requires some information before it starts:"
super().__init__("export", description, parent)

subs_list = ert_config.substitution_list
subs_list = ert_config.substitutions
default_csv_output_path = subs_list.get("<CSV_OUTPUT_PATH>", "output.csv")
self.output_path_model = PathModel(default_csv_output_path)
output_path_chooser = PathChooser(self.output_path_model)
Expand Down
2 changes: 1 addition & 1 deletion src/ert/libres_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def load_from_forward_model(
jobname_format=self.config.model_config.jobname_format_string,
runpath_format=self.config.model_config.runpath_format_string,
filename=str(self.config.runpath_file),
substitution_list=self.config.substitution_list,
substitutions=self.config.substitutions,
eclbase=self.config.model_config.eclbase_format_string,
),
realisations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def run(self, ert_config: ErtConfig, workflow_args: List[Any]) -> None:
jobname_format=ert_config.model_config.jobname_format_string,
runpath_format=ert_config.model_config.runpath_format_string,
filename=str(ert_config.runpath_file),
substitution_list=ert_config.substitution_list,
substitutions=ert_config.substitutions,
eclbase=ert_config.model_config.eclbase_format_string,
)
run_paths.write_runpath_list(
Expand Down
6 changes: 3 additions & 3 deletions src/ert/run_models/base_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ def __init__(
self._context_env: Dict[str, str] = {}
self.random_seed: int = _seed_sequence(random_seed)
self.rng = np.random.default_rng(self.random_seed)
self.substitution_list = config.substitution_list
self.substitutions = config.substitutions

self.run_paths = Runpaths(
jobname_format=config.model_config.jobname_format_string,
runpath_format=config.model_config.runpath_format_string,
filename=str(config.runpath_file),
substitution_list=self.substitution_list,
substitutions=self.substitutions,
eclbase=config.model_config.eclbase_format_string,
)
self._iter_snapshot: Dict[int, EnsembleSnapshot] = {}
Expand All @@ -198,7 +198,7 @@ def log_at_startup(self) -> None:
"ert_config",
"rng",
"run_paths",
"substitution_list",
"substitutions",
]
settings_dict = {
key: value
Expand Down
18 changes: 9 additions & 9 deletions src/ert/runpaths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import Iterable, List, Optional, Union

from ert.substitution_list import SubstitutionList
from ert.substitutions import Substitutions


class Runpaths:
Expand Down Expand Up @@ -33,30 +33,30 @@ def __init__(
jobname_format: str,
runpath_format: str,
filename: Union[str, Path] = ".ert_runpath_list",
substitution_list: Optional[SubstitutionList] = None,
substitutions: Optional[Substitutions] = None,
eclbase: Optional[str] = None,
):
self._jobname_format = jobname_format
self.runpath_list_filename = Path(filename)
self._runpath_format = str(Path(runpath_format).resolve())
self._substitution_list = substitution_list or SubstitutionList()
self._substitutions = substitutions or Substitutions()
self._eclbase = eclbase

def set_ert_ensemble(self, ensemble_name: str) -> None:
self._substitution_list["<ERT-CASE>"] = ensemble_name
self._substitution_list["<ERTCASE>"] = ensemble_name
self._substitutions["<ERT-CASE>"] = ensemble_name
self._substitutions["<ERTCASE>"] = ensemble_name

def get_paths(self, realizations: Iterable[int], iteration: int) -> List[str]:
return [
self._substitution_list.substitute_real_iter(
self._substitutions.substitute_real_iter(
self._runpath_format, realization, iteration
)
for realization in realizations
]

def get_jobnames(self, realizations: Iterable[int], iteration: int) -> List[str]:
return [
self._substitution_list.substitute_real_iter(
self._substitutions.substitute_real_iter(
self._jobname_format, realization, iteration
)
for realization in realizations
Expand Down Expand Up @@ -90,12 +90,12 @@ def write_runpath_list(
with open(self.runpath_list_filename, "w", encoding="utf-8") as filehandle:
for iteration in iteration_numbers:
for realization in realization_numbers:
job_name_or_eclbase = self._substitution_list.substitute_real_iter(
job_name_or_eclbase = self._substitutions.substitute_real_iter(
self._eclbase if self._eclbase else self._jobname_format,
realization,
iteration,
)
runpath = self._substitution_list.substitute_real_iter(
runpath = self._substitutions.substitute_real_iter(
self._runpath_format, realization, iteration
)

Expand Down
4 changes: 2 additions & 2 deletions src/ert/simulator/batch_simulator_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __post_init__(self) -> None:
driver, max_running=self.ert_config.queue_config.max_running
)
# fill in the missing geo_id data
global_substitutions = self.ert_config.substitution_list
global_substitutions = self.ert_config.substitutions
global_substitutions["<CASE_NAME>"] = _slug(self.ensemble.name)
for sim_id, (geo_id, _) in enumerate(self.case_data):
if self.mask[sim_id]:
Expand All @@ -138,7 +138,7 @@ def __post_init__(self) -> None:
jobname_format=ert_config.model_config.jobname_format_string,
runpath_format=ert_config.model_config.runpath_format_string,
filename=str(ert_config.runpath_file),
substitution_list=global_substitutions,
substitutions=global_substitutions,
eclbase=ert_config.model_config.eclbase_format_string,
)
self.run_args = create_run_arguments(
Expand Down
Loading

0 comments on commit a867d62

Please sign in to comment.