Notebook for the Python lesson at Weill Cornell Medical College

(Time 3 hours 15 minutes)

Python jargon

  • "Python" refers to the programming language itself. You use Python to write source code and give instructions to your computer.
    • Python has two dialects: Python 2 (we're using that) and Python 3 (newer, but not sticking yet)
    • Python has only a few words (keywords), like 20~ish. print is a keyword (in Python 2, not in 3)
  • "python" refers to the interpreter, a program that translates source code into actual instructions to the machine
    • you call the interpreter typing python in your terminal
    • working in the interpreter can be hard (ugly and error-prone)
  • "ipython" is a wrapper around the interpreter, which adds some nice features, but you still have to work in a command line interface
    • you call the "ipython" typing ipython in your terminal
  • "ipython notebook" is another "wrapper" that puts everything into your browser
    • you call the "ipython notebook" (now called "jupyter") typing ipython notebook in your terminal

A simple command (actually is called a statement) in Python

In [1]:
print "Hello"
Hello

Names and objects

In [2]:
weight_kg = 55
In [3]:
weight_kg
Out[3]:
55
In [4]:
print weight_kg
55
In [5]:
print "Weight in pounds is ", 2.2 * weight_kg
Weight in pounds is  121.0
In [6]:
weight_lb = 2.2 * weight_kg
In [7]:
print weight_lb
121.0
In [8]:
weight_kg = 100
In [9]:
print weight_kg
100
In [10]:
print weight_lb
121.0

Loading a library of functions (is called a module) from your hard drive into this program

Numpy is a library for some math in Python

In [11]:
import numpy

We creating a new name (data) to store the result of calling a function in the numpy module that reads text files (numpy.loadtxt)

In [14]:
data = numpy.loadtxt(fname="data/inflammation-01.csv", delimiter=',')
In [15]:
print data
[[ 0.  0.  1. ...,  3.  0.  0.]
 [ 0.  1.  2. ...,  1.  0.  1.]
 [ 0.  1.  1. ...,  2.  1.  1.]
 ..., 
 [ 0.  1.  1. ...,  1.  1.  1.]
 [ 0.  0.  0. ...,  0.  2.  0.]
 [ 0.  0.  1. ...,  1.  1.  0.]]

type is a Python built-in function (is loaded by default when you start any interpreter) which tells you the kind of object referred by a name.

Object is the generic concept for "things" in Python, everything is an object. Objects represent how the "thing" is stored in the computer memory, for example they have instructions on how your computer reads the object from the memory, so you may get an error if you are trying to process an object as the wrong kind. More importantly for you, objects have functions associated to them (called methods). Each type of object may have its own methods.

In [16]:
print type(data)
<type 'numpy.ndarray'>
In [17]:
print type(weight_lb)
<type 'float'>
In [18]:
data.shape
Out[18]:
(60, 40)
In [20]:
weight_lb.shape
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-18bfedc41582> in <module>()
----> 1 weight_lb.shape

AttributeError: 'float' object has no attribute 'shape'

Indexing and slicing

In [22]:
data[3:4, 20]
Out[22]:
13.0
In [22]:
data[:4, 30:]
Out[22]:
13.0
In [22]:
data[30, 20]
Out[22]:
13.0
In [28]:
sample = data[4:8, 1:3]
In [29]:
print sample
[[ 1.  1.]
 [ 0.  1.]
 [ 0.  2.]
 [ 0.  1.]]
In [ ]:
### Operators and methods for Numpy arrays
In [30]:
double_sample = 2 * sample
In [31]:
print double_sample
[[ 2.  2.]
 [ 0.  2.]
 [ 0.  4.]
 [ 0.  2.]]
In [32]:
data.mean()
Out[32]:
6.1487499999999997
In [33]:
data.max()
Out[33]:
20.0
In [34]:
data.min()
Out[34]:
0.0

Pause and reflect on the amount of work that your computer has done when you called any of this methods. This is called high-level programming language and means you don't need to know much about how your computer does its thing.

Slicing a numpy array gives you another numpy array (nice, we still have all methods available)

In [35]:
patient_0 = data[0, :]
In [36]:
print patient_0
[  0.   0.   1.   3.   1.   2.   4.   7.   8.   3.   3.   3.  10.   5.   7.
   4.   7.   7.  12.  18.   6.  13.  11.  11.   7.   7.   4.   6.   8.   8.
   4.   4.   5.   7.   3.   4.   2.   3.   0.   0.]
In [37]:
type(patient_0)
Out[37]:
numpy.ndarray
In [38]:
patient_0.shape
Out[38]:
(40,)
In [39]:
patient_0.mean()
Out[39]:
5.4500000000000002
In [41]:
data.mean(axis=1)
Out[41]:
array([ 5.45 ,  5.425,  6.1  ,  5.9  ,  5.55 ,  6.225,  5.975,  6.65 ,
        6.625,  6.525,  6.775,  5.8  ,  6.225,  5.75 ,  5.225,  6.3  ,
        6.55 ,  5.7  ,  5.85 ,  6.55 ,  5.775,  5.825,  6.175,  6.1  ,
        5.8  ,  6.425,  6.05 ,  6.025,  6.175,  6.55 ,  6.175,  6.35 ,
        6.725,  6.125,  7.075,  5.725,  5.925,  6.15 ,  6.075,  5.75 ,
        5.975,  5.725,  6.3  ,  5.9  ,  6.75 ,  5.925,  7.225,  6.15 ,
        5.95 ,  6.275,  5.7  ,  6.1  ,  6.825,  5.975,  6.725,  5.7  ,
        6.25 ,  6.4  ,  7.05 ,  5.9  ])

Plotting

You can import parts of a module using from, in this case the pyplot part of matplotlib.

In [ ]:
from matplotlib import pyplot
In [45]:
%matplotlib inline
image = pyplot.imshow(data)
pyplot.show(image)
In [46]:
ave_inflammation = data.mean(axis=0)
ave_plot = pyplot.plot(ave_inflammation)
pyplot.show(ave_plot)
In [47]:
max_plot = pyplot.plot(data.max(axis=0))
pyplot.show(max_plot)

Putting all together in one nice piece of code

In [48]:
import numpy as np
from matplotlib import pyplot as plt

data = np.loadtxt(fname="data/inflammation-01.csv", delimiter=",")

fig = plt.figure(figsize=(10.0, 3.0))

axes1 = fig.add_subplot(1, 3, 1)
axes2 = fig.add_subplot(1, 3, 2)
axes3 = fig.add_subplot(1, 3, 3)

axes1.set_ylabel('average')
axes1.plot(data.mean(axis=0))

axes2.set_ylabel('max')
axes2.plot(data.max(axis=0))

axes3.set_ylabel('min')
axes3.plot(data.min(axis=0))

fig.tight_layout()
plt.show(fig)

For loops

In [51]:
word = "lead"
In [52]:
word
Out[52]:
'lead'
In [55]:
print word[0]
print word[1]
print word[2]
print word[3]
d
In [58]:
length = 0
for char in word:
    length = length + 1
    print char
    print "The number of letters ", length 
l
The number of letters  1
e
The number of letters  2
a
The number of letters  3
d
The number of letters  4
In [59]:
print char
d
In [60]:
print length
4
In [61]:
len(word)
Out[61]:
4
In [64]:
range(2, 8, 2)
Out[64]:
[2, 4, 6]
In [66]:
for number in range(1,4):
    print number
1
2
3
In [71]:
result = 1
for times in range(3):
    result *= 5
print result
125

Lists

In [73]:
l = range(5)
In [74]:
type(l)
Out[74]:
list
In [75]:
odd = [1, 3, 5]
In [76]:
type(odd)
Out[76]:
list
In [77]:
print odd
[1, 3, 5]
In [81]:
odd[1:]
Out[81]:
[3, 5]
In [83]:
odd[-2]
Out[83]:
3
In [84]:
names = ["Newton", "Darwin", "Tuinrg"]
In [85]:
names[-2]
Out[85]:
'Darwin'
In [86]:
darwin = names[-2]
In [87]:
print darwin
Darwin
In [88]:
names[-1] = "Turing"
In [89]:
names
Out[89]:
['Newton', 'Darwin', 'Turing']
In [90]:
darwin
Out[90]:
'Darwin'
In [91]:
darwin[0]
Out[91]:
'D'
In [92]:
darwin[0] = 'd'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-92-1fe224db21d9> in <module>()
----> 1 darwin[0] = 'd'

TypeError: 'str' object does not support item assignment
In [93]:
odd
Out[93]:
[1, 3, 5]
In [94]:
odd.append(7)
In [95]:
odd
Out[95]:
[1, 3, 5, 7]
In [96]:
odd.reverse()
In [97]:
odd
Out[97]:
[7, 5, 3, 1]
In [98]:
odd.append('john')

Lists can have different types of objects

In [99]:
odd
Out[99]:
[7, 5, 3, 1, 'john']
In [101]:
type(odd[0])
Out[101]:
int
In [102]:
type(odd[-1])
Out[102]:
str

Using glob to get a list of files in our filesystem

Sorta ls in the shell, but storing the result into a Python list (nice!)

In [103]:
import glob
In [105]:
print glob.glob('*.ipynb')
['inflammation-analysis.ipynb']
In [106]:
print glob.glob('./data/*.csv')
['./data/inflammation-01.csv', './data/inflammation-02.csv', './data/inflammation-03.csv', './data/inflammation-04.csv', './data/inflammation-05.csv', './data/inflammation-06.csv', './data/inflammation-07.csv', './data/inflammation-08.csv', './data/inflammation-09.csv', './data/inflammation-10.csv', './data/inflammation-11.csv', './data/inflammation-12.csv', './data/small-01.csv', './data/small-02.csv', './data/small-03.csv']
In [107]:
filenames = glob.glob("data/*csv")
In [108]:
print filenames
['data/inflammation-01.csv', 'data/inflammation-02.csv', 'data/inflammation-03.csv', 'data/inflammation-04.csv', 'data/inflammation-05.csv', 'data/inflammation-06.csv', 'data/inflammation-07.csv', 'data/inflammation-08.csv', 'data/inflammation-09.csv', 'data/inflammation-10.csv', 'data/inflammation-11.csv', 'data/inflammation-12.csv', 'data/small-01.csv', 'data/small-02.csv', 'data/small-03.csv']

Putting all together: loop and analyze a bunch of files

In [111]:
for f in filenames[:3]:
    print "Analyzing file: ", f
    analyze_inflammation(f)
Analyzing file:  data/inflammation-01.csv
Analyzing file:  data/inflammation-02.csv
Analyzing file:  data/inflammation-03.csv
In [153]:
def analyze(filename):
    """Analyze inflammation data and make plots
    """
    data = np.loadtxt(fname=filename, delimiter=",")

    fig = plt.figure(figsize=(10.0, 3.0))

    axes1 = fig.add_subplot(1, 3, 1)
    axes2 = fig.add_subplot(1, 3, 2)
    axes3 = fig.add_subplot(1, 3, 3)

    axes1.set_ylabel('average')
    axes1.plot(data.mean(axis=0))

    axes2.set_ylabel('max')
    axes2.plot(data.max(axis=0))

    axes3.set_ylabel('min')
    axes3.plot(data.min(axis=0))

    fig.tight_layout()

    plt.show(fig)

Conditionals

In [116]:
weight_kg = 20
In [117]:
if weight_kg > 80:
    print "You're over 80 kg"
else:
    print "You're under 80 kg"
You're fine
In [129]:
number = -3
In [132]:
if (number >= 0):
    print "Positive"
else:
    print "Negative"
Negative

Functions

In [135]:
(80 - 32) * 5/9 + 273.15
Out[135]:
299.15
In [136]:
def fahr_to_kelvin(temp):
    return (temp - 32) * 5.0/9 + 273.15
In [150]:
def kelvin_to_celsius(temp):
    '''Converts temperatures in Kelvin to Celsius
    
    More stuff
    '''
    result = temp - 273.15
    return result
In [ ]:
def farh_to_celsius(temp):
    kelvin = fahr_to_kelvin(temp)
    result = kelvin_to_celsius(kelvin)
    return result
In [152]:
kelvin_to_celsius(fahr_to_kelvin(0))
Out[152]:
-18.0
In [138]:
fahr_to_kelvin(80)
Out[138]:
299.15
In [139]:
temp_in_kelvin = fahr_to_kelvin(100)
In [140]:
temp_in_kelvin
Out[140]:
310.15

Be careful with dividing numbers

In [3]:
10/9
Out[3]:
1
In [143]:
10.0/9
Out[143]:
1.1111111111111112
In [154]:
help(analyze)
Help on function analyze in module __main__:

analyze(filename)
    Analyze inflammation data and make plots

In [155]:
help(np.loadtxt)
Help on function loadtxt in module numpy.lib.npyio:

loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
    Load data from a text file.
    
    Each row in the text file must have the same number of values.
    
    Parameters
    ----------
    fname : file or str
        File, filename, or generator to read.  If the filename extension is
        ``.gz`` or ``.bz2``, the file is first decompressed. Note that
        generators should return byte strings for Python 3k.
    dtype : data-type, optional
        Data-type of the resulting array; default: float.  If this is a
        record data-type, the resulting array will be 1-dimensional, and
        each row will be interpreted as an element of the array.  In this
        case, the number of columns used must match the number of fields in
        the data-type.
    comments : str, optional
        The character used to indicate the start of a comment;
        default: '#'.
    delimiter : str, optional
        The string used to separate values.  By default, this is any
        whitespace.
    converters : dict, optional
        A dictionary mapping column number to a function that will convert
        that column to a float.  E.g., if column 0 is a date string:
        ``converters = {0: datestr2num}``.  Converters can also be used to
        provide a default value for missing data (but see also `genfromtxt`):
        ``converters = {3: lambda s: float(s.strip() or 0)}``.  Default: None.
    skiprows : int, optional
        Skip the first `skiprows` lines; default: 0.
    usecols : sequence, optional
        Which columns to read, with 0 being the first.  For example,
        ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns.
        The default, None, results in all columns being read.
    unpack : bool, optional
        If True, the returned array is transposed, so that arguments may be
        unpacked using ``x, y, z = loadtxt(...)``.  When used with a record
        data-type, arrays are returned for each field.  Default is False.
    ndmin : int, optional
        The returned array will have at least `ndmin` dimensions.
        Otherwise mono-dimensional axes will be squeezed.
        Legal values: 0 (default), 1 or 2.
    
        .. versionadded:: 1.6.0
    
    Returns
    -------
    out : ndarray
        Data read from the text file.
    
    See Also
    --------
    load, fromstring, fromregex
    genfromtxt : Load data with missing values handled as specified.
    scipy.io.loadmat : reads MATLAB data files
    
    Notes
    -----
    This function aims to be a fast reader for simply formatted files.  The
    `genfromtxt` function provides more sophisticated handling of, e.g.,
    lines with missing values.
    
    Examples
    --------
    >>> from StringIO import StringIO   # StringIO behaves like a file object
    >>> c = StringIO("0 1\n2 3")
    >>> np.loadtxt(c)
    array([[ 0.,  1.],
           [ 2.,  3.]])
    
    >>> d = StringIO("M 21 72\nF 35 58")
    >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
    ...                      'formats': ('S1', 'i4', 'f4')})
    array([('M', 21, 72.0), ('F', 35, 58.0)],
          dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])
    
    >>> c = StringIO("1,0,2\n3,0,4")
    >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
    >>> x
    array([ 1.,  3.])
    >>> y
    array([ 2.,  4.])