2
2
3
3
import math
4
4
import os
5
- import shutil
6
5
import warnings
7
6
from collections .abc import Callable
8
7
from dataclasses import dataclass
16
15
from scipy .stats import norm
17
16
from typing_extensions import TypedDict
18
17
19
- from ert .substitutions import substitute_runpath_name
20
-
21
18
from ._str_to_bool import str_to_bool
22
19
from .parameter_config import ParameterConfig
23
20
from .parsing import ConfigValidationError , ConfigWarning , ErrorInfo
@@ -59,10 +56,7 @@ class TransformFunctionDefinition:
59
56
60
57
@dataclass
61
58
class GenKwConfig (ParameterConfig ):
62
- template_file : str | None
63
- output_file : str | None
64
59
transform_function_definitions : list [TransformFunctionDefinition ]
65
- forward_init_file : str | None = None
66
60
67
61
def __post_init__ (self ) -> None :
68
62
self .transform_functions : list [TransformFunction ] = []
@@ -86,31 +80,19 @@ def __len__(self) -> int:
86
80
return len (self .transform_functions )
87
81
88
82
@classmethod
89
- def from_config_list (cls , gen_kw : list [str | dict [str , str ]]) -> Self :
83
+ def templates_from_config (
84
+ cls , gen_kw : list [str | dict [str , str ]]
85
+ ) -> tuple [str , str ] | None :
90
86
gen_kw_key = cast (str , gen_kw [0 ])
91
-
92
- options = cast (dict [str , str ], gen_kw [- 1 ])
93
87
positional_args = cast (list [str ], gen_kw [:- 1 ])
94
- forward_init = str_to_bool (options .get ("FORWARD_INIT" , "FALSE" ))
95
- init_file = _get_abs_path (options .get ("INIT_FILES" ))
96
- update_parameter = str_to_bool (options .get ("UPDATE" , "TRUE" ))
97
- errors = []
98
88
99
- if len (positional_args ) == 2 :
100
- parameter_file_contents = positional_args [1 ][1 ]
101
- parameter_file_context = positional_args [1 ][0 ]
102
- template_file = None
103
- output_file = None
104
- elif len (positional_args ) == 4 :
89
+ if len (positional_args ) == 4 :
105
90
output_file = positional_args [2 ]
106
- parameter_file_contents = positional_args [3 ][1 ]
107
91
parameter_file_context = positional_args [3 ][0 ]
108
92
template_file = _get_abs_path (positional_args [1 ][0 ])
109
93
if not os .path .isfile (template_file ):
110
- errors .append (
111
- ConfigValidationError .with_context (
112
- f"No such template file: { template_file } " , positional_args [1 ]
113
- )
94
+ raise ConfigValidationError .with_context (
95
+ f"No such template file: { template_file } " , positional_args [1 ]
114
96
)
115
97
elif Path (template_file ).stat ().st_size == 0 :
116
98
token = getattr (parameter_file_context , "token" , parameter_file_context )
@@ -121,31 +103,35 @@ def from_config_list(cls, gen_kw: list[str | dict[str, str]]) -> Self:
121
103
f"instead: GEN_KW { gen_kw_key } { token } " ,
122
104
positional_args [1 ],
123
105
)
106
+ if output_file .startswith ("/" ):
107
+ raise ConfigValidationError .with_context (
108
+ f"Output file cannot have an absolute path { output_file } " ,
109
+ positional_args [2 ],
110
+ )
111
+ return template_file , output_file
112
+ return None
113
+
114
+ @classmethod
115
+ def from_config_list (cls , gen_kw : list [str | dict [str , str ]]) -> Self :
116
+ gen_kw_key = cast (str , gen_kw [0 ])
117
+
118
+ options = cast (dict [str , str ], gen_kw [- 1 ])
119
+ positional_args = cast (list [str ], gen_kw [:- 1 ])
120
+ update_parameter = str_to_bool (options .get ("UPDATE" , "TRUE" ))
121
+ errors = []
122
+
123
+ if len (positional_args ) == 2 :
124
+ parameter_file_contents = positional_args [1 ][1 ]
125
+ parameter_file_context = positional_args [1 ][0 ]
126
+ elif len (positional_args ) == 4 :
127
+ parameter_file_contents = positional_args [3 ][1 ]
128
+ parameter_file_context = positional_args [3 ][0 ]
124
129
125
130
else :
126
131
raise ConfigValidationError (
127
132
f"Unexpected positional arguments: { positional_args } "
128
133
)
129
134
130
- if forward_init :
131
- errors .append (
132
- ConfigValidationError .with_context (
133
- "Loading GEN_KW from files created by the forward "
134
- "model is not supported." ,
135
- gen_kw ,
136
- )
137
- )
138
-
139
- if init_file and "%" not in init_file :
140
- errors .append (
141
- ConfigValidationError .with_context (
142
- "Loading GEN_KW from files requires %d in file format" , gen_kw
143
- )
144
- )
145
-
146
- if errors :
147
- raise ConfigValidationError .from_collected (errors )
148
-
149
135
transform_function_definitions : list [TransformFunctionDefinition ] = []
150
136
for line_number , item in enumerate (parameter_file_contents .splitlines ()):
151
137
item = item .split ("--" )[0 ] # remove comments
@@ -187,10 +173,7 @@ def from_config_list(cls, gen_kw: list[str | dict[str, str]]) -> Self:
187
173
)
188
174
return cls (
189
175
name = gen_kw_key ,
190
- forward_init = forward_init ,
191
- template_file = template_file ,
192
- output_file = output_file ,
193
- forward_init_file = init_file ,
176
+ forward_init = False ,
194
177
transform_function_definitions = transform_function_definitions ,
195
178
update = update_parameter ,
196
179
)
@@ -281,9 +264,6 @@ def _check_valid_derrf_parameters(prior: PriorDict) -> None:
281
264
def sample_or_load (
282
265
self , real_nr : int , random_seed : int , ensemble_size : int
283
266
) -> xr .Dataset :
284
- if self .forward_init_file :
285
- return self .read_from_runpath (Path (), real_nr , 0 )
286
-
287
267
keys = [e .name for e in self .transform_functions ]
288
268
parameter_value = self ._sample_value (
289
269
self .name ,
@@ -306,22 +286,7 @@ def read_from_runpath(
306
286
real_nr : int ,
307
287
iteration : int ,
308
288
) -> xr .Dataset :
309
- keys = [e .name for e in self .transform_functions ]
310
- if not self .forward_init_file :
311
- raise ValueError ("loading gen_kw values requires forward_init_file" )
312
-
313
- parameter_value = self ._values_from_file (
314
- substitute_runpath_name (self .forward_init_file , real_nr , iteration ),
315
- keys ,
316
- )
317
-
318
- return xr .Dataset (
319
- {
320
- "values" : ("names" , parameter_value ),
321
- "transformed_values" : ("names" , self .transform (parameter_value )),
322
- "names" : keys ,
323
- }
324
- )
289
+ return xr .Dataset ()
325
290
326
291
def write_to_runpath (
327
292
self ,
@@ -362,21 +327,6 @@ def parse_value(value: float | int | str) -> float | int | str:
362
327
if tf .use_log
363
328
}
364
329
365
- if self .template_file is not None and self .output_file is not None :
366
- target_file = substitute_runpath_name (
367
- self .output_file , real_nr , ensemble .iteration
368
- )
369
- target_file = target_file .removeprefix ("/" )
370
- (run_path / target_file ).parent .mkdir (exist_ok = True , parents = True )
371
- template_file_path = (
372
- ensemble .experiment .mount_point / Path (self .template_file ).name
373
- )
374
- with open (template_file_path , encoding = "utf-8" ) as f :
375
- template = f .read ()
376
- for key , value in data .items ():
377
- template = template .replace (f"<{ key } >" , f"{ value :.6g} " )
378
- with open (run_path / target_file , "w" , encoding = "utf-8" ) as f :
379
- f .write (template )
380
330
if log10_data :
381
331
return {self .name : data , f"LOG10_{ self .name } " : log10_data }
382
332
else :
@@ -553,14 +503,6 @@ def _parse_transform_function_definition(
553
503
calc_func = PRIOR_FUNCTIONS [t .param_name ],
554
504
)
555
505
556
- def save_experiment_data (self , experiment_path : Path ) -> None :
557
- if self .template_file :
558
- incoming_template_file_path = Path (self .template_file )
559
- template_file_path = Path (
560
- experiment_path / incoming_template_file_path .name
561
- )
562
- shutil .copyfile (incoming_template_file_path , template_file_path )
563
-
564
506
565
507
@dataclass
566
508
class TransformFunction :
0 commit comments