
cSc           @   sD  d  Z  d d l Z d d l Z d d l m Z d d l Td d l m Z d d l m	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m Z m Z d d l Z d d l Td d l Z d d l Z d f  d	     YZ d
 d d d  Z d   Z d   Z d   Z e d k r@e   n  d S(   s*   
Model Management and simulation objects.
iN(   t   deepcopy(   t   *(   t   OrderedDict(   t   ArgumentParser(   t   grapht   edget   siteobjt   simulatec        	   B   s  e  Z d  Z d! d d d d d d e d  Z d   Z d   Z d   Z d	   Z	 d
   Z
 d   Z 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 d d  Z d    Z RS("   s_   
    This class takes care of setting up the model, simulating it, and storing the results
    t	   localhosti  t   epigrasst   sqlitec	         C   s&  t  |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  |  _
 d  |  _ d  |  _ t  |  _ | |  _ t j   |  _ t j j d |  j  d |  _ t j   j d d  j d d  |  _ t j j |  j  d j d  d |  _ |  j |  j  |  _ |  j |  j  |  j s8d |  j |  _ }	 n  t j j  |  j  s`t j! |  j  n  |  j rt" j |  j |  j g   |  _ n  |  j#   d	 |  _$ d |  _% t& |  j' d
 d d |  j$ }
 t& |  j( d
 d d |  j$ } |  j) |
  } |  j* | |  } |  j+ |  j d | |  |  _, d  S(   Ni    t    t   _t   :t    it   .s   outdata-s   utf-8t   sept   ,t   encodingi   (-   t   Truet   parallelt   hostt   portt   usrt   dbt   fnamet   backendt   passwt   Nonet   repnamet	   shapefilet   Worldt   shpoutt   silentt   ost   getcwdt   dirt   syst   patht   insertt   guit   timet   asctimet   replacet   nowt   splitt	   modelNamet   loadModelScriptt   configt
   evalConfigt   outdirt   existst   mkdirt   epigdalt	   chkScriptR   t   roundt   loadDatat   sitest   edgest	   instSitest	   instEdgest	   instGrapht   g(   t   selfR   R   R   R   t   usert   passwordR   R!   R2   t   sitiost   edt   lt   e(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   __init__$   sD    														')		"
		c         C   s   i  } | j    } t j   } | j |  xm | j   D]_ } t j |  } xG | j |  D]6 } t j | j	 | |   | | d t j |  <q] Wq8 W| S(   s   
        Loads the model specification from the text file.
        Returns a dictionary with keys of the form
        <section>.<option> and the corresponding values.
        R   (
   t   copyt   ConfigParsert   SafeConfigParsert   readt   sectionst   stringt   lowert   optionst   stript   get(   R?   R   R0   t   cpt   sect   namet   opt(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR/   L   s    8c         C   s  y| d r# t  | d  |  _ n	 g  |  _ | d |  _ | d |  _ | d r` | d |  _ n  | d |  _ | d |  _ | d |  _ | d |  _ | d	 |  _	 | d
 |  _
 | d |  _ | d |  _ | d |  _ t  | d  |  _ t  | d  |  _ t  | d  |  _ t  | d  |  _ t  | d  |  _ t  | d  |  _ | d |  _ t  | d  |  _ t  | d  |  _ t  | d  |  _ t  | d  |  _ WnG t k
 r} | j   j d  } t j d | d | d f  n X|  j r	g  |  _ d |  _ n  t   |  _  i  |  _! xr | j"   D]d \ } } | j# d  rc| |  j  | j d  d <q+| j# d  r+| |  j! | j d  d <q+q+Wd  S(!   s[   
        Takes in the config dictionary and generates the global variables needed.
        s   the world.shapefiles   the world.sitess   the world.edgess   the world.encodings   epidemiological model.modtypes   model parameters.betas   model parameters.alphas   model parameters.es   model parameters.rs   model parameters.deltas   model parameters.bs   model parameters.ws   model parameters.ps   epidemic events.seeds   epidemic events.vaccinates   transportation model.dotransps   transportation model.stochastics   transportation model.speeds   simulation and output.stepss   simulation and output.outdirs   simulation and output.mysqlouts   simulation and output.replicass   simulation and output.randseeds   simulation and output.batchR   s\   Please check the syntax of your '.epg' file.
Variable %s, from section %s was not specified.i   i    s   initial conditionsis   model parametersN($   t   evalR   R9   R:   R   t   modtypet   betat   alphaRE   t   rt   deltat   Bt   wt   pt   seedt	   vaccinatet   doTranspt   stochTranspt   speedt   stepsR2   t   MySQLoutt   replicast   randomize_seedst   Batcht   KeyErrort   __str__R-   R%   t   exitR7   R   t   initst   parmst   itemst
   startswith(   R?   R0   t   vt   Vt   k(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR1   \   sV    
	
				c         C   s   |  j  d  t j |  j t j  sF |  j  d |  j  t j   n  t j |  j t j  s |  j  d |  j  t j   n  |  j d k r |  j  d |  j  n  |  j  d |  j	  d S(   s@   
        Checks the type of the variables on the script
        s&   Checking syntax of model script... NOWs7   Sites file %s does not exist, please check your script.s7   Egdes file %s does not exist, please check your script.t   SISt   SIS_st   SIRt   SIR_st   SEISt   SEIS_st   SEIRt   SEIR_st   SIpRpSt   SIpRpS_ss   SIpR,SIpR_st	   Influenzat   Customs3   Model type %s is invalid, please check your script.s"   Script %s passed syntax check NOW.N(   Rr   Rs   Rt   Ru   Rv   Rw   Rx   Ry   Rz   R{   s   SIpR,SIpR_sR|   R}   (
   t   SayR"   t   accessR9   t   F_OKR%   Rj   R:   RV   R.   (   R?   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR6      s    	c         C   s   | j  d  } t | d  d k  r\ t | d  t | d  d t | d  d } n4 t | d  t | d  d t | d  d } | S(   s.   
        converts lat/long to decimal
        R   i    i   i<   i   i  (   R-   t   intt   float(   R?   t   coordt   cot   result(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   deg2dec   s
    74c         C   sQ  | j  d  } t |  } g  } x | D] } t |  | k rY t d t |    n  d | d k r |  j | d  } |  j | d  } n  t | d  } t | d  } | j t | d | d | | f t t	 | d   t
 g  | d D] } t | j	    ^ q    q( Wx.| D]&}	 |  j r>d |	 _ n	 d |	 _ |	 j }
 |	 j } t   } i  } x- |  j j   D] \ } } t |  | | <qxWx- |  j j   D] \ } } t |  | | <qW|  j r`|  j d d d	 k r|  j d d |  j d d g |	 _ q`xM |  j D]? } t |	 j  | d k r| d t | d  g |	 _ qqWn  |  j r!x |  j D] } t |	 j  | d k s|  j d d d	 k r| | d j   c | d 7<|	 j |  j |  j d
 | d | d | qs|	 j |  j |  j d
 | d | d | qsWq#|	 j |  j |  j d
 | d | d | q#W| S(   s   
        Instantiates and returns a list of siteobj instances, from a list of site specification
        as returned by data_io.loadData.
        Here the site specific events are passed to each site, and the site models are created.
        i    s@   This line in your sites file has a different number elements:
