Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analysis workflow as optional #90

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/flownet/ahm/_ahm_iteration_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from ecl.summary import EclSum

from ..config_parser import parse_config
from ..data import FlowData


Expand Down Expand Up @@ -336,13 +335,17 @@ def save_iteration_analytics():
Nothing
"""

parser = argparse.ArgumentParser(prog=("Save iteration parameters to a file."))
parser.add_argument("config", type=str, help="Path to the ERT config file.")
parser = argparse.ArgumentParser(prog=("Save iteration analytics to a file."))
parser.add_argument(
"reference_simulation", type=str, help="Path to the reference simulation case"
)
parser.add_argument(
"perforation_strategy", type=str, help="Perforation handling strategy"
)
parser.add_argument("runpath", type=str, help="Path to the ERT runpath.")
parser.add_argument(
"eclbase", type=str, help="Path to the simulation from runpath."
)
parser.add_argument("yamlobs", type=str, help="Path to the yaml observation file.")
parser.add_argument(
"start", type=str, help="Start date (YYYY-MM-DD) for accuracy analysis."
)
Expand Down Expand Up @@ -376,11 +379,9 @@ def save_iteration_analytics():
)

# Load reference simulation (OPM-Flow/Eclipse)
config = parse_config(pathlib.Path(args.config))

field_data = FlowData(
config.flownet.data_source.input_case,
perforation_handling_strategy=config.flownet.perforation_handling_strategy,
args.reference_simulation,
perforation_handling_strategy=args.perforation_strategy,
)
df_obs: pd.DataFrame = field_data.production
df_obs["DATE"] = df_obs["date"]
Expand All @@ -398,7 +399,7 @@ def save_iteration_analytics():
for str_key in list(args.quantity.replace("[", "").replace("]", "").split(",")):
# Prepare data from reference simulation
tmp_data = filter_dataframe(
df_obs, "DATE", np.datetime64(args.start), np.datetime64(args.end)
df_obs, "DATE", np.datetime64(args.start), np.datetime64(args.end),
)

truth_data = pd.DataFrame()
Expand All @@ -407,7 +408,6 @@ def save_iteration_analytics():
truth_data[str_key[:5] + well] = tmp_data[tmp_data["WELL_NAME"] == well][
str_key[:4]
].values
truth_data.to_csv("truth_data.csv", index=False)

obs_opm = prepare_opm_reference_data(
truth_data, str_key, len(realizations_dict)
Expand All @@ -418,15 +418,15 @@ def save_iteration_analytics():
ens_flownet.append(
prepare_flownet_data(
filter_dataframe(
df_sim, "DATE", np.datetime64(args.start), np.datetime64(args.end)
df_sim, "DATE", np.datetime64(args.start), np.datetime64(args.end),
),
str_key,
len(realizations_dict),
)
)

filter_dataframe(
df_sim, "DATE", np.datetime64(args.start), np.datetime64(args.end)
df_sim, "DATE", np.datetime64(args.start), np.datetime64(args.end),
).to_csv("ens_flownet_data.csv", index=False)

# Normalizing data
Expand Down
8 changes: 8 additions & 0 deletions src/flownet/ahm/_assisted_history_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def __init__(
schedule: Schedule,
parameters: List[Parameter],
case_name: str,
perforation_strategy: str,
reference_simulation: str,
ert_config: Dict,
random_seed: Optional[int] = None,
):
Expand All @@ -50,6 +52,8 @@ def __init__(
schedule: Schedule instance
parameters: List of Parameter objects
case_name: Name of simulation case
perforation_strategy: String indicating perforation handling strategy
reference_simulation: String indicating path to reference simulation case
ert_config: Dictionary containing information about queue (system, name, server and max_running)
and realizations (num_realizations, required_success_percent and max_runtime)
random_seed: Random seed to control reproducibility of FlowNet
Expand All @@ -58,6 +62,8 @@ def __init__(
self._network: NetworkModel = network
self._schedule: Schedule = schedule
self._parameters: List[Parameter] = parameters
self._perforation_strategy: str = perforation_strategy
self._reference_simulation: str = reference_simulation
self._ert_config: dict = ert_config
self._case_name: str = case_name
self._random_seed: Optional[int] = random_seed
Expand All @@ -82,6 +88,8 @@ def create_ert_setup(self, args: argparse.Namespace, training_set_fraction: floa
args,
self._network,
self._schedule,
perforation_strategy=self._perforation_strategy,
reference_simulation=self._reference_simulation,
ert_config=self._ert_config,
parameters=self._parameters,
random_seed=self._random_seed,
Expand Down
2 changes: 2 additions & 0 deletions src/flownet/ahm/_run_ahm.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ def run_flownet_history_matching(
schedule,
parameters,
case_name=config.name,
perforation_strategy=config.flownet.perforation_handling_strategy,
reference_simulation=config.flownet.data_source.input_case,
ert_config=config.ert._asdict(),
random_seed=config.flownet.random_seed,
)
Expand Down
16 changes: 5 additions & 11 deletions src/flownet/config_parser/_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,11 @@ def create_schema(_to_abs_path) -> Dict:
"analysis": {
MK.Type: types.NamedDict,
MK.Content: {
"metric": {MK.Type: types.String, MK.Default: "[RMSE]"},
"quantity": {
MK.Type: types.String,
MK.Default: "[WOPR:BR-P-]",
},
"start": {MK.Type: types.String, MK.Default: "2001-04-01"},
"end": {MK.Type: types.String, MK.Default: "2006-01-01"},
"outfile": {
MK.Type: types.String,
MK.Default: "analysis_metrics_iteration",
},
"metric": {MK.Type: types.String, MK.AllowNone: True},
"quantity": {MK.Type: types.String, MK.AllowNone: True,},
"start": {MK.Type: types.String, MK.AllowNone: True},
"end": {MK.Type: types.String, MK.AllowNone: True},
"outfile": {MK.Type: types.String, MK.AllowNone: True,},
},
},
},
Expand Down
12 changes: 11 additions & 1 deletion src/flownet/ert/_create_ert_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def create_ert_setup( # pylint: disable=too-many-arguments
ert_config: dict,
parameters=None,
random_seed=None,
perforation_strategy: str = None,
reference_simulation: str = None,
training_set_fraction: float = 1,
prediction_setup: bool = False,
):
Expand All @@ -114,6 +116,12 @@ def create_ert_setup( # pylint: disable=too-many-arguments
output_folder = pathlib.Path(args.output_folder)
os.makedirs(output_folder, exist_ok=True)

# Derive absolute path to reference simulation case
if reference_simulation:
path_ref_sim = pathlib.Path(reference_simulation).resolve()
else:
path_ref_sim = pathlib.Path(".").resolve()

if prediction_setup:
ert_config_file = output_folder / "pred_config.ert"
template = _TEMPLATE_ENVIRONMENT.get_template("pred_config.ert.jinja2")
Expand All @@ -125,7 +133,7 @@ def create_ert_setup( # pylint: disable=too-many-arguments
with open(output_folder / "network.pickled", "wb") as fh:
pickle.dump(network, fh)

# Pickle schdule
# Pickle schedule
with open(output_folder / "schedule.pickled", "wb") as fh:
pickle.dump(schedule, fh)

Expand All @@ -141,6 +149,8 @@ def create_ert_setup( # pylint: disable=too-many-arguments
"pickled_parameters": output_folder.resolve()
/ "parameters.pickled",
"random_seed": random_seed,
"perforation_strategy": perforation_strategy,
"reference_simulation": path_ref_sim,
"ert_config": ert_config,
"debug": args.debug if hasattr(args, "debug") else False,
"pred_schedule_file": getattr(
Expand Down
3 changes: 1 addition & 2 deletions src/flownet/static/SAVE_ITERATION_ANALYTICS_WORKFLOW
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
SAVE_ITERATION_ANALYTICS_WORKFLOW_JOB <RUN_PATH> <ECL_BASE> <YAML_OBS> <ANALYSIS_START> <ANALYSIS_END> <ANALYSIS_QUANTITY> <ANALYSIS_METRIC> <ANALYSIS_OUTFILE>

SAVE_ITERATION_ANALYTICS_WORKFLOW_JOB <REFERENCE_SIMULATION> <PERFORATION_STRATEGY> <RUN_PATH> <ECL_BASE> <ANALYSIS_START> <ANALYSIS_END> <ANALYSIS_QUANTITY> <ANALYSIS_METRIC> <ANALYSIS_OUTFILE>
6 changes: 3 additions & 3 deletions src/flownet/static/SAVE_ITERATION_ANALYTICS_WORKFLOW_JOB
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INTERNAL FALSE
EXECUTABLE flownet_save_iteration_analytics
MIN_ARG 8
MAX_ARG 8
MIN_ARG 9
MAX_ARG 9
ARG_TYPE 0 STRING
ARG_TYPE 1 STRING
ARG_TYPE 2 STRING
Expand All @@ -10,4 +10,4 @@ ARG_TYPE 4 STRING
ARG_TYPE 5 STRING
ARG_TYPE 6 STRING
ARG_TYPE 7 STRING

ARG_TYPE 8 STRING
17 changes: 11 additions & 6 deletions src/flownet/templates/ahm_config.ert.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
INSTALL_JOB DELETE_IN_CURRENT_ITERATION ./DELETE_IN_CURRENT_ITERATION

REFCASE <CONFIG_PATH>/SYNTHETIC_REFCASE
DEFINE <RUN_PATH> {{ ert_config.runpath }}
DEFINE <ECL_BASE> {{ ert_config.eclbase }}
DEFINE <RANDOM_SAMPLES> ./parameters.json
DEFINE <YAML_OBS> {{ ert_config.yamlobs }}
{%- if ert_config.analysis.metric: %}
DEFINE <ANALYSIS_METRIC> {{ ert_config.analysis.metric }}
DEFINE <ANALYSIS_QUANTITY> {{ ert_config.analysis.quantity }}
DEFINE <ANALYSIS_START> {{ ert_config.analysis.start }}
DEFINE <ANALYSIS_END> {{ ert_config.analysis.end }}
DEFINE <ANALYSIS_OUTFILE> {{ ert_config.analysis.outfile }}
DEFINE <PERFORATION_STRATEGY> {{ perforation_strategy }}
DEFINE <REFERENCE_SIMULATION> {{ reference_simulation }}
{%- endif %}
DEFINE <RUN_PATH> {{ ert_config.runpath }}
DEFINE <ECL_BASE> {{ ert_config.eclbase }}
DEFINE <RANDOM_SAMPLES> ./parameters.json

INSTALL_JOB CREATE_FLOWNET_MODEL ./CREATE_FLOWNET_MODEL
{%- if not debug: %}
INSTALL_JOB DELETE_IN_CURRENT_ITERATION ./DELETE_IN_CURRENT_ITERATION
{%- endif %}

LOAD_WORKFLOW_JOB ./SAVE_ITERATION_PARAMETERS_WORKFLOW_JOB
LOAD_WORKFLOW_JOB ./SAVE_ITERATION_ANALYTICS_WORKFLOW_JOB
LOAD_WORKFLOW SAVE_ITERATION_PARAMETERS_WORKFLOW
LOAD_WORKFLOW SAVE_ITERATION_ANALYTICS_WORKFLOW
HOOK_WORKFLOW SAVE_ITERATION_PARAMETERS_WORKFLOW PRE_SIMULATION
{%- if ert_config.analysis.metric: %}
LOAD_WORKFLOW_JOB ./SAVE_ITERATION_ANALYTICS_WORKFLOW_JOB
LOAD_WORKFLOW SAVE_ITERATION_ANALYTICS_WORKFLOW
HOOK_WORKFLOW SAVE_ITERATION_ANALYTICS_WORKFLOW POST_SIMULATION
{%- endif %}

GEN_KW FLOWNET_PARAMETERS ./EMPTYFILE ./EMPTYFILE ./parameters.ertparam

Expand Down