ó
ÂKUc           @   s  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 Z d  d l	 Z	 d  d l
 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 „ 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 „ Z' d d „ Z( d „  Z) d  „  Z* d! „  Z+ d" „  Z, d# „  Z- d$ „  Z. d% „  Z/ d& „  Z0 d d' „ Z1 d( „  Z2 d) „  Z3 d* „  Z4 d+ „  Z5 g  d, „ Z6 d- „  Z7 d S(.   iÿÿÿÿNc         C   sD   t  j |  d ƒ �, } g  | D] } t | j ƒ  ƒ ^ q } Wd QX| S(   sJ   loads sample file
    Args:
       outpath:
    Returns:
       vals:
    t   rN(   t   gzipt   opent   floatt   rstrip(   t   outpatht   infilet   linet   vals(    (    s   lib/EmbedUtilities.pyt   loadSampleFileAll   s    +c      
   C   sR   t  j | d ƒ �: } | j d j g  |  D] } t | ƒ ^ q( ƒ d ƒ Wd QXd S(   s<   saves sampl
    Args:
       comp2vals:
       outpath:
    t   ws   
N(   R   R   t   writet   joint   str(   R   t   statpatht   outfilet   val(    (    s   lib/EmbedUtilities.pyt   saveSampleFileAll   s    c         C   s‰   t  j | d ƒ �q } xg t |  j ƒ  ƒ D]S } | j d j | ƒ ƒ | j d j g  |  | D] } t | ƒ ^ q[ ƒ d ƒ q( WWd QXd S(   sB   saves sample file
    Args:
       comp2vals:
       outpath:
    R
   s	   comp	{0}
s   
N(   R   R   t   sortedt   keysR   t   formatR   R   (   t	   comp2valsR   R   t   compR   (    (    s   lib/EmbedUtilities.pyt   saveSampleFile#   s    c      	   C   s–   i  } d } t j |  d ƒ �r } xh | D]` } | j ƒ  } | j d ƒ rq t | j d d ƒ ƒ } g  | | <q( n  | | j t | ƒ ƒ q( WWd QX| S(   sO   loads sample file
    Args:
       outpath:
    Returns:
       comp2vals:
    R    R   t    N(	   t   NoneR   R   R   t
   startswitht   intt   replacet   appendR   (   R   R   t   curcompR   R   (    (    s   lib/EmbedUtilities.pyt   loadSampleFile/   s    
!c         C   st   i  } xg |  j  ƒ  D]Y } t ƒ  } x7 |  j  ƒ  D]) } | | k r/ | t |  | ƒ O} q/ q/ Wt | ƒ | | <q W| S(   sh   returns cumulative scale to domains
    Args:
       scale2doms:
    Returns:
       cumscale2doms:
    (   R   t   set(   t
   scale2domst   cumscale2domst   scalet   domsett   scale2(    (    s   lib/EmbedUtilities.pyt   getCumulativeC   s    	c            s  ˆ  d d k s t  ‚ ˆ  d d k r8 ‡  f d †  } n" ˆ  d d k rZ ‡  f d †  } n  t j t j |  ƒ d t j ƒ} x… t t j | ƒ d ƒ D]j } xa t t j | ƒ d ƒ D]F } y6 | | | ƒ } | d	 k rð d
 | | | | f <n  Wqµ qµ Xqµ Wq• W| S(   s2   generates kernel coefs
    Args:
    Returns:
    t   kernelt   expt   gaussc            s#   t  j ˆ  d t |  | ƒ d ƒ S(   Nt   kerncoefi   (   t   mathR(   t   abs(   t   ind1t   ind2(   t   algopar(    s   lib/EmbedUtilities.pyt   <lambda>[   s    c            s!   t  j d ˆ  d |  | d ƒ S(   Ng      à?R*   i   (   R+   R(   (   R-   R.   (   R/   (    s   lib/EmbedUtilities.pyR0   ]   s    t   dtypei    i   I äT   g      ð?(   s   expR)   (   t   AssertionErrort   npt   zerost   shapeR   t   xrange(   t   freqmatR/   t   distfunct   coefmatR-   R.   t   dif(    (   R/   s   lib/EmbedUtilities.pyt   genKernelCoefMatT   s    !  c         C   s–  t  |  ƒ t k r' t |  j ƒ  ƒ } n! t  |  ƒ t k rH t |  ƒ } n  t j | | f d t j ƒ} x)t | ƒ D]} t  |  ƒ t k r¥ |  | d \ } } } n( t  |  ƒ t k rÍ |  | \ } } } n  x¾ t | d | ƒ D]© } t  |  ƒ t k r|  | d \ } }	 }
 n( t  |  ƒ t k r;|  | \ } }	 }
 n  t	 j
 | | d |	 | d |
 | d ƒ } | | | | f <| | | | f <qá Wqs W| S(   s�   estimates distances from index to location mapping
    Args:
       in2loc: index to locations
    Returns:
       distmat: 
    R1   i   i   (   t   typet   dictt   lenR   t   listR3   R4   R   R6   R+   t   sqrt(   t   in2loct   matlent   distmatR-   t   x1t   y1t   z1R.   t   x2t   y2t   z2t   dist(    (    s   lib/EmbedUtilities.pyt   estDistancesj   s&    /c         C   s”   t  | ƒ } x{ |  D]s \ } } t } xB | D]: \ } } t | | ƒ t | | ƒ d k r, t } Pq, q, W| r | j | | f ƒ q q Wt | ƒ S(   sb   preprocess domains
    Args:
       alldomains:
       domains:
    Returns:
       sentdoms:
    i   (   R    t   FalseR,   t   Truet   addR?   (   t
   alldomainst   domainst   sentdomst   startt   endt   flagt   start1t   end1(    (    s   lib/EmbedUtilities.pyt   preprocDomains†   s    $c         C   sa   g  t  t j |  ƒ d ƒ D]= } t  d t j |  ƒ d | ƒ D] } | | | f ^ q> q } | S(   si   get all possible intervals
    Args:
       freqmat:
       domains:
    Returns:
       posdomains:
    i    i   (   R6   R3   R5   (   R7   RP   t   nodet   intlent
   posdomains(    (    s   lib/EmbedUtilities.pyt   getPosDomainsš   s    ]c      	      s>   d j  |  | | | d j ‡  f d †  t ˆ  j ƒ  ƒ Dƒ ƒ ƒ S(   sg   gets algo str
    Args:
       method,objtype,algo,scalemode,algopar:
    Returns:
       algostr:
    s   {0}-{1}-{2}-{3}-{4}t   _c         3   s2   |  ]( } | ˆ  | g D] } t  | ƒ Vq q d  S(   N(   R   (   t   .0t   itemR   (   R/   (    s   lib/EmbedUtilities.pys	   <genexpr>­   s    (   R   R   R   R   (   t   methodt   objtypet   algot	   scalemodeR/   (    (   R/   s   lib/EmbedUtilities.pyt
   getAlgoStr¦   s    c         C   s4   d j  |  | ƒ } | d k r0 d j  | ƒ } n  | S(   s^   gets noise string
    Args:
       noisetype:
       noise:
    Returns:
       noisestr:
    s   {0}_{1}g        s   {0}(   R   (   t	   noisetypet   noiset   noisestr(    (    s   lib/EmbedUtilities.pyt   getNoiseStr°   s    c            s’   ‡  f d †  t  t ˆ  ƒ ƒ Dƒ } g  | D] } | | ^ q) } d | t g  |  j ƒ  D]. \ } } }	 | | k rV |  | | |	 f ^ qV ƒ }
 |
 S(   s„   add prior for frac variables
    Args:
       xdict:
       domains,predomains:
       lamcoef:
    Returns:
       addfracobj:
    c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   t   index(   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>Ç   s   	 g      ð¿(   R6   R>   t   sumR   (   t   xdictRP   t
   predomainst   lamcoeft	   dom2indext	   predomaint	   predominst   dominR   R#   t
   addfracobj(    (   RP   s   lib/EmbedUtilities.pyt   addPriorFracObj¾   s    	"Oc      	      s`   t  | ƒ } d | t g  |  j ƒ  D]5 } t t  ‡  f d †  |  | Dƒ ƒ j | ƒ ƒ ^ q# ƒ S(   s•   add prior to obj for comp2domain mapping
    Args:
       comp2dominds:
       domains,predomains:
       lamcoef:
    Returns:
       priorobj:
    g      ð¿c         3   s   |  ] } ˆ  | Vq d  S(   N(    (   R]   Rp   (   RP   (    s   lib/EmbedUtilities.pys	   <genexpr>Ø   s    (   R    Ri   R   R>   t   intersection(   t   comp2domindsRP   Rk   Rl   t   presetR   (    (   RP   s   lib/EmbedUtilities.pyt   addPriorObjÎ   s    	c            sé   d } | d k r d } n  ‡  f d †  t  t ˆ  ƒ ƒ Dƒ } t d „  | j ƒ  Dƒ ƒ }	 d }
 xƒ | D]{ \ } } xl t  | ƒ D]^ } xU |	 D]M } | | | f | | f } | j | ƒ rŒ |  | | c | | d 7<qŒ qŒ Wq Wqf Wd S(	   s·   adds prior coef
    Args:
       b,lamcoef:
       predomains: file domains
       compcount:
       domains: all domains
       var2index:
       minmax: min or max
    Returns:
    g      ð¿t   maxg      ð?c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   Rh   (   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>é   s   	 c         s   s   |  ] \ } } } | Vq d  S(   N(    (   R]   Rp   R   R#   (    (    s   lib/EmbedUtilities.pys	   <genexpr>ê   s    i    g      à?N(   R6   R>   R    R   t   has_key(   t   bRl   Rk   t	   compcountRP   t	   var2indext   minmaxt   coefRm   t	   usescalest   addcountRR   RS   R   R#   t   varstr(    (   RP   s   lib/EmbedUtilities.pyt   addPriorCoefÛ   s    	"c         C   sN  t  j t  j |  ƒ d t  j ƒ} xg | j ƒ  D]Y } xP | | D]D } | | \ } }	 | | |	 d … | |	 d … f c | | 7<q? Wq. W|  | }
 | d k rdt g  t t  j |
 ƒ d ƒ D]: } t t  j |
 ƒ d ƒ D] } |
 | | f d ^ qÛ q¾ ƒ } t g  t t  j |  ƒ d ƒ D]: } t t  j |  ƒ d ƒ D] } |  | | f d ^ q;qƒ } nÜ t g  t t  j |
 ƒ d ƒ D]H } t t  j |
 ƒ d ƒ D]( } |
 | | f d | | | f ^ qžq�ƒ } t g  t t  j |  ƒ d ƒ D]H } t t  j |  ƒ d ƒ D]( } |  | | f d | | | f ^ qqïƒ } | | | f S(   s£   returns solution ratio 
    Args:
       freqmat:
       comp2dominds,comp2scale
       domains:
       kernmat: 
    Returns:
       ratio:
       newobjval:
    R1   i   i    i   N(   R3   R4   R5   R   R   R   Ri   R6   (   R7   Rt   t
   comp2scaleRP   t   kernmatt	   secondmatR   Rp   RR   RS   t   difmatR-   R.   t   newobjt   freqsum(    (    s   lib/EmbedUtilities.pyt   getRatioô   s    !6
`cnnc         C   sÐ   |  j  d ƒ } t | ƒ d k r\ d j | d d !ƒ } t j j | ƒ s\ t j | ƒ q\ n  d j |  ƒ } d j |  ƒ } d GHg  | D]  \ }	 }
 |	 d |
 d f ^ q† } t | | | | ƒ t	 | | ƒ d	 S(
   s‰   make decon outputs
    Args:
       outprefix:
       domains:
       comp2dominds:
       comp2scale:
       metadata 
    Returns:
    t   /i   i    iÿÿÿÿs   {0}_decon.txts   {0}_objscore.txts   Writing outputi   N(
   t   splitR>   R   t   ost   patht   existst   makedirsR   t   writeDeconOutt   writeDeconMeta(   t	   outprefixRP   Rt   R‚   t   metadatat   splittedt	   outfoldert   deconoutfilet
   objoutfileRR   RS   t   tdomains(    (    s   lib/EmbedUtilities.pyt   makeDeconOutput  s    
-c         C   s2   t  |  d ƒ � } | j d j | ƒ ƒ Wd QXd S(   sZ   write class count file
    Args:
       compcountpath:
       compcount:
    Returns:
    R
   s   {0}
N(   R   R   R   (   t   compcountpathRz   R   (    (    s   lib/EmbedUtilities.pyt   writeCompcountFile%  s    c         C   s?   t  |  d ƒ �* } x  | D] } t | j ƒ  ƒ } q WWd QX| S(   sZ   reads class count file
    Args:
       compcountpath:
    Returns:
       compcount:
    R    N(   R   R   R   (   R™   R   R   Rz   (    (    s   lib/EmbedUtilities.pyt   readCompcountFile/  s    c         C   s[   t  j ƒ  } x |  D] } | j | ƒ q Wx$ | D] \ } } | j | | ƒ q1 Wt | ƒ S(   sp   finds maximal clique decomposition 
    Args:
       domains:
       interdom:
    Returns:
       cliques:
    (   t   nxt   Grapht   add_nodet   add_edget   findCliqueDecomp(   RP   t   interdomt   Gt   domt   dom1t   dom2(    (    s   lib/EmbedUtilities.pyt   findDomainCliqueDecomp;  s    c         C   s+   t  j |  ƒ } t t t t  j | ƒ ƒ ƒ S(   s]   finds maximal clique decomposition 
    Args:
       interG
    Returns:
       cliques:
    (   Rœ   R�   R?   t   mapt   find_cliques(   t   interGt   tinterG(    (    s   lib/EmbedUtilities.pyR    K  s    c            sd  ‡  f d †  t  t ˆ  ƒ ƒ Dƒ } i  g  d } } } xm t t j ˆ  t | ƒ | ƒ ƒ D]J \ }	 }
 } | | | |	 |
 | f <| d 7} | j | |	 |
 | f ƒ qX Wt j | | f d t j	 ƒ} d g | } g  t  | ƒ D] }
 | D] } |
 | f ^ qè qÞ } xÿ | D]÷ \ } } | | } | | } t
 | d | d ƒ t | d | d ƒ d } x¤ t j | | ƒ D]� \ \ } } \ } } | | | | | f | | | | f f c | d | | 7<| | | | | f | | | | f f c | d | | 7<qmWq
WxRˆ  D]J}	 | |	 } |	 d |	 d d d } t j |  |	 d |	 d d … |	 d |	 d d … f ƒ } x¤ t j | | ƒ D]� \ \ } } \ } } | | | | | f | | | | f f c d | | | 7<| | | | | f | | | | f f c d | | | 7<q†Wx9 | D]1 \ }
 } | | | |
 | f c d | | 7<q!WqW| | f S(   sí   generates sdp matrix coefs
    Args:
       freqmat: frequency matrix
       scales: set of scales
       compcount: number of components
       domains: all domains
       interdom: intersecting domains
    Returns:
       objstr: 
    c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   Rh   (   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>d  s   	 i    i   R1   i   g      à?g       À(   R6   R>   R?   t	   itertoolst   productt   rangeR   R3   R4   R   t   minRw   Ri   (   R7   t   scalesRz   RP   R¡   Rm   R{   t	   index2vart   varcountR£   R   R#   R9   t   bvect   pairsR¤   R¥   t   domin1t   domin2t   interlent   comp1t   scale1t   comp2R%   Rp   t   qcoeft   fsum(    (   RP   s   lib/EmbedUtilities.pyt   estMatricesX  s4    "1
2

0+<D
=+<@-c         C   s�   i  g  d } } } xl t  t |  ƒ ƒ D]X } xO t  | ƒ D]A } | | | | | | f <| d 7} | j | | | | f ƒ q: Wq' W| | f S(   s‘   maps vars to matrix indices specific
    Args:
       domains:
       compcount:
       comp2scale:
    Returns:
       var2index,index2var:
    i    i   (   R6   R>   R   (   RP   Rz   R‚   R{   R°   Rh   Rp   R   (    (    s   lib/EmbedUtilities.pyt   mapVarsSpec�  s    	
"c   	      C   sŽ   i  g  d } } } xm |  j  ƒ  D]_ } xV |  | D]J } xA | D]9 } | | | | | f <| d 7} | j | | | f ƒ q? Wq2 Wq! W| | f S(   s‰   maps vars to matrix indices
    Args:
       comp2dominds:
       compcount:
       scales:
    Returns:
       var2index,index2var:
    i    i   (   R   R   (	   Rt   Rz   R¯   R{   R°   Rh   R   Rp   R#   (    (    s   lib/EmbedUtilities.pyt   mapVars2“  s    	
"c   	      C   s–   i  g  d } } } xu t  t |  ƒ ƒ D]a } xX t  | ƒ D]J } xA | D]9 } | | | | | f <| d 7} | j | | | f ƒ qG Wq: Wq' W| | f S(   s    maps vars to matrix indices
    i    i   (   R6   R>   R   (	   RP   Rz   R¯   R{   R°   Rh   Rp   R   R#   (    (    s   lib/EmbedUtilities.pyt   mapVars¦  s    
"c      	      sH  ‡  f d †  t  t ˆ  ƒ ƒ Dƒ } t | j ƒ  ƒ } t j | | f d t j ƒ} t j d g | ƒ }	 t t j	 t
 t | j ƒ  ƒ ƒ t | ƒ ƒ ƒ }
 x| D]\ } } | | } | | } t | d | d ƒ t | d | d ƒ d d } x¾ |
 D]¶ \ } } x§ |
 D]Ÿ \ } } | | | k r| | | k r| | d | | f | d | | f f c | | | 7<| | d | | f | d | | f f c | | | 7<qqWqú WqŸ Wxƒˆ  D]{} | | } | d | d d d } xÆ |
 D]¾ \ } } x¯ |
 D]§ \ } } | | | k r| | | k r| | d | | f | d | | f f c d | | | 7<| | d | | f | d | | f f c d | | | 7<qqWqðWt j |  | d | d d … | d | d d … f ƒ } xH |
 D]@ \ } } | | | k rö|	 | d | | f c | | 8<qöqöWq¿W| |	 f S(   så   generates coefs for second part
    Args:
       freqmat: frequency matrix
       comp2dominds: 
       domains: all domains
       interdom: intersecting domains
       scales:
       var2index:
    Returns:
       objstr: 
    c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   Rh   (   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>¿  s   	 R1   g        i   i    i   g      à?(   R6   R>   R   R3   R4   R   t   arrayR?   R«   R¬   R­   R®   Rw   Ri   (   R7   Rt   RP   R¡   R¯   R{   Rm   R±   t   ARy   R³   R¤   R¥   R´   Rµ   Rº   R·   R¸   R¹   R%   R£   Rp   R»   R   R#   (    (   RP   s   lib/EmbedUtilities.pyt   genCoefsSecond³  s6    "0

4 8G
 <G=,c      	      sÇ  ‡  f d †  t  t ˆ  ƒ ƒ Dƒ } t ˆ  ƒ | t | ƒ } t j | | f d t j ƒ} t j d g | ƒ }	 t t j t	 | ƒ t | ƒ ƒ ƒ }
 xê | D]â \ } } | | } | | } t
 | d | d ƒ t | d | d ƒ d d } x‹ |
 D]ƒ \ } } xt |
 D]l \ } } | | | | | f | | | | f f c | 7<| | | | | f | | | | f f c | 7<q	Wqö Wq› Wx9ˆ  D]1} | | } | d | d d d } x“ |
 D]‹ \ } } x| |
 D]t \ } } | | | | | f | | | | f f c d | 7<| | | | | f | | | | f f c d | 7<qÌWq¹Wt j |  | d | d d … | d | d d … f ƒ } x1 |
 D]) \ } } |	 | | | | f c | 8<qŒWqˆW| |	 f S(   sæ   generates coefs
    Args:
       freqmat: frequency matrix
       compcount: number of components
       domains: all domains
       interdom: intersecting domains
       scales:
       var2index:
    Returns:
       objstr: 
    c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   Rh   (   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>è  s   	 R1   g        i   i    i   g      à?(   R6   R>   R3   R4   R   RÀ   R?   R«   R¬   R­   R®   Rw   Ri   (   R7   Rz   RP   R¡   R¯   R{   Rm   R±   RÁ   Ry   R³   R¤   R¥   R´   Rµ   Rº   R·   R¸   R¹   R%   R£   Rp   R»   R   R#   (    (   RP   s   lib/EmbedUtilities.pyt	   genCoefs4Ü  s0    "$

40<
4<=%c            só  ‡  f d †  t  t ˆ  ƒ ƒ Dƒ } t ˆ  ƒ | t | ƒ } t j | | f d t j ƒ}	 t j d g | ƒ }
 t t j t	 | ƒ t | ƒ ƒ ƒ } x | D]ø \ } } | | } | | } t
 | d | d ƒ t | d | d ƒ d d } x¡ t j t	 | ƒ t	 | ƒ ƒ D]� \ } } t | t | } } |	 | | | | f | | | | f f c | 7<|	 | | | | f | | | | f f c | 7<qWq› WxOˆ  D]G} | | } | d | d d d } x© t j t	 | ƒ t	 | ƒ ƒ D]‰ \ } } t | t | } } |	 | | | | f | | | | f f c d | 7<|	 | | | | f | | | | f f c d | 7<qçWt j |  | d | d d … | d | d d … f ƒ } x1 | D]) \ } } |
 | | | | f c | 8<q¸WqžW|	 |
 f S(   sü   generates coefs 3
    Args:
       freqmat: frequency matrix
       compcount: number of components
       domains: all domains
       interdom: intersecting domains
       scales:
       var2index:
       comp2scales:
    Returns:
       objstr: 
    c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   Rh   (   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>  s   	 R1   g        i   i    i   g      à?(   R6   R>   R3   R4   R   RÀ   R?   R«   R¬   R­   R®   Rw   R‚   Ri   (   R7   Rz   RP   R¡   R¯   R{   t   comp2scalesRm   R±   RÁ   Ry   R³   R¤   R¥   R´   Rµ   Rº   R·   R¹   R¸   R%   R£   Rp   R»   R   R#   (    (   RP   s   lib/EmbedUtilities.pyt	   genCoefs3  s0    "$

4+08
+48=%c      	      s´  ˆ d
 k r ‡ f d †  } n ˆ d
 k r6 d „  } n  d „  t t ˆ  ƒ ƒ Dƒ } x4 | j ƒ  D]& \ } }	 }
 | | j |	 |
 f ƒ q_ W‡  f d †  t t ˆ  ƒ ƒ Dƒ } t | j ƒ  ƒ } t j | | f d t j ƒ} t j d g | ƒ } xÞ | D]Ö \ } } | | } | | } | | | ƒ } x¤ t	 j
 | | | | ƒ D]ˆ \ \ } } \ } } | | | | | f | | | | f f c | | | 7<| | | | | f | | | | f f c | | | 7<qBWqø WxÕˆ  D]Í} | | } | | | ƒ } x¬ t	 j
 | | | | ƒ D]� \ \ } } \ } } | | | | | f | | | | f f c d | | | 7<| | | | | f | | | | f f c d | | | 7<qWˆ d
 k rót j |  | d | d	 d	 … | d | d	 d	 … f ƒ } nw t j t j ˆ | d | d	 d	 … | d | d	 d	 … f |  | d | d	 d	 … | d | d	 d	 … f ƒ ƒ } x9 | | D]- \ }	 }
 | | | |	 |
 f c | |
 8<quWqÙW| | f S(   sç   generates coefs
    Args:
       freqmat: frequency matrix
       compcount: number of components
       domains: all domains
       interdom: intersecting domains
       var2index:
       kernmat:
    Returns:
       objstr: 
    c            so   t  j ˆ  t |  d | d ƒ t |  d | d ƒ d … t |  d | d ƒ t |  d | d ƒ d … f ƒ S(   Ni    i   (   R3   Ri   Rw   R®   (   R¤   R¥   (   Rƒ   (    s   lib/EmbedUtilities.pyR0   9  s    c         S   s2   t  |  d | d ƒ t |  d | d ƒ d d S(   Ni   i    i   (   R®   Rw   (   R¤   R¥   (    (    s   lib/EmbedUtilities.pyR0   ;  s    c         S   s   i  |  ] } t  ƒ  | “ q S(    (   R    (   R]   Rp   (    (    s   lib/EmbedUtilities.pys
   <dictcomp><  s   	 c            s   i  |  ] } | ˆ  | “ q S(    (    (   R]   Rh   (   RP   (    s   lib/EmbedUtilities.pys
   <dictcomp>?  s   	 R1   g        g      à?i    i   N(   R   R6   R>   R   RN   R3   R4   R   RÀ   R«   R¬   Ri   t   multiply(   R7   Rz   RP   R¡   R{   Rƒ   t   coefestt	   dom2pairsRp   R   R#   Rm   R±   RÁ   Ry   R¤   R¥   R´   Rµ   Rº   R·   R¸   R¹   R%   R£   R»   (    (   RP   Rƒ   s   lib/EmbedUtilities.pyt   genCoefs,  s<    "

38@
3<@@w)c         C   s  d } xt  t j | ƒ d ƒ D]ö } xí t  t j | ƒ d ƒ D]Ò } | | | f }	 | | j | | ƒ }
 |	 t g  |
 D]U } t  | ƒ D]B } | D]5 } |  j | | | f ƒ r” | |  | | | f ^ q” qŠ qz ƒ 8}	 | d k rö | |	 d 7} q@ | |	 d | | | f 7} q@ Wq  W| S(   sÄ   estimates objective function when xdict may be fractional
    Args:
       xdict:
       freqmat:
       node2dom:
       scales:
       compcount:
       kernmat:
    Returns:
       objval:
    g        i    i   i   N(   R6   R3   R5   Rs   Ri   Rx   R   (   Rj   R7   t   node2domR¯   Rz   Rƒ   t   objvalt   in1t   in2t   cursumR$   R£   R   R#   (    (    s   lib/EmbedUtilities.pyt   estFracObjectiveY  s      l$c   
      C   s§   t  j d g t  j | ƒ d ƒ } xA | j ƒ  D]3 \ } } } | | | | f | | | | | f <q0 Wt  j | j ƒ  t  j |  | ƒ ƒ d t  j | | ƒ | }	 |	 S(   sª   estimates objective function in matrix form
    Args:
       A:
       b:
       fqsum:
       var2index:
       xdict:
       scales:
    Returns:
       matobjval:
    g        i    i   (   R3   RÀ   R5   R   t   dott	   transpose(
   RÁ   Ry   t   fqsumR{   Rj   t   xvecRp   R   R#   t	   matobjval(    (    s   lib/EmbedUtilities.pyt   estFracObjectiveMatr  s
    #(<c         C   s�   d „  t  t j |  ƒ d ƒ Dƒ } xW t t | ƒ ƒ D]C \ } } x4 t  | d | d d ƒ D] } | | j | ƒ q^ Wq6 W| S(   si   return node 2 domain mapping
    Args:
       freqmat:
       domains:
    Returns:
       node2dom:
    c         S   s   i  |  ] } t  ƒ  | “ q S(    (   R    (   R]   RX   (    (    s   lib/EmbedUtilities.pys
   <dictcomp>�  s   	 i    i   (   R6   R3   R5   R?   t	   enumerateRN   (   R7   RP   RÊ   t   indR£   RX   (    (    s   lib/EmbedUtilities.pyt   getnode2dom…  s
    #"c         C   sU   t  j ƒ  } x | D] } | j | ƒ q Wx$ |  D] \ } } | j | | ƒ q1 W| S(   sr   returns inter domain graph
    Args:
       interdom: list of domains
       domains:
    Returns:
       G: 
    (   Rœ   R�   Rž   RŸ   (   R¡   RP   R¢   R£   R¤   R¥   (    (    s   lib/EmbedUtilities.pyt   inter2Graph”  s    c            s8   d „  ‰ t  t |  ƒ ƒ ‰  t ‡  ‡ f d †  ˆ  Dƒ ƒ S(   sm   returns intersecting domain set
    Args:
       domains: list of domains
    Returns:
       interset: 
    c         S   s8   |  \ } } | \ } } | | k  s0 | | k  r4 t  St S(   N(   RL   RM   (   R]   t   .1t   s1t   e1t   s2t   e2(    (    s   lib/EmbedUtilities.pyR0   ª  s    c         3   s`   |  ]V \ } } t  | d  t ˆ  ƒ ƒ D]0 } ˆ | ˆ  | d  ƒ r& | ˆ  | d  f Vq& q d S(   i   N(   R6   R>   (   R]   RÌ   R¤   RÍ   (   t   enumdomainst	   intersect(    s   lib/EmbedUtilities.pys	   <genexpr>¬  s    (   R?   RÖ   R    (   RP   (    (   Rß   Rà   s   lib/EmbedUtilities.pyt   getInterDomain£  s    	c      	   C   sL   t  |  d ƒ �7 } x- | D]% \ } } | j d j | | ƒ ƒ q WWd QXd S(   sd   writes domain file
    Args:
       domoutfile:
       domains:
       comp2scale:
    Returns:
    R
   s   {0},{1}
N(   R   R   R   (   t
   domoutfileRP   R   RR   RS   (    (    s   lib/EmbedUtilities.pyt   writeDomainFile¯  s    c      
   C   so   g  } t  |  d ƒ �T } xJ | D]B } | j ƒ  j d ƒ } | j t | d ƒ t | d ƒ f ƒ q WWd QX| S(   sM   reads domain file
    Args:
       domfile:
    Returns:
       domains:
    R    t   ,i    i   N(   R   R   RŠ   R   R   (   t   domfileRP   R   R   R“   (    (    s   lib/EmbedUtilities.pyt   readDomainFile¼  s    1c         C   s”   |  j  d ƒ r t } n  d } | r] t j |  d ƒ �" } x | D] } | d 7} q@ WWd QXn3 t |  d ƒ �" } x | D] } | d 7} qv WWd QX| S(   sj   reads freq matrix size without reading data
    Args:
       freqpath:
    Returns:
       nodecount:
    s   .gzi    R    i   N(   t   endswithRM   R   R   (   t   freqpatht   compresst	   nodecountR   R   (    (    s   lib/EmbedUtilities.pyt   readFreqMatrixSizeË  s    	c         C   sz  g  g  } } t  } |  j d ƒ r+ t } n  | r± t j |  d ƒ �i } x_ | D]W } | j ƒ  j d ƒ d d !\ } } } | j | ƒ | j t | ƒ t | ƒ f ƒ qM WWd QXnz t |  d ƒ �i } x_ | D]W } | j ƒ  j d ƒ d d !\ } } } | j | ƒ | j t | ƒ t | ƒ f ƒ qÊ WWd QXt	 j
 t | ƒ t | ƒ f d t	 j ƒ}	 d }
 | rêt j |  d ƒ �r } xh | D]` } | j ƒ  j d ƒ } x8 t t | ƒ ƒ D]$ } t | d | ƒ |	 |
 | f <q«W|
 d 7}
 q}WWd QXnƒ t |  d ƒ �r } xh | D]` } | j ƒ  j d ƒ } x8 t t | ƒ ƒ D]$ } t | d | ƒ |	 |
 | f <q1W|
 d 7}
 qWWd QX|	 | | f S(	   s­   reads input matrix file in gz format
    Args:
       freqfile:
    Returns:
       freqmat:
       nodenames:
       in2pos: index to genome start end locations~(list)
    s   .gzR    s   	i    i   NR1   i   (   RL   Rç   RM   R   R   R   RŠ   R   R   R3   R4   R>   R   R6   (   t   freqfilet	   nodenamest   in2posRé   R   R   RX   RR   RS   R7   Rh   t   partst   index2(    (    s   lib/EmbedUtilities.pyt   readFreqFileà  s>    		%,%)*""c         C   sñ   | d	 k r2 d „  t t j |  ƒ d ƒ Dƒ } n  t j | d ƒ �§ } x� t t j |  ƒ d ƒ D]‚ } d j | | | d | | d ƒ d j g  t t j |  ƒ d ƒ D] } d j |  | | f ƒ ^ q© ƒ } | j | d ƒ qa WWd	 QXd	 S(
   si   writes frequency data to file
    Args:
       freqmat:
       freqfile:
       in2loc:
    Returns:
    c         S   s#   i  |  ] } | | d  g | “ q S(   i   (    (   R]   R×   (    (    s   lib/EmbedUtilities.pys
   <dictcomp>  s   	 i    R
   s   node{0}	{1}	{2}	i   s   	s   {0}s   
N(	   R   R6   R3   R5   R   R   R   R   R   (   R7   Rì   RA   R   RÌ   RÍ   t   linestr(    (    s   lib/EmbedUtilities.pyt   writeFreqData  s    & kc      
   C   s›   t  | d ƒ �† } x| |  j ƒ  D]n } t |  | ƒ t k rj d j g  |  | D] } t | ƒ ^ qL ƒ } n
 |  | } | j d j | | ƒ ƒ q WWd QXd S(   s^   writes deconvolution meta file
    Args:
       metadata:
       objoutfile:
    Returns:
    R
   s   	s   {0}	{1}
N(   R   R   R<   R?   R   R   R   R   (   R’   R–   R   t   keyR^   t   keystr(    (    s   lib/EmbedUtilities.pyR�     s    /
c      	   C   s    i  } t  |  d ƒ �… } x{ | D]s } | j ƒ  j d ƒ } t | ƒ d k rg t | d ƒ | | d <q g  | d D] } t | ƒ ^ qr | | d <q WWd QX| S(   sY   read deconvolution meta file
    Args:
       deconfile:
    Returns:
       metata:
    R    s   	i   i   i    N(   R   R   RŠ   R>   R   (   t	   deconfileR’   R   R   R“   R^   (    (    s   lib/EmbedUtilities.pyt   readDeconMeta,  s    5c      
   C   sà   i  i  } } t  |  d ƒ �¸ } d } x¨ | D]  } | j ƒ  j d ƒ } t | d ƒ | | <t ƒ  | | <xW | d D]K } g  | j d ƒ D] } t | ƒ ^ q‰ \ }	 }
 | | j |	 |
 f ƒ qs W| d 7} q, WWd QX| | f S(   ss   reads deconvolution output
    Args:
       deconoutfile:
    Returns:
       comp2domains:
       comp2scale:
    R    i    s   	i   Rä   N(   R   R   RŠ   R   R    R   RN   (   R•   t   comp2domainsR‚   R   Rh   R   Rï   t   partR^   RR   RS   (    (    s   lib/EmbedUtilities.pyt   readDeconOut>  s    .c         C   s—   t  | d ƒ �‚ } xx |  j ƒ  D]j } d j t | | ƒ g g  |  | D]( } d j | | d | | d ƒ ^ qF ƒ } | j | d ƒ q WWd QXd S(   s¦   writes deconvolution output
    Args:
       comp2dominds: list of tuples
       comp2scale:
       domains:
       outensfile: output ensemble file
    Returns:
    R
   s   	s   {0},{1}i    i   s   
N(   R   R   R   R   R   R   (   Rt   R‚   RP   R•   R   R   Rp   Rò   (    (    s   lib/EmbedUtilities.pyR�   T  s    	Sc   
   	   C   sW  i  } t  } d } t |  d ƒ �*} x | D]} | j ƒ  } | r· | j d ƒ d k r· | j d ƒ d k s˜ | j d ƒ d k s˜ | j d ƒ d k r· t | j d ƒ d ƒ } q+ n  | rô | j d	 ƒ d k rô | j d
 ƒ d k rô t } q+ n  | r+ xF | D]; } | j | ƒ r| j ƒ  \ } }	 t |	 ƒ | | <PqqWq+ q+ WWd QX| | f S(   sï   reads CPLEX output file and returns only SPECIFIC variable values as dictionary
    Args:
       outfile: CPLEX output file
       specific: specific variable prefixes such as x
    Returns:
       retvalues: variable-value dictionary
    R    t	   Objectiveiÿÿÿÿs   - Non-optimal:s
   - Optimal:s
    - Integert   =i   s   Variable Names   Solution ValueN(	   RL   R   R   R   t   findR   RŠ   RM   R   (
   R   t   specifict	   retvaluest   varflagRË   t   fileR   t   varnameRô   t   value(    (    s   lib/EmbedUtilities.pyt   readCplexOutc  s&    [1c         C   s£  t  j j | ƒ s" t  j | ƒ n  d } d } d j | | ƒ } t | d ƒ �T }	 |	 j | d ƒ |	 j |  ƒ |	 j | d ƒ |	 j | d ƒ |	 j d ƒ Wd QXd j | | ƒ }
 d	 j | | ƒ } t | d ƒ �Z }	 |	 j d
 j | ƒ ƒ |	 j d j | ƒ ƒ |	 j d ƒ |	 j d ƒ |	 j d ƒ Wd QXt j ƒ  } d j | |
 ƒ } t  j | ƒ t j ƒ  } d j | | ƒ GH| |
 ƒ } t  j d j | ƒ ƒ | S(   s5  Runs cplex code
    Args:
        consstr: constraint string
        objstr: objective function string
        boundstr: boundary string
        varstr: variable string
        runfolder: run folder
        outmethod = function to be run before returning output
    Returns:
        xdict:
        ydict:
    i   t   cplexruns
   {0}/{1}.lpR
   s   
s   End
Ns   {0}/{1}.lpouts   {0}/{1}.scripts	   read {0}
s   set threads {0}
s	   optimize
s   display solution objective
s   display solution variables -
s   cplex < {0} > {1}s#   Cplex solved problem in {0} secondss
   rm -rf {0}(	   R‹   RŒ   R�   RŽ   R   R   R   t   timet   system(   t   consstrt   objstrt   boundstrR€   t	   runfoldert	   outmethodt   PNUMt   filepreft	   outlppathR  t   cplexoutpatht   cplexscriptpatht   t1t   codet   t2t   returns(    (    s   lib/EmbedUtilities.pyt   runCplexCode€  s6    (8   R‹   t   sysR  t   numpyR3   t   networkxRœ   R   R«   R+   t   randomR	   R   R   R   R&   R;   RK   RW   R[   Rc   Rg   Rr   Rv   R�   R   Rˆ   R˜   Rš   R›   R¦   R    R¼   R½   R¾   R¿   RÂ   RÃ   RÅ   RÉ   RÏ   RÕ   RØ   RÙ   Rá   Rã   Ræ   Rë   Rñ   Ró   R�   R÷   Rú   R�   R  R  (    (    (    s   lib/EmbedUtilities.pyt   <module>   sh   		
								
						
				)				)	(	(-								,				