1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 23:42:29 +00:00
soil/docs/tutorial/soil_tutorial.ipynb

2278 lines
240 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T12:41:48.007238Z",
"start_time": "2017-10-19T14:41:47.980725+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"# Soil Tutorial"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T16:44:14.120953Z",
"start_time": "2017-07-02T18:44:14.117152+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
2023-04-20 15:56:44 +00:00
"collapsed": true,
"hideCode": false,
"hidePrompt": false
},
"source": [
"This notebook is an introduction to the soil agent-based social network simulation framework.\n",
"It will focus on a specific use case: studying the propagation of disinformation through TV and social networks.\n",
"\n",
"\n",
"The steps we will follow are:\n",
"\n",
"* Cover some basics about simulations in Soil (environments, agents, etc.)\n",
"* Simulate a basic scenario with a single agent\n",
"* Add more complexity to our scenario\n",
"* Running the simulation using different configurations\n",
"* Analysing the results of each simulation\n",
"\n",
"The simulations in this tutorial will be kept simple, for the sake of clarity.\n",
"However, they provide all the building blocks necessary to model, run and analyse more complex scenarios."
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:38:48.052876Z",
"start_time": "2017-07-03T15:38:48.044762+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"But before that, let's import the soil module and networkx."
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:13.451481Z",
"start_time": "2017-11-03T11:58:12.643469+01:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
2023-03-23 13:49:09 +00:00
"outputs": [],
"source": [
"from soil import *\n",
2023-05-03 10:14:49 +00:00
"from soil import analysis\n",
"import networkx as nx\n",
"\n",
2023-04-20 15:56:44 +00:00
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:41:19.788717Z",
"start_time": "2017-07-03T15:41:19.785448+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Basic concepts"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"There are two main elements in a soil simulation:\n",
" \n",
"* The **environment** or model. It assigns agents to nodes in the network, and stores the environment parameters (shared state for all agents).\n",
2023-04-24 17:23:04 +00:00
" - `soil.NetworkEnvironment` models also contain a network topology (accessible through through `self.G`). A simulation may use an existing NetworkX topology, or generate one on the fly. The `NetworkEnvironment` class is parameterized, which makes it easy to initialize environments with a variety of network topologies. **In this tutorial, we will manually add a network to each environment**.\n",
"* One or more **agents**. Agents are programmed with their individual behaviors, and they can communicate with the environment and with other agents. There are several types of agents, depending on their behavior and their capabilities. Some examples of built-in types of agents are:\n",
2023-04-24 17:23:04 +00:00
" - Network agents, which are linked to a node in the topology. They have additional methods to access their neighbors.\n",
" - FSM (Finite state machine) agents. Their behavior is defined in terms of states, and an agent will move from one state to another.\n",
" - Evented agents, an actor-based model of agents, which can communicate with one another through message passing.\n",
" - For convenience, a general `soil.Agent` class is provided, which inherits from Network, FSM and Evented at the same time.\n",
"\n",
"Soil provides several abstractions over events to make developing agents easier.\n",
2023-04-20 15:56:44 +00:00
"This means you can use events (timeouts, delays) in soil, but for the most part we will assume your models will be step-based o.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T15:55:12.933978Z",
"start_time": "2017-07-02T17:55:12.930860+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Modeling behaviour"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:49:31.269687Z",
"start_time": "2017-07-03T15:49:31.257850+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Our first step will be to model how every person in the social network reacts to hearing a piece of disinformation (news).\n",
"We will follow a very simple model based on a finite state machine.\n",
"\n",
2023-04-20 15:56:44 +00:00
"A person may be in one of two states: **neutral** (the default state) and **infected**.\n",
"A neutral person may hear about a piece of disinformation either on the TV (with probability **prob_tv_spread**) or through their friends.\n",
"Once a person has heard the news, they will spread it to their friends (with a probability **prob_neighbor_spread**).\n",
2023-04-20 15:56:44 +00:00
"Some users do not have a TV, so they will only be infected by their friends.\n",
"\n",
"The spreading probabilities will change over time due to different factors.\n",
2023-04-20 15:56:44 +00:00
"We will represent this variance using an additional agent which will not be a part of the social network."
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"### Modelling Agents\n",
"\n",
"The following sections will cover the basics of developing agents in SOIL.\n",
"\n",
"For more advanced patterns, please check the **examples** folder in the repository."
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"#### Basic agents"
]
},
{
2023-05-03 10:14:49 +00:00
"attachments": {},
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"The most basic agent in Soil is ``soil.BaseAgent``.\n",
"These agents implement their behavior by overriding the `step` method, which will be run in every simulation step.\n",
"Only one agent will be running at any given time, and it will be doing so until the `step` function returns.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Agents can access their environment through their ``self.model`` attribute.\n",
"This is most commonly used to get access to the environment parameters and methods.\n",
"Here is a simple example of an agent:\n",
"\n",
2023-04-20 15:56:44 +00:00
"\n",
"```python\n",
"class ExampleAgent(BaseAgent):\n",
" def init(self):\n",
" self.is_infected = False\n",
" self.steps_neutral = 0\n",
" \n",
" def step(self):\n",
" # Implement agent logic\n",
" if self.is_infected:\n",
" ... # Do something, like infecting other agents\n",
" return self.die(\"No need to do anything else\") # Stop forever\n",
" else:\n",
" ... # Do something\n",
" self.steps_neutral += 1\n",
" if self.steps_neutral > self.model.max_steps_neutral:\n",
" self.is_infected = True\n",
"```\n",
"\n",
"\n",
"\n",
2023-04-20 15:56:44 +00:00
"Any kind of agent behavior can be implemented with this `step` function.\n",
2023-05-03 10:14:49 +00:00
"dead, it has two main drawbacks: 1) complex behaviors can get difficult both write and understand; 2) these behaviors are not composable."
2023-04-20 15:56:44 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### FSM agents\n",
"\n",
"One way to solve both issues is to model agents as **[Finite-state Machines](https://en.wikipedia.org/wiki/Finite-state_machine)** (FSM, for short).\n",
"FSM define a series of possible states for the agent, and changes between these states.\n",
"These states can be modelled and extended independently.\n",
"\n",
2023-04-20 15:56:44 +00:00
"This is modelled in Soil through the `soil.FSM` class.\n",
"Agents that inherit from ``soil.FSM`` do not need to specify a ``step`` method.\n",
"Instead, we describe each finite state with a function.\n",
"To change to another state, a function may return the new state, or the ``id`` of a state.\n",
"If no state is returned, the state remains unchanged.\n",
"\n",
2023-04-20 15:56:44 +00:00
"The current state of the agent can be checked with ``agent.state_id``.\n",
"That state id can be used to look for other agents in that specific state.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Our previous example could be expressed like this:\n",
"\n",
2023-04-20 15:56:44 +00:00
"```python\n",
"class FSMExample(FSM):\n",
"\n",
2023-04-20 15:56:44 +00:00
" def init(self):\n",
" self.steps_neutral = 0\n",
" \n",
" @state(default=True)\n",
" def neutral(self):\n",
" ... # Do something\n",
" self.steps_neutral += 1\n",
" if self.steps_neutral > self.model.max_steps_neutral:\n",
" return self.infected # Change state\n",
"\n",
" @state\n",
" def infected(self):\n",
" ... # Do something\n",
" return self.die(\"No need to do anything else\")\n",
"```"
]
},
{
2023-05-03 10:14:49 +00:00
"attachments": {},
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-05-03 10:14:49 +00:00
"#### Async agents\n",
"\n",
2023-05-03 10:14:49 +00:00
"Another design pattern that can be very useful in some cases is to model each step (or a specific state) using asynchronous functions (and the `await` keyword).\n",
"Asynchronous functions will be paused on `await`, and resumed at a later step from the same point.\n",
"\n",
"The following agent will do something for `self.model.max_steps` and then stop forever.\n",
2023-04-20 15:56:44 +00:00
"\n",
"\n",
"\n",
"```python\n",
2023-05-03 10:14:49 +00:00
"class AsyncExample(BaseAgent):\n",
" async def step(self):\n",
" for i in range(self.model.max_steps):\n",
" self.do_something()\n",
" await self.delay() # Signal the scheduler that this agent is done for now\n",
2023-04-20 15:56:44 +00:00
" return self.die(\"No need to do anything else\") \n",
2023-05-03 10:14:49 +00:00
"```\n",
"\n",
"Notice that this trivial example could be implemented with a regular `step` and an attribute with the counts of times the agent has run so far.\n",
"By using an `async` we avoid complicating the logic of our function or adding spurious attributes."
2023-04-20 15:56:44 +00:00
]
},
{
2023-05-03 10:14:49 +00:00
"attachments": {},
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### Telling the scheduler when to wake up an agent\n",
"\n",
"By default, every agent will be called in every simulation step, and the time elapsed between two steps is controlled by the `interval` attribute in the environment.\n",
"\n",
2023-05-12 12:09:00 +00:00
"But agents may signal the scheduler how long to wait before calling them again by returning a value other than `None` or using `await` in asynchronous functions.\n",
2023-04-20 15:56:44 +00:00
"This is especially useful when an agent is going to be dormant for a long time.\n",
2023-05-03 10:14:49 +00:00
"There are two convenience methods to calculate the value to return: `Agent.delay`, which takes a time delay; and `Agent.at`, which takes an absolute time at which the agent should be awaken.\n",
2023-05-12 12:09:00 +00:00
"A return value of `None` will default to a wait of 1 unit of time.\n",
"Both `Agent.at` and `Agent.delay` can be awaited in async functions.\n",
2023-05-03 10:14:49 +00:00
"\n",
"When an `FSM` agent returns, it may signal two things: how long to wait, and a state to transition to.\n",
"This can be done by using the `delay` and `at` methods of each state."
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T12:22:53.931963Z",
"start_time": "2017-07-02T14:22:53.928340+02:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"source": [
"### Environment agents"
]
},
{
2023-05-03 10:14:49 +00:00
"attachments": {},
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-05-03 10:14:49 +00:00
"In our simulation, we need a way to model how TV broadcasts news, and those that have a TV are susceptible to it.\n",
"We will only model one very viral TV broadcast, which we will call an `event`, which has a high chance of infecting users with a TV.\n",
"\n",
"\n",
2023-05-03 10:14:49 +00:00
"There are several ways to model this behavior.\n",
"We will do it with an Environment Agent.\n",
"Environment agents are regular agents that interact with the environment but are invisible to other agents."
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:17.653736Z",
"start_time": "2017-11-03T11:58:17.612944+01:00"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"import logging\n",
"\n",
"class EventGenerator(BaseAgent):\n",
2023-04-20 15:56:44 +00:00
" level = logging.INFO\n",
" \n",
2023-05-03 10:14:49 +00:00
" async def step(self):\n",
2023-04-20 15:56:44 +00:00
" # Do nothing until the time of the event\n",
2023-05-03 10:14:49 +00:00
" await self.at(self.model.event_time)\n",
2023-04-20 15:56:44 +00:00
" self.info(\"TV event happened\")\n",
" self.model.prob_tv_spread = 0.5\n",
" self.model.prob_neighbor_spread *= 2\n",
" self.model.prob_neighbor_spread = min(self.model.prob_neighbor_spread, 1)\n",
2023-05-03 10:14:49 +00:00
" await self.delay()\n",
2023-04-20 15:56:44 +00:00
" self.model.prob_tv_spread = 0\n",
"\n",
" while self.alive:\n",
" self.model.prob_neighbor_spread = self.model.prob_neighbor_spread * self.model.neighbor_factor\n",
" if self.model.prob_neighbor_spread < 0.01:\n",
" return self.die(\"neighbors can no longer spread the rumour\")\n",
2023-05-03 10:14:49 +00:00
" await self.delay()"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Environment (Model)\n",
"\n",
"Let's define a environment model to test our event generator agent.\n",
"This environment will have a single agent (the event generator).\n",
"We will also tell the environment to save the value of `prob_tv_spread` after every step:"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 3,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
2023-05-12 12:09:00 +00:00
"class NewsEnvSimple(Environment):\n",
2023-04-20 15:56:44 +00:00
" \n",
" # Here we set the default parameters for our model\n",
" # We will be able to override them on a per-simulation basis\n",
2023-04-20 15:56:44 +00:00
" prob_tv_spread = 0.1\n",
" prob_neighbor_spread = 0.1\n",
" event_time = 10\n",
" neighbor_factor = 0.9\n",
"\n",
" \n",
2023-05-03 10:14:49 +00:00
" # This function initializes the model. It is run right at the end of the `__init__` function.\n",
2023-04-20 15:56:44 +00:00
" def init(self):\n",
" self.add_model_reporter(\"prob_tv_spread\")\n",
" self.add_agent(EventGenerator)"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
"Once the environment has been defined, we can quickly run our simulation through the `run` method on NewsEnv:"
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 4,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "14d3f5ae767b4e4f88363ac8a60e5fb6",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
"NewsEnvSimple: 0%| | 0/1 [00:00<?, ?configuration/s]"
]
},
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "25cf679897634ee69b9dbfe5fb2a14b4",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/1 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>step</th>\n",
" <th>agent_count</th>\n",
" <th>prob_tv_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>0.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>10.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>11.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>2</td>\n",
" <td>1</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>12.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>3</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>13.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>4</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>14.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>5</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" step agent_count prob_tv_spread\n",
"time \n",
2023-05-03 10:14:49 +00:00
"0.0 0 1 0.1\n",
"10.0 1 1 0.1\n",
"11.0 2 1 0.5\n",
"12.0 3 1 0.0\n",
"13.0 4 1 0.0\n",
"14.0 5 1 0.0"
]
},
2023-05-12 12:09:00 +00:00
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"it = NewsEnvSimple.run(iterations=1, max_time=14)\n",
"\n",
2023-04-20 15:56:44 +00:00
"it[0].model_df()"
]
},
{
2023-05-03 10:14:49 +00:00
"attachments": {},
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-05-03 10:14:49 +00:00
"As we can see, the event occurred right after `t=10`, so by `t=11` the value of `prob_tv_spread` was already set to `0.5`.\n",
"\n",
2023-04-20 15:56:44 +00:00
"You may notice nothing happened between `t=0` and `t=1`.\n",
"That is because there aren't any other agents in the simulation, and our event generator explicitly waited until `t=10`."
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Network agents"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
},
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"In our disinformation scenario, we will model our agents as a FSM with two states: ``neutral`` (default) and ``infected``.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Here's the code:"
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 5,
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-11-03T10:58:16.051690Z",
"start_time": "2017-11-03T11:58:16.006044+01:00"
},
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
2023-05-19 14:19:50 +00:00
"class Viewer(Agent):\n",
2023-04-20 15:56:44 +00:00
" has_tv = False\n",
" infected_by_friends = False\n",
" \n",
2023-05-03 10:14:49 +00:00
" # The state decorator is used to define the states of the agent\n",
" @state(default=True)\n",
2023-04-20 15:56:44 +00:00
" def neutral(self):\n",
2023-05-03 10:14:49 +00:00
" # The agent might have been infected by their infected friends since the last time they were checked\n",
2023-04-20 15:56:44 +00:00
" if self.infected_by_friends:\n",
2023-05-03 10:14:49 +00:00
" # Automatically transition to the infected state\n",
2023-04-20 15:56:44 +00:00
" return self.infected\n",
2023-05-03 10:14:49 +00:00
" # If the agent has a TV, they might be infected by the evenn\n",
2023-04-20 15:56:44 +00:00
" if self.has_tv:\n",
" if self.prob(self.model.prob_tv_spread):\n",
" return self.infected\n",
" \n",
" @state\n",
2023-04-20 15:56:44 +00:00
" def infected(self):\n",
" for neighbor in self.iter_neighbors(state_id=self.neutral.id):\n",
" if self.prob(self.model.prob_neighbor_spread):\n",
2023-05-03 10:14:49 +00:00
" neighbor.infected_by_friends = True\n",
" return self.delay(7) # Wait for 7 days before trying to infect their friends again"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
"We can check that our states are well defined:"
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 6,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [
{
"data": {
"text/plain": [
"['dead', 'neutral', 'infected']"
]
},
2023-05-12 12:09:00 +00:00
"execution_count": 6,
2023-04-20 15:56:44 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-05-19 14:19:50 +00:00
"Viewer.states()"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Environment (Model)"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Let's modify our simple simulation.\n",
"We will add a network of agents of type NewsSpread.\n",
"\n",
2023-04-20 15:56:44 +00:00
"Only one agent (0) will have a TV (in blue)."
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 7,
"metadata": {
"cell_style": "split",
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
2023-04-20 15:56:44 +00:00
"data": {
2023-05-19 14:19:50 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAeTklEQVR4nO3df4xV9cHn8fcdrgzK8ENUtAhFhmkogyKKrtZUBCO0YrU1+2z7x7q6ERDIav/YZPfJKs1u8kie3c0mm1WzgAzpj9hs2t0ntrUOFlrB2pB2pawgDFIYRgURaQEZBp2By9z948z48Exn7g+4Z7733PN+JWQ495zzPR/4537m/PieTD6fzyNJklKtLnQASZIUnoVAkiRZCCRJkoVAkiRhIZAkSVgIJEkSFgJJkgRkS9mot7eXI0eOMGbMGDKZTNyZJElSBeTzeU6fPs2kSZOoqyt8DqCkQnDkyBGmTJlSkXCSJGl4HTp0iMmTJxfcpqRCMGbMmM8HHDt27KUnkyRJsevs7GTKlCmff48XUlIh6L9MMHbsWAuBJEkJU8rlfm8qlCRJFgJJkmQhkCRJWAgkSRIWAkmShIVAkiRhIZAkSVgIJEkSFgJJkoSFQJIkYSGQJElYCCRJEhYCSZJEiW871ABdXXDgAPT0QH09NDVBQ0PoVJIkXTQLQana2mDtWmhthYMHIZ//x3WZDDQ2wuLFsGIFNDeHyylJ0kXwkkExHR2waBHMmgVr1kB7+z8tAxAtt7dH62fNirbv6AiTV5Kki2AhKKSlJfptf8uWaDmXK7x9//otW6L9WlrizSdJUoVYCIayejUsWwbd3cWLwEC5XLTfsmXROJIkVTkLwWBaWmDVqsqMtWoVbNhQmbEkSYqJhWCgjg546qlBV/0cmAxcBmSI/vPGAd8rNuaTT3pPgSSpqlkIBlq+fMhLBG8D3cBXgX8FfKvv82eBRwqNmctF40qSVKUy+fzAW+b/WmdnJ+PGjePUqVOMHTt2OHKF0d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xUb88knvadAkpR46SoEy5cPeYngGWAPMAN4FJgPHAG+DrxcaMxcLhpXkqQES08haGuDzZuHLATfA04BO4nOFvwaeK1v3b8tNG4uF427d28Fw0qSNLzSUwjWroUC7yRYDjQM+GwhcDnwcbGxs9nosURJkhIqPYWgtbXsJwp6gR7gimIb5nKwceNFBpMkKbx0FILTp6MZCMv0b4hKwYOlbNzeHk17LElSAqWjEAwyA2ExrcBaossI60rZIZ+P3oEgSVICpaMQ9PSUtfku4JvACOANYGRMx5EkqVqk4+VG9fUlb/oBcCdwHvgZcGtMx5EkqZqk4wxBUxNkMkU3+wS4CfiM6HLBQ+UcI5OJjiNJUgKloxA0NEBjY8FNzgIzgU7gPwFPlHuM6dOj40iSlEDpKAQAixcXnIfgDuAocG3fz5UD/hSUzcL991cmpyRJAWTy+eK333d2djJu3DhOnTrF2LFjhyNX5bW1waxZQ64eTzRT4VCK/ie1tcHMmeXnkiQpJuV8f6fnDEFzMyxcOORZgk+IvvSH+jOkbDYa1zIgSUqw9BQCgHXrCl42uCjZbDSuJEkJlq5CMG0aPP98Zcd84YVoXEmSEixdhQBg6VJ49tnKjLV6NSxZUpmxJEkKKH2FAOCZZ2D9ehg1qvxLCNlstF9LCzz9dDz5JEkaZuksBBCdKWhrgwULouVixaB//YIF0X6eGZAk1ZD0FgKIrv1v2gR79sDKlbw/ciS9A7fpn4Fw5cqoCGza5D0DkqSak555CEowbtw4vjBmDO/+8pfRi4rq66My4AyEkqQEKuf7Ox0vNypBb28vnZ2d3HnnnTBnTug4kiQNq3RfMrjAtm3bALjrrrsCJ5EkafhZCPq0trYC8MADDwROIknS8LM
2023-04-20 15:56:44 +00:00
"text/plain": [
2023-05-03 10:14:49 +00:00
"<Figure size 640x480 with 1 Axes>"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"def generate_simple():\n",
" G = nx.Graph()\n",
" G.add_edge(0, 1)\n",
" G.add_edge(0, 2)\n",
" G.add_edge(2, 3)\n",
" G.add_node(4)\n",
" return G\n",
"\n",
2023-04-20 15:56:44 +00:00
"G = generate_simple()\n",
"pos = nx.spring_layout(G)\n",
"nx.draw_networkx(G, pos, node_color='red')\n",
"nx.draw_networkx(G, pos, nodelist=[0], node_color='blue')"
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 8,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-03-23 13:49:09 +00:00
"outputs": [],
"source": [
"class NewsEnvNetwork(Environment):\n",
2023-04-20 15:56:44 +00:00
" \n",
" prob_tv_spread = 0\n",
" prob_neighbor_spread = 0.1\n",
" event_time = 10\n",
" neighbor_factor = 0.9\n",
"\n",
2023-04-20 15:56:44 +00:00
" \n",
" def init(self):\n",
" self.add_agent(EventGenerator)\n",
" self.G = generate_simple()\n",
2023-05-19 14:19:50 +00:00
" self.populate_network(Viewer)\n",
2023-04-20 15:56:44 +00:00
" self.agent(node_id=0).has_tv = True\n",
" self.add_model_reporter('prob_tv_spread')\n",
" self.add_model_reporter('prob_neighbor_spread')"
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 9,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
2023-04-20 15:56:44 +00:00
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "27211fdd070a4f768905e5d2187e79a3",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
"NewsEnvNetwork: 0%| | 0/1 [00:00<?, ?configuration/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "5c601a54aed4437c9a0f394e1f7cbc70",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/1 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
2023-04-20 15:56:44 +00:00
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
2023-04-20 15:56:44 +00:00
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>step</th>\n",
" <th>agent_count</th>\n",
" <th>prob_tv_spread</th>\n",
2023-04-20 15:56:44 +00:00
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
2023-04-20 15:56:44 +00:00
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>0.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>0</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>1.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>1</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>2.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>2</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>3.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>3</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>4.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>4</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>5.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>5</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>6.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>7.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>7</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>8.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>8</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>9.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>9</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>10.0</th>\n",
" <td>10</td>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.100000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>11.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>11</td>\n",
" <td>6</td>\n",
" <td>0.5</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.200000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>12.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>12</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.180000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>13.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>13</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.162000</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>14.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>14</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.145800</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>15.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>15</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.131220</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>16.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>16</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.118098</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>17.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>17</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.106288</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>18.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>18</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.095659</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>19.0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>19</td>\n",
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.086093</td>\n",
" </tr>\n",
" <tr>\n",
2023-05-03 10:14:49 +00:00
" <th>20.0</th>\n",
" <td>20</td>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>0.0</td>\n",
" <td>0.077484</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-04-20 15:56:44 +00:00
" step agent_count prob_tv_spread prob_neighbor_spread\n",
"time \n",
2023-05-03 10:14:49 +00:00
"0.0 0 6 0.0 0.100000\n",
"1.0 1 6 0.0 0.100000\n",
"2.0 2 6 0.0 0.100000\n",
"3.0 3 6 0.0 0.100000\n",
"4.0 4 6 0.0 0.100000\n",
"5.0 5 6 0.0 0.100000\n",
"6.0 6 6 0.0 0.100000\n",
"7.0 7 6 0.0 0.100000\n",
"8.0 8 6 0.0 0.100000\n",
"9.0 9 6 0.0 0.100000\n",
"10.0 10 6 0.0 0.100000\n",
"11.0 11 6 0.5 0.200000\n",
"12.0 12 6 0.0 0.180000\n",
"13.0 13 6 0.0 0.162000\n",
"14.0 14 6 0.0 0.145800\n",
"15.0 15 6 0.0 0.131220\n",
"16.0 16 6 0.0 0.118098\n",
"17.0 17 6 0.0 0.106288\n",
"18.0 18 6 0.0 0.095659\n",
"19.0 19 6 0.0 0.086093\n",
"20.0 20 6 0.0 0.077484"
]
},
2023-05-12 12:09:00 +00:00
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"it = NewsEnvNetwork.run(max_time=20)\n",
2023-04-20 15:56:44 +00:00
"it[0].model_df()"
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"In this case, notice that the inclusion of other agents (which run every step) means that the simulation did not skip to `t=10`.\n",
"\n",
"Now, let's look at the state of our agents in every step:"
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 10,
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
2023-05-03 10:14:49 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"No agent dataframe provided and no agent reporters found. Skipping agent plot.\n"
]
},
2023-04-20 15:56:44 +00:00
{
"data": {
2023-05-03 10:14:49 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGwCAYAAAB7MGXBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAABTfUlEQVR4nO3deVxU5eI/8M/MADMgm4qCKIoLKq4oKGKZVuRSN9PqG3m9uWRWKjcLvZV11dR+F8u1xatlqZV2NW9pXS1NSawUM0Fv5lYaiqagdmWTZWDm+f1xnIFRlhmY4ZwzfN6v13nlzJzlORxpPj6rRgghQERERCQTrdwFICIiosaNYYSIiIhkxTBCREREsmIYISIiIlkxjBAREZGsGEaIiIhIVgwjREREJCsPuQtgD7PZjIsXL8LPzw8ajUbu4hAREZEdhBAoKChAaGgotNrq6z9UEUYuXryIsLAwuYtBREREdXD+/Hm0adOm2s9VEUb8/PwASDfj7+8vc2mIiIjIHvn5+QgLC7N+j1dHFWHE0jTj7+/PMEJERKQytXWxYAdWIiIikhXDCBEREcmKYYSIiIhkpYo+I0REDc1sNsNoNMpdDCJF8/T0hE6nq/d5GEaIiG5iNBqRmZkJs9ksd1GIFC8wMBAhISH1mgeMYYSIqBIhBC5dugSdToewsLAaJ2oiasyEECgqKsLly5cBAK1atarzuRhGiIgqKS8vR1FREUJDQ+Hj4yN3cYgUzdvbGwBw+fJltGzZss5NNoz8RESVmEwmAICXl5fMJSFSB0toLysrq/M5GEaIiKrAdbCI7OOM3xWGESIiIpJVncLIihU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Ldd9+NadOmYfHixQ6VofLPorBQ6uS8fft2tG7d2mY/Sy39xo0bMXPmTCxZsgRxcXHw8/PDokWL8MMPPzh0Xaqaw0N7ExISrO2OUVFROHLkCHbs2GHt1JqVlYVLly5Z97927RomT56MyMhI3HvvvcjPz8f+/fvRrVs3590FEVElzgwjanLzF+OBAwcQERFR7eJlkZGR2Ldvn817+/btQ+fOnW2OOXDgwC3njYyMtKtMnp6e1pqZysaOHYtNmzYhLS0Nv/32Gx599FG7zgdIzT/jx4/H+vXrsXz5crz77ru3lM+ivLwc6enpNZa3W7du0Ov1yMrKQqdOnWw2S3/Fffv2YeDAgZg6dSr69OmDTp064cyZM9ZzBAQEoFWrVjbPwHJtql2d4r6lx3ZVUlNTbV4vW7YMy5Ytq8tliIjqpLGGkaysLCQlJeGpp55CRkYG3nrrrSpHx1jMmDED/fr1w4IFC5CQkIC0tDS8/fbb+Oc//2mz3759+/D6669j1KhR2LVrFzZv3ozt27fbVabw8HCkpKTgtttug16vR9OmTQEADz74IKZMmYIpU6bgzjvvRGhoqF3nmzNnDqKjo9G9e3eUlpZi27ZttwSNFStWICIiApGRkVi2bBmuXbuGxx9/vNpz+vn5YebMmXjuuedgNptx++23Iy8vD/v27YO/vz/Gjx+PiIgIfPjhh9i5cyfat2+Pjz76CD/++CPat29vPc/06dOxcOFCREREoGvXrli6dClyc3Ptuq/Gzj3qHomIKsmvFEDyG1EYGTduHIqLi9G/f3/odDpMnz7dOoS3Kn379sUnn3yCOXPmYMGCBWjVqhXmz59v03kVkELLoUOHMG/ePPj7+2Pp0qUYNsy+fjRLlixBUlISVq9ejdatW+Ps2bMApABw//3345NPPqlx0sybeXl5YdasWTh79iy8vb0xaNAgbNy40WafhQsXYuHChThy5Ag6deqEL774AkFBQTWed8GCBWjRogWSk5Px22+/ITAwEH379sVLL70EAHjqqadw+PBhJCQkQKP
2023-04-20 15:56:44 +00:00
"text/plain": [
2023-05-03 10:14:49 +00:00
"<Figure size 640x480 with 1 Axes>"
2023-04-20 15:56:44 +00:00
]
},
2023-05-03 10:14:49 +00:00
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
}
],
"source": [
"analysis.plot(it[0])"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"deletable": false,
"editable": false,
"hideCode": false,
"hidePrompt": false,
"run_control": {
"frozen": true
}
},
"source": [
2023-04-20 15:56:44 +00:00
"## Running in more scenarios\n",
"\n",
2023-04-20 15:56:44 +00:00
"In real life, you probably want to run several simulations, varying some of the parameters so that you can compare and answer your research questions.\n",
"\n",
2023-04-20 15:56:44 +00:00
"For instance:\n",
" \n",
"* Does the outcome depend on the structure of our network? We will use different generation algorithms to compare them (Barabasi-Albert and Erdos-Renyi)\n",
"* How does neighbor spreading probability affect my simulation? We will try probability values in the range of [0, 0.4], in intervals of 0.1."
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 11,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
"class NewsEnvComplete(Environment):\n",
2023-05-03 10:14:49 +00:00
" prob_tv = 0.1\n",
2023-04-20 15:56:44 +00:00
" prob_tv_spread = 0\n",
2023-05-03 10:14:49 +00:00
" prob_neighbor_spread = 0.1\n",
2023-04-20 15:56:44 +00:00
" event_time = 10\n",
" neighbor_factor = 0.5\n",
" generator = \"erdos_renyi_graph\"\n",
" n = 100\n",
"\n",
" def init(self):\n",
" self.add_agent(EventGenerator)\n",
2023-05-03 10:14:49 +00:00
" opts = {\"n\": self.n}\n",
" if self.generator == \"erdos_renyi_graph\":\n",
" opts[\"p\"] = 0.05\n",
" elif self.generator == \"barabasi_albert_graph\":\n",
" opts[\"m\"] = 2\n",
" self.create_network(generator=self.generator, **opts)\n",
2023-04-20 15:56:44 +00:00
"\n",
2023-05-19 14:19:50 +00:00
" self.populate_network([Viewer,\n",
" Viewer.w(has_tv=True)], # Part of the population has a TV\n",
2023-04-20 15:56:44 +00:00
" [1-self.prob_tv, self.prob_tv])\n",
" self.add_model_reporter('prob_tv_spread')\n",
" self.add_model_reporter('prob_neighbor_spread')\n",
2023-05-19 14:19:50 +00:00
" self.add_agent_reporter(\"has_tv\")\n",
2023-05-03 10:14:49 +00:00
" self.add_agent_reporter('state_id', lambda a: getattr(a, \"state_id\", None))"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"This time, we set `dump=True` because we want to store our results to a database, so that we can later analyze them.\n",
"\n",
"But since we do not care about existing results in the database, we will also set`overwrite=True`."
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 12,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
2023-04-20 15:56:44 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-05-19 14:19:50 +00:00
"[INFO ][12:53:35] Output directory: /mnt/data/home/j/git/lab.gsi/soil/soil/docs/tutorial/soil_output\n"
2023-04-20 15:56:44 +00:00
]
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "7b3f78c10bbf4e6cb3e22c7f8dd57915",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
"newspread: 0%| | 0/10 [00:00<?, ?configuration/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = erdos_renyi_graph\n",
"prob_neighbor_spread = 0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "e7564141ee0544e380424251250fbcd4",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = erdos_renyi_graph\n",
"prob_neighbor_spread = 0.25\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "45750d718d9040799fa4e661a87da5da",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = erdos_renyi_graph\n",
"prob_neighbor_spread = 0.5\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "faaebcb11afe4788a1120ba2732fd0ee",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = erdos_renyi_graph\n",
"prob_neighbor_spread = 0.75\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "ee88cccc76fd44729b52abb4aee20f07",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = erdos_renyi_graph\n",
"prob_neighbor_spread = 1.0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "32dfb0a600f04bae9cc1fcbd99793cd1",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = barabasi_albert_graph\n",
"prob_neighbor_spread = 0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "d20c600aba3143ee91408f54af0d82dd",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = barabasi_albert_graph\n",
"prob_neighbor_spread = 0.25\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "06bb40219baf496f996ce51f0d9cf2e5",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
]
},
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = barabasi_albert_graph\n",
"prob_neighbor_spread = 0.5\n"
]
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "3e8c135a383f41beba10992aaa384fab",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
]
},
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = barabasi_albert_graph\n",
"prob_neighbor_spread = 0.75\n"
]
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "53078ae627a54c729ccffd2ecc189b17",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 100\n",
"generator = barabasi_albert_graph\n",
"prob_neighbor_spread = 1.0\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-05-19 14:19:50 +00:00
"model_id": "a8abd731892148e0b88c25717727f9df",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-05-03 10:14:49 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
]
},
2023-03-23 13:49:09 +00:00
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"N = 100\n",
"probabilities = [0, 0.25, 0.5, 0.75, 1.0]\n",
"generators = [\"erdos_renyi_graph\", \"barabasi_albert_graph\"]\n",
"\n",
"\n",
"it = NewsEnvComplete.run(name=f\"newspread\",\n",
" iterations=5, max_time=30, dump=True, overwrite=True,\n",
" matrix=dict(n=[N], generator=generators, prob_neighbor_spread=probabilities))"
2023-04-20 15:56:44 +00:00
]
},
{
"cell_type": "code",
2023-05-12 12:09:00 +00:00
"execution_count": 13,
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
"DEFAULT_ITERATIONS = 5\n",
"assert len(it) == len(probabilities) * len(generators) * DEFAULT_ITERATIONS"
]
},
2023-05-19 14:19:50 +00:00
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False 2821\n",
"True 279\n",
"Name: has_tv, dtype: int64"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"it[0].agent_df().has_tv.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"neutral 3000\n",
"infected 100\n",
"Name: state_id, dtype: int64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"it[0].agent_df().state_id.value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-07-03T11:05:18.043194Z",
"start_time": "2017-07-03T13:05:18.034699+02:00"
},
"cell_style": "center",
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"The results are conveniently stored in sqlite (history of agent and environment state) and the configuration is saved in a YAML file.\n",
"\n",
"You can also export the results to GEXF format (dynamic network) and CSV using .`run(dump=['gexf', 'csv'])` or the command line flags `--graph --csv`."
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 16,
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-11-01T14:05:56.404540Z",
"start_time": "2017-11-01T15:05:56.122876+01:00"
},
"cell_style": "split",
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
2023-04-20 15:56:44 +00:00
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[01;34msoil_output\u001b[00m\n",
"└── \u001b[01;34mnewspread\u001b[00m\n",
" └── newspread.sqlite\n",
"\n",
2023-05-12 12:09:00 +00:00
"1 directory, 1 file\n",
2023-05-19 14:19:50 +00:00
"4.6M\tsoil_output/newspread\n"
2023-04-20 15:56:44 +00:00
]
}
],
"source": [
2023-04-20 15:56:44 +00:00
"!tree soil_output\n",
"!du -xh soil_output/*"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-07-02T10:40:14.384177Z",
"start_time": "2017-07-02T12:40:14.381885+02:00"
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"### Analysing the results"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### Loading data"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Once the simulations are over, we can use soil to analyse the results.\n",
"\n",
"There are two main ways: directly using the iterations returned by the `run` method, or loading up data from the results database.\n",
"This is particularly useful to store data between sessions, and to accumulate results over multiple runs.\n",
"\n",
"The mainThe main method to load data from the database is `read_sql`, which can be used in two ways:\n",
"\n",
"* `analysis.read_sql(<sqlite_file>)` to load all the results from a sqlite database . e.g. `read_sql('my_simulation/file.db.sqlite')`\n",
"* `analysis.read_sql(name=<simulation name>)` will look for the default path for a simulation named `<simulation name>`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"The result in both cases is a named tuple with four dataframes:\n",
"\n",
"* `configuration`, which contains configuration parameters per simulation\n",
"* `parameters`, which shows the parameters used **in every iteration** of every simulation\n",
"* `env`, with the data collected from the model in each iteration (as specified in `model_reporters`)\n",
"* `agents`, like `env`, but for `agent_reporters`"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-07-03T14:44:30.978223Z",
"start_time": "2017-07-03T16:44:30.971952+02:00"
},
"hideCode": false,
"hidePrompt": false
},
"source": [
2023-04-20 15:56:44 +00:00
"Let's see it in action by loading the stored results into a pandas dataframe:"
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 17,
"metadata": {
"ExecuteTime": {
2023-04-20 15:56:44 +00:00
"end_time": "2017-10-19T15:57:44.101253Z",
"start_time": "2017-10-19T17:57:44.039710+02:00"
},
"hideCode": false,
"hidePrompt": false
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
"res = analysis.read_sql(name=\"newspread\", include_agents=True)"
2023-04-20 15:56:44 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting data\n",
"\n",
"Once we have loaded the results from the file, we can use them just like any other dataframe.\n",
"\n",
"Here is an example of plotting the ratio of infected users in each of our simulations:"
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 18,
2023-04-20 15:56:44 +00:00
"metadata": {},
"outputs": [
{
"data": {
2023-05-03 10:14:49 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAHHCAYAAABtF1i4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAC/rUlEQVR4nOzdd1xT5/7A8U9ISBhhi4II4sCNCxe0dVQsjvJTa+1Vb0VbFxZ3bUW7rFixVXG0Wruuq9bbal23al11XBdeZ22luEBaFVBkyCbJ+f0ROTUyowzH83698tJz8j3P+Z4QyJNnHYUkSRKCIAiCIAjVxKK6ExAEQRAE4ekmKiOCIAiCIFQrURkRBEEQBKFaicqIIAiCIAjVSlRGBEEQBEGoVqIyIgiCIAhCtRKVEUEQBEEQqpWojAiCIAiCUK1EZUQQBEEQhGolKiOCYCZvb29efPHFCitv//79KBQKNmzYUGbs8OHD8fb2NtmnUCiYOXOmvL1y5UoUCgXx8fEVlmNJkpKSePnll3FxcUGhULBo0aJKP2dVGz58OFqttrrTMFtmZiYjR47Ezc0NhULBpEmTiI+PR6FQsHLlyupO76Hc/54XHn+iMvIUK/zQUigUHDp0qMjzkiTh6emJQqGo0A9foeotW7asUj6AJk+ezM6dO5k+fTpr1qyhZ8+eFX4O4c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WLQICApAkidOnT5t9Dr1ez65du+jXrx/169eX97u7uzNkyBAOHTpUpOt
"text/plain": [
2023-05-03 10:14:49 +00:00
"<Figure size 640x480 with 1 Axes>"
]
},
2023-05-03 10:14:49 +00:00
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"for (g, group) in res.env.dropna().groupby(\"params_id\"):\n",
" params = res.parameters.query(f'params_id == \"{g}\"').iloc[0]\n",
" title = f\"{params.generator.rstrip('_graph')} {params.prob_neighbor_spread}\"\n",
" prob = group.groupby(by=[\"step\"]).prob_neighbor_spread.mean()\n",
" line = \"-\"\n",
" if \"barabasi\" in params.generator:\n",
" line = \"--\"\n",
" prob.rename(title).fillna(0).plot(linestyle=line)\n",
"plt.title(\"Mean probability for each configuration\")\n",
"plt.legend();"
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"neutral 136410\n",
"infected 18590\n",
"Name: state_id, dtype: int64"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res.agents.state_id.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"data": {
2023-05-19 14:19:50 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiwAAAHHCAYAAACcHAM1AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAADQ0klEQVR4nOzdd3gU1frA8e/2JJtNbyQEQm9SJBRBpEgwICDNAvcqIKDiBaSI0iw0DYg0RcV2AVF/Kl4L94KIIIgUQTpSQpEQgSTUJGzK1vn9EVlY0iEV3s/z7GN25p0z7yzCvjlz5hyVoigKQgghhBAVmLq8ExBCCCGEKIwULEII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xz00UcMGDCAp556iitXrvDxxx8TGxvLjh07aNasGcHBwbz33ns8++yz9OnTh759+wLQpEmTfM81bNgwli1bxsMPP8zzzz/P9u3biYuL4/Dhw3z77bcF5rlu3Tq6detGzZo1mTp1KllZWbz99tvce++97N69m6ioKJ555hkiIiJ4/fXXee6552jZsiWhoaHF/kwuX75M165d6du3L48++ihff/01EyZMoHHjxnTr1o327duzfPlynnjiCbp06cLAgQNdx2ZmZtKhQwfOnDnDM888Q7Vq1di6dSuTJk0iKSnJbWDu0KFDWbp0Kd26dWPYsGHY7XZ+/fVXfvvtN1q0aMHy5csZNmwYrVq14umnnwagVq1aQM6A33vuucf1xR4cHMwPP/zA0KFDSU9PZ8yYMQBkZWXRuXNnEhMTee655wgPD2f58uX8/PPPxf5cCnLw4EF69OhBkyZNmD59OgaDgePHj7NlyxZXjNPp5KGHHmLz5s08/fTTNGjQgAMHDjB//nyOHj2aa6zOzz//zFdffcXIkSMJCgoiKiqKn376iQEDBtC5c2dmz54NwOHDh9myZQujR48u0WsS4pYpQohiW7JkiQIo69atU86fP6/89ddfytdff60EBwcrBoNB+euvv1yxdrtdsVgsbsdfvnxZCQ0NVYYMGeLadv78eQVQXn311Vzne/XVV5Xr/7ru3btXAZRhw4a5xY0fP14BlJ9//rnA/Js1a6aEhIQoFy9edG3bt2+folarlYEDB7q2bdiwQQGUFStWFPyBXBe7YcMG17YOHToogPLJJ5+4tlksFiUsLEzp16+f2/GAMmLECLdtM2bMUIxGo3L06FG37RMnTlQ0Go2SmJioKIqi/PzzzwqgPPfcc7nycjqdrp+NRqMyaNCgXDFDhw5VqlSpoly4cMFte//+/RVfX18lMzNTURRFWbBggQIoX331lSsmIyNDqV27dq5rz8ugQYOU6tWr59p+45/v/PnzFUA5f/58vm0tX75cUavVyq+//uq2ffHixQqgbNmyxbUNUNRqtXLw4EG32NGjRys+Pj6K3W4vMG8hKgK5JSTELYiJiSE4OJjIyEgefvhhjEYjK1eupGrVqq4YjUbjGojqdDq5dOkSd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m3rx5vP7660yZMoUNGzYQExNDrVq12LdvH507d76lwlWv19OzZ0969uyJ0+nkX//6F++//z4vv/xyrh48IcqT3BISogR17NiRVq1asWDBArKzs4FrPQzX9yhs376dbdu2uR3r5eUFkOcX2o0efPBBgFzT11/9bbt79+75HlulShWaNWvGsmXL3M71xx9/sHbtWlfbFcGjjz7Ktm3b+PHHH3PtS01NxW63A9CvXz8URXFNvHe96z93o9GY6/PVaDT069eP//znP/zxxx+5jj9//rzr5wcffJCzZ8/y9ddfu7ZlZmbywQcfFOl6atWqRVpaGvv373dtS0pKyvVU16VLl3Id26xZMwDXI8uPPvo
"text/plain": [
2023-05-03 10:14:49 +00:00
"<Figure size 640x480 with 1 Axes>"
]
},
2023-05-03 10:14:49 +00:00
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"for (g, group) in res.agents.dropna().groupby(\"params_id\"):\n",
" params = res.parameters.query(f'params_id == \"{g}\"').iloc[0]\n",
" title = f\"{params.generator.rstrip('_graph')} {params.prob_neighbor_spread}\"\n",
2023-05-19 14:19:50 +00:00
" # counts = group.groupby(by=[\"step\", \"state_id\"]).value_counts().unstack()\n",
" counts = group.state_id.groupby(by=[\"step\"]).value_counts().unstack()\n",
2023-04-20 15:56:44 +00:00
" line = \"-\"\n",
" if \"barabasi\" in params.generator:\n",
" line = \"--\"\n",
" (counts.infected/counts.sum(axis=1)).rename(title).fillna(0).plot(linestyle=line)\n",
"plt.legend()\n",
"plt.xlim([9, None]);\n",
"plt.title(\"Ratio of infected users\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"## Data format"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"### Parameters\n",
"\n",
"The `parameters` dataframe has three keys:\n",
"\n",
"* The identifier of the simulation. This will be shared by all iterations launched in the same run\n",
"* The identifier of the parameters used in the simulation. This will be shared by all iterations that have the exact same set of parameters.\n",
"* The identifier of the iteration. Each row should have a different iteration identifier\n",
"\n",
"There will be a column per each parameter passed to the environment. In this case, that's three: **generator**, **n** and **prob_neighbor_spread**."
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
2023-05-12 12:09:00 +00:00
" <th></th>\n",
" <th>index</th>\n",
2023-04-20 15:56:44 +00:00
" <th>n</th>\n",
2023-05-12 12:09:00 +00:00
" <th>generator</th>\n",
2023-04-20 15:56:44 +00:00
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>simulation_id</th>\n",
2023-05-12 12:09:00 +00:00
" <th>params_id</th>\n",
" <th>iteration_id</th>\n",
" <th></th>\n",
2023-04-20 15:56:44 +00:00
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2023-05-19 14:19:50 +00:00
" <th rowspan=\"5\" valign=\"top\">newspread_1684407215.653166</th>\n",
2023-05-12 12:09:00 +00:00
" <th rowspan=\"5\" valign=\"top\">ff1d24a</th>\n",
" <th>0</th>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>100</td>\n",
2023-05-12 12:09:00 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
2023-05-12 12:09:00 +00:00
" <th>1</th>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>100</td>\n",
2023-05-12 12:09:00 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
2023-05-12 12:09:00 +00:00
" <th>2</th>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>100</td>\n",
2023-05-12 12:09:00 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
2023-05-12 12:09:00 +00:00
" <th>3</th>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>100</td>\n",
2023-05-12 12:09:00 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
2023-05-12 12:09:00 +00:00
" <th>4</th>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>100</td>\n",
2023-05-12 12:09:00 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-05-12 12:09:00 +00:00
" index n \\\n",
"simulation_id params_id iteration_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 ff1d24a 0 0 100 \n",
2023-05-12 12:09:00 +00:00
" 1 0 100 \n",
" 2 0 100 \n",
" 3 0 100 \n",
" 4 0 100 \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-05-12 12:09:00 +00:00
" generator \\\n",
"simulation_id params_id iteration_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 ff1d24a 0 erdos_renyi_graph \n",
2023-05-12 12:09:00 +00:00
" 1 erdos_renyi_graph \n",
" 2 erdos_renyi_graph \n",
" 3 erdos_renyi_graph \n",
" 4 erdos_renyi_graph \n",
"\n",
" prob_neighbor_spread \n",
"simulation_id params_id iteration_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 ff1d24a 0 0 \n",
2023-05-12 12:09:00 +00:00
" 1 0 \n",
" 2 0 \n",
" 3 0 \n",
" 4 0 "
]
},
2023-05-19 14:19:50 +00:00
"execution_count": 21,
2023-03-23 13:49:09 +00:00
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.parameters.head()"
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
2023-03-23 13:49:09 +00:00
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"### Configuration\n",
"\n",
"This dataset is indexed by the identifier of the simulation, and there will be a column per each attribute of the simulation.\n",
"For instance, there is one for the number of processes used, another one for the path where the results were stored, etc."
2023-03-23 13:49:09 +00:00
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 22,
2023-03-23 13:49:09 +00:00
"metadata": {},
"outputs": [
{
"data": {
2023-03-23 13:49:09 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
2023-04-20 15:56:44 +00:00
" .dataframe thead th {\n",
2023-03-23 13:49:09 +00:00
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
2023-04-20 15:56:44 +00:00
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>index</th>\n",
" <th>version</th>\n",
" <th>source_file</th>\n",
" <th>name</th>\n",
" <th>description</th>\n",
" <th>group</th>\n",
" <th>backup</th>\n",
" <th>overwrite</th>\n",
" <th>dry_run</th>\n",
" <th>dump</th>\n",
2023-03-23 13:49:09 +00:00
" <th>...</th>\n",
2023-04-20 15:56:44 +00:00
" <th>num_processes</th>\n",
" <th>exporters</th>\n",
" <th>model_reporters</th>\n",
" <th>agent_reporters</th>\n",
" <th>tables</th>\n",
" <th>outdir</th>\n",
" <th>exporter_params</th>\n",
" <th>level</th>\n",
" <th>skip_test</th>\n",
" <th>debug</th>\n",
2023-03-23 13:49:09 +00:00
" </tr>\n",
" <tr>\n",
2023-04-20 15:56:44 +00:00
" <th>simulation_id</th>\n",
2023-03-23 13:49:09 +00:00
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2023-05-19 14:19:50 +00:00
" <th>newspread_1684407215.653166</th>\n",
2023-03-23 13:49:09 +00:00
" <td>0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>2</td>\n",
" <td>None</td>\n",
" <td>newspread</td>\n",
" <td></td>\n",
" <td>None</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
2023-05-12 12:09:00 +00:00
" <td>[\"&lt;class 'soil.exporters.SQLite'&gt;\"]</td>\n",
2023-04-20 15:56:44 +00:00
" <td>{}</td>\n",
" <td>{}</td>\n",
" <td>{}</td>\n",
" <td>/mnt/data/home/j/git/lab.gsi/soil/soil/docs/tu...</td>\n",
2023-04-20 15:56:44 +00:00
" <td>{}</td>\n",
" <td>20</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
2023-03-23 13:49:09 +00:00
" </tr>\n",
" </tbody>\n",
"</table>\n",
2023-05-03 10:14:49 +00:00
"<p>1 rows × 28 columns</p>\n",
2023-03-23 13:49:09 +00:00
"</div>"
],
"text/plain": [
2023-05-12 12:09:00 +00:00
" index version source_file name description \\\n",
2023-04-20 15:56:44 +00:00
"simulation_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 0 2 None newspread \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-05-12 12:09:00 +00:00
" group backup overwrite dry_run dump ... \\\n",
"simulation_id ... \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 None False True False True ... \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-05-12 12:09:00 +00:00
" num_processes \\\n",
"simulation_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 1 \n",
2023-03-23 13:49:09 +00:00
"\n",
2023-05-12 12:09:00 +00:00
" exporters \\\n",
"simulation_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 [\"<class 'soil.exporters.SQLite'>\"] \n",
2023-03-23 13:49:09 +00:00
"\n",
2023-05-12 12:09:00 +00:00
" model_reporters agent_reporters tables \\\n",
2023-04-24 16:05:07 +00:00
"simulation_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 {} {} {} \n",
2023-05-12 12:09:00 +00:00
"\n",
" outdir \\\n",
"simulation_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 /mnt/data/home/j/git/lab.gsi/soil/soil/docs/tu... \n",
2023-05-12 12:09:00 +00:00
"\n",
" exporter_params level skip_test debug \n",
"simulation_id \n",
2023-05-19 14:19:50 +00:00
"newspread_1684407215.653166 {} 20 False False \n",
2023-03-23 13:49:09 +00:00
"\n",
2023-05-03 10:14:49 +00:00
"[1 rows x 28 columns]"
]
},
2023-05-19 14:19:50 +00:00
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
2023-03-23 13:49:09 +00:00
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.config.head()"
2023-03-23 13:49:09 +00:00
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"### Model reporters\n",
"\n",
"The `env` dataframe includes the data collected from the model.\n",
"The keys in this case are the same as `parameters`, and an additional one: **step**."
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 23,
2023-04-20 15:56:44 +00:00
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>agent_count</th>\n",
" <th>time</th>\n",
" <th>prob_tv_spread</th>\n",
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>params_id</th>\n",
" <th>iteration_id</th>\n",
" <th>step</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2023-04-24 16:05:07 +00:00
" <th rowspan=\"5\" valign=\"top\">ff1d24a</th>\n",
2023-04-20 15:56:44 +00:00
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>0</th>\n",
" <td>101</td>\n",
2023-05-03 10:14:49 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>101</td>\n",
2023-05-03 10:14:49 +00:00
" <td>1.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>101</td>\n",
2023-05-03 10:14:49 +00:00
" <td>2.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>101</td>\n",
2023-05-03 10:14:49 +00:00
" <td>3.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>101</td>\n",
2023-05-03 10:14:49 +00:00
" <td>4.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-05-12 12:09:00 +00:00
" agent_count time prob_tv_spread \\\n",
"params_id iteration_id step \n",
"ff1d24a 0 0 101 0.0 0.0 \n",
" 1 101 1.0 0.0 \n",
" 2 101 2.0 0.0 \n",
" 3 101 3.0 0.0 \n",
" 4 101 4.0 0.0 \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-05-12 12:09:00 +00:00
" prob_neighbor_spread \n",
"params_id iteration_id step \n",
"ff1d24a 0 0 0.0 \n",
" 1 0.0 \n",
" 2 0.0 \n",
" 3 0.0 \n",
" 4 0.0 "
]
},
2023-05-19 14:19:50 +00:00
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.env.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Agent reporters\n",
"\n",
"This dataframe reflects the data collected for all the agents in the simulation, in every step where data collection was invoked.\n",
"\n",
"The key in this dataframe is similar to the one in the `parameters` dataframe, but there will be two more keys: the `step` and the `agent_id`.\n",
"There will be a column per each agent reporter added to the model. In our case, there is only one: `state_id`."
]
},
{
"cell_type": "code",
2023-05-19 14:19:50 +00:00
"execution_count": 24,
2023-04-20 15:56:44 +00:00
"metadata": {},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
2023-05-19 14:19:50 +00:00
" <th>has_tv</th>\n",
2023-04-20 15:56:44 +00:00
" <th>state_id</th>\n",
" </tr>\n",
" <tr>\n",
" <th>params_id</th>\n",
" <th>iteration_id</th>\n",
" <th>step</th>\n",
" <th>agent_id</th>\n",
" <th></th>\n",
2023-05-19 14:19:50 +00:00
" <th></th>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2023-04-24 16:05:07 +00:00
" <th rowspan=\"5\" valign=\"top\">ff1d24a</th>\n",
2023-04-20 15:56:44 +00:00
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>0</th>\n",
" <td>None</td>\n",
2023-05-19 14:19:50 +00:00
" <td>None</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
2023-05-19 14:19:50 +00:00
" <td>False</td>\n",
2023-04-20 15:56:44 +00:00
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
2023-05-19 14:19:50 +00:00
" <td>False</td>\n",
2023-04-20 15:56:44 +00:00
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
2023-05-19 14:19:50 +00:00
" <td>False</td>\n",
2023-04-20 15:56:44 +00:00
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
2023-05-19 14:19:50 +00:00
" <td>False</td>\n",
2023-04-20 15:56:44 +00:00
" <td>neutral</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
2023-05-19 14:19:50 +00:00
" has_tv state_id\n",
"params_id iteration_id step agent_id \n",
"ff1d24a 0 0 0 None None\n",
" 1 False neutral\n",
" 2 False neutral\n",
" 3 False neutral\n",
" 4 False neutral"
]
},
2023-05-19 14:19:50 +00:00
"execution_count": 24,
2023-03-23 13:49:09 +00:00
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"res.agents.head()"
]
}
],
"metadata": {
2023-04-20 15:56:44 +00:00
"hide_code_all_hidden": false,
"kernelspec": {
2023-04-20 15:56:44 +00:00
"display_name": "Python 3 (ipykernel)",
"language": "python",
2023-04-24 16:05:07 +00:00
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-03-23 13:49:09 +00:00
"version": "3.8.10"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"navigate_num": "#000000",
"navigate_text": "#333333",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700",
"sidebar_border": "#EEEEEE",
"wrapper_background": "#FFFFFF"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "31px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_position": {
"height": "867px",
"left": "0px",
"right": "1670px",
"top": "106px",
"width": "250px"
},
"toc_section_display": "block",
"toc_window_display": false,
"widenNotebook": false
},
"vscode": {
"interpreter": {
"hash": "3581132406f7320837865a422f37590c78ed7dabfbcb5bc7771b9d116b13a5cf"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}