I've just started with Python yesterday, and I'm getting an error using scipy.integrate.odeint
.
I've defined a function
def SIR(x, t, beta, gamma, mu, M):
which takes the numpy.array
objects x
, t
, and M
; and the scalar floats beta
, gamma
, and mu
.
M
is (60,60)
in size, but I don't think this matters.
x
and t
are both nonsingleton, with x.shape
being (180,)
and t.shape
being (5000,)
. I've tried giving them a singleton dimension, such that they have shapes (180,1)
and (5000,1)
respectively, but I still get the same error :
In [1]: run measles_age.py
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/utils/py3compat.py in execfile(fname, *where)
173 else:
174 filename = fname
--> 175 __builtin__.execfile(filename, *where)
/Users/qcaudron/Documents/SIR/measles_age.py in <module>()
111
112
--> 113 x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
114
115 # plot(t, x);
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg)
141 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
142 full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 143 ixpr, mxstep, mxhnil, mxordn, mxords)
144 if output[-1] < 0:
145 print _msgs[output[-1]]
I get this error even when SIR
just returns x
, and if I strip all arguments apart from x
and t
from it :
def SIR(x, t):
return x;
As you can see, the line causing the error is
x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
EDIT :
I've been asked to add the full code for the SIR
method. Because it's relatively long, I've dropped the full .py script in a pastebin :
http://pastebin.com/RphJbCHN
Thanks again.
I can reproduce your error in several ways.
The error occurs immediately if either the y0
argument or the t
argument to odeint
is not a 1-D array. In the code example posted on pastebin (referred to in a comment), t
is reshaped like this:
t = np.arange(0, 520, 1);
t = t.reshape(len(t),1);
Delete the line that reshapes t
. t
must be a 1-D array, not a 2-D array with shape (len(t), 1).
For example...
In [177]: def SIR(x, t):
.....: return x
.....:
This works...
In [178]: x0 = [0.1, 0.2]
In [179]: odeint(SIR, x0, t=[0, 0.5, 1])
Out[179]:
array([[ 0.1 , 0.2 ],
[ 0.16487213, 0.32974426],
[ 0.27182822, 0.54365643]])
This results in the error:
In [180]: x0 = [[0.1, 0.2]] # wrong shape
In [181]: odeint(SIR, x0, t=[0, 0.5, 1])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-181-a37878f92395> in <module>()
----> 1 odeint(SIR, x0, t=[0, 0.5, 1])
/home/warren/anaconda/lib/python2.7/site-packages/scipy/integrate/odepack.pyc in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg)
142 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
143 full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 144 ixpr, mxstep, mxhnil, mxordn, mxords)
145 if output[-1] < 0:
146 print _msgs[output[-1]]
ValueError: object too deep for desired array
Check that the initial condition that you give to odeint
(the second argument) is a 1-D numpy array (not a 2-D array with shape (1, 180) or (180, 1)).
I also get the 'object too deep...' error if SIR
returns an array with the wrong shape. It must return a 1-D array, with the same shape as its first argument. Make sure it is truly 1-D, and not 2-D with shape (1, 180) or (180, 1).