%sR   i   i   i   i   i   t   allRo   t   bit   bp(   t   popt   lent
   ValueErrort   strR   R   t   appendR   R   RO   t   tupleRa   t   stochtranspt   totpopt   valuesR   Rk   Rm   RU   Rl   R_   t   vaccinationt   geocodeR^   RM   t   createModelRV   R.   (   R?   t   sitelistt   headert   ncolst   objlistt   sitet   latt   longt   it   ot   NR   Rk   Rl   Rq   Ro   t   j(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR;      sN    37						('	0+/,c   	      C   s"  | j  d  } g  } d } } x | D] } | d | d k rF q& n  xb | D]Z } t | j  | d k ru | } n" t | j  | d k r | } n  | rM | rM PqM qM W| o | s t j d | d d | d d  n  | j t | | | d | d	 t | d
    d } } q& W| S(   s   
        Instantiates and returns a list of edge objects,

        sitelist -- list of siteobj objects.

        edgelist -- list of edge specifications as returned by data_io.loadData.
        i    i   i   s   One of the vertices on edge t   -i   s$    could not be found on the site listi   i   i   N(	   R   R   R   R   R%   Rj   R   R   R   (	   R?   R   t   edgelistR   R   t   sourcet   destt   edgR   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR<      s&    
		"1c         C   s[   t  | |  } |  j | _ x | D] } | j |  q" Wx | D] } | j |  q@ W| S(   sV   
        Instantiates and returns a graph object from a list of edge objects.
        (   R   Rb   t   addSitet   addEdge(   R?   RS   t   digrapht   siteobjlistt   edgeobjlistR>   R   R   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR=   %  s    c         C   s  | d k r g  |  j  j j   D] } t | j  ^ q } t |  } t |  t |  } t t	 d | |  j
   } g  } d } |  j  j }	 x | |  j
 k  r t	 d d  }
 |
 | t | |  k r | j |	 t | |   | d 7} q q Wn | d k r|  j  j } n  | S(   s$  
        Generate and return a list of randomized seed sites
        to be used in a repeated runs scenario.
        Seed sites are sampled with a probability proportional to the
        log of their population size.
        option: if 1 randomize; if 2, return unrundomized sequence
        i   i    i   (   R>   t	   site_dictt
   itervaluest   log10R   R   t   arrayt   sumt   floort   uniformRe   t	   site_listR   R   (   R?   t   optionR   t   poplistt   lplt   popprobt   uR9   R   R   R]   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   randomizeSeed4  s     .i   c         C   s   |  j  d d j   } |  j d |  |  j j } | j | | f g |  _  xW | D]O } | j | j k r | | j | <|  j d | | j f  qS d | j | <qS Wd S(   s   
        Resets the number of infected to zero in all nodes but seed,
        which get n infected cases.
        seed must be a siteobj object.
        i    i   s   seedvar= %ss!   %s infected case(s) arrived at %sN(   R^   RM   R~   R>   R   R   R   t   sitename(   R?   R^   t   nt   seedvarR   R   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   setSeedM  s    c         C   s  d |  j  k r. t j j |  j   d |  _  n  t j d  |  j d  t j   } |  j |  j |  j	 d |  j
 t j   | } |  j d |  |  j r |  j d k r |  j |  j   q |  j |  j   n  |  j r |  j   n  |  j   t j |  j |  j |  j  d S(	   s&   
        Start the simulation
        t   /ii
   s   Simulation starting.t   transps   Simulation lasted %s seconds.t   csvN(   R.   R"   R&   R-   R)   t   sleepR~   t   runGraphR>   Rc   R`   Rd   R   t   outToCsvt   outToDbR    t   outToShpt   dumpDatat   spreadt   SpreadR2   R   (   R?   t   startt   elapsed(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR   c  s     		
c   
      C   s   t  | j t  } | j } t |  j |  j d  } | j d  zx xq t |  D]c \ } } g  | D] } t	 |  ^ qd } t	 |  d j
 |  t	 | |  d }	 | j |	  qQ WWd | j   Xd t S(   sf   
        Saves the time series *site.ts* to outfile as specified on the
        model script.
        R\   s   time,E,I,S,incidence
R   s   
Ns   E,I,S
(   R   t   tst   Floatt	   incidencet   openR2   t   outfilet   writet	   enumerateR   t   joint   closet   tss(
   R?   R   t   datat   inct   ft   tR   Rq   R   t   line(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   createDataFrame}  s    	+c         C   s   d | } t  j |  d S(   sc   
        Convert Grass vector layer to an ascii file epigrass can read
        Deprecated!
        s3   v.out.ascii input=%s output=map.txt format=standardN(   R"   t   system(   R?   t   layert   com(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   grassVect2ascii  s    
c         C   s  |  j  s d S|  j d  |  j  j |  j  j j |  j  j d   |  j d  |  j d  |  j  j   |  j d  g  |  j j j	   D]8 \ } } | d | d t
 | j  t
 | j  f ^ q } |  j d  |  j  j |  |  j d	  d
 d d d g } g  |  j j j   D]C } | j t | j  | j | j t
 | j  t | j  f ^ q} d   |  j j j	   D } |  j d  |  j  j | |  |  j d  |  j |  |  j d  |  j  j   d S(   s9   
        Creates Data Layers in shapefile format
        Ns   REading Nodes from shapefile...i    s   Done reading nodes!s   Creating Nodes shapefile...s   Done creating Nodes shapefile!i   s   Creating Edges shapefile...s   Done creating Edges shapefile!t
   prevalencet
   totalcasest   arrivalst
   populationc         S   s"   i  |  ] \ } } | j  |  q S(    (   R   (   t   .0Rq   Ro   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pys
   <dictcomp>  s   	 s   Creating Data shapefile...s   Done creating Data shapefile!s   Done creating KML files!(   R   R~   t   get_node_listt   dst   GetLayerByNamet	   layerlistt   create_node_layerR>   t	   edge_dictt	   iteritemsR   t   fthetat   bthetat   create_edge_layerR   R   R   R   R   R   t	   thetahistt   create_data_layert
   out_to_kmlt   close_sources(   R?   t   gcsRE   t   elistt   varlistR   t	   sitestatst   names(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR     s,    	)QYc         C   sw   |  j  d  |  j j j d  } t j   } | j | |  | j |  j  |  j	 j
 } |  j	 j } |  j  d  d S(   s/   
        Generates output to kml files
        s   Creating KML output files...i    s   Done creating KML files!N(   R~   R   t
   datasourcet   GetLayerR5   t   KmlGeneratort   addNodest   writeToFileR2   R>   R   R   (   R?   R   t   lrRq   R   R   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR     s    c   	      C   s   |  j  d  i  } x |  j j j   D]n \ } } t j | j  } i  } x= t | j  D], \ } } | d d  | f j	   | | <qZ W| | | <q& Wt
 d d   } t j | |  Wd QXd S(   sH   
        Saves timeseries to JSON for uploading to epigrass web
        s   Saving series to JSONNs   series.jsonR\   (   R~   R>   R   R   t   npR   R   R   t   vnamest   tolistR   t   jsont   dump(	   R?   t   seriest   gct   sR   t   sdictR   t   vnR   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   series_to_JSON  s    $c         C   s  |  j  t j   k s( t j |  j   n  t | d d  } |  j j   } d j g  | D]0 \ } } | j   j	 d d  j	 d d  ^ qW  } d j g  | D] \ } } | j
 d  d	 ^ q  } | j | d
  | j | d
  | j   t j |  j  d S(   s)   
        Writes a meta CSV table
        t   _metaR\   t   ;R   R   R   t   $t   #i    s   
