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,35 +260,39 @@ 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 :
267
+ print (1 )
240
268
if array_format == FORMAT_TENSORFLOW :
241
269
expr = tf .constant (int (expr ))
242
270
else :
243
271
expr = int (expr )
272
+ print (2 )
244
273
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
274
+
275
+ try :
276
+ if array_format == FORMAT_TENSORFLOW :
277
+ expr = tf .constant (float (expr ))
278
+ else :
279
+ expr = float (expr )
280
+ except :
281
+ pass
253
282
254
283
if type (expr ) == list :
255
284
if verbose :
256
- print_ ("Returning a list in format: %s" % array_format , verbose )
285
+ print_ (" Returning a list in format: %s" % array_format , verbose )
257
286
if array_format == FORMAT_TENSORFLOW :
258
287
return tf .constant (expr , dtype = tf .float64 )
259
288
else :
260
289
return np .array (expr )
261
290
262
291
if type (expr ) == np .ndarray :
263
292
if verbose :
264
- print_ ("Returning a numpy array in format: %s" % array_format , verbose )
293
+ print_ (
294
+ " Returning a numpy array in format: %s" % array_format , verbose
295
+ )
265
296
if array_format == FORMAT_TENSORFLOW :
266
297
return tf .convert_to_tensor (expr , dtype = tf .float64 )
267
298
else :
@@ -270,22 +301,22 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
270
301
if "Tensor" in type (expr ).__name__ :
271
302
if verbose :
272
303
print_ (
273
- "Returning a tensorflow Tensor in format: %s" % array_format ,
304
+ " Returning a tensorflow Tensor in format: %s" % array_format ,
274
305
verbose ,
275
306
)
276
307
if array_format == FORMAT_NUMPY :
277
308
return expr .numpy ()
278
309
else :
279
310
return expr
280
311
281
- if int (expr ) == expr :
312
+ if int (expr ) == expr and cast_to_int :
282
313
if verbose :
283
- print_ ("Returning int: %s" % int (expr ), verbose )
314
+ print_ (" Returning int: %s" % int (expr ), verbose )
284
315
return int (expr )
285
316
else : # will have failed if not number
286
317
if verbose :
287
- print_ ("Returning float: %s" % expr , verbose )
288
- return float ( expr )
318
+ print_ (" Returning {}: {}" . format ( type ( expr ), expr ) , verbose )
319
+ return expr
289
320
except :
290
321
try :
291
322
if rng :
@@ -299,7 +330,7 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
299
330
300
331
if verbose :
301
332
print_ (
302
- "Trying to eval [%s] with Python using %s..."
333
+ " Trying to eval [%s] with Python using %s..."
303
334
% (expr , parameters .keys ()),
304
335
verbose ,
305
336
)
@@ -308,13 +339,14 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
308
339
309
340
if verbose :
310
341
print_ (
311
- "Evaluated with Python: {} = {}" .format (expr , _val_info (v )), verbose
342
+ " Evaluated with Python: {} = {}" .format (expr , _val_info (v )),
343
+ verbose ,
312
344
)
313
345
314
346
if (type (v ) == float or type (v ) == str ) and int (v ) == v :
315
347
316
348
if verbose :
317
- print_ ("Returning int: %s" % int (v ), verbose )
349
+ print_ (" Returning int: %s" % int (v ), verbose )
318
350
319
351
if array_format == FORMAT_TENSORFLOW :
320
352
return tf .constant (int (v ))
@@ -323,7 +355,7 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
323
355
return v
324
356
except Exception as e :
325
357
if verbose :
326
- print_ (f"Returning without altering: { expr } (error: { e } )" , verbose )
358
+ print_ (f" Returning without altering: { expr } (error: { e } )" , verbose )
327
359
return expr
328
360
329
361
0 commit comments