Changes: * Confirm version python --version * Do some easy but powerful things first, then learn more of the details about python programming. python is a programming language. But part of what makes it so powerful is all of the extra features that have been added to it by the "python community" These extra features are called 'modules'. So, first we are going to learn about numpy and matplotlib modules. * First, some review of shell and take a look at the files - cd data - ls - ls -1 - cat inflammation-01.csv - What if we wanted to know how many rows and how many columns? - a 'row' is a 'line' in the file...what is a shell function we learned recently that would tell us how many lines? - wc -l inflammation-01.csv - head -n 1 inflammation-01.csv - head -n 1 inflammation-01.csv | tr -d '0123456789' | wc -c - am using an new shell function, 'tr' (translate) that with -d (delete) option. All that is left is commas. - head -n 1 inflammation-01.csv | tr -d '0-9' | wc -c - each row represents a patient, each column one day of that patient's values * now, start python3 Loading and printing data * import numpy - like getting a piece of lab equipment and setting it up on the lab bench - will use a function in numpy module (loadtxt) to read the data in this file into a numpy object * help() all help on python * help(numpy) * help(numpy.loadtxt) * numpy.loadtxt(fname='inflammation-01.csv',delimiter=',') - all this did was read the contents of the file and print it. - We have to tell python to put that data somewhere so it will remember (memory) it for us when we want to access it - what would we do if the file was space delimited? - what would we do if the file was in a sub-directory named foo? * data = numpy.loadtxt(fname='inflammation-01.csv',delimiter=',') - reading x = 2 ==> x takes the value 2 - read the equal sign as 'takes the value' - Show command-line editing with arrow keys and ctrl-a, ctrl-e * print(data) - note, default action for python to take is to 'print' so can save a lot of typing by just typing the object's name * data What is an array? * type(data) * print numpy.shape(data) - remark...agrees with what we learned from shell commands, above * numpy.shape(data) * print(data.shape) * data.shape * data[0] * data[1] * data[0:3] * data[0][0] and data[0,0] Using numpy on the array: * patient_0 = data[0, :] - patient_0 = data[0] * numpy.mean(patient_0) * numpy.mean(data[2]) * numpy.mean(data[5:11] * numpy.mean(data) * numpy.mean(data, axis=0) - what do we do if we wanna store/remember the result of this operation Plotting * import matplotlib.pyplot as pyp * help(matplotlib.pyplot) fails * help(pyp) * image = pyp.imshow(data) * pyp.show(image) * plot = pyp.plot(patient_0) * pyp.show(plot) Analyzing data from multiple files * import os * os.listdir() # ls * os.getcwd() # pwd * os.chdir() # cd * * import glob * help(glob) * filenames = glob.glob('inflammation*') * filenames = filenames[0:3] * cut-n-paste code Ex: plot difference in first two datasets average Ex: Generate three plots for *all* patients in all files Making Choices: * num = 37 if num > 100: print('greater') else: print('NOT greater') print('done') - don't need an 'else:' part * num = -3 if num > 0: print(num, 'is positive') elif num == 0: print(num, 'is zero') else: print(num, 'is negative') - note the double equals (==) * if (1>0) and (-1 > 0): print('both are true') else: print('at least one is false') Sanity checking data * if numpy.max(data, axis=0)[0] == 0 and ... elif numpy.sum(data, axis=0) == 0): else: * test with file -01 and file -03 * What is True/False * using not Creating Functions * def fahr_to_kelvin(temp): return ((temp-32)*(5/9)) + 273.15 print('freezing point of water:', fahr_to_kelvin(32)) print('boiling point of water:', fahr_to_kelvin(212)) * Integer Division - note 5/9 in python 2 (do this in python 2, do float(5)/9 - what if you want to do integer division? - 5//9 * composing functions def kelvin_to_celsius(temp_k): return temp_k - 273.15 create a function to convert fahr to celsius * function to analyze and sanity_check but using nparr as argument * document the functions Ex: fence variables inside and outside functions old switch aroo Errors and Exceptions: * syntax errors (spacing) * variable name errors) - function name errors * index errors * file errors Ex: Reading Error Messages Command Line Arguments * Want to create "programs" that can be used like any other shell command * command to print version * command to list arguments * cat ../code/readings_01.py - run it and it does nothing * python ../code/readings_02.py inflammation-01.csv * importing vs. running * use small data inputs to sanity check cat small-01.csv python ../code/reading_02.py small-01.csv * cat ../code/readings_03.py * adding command-line flags * make it runnable by its name alone #!/usr/bin/env python for me.../Users/miller86/anaconda3/bin/python3