Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython error row and column

Is it possible in IPython interactive console to show not only the row at which the error is originating but also the column?

For example in this script, I know that the problematic line is 934 but I have no way to find which statement generates the error since I don't know the column.

--> 934                 dQ0 = (Knm0[u, :] + W0[u, u] - Knm0[u, ma]) - multiply((Kn0[u]), (Km0 + Kn0[u] - Km0[ma])) / s0  
    935                 dQ1 = (Knm1[u, :] + W1[u, u] - Knm1[u, ma]) - multiply((Kn1[u]), (Km1 + Kn1[u] - Km1[ma])) / s1  
    936                 dQ = d0 * dQ0 - d1 * dQ1  

IndexError: invalid index to scalar variable.
like image 210
linello Avatar asked Jun 21 '26 15:06

linello


2 Answers

Assuming I correctly understood your problem, change:

dQ0 = (Knm0[u, :] + W0[u, u] - Knm0[u, ma]) - multiply((Kn0[u]), (Km0 + Kn0[u] - Km0[ma])) / s0  

to

dummy0 = (Knm0[u, :] + W0[u, u] - Knm0[u, ma])
dummy1 = multiply((Kn0[u]), (Km0 + Kn0[u] - Km0[ma])) / s0  
dQ0 = dummy0 - dummy1

and so on, chopping the big problems in separate pieces, untill you know exactly where the problem is

Complicated oneliner statements make code more difficult to debug or to understand months/years later, or by someone else who did not write your code

like image 53
usethedeathstar Avatar answered Jun 24 '26 04:06

usethedeathstar


Normally, the shortest way to debug these is simply this:

In [5]: %debug

(or just debug if %automagic is on). That drops you into the debugger where the failure occurred. Then you can dump the different objects i.e. Knm0, Kn0, etc. or try different pieces like Knm0[u,:] and see which one throws the error, what the object looks like, etc.

In your case, it looks like you're trying to use slicing on something that's not array, like doing 245[0] is not legal. So it's not as simple as which column is the problem - this line isn't the problem at all, it's mostly likely some line above that generated the variables this line uses that actually has the bug.

like image 42
Corley Brigman Avatar answered Jun 24 '26 05:06

Corley Brigman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!