r/Python • • Mar 11 '20

I Made This Really simple simulation of virus spreading. Black dots are people, yellow dots are infected (it starts from 1). Each cell visited by an infected can infect other people passing through that for 3 iterations. After 20 iterations a person is infected is deleted from the grid.

1.4k Upvotes

128 comments sorted by

429

u/doktorneergaard Mar 11 '20

Yellow on white background, noice.

24

u/[deleted] Mar 11 '20

That's default coloring of gnuplot... BTW in the standard resolution was pretty clear..

7

u/wreckingballjcp Mar 12 '20

This is /python. Matplotlib or seaborn. Ez

1

u/[deleted] Mar 12 '20

you're absolutely right :( I had some issues and had to use what I got

2

u/wreckingballjcp Mar 12 '20

Share the code and I can help find your way. Some cool opportunities here too. Can add "quarantine " effect (diffusion rate decrease), valid testing effects (how quickly the diffusion decrease goes into effect), and do quick comparisons across a lot of scenarios.

5

u/[deleted] Mar 12 '20

https://github.com/pl992/VirusPropagator.git It's very bad code though... sorry I'm not a programmer

4

u/[deleted] Mar 12 '20

Well by definition you are so hi fellow programmer.

3

u/Brocean09 Mar 12 '20

To be honest, it's good code. Modular, mostly very easy to read, succint, seems efficient too. Could do with some improvements, but overall good job!

49

u/Pok3maniac00 Mar 11 '20

Probably because gui layout’s default background was gray? You can change the colors of everything. Looks great though! Love the model idea

43

u/dancinadventures Mar 12 '20

Light grey on grey

😎

8

u/chhuang Mar 12 '20

Best website pallette

6

u/[deleted] Mar 12 '20

We need to use red on blue instead.

-23

u/I-Do-Math Mar 11 '20

Disregard everything else and post a sarcastic comment so that you would feel superior. Noice.

22

u/knorfit Mar 12 '20

I personally cannot believe this individual cut straight to why this graphic is difficult to interpret before fluffing their post up with praise

-18

u/imforsurenotadog Mar 12 '20

Right? Couldn't just say "Nice work, but this might look better if you changed the yellow on the white background." Too difficult to be decent these days.

7

u/grey_energy Mar 12 '20

Anything said before "but" doesn't count.

57

u/lettuce_hammer Mar 11 '20

could you share github link ?

8

u/[deleted] Mar 12 '20

Sorry, I forgot to say that each person passing through a contaminated cell has a probability of 0.6 to get infected.

I wanted just to reply to those people who say this is scientifically nonsense:

  1. I made this just for fun it's not a PhD project or a publication

  2. It's a model! and a very easy one, I said that! Maybe you don't know but in science you build simplified models all the time... With that I don't want to say that this is a realistic one but just a model with the assumptions I said.. and you could get some meaningful results/ideas also from this easy model so it doesn't mean anything is scientifically nonsense...

1

u/mrasadnoman Mar 12 '20

What tools did you use for graphical simulation?

1

u/[deleted] Mar 12 '20

I just save in a text file the the position of each person and weather he's infected or not... then I read the data using gnuplot... you could reach the same (and I think it's easier) using matplotlib...

2

u/indiebryan Mar 12 '20

What happened to the black cell at the top right? Is he self isolating?

3

u/[deleted] Mar 12 '20

That's just the legend... Sorry about that xd

1

u/indiebryan Mar 12 '20

Ah, he is legend.

39

u/[deleted] Mar 11 '20

It'd be awesome if there was a counter on the side displaying the number of black and yellow dots

2

u/vapperss Mar 16 '20

How would one go about doing this?

38

u/warpchaos Mar 11 '20

Very cool! Next you could add air-travel simulation by allowing a few cells to jump further at random (regardless of their infection status)

18

u/excentricitet Mar 11 '20

And also decomposition of corpses. After 20 ticks you should also add some chance for a dot to get cured. Eventually, everyone will be dead

2

u/yvrelna Mar 12 '20

People build immunities once they got sick, so they should recover and become much less likely to get infected again.

1

u/excentricitet Mar 12 '20

Also add psychosis to infected individuals.

9

u/jwbowen Mar 11 '20

I feel bad for the dot stuck in the upper right.

2

u/Robroyb06 Mar 12 '20

Maybe he stayed home for a few days after seeing all those others interacting and getting sick! Ha

7

u/TheMaxCape Mar 11 '20

"Removed" 😵

3

u/inkling_nb Mar 12 '20

Press F to pay respects.

7

u/myislanduniverse Mar 12 '20

Cellular automata are some under utilized models.

3

