19
19
import pathlib
20
20
import sys
21
21
import time
22
+ import warnings
22
23
from typing import Mapping , MutableMapping , Optional
23
24
24
25
import smart_settings
@@ -122,7 +123,42 @@ def read_params_from_cmdline(
122
123
verbose : bool = True ,
123
124
dynamic : bool = True ,
124
125
save_params : bool = True ,
125
- ) -> smart_settings .AttributeDict :
126
+ ) -> smart_settings .param_classes .AttributeDict :
127
+ """Alias for :func:`initialize_job`.
128
+
129
+ Deprecated:
130
+ This function is deprecated and will be removed in a future release. Use
131
+ :func:`initialize_job` instead.
132
+ """
133
+ warnings .warn (
134
+ "`read_params_from_cmdline` is deprecated! Use `initialize_job` instead." ,
135
+ FutureWarning ,
136
+ stacklevel = 2 ,
137
+ )
138
+
139
+ if not make_immutable :
140
+ msg = (
141
+ "The option `make_immutable=False` is not supported anymore."
142
+ " You can create a mutable copy of the parameters with"
143
+ " `smart_settings.param_classes.AttributeDict(params)`"
144
+ )
145
+ raise RuntimeError (msg )
146
+
147
+ if not save_params :
148
+ msg = (
149
+ "The option `save_params=False` is not supported anymore."
150
+ " Parameters will always be saved."
151
+ )
152
+ raise RuntimeError (msg )
153
+
154
+ return initialize_job (cmd_line , verbose = verbose , dynamic = dynamic )
155
+
156
+
157
+ def initialize_job (
158
+ cmd_line : Optional [list [str ]] = None ,
159
+ verbose : bool = True ,
160
+ dynamic : bool = True ,
161
+ ) -> smart_settings .param_classes .AttributeDict :
126
162
"""Read parameters from command line and register at cluster_utils server.
127
163
128
164
This function is intended to be called at the beginning of your job scripts. It
@@ -135,10 +171,8 @@ def read_params_from_cmdline(
135
171
136
172
Args:
137
173
cmd_line: Command line arguments (defaults to sys.argv).
138
- make_immutable: See ``smart_settings.loads()``
139
174
verbose: If true, print the loaded parameters.
140
175
dynamic: See ``smart_settings.loads()``
141
- save_params: If true, save the settings as JSON file in the working_dir.
142
176
143
177
Returns:
144
178
Parameters as loaded from the command line arguments with smart_settings.
@@ -174,7 +208,7 @@ def add_cmd_params(orig_dict):
174
208
175
209
final_params = smart_settings .loads (
176
210
json .dumps (parameter_dict ),
177
- make_immutable = make_immutable ,
211
+ make_immutable = True ,
178
212
dynamic = dynamic ,
179
213
post_unpack_hooks = ([add_cmd_params , check_reserved_params ]),
180
214
)
@@ -186,7 +220,7 @@ def add_cmd_params(orig_dict):
186
220
187
221
final_params = smart_settings .load (
188
222
os .fspath (parameter_file ),
189
- make_immutable = make_immutable ,
223
+ make_immutable = True ,
190
224
dynamic = dynamic ,
191
225
post_unpack_hooks = ([add_cmd_params , check_reserved_params ]),
192
226
)
@@ -205,14 +239,31 @@ def add_cmd_params(orig_dict):
205
239
206
240
submission_state .start_time = time .time ()
207
241
208
- if save_params and "working_dir" in final_params :
242
+ # TODO should probably rather be an assert, there should always be a working dir
243
+ if "working_dir" in final_params :
209
244
os .makedirs (final_params .working_dir , exist_ok = True )
210
245
_save_settings_to_json (final_params , final_params .working_dir )
211
246
212
247
return final_params
213
248
214
249
215
250
def save_metrics_params (metrics : MutableMapping [str , float ], params ) -> None :
251
+ """Alias for :func:`finalize_job`.
252
+
253
+ Deprecated:
254
+ This function is deprecated and will be removed in a future release. Use
255
+ :func:`finalize_job` instead.
256
+ """
257
+ warnings .warn (
258
+ "`save_metric_params` is deprecated! Use `finalize_job` instead." ,
259
+ FutureWarning ,
260
+ stacklevel = 2 ,
261
+ )
262
+
263
+ finalize_job (metrics , params )
264
+
265
+
266
+ def finalize_job (metrics : MutableMapping [str , float ], params ) -> None :
216
267
"""Save metrics and parameters and send metrics to the cluster_utils server.
217
268
218
269
Save the used parameters and resulting metrics to CSV files (filenames defined by
@@ -339,9 +390,9 @@ def wrapper():
339
390
"""Saves settings file on beginning, calls wrapped function with params from cmd
340
391
and saves metrics to working_dir
341
392
"""
342
- params = read_params_from_cmdline (** read_params_args )
393
+ params = initialize_job (** read_params_args )
343
394
metrics = main_func (** params )
344
- save_metrics_params (metrics , params )
395
+ finalize_job (metrics , params )
345
396
return metrics
346
397
347
398
return wrapper
@@ -352,6 +403,8 @@ def wrapper():
352
403
"announce_fraction_finished" ,
353
404
"cluster_main" ,
354
405
"exit_for_resume" ,
406
+ "finalize_job" ,
407
+ "initialize_job" ,
355
408
"save_metrics_params" ,
356
409
"read_params_from_cmdline" ,
357
410
]
0 commit comments