More on Functions, Modules Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.

Slides:



Advertisements
Similar presentations
Genome 559: Introduction to Statistical and Computational Genomics
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Functions Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Functions as Arguments, Sorting Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Lists Introduction to Computing Science and Programming I.
Example 2.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Main task -write me a program
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Lists in Python.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
General Programming Introduction to Computing Science and Programming I.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python Functions.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Decision Structures, String Comparison, Nested Structures
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Python Programing: An Introduction to Computer Science
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Algorithmic complexity: Speed of algorithms
Containers and Lists CIS 40 – Introduction to Programming in Python
The Selection Structure
Functions CIS 40 – Introduction to Programming in Python
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
CISC101 Reminders Slides have changed from those posted last night…
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Algorithmic complexity: Speed of algorithms
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
G. Pullaiah College of Engineering and Technology
CISC101 Reminders All assignments are now posted.
Algorithmic complexity: Speed of algorithms
Searching and Sorting (2)
CISC101 Reminders Assignment 3 due today.
While Loops in Python.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Functions John R. Woodward.
More on Functions, Modules
Presentation transcript:

More on Functions, Modules Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

A quick review  Functions:  Reusable pieces of code (write once, use many times)  Take arguments, “do stuff”, and (usually) return a value  Use to organize & clarify your code, reduce code duplication  Functions have their own namespace  Local variables inside the function are invisible outside  Arguments can be of any type!  Number and strings  Lists and dictionaries  Other functions

A quick review – cont’  Sorting:  Built-in function for lists  Defaults:  Strings: ascending order, capital letters before small letters  Numbers: ascending order  The sort() function allows us to define how comparisons are performed!  A comparison function:  Always takes 2 arguments  Returns: -1 if first argument should appear earlier in sort, 1 if first argument should appear later, 0 if they are tied def myComparison(a, b): if a > b: return -1 elif a < b: return 1 else: return 0

Returning values  Check the following function:  What does this function do? # This function … # … def CalcSum(a_list): sum = 0 for item in a_list: sum += item return sum

Returning values >>> my_list = [1, 3, 2, 9] >>> print CalcSum(my_list) 15  Check the following function:  What does this function do? # This function calculates the sum # of all the elements in a list def CalcSum(a_list): sum = 0 for item in a_list: sum += item return sum

Returning more than one value  Let’s be more ambitious:  How can we return both values? # This function calculates the sum # AND the product of all the # elements in a list def CalcSumAndProd(a_list): sum = 0 prod = 1 for item in a_list: sum += item prod *= item return ???

Returning more than one value  We can use a list as a return value: # This function calculates the sum # AND the product of all the # elements in a list def CalcSumAndProd(a_list): sum = 0 prod = 1 for item in a_list: sum += item prod *= item return [sum, prod] >>> my_list = [1, 3, 2, 9] >>> print CalcSumAndProd(my_list) [15, 54] >>> res = CalcSumAndProd(my_list) >>> [s,p] = CalcSumAndProd(my_list) List assignment multiple assignment

Returning lists  Recall the increment function:  Is this good practice? # This function increment every element in # the input list by 1 def incrementEachElement(a_list): new_list = [] for item in a_list: new_list.append(item+1) return new_list # Now, create a list and use the function my_list = [1, 20, 34, 8] print my_list my_list = incrementEachElement(my_list) Print my_list [1, 20, 34, 8] [2, 21, 35, 9]

Returning lists  What will happen if we do this?  (note: no return value!!!) # This function increment every element in # the input list by 1 def incrementEachElement(a_list): for index in range(len(a_list)): a_list[index] +=1 # Now, create a list and use the function my_list = [1, 20, 34, 8] incrementEachElement(my_list) print my_list

Returning lists # This function increment every element in # the input list by 1 def incrementEachElement(a_list): for index in range(len(a_list)): a_list[index] +=1 # Now, create a list and use the function my_list = [1, 20, 34, 8] incrementEachElement(my_list) print my_list [2, 21, 35, 9]  What will happen if we do this?  (note: no return value) WHY IS THIS WORKING?

Pass-by-reference vs. pass-by-value  Two fundamentally different function calling strategies:  Pass-by-Value:  The value of the argument is copied into a local variable inside the function  C, Scheme, C++  Pass-by-reference:  The function receives an implicit reference to the variable used as argument, rather than a copy of its value  Perl, VB, C++  So, how does Python pass arguments?

