<s> """<STR_LIT>""" <EOL> import numpy as N <EOL> import math <EOL> import os . path <EOL> from copy import copy <EOL> from collections import deque <EOL> def as_list ( c ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( c , ( list , tuple , set ) ) : <EOL> return c <EOL> elif c is None : <EOL> return [ ] <EOL> else : <EOL> return [ c ] <EOL> def cond ( condition , expr1 , expr2 ) : <EOL> """<STR_LIT>""" <EOL> if condition : <EOL> return expr1 <EOL> else : <EOL> return expr2 <EOL> def flatten ( seq ) : <EOL> """<STR_LIT>""" <EOL> lst = [ ] <EOL> for el in seq : <EOL> if type ( el ) in [ list , tuple , set ] : <EOL> lst . extend ( flatten ( el ) ) <EOL> else : <EOL> lst . append ( el ) <EOL> return lst <EOL> def normalize ( lst ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( lst , N . ndarray ) : <EOL> lst = N . array ( lst ) <EOL> return lst / lst . sum ( ) <EOL> def rescale_logvalues ( lst ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( lst , N . ndarray ) : <EOL> lst = N . array ( lst ) <EOL> return lst - lst . max ( ) <EOL> _LogZero = <NUM_LIT> <EOL> _MinLogExp = math . log ( _LogZero ) ; <EOL> def logadd ( x , y ) : <EOL> """<STR_LIT>""" <EOL> if x < y : <EOL> temp = x <EOL> x = y <EOL> y = temp <EOL> z = math . exp ( y - x ) <EOL> logProb = x + math . log ( <NUM_LIT:1.0> + z ) <EOL> if logProb < _MinLogExp : <EOL> return _MinLogExp <EOL> else : <EOL> return logProb <EOL> def logsum ( lst ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( lst , N . ndarray ) : <EOL> lst = N . array ( lst ) <EOL> maxval = lst . max ( ) <EOL> lst = lst - maxval <EOL> return reduce ( logadd , lst ) + maxval <EOL> def autoassign ( self , locals ) : <EOL> """<STR_LIT>""" <EOL> for ( key , value ) in locals . iteritems ( ) : <EOL> if key == '<STR_LIT>' : <EOL> continue <EOL> setattr ( self , key , value ) <EOL> def unzip ( l , * jj ) : <EOL> """<STR_LIT>""" <EOL> if jj == ( ) : <EOL> jj = range ( len ( l [ <NUM_LIT:0> ] ) ) <EOL> rl = [ [ li [ j ] for li in l ] for j in jj ] <EOL> if len ( rl ) == <NUM_LIT:1> : <EOL> rl = rl [ <NUM_LIT:0> ] <EOL> return rl <EOL> def nestediter ( lst1 , lst2 ) : <EOL> """<STR_LIT>""" <EOL> for i in lst1 : <EOL> for j in lst2 : <EOL> yield ( i , j ) <EOL> def cartesian_product ( list_of_lists ) : <EOL> """<STR_LIT>""" <EOL> head , rest = list_of_lists [ <NUM_LIT:0> ] , list_of_lists [ <NUM_LIT:1> : ] <EOL> if len ( rest ) is <NUM_LIT:0> : <EOL> for val in head : <EOL> yield ( val , ) <EOL> else : <EOL> for val in head : <EOL> for val2 in cartesian_product ( rest ) : <EOL> yield ( val , ) + val2 <EOL> def probwheel ( items , weights ) : <EOL> """<STR_LIT>""" <EOL> weights = normalize ( N . array ( weights ) ) <EOL> binedges = weights . cumsum ( ) <EOL> randval = N . random . random ( ) <EOL> for item , edge in zip ( items , binedges ) : <EOL> if randval <= edge : <EOL> return item <EOL> return items [ - <NUM_LIT:1> ] <EOL> def logscale_probwheel ( items , logweights ) : <EOL> """<STR_LIT>""" <EOL> return probwheel ( items , N . exp ( rescale_logvalues ( logweights ) ) ) <EOL> def entropy_of_list ( lst ) : <EOL> """<STR_LIT>""" <EOL> unique_values = N . unique ( lst ) <EOL> unique_counts = N . array ( [ float ( len ( [ i for i in lst if i == unique_val ] ) ) for unique_val in unique_values ] ) <EOL> total = N . sum ( unique_counts ) <EOL> probs = unique_counts / total <EOL> probs = probs [ probs > <NUM_LIT:0> ] <EOL> return sum ( - probs * N . log ( probs ) ) <EOL> def edit_distance ( network1 , network2 ) : <EOL> """<STR_LIT>""" <EOL> def inverse ( edge ) : <EOL> return ( edge [ <NUM_LIT:1> ] , edge [ <NUM_LIT:0> ] ) <EOL> edges1 = copy ( list ( network1 . edges ) ) <EOL> edges2 = copy ( list ( network2 . edges ) ) <EOL> dist = <NUM_LIT:0> <EOL> for edge in edges1 : <EOL> if edge in edges2 : <EOL> edges2 . remove ( edge ) <EOL> elif inverse ( edge ) in edges2 : <EOL> dist += <NUM_LIT:1> <EOL> edges2 . remove ( inverse ( edge ) ) <EOL> else : <EOL> dist += <NUM_LIT:1> <EOL> dist += len ( edges2 ) <EOL> return dist <EOL> def levenshtein ( a , b ) : <EOL> """<STR_LIT>""" <EOL> n , m = len ( a ) , len ( b ) <EOL> if n > m : <EOL> a , b = b , a <EOL> n , m = m , n <EOL> current = range ( n + <NUM_LIT:1> ) <EOL> for i in range ( <NUM_LIT:1> , m + <NUM_LIT:1> ) : <EOL> previous , current = current , [ i ] + [ <NUM_LIT:0> ] * n <EOL> for j in range ( <NUM_LIT:1> , n + <NUM_LIT:1> ) : <EOL> add , delete = previous [ j ] + <NUM_LIT:1> , current [ j - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> change = previous [ j - <NUM_LIT:1> ] <EOL> if a [ j - <NUM_LIT:1> ] != b [ i - <NUM_LIT:1> ] : <EOL> change = change + <NUM_LIT:1> <EOL> current [ j ] = min ( add , delete , change ) <EOL> return current [ n ] <EOL> def extended_property ( func ) : <EOL> """<STR_LIT>""" <EOL> return property ( doc = func . __doc__ , ** func ( ) ) <EOL> def lru_cache ( maxsize ) : <EOL> '''<STR_LIT>''' <EOL> def decorating_function ( f ) : <EOL> cache = { } <EOL> queue = deque ( ) <EOL> refcount = { } <EOL> def wrapper ( * args ) : <EOL> _cache = cache ; _len = len ; _refcount = refcount ; _maxsize = maxsize <EOL> queue_append = queue . append ; queue_popleft = queue . popleft <EOL> try : <EOL> result = _cache [ args ] <EOL> wrapper . hits += <NUM_LIT:1> <EOL> except KeyError : <EOL> result = _cache [ args ] = f ( * args ) <EOL> wrapper . misses += <NUM_LIT:1> <EOL> queue_append ( args ) <EOL> _refcount [ args ] = _refcount . get ( args , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> while _len ( _cache ) > _maxsize : <EOL> k = queue_popleft ( ) <EOL> _refcount [ k ] -= <NUM_LIT:1> <EOL> if not _refcount [ k ] : <EOL> del _cache [ k ] <EOL> del _refcount [ k ] <EOL> if _len ( queue ) > _maxsize * <NUM_LIT:4> : <EOL> for i in [ None ] * _len ( queue ) : <EOL> k = queue_popleft ( ) <EOL> if _refcount [ k ] == <NUM_LIT:1> : <EOL> queue_append ( k ) <EOL> else : <EOL> _refcount [ k ] -= <NUM_LIT:1> <EOL> assert len ( queue ) == len ( cache ) == len ( refcount ) == sum ( refcount . itervalues ( ) ) <EOL> return result <EOL> wrapper . __doc__ = f . __doc__ <EOL> wrapper . __name__ = f . __name__ <EOL> wrapper . hits = wrapper . misses = <NUM_LIT:0> <EOL> return wrapper <EOL> return decorating_function </s>
