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

875080972e1c03c5774ecbb5ee0b75baa0d3f78a
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.core.emulsion_agent &#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   <div class="document">
29     
30       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
31         <div class="sphinxsidebarwrapper">
32 <h1 class="logo"><a href="../../../../index.html">EMULSION</a></h1>
36 <p class="blurb">Epidemiological Multi-Level Simulation Framework</p>
43 <h3>Navigation</h3>
44 <ul>
45 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Install.html">Installation</a></li>
46 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Getting_started.html">Getting started with EMULSION</a></li>
47 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Modelling_principles.html">Modelling principles</a></li>
48 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Modelling_language_basics.html">Modelling language (basics)</a></li>
49 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Modelling_language_advanced.html">Modelling language (advanced)</a></li>
50 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Feature_examples.html">Feature examples</a></li>
51 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/Information.html">Information</a></li>
52 <li class="toctree-l1"><a class="reference internal" href="../../../../pages/License.html">License</a></li>
53 <li class="toctree-l1"><a class="reference internal" href="../../../../emulsion_for_modellers.html">High-level functions for model designers</a></li>
54 <li class="toctree-l1"><a class="reference internal" href="../../../../emulsion.html">emulsion package</a></li>
55 </ul>
57 <div class="relations">
58 <h3>Related Topics</h3>
59 <ul>
60   <li><a href="../../../../index.html">Documentation overview</a><ul>
61   <li><a href="../../../index.html">Module code</a><ul>
62   </ul></li>
63   </ul></li>
64 </ul>
65 </div>
66 <div id="searchbox" style="display: none" role="search">
67   <h3>Quick search</h3>
68     <div class="searchformwrapper">
69     <form class="search" action="../../../../search.html" method="get">
70       <input type="text" name="q" />
71       <input type="submit" value="Go" />
72       <input type="hidden" name="check_keywords" value="yes" />
73       <input type="hidden" name="area" value="default" />
74     </form>
75     </div>
76 </div>
77 <script type="text/javascript">$('#searchbox').show(0);</script>
78         </div>
79       </div>
80       <div class="documentwrapper">
81         <div class="bodywrapper">
82           <div class="body" role="main">
83             
84   <h1>Source code for emulsion.agent.core.emulsion_agent</h1><div class="highlight"><pre>
85 <span></span><span class="sd">&quot;&quot;&quot;</span>
86 <span class="sd">.. module:: emulsion.agent.core.emulsion_agent</span>
88 <span class="sd">.. moduleauthor:: Sébastien Picault &lt;sebastien.picault@inra.fr&gt;</span>
90 <span class="sd">Part of this code was adapted from the PADAWAN framework (S. Picault,</span>
91 <span class="sd">Univ. Lille).</span>
92 <span class="sd">&quot;&quot;&quot;</span>
94 <span class="c1"># [HEADER]</span>
96 <span class="kn">import</span> <span class="nn">abc</span>
98 <span class="kn">import</span> <span class="nn">numpy</span>                      <span class="k">as</span> <span class="nn">np</span>
101 <span class="kn">from</span>   <span class="nn">emulsion.tools.graph</span>       <span class="k">import</span> <span class="n">EdgeTypes</span>
102 <span class="kn">from</span>   <span class="nn">emulsion.model</span>             <span class="k">import</span> <span class="n">EDGE_KEYWORDS</span>
103 <span class="kn">from</span>   <span class="nn">emulsion.tools.misc</span>        <span class="k">import</span> <span class="n">rates_to_probabilities</span><span class="p">,</span>\
104     <span class="n">aggregate_probabilities</span><span class="p">,</span> <span class="n">probabilities_to_rates</span><span class="p">,</span> <span class="n">count_population</span><span class="p">,</span> <span class="n">AGENTS</span>
106 <span class="kn">from</span>   <span class="nn">emulsion.agent.core.abstract_agent</span>  <span class="k">import</span> <span class="n">AbstractAgent</span>
109 <span class="c1">#  ______                 _     _                                      _</span>
110 <span class="c1"># |  ____|               | |   (_)               /\                   | |</span>
111 <span class="c1"># | |__   _ __ ___  _   _| |___ _  ___  _ __    /  \   __ _  ___ _ __ | |_</span>
112 <span class="c1"># |  __| | &#39;_ ` _ \| | | | / __| |/ _ \| &#39;_ \  / /\ \ / _` |/ _ \ &#39;_ \| __|</span>
113 <span class="c1"># | |____| | | | | | |_| | \__ \ | (_) | | | |/ ____ \ (_| |  __/ | | | |_</span>
114 <span class="c1"># |______|_| |_| |_|\__,_|_|___/_|\___/|_| |_/_/    \_\__, |\___|_| |_|\__|</span>
115 <span class="c1">#                                                      __/ |</span>
116 <span class="c1">#                                                     |___/</span>
119 <div class="viewcode-block" id="EmulsionAgent"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent">[docs]</a><span class="k">class</span> <span class="nc">EmulsionAgent</span><span class="p">(</span><span class="n">AbstractAgent</span><span class="p">):</span>
120     <span class="sd">&quot;&quot;&quot;The EmulsionAgent is the base class for multi-level</span>
121 <span class="sd">    epidemiologic agents. An EmulsionAgent can represent any entity</span>
122 <span class="sd">    involved in the epidemiologic model (individual, compartment,</span>
123 <span class="sd">    herd, [meta]population, etc.).</span>
125 <span class="sd">    Each agent contains an exchange box (in &amp; out) which is composed</span>
126 <span class="sd">    of a list of messages. A message is generally a dictionary of</span>
127 <span class="sd">    Source/Destination (In/Out) and content of exchange with</span>
128 <span class="sd">    Source/Destination agent.</span>
130 <span class="sd">    &quot;&quot;&quot;</span>
131     <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">model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">host</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>
132         <span class="sd">&quot;&quot;&quot;Initialize the unit with a health state and a name.&quot;&quot;&quot;</span>
133         <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>
134         <span class="c1"># self.statevars.health_state = health_state</span>
135         <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
136         <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">=</span> <span class="n">level</span>
137         <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">name</span>
138         <span class="bp">self</span><span class="o">.</span><span class="n">_host</span> <span class="o">=</span> <span class="n">host</span>
139         <span class="k">if</span> <span class="s1">&#39;step&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">statevars</span><span class="p">:</span>
140             <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="o">=</span> <span class="mi">0</span>
141         <span class="c1"># exchange inbox/outbox</span>
142         <span class="bp">self</span><span class="o">.</span><span class="n">_inbox</span> <span class="o">=</span> <span class="p">[]</span>
143         <span class="bp">self</span><span class="o">.</span><span class="n">_outbox</span> <span class="o">=</span> <span class="p">[]</span>
146 <div class="viewcode-block" id="EmulsionAgent.upper_level"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.upper_level">[docs]</a>    <span class="k">def</span> <span class="nf">upper_level</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
147         <span class="sd">&quot;&quot;&quot;Return the &#39;upper level&#39; from this agent, i.e. the first host with</span>
148 <span class="sd">        a not-None level attribute.</span>
150 <span class="sd">        TAG: USER</span>
151 <span class="sd">        &quot;&quot;&quot;</span>
152         <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">init</span><span class="p">:</span>
153             <span class="k">return</span> <span class="bp">self</span>
154         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_host</span><span class="p">()</span><span class="o">.</span><span class="n">upper_level</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>
156     <span class="nd">@property</span>
157     <span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
158         <span class="sd">&quot;&quot;&quot;Return the name of the unit. If no name was provided during</span>
159 <span class="sd">        instantiation,  the class name is returned.</span>
161 <span class="sd">        TAG: USER</span>
162 <span class="sd">        &quot;&quot;&quot;</span>
163         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>\
164             <span class="k">else</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span> <span class="c1"># self.__class__.__name__</span>
166     <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
167         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
170 <div class="viewcode-block" id="EmulsionAgent.evolve"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.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>
171         <span class="sd">&quot;&quot;&quot;This method is aimed at defining what has systematically to</span>
172 <span class="sd">        be done in the unit at each time step (e.g. age change...). It</span>
173 <span class="sd">        has to be overriden if needed in subclasses.</span>
175 <span class="sd">        &quot;&quot;&quot;</span>
176         <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="o">+=</span> <span class="mi">1</span></div>
178 <div class="viewcode-block" id="EmulsionAgent.get_host"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.get_host">[docs]</a>    <span class="k">def</span> <span class="nf">get_host</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
179         <span class="sd">&quot;&quot;&quot;Return the host of the current unit.</span>
181 <span class="sd">        TAG: USER</span>
182 <span class="sd">        &quot;&quot;&quot;</span>
183         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_host</span></div>
185 <div class="viewcode-block" id="EmulsionAgent.get_content"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.get_content">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
186     <span class="k">def</span> <span class="nf">get_content</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
187         <span class="sd">&quot;&quot;&quot;Return either the population (number) of the current unit,</span>
188 <span class="sd">        or the list of agents contained in the current unit. The</span>
189 <span class="sd">        output is a tuple with either (&#39;population&#39;, qty) or (&#39;agents&#39;, list).</span>
191 <span class="sd">        &quot;&quot;&quot;</span>
192         <span class="k">pass</span></div>
194 <div class="viewcode-block" id="EmulsionAgent.do_state_actions"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.do_state_actions">[docs]</a>    <span class="k">def</span> <span class="nf">do_state_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event</span><span class="p">,</span> <span class="n">state_machine</span><span class="p">,</span> <span class="n">state_name</span><span class="p">,</span>
195                          <span class="n">population</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">_</span><span class="p">):</span>
196         <span class="sd">&quot;&quot;&quot;Perform the actions associated to the current state. If the</span>
197 <span class="sd">        unit is a ViewAgent, actions are actually performed by each</span>
198 <span class="sd">        unit of the specified agents list, in turn. Otherwise, the</span>
199 <span class="sd">        actions are performed according to the population, which is</span>
200 <span class="sd">        expected to be a number.</span>
202 <span class="sd">        &quot;&quot;&quot;</span>
203         <span class="c1"># if actions are actually associated to the current state of</span>
204         <span class="c1"># the state machine...</span>
205            <span class="c1"># ... and to the &#39;event&#39; (enter/exit/stay)</span>
206         <span class="k">if</span> <span class="n">state_name</span> <span class="ow">in</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">state_actions</span>\
207            <span class="ow">and</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">state_name</span><span class="p">]:</span>
208             <span class="c1"># print(f&#39;Doing state actions {event} for {state_machine}\n\tfor {state_name}&#39;)</span>
209             <span class="c1"># retrieve the list of actions</span>
210             <span class="n">l_actions</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">state_name</span><span class="p">][</span><span class="n">event</span><span class="p">]</span>
211             <span class="c1"># ask the current unit to perform the actions with the</span>
212             <span class="c1"># specified population</span>
213             <span class="c1"># print(f&#39;\t{l_actions}&#39;)</span>
214             <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="n">l_actions</span><span class="p">:</span>
215                 <span class="n">action</span><span class="o">.</span><span class="n">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">population</span><span class="o">=</span><span class="n">population</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="n">agents</span><span class="p">)</span></div>
217 <div class="viewcode-block" id="EmulsionAgent.do_edge_actions"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.do_edge_actions">[docs]</a>    <span class="k">def</span> <span class="nf">do_edge_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">actions</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">population</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
218         <span class="sd">&quot;&quot;&quot;Perform the actions associated to a transition between</span>
219 <span class="sd">        states. If the unit is a ViewCompartment, actions are actually</span>
220 <span class="sd">        performed by each unit of the specified agents list, in</span>
221 <span class="sd">        turn. Otherwise, the actions are performed according to the</span>
222 <span class="sd">        population, which is expected to be a number.</span>
224 <span class="sd">        &quot;&quot;&quot;</span>
225         <span class="c1"># # if actions are actually associated to the current edge...</span>
226         <span class="c1">#    # ... and to the &#39;event&#39; (cross)</span>
227         <span class="c1"># if from_ in state_machine.edge_actions\</span>
228         <span class="c1">#    and to_ in state_machine.edge_actions[from_]\</span>
229         <span class="c1">#    and event in state_machine.edge_actions[from_][to_]:</span>
230         <span class="c1">#     # retrieve the list of actions</span>
231         <span class="c1">#     l_actions = state_machine.edge_actions[from_][to_][event]</span>
232         <span class="c1">#     # ask the current unit to perform the actions with the</span>
233         <span class="c1">#     # specified population</span>
234         <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="n">actions</span><span class="p">:</span>
235 <span class="c1">#            print(action)</span>
236             <span class="n">action</span><span class="o">.</span><span class="n">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">population</span><span class="o">=</span><span class="n">population</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="n">agents</span><span class="p">)</span></div>
239 <div class="viewcode-block" id="EmulsionAgent.next_states_from"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.next_states_from">[docs]</a>    <span class="k">def</span> <span class="nf">next_states_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_state</span><span class="p">,</span> <span class="n">state_machine</span><span class="p">):</span>
240         <span class="sd">&quot;&quot;&quot;Return a list of tuples composed of:</span>
242 <span class="sd">        - each of the possible states reachable from the specified</span>
243 <span class="sd">        initial state (some depending on a condition)</span>
245 <span class="sd">        - the transition rate, probability or amount to each state in</span>
246 <span class="sd">        the specified state_machine (possible clauses: &#39;rate&#39;,</span>
247 <span class="sd">        &#39;proba&#39;, &#39;amount&#39;, &#39;amount-all-but&#39; with the actual value)</span>
249 <span class="sd">        - a tuple indicating who can cross the edge, depending on</span>
250 <span class="sd">        conditions (either (&#39;population&#39;, qty) or (&#39;agents&#39;, list))</span>
252 <span class="sd">        - the list of actions to perform when crossing the edge (if</span>
253 <span class="sd">        any).</span>
255 <span class="sd">        The conditions, rates and probabilities may depend on the state</span>
256 <span class="sd">        variables or properties of the current unit.</span>
258 <span class="sd">        &quot;&quot;&quot;</span>
259         <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
260         <span class="c1"># remove unfulfilled &#39;when&#39; clauses if any</span>
261         <span class="k">for</span> <span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">props</span><span class="p">)</span> <span class="ow">in</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">edges_from</span><span class="p">(</span><span class="n">initial_state</span><span class="p">,</span>
262                                                              <span class="n">type_id</span><span class="o">=</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">):</span>
263             <span class="k">if</span> <span class="s1">&#39;when&#39;</span> <span class="ow">in</span> <span class="n">props</span><span class="p">:</span>
264                 <span class="n">when</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">props</span><span class="p">[</span><span class="s1">&#39;when&#39;</span><span class="p">])</span>
265                 <span class="n">fulfilled</span> <span class="o">=</span> <span class="n">when</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">when</span><span class="p">)</span> <span class="k">else</span> <span class="n">when</span>
266                 <span class="k">if</span> <span class="ow">not</span> <span class="n">fulfilled</span><span class="p">:</span>
267                     <span class="k">continue</span>
268             <span class="n">cond_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_content</span><span class="p">()</span>
269             <span class="c1"># if any condition, evaluate it</span>
270             <span class="k">if</span> <span class="s1">&#39;cond&#39;</span> <span class="ow">in</span> <span class="n">props</span><span class="p">:</span>
271                 <span class="n">cond</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">props</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">])</span>
272                 <span class="c1"># compute the content tuple (either (&#39;population&#39;, qty)</span>
273                 <span class="c1"># or (&#39;agents&#39;, lsit)) which fulfils the condition</span>
274                 <span class="n">cond_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate_condition</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
275             <span class="c1"># only edges with condition fulfilled are taken into account</span>
276             <span class="k">if</span> <span class="n">cond_result</span><span class="p">:</span>
277                 <span class="c1"># print(cond_result)</span>
278                 <span class="n">flux</span> <span class="o">=</span> <span class="kc">None</span>
279                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">EDGE_KEYWORDS</span><span class="p">:</span>
280                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">props</span><span class="p">:</span>
281                         <span class="n">flux</span> <span class="o">=</span> <span class="n">keyword</span>
282                         <span class="k">break</span>
283                 <span class="n">value</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">props</span><span class="p">[</span><span class="n">flux</span><span class="p">])</span>
284                 <span class="n">actions</span> <span class="o">=</span> <span class="n">props</span><span class="p">[</span><span class="s1">&#39;actions&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="s1">&#39;actions&#39;</span> <span class="ow">in</span> <span class="n">props</span> <span class="k">else</span> <span class="p">[]</span>
285                 <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
286                     <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
287                 <span class="k">if</span> <span class="n">value</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">flux</span> <span class="o">==</span> <span class="s1">&#39;amount-all-but&#39;</span><span class="p">:</span>
288                     <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">state</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">cond_result</span><span class="p">,</span> <span class="n">actions</span><span class="p">))</span>
289         <span class="k">return</span> <span class="n">result</span></div>
291 <div class="viewcode-block" id="EmulsionAgent.production_from"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.production_from">[docs]</a>    <span class="k">def</span> <span class="nf">production_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_state</span><span class="p">,</span> <span class="n">state_machine</span><span class="p">):</span>
292         <span class="sd">&quot;&quot;&quot;Return a list of tuples composed of:</span>
294 <span class="sd">        - each of the possible states that can be produced from the specified</span>
295 <span class="sd">        initial state (some depending on a condition)</span>
297 <span class="sd">        - the transition rate, probability or amount to each state in</span>
298 <span class="sd">        the specified state_machine (possible clauses: &#39;rate&#39;,</span>
299 <span class="sd">        &#39;proba&#39;, &#39;amount&#39;, &#39;amount-all-but&#39; with the actual value)</span>
301 <span class="sd">        - a tuple indicating who can produce cross the edge, depending on</span>
302 <span class="sd">        conditions (either (&#39;population&#39;, qty) or (&#39;agents&#39;, list))</span>
304 <span class="sd">        - the prototypes of agents produced through the edge (if</span>
305 <span class="sd">        any).</span>
307 <span class="sd">        The conditions, rates and probabilities may depend on the state</span>
308 <span class="sd">        variables or properties of the current unit.</span>
310 <span class="sd">        &quot;&quot;&quot;</span>
311         <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
312         <span class="c1"># remove unfulfilled &#39;when&#39; clauses if any</span>
313         <span class="k">for</span> <span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">props</span><span class="p">)</span> <span class="ow">in</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">edges_from</span><span class="p">(</span><span class="n">initial_state</span><span class="p">,</span>
314                                                              <span class="n">type_id</span><span class="o">=</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">PRODUCTION</span><span class="p">):</span>
315             <span class="k">if</span> <span class="s1">&#39;when&#39;</span> <span class="ow">in</span> <span class="n">props</span><span class="p">:</span>
316                 <span class="n">when</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">props</span><span class="p">[</span><span class="s1">&#39;when&#39;</span><span class="p">])</span>
317                 <span class="n">fulfilled</span> <span class="o">=</span> <span class="n">when</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">when</span><span class="p">)</span> <span class="k">else</span> <span class="n">when</span>
318                 <span class="k">if</span> <span class="ow">not</span> <span class="n">fulfilled</span><span class="p">:</span>
319                     <span class="k">continue</span>
320             <span class="n">cond_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_content</span><span class="p">()</span>
321             <span class="c1"># if any condition, evaluate it</span>
322             <span class="k">if</span> <span class="s1">&#39;cond&#39;</span> <span class="ow">in</span> <span class="n">props</span><span class="p">:</span>
323                 <span class="n">cond</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">props</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">])</span>
324                 <span class="c1"># compute the content tuple (either (&#39;population&#39;, qty)</span>
325                 <span class="c1"># or (&#39;agents&#39;, lsit)) which fulfils the condition</span>
326                 <span class="n">cond_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate_condition</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
327             <span class="c1"># only edges with condition fulfilled are taken into account</span>
328             <span class="k">if</span> <span class="n">cond_result</span><span class="p">:</span>
329                 <span class="c1"># print(cond_result)</span>
330                 <span class="n">flux</span> <span class="o">=</span> <span class="kc">None</span>
331                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">EDGE_KEYWORDS</span><span class="p">:</span>
332                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">props</span><span class="p">:</span>
333                         <span class="n">flux</span> <span class="o">=</span> <span class="n">keyword</span>
334                         <span class="k">break</span>
335                 <span class="n">value</span> <span class="o">=</span> <span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">props</span><span class="p">[</span><span class="n">flux</span><span class="p">])</span>
336                 <span class="n">protos</span> <span class="o">=</span> <span class="n">props</span><span class="p">[</span><span class="s1">&#39;prototype&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="s1">&#39;prototype&#39;</span> <span class="ow">in</span> <span class="n">props</span> <span class="k">else</span> <span class="kc">None</span>
337                 <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
338                     <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
339                 <span class="k">if</span> <span class="n">value</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">flux</span> <span class="o">==</span> <span class="s1">&#39;amount-all-but&#39;</span><span class="p">:</span>
340                     <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">state</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">cond_result</span><span class="p">,</span> <span class="n">protos</span><span class="p">))</span>
341         <span class="k">return</span> <span class="n">result</span></div>
344     <span class="k">def</span> <span class="nf">_compute_production</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</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">stochastic</span><span class="p">):</span>
345         <span class="sd">&quot;&quot;&quot;Compute the values of production edges.</span>
347 <span class="sd">        &quot;&quot;&quot;</span>
348         <span class="c1"># ALL PRODUCTIONS COME FROM THE SAME POPULATION</span>
349         <span class="c1"># (reference_pop) The `values` can represent : 1) amounts - in</span>
350         <span class="c1"># that case no transformation occurs, 2) probabilities - in</span>
351         <span class="c1"># that case the values must be converted to rates if the</span>
352         <span class="c1"># target compartment is deterministic, otherwise the step</span>
353         <span class="c1"># duration must be taken into account; 3) rates - computed</span>
354         <span class="c1"># using Poisson distribution if the target compartment is</span>
355         <span class="c1"># stochastic</span>
356         <span class="k">if</span> <span class="n">flux</span> <span class="o">==</span> <span class="s1">&#39;amount-all-but&#39;</span><span class="p">:</span>
357             <span class="n">amount</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">pop_size</span> <span class="o">-</span> <span class="n">value</span><span class="p">)</span>
358         <span class="k">elif</span> <span class="n">flux</span> <span class="o">==</span> <span class="s1">&#39;amount&#39;</span><span class="p">:</span>
359             <span class="n">amount</span> <span class="o">=</span> <span class="n">value</span>
360         <span class="k">elif</span> <span class="n">flux</span> <span class="o">==</span> <span class="s1">&#39;proba&#39;</span><span class="p">:</span>
361             <span class="c1"># if stochastic uses binomial</span>
362             <span class="k">if</span> <span class="n">stochastic</span><span class="p">:</span>
363                 <span class="c1"># aggregate probability wrt the time step duration</span>
364                 <span class="n">value</span> <span class="o">=</span> <span class="n">aggregate_probabilities</span><span class="p">([</span><span class="n">value</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
365                 <span class="n">amount</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">binomial</span><span class="p">(</span><span class="n">pop_size</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
366             <span class="k">else</span><span class="p">:</span>
367                 <span class="c1"># otherwise treat proba as proportion</span>
368                 <span class="n">amount</span> <span class="o">=</span> <span class="n">pop_size</span> <span class="o">*</span> <span class="n">value</span>
369         <span class="k">elif</span> <span class="n">flux</span> <span class="o">==</span> <span class="s1">&#39;rate&#39;</span><span class="p">:</span>
370             <span class="c1"># if stochastic: use Poisson distribution</span>
371             <span class="k">if</span> <span class="n">stochastic</span><span class="p">:</span>
372                 <span class="c1"># amount = np.random.poisson(value * pop_size * self.model.delta_t)</span>
373                 <span class="n">amount</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">binomial</span><span class="p">(</span><span class="n">pop_size</span><span class="p">,</span> <span class="mi">1</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">value</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">))</span>
374             <span class="k">else</span><span class="p">:</span>
375                 <span class="c1"># consider rate as a speed</span>
376                 <span class="c1"># S = S0 exp(rate * dt) =&gt; S += S * (exp(rate*dt) - 1)</span>
377                 <span class="c1"># amount = value * pop_size * self.model.delta_t</span>
378                 <span class="n">amount</span> <span class="o">=</span> <span class="n">pop_size</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">value</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
379         <span class="k">else</span><span class="p">:</span>                   <span class="c1"># should not happen !</span>
380             <span class="n">amount</span> <span class="o">=</span> <span class="kc">None</span>
381         <span class="k">return</span> <span class="n">amount</span>
384     <span class="k">def</span> <span class="nf">_compute_values_for_unique_population</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
385                                               <span class="n">values</span><span class="p">,</span>
386                                               <span class="n">flux</span><span class="p">,</span>
387                                               <span class="n">reference_pop</span><span class="p">,</span>
388                                               <span class="n">stochastic</span><span class="p">):</span>
389         <span class="sd">&quot;&quot;&quot;Restructure the values according to the situation, for</span>
390 <span class="sd">        edges concerning the same population.</span>
392 <span class="sd">        &quot;&quot;&quot;</span>
393         <span class="c1"># ALL TRANSITIONS AFFECT THE SAME POPULATION (reference_pop)</span>
394         <span class="c1"># The `values` can represent : 1) amounts - in that case no</span>
395         <span class="c1"># transformation occurs, 2) probabilities - in that case the</span>
396         <span class="c1"># values must be converted to rates if the target compartment</span>
397         <span class="c1"># is deterministic, otherwise the step duration must be taken</span>
398         <span class="c1"># into account; 3) rates - in that case the values must be</span>
399         <span class="c1"># converted to probabilities if the target compartment is</span>
400         <span class="c1"># stochastic</span>
401         <span class="c1"># print(&#39;IDENTICAL&#39;)</span>
402         <span class="n">available_flux</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">flux</span><span class="p">)</span>
403         <span class="c1"># try:</span>
404         <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">available_flux</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">available_flux</span> <span class="o">==</span> <span class="nb">set</span><span class="p">([</span><span class="s1">&#39;amount&#39;</span><span class="p">,</span> <span class="s1">&#39;amount-all-but&#39;</span><span class="p">])</span>
405         <span class="c1"># except:</span>
406         <span class="c1">#     print(available_flux)</span>
408         <span class="n">method</span> <span class="o">=</span> <span class="kc">None</span>
409         <span class="k">if</span> <span class="s1">&#39;amount&#39;</span> <span class="ow">in</span> <span class="n">available_flux</span> <span class="ow">or</span> <span class="s1">&#39;amount-all-but&#39;</span> <span class="ow">in</span> <span class="n">available_flux</span><span class="p">:</span>
410             <span class="c1"># handle values as amounts</span>
411             <span class="n">method</span> <span class="o">=</span> <span class="s1">&#39;amount&#39;</span>
412             <span class="n">total_ref_pop</span> <span class="o">=</span> <span class="n">count_population</span><span class="p">(</span><span class="n">reference_pop</span><span class="p">)</span>
413             <span class="c1"># check that values are between 0 and the population size,</span>
414             <span class="c1"># if needed invert &#39;amount-all-but&#39; values</span>
415             <span class="n">values</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">([</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">total_ref_pop</span><span class="o">-</span><span class="n">v</span><span class="p">,</span> <span class="n">total_ref_pop</span><span class="p">))</span>\
416                               <span class="k">if</span> <span class="n">f</span> <span class="o">==</span> <span class="s1">&#39;amount-all-but&#39;</span>\
417                               <span class="k">else</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">total_ref_pop</span><span class="p">))</span>
418                             <span class="k">for</span> <span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">flux</span><span class="p">,</span> <span class="n">values</span><span class="p">)])</span>
419             <span class="c1"># when finished the length of values is the number of</span>
420             <span class="c1"># outgoing edges</span>
421             <span class="c1"># print(&#39;AMOUNT&#39;, values)</span>
422         <span class="k">elif</span> <span class="s1">&#39;proba&#39;</span> <span class="ow">in</span> <span class="n">available_flux</span><span class="p">:</span>
423             <span class="k">if</span> <span class="ow">not</span> <span class="n">stochastic</span><span class="p">:</span>
424                 <span class="c1"># then if the target compartment is deterministic,</span>
425                 <span class="c1"># probabilities must be converted into rates</span>
426                 <span class="c1"># print(&#39;PROBA -&gt; RATES&#39;, values)</span>
427                 <span class="n">values</span> <span class="o">=</span> <span class="n">probabilities_to_rates</span><span class="p">(</span><span class="n">values</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">),))</span>
428                 <span class="c1"># when finished the length of values is the number of</span>
429                 <span class="c1"># outgoing edges</span>
430                 <span class="c1"># print(values)</span>
431             <span class="k">else</span><span class="p">:</span>
432                 <span class="c1"># aggregate probabilities wrt the time step duration</span>
433                 <span class="n">values</span> <span class="o">=</span> <span class="n">aggregate_probabilities</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)</span>
434                 <span class="n">values</span> <span class="o">=</span> <span class="n">values</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">),)</span>
435                 <span class="c1"># when finished the length of values is the number of</span>
436                 <span class="c1"># outgoing edges + 1</span>
437                 <span class="c1"># print(&#39;PROBA&#39;, values)</span>
438         <span class="k">elif</span> <span class="ow">not</span> <span class="n">stochastic</span><span class="p">:</span>
439             <span class="c1"># print(&#39;RATES&#39;, values)</span>
440             <span class="k">pass</span>
441         <span class="k">else</span><span class="p">:</span>
442             <span class="c1"># otherwise values are transformed from rates to</span>
443             <span class="c1"># probabilities</span>
444             <span class="n">values</span> <span class="o">=</span> <span class="n">rates_to_probabilities</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">),</span>
445                                             <span class="n">values</span><span class="p">,</span>
446                                             <span class="n">delta_t</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)</span>
447             <span class="c1"># when finished the length of values is the number of</span>
448             <span class="c1"># outgoing edges + 1</span>
449             <span class="c1"># print(&quot;RATES -&gt; PROBAS&quot;, values)</span>
450         <span class="k">return</span> <span class="n">values</span><span class="p">,</span> <span class="n">method</span>
452     <span class="k">def</span> <span class="nf">_compute_values_for_multiple_populations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
453                                                  <span class="n">values</span><span class="p">,</span>
454                                                  <span class="n">flux</span><span class="p">,</span>
455                                                  <span class="n">populations</span><span class="p">,</span>
456                                                  <span class="n">stochastic</span><span class="p">):</span>
457         <span class="sd">&quot;&quot;&quot;Restructure the values according to the situation, for</span>
458 <span class="sd">        edges concerning distinct populations.</span>
460 <span class="sd">        &quot;&quot;&quot;</span>
461         <span class="c1"># IN THAT CASE, EACH POSSIBLE TRANSITION IS RELATED TO A</span>
462         <span class="c1"># SPECIFIC SUBGROUP OF THE COMPART.</span>
463         <span class="c1">### IMPORTANT: we assume that all conditions are disjoint,</span>
464         <span class="c1">### i.e. there is no intersection between the populations. IF</span>
465         <span class="c1">### NOT THE CASE, this means that the model is not really</span>
466         <span class="c1">### consistent, and the calculation of probabilities should be</span>
467         <span class="c1">### done on each individual... thus why use a StructuredView</span>
468         <span class="c1">### instead of a set of EvolvingAtom ???</span>
469         <span class="c1"># print(&#39;MULTIPLE&#39;)</span>
470         <span class="n">pop_sets</span> <span class="o">=</span> <span class="p">[</span><span class="n">SortedSet</span><span class="p">(</span><span class="n">pop</span><span class="p">[</span><span class="n">AGENTS</span><span class="p">])</span> <span class="k">for</span> <span class="n">pop</span> <span class="ow">in</span> <span class="n">populations</span><span class="p">]</span>
471         <span class="c1"># pop_sets must be disjoint</span>
472         <span class="k">assert</span><span class="p">(</span><span class="ow">not</span> <span class="nb">any</span><span class="p">([</span><span class="n">pop_sets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&amp;</span> <span class="n">pop_sets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
473                         <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">pop_sets</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
474                         <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">pop_sets</span><span class="p">))]))</span>
475         <span class="c1"># binomial values must be computed for each transition to</span>
476         <span class="c1"># determine how many units among the candidates will cross the</span>
477         <span class="c1"># transition, thus we need probabilities. If all values are</span>
478         <span class="c1"># given as rates, they must be transformed</span>
479         <span class="n">method</span> <span class="o">=</span> <span class="kc">None</span>
480         <span class="n">available_flux</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">flux</span><span class="p">)</span>
481         <span class="c1"># try:</span>
482         <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">available_flux</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">available_flux</span> <span class="o">==</span> <span class="nb">set</span><span class="p">([</span><span class="s1">&#39;amount&#39;</span><span class="p">,</span> <span class="s1">&#39;amount-all-but&#39;</span><span class="p">])</span>
483         <span class="c1"># except:</span>
484         <span class="c1">#     print(available_flux)</span>
486         <span class="k">if</span> <span class="n">available_flux</span> <span class="o">==</span> <span class="p">{</span><span class="s1">&#39;rate&#39;</span><span class="p">}</span> <span class="ow">and</span> <span class="n">stochastic</span><span class="p">:</span>
487             <span class="c1"># values are transformed from rates to probabilities</span>
488             <span class="n">values</span> <span class="o">=</span> <span class="n">rates_to_probabilities</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">),</span>
489                                             <span class="n">values</span><span class="p">,</span>
490                                             <span class="n">delta_t</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)</span>
491             <span class="c1"># print(&#39;RATES -&gt; PROBAS&#39;, values)</span>
492         <span class="k">elif</span> <span class="s1">&#39;amount&#39;</span> <span class="ow">in</span> <span class="n">available_flux</span> <span class="ow">or</span> <span class="s1">&#39;amount-all-but&#39;</span> <span class="ow">in</span> <span class="n">available_flux</span><span class="p">:</span>
493             <span class="n">method</span> <span class="o">=</span> <span class="s1">&#39;amount&#39;</span>
494             <span class="n">pops</span> <span class="o">=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pop_sets</span><span class="p">]</span>
495             <span class="n">values</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">([</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">pop</span><span class="o">-</span><span class="n">v</span><span class="p">,</span> <span class="n">pop</span><span class="p">))</span>\
496                               <span class="k">if</span> <span class="n">f</span> <span class="o">==</span> <span class="s1">&#39;amount-all-but&#39;</span>\
497                               <span class="k">else</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">pop</span><span class="p">))</span>
498                             <span class="k">for</span> <span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">pop</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</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">pops</span><span class="p">)])</span>
499             <span class="n">values</span> <span class="o">=</span> <span class="n">values</span> <span class="c1">#+ (1 - sum(values),)</span>
500             <span class="c1"># print(&#39;AMOUNTS:&#39;, values)</span>
501         <span class="k">elif</span> <span class="s1">&#39;proba&#39;</span> <span class="ow">in</span> <span class="n">available_flux</span><span class="p">:</span>
502             <span class="k">if</span> <span class="n">stochastic</span><span class="p">:</span>
503                 <span class="c1"># values must be aggregated wrt the times step duration</span>
504                 <span class="n">values</span> <span class="o">=</span> <span class="n">aggregate_probabilities</span><span class="p">(</span><span class="n">values</span><span class="p">,</span>
505                                                  <span class="n">delta_t</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)</span>
506                 <span class="n">values</span> <span class="o">=</span> <span class="n">values</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">),)</span>
507                 <span class="c1"># print(&#39;PROBAS:&#39;, values)</span>
508             <span class="k">else</span><span class="p">:</span>
509                 <span class="c1"># values must be transformed from probabilities to rates</span>
510                 <span class="c1"># print(&#39;PROBABILITIES -&gt; RATES&#39;, values)</span>
511                 <span class="n">values</span> <span class="o">=</span> <span class="n">probabilities_to_rates</span><span class="p">(</span><span class="n">values</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">),))</span>
512                 <span class="c1"># print(values)</span>
513         <span class="k">else</span><span class="p">:</span>
514             <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;ERROR - DETERMINISTIC COMPARTMENT MODEL MUST NOT&#39;</span>
515                   <span class="s1">&#39; TRY TO HANDLE SEPARATE SUBPOPULATIONS&#39;</span><span class="p">)</span>
516         <span class="k">return</span> <span class="n">values</span><span class="p">,</span> <span class="n">method</span>
519 <div class="viewcode-block" id="EmulsionAgent.evaluate_condition"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.evaluate_condition">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate_condition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">condition</span><span class="p">):</span>
520         <span class="sd">&quot;&quot;&quot;Return the content (tuple either (&#39;population&#39;, qty) or</span>
521 <span class="sd">        (&#39;agents&#39;, list)) if the condition is fulfilled, () otherwise.</span>
523 <span class="sd">        &quot;&quot;&quot;</span>
524         <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span>
525             <span class="n">condition</span> <span class="o">=</span> <span class="n">condition</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
526         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_content</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="p">()</span></div>
528 <div class="viewcode-block" id="EmulsionAgent.evaluate_event"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.evaluate_event">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate_event</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
529         <span class="sd">&quot;&quot;&quot;Evaluate if the specified event name is fulfilled, using a</span>
530 <span class="sd">        calendar. The calendar is determined dynamically according to</span>
531 <span class="sd">        the name of the event.</span>
533 <span class="sd">        TAG: USER</span>
534 <span class="sd">        &quot;&quot;&quot;</span>
535         <span class="c1"># print(self, &#39;evaluating event&#39;, name, &quot;at&quot;, self.statevars.step)</span>
536         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_calendar_for_event</span><span class="p">(</span><span class="n">name</span><span class="p">)[</span><span class="n">name</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></div>
538 <div class="viewcode-block" id="EmulsionAgent.is_in_state"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.is_in_state">[docs]</a>    <span class="k">def</span> <span class="nf">is_in_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_name</span><span class="p">):</span>
539         <span class="sd">&quot;&quot;&quot;Evaluate if the agent is in the specified state. The state name is</span>
540 <span class="sd">        expected to be a valid state of one of the state machines. If</span>
541 <span class="sd">        so, the name of the state machine is expected to be one of the</span>
542 <span class="sd">        statevars of the agent.</span>
544 <span class="sd">        &quot;&quot;&quot;</span>
545         <span class="c1"># retrieve name of the state machine associated with the state_name</span>
546         <span class="n">varname</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_model_value</span><span class="p">(</span><span class="n">state_name</span><span class="p">)</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">machine_name</span>
547         <span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">statevars</span><span class="p">[</span><span class="n">varname</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">state_name</span>\
548                  <span class="k">if</span> <span class="n">varname</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">statevars</span> <span class="k">else</span> <span class="kc">False</span>
549         <span class="c1"># print(self, &#39;testing var:&#39;, varname, &#39; = &#39;, result)</span>
550         <span class="k">return</span> <span class="n">result</span></div>
552     <span class="c1"># def time_before_event(self, event_name):</span>
553     <span class="c1">#     &quot;&quot;&quot;Return the time to wait from the current time to the specified</span>
554     <span class="c1">#     *event_name*.</span>
555     <span class="c1">#     &quot;&quot;&quot;</span>
556     <span class="c1">#     pass self.model.get_cal</span>
558 <div class="viewcode-block" id="EmulsionAgent.get_outbox"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.get_outbox">[docs]</a>    <span class="k">def</span> <span class="nf">get_outbox</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
559         <span class="sd">&quot;&quot;&quot;Return the content of the outbox.</span>
561 <span class="sd">        TAG: USER</span>
562 <span class="sd">        &quot;&quot;&quot;</span>
563         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outbox</span></div>
565 <div class="viewcode-block" id="EmulsionAgent.add_outbox"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.add_outbox">[docs]</a>    <span class="k">def</span> <span class="nf">add_outbox</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
566         <span class="sd">&quot;&quot;&quot;Appendd a message to the outbox.</span>
568 <span class="sd">        TAG: USER</span>
569 <span class="sd">        &quot;&quot;&quot;</span>
570         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outbox</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">message</span><span class="p">)</span></div>
572 <div class="viewcode-block" id="EmulsionAgent.reset_outbox"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.reset_outbox">[docs]</a>    <span class="k">def</span> <span class="nf">reset_outbox</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
573         <span class="sd">&quot;&quot;&quot;Reset the content of the outbox.</span>
575 <span class="sd">        TAG: USER</span>
576 <span class="sd">        &quot;&quot;&quot;</span>
577         <span class="bp">self</span><span class="o">.</span><span class="n">_outbox</span> <span class="o">=</span> <span class="p">[]</span></div>
579 <div class="viewcode-block" id="EmulsionAgent.add_inbox"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.add_inbox">[docs]</a>    <span class="k">def</span> <span class="nf">add_inbox</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">messages</span><span class="o">=</span><span class="p">[]):</span>
580         <span class="sd">&quot;&quot;&quot;Add the specified list of messages in the inbox.</span>
582 <span class="sd">        TAG: USER&quot;&quot;&quot;</span>
583         <span class="bp">self</span><span class="o">.</span><span class="n">_inbox</span> <span class="o">+=</span> <span class="n">messages</span></div>
585 <div class="viewcode-block" id="EmulsionAgent.clean_inbox"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.clean_inbox">[docs]</a>    <span class="k">def</span> <span class="nf">clean_inbox</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
586         <span class="sd">&quot;&quot;&quot;Remove non sticky messages from the inbox.</span>
588 <span class="sd">        TAG: USER</span>
589 <span class="sd">        &quot;&quot;&quot;</span>
590         <span class="bp">self</span><span class="o">.</span><span class="n">_inbox</span> <span class="o">=</span> <span class="p">[</span><span class="n">message</span> <span class="k">for</span> <span class="n">message</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_inbox</span> <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;sticky&#39;</span><span class="p">)]</span></div>
592 <div class="viewcode-block" id="EmulsionAgent.checkout_inbox"><a class="viewcode-back" href="../../../../emulsion.agent.core.html#emulsion.agent.core.emulsion_agent.EmulsionAgent.checkout_inbox">[docs]</a>    <span class="k">def</span> <span class="nf">checkout_inbox</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
593         <span class="sd">&quot;&quot;&quot;Pick up agent&#39;s inbox content.</span>
595 <span class="sd">        TAG: USER</span>
596 <span class="sd">        &quot;&quot;&quot;</span>
597         <span class="k">for</span> <span class="n">message</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_inbox</span><span class="p">:</span>
598             <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">message</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
599                 <span class="bp">self</span><span class="o">.</span><span class="n">set_information</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span></div></div>
600 </pre></div>
602           </div>
603         </div>
604       </div>
605     <div class="clearer"></div>
606   </div>
607     <div class="footer">
608       &copy;2016, INRA and Univ. Lille.
609       
610       |
611       Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.5</a>
612       &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
613       
614     </div>
616     
618     
619   </body>
620 </html>