import numpy
1 + 3
numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
data
print data.min(), data.max()
print numpy.min(data), numpy.max(data)
data.median()
numpy.median(data)
x = 5
y = 3
print x
print y
print x + y
print x * y
print x - y
print x / y
type(x)
x = 5.0
y = 3.0
d
print x
print y
print x + y
print x * y
print x - y
print x / y
5.0 / 3.0
float(5) / float(3)
x = 5
y = 3
float(x)
x = 5
y = x * 2
print x, y
x = x * 10
print x, y
data
%matplotlib inline
import matplotlib.pyplot
data.shape
data[0]
data[1]
data[59]
data[60]
data[0]
data[0, 5]
matplotlib.pyplot.plot(data[0])
numpy.set_printoptions(linewidth=50)
data[0]
s = 'reclaim'
type(s)
for char in s:
print char
for char in s:
print char
print 'x'
print 'y'
range(5)
for i in range(5):
print i
for i in range(60):
patient = data[i]
print 'patient', i, 'min value', \
patient.min(), 'mean value', patient.mean()
for i in range(2):
patient = data[i]
matplotlib.pyplot.plot(patient)
data.min(axis=0)
data.min(axis=0).shape
f = matplotlib.pyplot.figure()
plot1 = f.add_subplot(2, 1, 1)
plot2 = f.add_subplot(2, 1, 2)
plot1.plot(data.mean(axis=0))
plot2.plot(data.max(axis=0))
plot1.set_ylabel('mean')
plot2.set_ylabel('maximum')
for i in range(3):
patient = data[i]
f = matplotlib.pyplot.figure()
mango = f.add_subplot(1, 1, 1)
# the next line actually does the plotting!
mango.plot(patient)
mango.set_title('patient ' + str(i))
print s[0], s[1], s[len(s)-1]
print s[-1], s[-2], s[-len(s)]
s[1:5]
5 - 1
len(s)
s = 'abcdefghijkl'
s[0:5]
s[0:5:2]
s[::]
s[1:-1]
# Print the string reverse, e.g.'lkjihgfedcba'
# Hint: the "step" can also be negative
s[::-1]
s[-1::-1]
s[0:len(s):1]
data
data[3:5, 1:4]
data[1:-1, 1:-1]
data[1:-1, 1:-1].shape
data[:3, :3]
data[0]
data[:, 0]
# Print out the average for all 40 time points (columns)
# E.g.
# day 0: 0.0
# day 1: 1.2
# ...
# day 39: 0.2
range(40)
for i in range(5):
print 'day', i, ':', data[:, i].mean()
range(5)
[0, 1, 2, 3, 4]
[0, 1, 'string', 4.5, numpy]
x = range(5)
x[0]
x[-1]
x[::2]
x[::-1]
x = 5
x = 6
x
y = x
y
x = 11
L = range(5)
L
L[1] = 'x'
L
L.append(5)
L
L2 = L
print L, L2
L[3] = 11
print L, L2
s
s[1] = 'x'
import glob
glob.glob('data/small-*.csv')
files = glob.glob('data/small-*.csv')
files[0]
# opening files:
# numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
# loops:
# for fname in files:
# ...
# glob
# data.shape
# Print the size (the shape) of each file
# from data/inflammation*.csv
files = glob.glob('data/inf*csv')
files
for fname in files:
data = numpy.loadtxt(fname=fname, delimiter=',')
print fname, data.shape
data
matplotlib.pyplot.imshow(data)
len(12)
len(files)
# Plot 12 colormaps for 12 files
matplotlib.pyplot.imshow(data)
f = matplotlib.pyplot.figure()
for i in range(3):
for j in range(4):
f.add_subplot(3, 4, i*4 + j)
datas = []
for fname in files:
data = numpy.loadtxt(fname=fname, delimiter=',')
# print fname, data.shape
datas.append(data)
len(datas)
f = matplotlib.pyplot.figure()
for i in range(3):
for j in range(4):
num = i*4 + j
subplot = f.add_subplot(3, 4, 1 + num)
subplot.set_title(str(num))
data = datas[num]
subplot.imshow(data)
f.tight_layout()
def fahr_to_celcius(temp):
return (temp - 32) * float(5)/9
fahr_to_celcius(0)
fahr_to_celcius(98.)
fahr_to_celcius(98)
def celcius_to_kelvin(temp):
return 273 + temp
celcius_to_kelvin(100)
def fahr_to_kelvin(temp):
x = fahr_to_celcius(temp)
return celcius_to_kelvin(x)
fahr_to_kelvin(98)
def plot_data(subplot, plot_number, data):
print subplot, plot_number, data.shape
subplot.imshow(data)
subplot.set_title(str(plot_number))
subplot.set_ylabel('patient')
subplot.set_xlabel('time')
f = matplotlib.pyplot.figure()
for i in range(3):
for j in range(4):
num = i*4 + j
subplot = f.add_subplot(3, 4, 1 + num)
data = datas[num]
plot_data(subplot, num, data)
f.tight_layout()
def plot_many_patients(datas):
f = matplotlib.pyplot.figure()
for i in range(3):
for j in range(4):
num = i*4 + j
subplot = f.add_subplot(3, 4, 1 + num)
data = datas[num]
plot_data(subplot, num, data)
f.tight_layout()
plot_many_patients(datas)
def f(x, y, z):
print x, y, z
f(x=1, z=3, y=2, )
def f(x, y, z=5):
print x, y, z
f(1, 2, 3)
f(x=1, y=2, z=3)
f(1, 2)
x = 5
if x == 5:
print 'x is 5'
else:
print 'x is not 5'
x = 6
if x == 5:
print 'x is 5'
# Write a function which prints the min, max, and median
# of data
# if requested
#
def print_info(data, show_min=1, show_max=1, show_median=1):
print '?'
print_info(data, 1, 1, 0)
def print_info(data, show_min=1, show_max=1, show_median=1):
if show_min == 1:
print 'min', data.min()
if show_max == 1:
print 'max', data.max()
if show_median == 1:
print 'median', numpy.median(data)
print_info(data, 1, 1, 0)
print_info(data, 0, 0, 0)
print_info(data, show_min=0, show_max=0, show_median=1)
if x:
print 'x is true'
print bool(0), bool(1)
print bool(0.0), bool(0.1)
L
bool(L)
print bool([1]), bool([])
bool([0])
bool(True)
bool(False)
bool(None)
def print_info(data, show_min=True, show_max=True,
show_median=True):
if show_min:
print 'min', data.min()
if show_max:
print 'max', data.max()
if show_median:
print 'median', numpy.median(data)
# Things that are false:
# 1. zeros and False
# 2. empty lists and other sequences
# 3. None