def choose(rects, lines = [], gauges = [None], trace=''):
    def induce(r):
	if trace == 'induce':
	    pdb.set_trace()
	uncommons = r.get_uncommons()
	if len(uncommons) < 2:
	    return
	irs = []
	for s in rects:
	    if s.dir != r.dir:
		continue
	    pss = []
	    uncs = s.get_uncommons(pickednos)
	    lnos = s.lnos & ~pickednos
	    assert len(uncs) == len(lnos)
	    for unc, lno in zip(uncs, lnos):
		if unc in uncommons:
		    pss.append(lno)
	    if len(pss) == len(uncommons):
		pslnos = immbitset(pss)
		pss = [lines[lno] for lno in pss]
		if s.dir == -1:
		    c = InducedRightRect
		else:
		    c = InducedLeftRect
		ir = c(s, pss, pslnos)
		if trace == 'indap':
		    pdb.set_trace()
		irs.append(ir)
		
	if irs:
	    #pdb.set_trace()
	    news.extend(irs)
		
	
    def overlap(r):
	#
	if 'overlap' in trace:
	    pdb.set_trace()
	rlnos = r.lnos
	tonews = []
	for s in rects:
	    if s is r:
		continue
	    if s.dir != r.dir:
		continue
	    slnos = s.lnos
	    if not (slnos & rlnos):
		continue
	    slnos &= ~ pickednos
	    if not slnos:
		# remove
		continue
	    scom = s.common_part
	    if not scom:
		continue
	    for t in rects:
		if t is s:
		    continue
		if t.dir == s.dir:
		    continue
		tlnos = t.lnos & ~pickednos
		if (tlnos & rlnos):
		    continue
		olnos = tlnos & slnos
		if not olnos:
		    continue
		if slnos == tlnos:
		    continue
		tcom = t.common_part
		if not tcom:
		    continue
		c = cmp_gauged(scom, tcom, gauges)
		if c > 0:
		    continue
		
		if trace == 'obreak':
		    pdb.set_trace
		break
		    
	    else:
		# s is ok
		tonews.append(s)
		rects.remove(s)
	if len(tonews) > 1:
	    pdb.set_trace()
	news.extend(tonews)

    def picknext():
	while 1:
	    if news:
		if trace == 'news':
		    pdb.set_trace()
		r = news[0]
		del news[0]
	    else:
		r = None
		for s in list(rects):
		    slnos = s.lnos - pickednos
		    if not slnos:
			rects.remove(s)
			continue
		    sn = len(slnos) - 1
		    sw = s.width
		    if r is not None:
			if not sw:
			    break
			if not sn:
			    continue
			if rwn:
			    rmemo = r.gainmemo
			    smemo = s.gainmemo
			    c = 0
			    for gauge in gauges:
				try:
				    gr = rmemo[gauge]
				except KeyError:
				    gr = sum_gauge(gauge, r.common_part)
				    rmemo[gauge] = gr
				gr *= rn
				try:
				    gs = smemo[gauge]
				except __HOLE__:
				    gs = sum_gauge(gauge, s.common_part)
				    smemo[gauge] = gs
				gs *= sn
				c = gr - gs
				if c:
				    break
			    if c >= 0:
				continue
		    r = s
		    rlnos = slnos
		    if not sw:
			break
		    rn = sn
		    rw = sw
		    rwn =  sn * sw
		if r is not None:
		    rects.remove(r)
	    if r is not None:
		r.reducelines(pickednos)
		if r.lnos:
		    return r

    def cmpinit(x, y):
	wx = x.width
	wy = y.width
	c = wy - wx
	if c:
	    return c
	c = y.dir - x.dir
	if c:
	    return c
	c = cmp(x.lnos[0], y.lnos[0])
	return c

    if gauges[0] == None:
	gauges = gauges[1:]


    lnobyid = dict([(id(line), i) for i, line in enumerate(lines)])

    orects = rects
    rects = list(orects)


    for r in rects:
	r.init2(lnobyid, lines)

    rects.sort(cmpinit)

    allnos = immbitrange(len(lines))

    pickednos = mutbitset()
    pickedrects = []

    news = []
    while pickednos != allnos:
	r = picknext()
	pickednos |= r.lnos
	pickedrects.append(r)
	induce(r)
	if trace == 'induced':
	    pdb.set_trace()
	overlap(r)

    if trace == 'chosorted':
	pdb.set_trace()

    if trace == 'choosen':
	pdb.set_trace()
    return pickedrects
dataset/ETHPy150Open cloudera/hue/desktop/core/ext-py/guppy-0.1.10/guppy/etc/RE_Rect.py/choose