[Tutor] Fitting data to error function
Oscar Benjamin
oscar.j.benjamin at gmail.com
Tue Mar 17 00:10:59 CET 2015
On 16 March 2015 at 22:22, Colin Ross <colin.ross.dal at gmail.com> wrote:
>
> Yes, thank you, they were suppose to both be E_out.
Hi Colin,
I'm not sure if that means that your problem is fixed or not but I
thought I would point something out that helps in fixing this kind of
problem.
You're using ipython which has an excellent debugger. So let's say I
have the following buggy (and not very useful) program called tmp.py:
from numpy.fft import fft
def g(z):
return str(z)
def f(x):
return fft(g(x))
f(1)
I can run the program under ipython by typing "run tmp.py":
$ ipython
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: run tmp.py
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in
execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/stuff/oscar/work/current/tmp/ipdb/tmp.py in <module>()
7 return fft(g(x))
8
----> 9 f(1)
/stuff/oscar/work/current/tmp/ipdb/tmp.py in f(x)
5
6 def f(x):
----> 7 return fft(g(x))
8
9 f(1)
/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in fft(a, n, axis)
172 """
173
--> 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf,
_fft_cache)
175
176
/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in _raw_fft(a,
n, axis, init_function, work_function, fft_cache)
48
49 if n is None:
---> 50 n = a.shape[axis]
51
52 if n < 1:
IndexError: tuple index out of range
That's great because the information that I see shows me where the
error occurs - if I know how to read it. In this case I look up until
I find a line that is part of my own code i.e. line 7 of tmp.py.
That's where I passed something into the numpy fft function which lead
to an error.
At this point I can try to understand what is the object that is being
passed into fft by looking at my code. Or I can use the ipython
debugger to manually inspect the object. Turn the debugger on by
typing "pdb". Then run your script again:
In [2]: pdb
Automatic pdb calling has been turned ON
In [3]: run tmp.py
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in
execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/stuff/oscar/work/current/tmp/ipdb/tmp.py in <module>()
7 return fft(g(x))
8
----> 9 f(1)
/stuff/oscar/work/current/tmp/ipdb/tmp.py in f(x)
5
6 def f(x):
----> 7 return fft(g(x))
8
9 f(1)
/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in fft(a, n, axis)
172 """
173
--> 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf,
_fft_cache)
175
176
/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in _raw_fft(a,
n, axis, init_function, work_function, fft_cache)
48
49 if n is None:
---> 50 n = a.shape[axis]
51
52 if n < 1:
IndexError: tuple index out of range
> /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(50)_raw_fft()
49 if n is None:
---> 50 n = a.shape[axis]
51
ipdb> u
> /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(174)fft()
173
--> 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf,
_fft_cache)
175
ipdb> u
> /stuff/oscar/work/current/tmp/ipdb/tmp.py(7)f()
6 def f(x):
----> 7 return fft(g(x))
8
ipdb> p x
1
ipdb> p g(x)
'1'
ipdb> p type(g(x))
<type 'str'>
The debugger with the ipdb prompt freezes the script at the point
where the error occurs and enables me to manually inspect what is
going on. It started in the numpy code so I typed "u" twice to go "up"
two frames to where my function called the function that called the
function that generated the exception. Here I can query the object x
(typing "p" for "print") and test what g(x) returns. Now the problem
is clear to me: I'm passing a string into fft when it is expecting an
array of numbers.
The python interpreter comes with a slightly less convenient debugger
called pdb that you can use in the same way if you run your script
from the terminal like so:
$ python -m pdb tmp.py
> /stuff/oscar/work/current/tmp/ipdb/tmp.py(1)<module>()
-> from numpy.fft import fft
(Pdb) c
Traceback (most recent call last):
File "/usr/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/usr/lib/python2.7/bdb.py", line 400, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "tmp.py", line 1, in <module>
from numpy.fft import fft
File "tmp.py", line 7, in f
return fft(g(x))
File "/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py", line 174, in fft
return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache)
File "/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py", line
50, in _raw_fft
n = a.shape[axis]
IndexError: tuple index out of range
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(50)_raw_fft()
-> n = a.shape[axis]
(Pdb) u
> /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(174)fft()
-> return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache)
(Pdb) u
> /stuff/oscar/work/current/tmp/ipdb/tmp.py(7)f()
-> return fft(g(x))
(Pdb) p x
1
(Pdb) p g(x)
'1'
Oscar
More information about the Tutor
mailing list