u/imdeeami Mar 12 '20

Actually, we use them all the time in statistical mechanics, when the continuum version is unfeasible. We call them lattice models. The (kinetic) Ising model is the best-known example. They are used a lot in epidemiology as well! Look up SIR model for instance.

1

u/myislanduniverse Mar 12 '20

That's awesome, I will!

1

u/imdeeami Mar 12 '20

A good place to start is this wiki page

2

u/robin-gvx Mar 12 '20

This is actually not a cellular automaton. In CA, the cells generally don't move around. Still both CA and non-CA simulations like this one are very interesting in their own right!

5

u/FriggenGooseThe Mar 11 '20

This looks fun! It would be interesting to see what happens to the model if you add some repelling force some time after infection.

1

u/justsomeguy05 Mar 12 '20

What are the criteria for infection?

2

u/[deleted] Mar 12 '20

sorry that I've forgotten to add... if a person passes through an infected cell has a probability of 0.6 to get infected...

1

u/Keyboard_null Mar 12 '20

What programs was this made with? Looks really cool?

1

u/[deleted] Mar 12 '20

I made it on python saved the data as text file, drawn them using gnuplot.... you could reach everything similar using just matplotlib

1

u/Kengaro Mar 12 '20

what did you use to simulate the random moves of the "people"?

1

u/[deleted] Mar 12 '20

Just simple random walk... In this simulation at each iteration each person can move on one adjacent cell or staying in the same cell.. Maybe you'd like to know that the increasing of the number of cell a person can move increseas strongly the spread of the yellow dots

1

u/Kengaro Mar 12 '20

Thank you, but I wanted to know which algo you used for the random walk

1

u/[deleted] Mar 12 '20

The easiest one: pick two random numbers between - dx;+dx and - dy:+dy. Then I just added that number to the position x, y. If the box borders are reached then the person won't move toward the borders

1

u/Kengaro Mar 12 '20

rather simple :) Thank you for your answer

1

u/[deleted] Mar 12 '20

How did you model this mathematically? Can you tell us more about the function

1

u/[deleted] Mar 12 '20

everything regarding the model is written in the title... I just forgot to say that a person who passes through an infected cell gets infected with a probability of 0.6... the people are moving using a simple random walk (up/down/left/right for each iteration). recap:

  1. 2D grid 100x100 representing the space

  2. Each cell of the grid can be contaminated or not contaminated. After three timesteps a cell is contaminated comes back to be not contaminated

  3. It starts from a population of 1000 people with one infected.

  4. People are moving through random walks

  5. If an infected person passes through a not contaminated cell the cell becomes contaminated, if it's already contaminated the timer of the contamination start over

  6. If a person not infected passes through a contaminated cell has a probability of 0.6 to get infected

  7. After 20 timesteps a person is infected is deleted from the list of people (and from the grid)

If you want to know something more specific just ask... but I think that's it... pretty easy model...

1

u/GidonC Mar 12 '20

Can you send source please?

1

u/[deleted] Mar 12 '20

2

u/GidonC Mar 12 '20

For me it looks amazing

1

u/[deleted] Mar 12 '20

Why can't I use this on Pycharm?

1

u/[deleted] Mar 12 '20

what is the problem?

1

u/[deleted] Mar 12 '20

Well I don't know if im doing it wrong, but I copy and pasted the code into it. Hit run, saw it needed Numpy, downloaded Numpy. And now all it does is prints a number then ends

1

u/[deleted] Mar 12 '20

yep in that folder where you run it there will be many different files called world#### each of this file contains the position of each person... you can plot them using pyplot.scatter(c1,c2,c=c3) where c1,c2,c3 represent column 1,2,3 respectively. You should loop through all this files and create different pngs for each one if you want a the video

1

u/[deleted] Mar 12 '20

I don't understand any of that but its my new project to figure it out xD

1

u/[deleted] Mar 13 '20

If you're interested I've just added a function which makes a movie similar to the one shown here

69

u/lorenthal Mar 11 '20

Scientifically speaking this is nonsense.

78

u/Takarov Mar 12 '20

The parameters of the simulation are obviously not reflective of any disease, but the basic mechanics behind the model are mechanics you would find in scientific simulations with a lot less complexity.

There's no need to shit on someone's project because it's not reaching the level of sophistication you think it should start with.

31

u/[deleted] Mar 12 '20

There's no need to shit on someone's project because it's not reaching the level of sophistication you think it should start with.

This is reddit, if a person haven't done a dissertation level's worth of work, analysis, and publishing you have to continually berate them for being a baddy.

2

u/kreactor Mar 12 '20

