Errors, or bugs, in your software

Today we'll cover dealing with errors in your Python code, an important aspect of writing software.

What is a software bug?

According to Wikipedia (accessed 16 Oct 2018), a software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or behave in unintended ways.

Where did the terminology come from?

Engineers have used the term well before electronic computers and software. Sometimes Thomas Edison is credited with the first recorded use of bug in that fashion. [Wikipedia]

If incorrect code is never executed, is it a bug?

This is the software equivalent to "If a tree falls and no one hears it, does it make a sound?".

Three classes of bugs

Let's discuss three major types of bugs in your code, from easiest to most difficult to diagnose:

  1. Syntax errors: Errors where the code is not written in a valid way. (Generally easiest to fix.)
  2. Runtime errors: Errors where code is syntactically valid, but fails to execute. Often throwing exceptions here. (Sometimes easy to fix, harder when in other's code.)
  3. Semantic errors: Errors where code is syntactically valid, but contain errors in logic. (Can be difficult to fix.)

In [1]:
import numpy as np

Syntax errors


In [38]:
print "This should only work in Python 2.x, not 3.x used in this class.


  File "<ipython-input-38-a09f58e53adb>", line 1
    print "This should only work in Python 2.x, not 3.x used in this class.
                                                                           ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("This should only work in Python 2.x, not 3.x used in this class.)?

INSTRUCTOR NOTE:

  1. Run as-is. Run. Error. Returns SyntaxError: Missing parentheses in call to print.
  2. Add parentheses. Run. Still an error. Returns SyntaxError: EOL while scanning string literal.
  3. Add closing quotation mark. Run. Should be successful.

In [17]:
x = 1; y = 2
b = x == y # Boolean variable that is true when x & y have the same value
b = 1 = 2

INSTRUCTOR NOTE:

  1. Emphasize the difference between the single and double equal operator.

In [18]:
b


Out[18]:
False

Runtime errors


In [11]:
# invalid operation
try:
    a = 0
    5/a  # Division by zero


  File "<ipython-input-11-395349a62179>", line 4
    5/a  # Division by zero
                           ^
SyntaxError: unexpected EOF while parsing

In [10]:
# invalid operation
input = '40'
input/11  # Incompatiable types for the operation


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-306c8d6b6c78> in <module>
      1 # Exception - invalid operation
      2 input = '40'
----> 3 input/11  # Incompatiable types for the operation

TypeError: unsupported operand type(s) for /: 'str' and 'int'

Semantic errors

Say we're trying to confirm that a trigonometric identity holds. Let's use the basic relationship between sine and cosine, given by the Pythagorean identity"

$$ \sin^2 \theta + \cos^2 \theta = 1 $$

We can write a function to check this:


In [31]:
import math

'''Checks that Pythagorean identity holds for one input, theta'''
def check_pythagorean_identity(theta):
    return math.sin(theta)**2 + math.cos(theta)*2 == 1


  File "<ipython-input-31-e644e043493d>", line 5
    return math.sin(theta)**2 math.cos(theta)*2 == 1
                                 ^
SyntaxError: invalid syntax

In [32]:
check_pythagorean_identity(12)


Out[32]:
False

How to find and resolve bugs?

Debugging has the following steps:

  1. Detection of an exception or invalid results.
  2. Isolation of where the program causes the error. This is often the most difficult step.
  3. Resolution of how to change the code to eliminate the error. Mostly, it's not too bad, but sometimes this can cause major revisions in codes.

Detection of Bugs

The detection of bugs is too often done by chance. While running your Python code, you encounter unexpected functionality, exceptions, or syntax errors. While we'll focus on this in today's lecture, you should never leave this up to chance in the future.

Software testing practices allow for thoughtful detection of bugs in software. We'll discuss more in the lecture on testing.

Isolation of Bugs

There are three main methods commonly used for bug isolation:

  1. The "thought" method. Think about how your code is structured and so what part of your could would most likely lead to the exception or invalid result.
  2. Inserting print statements (or other logging techniques)
  3. Using a line-by-line debugger like pdb.

Typically, all three are used in combination, often repeatedly.

Using print statements

Say we're trying to compute the entropy of a set of probabilities. The form of the equation is

$$ H = -\sum_i p_i \log(p_i) $$

We can write the function like this:


In [82]:
def entropy(p):
    items = p * np.log(p)
    return -np.add(items)

If we can't easily see the bug here, let's add print statements to see the variables change over time.

INSTRUCTOR NOTE:

  1. Add print statements in tiered way, starting with simple print statements.
  2. Point out that may need slight refactor on result.

    def entropy(p):
         print(p)
         items = p * np.log(p)
         print(items)
         result = -np.sum(items)
         print(result)
         return result
    
  3. Show complication of reading multiple print statements without labels.

  4. Add labels so code looks like below.

    def entropy(p):
         print("p=%s" % p)
         items = p * np.log(p)
         print("items=%s" % items)
         result = -np.sum(items)
         print("result=%s" % result)
         return result
    

In [73]:
np.add?

Note that the print statements significantly reduce legibility of the code. We would like to remove them when we're done debugging.


In [97]:
def entropy(p):
    items = p * np.log(p)
    return -np.sum(items)

In [80]:
p = [0.1, 0.3, 0.5, 0.7, 0.9]
entropy(p)


p=[0.1, 0.3, 0.5, 0.7, 0.9]
items=[-0.23025851 -0.36119184 -0.34657359 -0.24967246 -0.09482446]
result=1.2825208657263143
Out[80]:
1.2825208657263143

Now it works fine for the first set of inputs. Let's try other inputs.

We should have documented the inputs to the function!


In [101]:
# Create a vector of probabilities.
p = np.arange(start=5., stop=-1., step=-0.5)
p /= np.sum(p)
p


Out[101]:
array([ 0.18518519,  0.16666667,  0.14814815,  0.12962963,  0.11111111,
        0.09259259,  0.07407407,  0.05555556,  0.03703704,  0.01851852,
        0.        , -0.01851852])

In [105]:
entropy(p)


p=[ 0.18518519  0.16666667  0.14814815  0.12962963  0.11111111  0.09259259
  0.07407407  0.05555556  0.03703704  0.01851852  0.         -0.01851852]
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:3: RuntimeWarning: divide by zero encountered in log
  This is separate from the ipykernel package so we can avoid doing imports until
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in log
  This is separate from the ipykernel package so we can avoid doing imports until
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in multiply
  This is separate from the ipykernel package so we can avoid doing imports until
Out[105]:
nan

We get nan, which stands for "Not a Number". What's going on here?

Let's add our print statements again, but it only fails later in the range of numbers. We may choose to print only if we find a nan.


In [144]:
def entropy1(p):
    print("p=%s" % str(p))
    items = p * np.log(p)
    if [np.isnan(el) for el in items]:
        print(items)
    return -np.sum(items)

In [142]:
entropy1([.1, .2])


p=[0.1, 0.2]
Out[142]:
0.5521460917862246

In [145]:
entropy1(p)


p=[0.1, -0.2, 0.3]
[-0.23025851         nan -0.36119184]
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in log
  This is separate from the ipykernel package so we can avoid doing imports until
Out[145]:
nan

By printing some of the intermediate items, we see the problem: 0 * np.log(0) is resulting in a NaN. Though mathematically it's true that limx→0[xlog(x)]=0limx→0[xlog⁡(x)]=0, the fact that we're performing the computation numerically means that we don't obtain this result.

Often, inserting a few print statements can be enough to figure out what's going on.


In [140]:
def entropy2(p):
    p = np.asarray(p)  # convert p to array if necessary
    print(p)
    items = []
    for val in p:
        item = val * np.log(val)
        if np.isnan(item):
            print("%f makes a nan" % val)
        items.append(item)
    #items = p * np.log(ps)
    return -np.sum(items)

In [114]:
entropy2(p)


[ 0.18518519  0.16666667  0.14814815  0.12962963  0.11111111  0.09259259
  0.07407407  0.05555556  0.03703704  0.01851852  0.         -0.01851852]
0.000000 makes a nan
-0.018519 makes a nan
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:6: RuntimeWarning: divide by zero encountered in log
  
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in double_scalars
  
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in log
  
Out[114]:
nan

Using Python's debugger, pdb

Python comes with a built-in debugger called pdb. It allows you to step line-by-line through a computation and examine what's happening at each step. Note that this should probably be your last resort in tracing down a bug. I've probably used it a dozen times or so in five years of coding. But it can be a useful tool to have in your toolbelt.

You can use the debugger by inserting the line

import pdb; pdb.set_trace()

within your script. To leave the debugger, type "exit()". To see the commands you can use, type "help".

Let's try this out:


In [148]:
def entropy(p):
    import pdb; pdb.set_trace()
    items = p * np.log(p)
    return -np.sum(items)

This can be a more convenient way to debug programs and step through the actual execution.


In [ ]:
p = [.1, -.2, .3]
entropy(p)


> <ipython-input-148-f3ebd906567f>(3)entropy()
-> items = p * np.log(p)
(Pdb) n
/usr/local/Miniconda/lib/python3.6/site-packages/ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in log
  This is separate from the ipykernel package so we can avoid doing imports until
> <ipython-input-148-f3ebd906567f>(4)entropy()
-> return -np.sum(items)
(Pdb) items
array([-0.23025851,         nan, -0.36119184])

In [81]:
p = "[0.1, 0.3, 0.5, 0.7, 0.9]"
entropy(p)


p=[0.1, 0.3, 0.5, 0.7, 0.9]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-81-4bc474633a01> in <module>
      1 p = "[0.1, 0.3, 0.5, 0.7, 0.9]"
----> 2 entropy(p)

<ipython-input-79-cf4afdc2af13> in entropy(p)
      5 def entropy(p):
      6         print("p=%s" % p)
----> 7         items = p * np.log(p)
      8         print("items=%s" % items)
      9         result = -np.sum(items)

TypeError: ufunc 'log' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''