N(   R2   R"   R#   t   chdirR   R0   Rm   R   RO   R+   R-   R   R   R$   (   R?   t   tableR   t   cfgitemsRq   Ro   t   hRD   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   writeMetaCSV  s    F5
c      	   C   s  |  j  t j   k s( t j |  j   n  | d |  j d } | d |  j d 7} t | d  } d d d d d	 d
 g } t } x|  j j j	   D]} d } t
 | j  t
 |  t
 | j  | j j d d  j d d  t
 | j d  t
 | j d  g } | j rSxD t | j  D]0 \ }	 }
 | j d |	  | j t
 |
   qWn  t | j d  } | j d d g  x0 t | j  D] \ }	 }
 | j t
 |
   qWx | D] } t |  } | j t
 | j |  t
 | j |  g  t
 |  | d <x4 t | j  D]# \ }	 }
 | j t
 | |	   qW| shd j |  } | j | d  t } n  | j d j |  d  | d 7} qWq W| j   t | d  } d } d d d d d g } t } x |  j j D] } x t  | j! | j"  D] \ } } | s,| j d j |  d  t } n  | j# j | j$ j | | | g } g  | D] } t
 |  ^ qT} | j d j |  d  | d 7} qWqW| j   t j |  j%  d S(   s6   
        Save simulation results in csv file.
        R   s   _e.tabs   .tabR\   R   R)   R   RS   R   t   longiti    t   "R   t   asciiR+   i   s   values%sR   R   R   s   
