Skip to content

Commit

Permalink
Change validator for ensemble smoother
Browse files Browse the repository at this point in the history
Use error messages from the validators.
  • Loading branch information
oysteoh committed Aug 30, 2019
1 parent 5a58c73 commit 0d807b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
31 changes: 19 additions & 12 deletions ert_gui/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env python
import os
import sys
import re
from argparse import ArgumentParser, ArgumentTypeError
from ert_gui import run_cli
from ert_gui import ERT
from ert_gui.ide.keywords.definitions import RangeStringArgument, ProperNameFormatArgument, NumberListStringArgument
from ert_gui.ide.keywords.definitions import RangeStringArgument, ProperNameArgument, ProperNameFormatArgument, NumberListStringArgument
from ert_gui.simulation.models.multiple_data_assimilation import MultipleDataAssimilation


def strip_error_message_and_raise_exception(validated):
error = validated.message()
error = re.sub(r'\<[^>]*\>', " ", error)
raise ArgumentTypeError(error)

def valid_file(fname):
if not os.path.isfile(fname):
raise ArgumentTypeError("File was not found: {}".format(fname))
Expand All @@ -18,39 +24,40 @@ def valid_realizations(user_input):
validator = RangeStringArgument()
validated = validator.validate(user_input)
if validated.failed():
raise ArgumentTypeError(
"Defined realizations is not of correct format: {}".format(user_input))
strip_error_message_and_raise_exception(validated)
return user_input


def valid_weights(user_input):
validator = NumberListStringArgument()
validated = validator.validate(user_input)
if validated.failed():
raise ArgumentTypeError(
"Defined weights is not of correct format: {}".format(user_input))

strip_error_message_and_raise_exception(validated)
return user_input


def valid_name_format(user_input):
validator = ProperNameFormatArgument()
validated = validator.validate(user_input)
if validated.failed():
raise ArgumentTypeError(
"Defined name is not of correct format: {}".format(user_input))
strip_error_message_and_raise_exception(validated)
return user_input

def valid_name(user_input):
validator = ProperNameArgument()
validated = validator.validate(user_input)
if validated.failed():
strip_error_message_and_raise_exception(validated)
return user_input

def valid_name_format_not_default(user_input):
def valid_name_not_default(user_input):
if user_input == 'default':
msg = "Target file system and source file system can not be the same. "\
"They were both: <default>."
raise ArgumentTypeError(msg)
valid_name_format(user_input)
valid_name(user_input)
return user_input


def range_limited_int(user_input):
try:
i = int(user_input)
Expand Down Expand Up @@ -109,7 +116,7 @@ def ert_parser(parser, args):
ensemble_smoother_parser = subparsers.add_parser('ensemble_smoother',
help="run simulations in cli while performing one update on the "
"parameters by using the ensemble smoother algorithm")
ensemble_smoother_parser.add_argument('--target-case', type=valid_name_format_not_default, required=True,
ensemble_smoother_parser.add_argument('--target-case', type=valid_name_not_default, required=True,
help="This is the name of the case where the results for the "
"updated parameters will be stored")
ensemble_smoother_parser.add_argument('--verbose', action='store_true',
Expand Down
4 changes: 2 additions & 2 deletions tests/global/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def test_argparse_exec_ensemble_experiment_faulty_realizations(self):
def test_argparse_exec_ensemble_smoother_valid_case(self):
parser = ArgumentParser(prog="test_main")
parsed = ert_parser(parser, [
'ensemble_smoother', "--target-case", "some_case%d", 'test-data/local/poly_example/poly.ert'])
'ensemble_smoother', "--target-case", "some_case", 'test-data/local/poly_example/poly.ert'])
self.assertEquals(parsed.mode, "ensemble_smoother")
self.assertEquals(
parsed.config, "test-data/local/poly_example/poly.ert")
self.assertEquals(parsed.target_case, "some_case%d")
self.assertEquals(parsed.target_case, "some_case")
self.assertEquals(parsed.func.__name__, "run_cli")
self.assertFalse(parsed.verbose)

Expand Down

0 comments on commit 0d807b5

Please sign in to comment.