Python passes arguments by reference (almost)  So … this will work! # This function increment every element in # the input list by 1 def incrementEachElement(a_list): for index in range(len(a_list)): a_list[index] +=1 >>> my_list = [1, 20, 34, 8] >>> incrementEachElement(my_list) >>> my_list [2, 21, 35, 9] >>> incrementEachElement(my_list) >>> my_list [3, 22, 36, 10]

Python passes arguments by reference (almost)  How about this? def addQuestionMark(word): print “word inside function (1):”, word word = word + “?” print “word inside function (2):”, word my_word = “really” addQuestionMark(my_word) print “word after function:”, my_word

Python passes arguments by reference (almost)  How about this?  Remember: 1.Strings/numbers are immutable 2.The assignment command often creates a new object def addQuestionMark(word): print “word inside function (1):”, word word = word + “?” print “word inside function (2):”, word my_word = “really” addQuestionMark(my_word) print “word after function:”, my_word word inside function (1): really word inside function (2): really? word after function: really

Passing by reference: the bottom line  You can (and should) use this option when:  Handling large data structures  “In place” changes make sense  Be careful (a double-edged sword) :  Don’t lose the reference!  Don’t change an argument by mistake  When we learn about objects and methods we will see yet an additional way to change variables

Required Arguments  How about this? def printMulti(text, n): for i in range(n): print text >>> printMulti(“Bla”,4) Bla Traceback (most recent call last): File " ", line 1, in TypeError: printMulti() takes exactly 2 arguments (1 given) >>> printMulti("Bla")  What happens if I try to do this:

Default Arguments  Python allows you to define defaults for various arguments: def printMulti(text, n=3): for i in range(n): print text >>> printMulti(“Bla”,4) Bla >>> printMulti(“Yada”) Yada

Default Arguments  This is very useful if you have functions with numerous arguments/parameters, most of which will rarely be changed by the user:  You can now simply use:  Instead of: def runBlast(fasta_file, costGap=10, E=10.0, desc=100, max_align=25, matrix=“BLOSUM62”, sim=0.7, corr=True): >>> runBlast(“my_fasta.txt”) >>> runBlast(“my_fasta.txt”,10,10.0,100,25,“BLOSUM62”,0.7, True)

Keyword Arguments  You can still provide values for specific arguments using their label: def runBlast(fasta_file, costGap=10, E=10.0, desc=100, max_align=25, matrix=“BLOSUM62”, sim=0.7, corr=True): … >>> runBlast(“my_fasta.txt”, matrix=“PAM40”)

Variable-length arguments  You may want to allow a function to process more arguments than you specified while defining it (can you think of an example?)  These arguments are called variable-length arguments and are not named in the function definition. def printinfo( arg1, *vartuple ): print "Output is: " print arg1 for var in vartuple: print var return # Now you can call printinfo function printinfo( 10 ) printinfo( 70, 60, 50 )

TIP OF THE DAY Code like a pro … Write comments!

TIP OF THE DAY Why comments  Uncommented code = useless code  Comments are your way to communicate with:  Future you!  The poor bastard that inherits your code  Your users (most academic code is open source!)  At minimum, write a comment to explain:  Each function: target, arguments, return value  Each File: purpose, major revisions  Non-trivial code blocks  Non-trivial variables  Whatever you want future you to remember

Best (real) comments ever # When I wrote this, only God and I understood what I was doing # Now, God only knows # I dedicate all this code, all my work, to my wife, Darlene, # who will have to support me and our three children and the # dog once it gets released into the public. # drunk. fix later # I am not sure if we need this, but too scared to delete. # Magic. Do not touch. # I am not responsible of this code. # They made me write it, against my will. # Dear future me. Please forgive me. # I can't even begin to express how sorry I am. # no comments for you! # it was hard to write so it should be hard to read # somedev1 - 6/7/02 Adding temporary tracking of Logic screen # somedev2 - 5/22/07 Temporary my ass

Modules

 Recall your makeDict function:  This is in fact a very useful function which you may want to use in many programs!  So are other functions you wrote (e.g., makeMatrix) def makeDict(fileName): myFile = open(fileName, "r") myDict = {} for line in myFile: fields = line.strip().split("\t") myDict[fields[0]] = float(fields[1]) myFile.close() return myDict

