def getAgraph(n, mp=2, st=0.5, verbose=True): keeptrying = True while keeptrying: G = gk.rnd_CG(n, maxindegree=mp, force_connected=True) try: A = transitionMarix2(G, minstrength=st) keeptrying = False except ValueError as e: if verbose: print "!!! Unable to find strong links for a stable matrix !!!" print "*** trying a different graph" return {'graph': G, 'transition': A, 'converges': len(bfutils.call_undersamples(G))}
def getAring(n, density=0.1, st=0.5, verbose=True, dist='flatsigned'): keeptrying = True plusedges = bfutils.dens2edgenum(density, n) while keeptrying: G = gk.ringmore(n, plusedges) try: A = transitionMatrix4(G, minstrength=st, distribution=dist) try: s = A.shape keeptrying = False except AttributeError: keeptrying = True except ValueError: if verbose: print "!!! Unable to find strong links for a stable matrix !!!" print "*** trying a different graph" return {'graph': G, 'transition': A, 'converges': len(bfutils.call_undersamples(G))}
def wrapper_rate_agnostic(fold, n=10, k=10): scipy.random.seed() l = {} while True: try: g = gk.ringmore(n, k) # random ring of given density gs = bfutils.call_undersamples(g) for u in range(1, min([len(gs), UMAX])): g2 = bfutils.undersample(g, u) print fold, ': ', traversal.density(g), ':', startTime = int(round(time.time() * 1000)) s = ur.iteqclass(g2, verbose=False) endTime = int(round(time.time() * 1000)) print len(s) l[u] = {'eq': s, 'ms': endTime - startTime} except MemoryError: print 'memory error... retrying' continue break return {'gt': g, 'solutions': l}
def ra_wrapper_preset(fold, glist=[]): scipy.random.seed() l = {} while True: try: g = glist[fold] gs = bfutils.call_undersamples(g) for u in range(1, min([len(gs), UMAX])): g2 = bfutils.undersample(g, u) print fold, ': ', traversal.density(g), ':', startTime = int(round(time.time() * 1000)) s = ur.liteqclass(g2, verbose=False, capsize=CAPSIZE) endTime = int(round(time.time() * 1000)) print len(s), u l[u] = {'eq': s, 'ms': endTime - startTime} except MemoryError: print 'memory error... retrying' continue break return {'gt': g, 'solutions': l}
def test__call_undersamples(self): gs = [{'1': {'1': {(0, 1)}, '2': {(0, 1)}, '3': {(0, 1)}}, '2': {'2': {(0, 1)}, '3': {(0, 1)}, '4': {(0, 1)}}, '3': {'1': {(0, 1)}, '2': {(0, 1)}, '3': {(0, 1)}, '4': {(0, 1)}}, '4': {'1': {(0, 1)}, '2': {(0, 1)}, '3': {(0, 1)}, '5': {(0, 1)}}, '5': {'1': {(0, 1)}}}, {'1': {'1': {(0, 1)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(2, 0)}}, '2': {'1': {(0, 1), (2, 0)}, '2': {(0, 1)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '3': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '4': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1)}}, '5': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}}}, {'1': {'1': {(0, 1)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '2': {'1': {(0, 1), (2, 0)}, '2': {(0, 1)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '3': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '4': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1)}, '5': {(0, 1), (2, 0)}}, '5': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}}}, {'1': {'1': {(0, 1)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '2': {'1': {(0, 1), (2, 0)}, '2': {(0, 1)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '3': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1), (2, 0)}}, '4': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1)}, '5': {(0, 1), (2, 0)}}, '5': {'1': {(0, 1), (2, 0)}, '2': {(0, 1), (2, 0)}, '3': {(0, 1), (2, 0)}, '4': {(0, 1), (2, 0)}, '5': {(0, 1)}}}] gs_test = bfutils.call_undersamples(self._G) self.assertEqual(gs, gs_test)