Bayesian analysis of chemical network - python

I am trying to do a simple reaction network and see the sensitivity of reaction rates k1 and k2. So my reaction goes A --> B --> C with k1 and k2, respectively.
import numpy as np
import pymc3 as pm
from matplotlib.pyplot import figure, scatter, legend, plot
from scipy.integrate import solve_ivp
from sys import exit
Nt = 11
time = 10
tt = np.linspace(0,time,Nt+1)
y0 = [1,0,0]
k1_0, k2_0 = 1, 0.5
def equat(t,c):
da_dt = -k1_0*c[0]
db_dt = k1_0*c[0] - k2_0*c[1]
dc_dt = k2_0*c[1]
return da_dt, db_dt, dc_dt
c_est = solve_ivp(equat, t_span = [0,time], t_eval = tt, y0 = y0)
niter = 10
with pm.Model() as reak_model:
k1 = pm.Normal('k1', mu = 0, sd = 100)
k2 = pm.Normal('k2', mu=0, sd=100, shape = 1)
sigma = pm.HalfNormal('sigma', sd=1)
def equat_2(t,c):
da_dt = -k1*c[0]
db_dt = k1*c[0] - k1*c[1]
dc_dt = k1*c[1]
return da_dt, db_dt, dc_dt
c = solve_ivp(equat_2, t_span = [0,time], t_eval = tt, y0 = y0)
likelihood = pm.Normal('y', mu=c.y, sd=sigma, observed=c_est.y)
trace = pm.sample(niter, chains = 1)
pm.traceplot(trace, varnames=['k1','k2'])
ValueError: setting an array element with a sequence.
Im getting this error. Since I am new to Bayesian, I am wondering if its something with the distributions?
Traceback (most recent call last):
File "<ipython-input-95-26ff8f25faea>", line 1, in <module>
runfile('/Daft regresion.py', wdir='/Final code')
File "/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "Daft regression.py", line 53, in <module>
c = solve_ivp(equat_2, t_span = [0,time], t_eval = tt, y0 = y0)
File "/python3.7/site-packages/scipy/integrate/_ivp/ivp.py", line 454, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "/python3.7/site-packages/scipy/integrate/_ivp/rk.py", line 99, in __init__
self.f = self.fun(self.t, self.y)
File "/python3.7/site-packages/scipy/integrate/_ivp/base.py", line 139, in fun
return self.fun_single(t, y)
File "/python3.7/site-packages/scipy/integrate/_ivp/base.py", line 21, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
File "/python3.7/site-packages/numpy/core/numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
Edit: Apparently I need to implement Theano package instead of solve_ivp. Still help appreciated.

Related

Matrix and vector shape in TVP-VAR in the statespace mlemodels

