9
9
from modelspec .base_types import print_
10
10
from modelspec .base_types import EvaluableExpression
11
11
12
+ from random import Random
13
+ from typing import Union
14
+
12
15
verbose = False
13
16
14
17
15
- def load_json (filename ):
18
+ def load_json (filename : str ):
16
19
"""
17
20
Load a generic JSON file
21
+
22
+ Args:
23
+ filename: The name of the JSON file to load
18
24
"""
19
25
20
26
with open (filename ) as f :
@@ -23,19 +29,25 @@ def load_json(filename):
23
29
return data
24
30
25
31
26
- def load_yaml (filename ):
32
+ def load_yaml (filename : str ):
27
33
"""
28
34
Load a generic YAML file
35
+
36
+ Args:
37
+ filename: The name of the YAML file to load
29
38
"""
30
39
with open (filename ) as f :
31
40
data = yaml .load (f , Loader = yaml .SafeLoader )
32
41
33
42
return data
34
43
35
44
36
- def load_bson (filename ):
45
+ def load_bson (filename : str ):
37
46
"""
38
47
Load a generic BSON file
48
+
49
+ Args:
50
+ filename: The name of the BSON file to load
39
51
"""
40
52
with open (filename , "rb" ) as infile :
41
53
data_encoded = infile .read ()
@@ -211,11 +223,26 @@ def _params_info(parameters, multiline=False):
211
223
FORMAT_TENSORFLOW = "tensorflow"
212
224
213
225
214
- def evaluate (expr , parameters = {}, rng = None , array_format = FORMAT_NUMPY , verbose = False ):
226
+ def evaluate (
227
+ expr : Union [int , float , str , list , dict ],
228
+ parameters : dict = {},
229
+ rng : Random = None ,
230
+ array_format : str = FORMAT_NUMPY ,
231
+ verbose : bool = False ,
232
+ cast_to_int : bool = False ,
233
+ ):
215
234
"""
216
235
Evaluate a general string like expression (e.g. "2 * weight") using a dict
217
236
of parameters (e.g. {'weight':10}). Returns floats, ints, etc. if that's what's
218
237
given in expr
238
+
239
+ Args:
240
+ expr: The expression to convert
241
+ parameters: A dict of the parameters which can be substituted in to the expression
242
+ rng: The random number generator to use
243
+ array_format: numpy or tensorflow
244
+ verbose: Print the calculations
245
+ cast_to_int: return an int for float/string values if castable
219
246
"""
220
247
221
248
if array_format == FORMAT_TENSORFLOW :
@@ -233,7 +260,7 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
233
260
expr
234
261
] # replace with the value in parameters & check whether it's float/int...
235
262
if verbose :
236
- print_ ("Using for that param: %s" % _val_info (expr ), verbose )
263
+ print_ (" Using for that param: %s" % _val_info (expr ), verbose )
237
264
238
265
if type (expr ) == str :
239
266
try :
@@ -242,26 +269,28 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
242
269
else :
243
270
expr = int (expr )
244
271
except :
245
- pass
246
- try :
247
- if array_format == FORMAT_TENSORFLOW :
248
- expr = tf .constant (float (expr ))
249
- else :
250
- expr = float (expr )
251
- except :
252
- pass
272
+
273
+ try :
274
+ if array_format == FORMAT_TENSORFLOW :
275
+ expr = tf .constant (float (expr ))
276
+ else :
277
+ expr = float (expr )
278
+ except :
279
+ pass
253
280
254
281
if type (expr ) == list :
255
282
if verbose :
256
- print_ ("Returning a list in format: %s" % array_format , verbose )
283
+ print_ (" Returning a list in format: %s" % array_format , verbose )
257
284
if array_format == FORMAT_TENSORFLOW :
258
285
return tf .constant (expr , dtype = tf .float64 )
259
286
else :
260
287
return np .array (expr )
261
288
262
289
if type (expr ) == np .ndarray :
263
290
if verbose :
264
- print_ ("Returning a numpy array in format: %s" % array_format , verbose )
291
+ print_ (
292
+ " Returning a numpy array in format: %s" % array_format , verbose
293
+ )
265
294
if array_format == FORMAT_TENSORFLOW :
266
295
return tf .convert_to_tensor (expr , dtype = tf .float64 )
267
296
else :
@@ -270,22 +299,22 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
270
299
if "Tensor" in type (expr ).__name__ :
271
300
if verbose :
272
301
print_ (
273
- "Returning a tensorflow Tensor in format: %s" % array_format ,
302
+ " Returning a tensorflow Tensor in format: %s" % array_format ,
274
303
verbose ,
275
304
)
276
305
if array_format == FORMAT_NUMPY :
277
306
return expr .numpy ()
278
307
else :
279
308
return expr
280
309
281
- if int (expr ) == expr :
310
+ if int (expr ) == expr and cast_to_int :
282
311
if verbose :
283
- print_ ("Returning int: %s" % int (expr ), verbose )
312
+ print_ (" Returning int: %s" % int (expr ), verbose )
284
313
return int (expr )
285
314
else : # will have failed if not number
286
315
if verbose :
287
- print_ ("Returning float: %s" % expr , verbose )
288
- return float ( expr )
316
+ print_ (" Returning {}: {}" . format ( type ( expr ), expr ) , verbose )
317
+ return expr
289
318
except :
290
319
try :
291
320
if rng :
@@ -299,7 +328,7 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
299
328
300
329
if verbose :
301
330
print_ (
302
- "Trying to eval [%s] with Python using %s..."
331
+ " Trying to eval [%s] with Python using %s..."
303
332
% (expr , parameters .keys ()),
304
333
verbose ,
305
334
)
@@ -308,13 +337,14 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
308
337
309
338
if verbose :
310
339
print_ (
311
- "Evaluated with Python: {} = {}" .format (expr , _val_info (v )), verbose
340
+ " Evaluated with Python: {} = {}" .format (expr , _val_info (v )),
341
+ verbose ,
312
342
)
313
343
314
344
if (type (v ) == float or type (v ) == str ) and int (v ) == v :
315
345
316
346
if verbose :
317
- print_ ("Returning int: %s" % int (v ), verbose )
347
+ print_ (" Returning int: %s" % int (v ), verbose )
318
348
319
349
if array_format == FORMAT_TENSORFLOW :
320
350
return tf .constant (int (v ))
@@ -323,7 +353,7 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
323
353
return v
324
354
except Exception as e :
325
355
if verbose :
326
- print_ (f"Returning without altering: { expr } (error: { e } )" , verbose )
356
+ print_ (f" Returning without altering: { expr } (error: { e } )" , verbose )
327
357
return expr
328
358
329
359
0 commit comments