<s> from __future__ import print_function <EOL> __author__ = '<STR_LIT>' <EOL> import gc <EOL> import pickle <EOL> import logging <EOL> import threading <EOL> import os <EOL> import operator <EOL> from itertools import count <EOL> from math import sqrt <EOL> from random import random , choice <EOL> from scipy import where , array , exp , zeros , size , mat , median <EOL> from functools import reduce <EOL> known_extensions = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:ascii>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> def abstractMethod ( ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def drawIndex ( probs , tolerant = False ) : <EOL> """<STR_LIT>""" <EOL> if not sum ( probs ) < <NUM_LIT> or not sum ( probs ) > <NUM_LIT> : <EOL> if tolerant : <EOL> probs /= sum ( probs ) <EOL> else : <EOL> print ( ( probs , <NUM_LIT:1> - sum ( probs ) ) ) <EOL> raise ValueError ( ) <EOL> r = random ( ) <EOL> s = <NUM_LIT:0> <EOL> for i , p in enumerate ( probs ) : <EOL> s += p <EOL> if s > r : <EOL> return i <EOL> return choice ( list ( range ( len ( probs ) ) ) ) <EOL> def drawGibbs ( vals , temperature = <NUM_LIT:1.> ) : <EOL> """<STR_LIT>""" <EOL> if temperature == <NUM_LIT:0> : <EOL> m = max ( vals ) <EOL> best = [ ] <EOL> for i , v in enumerate ( vals ) : <EOL> if v == m : <EOL> best . append ( i ) <EOL> return choice ( best ) <EOL> else : <EOL> temp = vals / temperature <EOL> temp += <NUM_LIT:20> - max ( temp ) <EOL> if min ( temp ) < - <NUM_LIT:20> : <EOL> for i , v in enumerate ( temp ) : <EOL> if v < - <NUM_LIT:20> : <EOL> temp [ i ] = - <NUM_LIT:20> <EOL> temp = exp ( temp ) <EOL> temp /= sum ( temp ) <EOL> return drawIndex ( temp ) <EOL> def iterCombinations ( tup ) : <EOL> """<STR_LIT>""" <EOL> if len ( tup ) == <NUM_LIT:1> : <EOL> for i in range ( tup [ <NUM_LIT:0> ] ) : <EOL> yield ( i , ) <EOL> elif len ( tup ) > <NUM_LIT:1> : <EOL> for prefix in iterCombinations ( tup [ : - <NUM_LIT:1> ] ) : <EOL> for i in range ( tup [ - <NUM_LIT:1> ] ) : <EOL> yield tuple ( list ( prefix ) + [ i ] ) <EOL> def setAllArgs ( obj , argdict ) : <EOL> """<STR_LIT>""" <EOL> xmlstore = isinstance ( obj , XMLBuildable ) <EOL> for n in list ( argdict . keys ( ) ) : <EOL> if hasattr ( obj , n ) : <EOL> setattr ( obj , n , argdict [ n ] ) <EOL> if xmlstore : <EOL> obj . argdict [ n ] = argdict [ n ] <EOL> else : <EOL> print ( ( '<STR_LIT>' , n , '<STR_LIT>' ) ) <EOL> if xmlstore : <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> obj . _unknown_argdict = { } <EOL> obj . _unknown_argdict [ n ] = argdict [ n ] <EOL> def linscale ( d , lim ) : <EOL> """<STR_LIT>""" <EOL> return ( d - d . min ( ) ) * ( lim [ <NUM_LIT:1> ] - lim [ <NUM_LIT:0> ] ) + lim [ <NUM_LIT:0> ] <EOL> def percentError ( out , true ) : <EOL> """<STR_LIT>""" <EOL> arrout = array ( out ) . flatten ( ) <EOL> wrong = where ( arrout != array ( true ) . flatten ( ) ) [ <NUM_LIT:0> ] . size <EOL> return <NUM_LIT> * float ( wrong ) / float ( arrout . size ) <EOL> def formatFromExtension ( fname ) : <EOL> """<STR_LIT>""" <EOL> _base , ext = os . path . splitext ( fname ) <EOL> if not ext : <EOL> return None <EOL> try : <EOL> format = known_extensions [ ext . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) ] <EOL> except KeyError : <EOL> format = None <EOL> return format <EOL> class XMLBuildable ( object ) : <EOL> """<STR_LIT>""" <EOL> argdict = None <EOL> def setArgs ( self , ** argdict ) : <EOL> if not self . argdict : <EOL> self . argdict = { } <EOL> setAllArgs ( self , argdict ) <EOL> class Serializable ( object ) : <EOL> """<STR_LIT>""" <EOL> def saveToFileLike ( self , flo , format = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> format = '<STR_LIT>' if format is None else format <EOL> save = getattr ( self , "<STR_LIT>" % format , None ) <EOL> if save is None : <EOL> raise ValueError ( "<STR_LIT>" % format ) <EOL> save ( flo , ** kwargs ) <EOL> @ classmethod <EOL> def loadFromFileLike ( cls , flo , format = None ) : <EOL> """<STR_LIT>""" <EOL> format = '<STR_LIT>' if format is None else format <EOL> load = getattr ( cls , "<STR_LIT>" % format , None ) <EOL> if load is None : <EOL> raise ValueError ( "<STR_LIT>" % format ) <EOL> return load ( flo ) <EOL> def saveToFile ( self , filename , format = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if format is None : <EOL> format = formatFromExtension ( filename ) <EOL> with open ( filename , '<STR_LIT:wb>' ) as fp : <EOL> self . saveToFileLike ( fp , format , ** kwargs ) <EOL> @ classmethod <EOL> def loadFromFile ( cls , filename , format = None ) : <EOL> """<STR_LIT>""" <EOL> if format is None : <EOL> format = formatFromExtension ( filename ) <EOL> with open ( filename , '<STR_LIT>' ) as fp : <EOL> obj = cls . loadFromFileLike ( fp , format ) <EOL> obj . filename = filename <EOL> return obj <EOL> def save_pickle ( self , flo , protocol = <NUM_LIT:0> ) : <EOL> pickle . dump ( self , flo , protocol ) <EOL> @ classmethod <EOL> def load_pickle ( cls , flo ) : <EOL> return pickle . load ( flo ) <EOL> class Named ( XMLBuildable ) : <EOL> """<STR_LIT>""" <EOL> _nameIds = count ( <NUM_LIT:0> ) <EOL> def getName ( self ) : <EOL> logging . warning ( "<STR_LIT>" ) <EOL> return self . name <EOL> def setName ( self , newname ) : <EOL> logging . warning ( "<STR_LIT>" ) <EOL> self . name = newname <EOL> def _getName ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _name is None : <EOL> self . _name = self . _generateName ( ) <EOL> return self . _name <EOL> def _setName ( self , newname ) : <EOL> """<STR_LIT>""" <EOL> self . _name = newname <EOL> _name = None <EOL> name = property ( _getName , _setName ) <EOL> def _generateName ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , next ( self . _nameIds ) ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . name ) <EOL> def fListToString ( a_list , a_precision = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> from numpy import around <EOL> s_list = "<STR_LIT:U+002CU+0020>" . join ( ( "<STR_LIT>" % around ( x , a_precision ) ) . ljust ( a_precision + <NUM_LIT:3> ) <EOL> for x in a_list ) <EOL> return "<STR_LIT>" % s_list <EOL> def tupleRemoveItem ( tup , index ) : <EOL> """<STR_LIT>""" <EOL> l = list ( tup ) <EOL> return tuple ( l [ : index ] + l [ index + <NUM_LIT:1> : ] ) <EOL> def confidenceIntervalSize ( stdev , nbsamples ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:2> * <NUM_LIT> * stdev / sqrt ( nbsamples ) <EOL> def trace ( func ) : <EOL> def inner ( * args , ** kwargs ) : <EOL> print ( ( "<STR_LIT>" % ( func . __name__ , args , kwargs ) ) ) <EOL> return func ( * args , ** kwargs ) <EOL> return inner <EOL> def threaded ( callback = lambda * args , ** kwargs : None , daemonic = False ) : <EOL> """<STR_LIT>""" <EOL> def innerDecorator ( func ) : <EOL> def inner ( * args , ** kwargs ) : <EOL> target = lambda : callback ( func ( * args , ** kwargs ) ) <EOL> t = threading . Thread ( target = target ) <EOL> t . setDaemon ( daemonic ) <EOL> t . start ( ) <EOL> return inner <EOL> return innerDecorator <EOL> def garbagecollect ( func ) : <EOL> """<STR_LIT>""" <EOL> def inner ( * args , ** kwargs ) : <EOL> result = func ( * args , ** kwargs ) <EOL> gc . collect ( ) <EOL> return result <EOL> return inner <EOL> def memoize ( func ) : <EOL> """<STR_LIT>""" <EOL> cache = { } <EOL> def inner ( * args , ** kwargs ) : <EOL> args = tuple ( args ) <EOL> kwargs_set = frozenset ( iter ( kwargs . items ( ) ) ) <EOL> if ( args , kwargs_set ) in cache : <EOL> result = cache [ args , kwargs_set ] <EOL> else : <EOL> result = func ( * args , ** kwargs ) <EOL> cache [ args , kwargs_set ] = result <EOL> return result <EOL> return inner <EOL> def storeCallResults ( obj , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> oldcall = obj . __class__ . __call__ <EOL> def newcall ( * args , ** kwargs ) : <EOL> result = oldcall ( * args , ** kwargs ) <EOL> results . append ( result ) <EOL> if verbose : <EOL> print ( result ) <EOL> return result <EOL> obj . __class__ . __call__ = newcall <EOL> return results <EOL> def multiEvaluate ( repeat ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> def inner ( * args , ** kwargs ) : <EOL> result = <NUM_LIT:0.> <EOL> for dummy in range ( repeat ) : <EOL> result += func ( * args , ** kwargs ) <EOL> return result / repeat <EOL> return inner <EOL> return decorator <EOL> def _import ( name ) : <EOL> """<STR_LIT>""" <EOL> mod = __import__ ( name ) <EOL> components = name . split ( '<STR_LIT:.>' ) <EOL> for comp in components [ <NUM_LIT:1> : ] : <EOL> try : <EOL> mod = getattr ( mod , comp ) <EOL> except AttributeError : <EOL> raise ImportError ( "<STR_LIT>" % mod ) <EOL> return mod <EOL> def int2gray ( i ) : <EOL> """<STR_LIT>""" <EOL> return i ^ ( i >> <NUM_LIT:1> ) <EOL> def gray2int ( g , size ) : <EOL> """<STR_LIT>""" <EOL> res = <NUM_LIT:0> <EOL> for i in reversed ( list ( range ( size ) ) ) : <EOL> gi = ( g >> i ) % <NUM_LIT:2> <EOL> if i == size - <NUM_LIT:1> : <EOL> bi = gi <EOL> else : <EOL> bi = bi ^ gi <EOL> res += bi * <NUM_LIT:2> ** i <EOL> return res <EOL> def asBinary ( i ) : <EOL> """<STR_LIT>""" <EOL> if i > <NUM_LIT:1> : <EOL> if i % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> return asBinary ( i >> <NUM_LIT:1> ) + '<STR_LIT:1>' <EOL> else : <EOL> return asBinary ( i >> <NUM_LIT:1> ) + '<STR_LIT:0>' <EOL> else : <EOL> return str ( i ) <EOL> def one_to_n ( val , maxval ) : <EOL> """<STR_LIT>""" <EOL> a = zeros ( maxval , float ) <EOL> a [ val ] = <NUM_LIT:1.> <EOL> return a <EOL> def n_to_one ( arr ) : <EOL> """<STR_LIT>""" <EOL> return where ( arr == <NUM_LIT:1> ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def canonicClassString ( x ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , object ) : <EOL> return repr ( x . __class__ ) . split ( "<STR_LIT:'>" ) [ <NUM_LIT:1> ] <EOL> else : <EOL> return repr ( x . __class__ ) <EOL> def decrementAny ( tup ) : <EOL> """<STR_LIT>""" <EOL> res = [ ] <EOL> for i , x in enumerate ( tup ) : <EOL> if x > <NUM_LIT:0> : <EOL> res . append ( tuple ( list ( tup [ : i ] ) + [ x - <NUM_LIT:1> ] + list ( tup [ i + <NUM_LIT:1> : ] ) ) ) <EOL> return res <EOL> def reachable ( stepFunction , start , destinations , _alreadyseen = None ) : <EOL> """<STR_LIT>""" <EOL> if len ( start ) == <NUM_LIT:0> or len ( destinations ) == <NUM_LIT:0> : <EOL> return { } <EOL> if _alreadyseen is None : <EOL> _alreadyseen = [ ] <EOL> _alreadyseen . extend ( start ) <EOL> res = { } <EOL> for s in start : <EOL> if s in destinations : <EOL> res [ s ] = <NUM_LIT:0> <EOL> start . remove ( s ) <EOL> new = set ( ) <EOL> for s in start : <EOL> new . update ( stepFunction ( s ) ) <EOL> new . difference_update ( _alreadyseen ) <EOL> ndestinations = list ( destinations ) <EOL> for s in list ( new ) : <EOL> if s in destinations : <EOL> res [ s ] = <NUM_LIT:1> <EOL> new . remove ( s ) <EOL> ndestinations . remove ( s ) <EOL> _alreadyseen . append ( s ) <EOL> deeper = reachable ( stepFunction , new , ndestinations , _alreadyseen ) <EOL> for k , val in list ( deeper . items ( ) ) : <EOL> res [ k ] = val + <NUM_LIT:1> <EOL> return res <EOL> def flood ( stepFunction , fullSet , initSet , relevant = None ) : <EOL> """<STR_LIT>""" <EOL> if fullSet is None : <EOL> flooded = set ( initSet ) <EOL> else : <EOL> full = set ( fullSet ) <EOL> flooded = full . intersection ( set ( initSet ) ) <EOL> if relevant is None : <EOL> relevant = full . copy ( ) <EOL> if relevant : <EOL> relevant = set ( relevant ) <EOL> change = flooded . copy ( ) <EOL> while len ( change ) > <NUM_LIT:0> : <EOL> new = set ( ) <EOL> for m in change : <EOL> if fullSet is None : <EOL> new . update ( stepFunction ( m ) ) <EOL> else : <EOL> new . update ( full . intersection ( stepFunction ( m ) ) ) <EOL> change = new . difference ( flooded ) <EOL> flooded . update ( change ) <EOL> if relevant is not None and relevant . issubset ( flooded ) : <EOL> break <EOL> return list ( flooded ) <EOL> def crossproduct ( ss , row = None , level = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if row is None : <EOL> row = [ ] <EOL> if len ( ss ) > <NUM_LIT:1> : <EOL> return reduce ( operator . add , <EOL> [ crossproduct ( ss [ <NUM_LIT:1> : ] , row + [ i ] , level + <NUM_LIT:1> ) for i in ss [ <NUM_LIT:0> ] ] ) <EOL> else : <EOL> return [ row + [ i ] for i in ss [ <NUM_LIT:0> ] ] <EOL> def permute ( arr , permutation ) : <EOL> """<STR_LIT>""" <EOL> return array ( [ arr [ i ] for i in permutation ] ) <EOL> def permuteToBlocks ( arr , blockshape ) : <EOL> """<STR_LIT>""" <EOL> if len ( blockshape ) < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif len ( blockshape ) == <NUM_LIT:2> : <EOL> blockheight , blockwidth = blockshape <EOL> return permuteToBlocks2d ( arr , blockheight , blockwidth ) <EOL> elif len ( blockshape ) == <NUM_LIT:3> : <EOL> blockdepth , blockheight , blockwidth = blockshape <EOL> return permuteToBlocks3d ( arr , blockdepth , blockheight , blockwidth ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def permuteToBlocks3d ( arr , blockdepth , blockheight , blockwidth ) : <EOL> depth , height , width = arr . shape <EOL> arr_ = arr . reshape ( height * depth , width ) <EOL> arr_ = permuteToBlocks2d ( arr_ , blockheight , blockwidth ) <EOL> arr_ . shape = depth , height * width <EOL> return permuteToBlocks2d ( arr_ , blockdepth , blockwidth * blockheight ) <EOL> def permuteToBlocks2d ( arr , blockheight , blockwidth ) : <EOL> _height , width = arr . shape <EOL> arr = arr . flatten ( ) <EOL> new = zeros ( size ( arr ) ) <EOL> for i in range ( size ( arr ) ) : <EOL> blockx = ( i % width ) / blockwidth <EOL> blocky = i / width / blockheight <EOL> blockoffset = blocky * width / blockwidth + blockx <EOL> blockoffset *= blockwidth * blockheight <EOL> inblockx = i % blockwidth <EOL> inblocky = ( i / width ) % blockheight <EOL> j = blockoffset + inblocky * blockwidth + inblockx <EOL> new [ j ] = arr [ i ] <EOL> return new <EOL> def triu2flat ( m ) : <EOL> """<STR_LIT>""" <EOL> dim = m . shape [ <NUM_LIT:0> ] <EOL> res = zeros ( dim * ( dim + <NUM_LIT:1> ) / <NUM_LIT:2> ) <EOL> index = <NUM_LIT:0> <EOL> for row in range ( dim ) : <EOL> res [ index : index + dim - row ] = m [ row , row : ] <EOL> index += dim - row <EOL> return res <EOL> def flat2triu ( a , dim ) : <EOL> """<STR_LIT>""" <EOL> res = zeros ( ( dim , dim ) ) <EOL> index = <NUM_LIT:0> <EOL> for row in range ( dim ) : <EOL> res [ row , row : ] = a [ index : index + dim - row ] <EOL> index += dim - row <EOL> return res <EOL> def blockList2Matrix ( l ) : <EOL> """<STR_LIT>""" <EOL> dims = [ m . shape [ <NUM_LIT:0> ] for m in l ] <EOL> s = sum ( dims ) <EOL> res = zeros ( ( s , s ) ) <EOL> index = <NUM_LIT:0> <EOL> for i in range ( len ( l ) ) : <EOL> d = dims [ i ] <EOL> m = l [ i ] <EOL> res [ index : index + d , index : index + d ] = m <EOL> index += d <EOL> return res <EOL> def blockCombine ( l ) : <EOL> """<STR_LIT>""" <EOL> l = [ list ( map ( mat , row ) ) for row in l ] <EOL> hdims = [ m . shape [ <NUM_LIT:1> ] for m in l [ <NUM_LIT:0> ] ] <EOL> hs = sum ( hdims ) <EOL> vdims = [ row [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] for row in l ] <EOL> vs = sum ( vdims ) <EOL> res = zeros ( ( hs , vs ) ) <EOL> vindex = <NUM_LIT:0> <EOL> for i , row in enumerate ( l ) : <EOL> hindex = <NUM_LIT:0> <EOL> for j , m in enumerate ( row ) : <EOL> res [ vindex : vindex + vdims [ i ] , hindex : hindex + hdims [ j ] ] = m <EOL> hindex += hdims [ j ] <EOL> vindex += vdims [ i ] <EOL> return res <EOL> def avgFoundAfter ( decreasingTargetValues , listsOfActualValues , batchSize = <NUM_LIT:1> , useMedian = False ) : <EOL> """<STR_LIT>""" <EOL> from scipy import sum <EOL> numLists = len ( listsOfActualValues ) <EOL> longest = max ( list ( map ( len , listsOfActualValues ) ) ) <EOL> res = [ [ <NUM_LIT:0> ] for _ in range ( numLists ) ] <EOL> for tval in decreasingTargetValues : <EOL> for li , l in enumerate ( listsOfActualValues ) : <EOL> lres = res [ li ] <EOL> found = False <EOL> for i in range ( lres [ - <NUM_LIT:1> ] , len ( l ) ) : <EOL> if l [ i ] <= tval : <EOL> lres . append ( i ) <EOL> found = True <EOL> break <EOL> if not found : <EOL> lres . append ( longest ) <EOL> tmp = array ( res ) <EOL> if useMedian : <EOL> resx = median ( tmp , axis = <NUM_LIT:0> ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> resx = sum ( tmp , axis = <NUM_LIT:0> ) [ <NUM_LIT:1> : ] / float ( numLists ) <EOL> return resx * batchSize <EOL> class DivergenceError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def matchingDict ( d , selection , require_existence = False ) : <EOL> """<STR_LIT>""" <EOL> for k , v in list ( selection . items ( ) ) : <EOL> if k in d : <EOL> if isinstance ( v , list ) : <EOL> if d [ k ] not in v : <EOL> return False <EOL> else : <EOL> if d [ k ] != v : <EOL> return False <EOL> elif require_existence : <EOL> return False <EOL> return True <EOL> def subDict ( d , allowedkeys , flip = False ) : <EOL> """<STR_LIT>""" <EOL> res = { } <EOL> for k , v in list ( d . items ( ) ) : <EOL> if ( k in allowedkeys ) ^ flip : <EOL> res [ k ] = v <EOL> return res <EOL> def dictCombinations ( listdict ) : <EOL> """<STR_LIT>""" <EOL> listdict = listdict . copy ( ) <EOL> if len ( listdict ) == <NUM_LIT:0> : <EOL> return [ { } ] <EOL> k , vs = listdict . popitem ( ) <EOL> res = dictCombinations ( listdict ) <EOL> if isinstance ( vs , list ) or isinstance ( vs , tuple ) : <EOL> res = [ dict ( d , ** { k : v } ) for d in res for v in sorted ( set ( vs ) ) ] <EOL> else : <EOL> res = [ dict ( d , ** { k : vs } ) for d in res ] <EOL> return res <EOL> def r_argmax ( v ) : <EOL> """<STR_LIT>""" <EOL> if len ( v ) == <NUM_LIT:1> : <EOL> return <NUM_LIT:0> <EOL> maxbid = max ( v ) <EOL> maxbidders = [ i for ( i , b ) in enumerate ( v ) if b == maxbid ] <EOL> return choice ( maxbidders ) <EOL> def all_argmax ( x ) : <EOL> """<STR_LIT>""" <EOL> m = max ( x ) <EOL> return [ i for i , v in enumerate ( x ) if v == m ] <EOL> def dense_orth ( dim ) : <EOL> """<STR_LIT>""" <EOL> from scipy import rand <EOL> from scipy . linalg import orth <EOL> return orth ( rand ( dim , dim ) ) <EOL> def sparse_orth ( d ) : <EOL> """<STR_LIT>""" <EOL> from scipy . sparse import eye <EOL> from scipy import r_ , pi , sin , cos <EOL> if d % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> seq = r_ [ <NUM_LIT:0> : d : <NUM_LIT:2> , <NUM_LIT:1> : d - <NUM_LIT:1> : <NUM_LIT:2> ] <EOL> else : <EOL> seq = r_ [ <NUM_LIT:0> : d - <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:1> : d : <NUM_LIT:2> ] <EOL> Q = eye ( d , d ) . tocsc ( ) <EOL> for i in seq : <EOL> theta = random ( ) * <NUM_LIT:2> * pi <EOL> flip = ( random ( ) - <NUM_LIT:0.5> ) > <NUM_LIT:0> ; <EOL> Qi = eye ( d , d ) . tocsc ( ) <EOL> Qi [ i , i ] = cos ( theta ) <EOL> Qi [ ( i + <NUM_LIT:1> ) , i ] = sin ( theta ) <EOL> if flip > <NUM_LIT:0> : <EOL> Qi [ i , ( i + <NUM_LIT:1> ) ] = - sin ( theta ) <EOL> Qi [ ( i + <NUM_LIT:1> ) , ( i + <NUM_LIT:1> ) ] = cos ( theta ) <EOL> else : <EOL> Qi [ i , ( i + <NUM_LIT:1> ) ] = sin ( theta ) <EOL> Qi [ ( i + <NUM_LIT:1> ) , ( i + <NUM_LIT:1> ) ] = - cos ( theta ) <EOL> Q = Q * Qi ; <EOL> return Q <EOL> def xhash ( arr ) : <EOL> """<STR_LIT>""" <EOL> import hashlib <EOL> return hashlib . sha1 ( arr ) . hexdigest ( ) <EOL> def binArr2int ( arr ) : <EOL> """<STR_LIT>""" <EOL> from numpy import packbits <EOL> tmp2 = packbits ( arr . astype ( int ) ) <EOL> return sum ( val * <NUM_LIT> ** i for i , val in enumerate ( tmp2 [ : : - <NUM_LIT:1> ] ) ) <EOL> def uniqueArrays ( vs ) : <EOL> """<STR_LIT>""" <EOL> resdic = { } <EOL> for v in vs : <EOL> resdic [ xhash ( v ) ] = v <EOL> return list ( resdic . values ( ) ) <EOL> def seedit ( seed = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> import random <EOL> import numpy <EOL> random . seed ( seed ) <EOL> numpy . random . seed ( seed ) <EOL> def weightedUtest ( g1 , w1 , g2 , w2 ) : <EOL> """<STR_LIT>""" <EOL> from scipy . stats . distributions import norm <EOL> import numpy <EOL> n1 = sum ( w1 ) <EOL> n2 = sum ( w2 ) <EOL> u1 = <NUM_LIT:0.> <EOL> for x1 , wx1 in zip ( g1 , w1 ) : <EOL> for x2 , wx2 in zip ( g2 , w2 ) : <EOL> if x1 == x2 : <EOL> u1 += <NUM_LIT:0.5> * wx1 * wx2 <EOL> elif x1 > x2 : <EOL> u1 += wx1 * wx2 <EOL> mu = n1 * n2 / <NUM_LIT> <EOL> sigu = numpy . sqrt ( n1 * n2 * ( n1 + n2 + <NUM_LIT:1> ) / <NUM_LIT> ) <EOL> z = ( u1 - mu ) / sigu <EOL> conf = norm . cdf ( z ) <EOL> return conf </s>