FTFY: This is reddit, even if a person has done a dissertation level's worth of work, analysis, and publishing you have to continually berate them for being a baddy.

0

u/[deleted] Mar 12 '20

[deleted]

12

u/Certhas Mar 12 '20

It's one thing to say that it's not completely realistic. It's another to say its scientifically nonsense, especially if that's not true and there is lots of science out there using this type of modeling to understand rough features of infection dynamics.

3

u/robin-gvx Mar 12 '20

That's a false dichotomy. Not shitting on someone's project isn't the same thing as blindly praising it.

5

u/Certhas Mar 12 '20

Scientifically speaking this is a SIR model with a random temporal contact graph. It is perfectly sensible. Lot's of scientific literature exists on this style of modeling to understand general properties of this type of dynamics.

23

u/Von_Lincoln Mar 12 '20

Some people can learn about scientific subjects from non-scientifically rigorous depictions/imagery of related concepts (or contrasting concepts).

4

u/wreckingballjcp Mar 12 '20

Except with yellow on white

7

u/[deleted] Mar 12 '20

It looks neat and nice job OP, but yeah.

6

u/jcrocket Mar 12 '20 edited Nov 03 '25

SECOND This is that scornful piece, that scurvy hilding, That gave her promise faithfully, she would be here, Cicely, the sempster’s daughter. The next gloves that I give her shall be dogskin! Nay an she fail me once—You can tell, Arcas, She swore by wine and bread, she would not break.

-4

u/BenTheHokie Mar 12 '20

Whats so bad about it? My intuition says this is a pretty good model.

0

u/[deleted] Mar 12 '20

[deleted]

2

u/BenTheHokie Mar 12 '20

What specifically about this model concerns you

2

u/[deleted] Mar 12 '20

This simulation removes each node from the population following infection. While it's true that immunity is built up against many diseases after recovery, that doesn't remove them from the population. This actually slows the outbreak by buffering the rest of the immunological naive population.

This model simulates a 100% mortality rate by removing nodes following infection rather than leaving recovered nodes which cannot be infected.

All that aside, your use of the word "intuition" in this context is problematic.

0

u/[deleted] Mar 12 '20

[deleted]

-4

u/poobahh Mar 12 '20

It also is removing infected dots after 20 iterations. With that logic the entire population would die

12

u/Takarov Mar 12 '20

That's not at all accurate. People people who have a disease and recover from it generally do not contract it again. Epidemiological models remove those cases from the model because they can't meaningfully interact with infected or uninfected cases. They're just counted differently for the results.

4

u/poobahh Mar 12 '20

That makes sense. I thought it was just a model of the entire population. Didn’t think about the people not contracting it a second time

6

u/FriendOfDogZilla Mar 12 '20

Not necessarily. Removal could represent death or recovery/immunity. If the model is to explore the spread, the outcome of an infected dot isn't relevant.

-5

u/poobahh Mar 12 '20

I have been informed already. Thanks tho

-7

u/paradigmx Mar 12 '20

Exactly. This simulation assumes a lot about the contagion level and mortality rate while ignoring recovery rates. This simulation assumes that if you come into contact with the virus, you will catch it and you will die. This is simply not true and is based on a swath of other factors.

5

u/potatoinmymouth Mar 12 '20

I don’t think its assuming they die, rather that they cease to be infectious or infectable after a given period of time, which is roughly accurate for virus behaviour.

3

u/quickhakker Mar 12 '20

Is it wrong to be getting game of Life vibes from this

3

u/gazhole Mar 12 '20

The spread of coronavirus in a disco environment

3

u/invisible-nuke Mar 12 '20

I love how the scale to the right is absolute pointless haha

2

u/burrbro235 Mar 11 '20

Can you simulate virus quarantine effectiveness?

2

u/ourlastchancefortea Mar 12 '20

I like the introvert black dot in the upper right corner: "Nope not gonna move outside. Sitting here in my room."

2

u/[deleted] Mar 12 '20 edited Mar 12 '20

Lmao deleted from the grid

2

u/invisible-nuke Mar 12 '20

You should also add a kill count and density that changes throughout the simulation.

4

u/metriczulu Mar 11 '20

"Deleted from the grid" is a nice way to describe what's about to happen to everyone bruh.

3

u/Danny1878 Mar 11 '20

What percentage of people contacted the virus?

-3

u/[deleted] Mar 12 '20

It should be around 60-70%. I've seen 70% tossed around a few times. Also needs to adjust for age distribution and maybe weight the likelihood of spreading it based on how asymptotic a particular age group is. So like the less asymptotic you are (kids) we can probably a assign a slightly higher likelihood of them pass it around such that the overall rate is the stuff we've been reading about.

1

