In [1]:
temp_in_K = 290.5
temp_in_C = temp_in_K - 273.15
print temp_in_C


17.35

In [2]:
def kelvin_to_celsius(temp):
    return temp - 273.15

In [3]:
temp_in_K = 290.5
temp_in_C = kelvin_to_celsius(temp_in_K)
print temp_in_C


17.35

In [4]:
type(kelvin_to_celsius)


Out[4]:
function

In [6]:
def fahr_to_kelvin(temp):
    return ((temp - 32) * (5.0 / 9)) + 273.15

In [7]:
print fahr_to_kelvin(100)


310.927777778

In [8]:
print kelvin_to_celsius(fahr_to_kelvin(100))


37.7777777778

In [9]:
temp = 120
print fahr_to_kelvin(temp)
print temp


322.038888889
120

In [10]:
import numpy
import matplotlib.pyplot
import glob


Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 29 days

In [13]:
%matplotlib inline

In [31]:
def analyse(filename):
    """Analyse average, max and min of patient data"""
    data = numpy.loadtxt(fname=filename, delimiter=',')
    fig = matplotlib.pyplot.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()

    matplotlib.pyplot.show(fig)

In [12]:
analyse('inflammation-01.csv')

In [28]:
def detect_problem(filename):
    """Detect possible data quality problems.
        filename - CSV file containing data to analyse
    """
    data = numpy.loadtxt(fname=filename, delimiter=',')
    
    if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
        return (False, "Data looks suspicious")
#     elif data.min(axis=0).sum() == 0:
#         return (False, 'Minima add up to zero!')
    else:
        return (True, 'Data seems OK')

In [29]:
detect_problem('inflammation-01.csv')


Out[29]:
(False, 'Data looks suspicious')

In [23]:
datafiles = glob.glob('i*.csv')
datafiles.sort()
for filename in datafiles:
    quality = detect_problem(filename)
    print quality
    proceed = quality[0]
    if proceed:
        analyse(filename)


(False, 'Data looks suspicious')
(False, 'Data looks suspicious')
(True, 'Data seems OK')
(False, 'Data looks suspicious')
(False, 'Data looks suspicious')
(False, 'Data looks suspicious')
(False, 'Data looks suspicious')
(True, 'Data seems OK')
(False, 'Data looks suspicious')
(False, 'Data looks suspicious')
(True, 'Data seems OK')
(False, 'Data looks suspicious')

In [30]:
help(detect_problem)


Help on function detect_problem in module __main__:

detect_problem(filename)
    Detect possible data quality problems.
    filename - CSV file containing data to analyse


In [44]:
import math

where_i_am = [100, 200]

def distance(position, to=(0, 0)):
    """Calculate the distance between two points"""
    pos_x = position[0]
    pos_y = position[1]
    dest_x = to[0]
    dest_y = to[1]
    
    dist = math.sqrt((pos_x - dest_x)**3 + (pos_y - dest_y)**2)
    return dist

In [37]:
distance((10,0), (100,100))


Out[37]:
134.5362404707371

In [40]:
distance(to=(100,100), (10,0))


  File "<ipython-input-40-a178a0d41f49>", line 1
    distance(to=(100,100), (10,0))
SyntaxError: non-keyword arg after keyword arg

In [49]:
def test_distance():
    """Test the distance() function"""
    start1 = (0,10)
    assert distance(start1) == 10.0
    end2 = (0, 30)
    assert distance(start1, end2) == 20.0
    start2 = (10,0)
    assert distance(start2) == 10, "Distance from (10,0) to (0,0) is not 10"
    return True

In [50]:
test_distance()


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-50-0f315de521b6> in <module>()
----> 1 test_distance()

<ipython-input-49-61cd8cf1142a> in test_distance()
      6     assert distance(start1, end2) == 20.0
      7     start2 = (10,0)
----> 8     assert distance(start2) == 10, "Distance from (10,0) to (0,0) is not 10"
      9     return True

AssertionError: Distance from (10,0) to (0,0) is not 10

Exercises

1 Write a function fence that takes two strings and a "fence character". It should return the two strings joined together with the "fence character" in the middle. E.g. fence("oh","my","|") should return "oh|my".

2 Write a function that returns the first and last characters of a string. Call it outer(). So calling outer("Hello") should return "Ho".


In [51]:
"oh" + "|" + "my"


Out[51]:
'oh|my'

In [56]:
def fence(string1, string2, middle):
    """Combine strings with middle in the middle."""
    return string1 + middle + string2

In [67]:
fence("oh", "my", "|")


Out[67]:
'oh|my'

In [65]:
def test_fence():
    assert fence("oh", "my", "|") == "oh|my"
    return True

In [66]:
test_fence()


Out[66]:
True

In [63]:
list("hello")


Out[63]:
['h', 'e', 'l', 'l', 'o']

In [64]:
def fence(string1, string2, middle):
    string_list = [string1, string2]
    joined_string = middle.join(string_list)
    return joined_string

In [69]:
hello_list = list("hello")
print hello_list
fenced_hello = "|".join(hello_list)
print fenced_hello


['h', 'e', 'l', 'l', 'o']
h|e|l|l|o

In [74]:
def outer(string):
    return (string[0], string[-1])

In [77]:
print outer('Hello')[0]


H

In [ ]: