Numexpr in Python doesn't recognise a declared symbol

248 views Asked by At

I'm trying to do some plots of some symbolic data. I have some expression from a regression in the form:

expr =  '(((((((((1.0)*(2.0)))-(ER)))-(-0.37419122066665467))*0.006633039574629684)*(0.006633039574629684*((((T)-(((1.0)+(P)))))-(P))))+0.1451920626347467)'

Where expr here is some prediction: f = f(T, P, ER). I know this particular example is a crazy expression but it's not really super important. Basically, supposing I have some dataframe, plotdata, I am trying to produce plots with:

import pandas
import sympy
import numexpr
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

expr =  '(((((((((1.0)*(2.0)))-(ER)))-(-0.37419122066665467))*0.006633039574629684)*(0.006633039574629684*((((T)-(((1.0)+(P)))))-(P))))+0.1451920626347467)'

#Extract some data for surface plot but fixing one variable
plotdata = plotdata.loc[(plotdata.P == 1)]

#Extract data as lists for plotting
x = list(plotdata['T'])
y = list(plotdata['ER'])
f_real = list(plotdata['f'])

T_sympy = sympy.Symbol('T')
P_sympy = sympy.Symbol('P')
ER_sympy = sympy.Symbol('ER')
f_pred = numexpr.evaluate(expr)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(x,y,f_real, alpha = 0.3)
ax.plot_surface(x,y,f_pred)

However, I am getting an error with f_pred.

numexpr.evaluate(expr)
Traceback (most recent call last):

  File "/anaconda3/lib/python3.7/site-packages/numexpr/necompiler.py", line 744, in getArguments
    a = local_dict[name]

KeyError: 'ER'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-100-c765b0f1e5ce>", line 1, in <module>
    numexpr.evaluate(expr)

  File "/anaconda3/lib/python3.7/site-packages/numexpr/necompiler.py", line 818, in evaluate
    arguments = getArguments(names, local_dict, global_dict)

  File "/anaconda3/lib/python3.7/site-packages/numexpr/necompiler.py", line 746, in getArguments
    a = global_dict[name]

KeyError: 'ER'

I am not super familiar with the numexpr package. However, I have been building this up from a 1D regression to now a 3D regression. ER was my 1D variable and was working fine. I have obviously slightly altered my code since the 1D case but I am still slightly at a loss as to why this error is popping up.

Any pointers would be greatly appreciated.

1

There are 1 answers

0
imwellhowareyou On

I've figured it out. Pretty silly error in the end. I needed to change:

#Extract data as lists for plotting
x = list(plotdata['T'])
y = list(plotdata['ER'])

to:

T = list(plotdata['T'])
ER = list(plotdata['ER'])
P = list(plotdata['P'])

i.e. numexpr.evaluate was looking for the input data, not the symbol!