
cSc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z d d l Z d d l	 m
 Z
 d d l Z d d l Td d l Z e j d d d d	  Z e j   s t  e j d
  d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   sa   
This Module contains the definitions of objects for spatial simulation on geo reference spaces.
iN(   t   binomial(   t
   json_graph(   t   *t   hostt	   localhostt   porti  i  t   siteobjc           B   s   e  Z d  Z d d  Z d   Z d d g  d d d  Z e d  Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s]   
    Basic site object containing attributes and methods common to all
    site objects.
    c         C   s  |  |  _  d |  _ | |  _ t |  |  _ g  |  _ g  |  _ t |  _ d |  _
 | |  _ | |  _ d |  _ d |  _ d |  _ d |  _ d |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ d |  _ g  g  g |  _ d |  _ d |  _ d |  _ t j d g |  _ d |  _ | |  _  d |  _! d |  _" g  |  _# g  |  _$ g  |  _% g  |  _& d d d d d d d d d d d d g |  _' t( j) d j* |  j   d  d S(   s   
        Set initial values for site attributes.

        -name: name of the locality

        -coords: site coordinates.

        -initpop: total population size.

        -geocode: integer id code for site

        -values: Tuple containing adicional values from the sites file
        i    s   {}:totalcasesN(+   t   idt   stochtranspt   post   floatt   totpopt   tst	   incidencet   Falset   infectedt   Nonet   infectort   sitenamet   valuest
   centralityt
   betweenesst   thidxt   degreet   parentGrapht   edgest	   neighborst	   thetalistt	   thetahistt   passlistt
   totalcasest   vaccinationt
   vaccineNowt   vaccovt   nVaccinatedt   syst   maxintt
   quarantinet   nQuarantinedt   geocodet   paintedt   modtypet   migInft   inedgest   outedgest   pdestt   infectedvisitingt   redisclientt   sett   format(   t   selft   namet   initpopt   coordsR'   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   __init__#   sH    																															-c         C   s-   t  j    } |  j   d Gt  j    | GH|  S(   s-   
        For multiprocessing to work
        s   Time to runModel: (   t   timet   runModel(   R2   t   t0(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   __call__X   s    
t    t   model1c   	      C   s   |  j  } | |  _ | |  _ | |  _ | |  _ t j |  j |  |  _ t j	 | |  _	 y  |  j
 j t | j     Wnj t k
 r } |  j	 d d d g k r g  d d d g D] } | | ^ q g |  _
 q t d |   n Xd |  j d	 <d |  j d
 <d S(   s  
        Creates a model of type modtype and defines its initial parameters.
        init -- initial conditions for the state variables tuple with fractions of the total
        population in each category (state variable).
        par -- initial values for the parameters.
        v -- List of extra variables passed in the sites files
        bi, bp -- dictionaries containing all the inits and parms defined in the .epg model
        t   Exposedt
   Infectioust   Susceptiblet   et   it   ss   %si    R    R!   N(   R   R)   R   t   bit   bpt	   epimodelst   EpimodelR'   t   modelt   vnamesR   t   appendt   listt   KeyError(	   R2   R)   R3   t   vRC   RD   t   Nt   ket   vn(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   createModela   s    
					 /c      	   C   sh  |  j  j |  j d k rr d |  _ t |  j d |  j d j |  j  j   |  _ d |  j d <|  j |  j d <n d |  j d <|  j g  k r t	 g  |  j D] } | d ^ q  } t
 g  |  j D] } | d d k r | ^ q  |  _ n d } i  |  _ t	 |  j  } |  j  j } |  j d } |  j } t j   } | j d |  j d j |  j  |  j d j |  j  |  j d	 j |  j  |  j d
 j |  j  |  j d j |  j  |  j  j d j |  j  |  j  j   | r|  j  j j |  j d d d |  j }	 n@ |  j | | | | | |  j |  j |  j  }
 |  j |
  d }	 |  j j |  |	 S(   sn   
        Iterate the model
        :Parameters:
        - parallel: run in a separate process if true
        i    i   R    R!   it   simsteps	   {}:totpops   {}:initss   {}:npasss   {}:thetas   {}:bis   {}:bpt   argst   callback(    N(    R   RQ   R   R    R
   t   indexR!   RD   R   t   sumt   dictR   R   R   R   R/   t   pipelineR0   R1   R'   t   rpusht   hmsetRC   t   executet   pot   apply_asyncRG   t   handleR   R   R   RI   (   R2   t   parallelRA   t   thetat   npassRQ   t   initsR   t   pipet   rt   res(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR8      s>    	0&5		*-c         C   s  t  j   } | j d j |  j  d  j d j |  j   j d j |  j   j   \ } } } |  j j t	 |   t
 |  } t
 |  } |  j | 7_ |  j j |  |  j s| d k r|  j j |  _ |  j j j |  j j |  j |  j f  qn  |  j j |  d  S(   Ns   {}:tsis   {}:Lposs	   {}:migInfi    (   R/   RW   t   lindexR1   R'   t   getRZ   R   RI   t   evalR
   R   R   R   R   RQ   t   epipathR   R*   (   R2   Rd   Rb   t
   last_statet   LposR*   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR]      s    $	.c         C   s   | \ } } } |  j  j |  |  j | 7_ |  j j |  |  j s | d k r |  j j |  _ |  j j j |  j j |  j |  j	 f  q n  |  j
 j |  d S(   s   
        Processes the output of a step updating simulation statistics
        :param res: Tuple with the output of the simulation model
        i    N(   R   RI   R   R   R   R   RQ   Rh   R'   R   R*   (   R2   Rd   t   stateRj   R*   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt
   handle_old   s    	.c         C   s@   |  j  d d | |  _ |  j  d d d | |  j  d d <d S(   sP   
        At time t the population will be vaccinated with coverage cov.
        ii   i   N(   R   R"   (   R2   t   cov(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt	   vaccinate   s    c         C   s$   |  j  | d | | |  j  | <d S(   sX   
        From time t on, parameter par is changed to
        par * (1-cov*efic)
        i   N(   RD   (   R2   t   parRm   t   efic(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   intervention   s    c         C   s#  | t  |  j  k r+ t  |  j  d } n  d | } |  j g  k rJ d n
 |  j | } |  j d k r | | t |  j  } n@ y& t t |  | t |  j   } Wn t k
 r d } n X|  j j	 |  j
 d k r| |  j
 d |  _ | d |  j
 d | d |  j
 d f S| | f Sd S(   s   
        Returns the number of infected individuals in this
        site commuting through the edge that called
        this function.

        npass -- number of individuals leaving the node.
        i   ii    N(   t   lenR*   R   R
   R   R    t   intt
   ValueErrorR   RQ   R%   R&   (   R2   R`   t   delayt   lagR*   R_   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getTheta   s    
"&
(c         C   s[   |  j  r |  j  St g  |  j D] } | j | j d ^ q  t |  j j  |  _  } | S(   s   
        Returns the Theta index.
        Measures the function of a node, that is the average
        amount of traffic per intersection. The higher theta is,
        the greater the load of the network.
        g       @(   R   RU   R   t   fmigt   bmigRr   R   t	   site_list(   R2   RA   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getThetaindex  s    	Gc         C   s*   |  j  j | | f  |  j j |  d S(   s   
        Number of infectious individuals arriving from site i
        :param thetai: number of infected passengers
        :param npass: Number of passengers arriving
        :param site: site sending passengers
        :return:
        N(   R   RI   R   (   R2   t   thetaiR`   t   site(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   receiveTheta  s    c         C   s   |  j  r d Sd Sd S(   s9   
        find is given site is a node of a graph
        i   i    N(   R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   isNode+  s    	c         C   sH   |  j  r |  j  Sg  |  j D] } |  | j k r | ^ q } | |  _  | S(   s1   
        return a list of outbound edges
        (   R,   R   t   source(   R2   R@   t   oe(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getOutEdges4  s
    	+	c         C   sH   |  j  r |  j  Sg  |  j D] } |  | j k r | ^ q } | |  _  | S(   s1   
        return a list of outbound edges
        (   R+   R   t   dest(   R2   R@   t   ie(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt
   getInEdges>  s
    	+	c         C   s   |  j    s g  S|  j r  |  j Si  } xW |  j D]L } | j | j | j g } | j |   } | j |  | d | | d <q0 W| |  _ | S(   sf   
        Returns a dictionary of neighbooring sites as keys,
        and distances as values.
        ii    (   R   R   R   R   R   t   lengthRT   t   pop(   R2   t   neighRA   t   nt   idx(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getNeighborsH  s    		c         C   s(  |  j  s |  j   |  _  n  t |  t d  k r g  |  j  D]! } t | j  | k r= | ^ q= } | r g  |  j D] } | | j k rt | j ^ qt d } q$t j	 d | d j
 |  j
 f  na | |  j  k rg  |  j D] } | | j k r | j ^ q d } n t j	 d | j
 |  j
 f  | S(   s   
        Returns the distance in Km from a given neighbor.
        neighbor can be a siteobj object, or a geocode number
        i   i    s   %s is not a neighbor of %s!(   R   R   t   typeRs   R'   R   t   sitesR   R#   t   exitR   (   R2   t   neighborR   t   neiR@   t   d(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getDistanceFromNeighbor[  s    	15$5c         C   s$   |  j    s d St |  j    Sd S(   s  
        Returns the degrees of this site if it is part of a graph.
        The order (degree) of a node is the number of nodes attached to it
        and is a simple, but effective measure of nodal importance.

        The higher its value, the more a node is important in a graph
        as many links converge to it. Hub nodes have a high order,
        while terminal points have an order that can be as low as 1.

        A perfect hub would have its order equal to the summation of
        all the orders of the other nodes in the graph and a perfect
        spoke would have an order of 1.
        
        Returns an integer.
        i    N(   R   Rr   R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt	   getDegreet  s    c         C   sX   |  j    |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j |  j |  j |  j g S(   sR   
        Calculate indices describing the node and return them in a list.
        (   t   getCentralityR   R   R   R{   R   t   getBetweenessR   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   doStats  s
    c         C   se   |  j  r |  j  S|  j j j |   } |  j j j   sG |  j j   n  d t |  j j |  } | S(   s   
        Also known as closeness. A measure of global centrality, is the
        inverse of the sum of the shortest paths to all other nodes
        in the graph.
        g      ?(   R   R   Rz   RT   t   allPairst   anyt   getAllPairsRU   (   R2   R	   t   c(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR     s    	c         C   s]   |  j  r |  j  Sd } x@ |  j j D]2 } |  | k r# |  | d k rU | d 7} qU q# q# W| S(   s{   
        Is the number of times any node figures in the the shortest path
        between any other pair of nodes.
        i    i   i   (   R   R   t   shortPathList(   R2   t   BRA   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR     s    	(    N(   t   __name__t
   __module__t   __doc__R6   R:   R   RP   t   TrueR8   R]   Rl   Rn   Rq   Rw   R{   R~   R   R   R   R   R   R   R   R   R   (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR      s*   5		1										
	
					t   edgec           B   s2   e  Z d  Z d d d d  Z d   Z d   Z RS(   sn   
    Defines an edge connecting two nodes (node source to node dest).
    with attributes given by value.
    i    c         C   s   t  | t  s t d  n  t  | t  s6 t d  n  | |  _ | |  _ | | g |  _ t |  |  _ t |  |  _ d |  _
 | |  _ d |  _ g  |  _ g  |  _ | j j |   | j j |   d S(   s6  
        Main attributes of *Edge*.

        source -- Source site object.

        dest -- Destination site object.

        fmig -- forward migration rate in number of indiv./day.

        bmig -- backward migration rate in number of indiv./day.

        Length -- Length in kilometers of this route
        s*   source received a non siteobj class objects/   destination received a non siteobj class objecti    N(   t
   isinstanceR   t	   TypeErrorR   R   R   R
   Rx   Ry   R   R   R   Ru   t   fthetat   bthetaR   RI   (   R2   R   R   Rx   Ry   t   Leng(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR6   w  s     							c         C   s;   |  j  j d k r7 t t |  j  |  j  j  |  _ n  d S(   sP   
        calculate the Transportation delay given the speed and length.
        i    N(   R   t   speedRs   R
   R   Ru   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt	   calcDelay  s    c         C   s   |  j  j |  j |  j  \ } } |  j j |  |  j j | | |  j   |  j j |  j |  j  \ } } |  j	 j |  |  j  j | | |  j  d S(   s   
        Get infectious individuals commuting from source node and inform them to destination.
        this is done for both directions of the edge
        N(
   R   Rw   Rx   Ru   R   RI   R   R~   Ry   R   (   R2   R_   R`   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   migrate  s    !!(   R   R   R   R6   R   R   (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR   q  s    	t   graphc           B   sO  e  Z d  Z d d  Z d   Z d% d  Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d    Z" d!   Z# d"   Z$ d#   Z% d$   Z& RS(&   s.   
    Defines a graph with sites and edges
    i    c         C   s~  | |  _  | |  _ i  |  _ t d d    |  _ i  |  _ t d d    |  _ d |  _ d |  _ d |  _	 g  |  _
 i  |  _ g  |  _ |  |  _ t d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d |  _ g  |  _ d |  _  d |  _! d  |  _" d |  _# d |  _$ d |  _% d |  _& t' j( t' j)    |  _* d  S(   Nt   fgetc         S   s   |  j  j   S(   N(   t	   site_dictR   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   <lambda>  s    c         S   s   |  j  j   S(   N(   t	   edge_dictR   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR     s    i    i   id   (+   R3   t   digraphR   t   propertyRz   R   t	   edge_listR   RQ   t   maxstepRh   t	   graphdictR   R   t   zerosR   R   t   cyclest   wienerDt   meanDt   diameterR   t   weightt   iotaidxt   piidxt   betaidxt   alphaidxt   gammaidxt
   connmatrixt   shortDistMatrixt   episizet   epispeedt   infectedcitiest
   spreadtimet   mediansurvivalt   totVaccinatedt   totQuarantinedt   dmapt   printedt   multiprocessingt   Poolt	   cpu_countR[   (   R2   t
   graph_nameR   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR6     sJ    																																	c         C   s8   t  | t  s t d  n  | |  j | j <|  | _ d S(   s   
        Adds a site object to the graph.
        It takes a siteobj object as its only argument and returns
        None.
        s,   add_site received a non siteobj class objectN(   R   R   t   ErrorR   R'   R   (   R2   t   sitio(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   addSite  s    c   
      C   s   i  } i  } t    } d | | <x | D] } | | | | <| | k rJ Pn  x | | D]z } | | | | | }	 | | k r |	 | | k  r t d  q qU | | k s |	 | | k  rU |	 | | <| | | <qU qU Wq& W| | f S(   sI  
        Find shortest paths from the start vertex to all
        vertices nearer than or equal to the end.

        The input graph G is assumed to have the following
        representation: A vertex can be any object that can
        be used as an index into a dictionary.  G is a
        dictionary, indexed by vertices.  For any vertex v,
        G[v] is itself a dictionary, indexed by the neighbors
        of v.  For any edge v->w, G[v][w] is the length of
        the edge.  This is related to the representation in
        <http://www.python.org/doc/essays/graphs.html>
        where Guido van Rossum suggests representing graphs
        as dictionaries mapping vertices to lists of neighbors,
        however dictionaries of edges have many advantages
        over lists: they can store extra information (here,
        the lengths), they support fast existence tests,
        and they allow easy modification of the graph by edge
        insertion and removal.  Such modifications are not
        needed here but are important in other graph algorithms.
        Since dictionaries obey iterator protocol, a graph
        represented as described here could be handed without
        modification to an algorithm using Guido's representation.

        Of course, G and G[v] need not be Python dict objects;
        they can be any other object that obeys dict protocol,
        for instance a wrapper in which vertices are URLs
        and a call to G[v] loads the web page and finds its links.

        The output is a pair (D,P) where D[v] is the distance
        from start to v and P[v] is the predecessor of v along
        the shortest path from s to v.

        Dijkstra's algorithm is only guaranteed to work correctly
        when all edge lengths are positive. This code does not
        verify this property for all edges (only the edges seen
        before the end vertex is reached), but will correctly
        compute shortest paths even for some graphs with negative
        edges, and will raise an exception if it discovers that
        a negative edge has caused it to make a mistake.
        i    s3   Dijkstra: found better path to already-final vertex(   t   priorityDictionaryRt   (
   R2   t   Gt   startt   endt   Dt   Pt   QRL   t   wt   vwLength(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   dijkstra  s$    +	
 
c         C   sl   g  |  j  D]$ } | j   t |  k r
 | ^ q
 } t |  } | d k rT | d S| d k rd | Sd Sd S(   sE  Retrieved a site from the graph.

        Given a site's name the corresponding Siteobj
        instance will be returned.

        If multiple sites exist with that name, a list of
        Siteobj instances is returned.

        If only one site exists, the instance is returned.
        None is returned otherwise.
        i   i    N(   Rz   R3   t   strRr   R   (   R2   R3   R   t   matcht   l(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getSite,  s    4c         C   s   t  | t  s t d   n  | j j |  j k rB t d   n  | j j |  j k rf t d   n  | |  j | j j | j j f <|  | _	 | j
   d S(   sy   Adds an edge object to the graph.

        It takes a edge object as its only argument and returns
        None.
        s)   add_edge received a non edge class objects(   Edge source does not belong to the graphs-   Edge destination does not belong to the graphN(   R   R   R   R   R'   R   RK   R   R   R   R   (   R2   t
   graph_edge(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   addEdgeD  s    	c         C   s=   i  } x' |  j  j   D] } | j   | | <q W| |  _ | S(   s\   
        Generates a dictionary of the graph for use in the shortest path function.
        (   R   t
   itervaluesR   R   (   R2   R   RA   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getGraphdictY  s
    	c         C   sr   g  |  j  D]* } | j | k r
 | j | k r
 | ^ q
 } t |  } | d k rZ | d S| d k rj | Sd Sd S(   sn  
        Retrieved an edge from the graph.

        Given an edge's source and destination the corresponding
        Edge instance will be returned.

        If multiple edges exist with that source and destination,
        a list of Edge instances is returned.

        If only one edge exists, the instance is returned.
        None is returned otherwise.
        i   i    N(   R   R   R   Rr   R   (   R2   t   srct   dstR   R   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getEdgec  s    :c         C   s)   g  |  j  j   D] } | j ^ q } | S(   s?   
        returns list of site names for a given graph.
        (   R   R   R   (   R2   RB   t	   sitenames(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getSiteNames{  s    %c         C   s$   t  |  j  t  |  j  d } | S(   s  
        The maximum number of independent cycles in a graph.

        This number (u) is estimated by knowing the number of nodes (v),
        links (e) and of sub-graphs (p); u = e-v+p.

        Trees and simple networks will have a value of 0 since they have
        no cycles.

        The more complex a network is, the higher the value of u,
        so it can be used as an indicator of the level of development
        of a transport system.
        i   (   Rr   R   Rz   (   R2   t   u(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt	   getCycles  s     c         C   s\   |  j  | | |  \ } } g  } x* | j |  | | k rA Pn  | | } q$ | j   | S(   s^  
        Find a single shortest path from the given start node
        to the given end node.
        The input has the same conventions as self.dijkstra().
        'G' is the graph's dictionary self.graphdict.
        'start' and 'end' are site objects.
        The output is a list of the vertices in order along
        the shortest path.
        (   R   RI   t   reverse(   R2   R   R   R   R   R   t   Path(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   shortestPath  s     
c          C   s  d d l  m } d d l m } |   } g  |  j D] } | j ^ q3 } t g  |  j D] } | j d ^ qU  } t g  |  j D] } | j d ^ q~  } t g  |  j D] } | j	 j d ^ q  }	 t g  |  j D] } | j	 j d ^ q  }
 t g  |  j D] } | j
 j d ^ q  } t g  |  j D] } | j
 j d ^ q+ } g  t |	 |
 | |  D]* \ } } } } | | f | | f f ^ q`} t   } | j g   | j g   | j | |  } | j d  | j d  } g  | D] } | ^ q} | | d | d	 d
 } | j d  | j |  t |  } t |  } t |  } t |  } | | } | | } d | d | } } | | | | f | | | | f f } | j |  | j   t d  t   d S(   s/   
        Draws the network using pylab
        i(   t   LineCollection(   t   ColorConverteri   i    i   t   kt   colorst	   linestylet   solidg?s	   graph.pngN(   t   matplotlib.collectionsR   t   matplotlib.colorsR   Rz   R   t   arrayR	   R   R   R   t   zipt   axest
   set_xtickst
   set_ytickst   scattert
   set_zordert   to_rgbat   add_collectiont   amint   amaxt   update_datalimt   autoscale_viewt   savefigt   close(    R2   R   R   t   colorConverterRA   t   namest   xt   yR@   t   xst   yst   xdt   ydt   at   bR   R   R   t   axt	   node_plott   kolort	   ed_colorst	   edge_collt   minxt   maxxt   minyt   maxyR   t   ht   padxt   padyt   corners(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt	   drawGraph  sB    	)),,,,F	

(

c         C   s^  |  j  j   r |  j  S|  j r+ |  j } n |  j   } t |  } t | | f t  } t | | f t  } d } x | j   D] } d } x | j   |  D] } |  j	 | | |  }	 |  j
 | |	  }
 |  j j | | |	 |
 f  t |	  d | | | f <| | | f <|
 | | | f <| | | f <| d 7} q W| d 7} q W| |  _ | |  _  | S(   s   
        Returns a distance matrix for the graph nodes where
        the distance is the shortest path. Creates another
        distance matrix where the distances are the lengths of the paths.
        i    i   (   R   R   R   R   Rr   R   R
   t   iterkeyst   keysR   t   getShortestPathLengthR   RI   R   (   R2   t   gR   t   dmt   apRA   t   siteit   jt   sitejt   spt   lsp(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR     s,    	(		c         C   sJ   d } d } x7 | d  D]+ } | | j  | | d  7} | d 7} q W| S(   s#   
        Returns sp Length
        i    ii   (   R   (   R2   t   originR%  t   LengthRA   RB   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR    s    c         C   s   y |  j  j   r |  j  SWn n X|  j s: |  j   n  |  j j   } t |  j  } t | | f t  } x t	 |  D]u \ } } xf t	 | |   D]T \ } } | | k r n t | | j
 k  | | | f <| | | f | | | f <q Wqz W| S(   sn  
        The most basic measure of accessibility involves network connectivity
        where a network is represented as a  connectivity matrix (C1), which
        expresses the connectivity of each node with its adjacent nodes.

        The number of columns and rows in this matrix is equal to the number
        of nodes in the network and a value of 1 is given for each cell where
        this is a connected pair and a value of 0 for each cell where there
        is an unconnected pair. The summation of this matrix provides a very
        basic measure of accessibility, also known as the degree of a node.
        (   R   R   R   R   R   R   Rr   R   R
   t	   enumerateR   (   R2   Rz   t   nsitest   cmRA   R"  R#  R$  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getConnMatrix  s"     	"c         C   sB   |  j  r |  j  S|  j j   r/ t |  j j  St |  j   j  S(   s:   
        Returns the Wiener distance for a graph.
        (   R   R   R   RU   t   flatR   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt
   getWienerD&  s
    	c         C   su   |  j  r |  j  S|  j j   rG t t t |  j j d  |  j j   St t t |  j   j d  |  j   j   S(   s8   
        Returns the mean distance for a graph.
        i    (   R   R   R   t   meant   compresst   greaterR-  R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getMeanD2  s
    	(c         C   sB   |  j  r |  j  S|  j j   r/ t |  j j  St |  j   j  S(   sK   
        Returns the diameter of the graph: longest shortest path.
        (   R   R   R   t   maxR-  R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getDiameter?  s
    	c         C   s   |  j    |  j   } | S(   s  
        Returns the Iota index of the graph

        Measures the ratio between the network and its weighed vertices.
        It considers the structure, the length and the function
        of a graph and it is mainly used when data about traffic
        is not available.

        It divides the length of a graph (L(G)) by its weight (W(G)).
        The lower its value, the more efficient the network is.
        This measure is based on the fact that an intersection
        (represented as a node) of a high order is able to handle
        large amounts of traffic.

        The weight of all nodes in the graph (W(G)) is the summation
        of each node's order (o) multiplied by 2 for all orders above 1.
        (   t	   getLengtht	   getWeight(   R2   t   iota(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getIotaindexL  s    c         C   s   g  |  j  j   D] } | j   ^ q } t g  | D] } | d k r2 | d ^ q2  t g  | D] } | d k  r^ | ^ q^  } t |  S(   s   
        The weight of all nodes in the graph (W(G)) is the summation
        of each node's order (o) multiplied by 2 for all orders above 1.
        i   i   (   R   R   R   RU   R
   (   R2   RA   t   degreest   W(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR6  b  s    (Xc         C   s/   t  g  |  j D] } | j ^ q  } t |  S(   sL   
        Sum of the length in kilometers of all edges in the graph.
        (   RU   R   R   R
   (   R2   RA   t   L(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR5  l  s    %c         C   s   |  j  r |  j  } n |  j   } g  |  j D] } t | d  ^ q+ } | j t |   } |  j | d } d } x= t t |  d  D]% } | | | j | | d  7} q W| | } t |  S(   s  
        Returns the Pi index of the graph.

        The relationship between the total length of the graph L(G)
        and the distance along the diameter D(d).

        It is labeled as Pi because of its similarity with the
        real Pi (3.14), which is expressing the ratio between
        the circumference and the diameter of a circle.

        A high index shows a developed network. It is a measure
        of distance per units of diameter and an indicator of
        the  shape of a network.
        i   i    i   (	   R   R5  R   Rr   RT   R3  t   rangeR   R
   (   R2   R   RA   R&  t   lpidxt   lpt   Ddt   pi(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt
   getPiIndexs  s    	&#
c         C   s&   t  |  j  t t  |  j   } | S(   sM  
        The Beta index
        measures the level of connectivity in a graph and is
        expressed by the relationship between the number of
        links (e) over the number of nodes (v).

        Trees and simple networks have Beta value of less than one.
        A connected network with one cycle has a value of 1.
        More complex networks have a value greater than 1.
        In a network with a fixed number of nodes, the higher the
        number of links, the higher the number of paths possible in
        the network. Complex networks have a high value of Beta.
        (   Rr   R   R
   R   (   R2   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getBetaIndex  s    "c         C   s1   t  t |  j   } |  j   d | d } | S(   s9  
        The Alpha index is a measure of connectivity which evaluates
        the number of cycles in a graph in comparison with the maximum
        number of cycles. The higher the alpha index, the more a network
        is connected. Trees and simple networks will have a value of 0.
        A value of 1 indicates a completely connected network.

        Measures the level of connectivity independently of the number of
        nodes. It is very rare that a network will have an alpha value of 1,
        because this would imply very serious redundancies.
        g       @i   (   R
   Rr   R   R   (   R2   R*  t   A(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getAlphaIndex  s    c         C   s@   t  t |  j   } t  t |  j   } | d | d } | S(   s  
        The Gamma index is a A measure of connectivity that considers
        the relationship between the number of observed links and the
        number of possible links.

        The value of gamma is between 0 and 1 where a value of 1
        indicates a completely connected network and would be extremely
        unlikely in reality. Gamma is an efficient value to measure
        the progression of a network in time.
        i   i   (   R
   Rr   R   R   (   R2   t   nedgR*  R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getGammaIndex  s    c         C   s  |  j    |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j   |  _	 |  j
   |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j   |  _ |  j |  j |  j |  j |  j	 |  j |  j |  j |  j |  j |  j |  j |  j g S(   sA   
        Generate the descriptive stats about the graph.
        (   R   R   R   R   R.  R   R2  R   R4  R   R5  R   R6  R   R8  R   RA  R   RB  R   RD  R   RF  R   R,  R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR     s     $$c         C   sB   t  |  j  } y |  j t | d  d } Wn d } n X| S(   sS   
        Returns the time taken by the epidemic to reach 50% of the nodes.
        i   i    t   NA(   Rr   R   Rh   Rs   (   R2   R   t   median(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getMedianSurvival  s    
c         C   s/   t  g  |  j j   D] } | j ^ q  } | S(   s9   
        Returns the total number of vaccinated.
        (   RU   R   R   R"   (   R2   RA   t   tot(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getTotVaccinated  s    +c         C   s/   t  g  |  j j   D] } | j ^ q  } | S(   sF   
        Returns the total number of quarantined individuals.
        (   RU   R   R   R&   (   R2   RA   RJ  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getTotQuarantined  s    +c         C   s   |  j    |  _ |  j   |  _ |  j   |  _ d |  _ |  j   |  _ |  j	   |  _
 |  j   |  _ |  j |  j |  j |  j |  j |  j
 |  j g S(   sH   
        Returns a list of all epidemiologically related stats.
        i    (   t
   getEpisizeR   t   getEpispeedR   t   getInfectedCitiesR   R   RI  R   RK  R   RL  R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getEpistats  s    	c         C   s   t  |  j  } | S(   s8   
        Returns the number of infected cities.
        (   Rr   Rh   (   R2   Rd   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyRO    s    c         C   s/   t  g  |  j j   D] } | j ^ q  } | S(   s2   
        Returns the size of the epidemic
        (   RU   R   R   R   (   R2   R}   RM   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyRM    s    +c         C   sZ   g  |  j  D] } | d ^ q
 } g  } x- t |  j  D] } | j | j |   q6 W| S(   s7   
        Returns the epidemic spreading speed.
        i    (   Rh   R<  RQ   RI   t   count(   R2   RA   t   tlt   nsptR#  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyRN  "  s
     c         C   sE   g  |  j  D] } | d ^ q
 } | s/ d } n | d | d } | S(   sH   
        Returns the duration of the epidemic in units of time.
        i    RG  i(   Rh   (   R2   RA   RR  t   dur(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   getSpreadTime-  s
     	c   
      C   s   t  j   } x: |  j j   D]) \ } } | j | d i | j d 6q WxE |  j j   D]4 \ } } | j | d | d d | j | j	 qY Wt  j
 | |  t j |  } | j d d  } t | d   }	 t j | |	  Wd	 QXd	 S(
   s   
        Saves graph structure to a graphml file for visualization
        :Parameters:
        :pa: path in which to save the graphml file
        t	   attr_dictR3   i    i   R   t   graphmlt   jsonR   N(   t   NXt   MultiDiGraphR   t	   iteritemst   add_nodeR   R   t   add_edgeRx   Ry   t   write_graphmlR   t   node_link_datat   replacet   openRX  t   dump(
   R2   t   paR  t   gcR   t   edR@   t   nlt   jsonpatht   f(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   save_topology8  s    ,c         C   sp   d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _	 d |  _
 d |  _ d |  _ d S(   s0   
        Resets all graph related stats
        N(   R   R   R   R   R   R   R   R   R   R   R   R   R   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt
   resetStatsL  s    											N('   R   R   R   R6   R   R   R   R   R   R   R   R   R   R   R  R   R  R,  R.  R2  R4  R8  R6  R5  RA  RB  RD  RF  R   RI  RK  RL  RP  RO  RM  RN  RU  Ri  Rj  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR     sH   (	A			
						1	!		 					
						(									R   c           B   s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s   g  |  _  t j |   d S(   so  
        by David Eppstein.
        <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117228>
        Initialize priorityDictionary by creating binary heap
        of pairs (value,key).  Note that changing or removing a dict entry will
        not remove the old pair from the heap until it is found by smallest() or
        until the heap is rebuilt.
        N(   t   _priorityDictionary__heapRV   R6   (   R2   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR6   _  s    		c         C   s  t  |   d k r t d  n  |  j } x | d d |  k s^ |  | d d | d d k r| j   } d } x d | d } | d t  |  k  r | | | | d k r | d 7} n  | t  |  k s | | | k r | | | <Pn  | | | | <| } qs q* W| d d S(   sL   
        Find smallest item after removing deleted items from heap.
        i    s$   smallest of empty priorityDictionaryi   i   (   Rr   t
   IndexErrorRk  R   (   R2   t   heapt   lastItemt   insertionPointt
   smallChild(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   smallestk  s"    	7"
c            s     f d   } |   S(   sK   
        Create destructive sorted iterator of priorityDictionary.
        c          3   s5   x. t     d k r0   j   }  |  V  |  =q Wd  S(   Ni    (   Rr   Rq  (   R  (   R2   (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   iterfn  s    (    (   R2   Rr  (    (   R2   sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   __iter__  s    c         C   s   t  j |  | |  |  j } t |  d t |   k rv g  |  j   D] \ } } | | f ^ qE |  _ |  j j   n~ | | f } t |  } | j d  xL | d k r | | | d d k  r | | d d | | <| d d } q W| | | <d S(   s   
        Change value stored in dictionary and add corresponding
        pair to heap.  Rebuilds the heap if the number of deleted
        items grows too large, to avoid memory leakage.
        i   i    i   N(   RV   t   __setitem__Rk  Rr   R[  t   sortRI   R   (   R2   t   keyt   valRm  R   RL   t   newPairRo  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyRt    s    	.c         C   s)   x" | j    D] } | | |  | <q Wd  S(   N(   R  (   R2   t   otherRv  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   update  s    c         C   s!   | |  k r | |  | <n  |  | S(   s:   Reimplement setdefault to call our customized __setitem__.(    (   R2   Rv  Rw  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt
   setdefault  s    (   R   R   R6   Rq  Rs  Rt  Rz  R{  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyR   ^  s   					(   R   R#   R   R7   RX  t   numpy.randomR    t   networkxRY  t   networkx.readwriteR   t   redist   data_ioRE   t   RedisR/   t   pingt   AssertionErrort   setrecursionlimitt   objectR   R   R   RV   R   (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/simobj.pyt   <module>   s.   
    X@  