# Design Example # Exercise 2: Quantitative Analysis # For demonstration purposes only import matplotlib.pyplot as plt def read_measurements(filename): """Return a dictionary mapping column names to a tuple of all of the values in filename corresponding to that column name. Assumes the first line of the file is column names.""" datafile = open(filename) row_list = [row.split() for row in datafile] datafile.close() print "row_list=", row_list rawcolumns = zip(*row_list) print "rawcols=", rawcolumns columns = dict([(col[0], col[1:]) for col in rawcolumns]) print "read_measurements returns:", columns return columns def tofloat(measurements, columnname): """measurements is a dictionary columnname is a key in measurements Extract the specified column of values from the given measurements Returns a list of values in that column, converted to floats""" float_list = [float(x) for x in measurements[columnname]] print "tofloat(", columnname, ") returns:", float_list return float_list def STplot(measurements): """Plot salt vs. temp""" xs = tofloat(measurements, "salt") ys = tofloat(measurements, "temp") plt.plot(xs, ys) plt.xlabel("salt") plt.ylabel("temp") plt.show() def minimumO2(measurements): """Return the minimum value of the oxygen measurement""" return min(tofloat(measurements, "o2")) # Sample client calls: col_to_data_dict = read_measurements("quant.txt") STplot(col_to_data_dict) print "minimum o2 in file:", minimumO2(col_to_data_dict)