In [1]:
temp_in_K = 290.5
temp_in_C = temp_in_K - 273.15
print temp_in_C
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
In [4]:
type(kelvin_to_celsius)
Out[4]:
In [6]:
def fahr_to_kelvin(temp):
return ((temp - 32) * (5.0 / 9)) + 273.15
In [7]:
print fahr_to_kelvin(100)
In [8]:
print kelvin_to_celsius(fahr_to_kelvin(100))
In [9]:
temp = 120
print fahr_to_kelvin(temp)
print temp
In [10]:
import numpy
import matplotlib.pyplot
import glob
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]:
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)
In [30]:
help(detect_problem)
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]:
In [40]:
distance(to=(100,100), (10,0))
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()
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]:
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]:
In [65]:
def test_fence():
assert fence("oh", "my", "|") == "oh|my"
return True
In [66]:
test_fence()
Out[66]:
In [63]:
list("hello")
Out[63]:
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
In [74]:
def outer(string):
return (string[0], string[-1])
In [77]:
print outer('Hello')[0]
In [ ]: