Modeling and Optimizing Biological Systems: Introduction to Objects in Programming
In this course at Tufts University, explore the concepts of objects in programming and their significance in modeling, simulating, and optimizing biological systems. Discover how objects enable the treatment of diverse data as unified entities, enhancing code organization and teamwork efficiency in large projects. Learn about object-oriented programming and its essential role in software development.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
EE 194/BIO 196: Modeling,simulating and optimizing biological systems Spring 2018 Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu Objects 1
Why do we care about objects We ve learned about variables Variables are great, but there s only one of each We ve learned about arrays Arrays are great, but each member of an array has the same data type and is, conceptually, an instance of the same thing. Often we want to treat a group of dissimilar things as one big group. Example: A student has name, UTLN, year, grades, but it's still one student and there are many students. We have no good way to do that so far EE 194/Bio 196 Joel Grodstein 2
More examples? What other examples can you think of? An item in a store has price, bar code, shelf location, weight A Manduca sexta might have one 2D array for its leg-locked pattern and another for its muscle-activation pattern and a variable for its name, perhaps EE 194/Bio 196 Joel Grodstein 3
Demo Demo using Lunchables to introduce objects. EE 194/Bio 196 Joel Grodstein 4
Impressive terminology Other courses will take this idea much further: A student, or a lunch, or a hornworm will be an object. Nobody has to know what s inside of it. You just know what you can do to it; you can make() your lunch, eat() it, or trade() it. We re taking the first steps in object-oriented programming. EE 194/Bio 196 Joel Grodstein 5
Objects Computer programs can get big. Really big. Mac OS/X is about 85 million of lines of code Nobody can do that alone; big projects have big teams Big teams of people don t really help if everyone has to understand all 85M lines of code. We ve talked about using functions and packages to divide up the work Objects are the next big tool divide up the work. One team of people implements a Manduca object like what you ll see in the HW. It can be created, mate and mutate. It can be printed and simulated. Another team of people uses Manduca. E.g., put together a genetic algorithm that uses the abilities just described. They don t have to know how Manduca is implemented They need only know what its capabilities are Object-oriented programming is a Big Deal in Computer Science. EE 194/Bio 196 Joel Grodstein 6
Objects Let s start writing code for a Manduca object says that the rest of this code will define a class of object called a Manduca says what leg and muscle patterns go into the new Manduca class Manduca: def __init__(self, legs, muscles): self.legs=np.copy(legs) self.muscles=np.copy(muscles) self.name = "Bob" this function tells how to make a new Manduca copy/save the parameters into this Manduca says which of the many Manducas to work with Fine, all worms have the same name! EE 194/Bio 196 Joel Grodstein 7
The basics my_legs = np.array ([[1,1,0,0,0], ]) my_musc = np.array ([[100,0,0,100], ]) m1 = Manduca (my_legs, my_musc) def __init__(self, legs, muscles): self.legs=np.copy(legs) self.muscles=np.copy(muscles) self.name= "Bob" legs my_musc my_legs m1 muscles name [100,0, ] [1,1,0, ] [1,1,0, ] self "Bob" [100,0, ] EE 194/Bio 196 Joel Grodstein 8
Next steps Great, we can make a Manduca. In fact, we can make as many of them as we want: my_legs = np.array ([[1,1,0,0,0], ]) my_musc = np.array ([[100,0,0,100], ] m1 = Manduca (my_legs, my_musc) my_legs[3,:]=[1,0,0,0,1]; m2 = manduca (my_legs, my_musc) But what else can we do with them? Print them out EE 194/Bio 196 Joel Grodstein 9
Printing an object class Manduca: def __init__(self, legs, muscles, name): def __repr__(self): str = self.name + " legs | muscles\n" for r in range(10): # for each row str+= np.array2string (self.legs[r,:]) + " | " + \ np.array2string (self.muscles[r,:]) + "\n" return(str) __repr__() is a special function that Python calls whenever you print an object. It lets you control how an object gets printed. EE 194/Bio 196 Joel Grodstein 10
Printing example m1 = Manduca (my_legs, my_musc) my_legs[0,:]=[1,0,0,0,1] m2 = manduca (my_legs, my_musc) print (m1) print (m2) legs muscles [1 1 0 0 0] | [100 0 0 100] the other 9 rows [1 0 0 0 1] | [100 0 0 100] the other 9 rows legs muscles EE 194/Bio 196 Joel Grodstein 11
What else can you do with a worm? Mutate it class Manduca: def __init__(self, legs, muscles): def __repr__(self): def mutate (self): self.legs[3,3] = 1 self.legs[3,3] How do we use this? child = parent child.mutate() Now we have a mutated child What about the parent. Is it mutated too? Unfortunately, yes Objects are also too big to fit into a cup. Real mutations might be more random and perhaps more widespread EE 194/Bio 196 Joel Grodstein 12
The issue start with the parent child = parent child.mutate() Now the child points at the same worm as the parent. There's only one worm here When we mutate the child, the parent changes too they are just different names for the same worm. child parent legs [1,1,0, ] [0,1,0, ] [100,0, ] musc EE 194/Bio 196 Joel Grodstein 13
More fun with a worm Add a copy function: class Manduca: def __init__(self, legs, muscles, name): def __repr__(self): def mutate (self): def copy (self): return (Manduca (self.legs.copy(), self.muscles.copy()) child = parent.copy() child.mutate() child parent EE 194/Bio 196 Joel Grodstein 14
The fix start with the parent child = parent.copy() child.mutate() parent.copy() creates a new worm with identical genes as the parent. When we mutate the child, the parent is not affected. child parent legs legs musc musc [1,1,0, ] [0,1,0, ] [100,0, ] [100,0, ] [1,1,0, ] EE 194/Bio 196 Joel Grodstein 15
More exercises https://www.w3resource.com/python- exercises/class-exercises/ problems #10 and #11. Play with the homework: Look at the Manduca homework program manducaEv.py. Make sure you understand the class it uses. Write your own mutate() or mate() function, and print the resulting object to see if your function worked. The __repr__ function is a bit complicated. Write your own, and see if you can get it to print differently. EE 194/Bio 196 Joel Grodstein 16