Modules  A module is a file that contains a collection of related functions.  You have already used several built-in modules:  e.g.: sys, math  Python has numerous standard modules  Python Standard Library: (  It is easy to create and use your own modules:  JUST PUT YOUR FUNCTIONS IN A SEPARATE FILE!

Importing Modules  To use a module, you first have to import it into your namespace  To import the entire module: import module_name # This function makes a dictionary def makeDict(fileName): myFile = open(fileName, "r") myDict = {} for line in myFile: fields = line.strip().split("\t") myDict[fields[0]] = float(fields[1]) myFile.close() return myDict # This function reads a 2D matrix def makeMatrix(fileName): utils.py import utils import sys Dict1 = utils.makeDict(sys.argv[1]) Dict2 = utils.makeDict(sys.argv[2]) Mtrx = utils.makeMatrix(“blsm.txt”) … my_prog.py

The dot notation  Why did we use utils.makeDict() instead of just makeDict() ?  Dot notation allows the Python interpreter to organize and divide the namespace  If you intend to use a function often you can assign it to a local name:  makeDict = utils.makeDict  Log = math.log

Importing functions  To import specific functions: from module_name import function_name  To import all the functions in a module: from module_name import *  If functions (rather than modules) are imported, there is no need to use the dot notation  Be careful when using the import * command. It can easily lead to namespace conflicts.

Sample problem #1  Write a function that calculates the first n elements of the Fibonacci sequence.  Reminder: In the Fibonacci sequence of numbers, each number is the sum of the previous two numbers, starting with 0 and 1. This sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, …  The function should return these n elements as a list

# Calculate Fibonacci series up to n def fibonacci(n): fib_seq = [0, 1]; for i in range(2,n): fib_seq.append(fib_seq[i-1] + fib_seq[i-2]) return fib_seq[0:n]# Why not just fib_seq? print fibonacci(10) Solution #1 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Sample problem #2  Make the following improvements to your function: 1.Add two optional arguments that will denote alternative starting values (instead of 0 and 1).  fibonacci(10)  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]  fibonacci(10,4)  [4, 1, 5, 6, 11, 17, 28, 45, 73, 118]  fibonacci(10,4,7)  [4, 7, 11, 18, 29, 47, 76, 123, 199, 322] 2.Return, in addition to the sequence, also the ratio of the last two elements you calculated (how would you return it?). 3.Create a module “my_math” and include your function in this module. Import this module into another program and use the function.

Solution #2 # Calculate Fibonacci series up to n def fibonacci(n, start1=0, start2=1): fib_seq = [start1, start2]; for i in range(2,n): fib_seq.append(fib_seq[i-1]+fib_seq[i-2]) ratio = float(fib_seq[n-1])/float(fib_seq[n-2]) return [fib_seq[0:n], ratio] my_math.py import my_math seq, ratio = my_math.fibonacci(1000) print "first 10 elements:",seq[0:10] print "ratio:", ratio # Will print: # first 10 elements: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] # ratio: fib = my_math.fibonacci # creating a local name print fib(5,12,14) # Will print: # [[12, 14, 26, 40, 66], 1.65] my_prog.py

Challenge problem  Write your own sort function!  Sort elements in ascending order.  The function should sort the input list in-place (i.e. do not return a new sorted list as a return value; the list that is passed to the function should itself be sorted after the function is called).  As a return value, the function should return the number of elements that were in their appropriate (“sorted”) location in the original list.  You can use any sorting algorithm. Don’t worry about efficiency right now.

Challenge solution 1 This is the actual sorting algorithm. Simple! def swap(a_list, k, l): temp = a_list[k] a_list[k] = a_list[l] a_list[l] = temp def bubbleSort(a_list): n = len(a_list) a_list_copy = [] # note: why don't we use assignment for item in a_list: a_list_copy.append(item) # bubble sort for i in range(n): for j in range(n-1): if a_list[j] > a_list[j+1]: swap(a_list, j, j+1) # note: in place swapping # check how many are in the right place count = 0 for i in range(n): if a_list[i] == a_list_copy[i]: count += 1 return count >>> ls = [1, 3, 2, 15, 7, 4, 8, 12] >>> print bubbleSort(ls) 2 >>> print ls [1, 2, 3, 4, 7, 8, 12, 15]

Alternative challenge solution 1 Why is this better? Why is this working? def swap(a_list, k, l): temp = a_list[k] a_list[k] = a_list[l] a_list[l] = temp def bubbleSort(a_list): n = len(a_list) a_list_copy = [] # note: why don't we use assignment for item in a_list: a_list_copy.append(item) # bubble sort for i in range(n): for j in range(n-1-i): if a_list[j] > a_list[j+1]: swap(a_list, j, j+1) # note: in place swapping # check how many are in the right place count = 0 for i in range(n): if a_list[i] == a_list_copy[i]: count += 1 return count >>> ls = [1, 3, 2, 15, 7, 4, 8, 12] >>> print bubbleSort(ls) 2 >>> print ls [1, 2, 3, 4, 7, 8, 12, 15]