In [1]:
import numpy
import matplotlib.pyplot
%matplotlib inline

In [2]:
data = numpy.loadtxt (fname = 'data/weather-01.csv', delimiter =',')

In [3]:
fig = matplotlib.pyplot.figure (figsize= (10.0, 3.0))

# create placeholders for plots 
subplot1 = fig.add_subplot (1, 3, 1)
subplot2 = fig.add_subplot (1, 3, 2)
subplot3 = fig.add_subplot (1, 3, 3)

subplot1.set_ylabel('average') 
subplot1.plot(numpy.mean(data, axis = 0))

subplot2.set_ylabel('max') 
subplot2.plot(numpy.max(data, axis = 0))

subplot3.set_ylabel('min') 
subplot3.plot(numpy.min(data, axis = 0))

fig.tight_layout()
matplotlib.pyplot.show


Out[3]:
<function matplotlib.pyplot.show>

LOOPS


In [4]:
word = 'notebook'

In [5]:
print(word[4])


b

In [6]:
for char in word: 
    print(char)


n
o
t
e
b
o
o
k

Get a list of all filenames from disk


In [7]:
import glob

In [8]:
print (glob.glob('data/weather*.csv')) # generates a list, single dimension container for data


['data\\weather-01.csv', 'data\\weather-02.csv', 'data\\weather-03.csv', 'data\\weather-04.csv', 'data\\weather-05.csv', 'data\\weather-06.csv', 'data\\weather-07.csv', 'data\\weather-08.csv', 'data\\weather-09.csv', 'data\\weather-10.csv', 'data\\weather-11.csv', 'data\\weather-12.csv']

Putting it all together


In [9]:
filenames = sorted(glob.glob('data/weather*.csv')) # sorted gets data sets in right order
#filenames = filenames[0:3]

for f in filenames: 
    print (f)
    
    data = numpy.loadtxt(fname=f, delimiter =',')
    if numpy.max (data, axis = 0)[0] and numpy.max(data, axis = 0)[20] ==20:
        print ("Suspicious looking maximum")
    elif numpy.sum(numpy.min(data, axis = 0)) ==0:
        print ("Minima to zero")
    else: 
        print ("Data looks OK")
    
    fig = matplotlib.pyplot.figure (figsize= (10.0, 3.0)) 
    subplot1 = fig.add_subplot (1, 3, 1)
    subplot2 = fig.add_subplot (1, 3, 2)
    subplot3 = fig.add_subplot (1, 3, 3)

    subplot1.set_ylabel('average') 
    subplot1.plot(numpy.mean(data, axis = 0))

    subplot2.set_ylabel('max') 
    subplot2.plot(numpy.max(data, axis = 0))

    subplot3.set_ylabel('min') 
    subplot3.plot(numpy.min(data, axis = 0))

    fig.tight_layout()
    matplotlib.pyplot.show()


data\weather-01.csv
Data looks OK
data\weather-02.csv
Data looks OK
data\weather-03.csv
Minima to zero
data\weather-04.csv
Data looks OK
data\weather-05.csv
Data looks OK
data\weather-06.csv
Data looks OK
data\weather-07.csv
Data looks OK
data\weather-08.csv
Minima to zero
data\weather-09.csv
Data looks OK
data\weather-10.csv
Data looks OK
data\weather-11.csv
Minima to zero
data\weather-12.csv
Data looks OK

Making decisions


In [10]:
num = 107 
if num > 100: 
    print("Greater")
    print("Done")
else: 
    print ("Not greater")
    print ("Done")


Greater
Done

In [11]:
num = -3
if num > 0: 
    print(num, "is positive")
elif num == 0: 
    print (num, "is zero")
else: 
    print (num, "is negative")


-3 is negative

In [12]:
num = 107 + 33 + 222
for inte in num:
    if num > 100: 
        print("Greater")
        print("Done")
    else: 
        print ("Not greater")
        print ("Done")


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-94f24eb96474> in <module>()
      1 num = 107 + 33 + 222
----> 2 for inte in num:
      3     if num > 100:
      4         print("Greater")
      5         print("Done")

TypeError: 'int' object is not iterable

Functions


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

In [14]:
fahr_to_kelvin(44)


Out[14]:
279.81666666666666

In [15]:
print('Freezing point of water: ', fahr_to_kelvin(32))


Freezing point of water:  273.15

In [16]:
print('Boiling point of water: ', fahr_to_kelvin(212))


Boiling point of water:  373.15

In [35]:
def analyse (filename): 
        """ Displays  the mean, maxima and minimum value for each weather station.
            Creates a figure of three subplots, showing values for mean, maxima and minimum value with axis = 0, 
            y axis labels, and a tight layout. 
        """
        data = numpy.loadtxt(fname=filename, delimiter =',')
        
        fig = matplotlib.pyplot.figure (figsize= (10.0, 3.0)) 
        subplot1 = fig.add_subplot (1, 3, 1)
        subplot2 = fig.add_subplot (1, 3, 2)
        subplot3 = fig.add_subplot (1, 3, 3)

        subplot1.set_ylabel('average') 
        subplot1.plot(numpy.mean(data, axis = 0))

        subplot2.set_ylabel('max') 
        subplot2.plot(numpy.max(data, axis = 0))

        subplot3.set_ylabel('min') 
        subplot3.plot(numpy.min(data, axis = 0))

        fig.tight_layout()
        matplotlib.pyplot.show()

In [36]:
def detect_problems (filename):
    """Some of our temperature files have problems, check for these
       This function reads a file(filename argument) and reports on odd looking maxima, 
       and minima that add up to zero. This seems to happen when the sensors break. 
       The function does not return any data. 
    """
    data = numpy.loadtxt(fname=filename, delimiter =',')
    
    if numpy.max (data, axis = 0)[0] and numpy.max(data, axis = 0)[20] ==20:
        print ("Suspicious looking maximum")
    elif numpy.sum(numpy.min(data, axis = 0)) ==0:
        print ("Minima to zero")
    else: 
        print ("Data looks OK")

In [26]:
for f in filenames [0:5]: 
    print(f)
    analyse(f)
    detect_problems(f)


data\weather-01.csv
Data looks OK
data\weather-02.csv
Data looks OK
data\weather-03.csv
Minima to zero
data\weather-04.csv
Data looks OK
data\weather-05.csv
Data looks OK

In [31]:
# how to we write help documentation for our own functions? 
help(detect_problems)


Help on function detect_problems in module __main__:

detect_problems(filename)


In [34]:
help(detect_problems)


Help on function detect_problems in module __main__:

detect_problems(filename)
    Some of our temperature files have problems, check for these
    This function reads a file(filename argument) and reports on odd looking maxima, 
    and minima that add up to zero. This seems to happen when the sensors break. 
    The function does not return any data.


In [37]:
help(analyse)


Help on function analyse in module __main__:

analyse(filename)
    Displays  the mean, maxima and minimum value for each weather station.
    Creates a figure of three subplots, showing values for mean, maxima and minimum value with axis = 0, 
    y axis labels, and a tight layout.


In [ ]:


In [ ]: