2017-10-18 18:28:42 +00:00
{
"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
2017-10-18 18:28:42 +00:00
},
"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
2017-10-18 18:28:42 +00:00
},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
2023-04-20 15:56:44 +00:00
"collapsed": true,
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
"This notebook is an introduction to the soil agent-based social network simulation framework.\n",
"In particular, we will focus on a specific use case: studying the propagation of news in a social network.\n",
"\n",
"The steps we will follow are:\n",
"\n",
"* Modelling the behavior of agents\n",
"* Running the simulation using different configurations\n",
"* Analysing the results of each simulation"
]
},
{
"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
2017-10-18 18:28:42 +00:00
},
"source": [
"But before that, let's import the soil module and networkx."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
2018-05-04 08:01:49 +00:00
"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
2017-10-18 18:28:42 +00:00
},
2023-03-23 13:49:09 +00:00
"outputs": [],
2017-10-18 18:28:42 +00:00
"source": [
"import soil\n",
"import networkx as nx\n",
" \n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
2023-04-20 15:56:44 +00:00
"import matplotlib.pyplot as plt"
2017-10-18 18:28:42 +00:00
]
},
{
"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
2017-10-18 18:28:42 +00:00
},
"source": [
"## Basic concepts"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
2017-10-18 18:28:42 +00:00
"source": [
"There are three main elements in a soil simulation:\n",
" \n",
2023-04-20 15:56:44 +00:00
"* The environment or model. It assigns agents to nodes in the network, and stores the environment parameters (shared state for all agents).\n",
"* The network topology. A simulation may use an existing NetworkX topology, or generate one on the fly.\n",
"* Agents. There are several types of agents, depending on their behavior and their capabilities. Some examples of built-in types of agents are:\n",
" - 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",
2017-10-18 18:28:42 +00:00
"\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"
2017-10-18 18:28:42 +00:00
]
},
{
"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
2017-10-18 18:28:42 +00:00
},
"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
2017-10-18 18:28:42 +00:00
},
"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",
2017-10-18 18:28:42 +00:00
"\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",
2017-10-18 18:28:42 +00:00
"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",
2017-10-18 18:28:42 +00:00
"\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."
2017-10-18 18:28:42 +00:00
]
},
{
"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
},
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"#### Basic agents"
2017-10-18 18:28:42 +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"
2023-04-20 15:56:44 +00:00
},
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"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",
2017-10-18 18:28:42 +00:00
"\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",
2017-10-18 18:28:42 +00:00
"\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",
2017-10-18 18:28:42 +00:00
"```\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",
"However, it has two main drawbacks: 1) complex behaviors can get difficult both write and understand; 2) these behaviors are not composable."
]
},
{
"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",
2017-10-18 18:28:42 +00:00
"\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",
2020-10-19 11:14:48 +00:00
"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",
2020-10-19 11:14:48 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"Our previous example could be expressed like this:\n",
2020-10-19 11:14:48 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"```python\n",
"class FSMExample(FSM):\n",
2017-10-18 18:28:42 +00:00
"\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",
"```"
2017-10-18 18:28:42 +00:00
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +00:00
"#### Generator-based agents\n",
2017-10-18 18:28:42 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"Another design pattern that can be very useful in some cases is to model each step (or a specific state) using generators (the `yield` keyword).\n",
"\n",
"\n",
"\n",
"```python\n",
"class GenExample(BaseAgent):\n",
" def step(self):\n",
" for i in range(self.model.max_steps_neutral):\n",
" ... # Do something\n",
" yield # Signal the scheduler that this step is done for now\n",
" ... # Do something\n",
" return self.die(\"No need to do anything else\") \n",
"```"
]
},
{
"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",
"But agents may signal the scheduler when they expect to be called again.\n",
"This is especially useful when an agent is going to be dormant for a long time.\n",
"To do so, an agent can return (or `yield`) from a `step` or a `state` a value of type `soil.When` (absolute time), `soil.Delta` (relative time) or `soil.Cond`, telling the scheduler when the agent will be ready to run again.\n",
"If it returns nothing (i.e., `None`), the agent will be ready to run at the next simulation step."
2017-10-18 18:28:42 +00:00
]
},
{
"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
2017-10-18 18:28:42 +00:00
},
"source": [
"### Environment agents"
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
2017-10-18 18:28:42 +00:00
"source": [
"Environment agents allow us to control the state of the environment.\n",
"In this case, we will use an environment agent to simulate a very viral event.\n",
"\n",
"When the event happens, the agent will modify the probability of spreading the rumor."
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 2,
2017-10-18 18:28:42 +00:00
"metadata": {
"ExecuteTime": {
2018-05-04 08:01:49 +00:00
"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
2017-10-18 18:28:42 +00:00
},
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"import logging\n",
2020-10-19 11:14:48 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"class EventGenerator(soil.BaseAgent):\n",
" level = logging.INFO\n",
" \n",
2017-10-18 18:28:42 +00:00
" def step(self):\n",
2023-04-20 15:56:44 +00:00
" # Do nothing until the time of the event\n",
" yield soil.When(self.model.event_time)\n",
" 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",
" yield\n",
" 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",
" yield"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"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:"
2017-10-18 18:28:42 +00:00
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 3,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2023-04-20 15:56:44 +00:00
"outputs": [],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"class NewsEnv(soil.NetworkEnvironment):\n",
" \n",
" prob_tv_spread = 0.1\n",
" prob_neighbor_spread = 0.1\n",
" event_time = 10\n",
" tv_factor = 0.5\n",
" neighbor_factor = 0.9\n",
"\n",
" \n",
" def init(self):\n",
" self.add_model_reporter(\"prob_tv_spread\")\n",
" self.add_agent(EventGenerator)"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +00:00
"Once the environment has been defined, we can run a simulation "
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 4,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"outputs": [
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-04-24 16:05:07 +00:00
"model_id": "9fdc8c75869c4af092737891dab50a30",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
2017-10-18 18:28:42 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
"NewsEnv: 0%| | 0/1 [00:00<?, ?configuration/s]"
2017-10-18 18:28:42 +00:00
]
},
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
2017-10-18 18:28:42 +00:00
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
2017-10-18 18:28:42 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
" 0%| | 0/1 [00:00<?, ?it/s]"
2017-10-18 18:28:42 +00:00
]
},
"metadata": {},
"output_type": "display_data"
},
2020-10-19 11:14:48 +00:00
{
"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",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-20 15:56:44 +00:00
" step agent_count prob_tv_spread\n",
"time \n",
"0 0 1 0.1\n",
"10 1 1 0.1\n",
"11 2 1 0.5\n",
"12 3 1 0.0\n",
"13 4 1 0.0\n",
"14 5 1 0.0"
2020-10-19 11:14:48 +00:00
]
},
2023-04-24 16:05:07 +00:00
"execution_count": 4,
2020-10-19 11:14:48 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-04-20 15:56:44 +00:00
"it = NewsEnv.run(iterations=1, dump=False, max_time=14)\n",
2017-10-18 18:28:42 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"it[0].model_df()"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +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 `1.0`.\n",
2017-10-18 18:28:42 +00:00
"\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`."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +00:00
"### Network agents"
2017-10-18 18:28:42 +00:00
]
},
{
"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"
2017-10-18 18:28:42 +00:00
},
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"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",
2017-10-18 18:28:42 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"Here's the code:"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 5,
2017-10-18 18:28:42 +00:00
"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"
2017-10-18 18:28:42 +00:00
},
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"outputs": [],
"source": [
2023-04-20 15:56:44 +00:00
"class NewsSpread(soil.Agent):\n",
" has_tv = False\n",
" infected_by_friends = False\n",
" \n",
" @soil.state(default=True)\n",
" def neutral(self):\n",
" if self.infected_by_friends:\n",
" return self.infected\n",
" if self.has_tv:\n",
" if self.prob(self.model.prob_tv_spread):\n",
" return self.infected\n",
" \n",
" @soil.state\n",
" 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",
" neighbor.infected_by_friends = True"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +00:00
"We can check that our states are well defined, here:"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 6,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2023-04-20 15:56:44 +00:00
"outputs": [
{
"data": {
"text/plain": [
"['dead', 'neutral', 'infected']"
]
},
2023-04-24 16:05:07 +00:00
"execution_count": 6,
2023-04-20 15:56:44 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"NewsSpread.states()"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +00:00
"### Environment (Model)"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"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",
2017-10-18 18:28:42 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"Only one agent (0) will have a TV (in blue)."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 7,
2017-10-18 18:28:42 +00:00
"metadata": {
"cell_style": "split",
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2020-10-19 11:14:48 +00:00
"outputs": [
{
2023-04-20 15:56:44 +00:00
"data": {
2023-04-24 16:05:07 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAco0lEQVR4nO3df3CU953Y8ffCAjIWEmBCKQZsgWxHkg34V3N/nGNzYzGBJD5n2k7/aGo7RhSYQj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WLUp6lFExCCRJikj+/IHm5uakRxk1g0CSpIh0dHRMuv0H8gwCSZIi8r3vfQ+YXPsP5BkEkiRFJH/+wJIlS5IeZdQMAkmSIpDNZjl16hRNTU1JjzImBoEkSRE4fPgwV69eZc2aNUmPMiYGgSRJEXjmmWcAaGtrS3iSsTEIJEmKwOHDh7nuuuu46aabkh5lTAwCSZIi8O677/L5z38+6THGzCCQJGmcfvnLX3L16lW+9KUvJT3KmBkEkiSN03e+8x1g8l2/YDCDQJKkcXrxxRepqalh2bJlSY8yZgaBJEnjNNnPHwCDQJKkcTly5AhBELB69eqkRxkXg0CSpHGY7PsP5BkEkiSNQ/78gVtuuSXpUcbFIJAkaRy6u7u59dZbkx5j3AwCSZLG6JVXXqmI8wfAIJAkaczy5w88/vjjCU8yfgaBJElj1NHRwYwZMybtJY8HMwgkSRqjEydOVMT5A2AQSJI0Jq+99hpBENDa2pr0KJEwCCRJGoOnn34aqIzzB8AgkCRpTPLnD7S0tCQ9SiQMAkmSxqCrq4vGxsakx4hMOukBJEmaDPrO9dH50hn6+65w5v3TTLsyvWLOHwCDQJKkEWV+2smeb51
2023-04-20 15:56:44 +00:00
"text/plain": [
2023-04-21 13:19:21 +00:00
"<Figure size 640x480 with 1 Axes>"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
2020-10-19 11:14:48 +00:00
}
],
2017-10-18 18:28:42 +00:00
"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",
2017-10-18 18:28:42 +00:00
"\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')"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 8,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2023-03-23 13:49:09 +00:00
"outputs": [],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"class NewsEnv(soil.NetworkEnvironment):\n",
" \n",
" prob_tv_spread = 0\n",
" prob_neighbor_spread = 0.1\n",
" event_time = 10\n",
" tv_factor = 0.5\n",
" neighbor_factor = 0.9\n",
2018-05-04 08:01:49 +00:00
"\n",
2023-04-20 15:56:44 +00:00
" \n",
" def init(self):\n",
" self.add_agent(EventGenerator)\n",
" self.G = generate_simple()\n",
" self.populate_network(NewsSpread)\n",
" self.agent(node_id=0).has_tv = True\n",
" self.add_model_reporter('prob_tv_spread')\n",
" self.add_model_reporter('prob_neighbor_spread')"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 22,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2020-10-19 11:14:48 +00:00
"outputs": [
{
2023-04-20 15:56:44 +00:00
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-04-24 16:05:07 +00:00
"model_id": "6b8425e6b67143ac83c5613346abef3c",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +00:00
"NewsEnv: 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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +00:00
" 0%| | 0/1 [00:00<?, ?it/s]"
2023-04-20 15:56:44 +00:00
]
},
"metadata": {},
"output_type": "display_data"
2020-10-19 11:14:48 +00:00
},
{
"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",
2020-10-19 11:14:48 +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>step</th>\n",
" <th>agent_count</th>\n",
2020-10-19 11:14:48 +00:00
" <th>prob_tv_spread</th>\n",
2023-04-20 15:56:44 +00:00
" <th>prob_neighbor_spread</th>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
2023-04-20 15:56:44 +00:00
" <th>time</th>\n",
2020-10-19 11:14:48 +00:00
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
2023-04-20 15:56:44 +00:00
" <td>0</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
2023-04-20 15:56:44 +00:00
" <td>1</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
2023-04-20 15:56:44 +00:00
" <td>2</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
2023-04-20 15:56:44 +00:00
" <td>3</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
2023-04-20 15:56:44 +00:00
" <td>4</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
2023-04-20 15:56:44 +00:00
" <td>5</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
2023-04-20 15:56:44 +00:00
" <td>6</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
2023-04-20 15:56:44 +00:00
" <td>7</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
2023-04-20 15:56:44 +00:00
" <td>8</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
2023-04-20 15:56:44 +00:00
" <td>9</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.0</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.100000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>10</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
2023-04-20 15:56:44 +00:00
" <td>11</td>\n",
" <td>6</td>\n",
2020-10-19 11:14:48 +00:00
" <td>0.5</td>\n",
2023-04-20 15:56:44 +00:00
" <td>0.200000</td>\n",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>12</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>13</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>14</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>15</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>16</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>17</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>18</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>19</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",
2020-10-19 11:14:48 +00:00
" </tr>\n",
" <tr>\n",
" <th>20</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",
2020-10-19 11:14:48 +00:00
" </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",
"0 0 6 0.0 0.100000\n",
"1 1 6 0.0 0.100000\n",
"2 2 6 0.0 0.100000\n",
"3 3 6 0.0 0.100000\n",
"4 4 6 0.0 0.100000\n",
"5 5 6 0.0 0.100000\n",
"6 6 6 0.0 0.100000\n",
"7 7 6 0.0 0.100000\n",
"8 8 6 0.0 0.100000\n",
"9 9 6 0.0 0.100000\n",
"10 10 6 0.0 0.100000\n",
"11 11 6 0.5 0.200000\n",
"12 12 6 0.0 0.180000\n",
"13 13 6 0.0 0.162000\n",
"14 14 6 0.0 0.145800\n",
"15 15 6 0.0 0.131220\n",
"16 16 6 0.0 0.118098\n",
"17 17 6 0.0 0.106288\n",
"18 18 6 0.0 0.095659\n",
"19 19 6 0.0 0.086093\n",
"20 20 6 0.0 0.077484"
2020-10-19 11:14:48 +00:00
]
},
2023-04-24 16:05:07 +00:00
"execution_count": 22,
2020-10-19 11:14:48 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"it = NewsEnv.run(max_time=20)\n",
"it[0].model_df()"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
"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:"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 23,
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
2023-04-24 16:05:07 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"No agent dataframe provided. Skipping agent plot.\n"
]
},
2023-04-20 15:56:44 +00:00
{
"data": {
2023-04-24 16:05:07 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGwCAYAAAB7MGXBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAABTfUlEQVR4nO3deVxU5eI/8M/MADMgm4qCKIoLKq4oKGKZVuRSN9PqG3m9uWRWKjcLvZV11dR+F8u1xatlqZV2NW9pXS1NSawUM0Fv5lYaiqagdmWTZWDm+f1xnIFRlhmY4ZwzfN6v13nlzJzlORxpPj6rRgghQERERCQTrdwFICIiosaNYYSIiIhkxTBCREREsmIYISIiIlkxjBAREZGsGEaIiIhIVgwjREREJCsPuQtgD7PZjIsXL8LPzw8ajUbu4hAREZEdhBAoKChAaGgotNrq6z9UEUYuXryIsLAwuYtBREREdXD+/Hm0adOm2s9VEUb8/PwASDfj7+8vc2mIiIjIHvn5+QgLC7N+j1dHFWHE0jTj7+/PMEJERKQytXWxYAdWIiIikhXDCBEREcmKYYSIiIhkpYo+I0REDc1sNsNoNMpdDCJF8/T0hE6nq/d5GEaIiG5iNBqRmZkJs9ksd1GIFC8wMBAhISH1m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79iy8vb0xaNAgbNy40WafhQsXYuHChThy5Ag6deqEL774AkFBQTWed8GCBWjRogWSk5Px22+/ITAwEH379sVLL70EAHjqqadw+PBhJCQkQKP
2023-04-20 15:56:44 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
"<Figure size 640x480 with 1 Axes>"
2023-04-20 15:56:44 +00:00
]
},
2023-04-24 16:05:07 +00:00
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "display_data"
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"soil.analysis.plot(it[0])"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {
"deletable": false,
"editable": false,
"hideCode": false,
"hidePrompt": false,
"run_control": {
"frozen": true
}
},
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"## Running in more scenarios\n",
2017-10-18 18:28:42 +00:00
"\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",
2017-10-18 18:28:42 +00:00
"\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."
2017-10-18 18:28:42 +00:00
]
},
{
2020-10-19 11:14:48 +00:00
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 11,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2023-04-20 15:56:44 +00:00
"outputs": [],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"class NewsEnvComplete(soil.Environment):\n",
" prob_tv = 0.05\n",
" prob_tv_spread = 0\n",
" prob_neighbor_spread = 0\n",
" event_time = 10\n",
" tv_factor = 0\n",
" neighbor_factor = 0.5\n",
" generator = \"erdos_renyi_graph\"\n",
" n = 100\n",
"\n",
" def init(self):\n",
" self.add_agent(EventGenerator)\n",
" if not self.G:\n",
" opts = {\"n\": self.n}\n",
" if self.generator == \"erdos_renyi_graph\":\n",
" opts[\"p\"] = 0.5\n",
" elif self.generator == \"barabasi_albert_graph\":\n",
" opts[\"m\"] = 4\n",
" self.create_network(generator=self.generator, **opts)\n",
"\n",
" self.populate_network([NewsSpread,\n",
" NewsSpread.w(has_tv=True)],\n",
" [1-self.prob_tv, self.prob_tv])\n",
" self.add_model_reporter('prob_tv_spread')\n",
" self.add_model_reporter('prob_neighbor_spread')\n",
" self.add_agent_reporter('state_id')"
2017-10-18 18:28:42 +00:00
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
2020-10-19 11:14:48 +00:00
"metadata": {},
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"Since we do not care about previous results, we will set`overwrite=True`."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 12,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
2017-10-18 18:28:42 +00:00
},
2020-10-19 11:14:48 +00:00
"outputs": [
2023-04-20 15:56:44 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-04-24 16:05:07 +00:00
"[INFO ][18:00:39] Output directory: /mnt/data/home/j/git/lab.gsi/soil/soil/examples/tutorial/soil_output\n"
2023-04-20 15:56:44 +00:00
]
},
2020-10-19 11:14:48 +00:00
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
2023-04-24 16:05:07 +00:00
"model_id": "178024d02ecc425395b3f67c19ba9fee",
2023-04-20 15:56:44 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2020-10-19 11:14:48 +00:00
]
},
"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"
]
},
2020-10-19 11:14:48 +00:00
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2020-10-19 11:14:48 +00:00
]
},
"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"
]
2020-10-19 11:14:48 +00:00
},
{
"data": {
2023-04-20 15:56:44 +00:00
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +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": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2023-04-24 16:05:07 +00:00
" 0%| | 0/5 [00:00<?, ?it/s]"
2020-10-19 11:14:48 +00:00
]
},
2023-03-23 13:49:09 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"output_type": "display_data"
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"s = soil.Simulation(model=NewsEnvComplete, iterations=5, max_time=30, dump=True, overwrite=True)\n",
"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 = s.run(name=f\"newspread\", matrix=dict(n=[N], generator=generators, prob_neighbor_spread=probabilities))"
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 13,
2023-04-20 15:56:44 +00:00
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [],
"source": [
"assert len(it) == len(probabilities) * len(generators) * s.iterations"
2017-10-18 18:28:42 +00:00
]
},
{
"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
2017-10-18 18:28:42 +00:00
},
"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`."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 14,
2017-10-18 18:28:42 +00:00
"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
2018-05-04 08:01:49 +00:00
},
2020-10-19 11:14:48 +00:00
"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_1681989837.124865.dumped.yml\n",
" ├── newspread_1681990513.1584163.dumped.yml\n",
" ├── newspread_1681990524.5204282.dumped.yml\n",
" ├── newspread_1681990796.858183.dumped.yml\n",
" ├── newspread_1682002299.544348.dumped.yml\n",
" ├── newspread_1682003721.597205.dumped.yml\n",
" ├── newspread_1682003784.1948986.dumped.yml\n",
" ├── newspread_1682003812.4626257.dumped.yml\n",
" ├── newspread_1682004020.182087.dumped.yml\n",
" ├── newspread_1682004044.6837814.dumped.yml\n",
" ├── newspread_1682004398.267355.dumped.yml\n",
" ├── newspread_1682004564.1052232.dumped.yml\n",
2023-04-24 16:05:07 +00:00
" ├── newspread_1682351953.9104202.dumped.yml\n",
" ├── newspread_1682352039.0612123.dumped.yml\n",
2023-04-20 15:56:44 +00:00
" └── newspread.sqlite\n",
"\n",
2023-04-24 16:05:07 +00:00
"1 directory, 15 files\n",
2023-04-20 15:56:44 +00:00
"21M\tsoil_output/newspread\n"
]
2020-10-19 11:14:48 +00:00
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"!tree soil_output\n",
"!du -xh soil_output/*"
2017-10-18 18:28:42 +00:00
]
},
{
"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
},
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"### Analysing the results"
2017-10-18 18:28:42 +00:00
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false
},
"source": [
"#### Loading data"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
2018-05-04 08:01:49 +00:00
},
2017-10-18 18:28:42 +00:00
"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>`"
2017-10-18 18:28:42 +00:00
]
},
{
"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`"
2017-10-18 18:28:42 +00:00
]
},
{
2023-04-20 15:56:44 +00:00
"cell_type": "markdown",
2017-10-18 18:28:42 +00:00
"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
2017-10-18 18:28:42 +00:00
},
"source": [
2023-04-20 15:56:44 +00:00
"Let's see it in action by loading the stored results into a pandas dataframe:"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 15,
2017-10-18 18:28:42 +00:00
"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
2017-10-18 18:28:42 +00:00
},
2023-04-20 15:56:44 +00:00
"outputs": [],
"source": [
"res = soil.read_sql(name=\"newspread\", include_agents=True)"
]
},
{
"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-04-24 16:05:07 +00:00
"execution_count": 16,
2023-04-20 15:56:44 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"outputs": [
{
"data": {
2023-04-24 16:05:07 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAHHCAYAAABtF1i4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAC/rUlEQVR4nOzdd1xT5/7A8U9ISBhhi4II4sCNCxe0dVQsjvJTa+1Vb0VbFxZ3bUW7rFixVXG0Wruuq9bbal23al11XBdeZ22luEBaFVBkyCbJ+f0ROTUyowzH83698tJz8j3P+Z4QyJNnHYUkSRKCIAiCIAjVxKK6ExAEQRAE4ekmKiOCIAiCIFQrURkRBEEQBKFaic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FCT7e3btwMwZcoUk/2F3yrv72KoXbu2yTd8e3t7QkJCOH36NImJiQBoNBosLIy/Ynq9npSUFLRaLY0bN+bUqVNF8g8JCcHOzk7efvnll3F3d5dzqwyvvPIKVlZWrF27Vt63c+dObt26ZfKzMMf27dvp0KGDyYBorVbL6NGjiY+P5/z58ybxw4YNK1fLT0W/706fPk1cXByTJk0qdmzF/e5/zzz33HNlvjd3797NnTt3CA8Px8rKqthznDhxguTkZN544w2TmD59+tCkSZMi770HzaU4169f59y5c4SEhJhMX+7SpQu+vr5ml3e/4lqN7v3ZZGVlcevWLQICApAkidOnT5t9Dr1ez65du+jXrx/169eX97u7uzNkyBAOHTpUpOt
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
"<Figure size 640x480 with 1 Axes>"
2020-10-19 11:14:48 +00:00
]
},
2023-04-24 16:05:07 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"output_type": "display_data"
}
],
2017-10-18 18:28:42 +00:00
"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();"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 17,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"hideCode": false,
"hidePrompt": false,
"scrolled": true
2017-10-18 18:28:42 +00:00
},
2020-10-19 11:14:48 +00:00
"outputs": [
{
"data": {
2023-04-24 16:05:07 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAi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4O+77z4eeOCBKo+51NidmrjYz8vhcLg99/T0ZOPGjaxfv56VK1fy888/89VXX3HTTTexevVq1Go1siyTkJDAvHnzqjxnVFRUpXP+WUhICHv37uWXX37hp59+4qeffmLx4sWMHTuWpUuXXuFdCkLdEMGIIFRDeYAxcOBA3nnnHVdekOXLl9OyZUu+++47tw+jP3cp1CSwiImJQZZlkpKSiI+Pd23PyMggPz//konJyvcdO3as0r6jR48SFBSE0Wisdl3qUqtWrTCZTK6WkEsd98svv5Cbm3vJ1pGqXuPg4GC8vb1xOByXvU5MTAwHDx5EURS3c1X1WlbF39+f/Pz8SturaslSqVQMGjSIQYMGMW/ePF577TWef/551q9fz+DBg2nVqhX79u1j0KBBVxWU6nQ6RowYwYgRI5Blmf/7v//j/fff58UXX6zU8iYI9Ul00whCNQ0YMIAePXqwYMECzGYzUNEycGFLwI4dO9i2bZtbWYPBAFDlh9Wf3XLLLQCVUqaX/5d86623XrRseHg4Xbp0YenSpW7XOnjwIKtXr3aduyG4++672bZtG7/88kulffn5+djtdgBGjx6NoiiupHEXuvB1NxqNlV5ftVrN6NGj+fbbbzl48GCl8llZWa7vb7nlFs6fP8/y5ctd20pKSvjggw+qdT+tWrWioKCA/fv3u7alpaVVmv2Um5tbqWyXLl0AXNN
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
"<Figure size 640x480 with 1 Axes>"
2020-10-19 11:14:48 +00:00
]
},
2023-04-24 16:05:07 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"output_type": "display_data"
}
],
2017-10-18 18:28:42 +00:00
"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",
" counts = group.groupby(by=[\"step\", \"state_id\"]).value_counts().unstack()\n",
" 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\");"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-20 15:56:44 +00:00
"## Data format"
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "markdown",
2023-04-20 15:56:44 +00:00
"metadata": {},
2017-10-18 18:28:42 +00:00
"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**."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-20 15:56:44 +00:00
"execution_count": 18,
2017-10-18 18:28:42 +00:00
"metadata": {
2023-04-20 15:56:44 +00:00
"scrolled": true
2017-10-18 18:28:42 +00:00
},
2020-10-19 11:14:48 +00:00
"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>key</th>\n",
" <th>generator</th>\n",
" <th>n</th>\n",
" <th>prob_neighbor_spread</th>\n",
" </tr>\n",
" <tr>\n",
" <th>iteration_id</th>\n",
" <th>params_id</th>\n",
" <th>simulation_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">0</th>\n",
" <th>39063f8</th>\n",
2023-04-24 16:05:07 +00:00
" <th>newspread_1682352039.0612123</th>\n",
2023-04-20 15:56:44 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>100</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
2023-04-24 16:05:07 +00:00
" <th>8f26adb</th>\n",
" <th>newspread_1682352039.0612123</th>\n",
2023-04-20 15:56:44 +00:00
" <td>barabasi_albert_graph</td>\n",
" <td>100</td>\n",
2023-04-24 16:05:07 +00:00
" <td>0.5</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
2023-04-24 16:05:07 +00:00
" <th>92fdcb9</th>\n",
" <th>newspread_1682352039.0612123</th>\n",
" <td>erdos_renyi_graph</td>\n",
2023-04-20 15:56:44 +00:00
" <td>100</td>\n",
2023-04-24 16:05:07 +00:00
" <td>0.25</td>\n",
2023-04-20 15:56:44 +00:00
" </tr>\n",
" <tr>\n",
" <th>cb3dbca</th>\n",
2023-04-24 16:05:07 +00:00
" <th>newspread_1682352039.0612123</th>\n",
2023-04-20 15:56:44 +00:00
" <td>erdos_renyi_graph</td>\n",
" <td>100</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d1fe9c1</th>\n",
2023-04-24 16:05:07 +00:00
" <th>newspread_1682352039.0612123</th>\n",
2023-04-20 15:56:44 +00:00
" <td>barabasi_albert_graph</td>\n",
" <td>100</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
"key generator \\\n",
"iteration_id params_id simulation_id \n",
"0 39063f8 newspread_1682352039.0612123 erdos_renyi_graph \n",
" 8f26adb newspread_1682352039.0612123 barabasi_albert_graph \n",
" 92fdcb9 newspread_1682352039.0612123 erdos_renyi_graph \n",
" cb3dbca newspread_1682352039.0612123 erdos_renyi_graph \n",
" d1fe9c1 newspread_1682352039.0612123 barabasi_albert_graph \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-04-24 16:05:07 +00:00
"key n prob_neighbor_spread \n",
"iteration_id params_id simulation_id \n",
"0 39063f8 newspread_1682352039.0612123 100 1.0 \n",
" 8f26adb newspread_1682352039.0612123 100 0.5 \n",
" 92fdcb9 newspread_1682352039.0612123 100 0.25 \n",
" cb3dbca newspread_1682352039.0612123 100 0.5 \n",
" d1fe9c1 newspread_1682352039.0612123 100 1.0 "
2020-10-19 11:14:48 +00:00
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 18,
2023-03-23 13:49:09 +00:00
"metadata": {},
2023-04-20 15:56:44 +00:00
"output_type": "execute_result"
2020-10-19 11:14:48 +00:00
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"res.parameters.head()"
2017-10-18 18:28:42 +00:00
]
},
{
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-04-20 15:56:44 +00:00
"execution_count": 19,
2023-03-23 13:49:09 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"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-04-24 16:05:07 +00:00
" <th>newspread_1682352039.0612123</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",
" <td>[<class 'soil.exporters.default'>]</td>\n",
" <td>{}</td>\n",
" <td>{}</td>\n",
" <td>{}</td>\n",
" <td>/mnt/data/home/j/git/lab.gsi/soil/soil/example...</td>\n",
" <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-04-20 15:56:44 +00:00
"<p>1 rows × 29 columns</p>\n",
2023-03-23 13:49:09 +00:00
"</div>"
],
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
" index version source_file name \\\n",
"simulation_id \n",
"newspread_1682352039.0612123 0 2 None newspread \n",
"\n",
" description group backup overwrite dry_run dump \\\n",
2023-04-20 15:56:44 +00:00
"simulation_id \n",
2023-04-24 16:05:07 +00:00
"newspread_1682352039.0612123 None False True False True \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" ... num_processes \\\n",
"simulation_id ... \n",
"newspread_1682352039.0612123 ... 1 \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" exporters \\\n",
"simulation_id \n",
"newspread_1682352039.0612123 [<class 'soil.exporters.default'>] \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" model_reporters agent_reporters tables \\\n",
"simulation_id \n",
"newspread_1682352039.0612123 {} {} {} \n",
2023-03-23 13:49:09 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" outdir \\\n",
"simulation_id \n",
"newspread_1682352039.0612123 /mnt/data/home/j/git/lab.gsi/soil/soil/example... \n",
2023-03-23 13:49:09 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" exporter_params level skip_test debug \n",
"simulation_id \n",
"newspread_1682352039.0612123 {} 20 False False \n",
2023-03-23 13:49:09 +00:00
"\n",
2023-04-20 15:56:44 +00:00
"[1 rows x 29 columns]"
2020-10-19 11:14:48 +00:00
]
},
2023-04-20 15:56:44 +00:00
"execution_count": 19,
2020-10-19 11:14:48 +00:00
"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": {},
2017-10-18 18:28:42 +00:00
"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**."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 20,
2023-04-20 15:56:44 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"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",
" <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>simulation_id</th>\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\">newspread_1682352039.0612123</th>\n",
" <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",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>101</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>101</td>\n",
" <td>2</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>101</td>\n",
" <td>3</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>101</td>\n",
" <td>4</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
" agent_count time \\\n",
"simulation_id params_id iteration_id step \n",
"newspread_1682352039.0612123 ff1d24a 0 0 101 0 \n",
" 1 101 1 \n",
" 2 101 2 \n",
" 3 101 3 \n",
" 4 101 4 \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" prob_tv_spread \\\n",
"simulation_id params_id iteration_id step \n",
"newspread_1682352039.0612123 ff1d24a 0 0 0.0 \n",
" 1 0.0 \n",
" 2 0.0 \n",
" 3 0.0 \n",
" 4 0.0 \n",
2023-04-20 15:56:44 +00:00
"\n",
2023-04-24 16:05:07 +00:00
" prob_neighbor_spread \n",
"simulation_id params_id iteration_id step \n",
"newspread_1682352039.0612123 ff1d24a 0 0 0.0 \n",
" 1 0.0 \n",
" 2 0.0 \n",
" 3 0.0 \n",
" 4 0.0 "
2020-10-19 11:14:48 +00:00
]
},
2023-04-24 16:05:07 +00:00
"execution_count": 20,
2020-10-19 11:14:48 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
2017-10-18 18:28:42 +00:00
"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`."
2017-10-18 18:28:42 +00:00
]
},
{
"cell_type": "code",
2023-04-24 16:05:07 +00:00
"execution_count": 21,
2023-04-20 15:56:44 +00:00
"metadata": {},
2020-10-19 11:14:48 +00:00
"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",
" <th></th>\n",
" <th>state_id</th>\n",
" </tr>\n",
" <tr>\n",
" <th>simulation_id</th>\n",
" <th>params_id</th>\n",
" <th>iteration_id</th>\n",
" <th>step</th>\n",
" <th>agent_id</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\">newspread_1682352039.0612123</th>\n",
" <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",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>neutral</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
2020-10-19 11:14:48 +00:00
"text/plain": [
2023-04-24 16:05:07 +00:00
" state_id\n",
"simulation_id params_id iteration_id step agent_id \n",
"newspread_1682352039.0612123 ff1d24a 0 0 0 None\n",
" 1 neutral\n",
" 2 neutral\n",
" 3 neutral\n",
" 4 neutral"
2020-10-19 11:14:48 +00:00
]
},
2023-04-24 16:05:07 +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"
2020-10-19 11:14:48 +00:00
}
],
2017-10-18 18:28:42 +00:00
"source": [
2023-04-20 15:56:44 +00:00
"res.agents.head()"
2017-10-18 18:28:42 +00:00
]
}
],
"metadata": {
2023-04-20 15:56:44 +00:00
"hide_code_all_hidden": false,
2017-10-18 18:28:42 +00:00
"kernelspec": {
2023-04-20 15:56:44 +00:00
"display_name": "Python 3 (ipykernel)",
2017-10-18 18:28:42 +00:00
"language": "python",
2023-04-24 16:05:07 +00:00
"name": "python3"
2017-10-18 18:28:42 +00:00
},
"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"
2017-10-18 18:28:42 +00:00
},
"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",
2018-05-04 08:01:49 +00:00
"toc_window_display": false,
2017-10-18 18:28:42 +00:00
"widenNotebook": false
2023-04-14 17:41:24 +00:00
},
"vscode": {
"interpreter": {
"hash": "3581132406f7320837865a422f37590c78ed7dabfbcb5bc7771b9d116b13a5cf"
}
2017-10-18 18:28:42 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}