We use Python for this:
Use what your colleagues (tend to) use
To analyse and visualise experimental data
Tabular (comma-separated) data
We can do this with a little programming
Before we begin…
cd ~/Desktop
cd python-novice-inflammationLIVE DEMO
JupyterAt the command-line, start Jupyter notebook:
jupyter notebookJupyter landing page
\[y = f(x)\]
\(y\) is the returned value, or output(s)
Not all functions in code take an input, or produce a usable output, but the principle is generally the same.
fahr_to_kelvin() to convert Fahrenheit to Kelvin\[f(x) = ((x - 32) \times \frac{5}{9}) + 273.15\]
LIVE DEMO
fahr_to_kelvin() is the same as calling any other functionprint('freezing point of water:', fahr_to_kelvin(32))
print('boiling point of water:', fahr_to_kelvin(212))LIVE DEMO
Python functions works like mathematical functions: \(y = f(g(x))\)kelvin_to_celsius()temp_f) to C (temp_c) by executing the code:temp_c = kelvin_to_celsius(fahr_to_kelvin(temp_f))LIVE DEMO
fahr_to_celsius:def fahr_to_celsius(temp_f):
return kelvin_to_celsius(fahr_to_kelvin(temp_f))
print('freezing point of water in Celsius:', fahr_to_celsius(32.0))LIVE DEMO
Can you write a function called outer() that:
string argumentprint(outer("helium"))
hma = "Hello"
def my_fn(a):
a = "Goodbye"
my_fn(a)
print(a)LIVE DEMO
What would be printed if you ran the code below?
a = 3
b = 7
def swap(a, b):
temp = a
a = b
b = temp
swap(a, b)
print(b, a)7 33 73 37 7%pylab inline
import matplotlib.pyplot
import numpy as np
import os
import seabornanalyse()analyse() that plots the datadef analyze(data):
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(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()LIVE DEMO
detect_problems()def detect_problems(data):
if numpy.max(data, axis=0)[0] == 0 and numpy.max(data, axis=0)[20] == 20:
print('Suspicious looking maxima!')
elif numpy.sum(numpy.min(data, axis=0)) == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')LIVE DEMO
analyse() and detect_problems()filenames = [os.path.join('data', f) for f in os.listdir('data')
if f.startswith('inflammation')]
for fname in filenames:
data = np.loadtxt(fname, delimiter=",")
print(fname)
analyse(data)
detect_problems(data) LIVE DEMO
def centre(data, desired):
return (data - np.mean(data)) + desiredLIVE DEMO
centre() on real data
numpy to create an artificial datasetz = np.zeros((2, 2))
print(centre(z, 3.0))LIVE DEMO
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
print(centre(data, 0))LIVE DEMO
mean, min, max, stdcentred = centre(data, 0)
print('original min, mean, and max are:', numpy.min(data), numpy.mean(data), numpy.max(data))
print('min, mean, and max of centered data are:', numpy.min(centred),
numpy.mean(centred), numpy.max(centred))
print('std dev before and after:', numpy.std(data), numpy.std(centred)) LIVE DEMO
#) is a good thingPython provides for docstrings
Python’s help systemdef centre(data, desired):
"""Returns the array in data, recentered around the desired value."""
return (data - numpy.mean(data)) + desired
help(centre)LIVE DEMO
centre() function requires two argumentsdef centre(data, desired=0.0):
"""Returns the array in data, recentered around the desired value.
Example: centre([1, 2, 3], 0) => [-1, 0, 1]
"""
return (data - np.mean(data)) + desired
centre(data, 0.0)
centre(data, desired=0.0)
centre(data)LIVE DEMO
Can you write a function called rescale() that
L and H are the lowest and highest values in the original array, then the replacement for a value v should be (v-L) / (H-L).
Python tries to tell you what has gone wrong by providing a tracebackdef favourite_ice_cream():
ice_creams = [
"chocolate",
"vanilla",
"strawberry"
]
print(ice_creams[3])
favourite_ice_cream()LIVE DEMO
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-1-b0e1f9b712d6> in <module>()
8 print(ice_creams[3])
9
---> 10 favourite_ice_cream()
<ipython-input-1-b0e1f9b712d6> in favourite_ice_cream()
6 "strawberry"
7 ]
----> 8 print(ice_creams[3])
9
10 favourite_ice_cream()
IndexError: list index out of rangeLIVE DEMO
Pythondef some_function()
msg = "hello, world!"
print(msg)
return msgLIVE DEMO
File "<ipython-input-3-dbf32ad5d3e8>", line 1
def some_function()
^
SyntaxError: invalid syntaxLIVE DEMO
def some_function():
msg = "hello, world!"
print(msg)
return msgLIVE DEMO
File "<ipython-input-4-e169556d667b>", line 4
return msg
^
IndentationError: unexpected indentLIVE DEMO
NameErrors occur when a variable is not defined in scopeprint(a)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-c5a4f3535135> in <module>()
----> 1 print(a)
NameError: name 'a' is not definedLIVE DEMO
IndexErrorletters = ['a', 'b', 'c']
print("Letter #1 is", letters[0])
print("Letter #2 is", letters[1])
print("Letter #3 is", letters[2])
print("Letter #4 is", letters[3])
Letter #1 is a
Letter #2 is b
Letter #3 is c
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-656a22fa6ec5> in <module>()
3 print("Letter #2 is", letters[1])
4 print("Letter #3 is", letters[2])
----> 5 print("Letter #4 is", letters[3])
IndexError: list index out of rangeLIVE DEMO
abbabbabba?for number in range(10):
# use a if the number is a multiple of 3, otherwise use b
if (Number % 3) == 0:
message = message + a
else:
message = message + "b"
print(message)
Pythonic way to see if code runs correctly
Firefox source code is assertions!assert that a condition is True
True, the code may be correctFalse, the code is not correctassert <condition>, "Some text describing the problem"numbers = [1.5, 2.3, 0.7, -0.001, 4.4]
total = 0.0
for n in numbers:
assert n > 0.0, 'Data should only contain positive values'
total += n
print('total is:', total)QUESTION: What does this assertion do?