Skip to content

Commit

Permalink
Add validate_pre_experiment-method to DESIGN_KW forward model
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-eq committed Sep 19, 2024
1 parent 71b3d90 commit ba99446
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/semeio/forward_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
ForwardModelStepDocumentation,
ForwardModelStepJSON,
ForwardModelStepPlugin,
ForwardModelStepValidationError,
ForwardModelStepWarning,
)

from .scripts.design2params import description as design2params_description
from .scripts.design_kw import description as design_kw_description
from .scripts.design_kw import design_kw as DesignKWScript


class Design2Params(ForwardModelStepPlugin):
Expand Down Expand Up @@ -45,7 +48,8 @@ class DesignKW(ForwardModelStepPlugin):
def __init__(self):
super().__init__(
name="DESIGN_KW",
command=["design_kw", "<template_file>", "<result_file>"],
command=["design_kw", "<template_file>", "<result_file>", "<VALIDATE>"],
default_mapping={"<VALIDATE>": "false"},
)

def validate_pre_realization_run(
Expand All @@ -54,7 +58,15 @@ def validate_pre_realization_run(
return fm_step_json

def validate_pre_experiment(self, fm_step_json: ForwardModelStepJSON) -> None:
pass
errors = DesignKWScript.validate_configuration(fm_step_json.get("argList")[0])
should_fail_on_validation_error = (
fm_step_json.get("argList")[2].lower() == "true"
)
error_message = f"DESIGN_KW configuration validation failed:\n{errors}"
if errors and should_fail_on_validation_error:
raise ForwardModelStepValidationError(error_message)
if errors:
ForwardModelStepWarning.warn(str(error_message))

@staticmethod
def documentation() -> Optional[ForwardModelStepDocumentation]:
Expand Down
42 changes: 41 additions & 1 deletion src/semeio/forward_models/design_kw/design_kw.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import os
import re
import shlex
from typing import List

_STATUS_FILE_NAME = "DESIGN_KW.OK"

_logger = logging.getLogger(__name__)
errors = []


def run(
Expand Down Expand Up @@ -65,7 +67,9 @@ def all_matched(line, template_file_name, template):
f"but this is probably an xml file"
)
else:
_logger.error(f"{unmatched} not found in design matrix")
error_msg = f"{unmatched} not found in design matrix"
_logger.error(error_msg)
errors.append(error_msg)
valid = False
return valid

Expand Down Expand Up @@ -178,3 +182,39 @@ def rm_genkw_prefix(paramsdict, ignoreprefixes="LOG10_"):
for keyvalue in keyvalues
if keys.count(keyvalue[0]) == 1
}


def validate_configuration(
template_file_name: str, parameters_file_name: str = "parameters.txt"
) -> List[str]:
try:
if not os.path.exists(template_file_name):
raise ValueError(
f"Argument template_file: '{template_file_name}' is not an existing file"
)

if not os.path.exists(parameters_file_name):
raise ValueError(
f"Could not find required parameters file from DESIGN2PARAMS: '{parameters_file_name}'"
)
with open(parameters_file_name, encoding="utf-8") as parameters_file:
parameters = parameters_file.readlines()

key_vals = extract_key_value(parameters)

key_vals.update(rm_genkw_prefix(key_vals))

with open(template_file_name, encoding="utf-8") as template_file:
template = template_file.readlines()

for line in template:
if not is_comment(line):
for key, value in key_vals.items():
line = line.replace(f"<{key}>", str(value))

all_matched(line, template_file_name, template)

except ValueError as e:
errors.append(str(e))

return errors
4 changes: 3 additions & 1 deletion src/semeio/forward_models/scripts/design_kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Example: If ``parameters.txt`` has the line ``MULTFLT:FLT_A10_B18 0.001``, both
of the templates ``<MULTFLT:FLT_A10_B18>`` and ``<FLT_A10_B18>`` will expand to
``0.001``.
Configuration validation pre experiment can be disabled by explicitly setting ``<VALIDATION>=false``
"""


Expand All @@ -44,7 +46,7 @@ def create_parser():
default="WARNING",
type=logging.getLevelName,
)

parser.add_argument("--validate", required=False, type=bool, default=True)
return parser


Expand Down
2 changes: 2 additions & 0 deletions src/semeio/hook_implementations/forward_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import importlib_resources

from semeio.forward_models import (
JONAK_FM,
OTS,
Design2Params,
DesignKW,
Expand Down Expand Up @@ -111,6 +112,7 @@ def installable_forward_model_steps():
return [
Design2Params,
DesignKW,
JONAK_FM,
GenDataRFT,
OTS,
Pyscal,
Expand Down

0 comments on commit ba99446

Please sign in to comment.