From 39bdc1f5015b3a3d71c0d4e7c7f053cf166cb426 Mon Sep 17 00:00:00 2001 From: xjules Date: Wed, 11 Sep 2024 11:42:57 +0200 Subject: [PATCH] Update wrt. review comments --- src/ert/gui/plottery/plots/ensemble.py | 2 +- src/ert/shared/storage/key_utils.py | 199 -------------------- src/ert/shared/storage/summary_key_utils.py | 199 ++++++++++++++++++++ tests/unit_tests/shared/test_rate_keys.py | 3 +- 4 files changed, 202 insertions(+), 201 deletions(-) delete mode 100644 src/ert/shared/storage/key_utils.py create mode 100644 src/ert/shared/storage/summary_key_utils.py diff --git a/src/ert/gui/plottery/plots/ensemble.py b/src/ert/gui/plottery/plots/ensemble.py index cc45a0a7dab..b045b1b395c 100644 --- a/src/ert/gui/plottery/plots/ensemble.py +++ b/src/ert/gui/plottery/plots/ensemble.py @@ -7,7 +7,7 @@ from ert.gui.plottery.plots.history import plotHistory from ert.gui.tools.plot.plot_api import EnsembleObject -from ert.shared.storage.key_utils import is_rate +from ert.shared.storage.summary_key_utils import is_rate from .observations import plotObservations from .plot_tools import PlotTools diff --git a/src/ert/shared/storage/key_utils.py b/src/ert/shared/storage/key_utils.py deleted file mode 100644 index 032a6be25b4..00000000000 --- a/src/ert/shared/storage/key_utils.py +++ /dev/null @@ -1,199 +0,0 @@ -from enum import Enum, auto -from typing import List - -special_vars = [ - "NAIMFRAC", - "NBAKFL", - "NBYTOT", - "NCPRLINS", - "NEWTFL", - "NEWTON", - "NLINEARP", - "NLINEARS", - "NLINSMAX", - "NLINSMIN", - "NLRESMAX", - "NLRESSUM", - "NMESSAGE", - "NNUMFL", - "NNUMST", - "NTS", - "NTSECL", - "NTSMCL", - "NTSPCL", - "ELAPSED", - "MAXDPR", - "MAXDSO", - "MAXDSG", - "MAXDSW", - "STEPTYPE", - "WNEWTON", -] -rate_vars = [ - "OPR", - "OIR", - "OVPR", - "OVIR", - "OFR", - "OPP", - "OPI", - "OMR", - "GPR", - "GIR", - "GVPR", - "GVIR", - "GFR", - "GPP", - "GPI", - "GMR", - "WGPR", - "WGIR", - "WPR", - "WIR", - "WVPR", - "WVIR", - "WFR", - "WPP", - "WPI", - "WMR", - "LPR", - "LFR", - "VPR", - "VIR", - "VFR", - "GLIR", - "RGR", - "EGR", - "EXGR", - "SGR", - "GSR", - "FGR", - "GIMR", - "GCR", - "NPR", - "NIR", - "CPR", - "CIR", - "SIR", - "SPR", - "TIR", - "TPR", - "GOR", - "WCT", - "OGR", - "WGR", - "GLR", -] - -seg_rate_vars = [ - "OFR", - "GFR", - "WFR", - "CFR", - "SFR", - "TFR", - "CVPR", - "WCT", - "GOR", - "OGR", - "WGR", -] - - -class SummaryVarType(Enum): - RD_SMSPEC_INVALID_VAR = auto() - RD_SMSPEC_FIELD_VAR = auto() - RD_SMSPEC_REGION_VAR = auto() - RD_SMSPEC_GROUP_VAR = auto() - RD_SMSPEC_WELL_VAR = auto() - RD_SMSPEC_SEGMENT_VAR = auto() - RD_SMSPEC_BLOCK_VAR = auto() - RD_SMSPEC_AQUIFER_VAR = auto() - RD_SMSPEC_COMPLETION_VAR = auto() - RD_SMSPEC_NETWORK_VAR = auto() - RD_SMSPEC_REGION_2_REGION_VAR = auto() - RD_SMSPEC_LOCAL_BLOCK_VAR = auto() - RD_SMSPEC_LOCAL_COMPLETION_VAR = auto() - RD_SMSPEC_LOCAL_WELL_VAR = auto() - RD_SMSPEC_MISC_VAR = auto() - - @staticmethod - def determine_var_type(var: str) -> "SummaryVarType": - if var in special_vars: - return SummaryVarType.RD_SMSPEC_MISC_VAR - default_case = { - "A": SummaryVarType.RD_SMSPEC_AQUIFER_VAR, - "B": SummaryVarType.RD_SMSPEC_BLOCK_VAR, - "C": SummaryVarType.RD_SMSPEC_COMPLETION_VAR, - "F": SummaryVarType.RD_SMSPEC_FIELD_VAR, - "G": SummaryVarType.RD_SMSPEC_GROUP_VAR, - "N": SummaryVarType.RD_SMSPEC_NETWORK_VAR, - "S": SummaryVarType.RD_SMSPEC_SEGMENT_VAR, - "W": SummaryVarType.RD_SMSPEC_WELL_VAR, - } - - if var.startswith("L"): - secondary = var[1] if len(var) > 1 else "" - return { - "B": SummaryVarType.RD_SMSPEC_LOCAL_BLOCK_VAR, - "C": SummaryVarType.RD_SMSPEC_LOCAL_COMPLETION_VAR, - "W": SummaryVarType.RD_SMSPEC_LOCAL_WELL_VAR, - }.get(secondary, SummaryVarType.RD_SMSPEC_MISC_VAR) - - if var.startswith("R"): - if len(var) == 3 and var[2] == "F": - return SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR - if var == "RNLF": - return SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR - if var == "RORFR": - return SummaryVarType.RD_SMSPEC_REGION_VAR - if len(var) >= 4 and var[2] == "F" and var[3] in {"T", "R"}: - return SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR - if len(var) >= 5 and var[3] == "F" and var[4] in {"T", "R"}: - return SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR - return SummaryVarType.RD_SMSPEC_REGION_VAR - - # default cases or miscellaneous if not matched - return default_case.get(var[0], SummaryVarType.RD_SMSPEC_MISC_VAR) - - -def match_keyword_vector(start: int, rate_vars: List[str], keyword: str) -> bool: - # Get the suffix of keyword starting from 'start' index (if not out of range) - suffix = keyword[start:] if len(keyword) > start else "" - return any(suffix.startswith(var) for var in rate_vars) - - -def match_keyword_string(start: int, rate_string: str, keyword: str) -> bool: - return keyword[start:].startswith(rate_string) - - -def is_rate(key: str) -> bool: - var_type = SummaryVarType.determine_var_type(key) - if var_type in { - SummaryVarType.RD_SMSPEC_WELL_VAR, - SummaryVarType.RD_SMSPEC_GROUP_VAR, - SummaryVarType.RD_SMSPEC_FIELD_VAR, - SummaryVarType.RD_SMSPEC_REGION_VAR, - SummaryVarType.RD_SMSPEC_COMPLETION_VAR, - SummaryVarType.RD_SMSPEC_LOCAL_WELL_VAR, - SummaryVarType.RD_SMSPEC_LOCAL_COMPLETION_VAR, - SummaryVarType.RD_SMSPEC_NETWORK_VAR, - }: - if var_type in { - SummaryVarType.RD_SMSPEC_LOCAL_WELL_VAR, - SummaryVarType.RD_SMSPEC_LOCAL_COMPLETION_VAR, - SummaryVarType.RD_SMSPEC_NETWORK_VAR, - }: - return match_keyword_vector(2, rate_vars, key) - return match_keyword_vector(1, rate_vars, key) - - if var_type == SummaryVarType.RD_SMSPEC_SEGMENT_VAR: - return match_keyword_vector(1, seg_rate_vars, key) - - if var_type == SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR: - # Region to region rates are identified by R*FR or R**FR - if match_keyword_string(2, "FR", key): - return True - return match_keyword_string(3, "FR", key) - - return False diff --git a/src/ert/shared/storage/summary_key_utils.py b/src/ert/shared/storage/summary_key_utils.py new file mode 100644 index 00000000000..784a28a96f7 --- /dev/null +++ b/src/ert/shared/storage/summary_key_utils.py @@ -0,0 +1,199 @@ +from enum import Enum, auto +from typing import List + +special_keys = [ + "NAIMFRAC", + "NBAKFL", + "NBYTOT", + "NCPRLINS", + "NEWTFL", + "NEWTON", + "NLINEARP", + "NLINEARS", + "NLINSMAX", + "NLINSMIN", + "NLRESMAX", + "NLRESSUM", + "NMESSAGE", + "NNUMFL", + "NNUMST", + "NTS", + "NTSECL", + "NTSMCL", + "NTSPCL", + "ELAPSED", + "MAXDPR", + "MAXDSO", + "MAXDSG", + "MAXDSW", + "STEPTYPE", + "WNEWTON", +] +rate_keys = [ + "OPR", + "OIR", + "OVPR", + "OVIR", + "OFR", + "OPP", + "OPI", + "OMR", + "GPR", + "GIR", + "GVPR", + "GVIR", + "GFR", + "GPP", + "GPI", + "GMR", + "WGPR", + "WGIR", + "WPR", + "WIR", + "WVPR", + "WVIR", + "WFR", + "WPP", + "WPI", + "WMR", + "LPR", + "LFR", + "VPR", + "VIR", + "VFR", + "GLIR", + "RGR", + "EGR", + "EXGR", + "SGR", + "GSR", + "FGR", + "GIMR", + "GCR", + "NPR", + "NIR", + "CPR", + "CIR", + "SIR", + "SPR", + "TIR", + "TPR", + "GOR", + "WCT", + "OGR", + "WGR", + "GLR", +] + +seg_rate_keys = [ + "OFR", + "GFR", + "WFR", + "CFR", + "SFR", + "TFR", + "CVPR", + "WCT", + "GOR", + "OGR", + "WGR", +] + + +class SummaryKeyType(Enum): + INVALID = auto() + FIELD = auto() + REGION = auto() + GROUP = auto() + WELL = auto() + SEGMENT = auto() + BLOCK = auto() + AQUIFER = auto() + COMPLETION = auto() + NETWORK = auto() + REGION_2_REGION = auto() + LOCAL_BLOCK = auto() + LOCAL_COMPLETION = auto() + LOCAL_WELL = auto() + MISC = auto() + + @staticmethod + def determine_key_type(var: str) -> "SummaryKeyType": + if var in special_keys: + return SummaryKeyType.MISC + default_case = { + "A": SummaryKeyType.AQUIFER, + "B": SummaryKeyType.BLOCK, + "C": SummaryKeyType.COMPLETION, + "F": SummaryKeyType.FIELD, + "G": SummaryKeyType.GROUP, + "N": SummaryKeyType.NETWORK, + "S": SummaryKeyType.SEGMENT, + "W": SummaryKeyType.WELL, + } + + if var.startswith("L"): + secondary = var[1] if len(var) > 1 else "" + return { + "B": SummaryKeyType.LOCAL_BLOCK, + "C": SummaryKeyType.LOCAL_COMPLETION, + "W": SummaryKeyType.LOCAL_WELL, + }.get(secondary, SummaryKeyType.MISC) + + if var.startswith("R"): + if len(var) == 3 and var[2] == "F": + return SummaryKeyType.REGION_2_REGION + if var == "RNLF": + return SummaryKeyType.REGION_2_REGION + if var == "RORFR": + return SummaryKeyType.REGION + if len(var) >= 4 and var[2] == "F" and var[3] in {"T", "R"}: + return SummaryKeyType.REGION_2_REGION + if len(var) >= 5 and var[3] == "F" and var[4] in {"T", "R"}: + return SummaryKeyType.REGION_2_REGION + return SummaryKeyType.REGION + + # default cases or miscellaneous if not matched + return default_case.get(var[0], SummaryKeyType.MISC) + + +def match_keyword_vector(start: int, rate_keys: List[str], keyword: str) -> bool: + # Get the suffix of keyword starting from 'start' index (if not out of range) + suffix = keyword[start:] if len(keyword) > start else "" + return any(suffix.startswith(var) for var in rate_keys) + + +def match_keyword_string(start: int, rate_string: str, keyword: str) -> bool: + return keyword[start:].startswith(rate_string) + + +def is_rate(key: str) -> bool: + var_type = SummaryKeyType.determine_key_type(key) + if var_type in { + SummaryKeyType.WELL, + SummaryKeyType.GROUP, + SummaryKeyType.FIELD, + SummaryKeyType.REGION, + SummaryKeyType.COMPLETION, + SummaryKeyType.LOCAL_WELL, + SummaryKeyType.LOCAL_COMPLETION, + SummaryKeyType.NETWORK, + }: + if var_type in { + SummaryKeyType.LOCAL_WELL, + SummaryKeyType.LOCAL_COMPLETION, + SummaryKeyType.NETWORK, + }: + return match_keyword_vector(2, rate_keys, key) + return match_keyword_vector(1, rate_keys, key) + + if var_type == SummaryKeyType.SEGMENT: + return match_keyword_vector(1, seg_rate_keys, key) + + if var_type == SummaryKeyType.REGION_2_REGION: + # Region to region rates are identified by R*FR or R**FR + if match_keyword_string(2, "FR", key): + return True + return match_keyword_string(3, "FR", key) + + return False diff --git a/tests/unit_tests/shared/test_rate_keys.py b/tests/unit_tests/shared/test_rate_keys.py index 109ff70dc0c..d29e0d4c20c 100644 --- a/tests/unit_tests/shared/test_rate_keys.py +++ b/tests/unit_tests/shared/test_rate_keys.py @@ -1,6 +1,7 @@ import hypothesis.strategies as st from hypothesis import given -from src.ert.shared.storage.key_utils import is_rate + +from ert.shared.storage.summary_key_utils import is_rate def nonempty_string_without_whitespace():