From 7b6b4984b7115e1e0daeead468d5d9024f289c3a Mon Sep 17 00:00:00 2001 From: coderbeta1 Date: Mon, 19 Feb 2024 00:31:14 +0530 Subject: [PATCH 1/3] Documentation improvement --- examples/virus_on_network/README.md | 17 +++- examples/virus_on_network/app.py | 2 - .../virus_on_network/__init__.py | 0 .../virus_on_network/model.py | 7 +- .../virus_on_network/server.py | 81 ++++++++++--------- 5 files changed, 67 insertions(+), 40 deletions(-) delete mode 100644 examples/virus_on_network/virus_on_network/__init__.py diff --git a/examples/virus_on_network/README.md b/examples/virus_on_network/README.md index b9fd1e94..6f470702 100644 --- a/examples/virus_on_network/README.md +++ b/examples/virus_on_network/README.md @@ -2,7 +2,13 @@ ## Summary -This model is based on the NetLogo model "Virus on Network". +This model is based on the NetLogo model "Virus on Network". It demonstrates the spread of a virus through a network and follows the SIR model, commonly seen in epidemiology. + +The SIR model is one of the simplest compartmental models, and many models are derivatives of this basic form. The model consists of three compartments: + +S: The number of susceptible individuals. When a susceptible and an infectious individual come into "infectious contact", the susceptible individual contracts the disease and transitions to the infectious compartment. +I: The number of infectious individuals. These are individuals who have been infected and are capable of infecting susceptible individuals. +R for the number of removed (and immune) or deceased individuals. These are individuals who have been infected and have either recovered from the disease and entered the removed compartment, or died. It is assumed that the number of deaths is negligible with respect to the total population. This compartment may also be called "recovered" or "resistant". For more information about this model, read the NetLogo's web page: http://ccl.northwestern.edu/netlogo/models/VirusonaNetwork. @@ -26,6 +32,15 @@ To run the model interactively, run ``mesa runserver`` in this directory. e.g. Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run. +or + +Directly run the file ``run.py`` in the terminal. e.g. + +``` + $ python run.py +``` + + ## Files * ``run.py``: Launches a model visualization server. diff --git a/examples/virus_on_network/app.py b/examples/virus_on_network/app.py index da3e5dda..5b9a4586 100644 --- a/examples/virus_on_network/app.py +++ b/examples/virus_on_network/app.py @@ -1,5 +1,4 @@ import math - import solara from matplotlib.figure import Figure from matplotlib.ticker import MaxNLocator @@ -48,7 +47,6 @@ def get_resistant_susceptible_ratio(model): def make_plot(model): # This is for the case when we want to plot multiple measures in 1 figure. - # We could incorporate this into core Mesa. fig = Figure() ax = fig.subplots() measures = ["Infected", "Susceptible", "Resistant"] diff --git a/examples/virus_on_network/virus_on_network/__init__.py b/examples/virus_on_network/virus_on_network/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/virus_on_network/virus_on_network/model.py b/examples/virus_on_network/virus_on_network/model.py index 2cee39cc..f886adb5 100644 --- a/examples/virus_on_network/virus_on_network/model.py +++ b/examples/virus_on_network/virus_on_network/model.py @@ -28,7 +28,9 @@ def number_resistant(model): class VirusOnNetwork(mesa.Model): - """A virus model with some number of agents""" + """ + A virus model with some number of agents + """ def __init__( self, @@ -104,6 +106,9 @@ def run_model(self, n): class VirusAgent(mesa.Agent): + ''' + Individual Agent definition and its properties/interaction methods + ''' def __init__( self, unique_id, diff --git a/examples/virus_on_network/virus_on_network/server.py b/examples/virus_on_network/virus_on_network/server.py index c49306a9..79be5810 100644 --- a/examples/virus_on_network/virus_on_network/server.py +++ b/examples/virus_on_network/virus_on_network/server.py @@ -49,7 +49,11 @@ def get_agents(source, target): return portrayal -network = mesa.visualization.NetworkModule(network_portrayal, 500, 500) +network = mesa.visualization.NetworkModule( + portrayal_method=network_portrayal, + canvas_height=500, + canvas_width=500, + ) chart = mesa.visualization.ChartModule( [ {"Label": "Infected", "Color": "#FF0000"}, @@ -71,63 +75,68 @@ def get_resistant_susceptible_ratio(model): model_params = { "num_nodes": mesa.visualization.Slider( - "Number of agents", - 10, - 10, - 100, - 1, + name="Number of agents", + value=10, + min_value=10, + max_value=100, + step=1, description="Choose how many agents to include in the model", ), "avg_node_degree": mesa.visualization.Slider( - "Avg Node Degree", 3, 3, 8, 1, description="Avg Node Degree" + name="Avg Node Degree", + value=3, + min_value=3, + max_value=8, + step=1, + description="Avg Node Degree" ), "initial_outbreak_size": mesa.visualization.Slider( - "Initial Outbreak Size", - 1, - 1, - 10, - 1, + name="Initial Outbreak Size", + value=1, + min_value=1, + max_value=10, + step=1, description="Initial Outbreak Size", ), "virus_spread_chance": mesa.visualization.Slider( - "Virus Spread Chance", - 0.4, - 0.0, - 1.0, - 0.1, + name="Virus Spread Chance", + value=0.4, + min_value=0.0, + max_value=1.0, + step=0.1, description="Probability that susceptible neighbor will be infected", ), "virus_check_frequency": mesa.visualization.Slider( - "Virus Check Frequency", - 0.4, - 0.0, - 1.0, - 0.1, + name="Virus Check Frequency", + value=0.4, + min_value=0.0, + max_value=1.0, + step=0.1, description="Frequency the nodes check whether they are infected by a virus", ), "recovery_chance": mesa.visualization.Slider( - "Recovery Chance", - 0.3, - 0.0, - 1.0, - 0.1, + name="Recovery Chance", + value=0.3, + min_value=0.0, + max_value=1.0, + step=0.1, description="Probability that the virus will be removed", ), "gain_resistance_chance": mesa.visualization.Slider( - "Gain Resistance Chance", - 0.5, - 0.0, - 1.0, - 0.1, + name="Gain Resistance Chance", + value=0.5, + min_value=0.0, + max_value=1.0, + step=0.1, description="Probability that a recovered agent will become " "resistant to this virus in the future", ), } server = mesa.visualization.ModularServer( - VirusOnNetwork, - [network, get_resistant_susceptible_ratio, chart], - "Virus Model", - model_params, + model_cls=VirusOnNetwork, + visualization_elements=[network, get_resistant_susceptible_ratio, chart], + name="Virus on Network Model", + model_params=model_params, ) server.port = 8521 From 37db11c1aa878fcb71fc0bace155935bdfbcf00d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:59:17 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/virus_on_network/README.md | 2 +- .../virus_on_network/virus_on_network/model.py | 5 +++-- .../virus_on_network/server.py | 18 +++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/virus_on_network/README.md b/examples/virus_on_network/README.md index 6f470702..f6a51fd5 100644 --- a/examples/virus_on_network/README.md +++ b/examples/virus_on_network/README.md @@ -32,7 +32,7 @@ To run the model interactively, run ``mesa runserver`` in this directory. e.g. Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run. -or +or Directly run the file ``run.py`` in the terminal. e.g. diff --git a/examples/virus_on_network/virus_on_network/model.py b/examples/virus_on_network/virus_on_network/model.py index f886adb5..a33e7545 100644 --- a/examples/virus_on_network/virus_on_network/model.py +++ b/examples/virus_on_network/virus_on_network/model.py @@ -106,9 +106,10 @@ def run_model(self, n): class VirusAgent(mesa.Agent): - ''' + """ Individual Agent definition and its properties/interaction methods - ''' + """ + def __init__( self, unique_id, diff --git a/examples/virus_on_network/virus_on_network/server.py b/examples/virus_on_network/virus_on_network/server.py index 79be5810..afb5c195 100644 --- a/examples/virus_on_network/virus_on_network/server.py +++ b/examples/virus_on_network/virus_on_network/server.py @@ -50,10 +50,10 @@ def get_agents(source, target): network = mesa.visualization.NetworkModule( - portrayal_method=network_portrayal, - canvas_height=500, + portrayal_method=network_portrayal, + canvas_height=500, canvas_width=500, - ) +) chart = mesa.visualization.ChartModule( [ {"Label": "Infected", "Color": "#FF0000"}, @@ -83,12 +83,12 @@ def get_resistant_susceptible_ratio(model): description="Choose how many agents to include in the model", ), "avg_node_degree": mesa.visualization.Slider( - name="Avg Node Degree", - value=3, - min_value=3, - max_value=8, - step=1, - description="Avg Node Degree" + name="Avg Node Degree", + value=3, + min_value=3, + max_value=8, + step=1, + description="Avg Node Degree", ), "initial_outbreak_size": mesa.visualization.Slider( name="Initial Outbreak Size", From b2d4d589b86616dc594baf60bc8e0d2a7fadf178 Mon Sep 17 00:00:00 2001 From: Achal Jain Date: Thu, 22 Feb 2024 19:06:46 +0530 Subject: [PATCH 3/3] Update app.py --- examples/virus_on_network/app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/virus_on_network/app.py b/examples/virus_on_network/app.py index 5b9a4586..cefb0059 100644 --- a/examples/virus_on_network/app.py +++ b/examples/virus_on_network/app.py @@ -1,4 +1,5 @@ import math + import solara from matplotlib.figure import Figure from matplotlib.ticker import MaxNLocator