Debugging, Profiling, ...

Progress Bars...


In [1]:
!pip install tqdm


Collecting tqdm
  Downloading tqdm-4.14.0-py2.py3-none-any.whl (46kB)
    100% |████████████████████████████████| 51kB 1.2MB/s ta 0:00:01
Installing collected packages: tqdm
Successfully installed tqdm-4.14.0

In [4]:
from tqdm import tqdm
from time import sleep

for i in tqdm(range(1000)):
    sleep(0.01)


100%|██████████| 1000/1000 [00:11<00:00, 89.39it/s]

In [1]:
%%time

for _ in range(1000):
    sleep(0.01)# sleep for 0.01 seconds


CPU times: user 20 ms, sys: 18.5 ms, total: 38.5 ms
Wall time: 11.6 s

In [2]:
import numpy as np
%timeit np.random.normal(size=100)


The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 6.1 µs per loop

In [4]:
# %load some_code.py

import numpy as np
from scipy.stats import kendalltau
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="ticks")

rs = np.random.RandomState(11)
x = rs.gamma(2, size=1000)
y = -.5 * x + rs.normal(size=1000)

%prun sns.jointplot(x, y, kind="hex", stat_func=kendalltau, color="#4CB391")
plt.show()


 

In [1]:
%pdb
def f(x):
    return 1. / x

y = f(0)


Automatic pdb calling has been turned ON
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-1-23fd9377a67e> in <module>()
      3     return 1. / x
      4 
----> 5 y = f(0)

<ipython-input-1-23fd9377a67e> in f(x)
      1 get_ipython().magic('pdb')
      2 def f(x):
----> 3     return 1. / x
      4 
      5 y = f(0)

ZeroDivisionError: float division by zero
> <ipython-input-1-23fd9377a67e>(3)f()
      1 get_ipython().magic('pdb')
      2 def f(x):
----> 3     return 1. / x
      4 
      5 y = f(0)

ipdb> x =5
ipdb> step

In [ ]:
%pdf off

In [ ]:
%debug


> <ipython-input-10-b23e3a05e379>(2)f()
      1 def f(x):
----> 2     return 1. / x
      3 
      4 y = f(0)

ipdb> x
0
ipdb> help

Documented commands (type help <topic>):
========================================
EOF    cl         disable  interact  next    psource  rv         unt   
a      clear      display  j         p       q        s          until 
alias  commands   down     jump      pdef    quit     source     up    
args   condition  enable   l         pdoc    r        step       w     
b      cont       exit     list      pfile   restart  tbreak     whatis
break  continue   h        ll        pinfo   return   u          where 
bt     d          help     longlist  pinfo2  retval   unalias  
c      debug      ignore   n         pp      run      undisplay

Miscellaneous help topics:
==========================
exec  pdb

ipdb> source
*** SyntaxError: unexpected EOF while parsing
ipdb> whatis x
<class 'int'>
ipdb> whatis y
*** NameError: name 'y' is not defined

In [9]:
y


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-009520053b00> in <module>()
----> 1 y

NameError: name 'y' is not defined

Cython


In [ ]:
!pip install cython

In [ ]:
%load_ext Cython

In [ ]:
%%cython
def myltiply_by_2(float x):
    return 2.0 * x

In [ ]:
def mult(x):
    return 2.0 * x

In [ ]:
%ptime [mult(x) for x in range(100000)]

In [ ]:
%ptime [myltiply_by_2(x) for x in range(100000)]