We now have almost everything we need to process our data files, only thing missing is a library to grab files
In [1]:
import glob
import numpy
import matplotlib.pyplot
*
matches 0+ characters; ?
matches any one char
In [3]:
print(glob.glob('data/inflammation*.csv'))
In [2]:
%matplotlib inline
In [5]:
# loop here
counter = 0
for filename in glob.glob('data/*.csv'):
#counter+=1
counter = counter + 1
print("number of files:", counter)
In [7]:
counter = 0
for filename in glob.glob('data/infl*.csv'):
#counter+=1
counter = counter + 1
print("number of files:", counter)
In [8]:
counter = 0
for filename in glob.glob('data/infl*.csv'):
#counter+=1
data = numpy.loadtxt(fname=filename, delimiter=',')
print(filename, "mean is: ", data.mean())
counter = counter + 1
print("number of files:", counter)
In [ ]:
%matplotlib inline
In [4]:
counter = 0
for filename in glob.glob('data/infl*.csv')[0:3]:
counter+=1
print(filename)
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)
print("number of files:", counter)
We can ask Python to take different actions, depending on a condition, with an if statement:
In [6]:
#We use an if statement to take different actions
#based on conditions
num = 37
if num > 100:
print('greater')
else:
print('not greater')
print('done')
if
to denote choiceif
is true, the body of the if
are executedelse
is executedelse
- if not present python does nothing
In [7]:
num = 53
print('before conditional...')
if num > 100:
print('53 is greater than 100')
print('...after conditional')
we can also chain several tests together using elif
, short for else if
In [8]:
num = -3
if num > 0:
print(num, "is positive")
elif num == 0:
print(num, "is zero")
else:
print(num, "is negative")
==
to test for equality rather than single equal b/c the later is the assignment operatorand
and or
. and
is only true if both parts are true
In [9]:
if (1 > 0) and (-1 > 0):
print('both parts are true')
else:
print('at least one part is false')
while or
is true if at least one part is true:
In [10]:
if (1 < 0) or (-1 < 0):
print('at least one test is true')
In [1]:
if 4 > 5:
print('A')
elif 4 == 5:
print('B')
elif 4 < 5:
print('C')
In [12]:
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print('Suspicious looking maxima!')
In [ ]:
elif data.min(axis=0).sum() == 0:
print('Minima add up to zero!')
else
for all clear
In [ ]:
else:
print('seems OK!')
let's test!
In [16]:
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')
In [17]:
data = numpy.loadtxt(fname='data/inflammation-03.csv', delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')
asked python to do something depending on diff conditions; we can also imaging not using the else
so messages are only printed whens omething is wrong
True and False are special words in Python called booleans which represent true and false statements. However, they aren’t the only values in Python that are true and false. In fact, any value can be used in an if or elif. After reading and running the code below, explain what the rule is for which values are considered true and which are considered false.
if '':
print('empty string is true')
if 'word':
print('word is true')
if []:
print('empty list is true')
if [1, 2, 3]:
print('non-empty list is true')
if 0:
print('zero is true')
if 1:
print('one is true')
In [3]:
if '':
print('empty string is true')
if 'word':
print('word is true')
if []:
print('empty list is true')
if [1, 2, 3]:
print('non-empty list is true')
if 0:
print('zero is true')
if 1:
print('one is true')
In [ ]: