@@ -351,31 +351,39 @@ def eval(
351
351
msg = f"evaluating { expr !r} with locals={ (self_unwrap | parameters )} and { has_ak = } "
352
352
log .debug (msg )
353
353
354
- # use numexpr if we are only dealing with numpy data types (and no global dictionary)
355
- if not has_ak and modules is None :
356
- out_data = ne .evaluate (
357
- expr ,
358
- local_dict = (self_unwrap | parameters ),
359
- )
360
-
361
- msg = f"...the result is { out_data !r} "
362
- log .debug (msg )
363
-
364
- # need to convert back to LGDO
365
- # np.evaluate should always return a numpy thing?
366
- if out_data .ndim == 0 :
367
- return Scalar (out_data .item ())
368
- if out_data .ndim == 1 :
369
- return Array (out_data )
370
- if out_data .ndim == 2 :
371
- return ArrayOfEqualSizedArrays (nda = out_data )
354
+ def _make_lgdo (data ):
355
+ if data .ndim == 0 :
356
+ return Scalar (data .item ())
357
+ if data .ndim == 1 :
358
+ return Array (data )
359
+ if data .ndim == 2 :
360
+ return ArrayOfEqualSizedArrays (nda = data )
372
361
373
362
msg = (
374
- f"evaluation resulted in { out_data .ndim } -dimensional data, "
363
+ f"evaluation resulted in { data .ndim } -dimensional data, "
375
364
"I don't know which LGDO this corresponds to"
376
365
)
377
366
raise RuntimeError (msg )
378
367
368
+ # use numexpr if we are only dealing with numpy data types (and no global dictionary)
369
+ if not has_ak and modules is None :
370
+ try :
371
+ out_data = ne .evaluate (
372
+ expr ,
373
+ local_dict = (self_unwrap | parameters ),
374
+ )
375
+
376
+ msg = f"...the result is { out_data !r} "
377
+ log .debug (msg )
378
+
379
+ # need to convert back to LGDO
380
+ # np.evaluate should always return a numpy thing?
381
+ return _make_lgdo (out_data )
382
+
383
+ except Exception :
384
+ msg = f"Warning { expr } could not be evaluated with numexpr probably due to some not allowed characters, trying with eval()."
385
+ log .debug (msg )
386
+
379
387
# resort to good ol' eval()
380
388
globs = {"ak" : ak , "np" : np }
381
389
if modules is not None :
@@ -392,6 +400,10 @@ def eval(
392
400
return Array (out_data .to_numpy ())
393
401
return VectorOfVectors (out_data )
394
402
403
+ # modules can still produce numpy array
404
+ if isinstance (out_data , np .ndarray ):
405
+ return _make_lgdo (out_data )
406
+
395
407
if np .isscalar (out_data ):
396
408
return Scalar (out_data )
397
409
0 commit comments