 Now let's actually take a look at a very advanced topic and again We're in remote learning because we're in a wonderful little pandemic or at the ending portion of it Well, we'll see Delta variants and whatnot But let's say it wouldn't be a data visualization course without some type of visualization of Disease spreading or infection spreading. So in this instance, I've got all of this already built out just to show and kind of Walk through what we're working off of So to even begin we're starting with some of our basic libraries a random library So we can just have a little randomness to their numpy because we're gonna actually need it math because we're gonna be doing the distance formula so square roots, but The really important part here is this kind of slew of information. You're seeing going on here. So I'm importing matplotlib I'm importing a Subplot or sub portion of matplotlib again instead of being piplot I'm actually importing animation because again if we're trying to simulate something over time You know time means we need to represent at different time cycles and for that We're gonna use something called function animation or funk animation Then I am adding in just a little bit more for example this ipython.display Since we're doing a visualization Jupyter will not just automatically show the visualization. So we're gonna have to like Force it to do that. And so we've got you know that also imported in now I do have some extra a little functionality going on here for example RGB to float When we get to a certain portion, I'll acknowledge we have to convert our RGB values into a floating point number from 0 to 1 But that's really weird when we're trying to just represent those as colors So I'm built a little function here that I'll just do that quickly for me And then in fact that's actually where some of my parameters are coming in so population size Let's just do a thousand people the height and width of sort of my visualization. So 500 by 500 square Red and blue are gonna represent our people red will represent someone is infected and blue Someone is not infected. So these are just some RGB values that I got from flat UI colors Com and then I'm just going to work off of an infection rate of five percent You have a five percent if you come in contact with someone with the infection You have a five percent chance of getting the infection nothing terribly crazy We're not going into like oh what what happens when they recover or they become immune just Starting with the most basic version of that So to start I'm first just generating where each one of the population Individuals participants in our little simulation are going to appear So I have two values positions Which is a list of their XY coordinates and then status which is going to be whether or not they are a red or blue person then Going through all thousand people generate some random XY position that from zero to five hundred and Do that for both X and Y? Okay, so This is just a little quick, you know when you're dealing with randomizing data You know just storing that in your list and checking to see if it is if it is Randomly generate a new piece in this case I don't want two people in the same spot that doesn't physically happen You can't stand on that person you can stand on a person anyways But just a quick while now this technically could produce theoretically This could produce an infinite loop, but since we're dealing with random. How possible is it to randomly generate? the same location twice You know Doing it in an infinite number of times again theoretically possible app Realistically not so much, you know, you may happen like once or twice So either way once I've established an X and Y Coordinate that is not currently in use. I will go ahead and do it and add it into positions Then I'm again. I'm just doing a check. Let's see who already starts with it just to you know, inject it in so Generating a random number from zero to one and if that is below my infection rate So if it's below five percent, you're infected and I am keeping a tally just so I can have sort of a Know of how many people started with my infection. So then I've just got some visualizations going on here That's the same number So let me Actually get rid of that So let's go ahead and run that first one boom take that second one boom and you see that we initially have 62 people Infected out of our thousand. Okay fair enough This is just more of a when I'm sort of working off of large data sets I like to throw in these little cells here where just show me five just show me five values So I have a good feeling of what's going on here Okay, so the positions of the first five are those that all that's all it's doing for me But that's you know, it tells me my data is being generated properly Now when we're dealing with a scatter plot diagram One of the things you might remember is that we need an X value Which is the x axis values and a y value which is the y value values Since I'm dealing with sort of the entries as x and y in one lump Instead I do need to do some fancy Python magic and that's what this is what I'm effectively saying here is first Convert my list into an NP array then from that entire array Extract out the 0th column So in this case again, that's exactly what that does takes out them And so we can prove this so again that 0th column should be 26 24 300 453 8190 189 That's exactly what we see. I'm doing it again with the y coordinates And we can confirm that those are also there So now what we're going to do and you can already see I've got a little text coming in here as well I'm just going to just show a visualization to start So in this case you can see I'm building I am technically still using the subplots But since I'm only using I'm not using I'm only using one row one column the same kind of thing again just with my prior video Acts is not an array or it's not a dictionary. It's just the one single plot We kind of have to do this for some of the fancier magic later But then I'm coming in and this is again that RB RGB to floats Doing that as well So we established that those are now instead of them being the tuples of integers. They're now tuples of floats I am adding in a little text point here a ax.txt and so same kind of thing with a plt or I mean sorry a plot a pie or a scatter Text is just going to add a text where well zero zero represents put it at the bottom left corner It's not going to be the cleanest, but it gets the job done for a visualization And then we'll just say oh, this is the initial point Then as you can see I'm doing scatter now one thing you might notice is I'm saving those as variables when we were working off of them Previously so when we are working off of say for example this value or rather. Let me show the pie chart one that will show it It's sort of producing it's just going to magically imprinting out those values. Well, we're going ahead and saying let's save them into variables and the reason why is we're gonna have to Update them and manipulate them later. So again, if we take this and we run it We see some little red dots every once in a while that's indicating where our infected individuals are in our space. So again, this is just a little text saying that For if you're you know looking at the the notebook now, I do have two specific Sort of functions that I'm working off of here distance. It's the traditional distance formula. So again given two points Put it somewhere Update position. I'll draw this out actually just for a quick second. So the entire idea to Update position what I'm working off of is if I think about this is where my Person is right. Well, we can think about this as sort of It's own little tiny XY plane and again, this will just sort of be a very rudimentary drawing to this But I want to say I want my participants my Individuals to be able to move so the way I'm looking at it is I want to produce sort of a Direction that they can move from and a radius to that So I've got this giant circle again, you know, just thinking about them As a particular step. So if I wanted to go here, well, that's technically just a radius And so my in-game is I need to take that radius and I need to find Specifically to change the color again. I need to find where that x value is so where that x value is and where that y value is and Once I find out that point You know again, if I'm thinking about this as it moving I want my person to move to this new location So once again There we are That's what I'm doing. I take my XY position that I'm currently at Then I'm randomly generating just a direction just pick a number from 0 to 360 That's gonna be where we're kind of working off of I probably don't need to be doing that much, but that's just it works. So And then I'm doing a simple little mathematical calculation I am just arbitrarily stating that they're moving two steps. I could have made this another random So someone traveled far distances. I just said two But I'm again since I'm working off of a circle Math that sign convert that 0 to 360 into radians So it'll tell me sort of a good spot to work off of And then I take wherever my x my y were and I'm adding that to that position. So in theory It'll update the XY Now we get to the meat and potatoes and you can see I've got a lot of text here going on here What happens is when we think about sort of a simulation? Just to bring this back up When we think about a simulation We are operating off of time and time constantly moves forward So wherever my point is, let's call this, you know t1 just to be fancy Wherever my point is Well, the next time cycle or time step. You'll see a lot I want my point to be somewhere else. So in this case, I want it to be maybe up here And then you know for a time cycle three maybe up here. And so at each one of these time cycles We're effectively producing a new Visualization so I want to see this visualization. I want to see this visualization and this visualization So what we're working off of when we're building out something like an animation is I need to make each of these individual frames so To do this No There we are to do this I'm going in and one of the things we have to take note is I have to go through every single person so Everyone in my simulation has to be updated So again, I'm going in and I'm doing a for I in the population for all thousand people Get my specific location or my individual location And then update that position so I'm saying move before infect you could do the opposite way But I that's the position that I chose then I'm getting my status Okay, fair enough then Something that we can sort of enjoy here is I don't care about any other Instance if you are not infected if you moved you moved awesome I only care if you are infected because then I got to see if you spread so if you are A red you're an infected individual Guess what I have to do well There are a number of different points that are in my population size Unfortunately, I have to go through each one of these points and maybe cleaner ways to do this But I'm just going to say well now go through the entire population again to see who you who you can Potentially infect so again for J in population size and get their location Then I'm doing just some since we're doing a large number of traversings a thousand by a thousand I'm doing some quick things if I'm if my I and J are the same aka if I'm the same person skip it You can't infect yourself. You're already infected and then secondly I am just checking well is that person already infected again They are already infected can infect them again. So if that's possible skip move on Then as you can see I'm doing a distance. I'm checking for 10. I had it at 6 I've had it at different numbers, but you know, you could say for example just to make the joke there six feet if you are Closer than six feet Then let's roll the dice Generate a random number zero to one and if that is less than the infection rate well, guess what you have just swapped from being Clean or negative to a positive. You're now a infected party crap So that is just on sort of the mathematical side, right? All of this looping was just to again Update locations and statuses then we have to actually update the visualization and to do that Again, this is where it's a little funky. I won't lie. It took me a few minutes of Working through this and debugging and whatnot first thing if you want to update those positions This is one of the reasons why we saved the scatter plot diagram earlier as a variable Because we're gonna reference it. We're gonna say take scatter And in our case of updating the XY coordinates Update or set offsets and that'll change the XY positions Then since we're dealing with sort of this visualization of colors this I will go ahead and just Say this took me down a rabbit hole because apparently that's not just as easy as it looks And I acknowledge this with the variable naming But to change colors in a scatter plot diagram and animated scatter plot diagram on that plot lib You use set face colors now. This is actually why I have to do this RGB to floats Variable is because set face values these have to be a tuple of floats if they are not a tuple of floats It doesn't work Why? Because I didn't invent that plot live and whoever is working on set face colors has not modified it as of this video Either way, then just a quick little the next thing I'm going in with is the text is Changing to the new iteration And then one of the things that I have to do as part of the animation function for the funk animation Process is say what updated this is again for the internals of matplotlib to understand Oh, this is what needs to be updated and whatnot Okay, well we take that and where we leave off. So let's load that into memory So now we officially have everything in place And so it's time to now work on actually making the animation and to do that again We're using funk animation So to break down every single one of the pieces parts that are going on here The first thing I'm giving it is fig again. What is the visual that we're working off of? Animate which is in this case telling funk animation. What is the update function that you want to happen at each Time cycle or frame Then I'm saying how many frames I want to work off of and again as you can see it's frames equal Because this is where we start getting into the special keywords. So I'm saying do 200 iterations 200 frames and Interval interval is now specifying well What kind of delay do you want between each one of your frames and in this case? This is a millisecond approach So I'm just saying have a hundred milliseconds going on there now Fargs Fargs is saying what additional values you need to give your update function if I did not have this The only thing it would be expecting is iteration, but I'm also passing in position and status as well So I'm giving those to it and then blitz blitz is just it's again It's some of the internals going on with that plot lib to say oh well, you know This is going to optimize it and speed up the entire process. So we take this and we'll run it Now we've just set the animation. We haven't made the visual as you can see it's not doing anything But okay, well, I'm going to I think this is actually This will I move some things around for the video But that should stay the same roughly speaking or one got updated, I guess for that initial step But the entire idea here, let me actually edit cut Edit Paste above there we are so now there comes a place where we have a visualization We have an animation that we want to look at we have two approaches. We can either put it as a Little graphic in our Jupiter if we are working off of Jupiter or as you can see we can also Save it and I'll talk about the saving in just a little bit But as you can see this is where that HTML comes into play We take our animation and there is a function for funk animation To HTML 5 video and it will inject the video into our player Now this is going to take a second it will process for some time But to just at least show that off So we don't have to wait for it When it saves this is the visualization that you'd be getting so as you can see the points are floating and as Time and iterations go on The spread is happening So congratulations, you can see and again this will take some seconds So I'm going to leave it running until that happens Please by all means start hitting the arrow or the L key to skip ahead Because again, this is going to take some time and in fact while it's happening What I'll do is I'll just go ahead and explain so with animation save Again, we're specifying the file name so in this case I'm calling it simulation and giving it the file extension mpv or mp4 up there. We are so here We are with that visualization. This is actually one of the reasons why as you can see the quality is not that Great, that's mostly again Jupiter just And Matt plot lib making sure things stay minimal so you can see there is some growth going on there And as we reset as It begins every little piece is slowly moving and then you can see the spreads are slowly happening the entire time But there we are Again, so again that visualization quality was low quality and again That's just to make rendering it onto a page very quick and easy But that's actually where when we're saving those animations the different Parameters that we're using are going to increase the quality. So FPS frames per second Put 30 frames in one second DPI dots per inch is what that stands for 800 is just it's a high number So have higher I have a lot more pixels for a particular dot and then just some extra arguments This is more for when you're rendering it since we're rendering a video. You need to use a video codec So in this case v codec and then libx to 64 is What I am using But once again if we were to now take sort of that we've ran through our simulation And we run this we can see that after our 200 iterations We started with 62 infected, but after just 200 time steps Almost one-third of our population had been infected and this is why we had the whole stay at home thing So, you know some fun simulations you can play with this. So you can obviously update it maybe create another variable for like someone who has been infected but has sort of gotten over the Parameter and again, you would use sort of for the same format of You know if they are red You know You can also determine if they get cured And then you would change my status to I don't know purple or something like that and you can represent that So again, this is a nice little blanket template for working off of again changing Colors in an animated scatter plot diagram in that plot loop