2

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.

6
  • 1
    Welcome to python! Most of those semicolons are unnecessary in your code. Commented Mar 29, 2013 at 19:20
  • Thanks. I'm aware they're unnecessary - I just think things looks cleaner with them :) Thanks for the edit above. Commented Mar 29, 2013 at 19:22
  • From the documentation, SIR should return dx/dt at t. Is that what SIR does? Commented Mar 29, 2013 at 19:28
  • Yes, in the full code, it does. Where it returns x immediately, it's not really relevant - the equation we're solving could well be dx/dt = x, in which case, yes, it's returning the correct calculation. I'm not sure why an "object is too deep for desired array" error is being returned. Commented Mar 29, 2013 at 19:38
  • The source is in fortran. Have you tried using integrate.ode? Commented Mar 29, 2013 at 19:45

2 Answers 2

3

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you - however, I've checked this. SIR returns a numpy array (180,) in size, and the y0 argument to odeint is also (180,) in shape.
@Quentin: If you can, please add your code for SIR to the question, along with a self-contained way of running it, so we can reproduce the error you get.
I updated my answer with a comment about t being 1-D. Don't reshape it to be a 2-D array.
Thank you so much, Warren. I don't know how I missed that. I fixed this, and it seems to run.
-1

From the tutorial it looks like the first argument to integrate.odeint() needs to be a function to operate on (in your case) x0 and t. Since your SIR() function only takes one argument, the operation is failing. It's possible that the size and/or shape of the results returned from SIR() are important in relation to the rest of the arguments.

1 Comment

Sorry, perhaps I wasn't clear. I stripped it to just that to see if the problem was the SIR function. It wasn't. I got the same error as when SIR took five arguments : def SIR(x, t, beta, gamma, mu): return x; and was called using x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.