The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings EMULSION public releases
Summary Activity Surveys SCM Listes Sympa

SCM Repository

2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
6   <head>
7     <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
8     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9     <title>emulsion.agent.managers.group_manager &#8212; EMULSION (Epidemiological Multi-Level Simulation framework)</title>
10     <link rel="stylesheet" href="../../../../_static/alabaster.css" type="text/css" />
11     <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
12     <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
13     <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
14     <script type="text/javascript" src="../../../../_static/jquery.js"></script>
15     <script type="text/javascript" src="../../../../_static/underscore.js"></script>
16     <script type="text/javascript" src="../../../../_static/doctools.js"></script>
17     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
18     <link rel="shortcut icon" href="../../../../_static/STEMAH-favicon.png"/>
19     <link rel="index" title="Index" href="../../../../genindex.html" />
20     <link rel="search" title="Search" href="../../../../search.html" />
21    
22   <link rel="stylesheet" href="../../../../_static/custom.css" type="text/css" />
23   
24   
25   <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
27   </head><body>
28   
30     <div class="document">
31       <div class="documentwrapper">
32         <div class="bodywrapper">
33           <div class="body" role="main">
34             
35   <h1>Source code for emulsion.agent.managers.group_manager</h1><div class="highlight"><pre>
36 <span></span><span class="sd">&quot;&quot;&quot;</span>
37 <span class="sd">.. module:: emulsion.agent.managers.functions</span>
39 <span class="sd">.. moduleauthor:: Sébastien Picault &lt;sebastien.picault@inra.fr&gt;</span>
41 <span class="sd">&quot;&quot;&quot;</span>
43 <span class="c1"># [HEADER]</span>
45 <span class="kn">from</span>   <span class="nn">collections</span>               <span class="k">import</span> <span class="n">OrderedDict</span>
47 <span class="kn">from</span>   <span class="nn">sortedcontainers</span>          <span class="k">import</span> <span class="n">SortedDict</span>
49 <span class="kn">from</span>   <span class="nn">emulsion.agent.views</span>      <span class="k">import</span> <span class="n">StructuredView</span>
50 <span class="kn">from</span>   <span class="nn">emulsion.tools.misc</span>       <span class="k">import</span> <span class="n">count_population</span><span class="p">,</span> <span class="n">rewrite_keys</span>
52 <span class="kn">from</span>   <span class="nn">emulsion.agent.managers.functions</span> <span class="k">import</span> <span class="n">group_and_split_populations</span>
54 <span class="c1">#   _____                       __  __</span>
55 <span class="c1">#  / ____|                     |  \/  |</span>
56 <span class="c1"># | |  __ _ __ ___  _   _ _ __ | \  / | __ _ _ __   __ _  __ _  ___ _ __</span>
57 <span class="c1"># | | |_ | &#39;__/ _ \| | | | &#39;_ \| |\/| |/ _` | &#39;_ \ / _` |/ _` |/ _ \ &#39;__|</span>
58 <span class="c1"># | |__| | | | (_) | |_| | |_) | |  | | (_| | | | | (_| | (_| |  __/ |</span>
59 <span class="c1">#  \_____|_|  \___/ \__,_| .__/|_|  |_|\__,_|_| |_|\__,_|\__, |\___|_|</span>
60 <span class="c1">#                        | |                              __/ |</span>
61 <span class="c1">#                        |_|                             |___/</span>
63 <div class="viewcode-block" id="GroupManager"><a class="viewcode-back" href="../../../../emulsion.agent.managers.html#emulsion.agent.managers.group_manager.GroupManager">[docs]</a><span class="k">class</span> <span class="nc">GroupManager</span><span class="p">(</span><span class="n">StructuredView</span><span class="p">):</span>
64     <span class="sd">&quot;&quot;&quot;An GroupManager is able to make its content</span>
65 <span class="sd">    evolve according to a specific state machine, the state of each</span>
66 <span class="sd">    subcompartment being stored in a specific state variable or</span>
67 <span class="sd">    attribute.</span>
69 <span class="sd">    &quot;&quot;&quot;</span>
70     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_machine</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
71         <span class="sd">&quot;&quot;&quot;Create an GroupManager based on the</span>
72 <span class="sd">        specified state machine. The state of each subcompartment can</span>
73 <span class="sd">        be retrieved in the specified statevar name (&#39;true&#39; statevar</span>
74 <span class="sd">        or attribute)</span>
76 <span class="sd">        &quot;&quot;&quot;</span>
77         <span class="c1">### WARNING: strange bug found sometimes when content={} not</span>
78         <span class="c1">### explicitly specified, another content (from another</span>
79         <span class="c1">### instance ???) may be used instead !!!!</span>
80         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
81         <span class="bp">self</span><span class="o">.</span><span class="n">_content</span> <span class="o">=</span> <span class="n">SortedDict</span><span class="p">()</span>
82         <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span> <span class="o">=</span> <span class="n">state_machine</span>
83         <span class="bp">self</span><span class="o">.</span><span class="n">process_name</span> <span class="o">=</span> <span class="kc">None</span>
84         <span class="bp">self</span><span class="o">.</span><span class="n">init_counts</span><span class="p">()</span>
86 <div class="viewcode-block" id="GroupManager.init_counts"><a class="viewcode-back" href="../../../../emulsion.agent.managers.html#emulsion.agent.managers.group_manager.GroupManager.init_counts">[docs]</a>    <span class="k">def</span> <span class="nf">init_counts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
87         <span class="sd">&quot;&quot;&quot;Initialize the counts.&quot;&quot;&quot;</span>
88         <span class="c1">## DEBUG</span>
89         <span class="c1"># print(&#39;STEP&#39;, self.statevars.step, &#39;\nInit counts in GM&#39;, self)</span>
90         <span class="bp">self</span><span class="o">.</span><span class="n">counts</span> <span class="o">=</span> <span class="p">{}</span>
91         <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
92             <span class="bp">self</span><span class="o">.</span><span class="n">counts</span> <span class="o">=</span> <span class="p">{</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="p">[]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_history</span> <span class="k">else</span> <span class="mi">0</span>
93                            <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">states</span><span class="p">}</span>
94             <span class="bp">self</span><span class="o">.</span><span class="n">counts</span><span class="p">[</span><span class="s1">&#39;step&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_history</span> <span class="k">else</span> <span class="mi">0</span>
95         <span class="k">else</span><span class="p">:</span>
96             <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">init_counts</span><span class="p">()</span></div>
97         <span class="c1">## DEBUG</span>
98         <span class="c1"># print(self.counts)</span>
100 <div class="viewcode-block" id="GroupManager.update_counts"><a class="viewcode-back" href="../../../../emulsion.agent.managers.html#emulsion.agent.managers.group_manager.GroupManager.update_counts">[docs]</a>    <span class="k">def</span> <span class="nf">update_counts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
101         <span class="sd">&quot;&quot;&quot;Update the number of atoms for each state of the state</span>
102 <span class="sd">        machine (TODO: for each value of the key[index] enum).</span>
104 <span class="sd">        &quot;&quot;&quot;</span>
105         <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
106             <span class="n">total</span> <span class="o">=</span> <span class="p">{</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="mi">0</span> <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">states</span><span class="p">}</span>
107             <span class="c1">## DEBUG</span>
108             <span class="c1"># print(&#39;\t&#39;, self.statevars.step, sep=&#39;&#39;)</span>
109             <span class="k">for</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_content</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
110                 <span class="k">if</span> <span class="n">key</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
111                     <span class="n">total</span><span class="p">[</span><span class="n">key</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">+=</span> <span class="n">unit</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span><span class="s1">&#39;population&#39;</span><span class="p">)</span>
112                     <span class="c1"># total[key[index].name] += unit.population</span>
113                     <span class="c1">## DEBUG</span>
114                     <span class="c1"># print(key, unit.population, sep=&#39; + &#39;)</span>
115             <span class="c1"># print()</span>
116             <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_history</span><span class="p">:</span>
117                 <span class="bp">self</span><span class="o">.</span><span class="n">counts</span><span class="p">[</span><span class="s1">&#39;step&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevars</span><span class="o">.</span><span class="n">step</span><span class="p">)</span>
118                 <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">states</span><span class="p">:</span>
119                     <span class="bp">self</span><span class="o">.</span><span class="n">counts</span><span class="p">[</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">total</span><span class="p">[</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>
120             <span class="k">else</span><span class="p">:</span>
121                 <span class="bp">self</span><span class="o">.</span><span class="n">counts</span><span class="p">[</span><span class="s1">&#39;step&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">statevars</span><span class="o">.</span><span class="n">step</span>
122                 <span class="bp">self</span><span class="o">.</span><span class="n">counts</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">total</span><span class="p">)</span>
123         <span class="k">else</span><span class="p">:</span>
124             <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">update_counts</span><span class="p">()</span></div>
125         <span class="c1">## DEBUG</span>
126         <span class="c1"># print(&#39;STEP&#39;, self.statevars.step, &#39;\nUPDATE&#39;, self.counts)</span>
128 <div class="viewcode-block" id="GroupManager.apply_changes"><a class="viewcode-back" href="../../../../emulsion.agent.managers.html#emulsion.agent.managers.group_manager.GroupManager.apply_changes">[docs]</a>    <span class="k">def</span> <span class="nf">apply_changes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transitions</span><span class="p">,</span> <span class="n">productions</span><span class="p">):</span>
129         <span class="sd">&quot;&quot;&quot;Apply modifications to the compartments contained in the current</span>
130 <span class="sd">        StructuredView, according to `transitions` and</span>
131 <span class="sd">        `productions`. Dictionary `transitions` is keyed by a tuple of</span>
132 <span class="sd">        variables and associated with a list of dictionaries, either</span>
133 <span class="sd">        {&#39;population&#39;: qty, &#39;actions&#39;: list} or {&#39;agents&#39;: list,</span>
134 <span class="sd">        &#39;actions&#39;: list}. List `productions` contains tuples (target,</span>
135 <span class="sd">        {&#39;population&#39;: qty}, None) or (target, {&#39;agents&#39;: list},</span>
136 <span class="sd">        prototype).</span>
138 <span class="sd">        &quot;&quot;&quot;</span>
139         <span class="k">for</span> <span class="n">source</span><span class="p">,</span> <span class="n">evolutions</span> <span class="ow">in</span> <span class="n">transitions</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
140             <span class="k">for</span> <span class="n">target</span><span class="p">,</span> <span class="n">population_or_agents</span> <span class="ow">in</span> <span class="n">evolutions</span><span class="p">:</span>
141                 <span class="n">target_comp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_or_build</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">source</span><span class="o">=</span><span class="bp">self</span><span class="p">[</span><span class="n">source</span><span class="p">])</span>
142                 <span class="bp">self</span><span class="o">.</span><span class="n">_content</span><span class="p">[</span><span class="n">source</span><span class="p">]</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span>
143                     <span class="n">target_comp</span><span class="p">,</span>
144                     <span class="n">state_machine</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="p">,</span>
145                     <span class="o">**</span><span class="n">population_or_agents</span><span class="p">)</span>
146         <span class="bp">self</span><span class="o">.</span><span class="n">new_population</span> <span class="o">=</span> <span class="n">productions</span></div>
148 <div class="viewcode-block" id="GroupManager.evolve"><a class="viewcode-back" href="../../../../emulsion.agent.managers.html#emulsion.agent.managers.group_manager.GroupManager.evolve">[docs]</a>    <span class="k">def</span> <span class="nf">evolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">machine</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
149         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">machine</span><span class="o">=</span><span class="n">machine</span><span class="p">)</span>
150         <span class="c1">## DEBUG</span>
151         <span class="c1"># print(self.statevars.step, self.counts)</span>
152         <span class="bp">self</span><span class="o">.</span><span class="n">evolve_states</span><span class="p">()</span>
153         <span class="c1">## DEBUG</span>
154         <span class="c1"># print(self.statevars.step, self.counts)</span>
155         <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">comp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_content</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
156             <span class="k">if</span> <span class="n">comp</span><span class="o">.</span><span class="n">autoremove</span><span class="p">:</span>
157                 <span class="n">agents_or_population</span> <span class="o">=</span> <span class="n">comp</span><span class="o">.</span><span class="n">get_content</span><span class="p">()</span>
158                 <span class="k">if</span> <span class="n">agents_or_population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span>
159                     <span class="n">agents_or_population</span> <span class="o">=</span> <span class="p">(</span><span class="n">agents_or_population</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
160                                             <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">process_name</span><span class="p">:</span> <span class="p">{</span>
161                                                 <span class="n">key</span><span class="p">:</span> <span class="n">agents_or_population</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
162                                             <span class="p">}})</span>
163                 <span class="bp">self</span><span class="o">.</span><span class="n">_host</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">agents_or_population</span><span class="p">)</span>
164         <span class="bp">self</span><span class="o">.</span><span class="n">update_counts</span><span class="p">()</span></div>
165         <span class="c1">## DEBUG</span>
166         <span class="c1"># print(self.statevars.step, self.counts)</span>
168 <div class="viewcode-block" id="GroupManager.evolve_states"><a class="viewcode-back" href="../../../../emulsion.agent.managers.html#emulsion.agent.managers.group_manager.GroupManager.evolve_states">[docs]</a>    <span class="k">def</span> <span class="nf">evolve_states</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">machine</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
169         <span class="sd">&quot;&quot;&quot;Ask each compartment to make its content evolve according</span>
170 <span class="sd">        to its current state and the specified state_machine.</span>
172 <span class="sd">        &quot;&quot;&quot;</span>
173         <span class="bp">self</span><span class="o">.</span><span class="n">new_population</span> <span class="o">=</span> <span class="kc">None</span>
174         <span class="n">transitions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_evolve_transitions</span><span class="p">(</span><span class="n">machine</span><span class="o">=</span><span class="n">machine</span><span class="p">)</span>
175         <span class="n">productions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_evolve_productions</span><span class="p">(</span><span class="n">machine</span><span class="o">=</span><span class="n">machine</span><span class="p">)</span>
176         <span class="bp">self</span><span class="o">.</span><span class="n">apply_changes</span><span class="p">(</span><span class="n">transitions</span><span class="p">,</span> <span class="n">productions</span><span class="p">)</span></div>
178     <span class="k">def</span> <span class="nf">_evolve_transitions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">machine</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
179         <span class="c1"># init empty dictionary for all changes to perform</span>
180         <span class="n">future</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
181         <span class="c1"># iterate over all compartments</span>
182         <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">compart</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_content</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
183             <span class="n">future</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
184             <span class="c1"># compute the current population of each source compartment</span>
185             <span class="n">current_pop</span> <span class="o">=</span> <span class="n">compart</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span><span class="s1">&#39;population&#39;</span><span class="p">)</span>
186             <span class="c1"># no action if current pop &lt;= 0</span>
187             <span class="k">if</span> <span class="n">current_pop</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
188                 <span class="k">continue</span>
189             <span class="c1"># compute all possible transitions from the current state</span>
190             <span class="n">current_state</span> <span class="o">=</span> <span class="n">compart</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span>
191                 <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">machine_name</span><span class="p">)</span>
192             <span class="c1"># execute actions on stay for current state</span>
193             <span class="n">compart</span><span class="o">.</span><span class="n">do_state_actions</span><span class="p">(</span><span class="s1">&#39;on_stay&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="p">,</span>
194                                      <span class="n">current_state</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
195                                      <span class="o">**</span><span class="nb">dict</span><span class="p">([</span><span class="n">compart</span><span class="o">.</span><span class="n">get_content</span><span class="p">()]))</span>
196             <span class="c1"># get the possible transitions from the current state</span>
197             <span class="c1"># i.e. a list of tuples (state, flux, value, cond_result,</span>
198             <span class="c1"># actions) where:</span>
199             <span class="c1"># - state is a possible state reachable from the current state</span>
200             <span class="c1"># - flux is either &#39;rate&#39; or &#39;proba&#39; or &#39;amount&#39; or &#39;amount-all-but&#39;</span>
201             <span class="c1"># - value is the corresponding rate or probability or amount</span>
202             <span class="c1"># - cond_result is a tuple (either (&#39;population&#39;, qty) or</span>
203             <span class="c1"># (&#39;agents&#39;, list)) describing who fulfills the condition to cross</span>
204             <span class="c1"># the transition</span>
205             <span class="c1"># - actions is the list of actions on cross</span>
206             <span class="n">transitions</span> <span class="o">=</span> <span class="n">compart</span><span class="o">.</span><span class="n">next_states_from</span><span class="p">(</span><span class="n">current_state</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
207                                                    <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="p">)</span>
208             <span class="c1"># print(&#39;TRANSITIONS = &#39;, name, &#39;-&gt;&#39;, transitions)</span>
209             <span class="c1"># nothing to do if no transitions</span>
210             <span class="k">if</span> <span class="ow">not</span> <span class="n">transitions</span><span class="p">:</span>
211                 <span class="k">continue</span>
213             <span class="c1">### REWRITE TRANSITIONS TO HAVE DISJOINT SUB-POPULATIONS</span>
214             <span class="n">transitions_by_pop</span> <span class="o">=</span> <span class="n">group_and_split_populations</span><span class="p">(</span><span class="n">transitions</span><span class="p">)</span>
215             <span class="c1"># print(transitions_by_pop)</span>
216             <span class="k">for</span> <span class="n">ref_pop</span><span class="p">,</span> <span class="n">properties</span> <span class="ow">in</span> <span class="n">transitions_by_pop</span><span class="p">:</span>
217                 <span class="c1"># retrieve the list of states, the list of flux, the</span>
218                 <span class="c1"># list of values, the list of populations affected by</span>
219                 <span class="c1"># each possible transition</span>
220                 <span class="n">states</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">actions</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">properties</span><span class="p">)</span>
221                 <span class="c1"># print(name, &#39;-&gt;\n\t&#39;, states, values, [ag._agid</span>
222                 <span class="c1">#                                        for u in populations</span>
223                 <span class="c1">#                                        for ag in u[&#39;agents&#39;]])</span>
224                 <span class="c1"># add the current state to the possible destination states...</span>
225                 <span class="n">states</span> <span class="o">=</span> <span class="n">states</span> <span class="o">+</span> <span class="p">(</span><span class="n">current_state</span><span class="o">.</span><span class="n">name</span><span class="p">,)</span>
226                 <span class="c1"># ... with no action</span>
227                 <span class="n">actions</span> <span class="o">=</span> <span class="n">actions</span> <span class="o">+</span> <span class="p">([],</span> <span class="p">)</span>
228                 <span class="c1">#</span>
229                 <span class="n">values</span><span class="p">,</span> <span class="n">method</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_compute_values_for_unique_population</span><span class="p">(</span>
230                     <span class="n">values</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">ref_pop</span><span class="p">,</span> <span class="n">compart</span><span class="o">.</span><span class="n">stochastic</span><span class="p">)</span>
231                 <span class="n">change_list</span> <span class="o">=</span> <span class="n">compart</span><span class="o">.</span><span class="n">next_states</span><span class="p">(</span><span class="n">states</span><span class="p">,</span>
232                                                   <span class="n">values</span><span class="p">,</span>
233                                                   <span class="p">[</span><span class="n">ref_pop</span><span class="p">],</span>
234                                                   <span class="n">actions</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="n">method</span><span class="p">)</span>
235                 <span class="n">future</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">+=</span> <span class="n">rewrite_keys</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">current_state</span><span class="p">),</span>
236                                               <span class="n">change_list</span><span class="p">)</span>
237         <span class="c1"># print(&#39;FUTURE:&#39;, future)</span>
238         <span class="k">return</span> <span class="n">future</span>
241     <span class="k">def</span> <span class="nf">_evolve_productions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">machine</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
242         <span class="c1"># init empty list for all changes to perform</span>
243         <span class="n">future</span> <span class="o">=</span> <span class="p">[]</span>
244         <span class="c1"># iterate over all compartments</span>
245         <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">compart</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_content</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
246             <span class="c1"># compute the current population of each source compartment</span>
247             <span class="n">current_pop</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">compart</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span><span class="s1">&#39;population&#39;</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
248             <span class="c1"># no action if &quot;fake&quot; compartment</span>
249             <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="kc">None</span><span class="p">}:</span>
250                 <span class="k">continue</span>
251             <span class="c1"># # no action if current pop &lt;= 0</span>
252             <span class="c1"># if current_pop &lt;= 0:</span>
253             <span class="c1">#     continue</span>
254             <span class="c1"># compute all possible transitions from the current state</span>
255             <span class="n">current_state</span> <span class="o">=</span> <span class="n">compart</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">machine_name</span><span class="p">)</span>
256             <span class="c1"># get the possible transitions from the current state</span>
257             <span class="c1"># i.e. a list of tuples (state, flux, value, cond_result,</span>
258             <span class="c1"># prototype) where:</span>
259             <span class="c1"># - state is a possible state producible from the current state</span>
260             <span class="c1"># - flux is either &#39;rate&#39; or &#39;proba&#39; or &#39;amount&#39; or &#39;amount-all-but&#39;</span>
261             <span class="c1"># - value is the corresponding rate or probability or amount</span>
262             <span class="c1"># - cond_result is a tuple (either (&#39;population&#39;, qty) or</span>
263             <span class="c1"># (&#39;agents&#39;, list)) describing who fulfills the condition to cross</span>
264             <span class="c1"># the transition</span>
265             <span class="c1"># - prototype is the prototype for creating new agents</span>
266             <span class="n">productions</span> <span class="o">=</span> <span class="n">compart</span><span class="o">.</span><span class="n">production_from</span><span class="p">(</span><span class="n">current_state</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
267                                                   <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="p">)</span>
268             <span class="c1"># print(&#39;PRODUCTIONS = &#39;, productions)</span>
269             <span class="c1"># nothing to do if no transitions</span>
270             <span class="k">if</span> <span class="ow">not</span> <span class="n">productions</span><span class="p">:</span>
271                 <span class="k">continue</span>
272             <span class="c1">### HERE WE ASSUME THAT AN AGENT CAN PRODUCE SEVERAL OTHER</span>
273             <span class="c1">### AGENTS SIMULTANEOUSLY (OTHERWISE USE CONDITIONS)</span>
274             <span class="c1">### REWRITE TRANSITIONS TO HAVE DISJOINT SUB-POPULATIONS</span>
275             <span class="k">for</span> <span class="n">target_state</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">ref_pop</span><span class="p">,</span> <span class="n">proto</span> <span class="ow">in</span> <span class="n">productions</span><span class="p">:</span>
276                 <span class="n">pop_size</span> <span class="o">=</span> <span class="n">count_population</span><span class="p">(</span><span class="n">ref_pop</span><span class="p">)</span>
277                 <span class="n">amount</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_compute_production</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">pop_size</span><span class="p">,</span> <span class="n">compart</span><span class="o">.</span><span class="n">stochastic</span><span class="p">)</span>
278                 <span class="k">if</span> <span class="n">amount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
279                     <span class="n">future</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">target_state</span><span class="p">,</span> <span class="n">amount</span><span class="p">,</span> <span class="n">proto</span><span class="p">))</span>
280         <span class="c1"># print(&#39;FUTURE:&#39;, future)</span>
281         <span class="k">return</span> <span class="n">future</span></div>
282 </pre></div>
284           </div>
285         </div>
286       </div>
287       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
288         <div class="sphinxsidebarwrapper">
289 <h1 class="logo"><a href="../../../../index.html">EMULSION</a></h1>
293 <p class="blurb">Epidemiological Multi-Level Simulation Framework</p>
300 <h3>Navigation</h3>
301 <ul>
302 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Install.html">Installation</a></li>
303 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Getting_started.html">Getting started with EMULSION</a></li>
304 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Modelling_principles.html">Modelling principles</a></li>
305 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Modelling_language_basics.html">Modelling language (basics)</a></li>
306 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Modelling_language_advanced.html">Modelling language (advanced)</a></li>
307 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Feature_examples.html">Feature examples</a></li>
308 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Information.html">Information</a></li>
309 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/License.html">License</a></li>
310 <li class="toctree-l1"><a class="reference internal" href="../../../../emulsion_for_modellers.html">High-level functions for model designers</a></li>
311 <li class="toctree-l1"><a class="reference internal" href="../../../../emulsion.html">emulsion package</a></li>
312 </ul>
314 <div class="relations">
315 <h3>Related Topics</h3>
316 <ul>
317   <li><a href="../../../../index.html">Documentation overview</a><ul>
318   <li><a href="../../../index.html">Module code</a><ul>
319   </ul></li>
320   </ul></li>
321 </ul>
322 </div>
323 <div id="searchbox" style="display: none" role="search">
324   <h3>Quick search</h3>
325     <div class="searchformwrapper">
326     <form class="search" action="../../../../search.html" method="get">
327       <input type="text" name="q" />
328       <input type="submit" value="Go" />
329       <input type="hidden" name="check_keywords" value="yes" />
330       <input type="hidden" name="area" value="default" />
331     </form>
332     </div>
333 </div>
334 <script type="text/javascript">$('#searchbox').show(0);</script>
335         </div>
336       </div>
337       <div class="clearer"></div>
338     </div>
339     <div class="footer">
340       &copy;2016, INRA and Univ. Lille.
341       
342       |
343       Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.5</a>
344       &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
345       
346     </div>
348     
350     
351   </body>
352 </html>