Presentation is loading. Please wait.

Presentation is loading. Please wait.

EE 194/BIO 196: Modeling,simulating and optimizing biological systems

Similar presentations


Presentation on theme: "EE 194/BIO 196: Modeling,simulating and optimizing biological systems"— Presentation transcript:

1 EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Spring 2018 Tufts University Instructor: Joel Grodstein Objects

2 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

3 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

4 Demo Demo using Lunchables to introduce objects.
EE 194/Bio 196 Joel Grodstein

5 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

6 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

7 Objects Let’s start writing code for a Manduca object class Manduca:
def __init__(self, legs, muscles): self.legs=np.copy(legs) self.muscles=np.copy(muscles) self.name = "Bob" 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 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

8 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,…] [100,0,…] "Bob" self EE 194/Bio 196 Joel Grodstein

9 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

10 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

11 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 [ ] | [ ] … the other 9 rows … [ ] | [ ] EE 194/Bio 196 Joel Grodstein

12 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? Real mutations might be more random and perhaps more widespread Unfortunately, yes  Objects are also too big to fit into a cup. EE 194/Bio 196 Joel Grodstein

13 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. parent child legs [0,1,0,…] [1,1,0,…] musc [100,0,…] EE 194/Bio 196 Joel Grodstein

14 More fun with a worm child = parent.copy() child.mutate()
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() parent child EE 194/Bio 196 Joel Grodstein

15 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. parent child legs legs musc musc [1,1,0,…] [100,0,…] [0,1,0,…] [1,1,0,…] [100,0,…] EE 194/Bio 196 Joel Grodstein

16 More 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


Download ppt "EE 194/BIO 196: Modeling,simulating and optimizing biological systems"

Similar presentations


Ads by Google