Thanks to everyone in advance for their time!
I am trying to run a TVP-VAR (time varying papramenter) for a panel in the statespace mlemodels in statsmodel. I get an error while trying to fit the model. My understanding is that mostly is regarding the dimensions for state covariance matrix. I suspect will get the same error later when I will deal with column shape.
The error is :
ValueError: Invalid dimensions for state covariance matrix matrix: requires 702 rows, got 3
How could I solve that ? The type error is showing is as below, highlighted in bold both the error and the Traceback :
preliminary = tvppanelvarmodel.fit(maxiter=1000)
Traceback (most recent call last):
File "/var/folders/m6/68zljfsj2t9_dzgpwwslj29r0000gp/T/ipykernel_6232/3038987883.py", line 1, in <module>
preliminary = tvppanelvarmodel.fit(maxiter=1000)
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 704, in fit
mlefit = super(MLEModel, self).fit(start_params, method=method,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/model.py", line 563, in fit
xopt, retvals, optim_settings = optimizer._fit(f, score, start_params,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/optimizer.py", line 241, in _fit
xopt, retvals = func(objective, gradient, start_params, fargs, kwargs,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/optimizer.py", line 651, in _fit_lbfgs
retvals = optimize.fmin_l_bfgs_b(func, start_params, maxiter=maxiter,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/lbfgsb.py", line 197, in fmin_l_bfgs_b
res = _minimize_lbfgsb(fun, x0, args=args, jac=jac, bounds=bounds,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/lbfgsb.py", line 306, in _minimize_lbfgsb
sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/optimize.py", line 261, in _prepare_scalar_function
sf = ScalarFunction(fun, x0, args, grad, hess,
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 140, in __init__
self._update_fun()
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 233, in _update_fun
self._update_fun_impl()
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 137, in update_fun
self.f = fun_wrapped(self.x)
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 134, in fun_wrapped
return fun(np.copy(x), *args)
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/model.py", line 531, in f
return -self.loglike(params, *args) / nobs
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 933, in loglike
self.update(params, transformed=True, includes_fixed=True,
File "/var/folders/m6/68zljfsj2t9_dzgpwwslj29r0000gp/T/ipykernel_6232/3786466608.py", line 104, in update
self['state_cov'] = np.diag([params[2]**2, params[3]**2, params[4]**2]) # W
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 239, in __setitem__
return self.ssm.__setitem__(key, value)
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/representation.py", line 420, in __setitem__
setattr(self, key, value)
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/representation.py", line 54, in __set__
value = self._set_matrix(obj, value, shape)
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/representation.py", line 68, in _set_matrix
validate_matrix_shape(
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/tools.py", line 1474, in validate_matrix_shape
raise ValueError('Invalid dimensions for %s matrix: requires %d'
ValueError: Invalid dimensions for state covariance matrix matrix: requires 702 rows, got 3
When I defined the class initially, I did the following
class TVPVAR(sm.tsa.statespace.MLEModel):
def __init__(self, y):
# Create a matrix with [y_t' : y_{t-1}'] for t = 2, ..., T
augmented = sm.tsa.lagmat(y, 1, trim='both', original='in', use_pandas=True)
# Separate into y_t and z_t = [1 : y_{t-1}']
p = y.shape[1]
y_t = augmented.iloc[:, :p]
z_t = sm.add_constant(augmented.iloc[:, p:])
nobs = y.shape[0]
T=y.shape[0]
# Recall that the length of the state vector is p * (p + 1)
k_states = p * (p + 1)
super(TVPVAR,self).__init__(y_t, exog=None, k_states=k_states,k_posdef=k_states)
self.k_y = p
self.k_states = p * (p + 1)
self.nobs = T
self['design'] = np.zeros((self.k_y, self.k_states, 1))
self['transition'] = np.eye(k_states) # G
self['selection'] = np.eye(k_states) # R=1
def update_variances(self, obs_cov, state_cov_diag):
self['obs_cov'] = obs_cov
self['state_cov'] = np.diag(state_cov_diag) # W
init = initialization.Initialization(self.k_states)
init.set((0, 2), 'diffuse')
init.set((2, 4), 'stationary')
self.ssm.initialize(init)
OTHER CODE
def update(self, params, **kwargs):
params = super().update(params, **kwargs)
self['transition', 2,2] = params[0]
self['transition', 3,2] = params[1]
self['state_cov'] = np.diag([params[2]**2, params[3]**2, params[4]**2]) # W
How can I define the dimensions for state covariance matrix and the vector shape? Thanks your inputs.

Python vestigial parameter and uncallable function

import numpy as np
from scipy.optimize import fsolve
from scipy.integrate import quad
import matplotlib.pyplot as plt
Rgas = 8.31446261815324 #Pa*m**3/mol*K
def Peng_Robinson_EOS(P,V,T,Tc,Pc,ω):
a = (1+(0.37464+1.54226*ω-0.26992*ω**2)*(1-(T/Tc)**(1/2)))**2*Rgas**2*Tc**2/Pc #Pa*m**3
b = 0.07780 * Rgas*Tc/Pc
return P + a/((V+(1-np.sqrt(2))*b)*(V+(1+np.sqrt(2)))) - Rgas*T/(V-b)
def PR_Psat(T,Tc,Pc,ω,V,Pguess = 100000):
def integral_diff (Pguess,T,Tc,Pc,ω,V):
def Psat_integrand (V,Pguess,T,Tc,Pc,ω):
integrand1 = fsolve(Peng_Robinson_EOS(Pguess,V,T,Tc,Pc,ω),Pguess)
integrand2 = Pguess
integrand = integrand1-integrand2
return integrand
Vl = fsolve(Psat_integrand(V,Pguess,T,Tc,Pc,ω),0)
Vv_guess = Rgas*T/Pguess
Vv = fsolve(Psat_integrand(V,Pguess,T,Tc,Pc,ω),Vv_guess)
Vinf_guess = (Vl + Vv)/2
Vinf = fsolve(Psat_integrand(V,Pguess,T,Tc,Pc,ω),Vinf_guess)
left = quad(Psat_integrand(V,Pguess,T,Tc,Pc,ω),Vl,Vinf)[0]
right = quad(Psat_integrand(V,Pguess,T,Tc,Pc,ω),Vinf,Vv)[0]
diff = left + right
return diff
Psat = fsolve(integral_diff(Pguess,T,Tc,Pc,ω,V),Pguess)
return Psat
There are two issues with this code.
1: in theory, PR_Psat should not depend on V, since all values of V used in calculation are found via fsolve. However, because Peng_Robinson_EOS depends on V, it Python won't let it be ignored in enclosing functions. Is there a way to eliminate the need to "specify" V?
from an earlier version (before V was a parameter of all functions), to demonstrate:
runfile('...', wdir='...')
Traceback (most recent call last):
File "<ipython-input-4-0875bc6411e8>", line 1, in <module>
runfile('...', wdir='...')
File "...\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "...\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "...", line 40, in <module>
print(PR_Psat(300,647.1,22055000,0.345))
File "...", line 37, in PR_Psat
Psat = fsolve(integral_diff(Pguess,T,Tc,Pc,ω),Pguess)
File "...", line 25, in integral_diff
Vl = fsolve(Psat_integrand(V,Pguess,T,Tc,Pc,ω),0)
NameError: name 'V' is not defined
2: It seems that Peng_Robinson is not being treated as a callable function, but rather as a float. I'm not sure what is causing this.
runfile('...', wdir='...')
Traceback (most recent call last):
File "<ipython-input-13-0875bc6411e8>", line 1, in <module>
runfile('...', wdir='...')
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "...\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "...", line 42, in <module>
print(PR_Psat(300,647.1,22055000,0.345,1))
File "...", line 39, in PR_Psat
Psat = fsolve(integral_diff(Pguess,T,Tc,Pc,ω,V),Pguess)
File "...", line 27, in integral_diff
Vl = fsolve(Psat_integrand(V,Pguess,T,Tc,Pc,ω),0)
File "...", line 23, in Psat_integrand
integrand1 = fsolve(Peng_Robinson_EOS(Pguess,V,T,Tc,Pc,ω),Pguess)
File "...\lib\site-packages\scipy\optimize\minpack.py", line 148, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File "...\lib\site-packages\scipy\optimize\minpack.py", line 214, in _root_hybr
shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
File "...\lib\site-packages\scipy\optimize\minpack.py", line 27, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
TypeError: 'numpy.float64' object is not callable
In theory, Peng_Robinson_EOS should, if plotted as P(V) with constant T, produce a cubic regression. The goal of PR_Psat is to find the value of P for which the integrals between P and the cubic regression cancel. (Hence, integral_diff being plugged into fsolve)
To summarize the questions,
1) Is there a way to eliminate the need for V in PR_Psat?
2) Why is Peng_Robinson_EOS being flagged as an un-callable numpy.float64 object?
The problem was a syntax error. Arguments for fsolve and quad were placed wrong. To fix both problems, I moved the args to the back. For example,
incorrect:
Vv = fsolve(Psat_integrand(V,Pguess,T,Tc,Pc,ω),Vv_guess)
correct:
Vv = fsolve(Psat_integrand,Vv_guess,args=(Pguess,T,Tc,Pc,ω))
The reason Peng_Robinson_EOS is being called a numpy.float64 is because with the syntax of the code in the question the program is evaluated before being passed to the solver.
Also, with proper syntax, V is no longer an issue.
def PR_Psat(T,Tc,Pc,ω,Pguess = 1000):
def integral_diff (Pguess,T,Tc,Pc,ω):
def Psat_integrand (V,Pguess,T,Tc,Pc,ω):
integrand1 = fsolve(Peng_Robinson_EOS,Pguess,args=(V,T,Tc,Pc,ω))
integrand2 = Pguess
integrand = integrand1-integrand2
return integrand
Vl = fsolve(Psat_integrand,0,args=(Pguess,T,Tc,Pc,ω))
Vv_guess = Rgas*T/Pguess
Vv = fsolve(Psat_integrand,Vv_guess,args=(Pguess,T,Tc,Pc,ω))
Vinf_guess = (Vl + Vv)/2
Vinf = fsolve(Psat_integrand,Vinf_guess,args=(Pguess,T,Tc,Pc,ω))
left = quad(Psat_integrand,Vl,Vinf,args=(Pguess,T,Tc,Pc,ω))[0]
right = quad(Psat_integrand,Vinf,Vv,args=(Pguess,T,Tc,Pc,ω))[0]
diff = left + right
return diff
Psat = fsolve(integral_diff,Pguess,args=(T,Tc,Pc,ω))
return Psat

Numba gives: ' TypingError: %r not allowed in a homogenous sequence'

I really have no clue what that error is about, since I dont even directly use %r. I suspect that somewhere there's a mixing of types, but I can not fathom where that would happen. That aside other suggestions to speed up the code would be much appreciated.
import numpy as np
from numba import jit, float64
c = 3*10**8
epsilon = 8.854187817 * 10**(-12)
mu = 4*np.pi *10**(-7)
#jit( nopython=True)
def cross(vec1, vec2):
result = np.array([0.,0.,0.])
a1, a2, a3 = vec1[0],vec1[1], vec1[2]
b1, b2, b3 = vec2[0], vec2[1], vec2[2]
result[0] = a2 * b3 - a3 * b2
result[1] = a3 * b1 - a1 * b3
result[2] = a1 * b2 - a2 * b1
return result
#jit( float64[:,:](float64[:],float64,float64,float64[:],float64[:],float64[:]), nopython = True)
def jit_EM_field(position,length,ladung,velocity,acceleration,R):
#using solutions to lienard wiechert potential
radius = np.linalg.norm(R - position)
if radius != 0:
unitradius = (R - position)/radius
else:
unitradius = np.array([0.,0.,0.])
if radius != 0 and np.dot(unitradius, velocity)!=1:
charge = ladung / ( (1 - np.dot(unitradius, velocity)/c)** 3)
if radius < length:
radius = length
radius2 = radius ** 2
velocity_in_c = velocity/c
oneMinusV2 = 1 - np.dot(velocity_in_c, velocity_in_c)
uMinusV = unitradius - velocity_in_c
aCrossUmV = cross(uMinusV, acceleration)
Eleft = (oneMinusV2 * (unitradius - velocity_in_c)) / radius2
Eright = cross(unitradius, aCrossUmV) / (radius*c**2)
E = (charge/(4*np.pi*epsilon)) * (Eleft - Eright)
B = cross(unitradius/c, ((mu*epsilon*charge*c**2) * (Eleft - Eright)))
EM_field = np.array([E,B], dtype = float)
else:
EM_field = np.zeros((2,3), dtype = float)
return EM_field
jit_EM_field( np.array([0.,1.,0.]),1.,0.1,np.array([0.,1.,0.]),np.array([0.,1.,0.])
,np.array([7.2,5.6,0.1]))
And here's the full error message.
runfile('C:/Users/Elios/testingjit.py', wdir='C:/Users/Elios')
Traceback (most recent call last):
File "<ipython-input-26-221208a798d4>", line 1, in <module>
runfile('C:/Users/Elios/testingjit.py', wdir='C:/Users/Elios')
File "C:\Users\Elios\Anaconda4\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Elios\Anaconda4\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Elios/testingjit.py", line 32, in <module>
#jit( float64[:,:](float64[:],float64,float64,float64[:],float64[:],float64[:]), nopython = True)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\decorators.py", line 176, in wrapper
disp.compile(sig)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\dispatcher.py", line 531, in compile
cres = self._compiler.compile(args, return_type)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\dispatcher.py", line 80, in compile
flags=flags, locals=self.locals)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 725, in compile_extra
return pipeline.compile_extra(func)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 369, in compile_extra
return self.compile_bytecode(bc, func_attr=self.func_attr)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 378, in compile_bytecode
return self._compile_bytecode()
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 690, in _compile_bytecode
return self._compile_core()
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 677, in _compile_core
res = pm.run(self.status)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 257, in run
raise patched_exception
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 249, in run
stage()
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 476, in stage_nopython_frontend
self.locals)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\compiler.py", line 828, in type_inference_stage
infer.propagate()
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typeinfer.py", line 717, in propagate
raise errors[0]
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typeinfer.py", line 127, in propagate
constraint(typeinfer)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typeinfer.py", line 372, in __call__
self.resolve(typeinfer, typevars, fnty)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typeinfer.py", line 385, in resolve
sig = typeinfer.resolve_call(fnty, pos_args, kw_args)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typeinfer.py", line 972, in resolve_call
return self.context.resolve_function_type(fnty, pos_args, kw_args)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typing\context.py", line 124, in resolve_function_type
return func.get_call_type(self, args, kws)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\types\functions.py", line 49, in get_call_type
sig = temp.apply(args, kws)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typing\templates.py", line 216, in apply
sig = typer(*args, **kws)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typing\npydecl.py", line 456, in typer
ndim, seq_dtype = _parse_nested_sequence(self.context, object)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typing\npydecl.py", line 423, in _parse_nested_sequence
n, dtype = _parse_nested_sequence(context, typ.dtype)
File "C:\Users\Elios\Anaconda4\lib\site-packages\numba\typing\npydecl.py", line 421, in _parse_nested_sequence
raise TypingError("%r not allowed in a homogenous sequence")
TypingError: %r not allowed in a homogenous sequence
Numba seems to lack support for nested arrays. Have managed to circumvent the issue in the following code. jit_EM_field() now returns an array of length 6 instead of 2 nested arrays of length 3 which you can see as #jit( float64[:] in the jit decorator. Also removed a redundant else clause.
import numpy as np
from numba import jit, float64
c = 3*10**8
epsilon = 8.854187817 * 10**(-12)
mu = 4*np.pi *10**(-7)
#jit( nopython=True)
def cross(vec1, vec2):
result = np.array([0.,0.,0.])
a1, a2, a3 = vec1[0],vec1[1], vec1[2]
b1, b2, b3 = vec2[0], vec2[1], vec2[2]
result[0] = a2 * b3 - a3 * b2
result[1] = a3 * b1 - a1 * b3
result[2] = a1 * b2 - a2 * b1
return result
#jit( float64[:](float64[:],float64,float64,float64[:],float64[:],float64[:]), nopython=True)
def jit_EM_field(position,length,ladung,velocity,acceleration,R):
#using solutions to lienard wiechert potential
EM_field = np.array([0.,0.,0.,0.,0.,0.])
radius = np.linalg.norm(R - position)
if radius != 0:
unitradius = (R - position)/radius
if np.dot(unitradius, velocity) != 1:
charge = ladung / ( (1 - np.dot(unitradius, velocity)/c)** 3)
if radius < length:
radius = length
radius2 = radius ** 2
velocity_in_c = velocity/c
oneMinusV2 = 1 - np.dot(velocity_in_c, velocity_in_c)
uMinusV = unitradius - velocity_in_c
aCrossUmV = cross(uMinusV, acceleration)
Eleft = (oneMinusV2 * (unitradius - velocity_in_c)) / radius2
Eright = cross(unitradius, aCrossUmV) / (radius*c**2)
E = (charge/(4*np.pi*epsilon)) * (Eleft - Eright)
B = cross(unitradius/c, ((mu*epsilon*charge*c**2) * (Eleft - Eright)))
EM_field = np.array([E[0],E[1],E[2],B[0],B[1],B[2]])
return EM_field
em_field = jit_EM_field(np.array([0.,1.,0.]),1.,0.1,np.array([0.,1.,0.]),np.array([0.,1.,0.]),np.array([7.2,5.6,0.1]))
em_field_zero = jit_EM_field(np.array([0.,1.,0.]),1.,0.1,np.array([0.,1.,0.]),np.array([0.,1.,0.]),np.array([0.,1.,0.]))
import pprint as pp
pp.pprint(em_field)
pp.pprint(em_field_zero)

TypeError raised when using Numba to accelerate Python code

I am using Numba's #jit decorator to optimize my function with the nopython=True option. When I remove this option my code runs fine, but I've read that enabling the option will significantly speed up the code.
However, I am getting the following traceback and error:
Traceback (most recent call last):
File "C:\Users\dis_YO_boi\Documents\Programming\Python\Base3DSolver4.py", line 83, in <module>
soln = odeint(f, y0, time, mxstep = 5000)
File "C:\Anaconda3\lib\site-packages\scipy\integrate\odepack.py", line 153, in odeint
ixpr, mxstep, mxhnil, mxordn, mxords)
File "C:\Anaconda3\lib\site-packages\numba\dispatcher.py", line 172, in _compile_for_args
return self.compile(sig)
File "C:\Anaconda3\lib\site-packages\numba\dispatcher.py", line 350, in compile
flags=flags, locals=self.locals)
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 644, in compile_extra
return pipeline.compile_extra(func)
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 361, in compile_extra
return self.compile_bytecode(bc, func_attr=self.func_attr)
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 370, in compile_bytecode
return self._compile_bytecode()
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 631, in _compile_bytecode
return pm.run(self.status)
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 251, in run
raise patched_exception
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 243, in run
res = stage()
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 458, in stage_nopython_frontend
self.locals)
File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 759, in type_inference_stage
infer.propagate()
File "C:\Anaconda3\lib\site-packages\numba\typeinfer.py", line 510, in propagate
raise errors[0]
numba.errors.TypingError: Failed at nopython (nopython frontend)
Internal error at <numba.typeinfer.SetItemConstraint object at 0x0000000007C082B0>:
--%<-----------------------------------------------------------------
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\numba\typeinfer.py", line 111, in propagate
constraint(typeinfer)
File "C:\Anaconda3\lib\site-packages\numba\typeinfer.py", line 377, in __call__
index=it, value=vt):
File "C:\Anaconda3\lib\site-packages\numba\typing\context.py", line 149, in resolve_setitem
return self.resolve_function_type("setitem", args, kws)
File "C:\Anaconda3\lib\site-packages\numba\typing\context.py", line 97, in resolve_function_type
res = defn.apply(args, kws)
File "C:\Anaconda3\lib\site-packages\numba\typing\templates.py", line 155, in apply
sig = generic(args, kws)
File "C:\Anaconda3\lib\site-packages\numba\typing\arraydecl.py", line 158, in generic
raise TypeError("Cannot modify value of type %s" %(ary,))
TypeError: Cannot modify value of type readonly array(float64, 2d, C)
--%<-----------------------------------------------------------------
File "Base3DSolver4.py", line 61
What does this mean and how can I fix it?
Here is my code:
import numpy as np
from scipy.integrate import odeint
from numba import jit
Kvec = np.logspace(-2, 2, 101, 10)
lenK = len(Kvec)
Qvec = 2*np.logspace(-4, 1, 101, 10)
lenQ = len(Qvec)
y0 = [0] * (15)
S0plot = np.zeros((lenK,lenQ))
S1plot = np.zeros((lenK,lenQ))
S2plot = np.zeros((lenK,lenQ))
S3plot = np.zeros((lenK,lenQ))
S4plot = np.zeros((lenK,lenQ))
KS0plot = np.zeros((lenK,lenQ))
KS1plot = np.zeros((lenK,lenQ))
KS2plot = np.zeros((lenK,lenQ))
KS3plot = np.zeros((lenK,lenQ))
PS1plot = np.zeros((lenK,lenQ))
PS2plot = np.zeros((lenK,lenQ))
PS3plot = np.zeros((lenK,lenQ))
PS4plot = np.zeros((lenK,lenQ))
Kplot = np.zeros((lenK,lenQ))
Pplot = np.zeros((lenK,lenQ))
ydot = np.zeros((15,1))
for Qloop in range(lenQ):
for Kloop in range(lenK):
K0 = Kvec[Kloop]
Q = Qvec[Qloop]
r1 = 2e-5
r2 = 2e-4
a = 0.001
d = 0.001
k = 0.999
P0 = 1
tf = 1e10
time = np.linspace(0, tf, 1001)
S00 = Q/r1
#jit(nopython=True)
def f(y, t):
S0 = y[0]
S1 = y[1]
S2 = y[2]
S3 = y[3]
S4 = y[4]
KS0 = y[5]
KS1 = y[6]
KS2 = y[7]
KS3 = y[8]
PS1 = y[9]
PS2 = y[10]
PS3 = y[11]
PS4 = y[12]
K = y[13]
P = y[14]
ydot[0] = Q-r1*S0+d*KS0-a*K*S0+k*(PS1+PS2+PS3+PS4)
ydot[1] = k*KS0+d*(PS1+KS1)-S1*(r1+a*K+a*P)
ydot[2] = k*KS1+d*(PS2+KS2)-S2*(r1+a*K+a*P)
ydot[3] = k*KS2+d*(PS3+KS3)-S3*(r1+a*K+a*P)
ydot[4] = k*KS3+d*PS4-S4*(r2+a*P)
ydot[5] = a*K*S0-(d+k+r1)*KS0
ydot[6] = a*K*S1-(d+k+r1)*KS1
ydot[7] = a*K*S2-(d+k+r1)*KS2
ydot[8] = a*K*S3-(d+k+r1)*KS3
ydot[9] = a*P*S1-(d+k+r1)*PS1
ydot[10] = a*P*S2-(d+k+r1)*PS2
ydot[11] = a*P*S3-(d+k+r1)*PS3
ydot[12] = a*P*S4-(d+k+r2)*PS4
ydot[13] = (d+k+r1)*(KS0+KS1+KS2+KS3)-a*K*(S0+S1+S2+S3)
ydot[14] = (d+k+r1)*(PS1+PS2+PS3)+(d+k+r2)*PS4-a*P*(S1+S2+S3+S4)
return ydot[:,0]
y0[0] = S00
y0[13] = K0
y0[14] = P0
soln = odeint(f, y0, time, mxstep = 5000)
S0 = soln[:,0]
S1 = soln[:,1]
S2 = soln[:,2]
S3 = soln[:,3]
S4 = soln[:,4]
KS0 = soln[:,5]
KS1 = soln[:,6]
KS2 = soln[:,7]
KS3 = soln[:,8]
PS1 = soln[:,9]
PS2 = soln[:,10]
PS3 = soln[:,11]
PS4 = soln[:,12]
K = soln[:,13]
P = soln[:,14]
S0plot[Kloop,Qloop] = soln[len(time)-1,0]
S1plot[Kloop,Qloop] = soln[len(time)-1,1]
S2plot[Kloop,Qloop] = soln[len(time)-1,2]
S3plot[Kloop,Qloop] = soln[len(time)-1,3]
S4plot[Kloop,Qloop] = soln[len(time)-1,4]
KS0plot[Kloop,Qloop] = soln[len(time)-1,5]
KS1plot[Kloop,Qloop] = soln[len(time)-1,6]
KS2plot[Kloop,Qloop] = soln[len(time)-1,7]
KS3plot[Kloop,Qloop] = soln[len(time)-1,8]
PS1plot[Kloop,Qloop] = soln[len(time)-1,9]
PS2plot[Kloop,Qloop] = soln[len(time)-1,10]
PS3plot[Kloop,Qloop] = soln[len(time)-1,11]
PS4plot[Kloop,Qloop] = soln[len(time)-1,12]
Kplot[Kloop,Qloop] = soln[len(time)-1,13]
Pplot[Kloop,Qloop] = soln[len(time)-1,14]
Stot = S0plot + S1plot + S2plot + S3plot + S4plot + KS1plot + \
KS2plot + KS3plot + PS1plot + PS2plot + PS3plot + PS4plot
Smod = S4plot
print(Stot)
print(Smod)
Thanks for any help.
You are attempting to modify a global variable (namely ydot) in-place from a Numba function which has been JITed in nopython mode. This is not supported. Either change things so that ydot is a parameter of the function, or don't compile in nopython mode.

Scipy minimize erroring for large array of variables

Hi I am trying to use scipy for optimization. The minimize function with method as 'COBYLA' is working fine for small array size but errors out for larger sized arrays. I tried with 'COBYLA' and 'SLSQP' methods since I have a constrained optimization problem for non-linear functions.
Code snippet:
import scipy as sp
import random
def mytest7obj(x, x_d, y_d, z_d, power):
for x_i in x:
if x_i < 0:
return 0.
sum = 0.
for i in range(x_d):
for j in range(z_d):
term = 1.
for k in range(y_d):
term *= (x[i*(y_d*z_d)+j*(y_d)+k] ** power[k])
sum += term
return 0. - sum
def mytest7():
x_d = 30
y_d = 10
z_d = 100
goal = 1000000.
constraints = []
power = []
for i in range(y_d):
power.append(random.uniform(0.,0.3))
constraints.append({'type':'ineq', 'fun': lambda x: goal - sum(x)})
print 'power: %s\n' % (power,)
result = sp.optimize.minimize(fun = mytest7obj, x0 = [30.] * (x_d*y_d*z_d), method = 'COBYLA', args = (x_d, y_d, z_d, power), jac=False, constraints=constraints, options={'disp':True, 'rhobeg':3., 'maxiter': 10000})
print 'goal attained: %s'% (sum(result.x),)
if __name__ == “__main__”:
mytest7()
The traceback of the error with method 'COBYLA' is:
Traceback (most recent call last):
File "opt_test.py", line 584, in <module>
print 'mytest7'; mytest7()
File "opt_test.py", line 571, in mytest7
result = sp.optimize.minimize(fun = mytest7obj, x0 = [30.] * (x_d*y_d*z_d), method = 'COBYLA', args = (x_d, y_d, z_d, power), jac=False, constraints=constraints, options={'disp':True, 'rhobeg':3., 'maxiter': 10000})
File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 385, in minimize
return _minimize_cobyla(fun, x0, args, constraints, **options)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/cobyla.py", line 238, in _minimize_cobyla
dinfo=info)
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (-1594577286,)
With 'SLSQP', the error is:
File "opt_test.py", line 586, in <module>
print 'test'; test()
File "opt_test.py", line 454, in test
x = get_optimal(base, budget, initial_values, x_elas, y_elas, x_history, y_history, constraint_coeffs, opt_method = 'SLSQP')
File "opt_test.py", line 355, in get_optimal
constraints=constraints, options=opts)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 388, in minimize
constraints, **options)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py", line 316, in _minimize_slsqp
w = zeros(len_w)
MemoryError
I am using python 2.7.5,
scipy version: 0.14.0rc1,
numpy version: 1.8.1

Categories