In-Class Assignment: Agent Based Modeling of Superbugs (part 2)#

With more code review!#

Day 09#

CMSE 202#

Amusing comic from: http://abstrusegoose.com/

Put your name here

#

✅ Put your group member names here

#

Agenda for today’s class#


Part 1. Pre-class assignment review and discussion#

Did anyone have any specific issues with the pre-class assignment?

Let’s take a moment to highlight some key concepts. Discuss with your group the following prompts and write down a some brief notes from your discussion.

Question 1: What did you see as some of the key differences, advantages, and disadvantages in the linear, functional, and object oriented programming approaches you reviewed in the pre-class assignment?

Do This - Make note of anything that comes up in your discussion that you didn’t originally think or any ideas that you had based on your discussion.

Question 2: What is a Python “docstring”, how it is used, and why is it useful?

Do This - Make note of anything that comes up in your discussion that you didn’t originally think or any ideas that you had based on your discussion.


Part 2. Revisiting your assignment from last class, building your Python class(es)#

Now that you’ve had a bit more time to think about objected oriented programming and review some code that was written to build an agent-based model in an objected oriented way, you’re going to spend the first 30 minutes of your group activity time picking up where you left off with your “superbugs” model.

✅ Do This: Take this time to review the progress you made last week on initializing your petri dish. If you didn’t finish it, try to finish it. If you finished it, review your code and think about ways you might be able to make it better. Reviewing your own code will sometimes feel like looking at someone else’s code. You might find that your “present self” isn’t a huge fan of how your “past self” did things (especially if your past self didn’t leave you any comments!). Do this as a group!

Copy your code from last time into this notebook and try to get your petri dish code working or make improvements to a functioning version.

If you already managed to get the basic petri dish initialization working, start building other methods that would be necessary to evolve the model.

# CODE GOES HERE!

🛑 STOP#

Check-in with the rest of the group and with the instructor


Part 3. Understanding someone else’s code#

In the pre-class assignment, you spent some time doing a code review of multiple solutions to the same problem. In addition to simply reading unfamiliar code to make sense of it, when we use a new bit of code, if it’s reasonably well written there are ways to explore how it works from inside our Jupyter notebooks or within an IPython prompt as well.

Thus far you’ve been a bit of time trying to write your own solution to the superbug problem in an effort to recreate the anti-bacterial resistance experiment. Maybe you got everything working the way you wanted, or perhaps you’ve still ironing out a few bugs. Now we’re going to switch gears a bit and look at one possible complete solution to the problem.

✅ Do This: To start, you’re going to import a script that contains this solution. First, you need to download the script, superbugs.py, which you can get here:

https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/superbugs.py

Run the following cell to import the script once you download it.

import superbugs

Do This: Now, explore the above software, but don’t just look at the code right now. Use standard exploration commands (e.g. use help(), dir(), type, or ?) in your notebook to see if you can figure out what “superbugs” does/can do.

# put your exploration code here.
superbugs

Do This: Using the superbugs code, initialize the experiment and display the first time step of the board (t=0). Then, try to take a timestep and visualize the result. Again, see if you can do this without looking at the source code.

# put your initialization and visualization code here
# put your code for taking a step and visualizing the code here

Part 4. Reviewing the provided code#

OK, now that you’ve managed to learn a bit about the the code to the point where you can initialize the simulation, you’re going to take a look at the code and see how it compares to what you came up with.

Do This: Open superbugs.py in a text editor and review the code. How does it work, what classes are available? What are their associated methods and attributes? Compare and constrast the code with the code that you wrote (even if you didn’t get all bits of your code working). What parts are similar? What parts are different? What advantages/disadvantages do you see in your approach and this approach? Discuss this with your group and write down some notes from your discussion.

If there are parts of the code that you don’t understand, talk with your group or ask an instructor for help!

Do This - Erase this and record notes from your discussion here.

🛑 STOP#

Check-in with the rest of the group and with the instructor.


Part 5. Running the Simulation#

Now that you understand a bit about how the code works, you’re going to try to run a full simulation for a set number of timesteps.

Do This: Using the petri_dish variable you initialized previously, write an loop that runs the timestep function followed by the draw function 10 times. Make sure to animate the figure (you might need to dig up a previous example to get the animation working).

# Put your code here

Do This: You might have noticed that the simulation gets slower as it runs. Comment out the plotting and animation code and rerun the simulation with 100 iterations. Only draw the simulation once it is complete.

Question 3: Do you notice a change in the simulation run time when you comment out the plotting and animation code? How can you tell?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 4: What if you wanted to know exactly how long it took to run the simulation? You might have used the time module in your animation code or in previous code you’ve worked with, but we can also use it to time how long certain pieces of code take to run. We can record an initial time with something like:

start = time.time()

And then record the time the code finishes with:

end = time.time()

Try adding this to your code above to time exactly how long the simulation took. Add a line to print the total run time at the end. Try comparing the total run time to the simulation that includes the plotting and animation components.

What is the difference in the timing between the versions with (just 10 steps) and without (100 steps) the plotting?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 5: What if you wanted to know how long each step of the simulation took (i.e. each iteration of the loop)? How would you track this information in your code? If you have time at the end of class, come back to this and try implementing some code that tracks the time that each step took and prints or visualizes the results.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


Part 6. Testing Scientific Questions#

For the remainder of the class, we are going to use this code to ask some scientific questions.

Do This: Discuss as a group/class the types of scientific question can you could ask (and potentially answer) using this type of simulation.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 6: Given the above questions, what types of measurements or modifications might we need to add to the superbug code?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Visualizing the progress of the superbugs#

If we wanted to use this simulation to answer one of your scientific questions, we might want to visualize the progress of the superbugs as a function of time as they march across the petri dish.

Do This: Write some code to show, in one graph, the progress of the simulation. For example: you could plot the number of iterations on the x axis and the progress the bugs have made toward the center of the simulation on the y axis.

#Put your code here.

Do This: Do this, modify or use the simulation in some way that allows you to test a scientific question of the simulation.

# put your scientific experiment code here.

Question 7: What scientific question does your new usage or modification demonstrate and what can you conclude from the simulation?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


Congratulations, we’re done!#

Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for today’s submission folder (Don’t forget to add your names in the first cell).

© Copyright 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University