1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-22 19:22:29 +00:00
soil/examples/tutorial/soil_tutorial.ipynb

1351 lines
47 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"
}
},
"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"
}
},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
"collapsed": true
},
"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"
}
},
"source": [
"But before that, let's import the soil module and networkx."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:13.451481Z",
"start_time": "2017-11-03T11:58:12.643469+01:00"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"import soil\n",
"import networkx as nx\n",
" \n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"%pylab inline\n",
"# To display plots in the notebooed_"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T13:41:19.788717Z",
"start_time": "2017-07-03T15:41:19.785448+02:00"
}
},
"source": [
"## Basic concepts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are three main elements in a soil simulation:\n",
" \n",
"* The network topology. A simulation may use an existing NetworkX topology, or generate one on the fly\n",
"* Agents. There are two types: 1) network agents, which are linked to a node in the topology, and 2) environment agents, which are freely assigned to the environment.\n",
"* The environment. It assigns agents to nodes in the network, and stores the environment parameters (shared state for all agents).\n",
"\n",
"Soil is based on ``simpy``, which is an event-based network simulation library.\n",
"Soil provides several abstractions over events to make developing agents easier.\n",
"This means you can use events (timeouts, delays) in soil, but for the most part we will assume your models will be step-based.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T15:55:12.933978Z",
"start_time": "2017-07-02T17:55:12.930860+02: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"
}
},
"source": [
"Our first step will be to model how every person in the social network reacts when it comes to news.\n",
"We will follow a very simple model (a finite state machine).\n",
"\n",
"There are two types of people, those who have heard about a newsworthy event (infected) or those who have not (neutral).\n",
"A neutral person may heard about the news 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",
"Some users do not have a TV, so they only rely on their friends.\n",
"\n",
"The spreading probabilities will change over time due to different factors.\n",
"We will represent this variance using an environment agent."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Network Agents"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:03:07.171127Z",
"start_time": "2017-07-03T16:03:07.165779+02:00"
}
},
"source": [
"A basic network agent in Soil should inherit from ``soil.agents.BaseAgent``, and define its behaviour in every step of the simulation by implementing a ``run(self)`` method.\n",
"The most important attributes of the agent are:\n",
"\n",
"* ``agent.state``, a dictionary with the state of the agent. ``agent.state['id']`` reflects the state id of the agent. That state id can be used to look for other networks in that specific state. The state can be access via the agent as well. For instance:\n",
"```py\n",
"a = soil.agents.BaseAgent(env=env)\n",
"a['hours_of_sleep'] = 10\n",
"print(a['hours_of_sleep'])\n",
"```\n",
" The state of the agent is stored in every step of the simulation:\n",
" ```py\n",
" print(a['hours_of_sleep', 10]) # hours of sleep before step #10\n",
" print(a[None, 0]) # whole state of the agent before step #0\n",
" ```\n",
"\n",
"* ``agent.env``, a reference to the environment. Most commonly used to get access to the environment parameters and the topology:\n",
" ```py\n",
" a.env.G.nodes() # Get all nodes ids in the topology\n",
" a.env['minimum_hours_of_sleep']\n",
"\n",
" ```\n",
"\n",
"Since our model is a finite state machine, we will be basing it on ``soil.agents.FSM``.\n",
"\n",
"With ``soil.agents.FSM``, we do not need to specify a ``step`` method.\n",
"Instead, we describe every step as a function.\n",
"To change to another state, a function may return the new state.\n",
"If no state is returned, the state remains unchanged.[\n",
"It will consist of two states, ``neutral`` (default) and ``infected``.\n",
"\n",
"Here's the code:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:16.051690Z",
"start_time": "2017-11-03T11:58:16.006044+01:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"import random\n",
"\n",
"class NewsSpread(soil.agents.FSM):\n",
" @soil.agents.default_state\n",
" @soil.agents.state\n",
" def neutral(self):\n",
" r = random.random()\n",
" if self['has_tv'] and r < self.env['prob_tv_spread']:\n",
" return self.infected\n",
" return\n",
" \n",
" @soil.agents.state\n",
" def infected(self):\n",
" prob_infect = self.env['prob_neighbor_spread']\n",
" for neighbor in self.get_neighboring_agents(state_id=self.neutral.id):\n",
" r = random.random()\n",
" if r < prob_infect:\n",
" neighbor.state['id'] = self.infected.id\n",
" return\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T12:22:53.931963Z",
"start_time": "2017-07-02T14:22:53.928340+02:00"
}
},
"source": [
"### Environment agents"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:17.653736Z",
"start_time": "2017-11-03T11:58:17.612944+01:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"NEIGHBOR_FACTOR = 0.9\n",
"TV_FACTOR = 0.5\n",
"class NewsEnvironmentAgent(soil.agents.BaseAgent):\n",
" def step(self):\n",
" if self.now == self['event_time']:\n",
" self.env['prob_tv_spread'] = 1\n",
" self.env['prob_neighbor_spread'] = 1\n",
" elif self.now > self['event_time']:\n",
" self.env['prob_tv_spread'] = self.env['prob_tv_spread'] * TV_FACTOR\n",
" self.env['prob_neighbor_spread'] = self.env['prob_neighbor_spread'] * NEIGHBOR_FACTOR"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T11:23:18.052235Z",
"start_time": "2017-07-02T13:23:18.047452+02:00"
}
},
"source": [
"### Testing the agents"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T16:14:54.572431Z",
"start_time": "2017-07-02T18:14:54.564095+02:00"
}
},
"source": [
"Feel free to skip this section if this is your first time with soil."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Testing agents is not easy, and this is not a thorough testing process for agents.\n",
"Rather, this section is aimed to show you how to access internal pats of soil so you can test your agents."
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split"
},
"source": [
"First of all, let's check if our network agent has the states we would expect:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:19.781155Z",
"start_time": "2017-11-03T11:58:19.754362+01:00"
},
"cell_style": "split"
},
"outputs": [
{
"data": {
"text/plain": [
"{'infected': <function __main__.NewsSpread.infected>,\n",
" 'neutral': <function __main__.NewsSpread.neutral>}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"NewsSpread.states"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split"
},
"source": [
"Now, let's run a simulation on a simple network. It is comprised of three nodes:\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:20.791777Z",
"start_time": "2017-11-03T11:58:20.565173+01:00"
},
"cell_style": "split",
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAD8CAYAAACfF6SlAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHNVJREFUeJzt3X+UFOWd7/H3VwaYGY1kVNBBGSARE1hBXRtd70ZlV9gg\nyYr8SBYjCl4JN7je/WMPnstc1xh1z8EfmL33nkQM111B9mx0FRaIAUWJGJMVnGHVQSAK4i8Cl0EX\nJ+rMiMD3/lE12jTd0z1T1T0/6vM6p09Xdz1d9XUYP13z1FNPmbsjIiLJckJXFyAiIqWn8BcRSSCF\nv4hIAin8RUQSSOEvIpJACn8RkQRS+IuIJJDCX0QkgRT+IiIJVNbVBeRy2mmn+bBhw7q6DBGRHmXL\nli3vu/vAfO26bfgPGzaM+vr6ri5DRKRHMbN3Cmmnbh8RkQRS+IuIJJDCX0QkgRT+IiIJpPAXEUkg\nhb+ISAIp/EVEEiiW8DeziWb2upntMrMFWdb/rZltN7MGM9tgZkPj2K+IiHRO5PA3sz7AT4ErgVHA\nNWY2KqPZy0DK3ccATwD3Rt2viIh0XhxH/hcBu9x9t7sfAh4FJqc3cPfn3L05fLkJOCuG/YqISCfF\nMb3DmcB7aa/3ABe30/5GYF0M+5WkaWyEpUuhoQGammDAABgzBm64AQbmncpERNLEEf6W5T3P2tBs\nJpACLs+xfi4wF6CmpiaG0qRXqKuDhQthXXjM0Nr6xbqVK+H22+HKK6G2FsaO7ZoaRXqYOLp99gBD\n0l6fBezNbGRm44Fbgavc/dNsG3L3Je6ecvfUQB3JCc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/+MMfKCsr+zzoZ86
"text/plain": [
"<matplotlib.figure.Figure at 0x7f1b7c04fcc0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"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",
"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": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T11:53:30.997756Z",
"start_time": "2017-07-03T13:53:30.989609+02:00"
},
"cell_style": "split"
},
"source": [
"Let's run a simple simulation that assigns a NewsSpread agent to all the nodes in that network.\n",
"Notice how node 0 is the only one with a TV."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:58:55.517768Z",
"start_time": "2017-11-03T11:58:55.424083+01:00"
},
"cell_style": "split"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:soil:Starting simulation UnnamedSimulation at 1509706735.4865255.\n",
"INFO:soil:Starting Simulation UnnamedSimulation trial 0 at 1509706735.489935.\n",
"INFO:soil:Finished Simulation UnnamedSimulation trial 0 in 0.022043228149414062 seconds\n",
"INFO:soil:NOT dumping results\n",
"INFO:soil:Finished simulation UnnamedSimulation in 0.026972055435180664 seconds\n"
]
}
],
"source": [
"env_params = {'prob_tv_spread': 0,\n",
" 'prob_neighbor_spread': 0}\n",
"\n",
"MAX_TIME = 100\n",
"EVENT_TIME = 10\n",
"\n",
"sim = soil.simulation.SoilSimulation(topology=G,\n",
" num_trials=1,\n",
" max_time=MAX_TIME,\n",
" environment_agents=[{'agent_type': NewsEnvironmentAgent,\n",
" 'state': {\n",
" 'event_time': EVENT_TIME\n",
" }}],\n",
" network_agents=[{'agent_type': NewsSpread,\n",
" 'weight': 1}],\n",
" states={0: {'has_tv': True}},\n",
" dry_run=True,\n",
" default_state={'has_tv': False},\n",
" environment_params=env_params)\n",
"env = sim.run_simulation()[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split"
},
"source": [
"Now we can access the results of the simulation and compare them to our expected results"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T10:59:01.577474Z",
"start_time": "2017-11-03T11:59:01.414215+01:00"
},
"cell_style": "split",
"scrolled": false
},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m----------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-e08fafc133f0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mt\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ma\u001b[0m \u001b[0;32min\u001b[0m \u001b[0magents\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'id'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mneutral\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m# After the event, the node with a TV is infected, the rest are not\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"agents = list(env.network_agents)\n",
"\n",
"# Until the event, all agents are neutral\n",
"for t in range(10):\n",
" for a in agents:\n",
" assert a['id', t] == a.neutral.id\n",
"\n",
"# After the event, the node with a TV is infected, the rest are not\n",
"assert agents[0]['id', 11] == NewsSpread.infected.id\n",
"\n",
"for a in agents[1:4]:\n",
" assert a['id', 11] == NewsSpread.neutral.id\n",
"\n",
"# At the end, the agents connected to the infected one will probably be infected, too.\n",
"assert agents[1]['id', MAX_TIME] == NewsSpread.infected.id\n",
"assert agents[2]['id', MAX_TIME] == NewsSpread.infected.id\n",
"\n",
"# But the node with no friends should not be affected\n",
"assert agents[4]['id', MAX_TIME] == NewsSpread.neutral.id\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-03T11:09:23.581734Z",
"start_time": "2017-11-03T12:09:23.559473+01:00"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{0: 'neutral'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a['id', t]"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T16:41:09.110652Z",
"start_time": "2017-07-02T18:41:09.106966+02:00"
},
"cell_style": "split"
},
"source": [
"Lastly, let's see if the probabilities have decreased as expected:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-01T14:07:55.288616Z",
"start_time": "2017-11-01T15:07:55.241116+01:00"
},
"cell_style": "split",
"collapsed": true
},
"outputs": [],
"source": [
"assert abs(env.environment_params['prob_neighbor_spread'] - (NEIGHBOR_FACTOR**(MAX_TIME-1-10))) < 10e-4\n",
"assert abs(env.environment_params['prob_tv_spread'] - (TV_FACTOR**(MAX_TIME-1-10))) < 10e-6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Running the simulation"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T11:20:28.566944Z",
"start_time": "2017-07-03T13:20:28.561052+02:00"
},
"cell_style": "split"
},
"source": [
"To run a simulation, we need a configuration.\n",
"Soil can load configurations from python dictionaries as well as JSON and YAML files.\n",
"For this demo, we will use a python dictionary:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-01T14:07:57.008940Z",
"start_time": "2017-11-01T15:07:56.966433+01:00"
},
"cell_style": "split",
"collapsed": true
},
"outputs": [],
"source": [
"config = {\n",
" 'name': 'ExampleSimulation',\n",
" 'max_time': 20,\n",
" 'interval': 1,\n",
" 'num_trials': 1,\n",
" 'network_params': {\n",
" 'generator': 'complete_graph',\n",
" 'n': 500,\n",
" },\n",
" 'network_agents': [\n",
" {\n",
" 'agent_type': NewsSpread,\n",
" 'weight': 1,\n",
" 'state': {\n",
" 'has_tv': False\n",
" }\n",
" },\n",
" {\n",
" 'agent_type': NewsSpread,\n",
" 'weight': 2,\n",
" 'state': {\n",
" 'has_tv': True\n",
" }\n",
" }\n",
" ],\n",
" 'environment_agents':[\n",
" {'agent_type': NewsEnvironmentAgent,\n",
" 'state': {\n",
" 'event_time': 10\n",
" }\n",
" }\n",
" ],\n",
" 'states': [ {'has_tv': True} ],\n",
" 'environment_params':{\n",
" 'prob_tv_spread': 0.01,\n",
" 'prob_neighbor_spread': 0.5\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T11:57:34.219618Z",
"start_time": "2017-07-03T13:57:34.213817+02:00"
},
"cell_style": "split"
},
"source": [
"Let's run our simulation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-01T14:08:34.312637Z",
"start_time": "2017-11-01T15:07:57.774458+01:00"
},
"cell_style": "split",
"collapsed": true
},
"outputs": [],
"source": [
"soil.simulation.run_from_config(config, dump=False)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T12:03:32.183588Z",
"start_time": "2017-07-03T14:03:32.167797+02:00"
},
"cell_style": "split",
"collapsed": true
},
"source": [
"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",
"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",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-01T14:10:38.099667Z",
"start_time": "2017-11-01T15:10:06.008314+01:00"
},
"cell_style": "split",
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"network_1 = {\n",
" 'generator': 'erdos_renyi_graph',\n",
" 'n': 500,\n",
" 'p': 0.1\n",
"}\n",
"network_2 = {\n",
" 'generator': 'barabasi_albert_graph',\n",
" 'n': 500,\n",
" 'm': 2\n",
"}\n",
"\n",
"\n",
"for net in [network_1, network_2]:\n",
" for i in range(5):\n",
" prob = i / 10\n",
" config['environment_params']['prob_neighbor_spread'] = prob\n",
" config['network_params'] = net\n",
" config['name'] = 'Spread_{}_prob_{}'.format(net['generator'], prob)\n",
" s = soil.simulation.run_from_config(config, parallel=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T11:05:18.043194Z",
"start_time": "2017-07-03T13:05:18.034699+02:00"
},
"cell_style": "center"
},
"source": [
"The results are conveniently stored in pickle (simulation), and sqlite (history of agent and environment state).\n",
"\n",
"You can also export the results to GEXF format (dynamic network) and CSV using .`run_from_config(config, dump=['gexf', 'csv'])` or the command line flags `--graph --csv`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-01T14:05:56.404540Z",
"start_time": "2017-11-01T15:05:56.122876+01:00"
},
"cell_style": "split",
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"!tree soil_output\n",
"!du -xh soil_output/*"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-02T10:40:14.384177Z",
"start_time": "2017-07-02T12:40:14.381885+02:00"
}
},
"source": [
"## Analysing the results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once the simulations are over, we can use soil to analyse the results.\n",
"\n",
"Soil allows you to load results for specific trials, or for a set of trials if you specify a pattern. The specific methods are:\n",
"\n",
"* `analysis.read_data(<directory pattern>)` to load all the results from a directory. e.g. `read_data('my_simulation/')`. For each trial it finds in each folder matching the pattern, it will return the dumped configuration for the simulation, the results of the trial, and the configuration itself. By default, it will try to load data from the sqlite database. \n",
"* `analysis.read_csv(<csv_file>)` to load all the results from a CSV file. e.g. `read_csv('my_simulation/my_simulation_trial0.environment.csv')`\n",
"* `analysis.read_sql(<sqlite_file>)` to load all the results from a sqlite database . e.g. `read_sql('my_simulation/my_simulation_trial0.db.sqlite')`"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-07-03T14:44:30.978223Z",
"start_time": "2017-07-03T16:44:30.971952+02:00"
}
},
"source": [
"Let's see it in action by loading the stored results into a pandas dataframe:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:43.662893Z",
"start_time": "2017-10-19T17:57:43.632252+02:00"
},
"cell_style": "center",
"collapsed": true
},
"outputs": [],
"source": [
"from soil.analysis import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:44.101253Z",
"start_time": "2017-10-19T17:57:44.039710+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"df = read_csv('soil_output/Spread_barabasi_albert_graph_prob_0.0/Spread_barabasi_albert_graph_prob_0.0_trial_0.environment.csv', keys=['id'])\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Soil can also process the data for us and return a dataframe with as many columns as there are attributes in the environment and the agent states:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:45.777794Z",
"start_time": "2017-10-19T17:57:45.698020+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"env, agents = process(df)\n",
"agents"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-18T14:01:00.669671Z",
"start_time": "2017-10-18T16:01:00.635624+02:00"
}
},
"source": [
"The index of the results are the simulation step and the agent_id. Hence, we can access the state of the simulation at a given step: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:47.132212Z",
"start_time": "2017-10-19T17:57:47.084737+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"agents.loc[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or, we can perform more complex tasks such as showing the agents that have changed their state between two simulation steps:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:48.168805Z",
"start_time": "2017-10-19T17:57:48.113961+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"changed = agents.loc[1]['id'] != agents.loc[0]['id']\n",
"agents.loc[0][changed]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To focus on specific agents, we can swap the levels of the index:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:49.046261Z",
"start_time": "2017-10-19T17:57:49.019721+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"agents1 = agents.swaplevel()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:49.459500Z",
"start_time": "2017-10-19T17:57:49.420016+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"agents1.loc['0'].dropna(axis=1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T10:35:40.140920Z",
"start_time": "2017-10-19T12:35:40.106265+02:00"
}
},
"source": [
"### Plotting data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you don't want to work with pandas, you can also use some pre-defined functions from soil to conveniently plot the results:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:52.271094Z",
"start_time": "2017-10-19T17:57:51.102434+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"plot_all('soil_output/Spread_barabasi_albert_graph_prob_0.0/', get_count, 'id');"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:57:57.982007Z",
"start_time": "2017-10-19T17:57:52.273160+02:00"
},
"collapsed": true,
"scrolled": false
},
"outputs": [],
"source": [
"plot_all('soil_output/Spread_barabasi*', get_count, 'id');"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:58:26.903783Z",
"start_time": "2017-10-19T17:57:57.983957+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"plot_all('soil_output/Spread_erdos*', get_value, 'prob_tv_spread');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Manually plotting with pandas"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T11:00:37.003972Z",
"start_time": "2017-10-19T13:00:36.983128+02:00"
}
},
"source": [
"Although the simplest way to visualize the results of a simulation is to use the built-in methods in the analysis module, sometimes the setup is more complicated and we need to explore the data a little further.\n",
"\n",
"For that, we can use native pandas over the results.\n",
"\n",
"Soil provides some convenience methods to simplify common operations:\n",
"\n",
"* `analysis.split_df` to separate a history dataframe into environment and agent parameters.\n",
"* `analysis.get_count` to get a dataframe with the value counts for different attributes during the simulation.\n",
"* `analysis.get_value` to get the evolution of the value of an attribute during the simulation.\n",
"\n",
"And, as we saw earlier, `analysis.process` can turn a dataframe in canonical form into a dataframe with a column per attribute.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:59:15.791793Z",
"start_time": "2017-10-19T17:59:15.604960+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"p = read_sql('soil_output/Spread_barabasi_albert_graph_prob_0.0/Spread_barabasi_albert_graph_prob_0.0_trial_0.db.sqlite')\n",
"env, agents = split_df(p);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at the evolution of agent parameters in the simulation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:59:17.153282Z",
"start_time": "2017-10-19T17:59:16.830872+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"res = agents.groupby(by=['t_step', 'key', 'value']).size().unstack(level=[1,2]).fillna(0)\n",
"res.plot();"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T11:10:36.086913Z",
"start_time": "2017-10-19T13:10:36.058547+02:00"
}
},
"source": [
"As we can see, `event_time` is cluttering our results, "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:59:18.418348Z",
"start_time": "2017-10-19T17:59:18.143443+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"del res['event_time']\n",
"res.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:59:42.750011Z",
"start_time": "2017-10-19T17:59:42.649353+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"processed = process_one(agents);\n",
"processed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which is equivalent to:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:59:51.165806Z",
"start_time": "2017-10-19T17:59:50.886780+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"get_count(agents, 'id', 'has_tv').plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T15:59:55.203641Z",
"start_time": "2017-10-19T17:59:54.950046+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"get_value(agents, 'event_time').plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dealing with bigger data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:18.148006Z",
"start_time": "2017-10-19T18:00:18.117654+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"from soil import analysis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:18.636440Z",
"start_time": "2017-10-19T18:00:18.504421+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"!du -xsh ../rabbits/soil_output/rabbits_example/"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T11:22:22.301765Z",
"start_time": "2017-10-19T13:22:22.281986+02:00"
}
},
"source": [
"If we tried to load the entire history, we would probably run out of memory. Hence, it is recommended that you also specify the attributes you are interested in."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:25.080582Z",
"start_time": "2017-10-19T18:00:19.594165+02:00"
},
"collapsed": true,
"scrolled": false
},
"outputs": [],
"source": [
"p = analysis.plot_all('../rabbits/soil_output/rabbits_example/', analysis.get_count, 'id')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:38.434367Z",
"start_time": "2017-10-19T18:00:33.645762+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"df = analysis.read_sql('../rabbits/soil_output/rabbits_example/rabbits_example_trial_0.db.sqlite', keys=['id', 'rabbits_alive'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:39.160418Z",
"start_time": "2017-10-19T18:00:38.436153+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"states = analysis.get_count(df, 'id')\n",
"states.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:39.515032Z",
"start_time": "2017-10-19T18:00:39.162240+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"alive = analysis.get_value(df, 'rabbits_alive', 'rabbits_alive', aggfunc='sum').apply(pd.to_numeric)\n",
"alive.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:00:58.815038Z",
"start_time": "2017-10-19T18:00:58.566807+02:00"
},
"collapsed": true
},
"outputs": [],
"source": [
"h = alive.join(states);\n",
"h.plot();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2017-10-19T16:01:01.195253Z",
"start_time": "2017-10-19T18:01:01.142907+02:00"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"states[[('id','newborn'),('id','fertile'),('id', 'pregnant')]].sum(axis=1).sub(alive['rabbits_alive'], fill_value=0)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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",
"version": "3.6.3"
},
"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
}
},
"nbformat": 4,
"nbformat_minor": 2
}