Debugging a python function with parameters in Spyder

1,772 views
Skip to first unread message

FinnMcGovern

unread,
Aug 1, 2017, 12:34:51 PM8/1/17
to spyder
Hello Guys,

I am new to the debugging in spyder. I usually use a step by step debugging but I am getting confused because I am not able to get the different values of the for loop at some point. 

Here is the code :

# NelSon Siegel Yield Curve Computation
from datetime import date
import numpy as np
def nelsonsiegel(Beta0,Beta1,Beta2,Beta3,Lambda1,Lambda2):
    SettleDate = date(2017,07,14)
    Bond1MaturityDate = date(2018,7,13)
    Bond3MaturityDate = date(2020,2,17)
    Bond5MaturityDate = date(2022,7,21)
    Bond10MaturityDate = date(2027,1,20)
    Bond15MaturityDate = date(2031,9,16)
    Bond20MaturityDate = date(2037,3,17)
    Yearfraction = [float((Bond1MaturityDate-SettleDate).days)/365,float((Bond3MaturityDate-SettleDate).days)/365, float((Bond5MaturityDate-SettleDate).days)/365, float((Bond10MaturityDate-SettleDate).days)/365, float((Bond15MaturityDate-SettleDate).days)/365, float((Bond20MaturityDate-SettleDate).days)/365]
#    CouponRate = [0,0.0290,0.0321,0.0494,0.0585,0.0624]
#    BondPrices = [0.97863,0.99745,0.9968, 0.99922,0.98724,0.96679 ]
    NS = []
#    df = []
#    rst = []
#    NSS = []
#    result = []
    for i in range(len(Yearfraction)):        
      NelsonSiegel = Beta0 + (Beta1 * ((1-np.exp(-Yearfraction[i]/Lambda1)/Yearfraction[i]*Lambda1))) +  (Beta2 * ((((1-np.exp(-Yearfraction[i]/Lambda1))/(Yearfraction[i]*Lambda1))) - (np.exp(-Yearfraction[i]/Lambda1)))) + (Beta3 * ((((1-np.exp(-Yearfraction[i]/Lambda2))/(Yearfraction[i]*Lambda2))) - (np.exp(-Yearfraction[i]/Lambda2)))) 
      NS.append(NelsonSiegel)
      a = np.array(NS) # array for nelson siegel values             
      return NelsonSiegel

to run this function I use  : nelsonsiegel(0.01,0.01,0.01,0.01,1,1)

I normally put a break point one row before the return command.

can you explain to me please how to debug this in the loop step by step. I put the breakpoint and executed the command but it never stops at my breakpoint.

Thank you

FMG

Greg Bullock

unread,
Aug 4, 2017, 11:28:07 PM8/4/17
to spyder
Instead of setting the breakpoint using spyder's tools and using spyder's Debug command, I sometimes insert this line

import pdb; pdb.set_trace() # Pause execution here with the debugger active.

as the breakpoint in my code then use spyder's Run (not Debug) command.  Once this line is reached, spyder's variable inspector and the rest of its debug tools all seem to work fine (including the Continue button).

This offers multiple benefits:
  1. It averts a condition I've sometimes observed in using spyder 3.1.x's Debug command where breakpoints remain visible and apparently active but become ineffective after some code editing and a few rounds of debugging.  I'm not sure what the pattern is for this condition, but it maybe only on Windows (not Linux), and maybe only when using the Python console (instead of IPython console).
  2. My code seems to run faster up to the point of reaching this line when using the Run command instead of the Debug command.  (Some of my codes take tens of minutes to reach the line of interest, so "running faster" makes a difference to me.
  3. It allows making or modifying conditional breakpoints right in the code, with the conditions as simple or complex as you like.
Reply all
Reply to author
Forward
0 new messages