t   source_codet	   dest_codeR   R   N(&   R2   R"   R#   R  R,   R   t   FalseR>   R   R   R   R   R   R   R+   t   encodet   posR   R   R   R   R   t   extendR   R    R   R   R   R   R   R   t	   edge_listt   zipR   R   R   R   R$   (   R?   R  t   tableeR   t   headt   headerwrittenR   R   t   regbR   Ro   R   R   t   regR  R>   t   ehwRE   t   bt   ereg(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR     sb    !&	-	
"	!
c         C   s  z| d } |  j  j   d k r^ t j d |  j d |  j d |  j d |  j d |  j  } n_ |  j  j   d k r |  j	 t
 j   k s t
 j |  j	  n  t j d	  } t
 j |  j  n  d
 | } g  } |  j j   } x? | D]7 \ } } | j | j   j d d  j d d   q Wd j g  | D] } d | ^ q+ }	 | j   }
 |
 j | |	 d  g  | D] \ } } | j d  d ^ ql} d j g  | D] } d | ^ q d } d | | f } |
 j |  Wd | r| j   | j   n  Xd S(   s^   
        Creates a Meta-Info Table on the database with the contents of the .epg file
        R  t   mysqlR   R   R@   t   passwdR   R
   s   Epigrass.sqlites   CREATE TABLE %s(R   R   R   R	  s   , s   %s texts   );R
  i    R   s   "%s"t   )s   INSERT INTO %s VALUES(%sN(   R   RM   t   MySQLdbt   connectR   R   R   R   R   R2   R"   R#   R  t   sqlite3R$   R0   Rm   R   RO   R+   R   t   cursort   executeR-   t   commitR   (   R?   R  t   cont   sqlstr1t   varsR  Rq   Ro   R   t   sqlstr2t   CursorR   t   str3t   sqlstr3(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   writeMetaTable  s2    
!
/&,*
c   (      C   sG  |  j  j   d k r% |  j d  n% |  j  j   d k rJ |  j d  n  d$ } z| d |  j } |  j |  | |  _ |  j  j   d k r t j d |  j	 d |  j
 d |  j d	 |  j d
 |  j  } n_ |  j  j   d k r*|  j t j   k st j |  j  n  t j d  } t j |  j  n  t |  j j j   d j d  d } d | } d | } d d g t |  j j j   d j  d g d g } | Gt |  GH| d  t |  } | t |  d  t |  } | j   } d | } d | }	 | | d }
 |	 | d } |  j  j   d k rZ| j |
  | d d } | d  d } nA |  j  j   d k r| j |  | d d } | d  d } n  d | d | } g  } x |  j j j   D] } | j } | j  d } | j  d } | j! } t" | j d  } | j# } | j$ } d } xp | D]h } t% |  } | j& t | | | g | | g t |  | | g t' | |  g   | d 7} q*WqW| j( | |  | d |  _) } d  | } d! | } |  j  j   d k r| j |  d | d" } n3 |  j  j   d k r5| j |  d | d# } n  g  } x |  j j* j+   D]r \ } }  | d }! | d }" d } xI t, |  j- |  j.  D]2 \ }# }$ | j& |! |" | |# |$ f  | d 7} qWqNW| j( | |  Wd$ | r| j/   n  Xd% |  j0 }% t j1 j2 |  j |%  }& t3 |& d&  }' t4 j5 |  j j6   |'  |' j/   d$ S('   sg   
        Insert simulation results on a mysql or SQLite table
        :param table: Table name
        R#  s   Saving data on MySQL...R
   s   Saving data on SQLite...R   R   R   R@   R$  R   s   Epigrass.sqlitei    ii   s   `%s` FLOAT(9),s   %s REAL,R   R  R   t   Arrivalsi   s   CREATE TABLE %s(
            `geocode` INT( 9 )  ,
            `time` INT( 9 ) ,
            `name` varchar(128) ,
            so   CREATE TABLE %s(
            geocode INTEGER  ,
            time INTEGER ,
            name TEXT ,
            s   );i   s   %s,R%  s   ?,s   INSERT INTO %ss    VALUES(RE   s   CREATE TABLE %s(
            `source_code` INT( 9 )  ,
            `dest_code` INT( 9 )  ,
            `time` INT( 9 ) ,
            `ftheta` FLOAT(9) ,
            `btheta` FLOAT(9) );s   CREATE TABLE %s(
            source_code INTEGER  ,
            dest_code INTEGER  ,
            time INTEGER ,
            ftheta REAL ,
            btheta REAL );s    VALUES(%s,%s,%s,%s,%s)s    VALUES(?,?,?,?,?)Nt   adj_R\   (7   R   RM   R~   R   R,   R3  t   outtableR&  R'  R   R   R   R   R   R2   R"   R#   R  R(  R$   R   R>   R   R   R   t   listR   R   R)  R*  R   R   R  R   R   R   R   R   R   R   t   executemanyt   etableR   R   R  R   R   R   R.   R&   R   R   t   cPickleR   t   getConnMatrix((   R?   R  R,  t   nvart   str1t   str1litet   varnamesR0  t   str2t   str2litet   sqlR
   R1  t   sql2t   nvaluesR   t   geocR   R   RS   R   R   t   thistR   R   t   tstepR9  t   esqlt   esqlitet   esql2R   R   RE   R  t   dR   R!  t   matnameR   t   adjfile(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR     s    	!'

7

				L



"c         C   s   g  |  j  j j   D] } t | j  ^ q } t j j d  s |  j d  |  j  j	   } t
 d d  } | j d j |  d  xG | D]? } g  | D] } t |  ^ q } | j d j |  d  q W| j   |  j d  n  d  S(   Ns
   adjmat.csvs   Saving the adjacency  matrix...R\   R   s   
s   Done!(   R>   R   R   R   R   R"   R&   R3   R~   R;  R   R   R   R   (   R?   R   t	   codeslistt   amt   amft   row(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   criaAdjMatrixM  s    .
c         C   sD  |  j  d  t j   } |  j | k s; t j |  j  n  |  j j j   } |  j   |  j  d  |  j	 r t
 j d t |  j	  d |  j  } n t
 j d d |  j  } | j d  x |  j j D] } |  j j | d } | d } t |  r*| j   } | j d	 d
    | d d j } n d } | j t | d  d | j d | d  q W| j   |  j  d  |  j j d  |  j  d  g  |  j j   D] } t |  ^ q}	 g  |  j j j   D]& }
 |
 j |  j d d k r|
 ^ qd } |	 j d  t j j d  r;t
 j d d |  j  } n% t
 j d d |  j  } | j d  t | j    } t | j  } | j } d | | f } | j t |  j d d  d | d d j |	  d | d  | j   |  j  d  |  j  d  t j j d  r.t
 j d d |  j  } n t
 j d d |  j  } | j d  x |  j j j   D] }
 t |
 j    } t |  j d d  } | j } t |
 j   } | d k rd } n  | j t |  j	  d t |
 j  d |
 j d | d | d | d | d  qfW|  j!   t j |  |  j  d  d S(   s<   
        Dumps data as csv (comma-separated-values)
        s   Starting simulation Analysiss   Saving Epidemic path...s   epipath%s.csvR\   s   epipath.csvs   time,site,infector
i   it   keyc         S   s   |  d S(   Ni   (    (   R   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   <lambda>  s    i    t   NAR   s   
s   Done!s   network.graphmls!   Saving Epidemiological results...s   epistats.csvt   asf   seed,name,size,infected_sites,spreadtime,median_survival,totvaccinated,totquarantined,seeddeg,seedpop
s   %s,%ss   Done saving!s   Saving site statistics...s   sitestats.csvs7   round,geocode,name,infection_time,degree,seed,seedname
t   FALSEs   Done saving data!N("   R~   R"   R#   R2   R  R>   R   t   keysRR  R7   t   codecsR   R   R   R   t   epipathR   Rm   t   sortR   R   t   save_topologyt   getEpistatsR   R   R^   R   R&   R3   t	   getDegreeR   R   t   infectedR  (   R?   t   curdirRN  t   eppR   R   t	   infectorst   reverse_infectorst   mlit   statsR  R^   t   stft   sdegt   spopt   snamet   sstatst   siteft   degreet   seedgct   seednamet   it(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR   [  sp    
	(
2
(C	C
		T
c         C   s0   t  | d  } t j |  j |  | j   d S(   s1   
        Save the fully specified graph.
        R\   N(   R   R:  R   R>   R   (   R?   R   R   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt	   saveModel  s    c         C   s   t  j |  } | S(   s*   
        Loads a pre-saved graph.
        (   R:  t   load(   R?   R   R>   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt	   loadModel  s    i    c      
   C   s  | } | | _  | j j   } | j j   } | rxt |  D] } Hd j | j  f g  | D] } | j |  j  ^ qa }	 |  j r g  |	 D] }
 |
 j	   ^ q n  x | D] } | j
   q W|  j r |  j j j | j  |  j j j   n  | j d 7_ d | _ q@ Wn x t |  D] } x | D] } | j |  j  q.W|  j r|  j j j | j  |  j j j   |  j j j j   |  j j j |  j j j d  | j t g  | D] } | j | j d f ^ q  |  j j j j   n  | j d 7_ q!Wd S(   s3   
        Starts the simulation on a graph.
        s   ==> {}i   i    t   drawStepiN(   t   maxstepR   R   R   t   xranget   formatt   simstept   runModelR   t   waitt   migrateR(   t   stepLCDt   displayt   appt   processEventst
   sites_donet   RTt   mutext   lockt   emitt   QtCoret   SIGNALt   dictR   R   t   unlock(   R?   t   graphobjt
   iterationsR   R>   R9   R:   R   R   t   resultsRY   R   R  (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR     s:    	%	 		$0c         C   s=   |  j  r# |  j  j j | d  n |  j r0 d SH| d d S(   sN   
        Exits outputs messages to the console or the gui accordingly
        s   
N(   R(   t	   textEdit1t   insertPlainTextR!   (   R?   RL   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR~     s    		N(   t   __name__t
   __module__t   __doc__R   R  RF   R/   R1   R6   R   R;   R<   R=   R   R   R   R   R   R   R   R  R  R   R3  R   RR  R   Rp  Rr  R   R~   (    (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR      s8   '		G			=	&						!					'	q		t		%R	   R   i  c         C   s   t  j   j d d  j d d  } d t j | } t j d | d | d t d	 t d
 |  } | j	   } d | } | j
 |  t j |   }	 d | d }
 | j
 |
 |	  | j   d S(   sv   
    store the Simulate object *s* in the epigrass database
    to allow distributed runs. Currently not working.
    R   R   R   R   t   Model_R   R   R@   R$  R   s,   CREATE TABLE %s(
        `simulation` BLOB);s   INSERT INTO %ss    VALUES(%s)N(   R)   R*   R+   t   SR.   R&  R'  R   R   R)  R*  R:  t   dumpsR   (   R  R   R   R   R,   R  R,  R0  RB  t   blobRC  (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   storeSimulation  s    $*
c         C   s?  |  j  d k rL t d |  j d d |  j d |  j d |  j d |  j   } n t d |  j d d |  j   } |  j | _ | j s | j   t	 j
 | j  n
 t |  | j r;t j d  x{ | j D]m } t j | j  d
 | _ t d | d | j d | j d | j d | j   } t j d	 |  | j   q Wn  d
 S(   s-   
    Runs the model from the commandline
    R#  R   i    R   R@   RA   R   s   Simulation Started.s   starting model %sN(   R   R   t   epgt   dbhostt   dbusert   dbpassR   Re   R   R   R   R>   t   repRunsRg   R?   R~   R"   R  R$   R   R   R   R   R   (   t   argsR  R   t   T(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   onStraightRun#  s     =	

		3c         C   s  |  j  } |  j } |  j } |  j } |  j } |  j } |  j d d } t j d |  | ro |  j	 |  } n  |  j
 }	 x t |	  D]z }
 t j d |
  t d | d | d | d | d	 |  }  | r |  j | |
 |  n  |
 |  _ t |  _ |  j   ~  q Wd
 S(   s   
    Do repeated runs
    i    i   s   Replication type: s   Starting replicate number %sR   R   R@   RA   R   N(   Rf   R   R   R   R   R   R^   R?   R~   R   Re   Ru  R   R   R7   R  R    R   (   R  t   randseedR   R   R@   RA   R   t   nseedst   seedst   repsR   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyR  D  s(    							'		
c          C   sq  d }  t  d |  d d d d t j  } | j d d d	 d
 d d d d d d | j d d d	 d d d | j d d d	 d d d | j d d d	 d d d d d | j d d d  d! d t d	 d" d d# | j d$ d d% d& d' d d( | j   } | j d) k r/t j ot j	 r/| j
 d*  n  | j d. k rU| j
 d, t j  n  Hd- | j f t |  d  S(/   Ns$   usage: PROG [options] your_model.epgt   usaget   descriptions$   Run epigrass models from the consolet   progs   PROG s   -bs	   --backendR   R   t   helps'   Define which datastorage backend to uset   metavars   <mysql|sqlite|csv>t   defaultR
   s   -us   --dbusernameR  s   MySQL user names   -ps
   --passwordR  s   MySQL password for users   -Hs   --dbhostR  R   s   MySQL hostname or IP addresss   -Ps
   --parallelt   actiont
   store_trueR   s)   use multiprocessing to run the simulationR  t   EPGt   nargsi   s&   Epigrass model definition file (.epg).R#  s6   You must specify a user and password when using MySQL.R   s    "%s" is an invalid backend type.s   ==> (   s   mysqls   sqlites   csv(   R   t   __version__t   versiont   add_argumentR  t
   parse_argsR   RN   R  R  t   errorR  R  (   R  t   parserR  (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   main^  s.    "t   __main__(   R  R:  R)   RG   R    t   numpyt   collectionsR   t   argparseR   RH   RL   R   R(  R"   R&  t   simobjR   R   R   R   t   data_ioR5   R  R   R  R  R  R  R  (    (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/manager.pyt   <module>   s6   

   	!		