# Design Example # Quantitative Analysis: Version 2 # For demonstration purposes only import matplotlib.pyplot as plt def read_measurements(filename): """ Returns 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 raw_columns = zip(*row_list) # raw_columns = [] # for i in range(len(row_list[0])): # raw_columns.append([]) # # for row in row_list: # for i in range(len(row)): # raw_columns[i].append(row[i]) # # for i in range(len(row)): # raw_columns[i] = tuple(raw_columns[i]) print "raw_columns:", raw_columns columns = dict([(col[0], col[1:]) for col in raw_columns]) # columns = {} # for col in raw_columns: # columns[col[0]] = col[1:] # 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]] # float_list = [] # for x in measurements[columnname]: # float_list.append(float(x)) # print "tofloat(", columnname, ") returns:", float_list return float_list def STplot(measurements): """ Given a dictionary mapping column names to a tuple of values in that column, plots 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): """ Returns the minimum value of the oxygen measurement. """ return min(tofloat(measurements, "o2")) # Sample client calls: col_to_data_dict = read_measurements("sample_quant.txt") STplot(col_to_data_dict) print "minimum o2 in file:", minimumO2(col_to_data_dict)