<s> '''<STR_LIT>''' <EOL> import itertools <EOL> import numpy <EOL> def combinations ( list_of_sets ) : <EOL> '''<STR_LIT>''' <EOL> sr = range ( len ( list_of_sets ) ) <EOL> results = [ ] <EOL> for l in range ( <NUM_LIT:1> , len ( list_of_sets ) ) : <EOL> for combination in itertools . combinations ( sr , l ) : <EOL> union = list_of_sets [ combination [ <NUM_LIT:0> ] ] . union ( <EOL> * [ list_of_sets [ x ] for x in combination [ <NUM_LIT:1> : ] ] ) <EOL> inter = list_of_sets [ combination [ <NUM_LIT:0> ] ] . intersection ( <EOL> * [ list_of_sets [ x ] for x in combination [ <NUM_LIT:1> : ] ] ) <EOL> results . append ( ( combination , union , inter ) ) <EOL> return results <EOL> def writeSets ( outfile , list_of_sets , labels = None ) : <EOL> '''<STR_LIT>''' <EOL> all_ids = list_of_sets [ <NUM_LIT:0> ] . union ( * list_of_sets [ <NUM_LIT:1> : ] ) <EOL> if not labels : <EOL> labels = range ( len ( list_of_sets ) ) <EOL> outfile . write ( "<STR_LIT>" % "<STR_LIT:\t>" . join ( map ( str , labels ) ) ) <EOL> for i in sorted ( list ( all_ids ) ) : <EOL> outfile . write ( "<STR_LIT>" % ( <EOL> i , <EOL> "<STR_LIT:\t>" . join ( map ( str , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] [ i in x ] for x in list_of_sets ] ) ) ) ) <EOL> def unionIntersectionMatrix ( list_of_sets ) : <EOL> '''<STR_LIT>''' <EOL> l = len ( list_of_sets ) <EOL> matrix = numpy . zeros ( ( l , l ) , dtype = numpy . int ) <EOL> for x in range ( l ) : <EOL> xx = list_of_sets [ x ] <EOL> for y in range ( x ) : <EOL> yy = list_of_sets [ y ] <EOL> union = xx . union ( yy ) <EOL> inter = xx . intersection ( yy ) <EOL> matrix [ x ] [ y ] = len ( union ) <EOL> matrix [ y ] [ x ] = len ( inter ) <EOL> return matrix <EOL> def getAllCombinations ( * sets ) : <EOL> """<STR_LIT>""" <EOL> if not sets : <EOL> return [ ] <EOL> F = _makeListComprehensionFunction ( '<STR_LIT:F>' , len ( sets ) ) <EOL> return F ( * sets ) <EOL> def _makeListComprehensionFunction ( name , nsets ) : <EOL> """<STR_LIT>""" <EOL> if nsets <= <NUM_LIT:0> : <EOL> source = '<STR_LIT>' % name <EOL> else : <EOL> constructs = [ ( '<STR_LIT>' % i , '<STR_LIT>' % i , '<STR_LIT>' % ( i , i ) ) <EOL> for i in range ( nsets ) ] <EOL> a , e , f = map ( None , * constructs ) <EOL> source = '<STR_LIT>' % ( name , _tuplestr ( a ) , _tuplestr ( e ) , '<STR_LIT:U+0020>' . join ( f ) ) <EOL> scope = { } <EOL> exec source in scope <EOL> return scope [ name ] <EOL> def _tuplestr ( t ) : <EOL> if not t : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT:(>' + '<STR_LIT:U+002C>' . join ( t ) + '<STR_LIT>' <EOL> def xuniqueCombinations ( items , n ) : <EOL> """<STR_LIT>""" <EOL> if n == <NUM_LIT:0> : <EOL> yield [ ] <EOL> else : <EOL> for i in xrange ( len ( items ) ) : <EOL> for cc in xuniqueCombinations ( items [ i + <NUM_LIT:1> : ] , n - <NUM_LIT:1> ) : <EOL> yield [ items [ i ] ] + cc <EOL> def compareLists ( list1 , list2 ) : <EOL> """<STR_LIT>""" <EOL> unique1 = [ ] <EOL> unique2 = [ ] <EOL> common = [ ] <EOL> set1 = sorted ( list1 ) <EOL> set2 = sorted ( list2 ) <EOL> x1 = <NUM_LIT:0> <EOL> x2 = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> if x1 >= len ( set1 ) or x2 >= len ( set2 ) : <EOL> break <EOL> if set2 [ x2 ] == set1 [ x1 ] : <EOL> common . append ( set1 [ x1 ] ) <EOL> x1 += <NUM_LIT:1> <EOL> x2 += <NUM_LIT:1> <EOL> continue <EOL> if set1 [ x1 ] < set2 [ x2 ] : <EOL> unique1 . append ( set1 [ x1 ] ) <EOL> x1 += <NUM_LIT:1> <EOL> continue <EOL> if set1 [ x1 ] > set2 [ x2 ] : <EOL> unique2 . append ( set2 [ x2 ] ) <EOL> x2 += <NUM_LIT:1> <EOL> if x2 < len ( set2 ) : <EOL> unique2 += set2 [ x2 : ] <EOL> elif x1 < len ( set1 ) : <EOL> unique1 += set1 [ x1 : ] <EOL> return unique1 , unique2 , common </s>