u/DeadlyDolphins Mar 11 '20

Oh that's an awesome visualization. I agree yellow might not be the best color to visualize this, but it actually helped me a lot to grasp how infections like this could work. Have my upvote

1

u/PlantsAreAliveToo Mar 11 '20

You mentioned gnuplot. I assume you saved each frame as an image. How did you string it all together to make a video?

1

u/ThyMagicalDuckling Mar 11 '20

Damn, I wanted to make something like this but you beat me to it

1

u/GenericHumanNo7 Mar 11 '20

Implement planes

1

u/vectorpropio Mar 11 '20

I think you should use some distribution with heavier tails to make the model more realistic.

I think it's s great work, i said it because it seems like an easy improvement.

1

u/TruePhilosophe Mar 12 '20

Please share source code!

1

u/shiuido Mar 12 '20

Very cool, I love seeing emergent systems like this play out.

There's a square that sits at about 90, 90 and doesn't move. At 21 seconds it randomly gets infected, then a few seconds later is cured.

1

u/[deleted] Mar 12 '20 edited Mar 12 '20

Depending on the the logic on how likely a dot is to visit an infected cell it looks like you are simulating an r naught number of less than 3 which roughly that of coronavirus. Removing the dots implies a mortality rate of 100% or at least implies that the virus will continue to focus on immunologically naive population only neither of which really fly.

1

u/MiksBricks Mar 12 '20

Person infected is deleted from the grid

  • that’s grim.

What does it look like if they are just turned back to black?

1

u/newtomtl83 Mar 12 '20

Thanks for that, I'll sleep great.

1

u/virtualadept Mar 12 '20

That's awesome - nice work.

1

u/undernutbutthut Mar 12 '20

It's like a wave of cheese!

1

u/[deleted] Mar 12 '20

Good, maybe I can go to the store without sitting in traffic now.

1

u/doorrat Mar 12 '20

Cool stuff. I wonder how it would progress with both "symptomatic" (i.e. other dots are repelled) and "asymptomatic" (i.e. other dots have no reaction) dots. Actually I'm sure there's a dozen parameters one could play with.

1

u/aSmallAssAnt Mar 12 '20

Now try to simulate a cure, maybe with blue dots just like you did with yellow, see what happens?

1

u/murrietta Mar 12 '20

This is nice and reminded me of a project I should dig up.

I did something similar to this using Processing (packaged Java for general visualizations). I added parameters for individual resistance and transmissibility (which basically added together). And also two params for number of iterations to run and speed to run at. I added a transparent histogram that gradually overlaid the window indicating the distribution of the time it took for the whole population to become infected (I think I also added a cut-off time for very resistant individuals).

1

u/KingBubIII Mar 12 '20

Next add a cure after it reaches a certain infection percentage to get rid of the infection.

1

u/RS_05 Mar 12 '20

Why 3 iterations to get infected? Is that a sort of 33% chance of getting infected? Thanks

1

u/mrugacz95 Mar 12 '20

Nice simulation. You could add plots showing number of susceptible, infected and recovered and you should get plots similar to mathematical models: https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology

1

u/Qindral Mar 12 '20

Could you plot the graph of how many infected and dead? Would be nice to see the distribution

1

u/AX-11 Mar 12 '20

are the xy numbers coordinates? its interesting that the yellow moves like a wave across the x axis.

1

u/[deleted] Mar 12 '20

"Here lies u/robo_socks. Gone after only 20 iterations. Deleted from the grid on a Thursday"

1

u/acroporaguardian Mar 12 '20

You gotta add air travel. Random dots that travel to and from. Some are infected, some aren't.

1

u/[deleted] Mar 12 '20

Pretty neat, shame that the top comments are so negative

1

u/Lucky_Miner01 print('Still learning!') Mar 12 '20

Can someone tell me how you would build something like this. I’m relatively new to programming.

1

u/[deleted] Mar 12 '20

That looks like a simulation of Christopher Columbus landing on America with his buddy smallpox.

1

u/[deleted] Mar 12 '20

"black dots are people, yellow dots are infected". TIL infected people are not people anymore

1

u/goatsnboots Mar 12 '20

You should post this in r/dataisbeautiful too. They might like it.

1

u/Rieux_n_Tarrou Mar 11 '20

Moral of the story? This is all right

Sad to not see any gradations from 0 to 1. As the dot gets sicker it should be red and purple, just like humans

-1

u/krcha Mar 12 '20

This looks inaccurate

0

u/[deleted] Mar 11 '20

[deleted]

12

u/[deleted] Mar 11 '20

Death

6

u/alecbenzer Mar 11 '20

Or quarantine because you've started to show symptoms.