def add2set(gset, elist, H): n = len(H) s = set() ss = set() eremove = {e: True for e in elist} for gnum in gset: g = bfu.num2CG(gnum, n) for e in elist: if not e[1] in g[e[0]]: gk.addanedge(g, e) num = bfu.g2num(g) if not num in s: au = bfu.call_undersamples(g) if not gk.checkconflict(H, g, au=au): eremove[e] = False s.add(num) if gk.checkequality(H, g, au=au): ss.add(num) gk.delanedge(g, e) for e in eremove: if eremove[e]: elist.remove(e) return s, ss, elist
def add2set(gset, elist, H): n = len(H) s = set() ss = set() eremove = {e: True for e in elist} for gnum in gset: g = bfu.num2CG(gnum, n) for e in elist: if not e[1] in g[e[0]]: gk.addanedge(g,e) num = bfu.g2num(g) if not num in s: au = bfu.call_undersamples(g) if not gk.checkconflict(H, g, au=au): eremove[e] = False s.add(num) if gk.checkequality(H, g, au=au): ss.add(num) gk.delanedge(g,e) for e in eremove: if eremove[e]: elist.remove(e) return s, ss, elist
def getAring(n, density=0.1, st=0.5, verbose=True, dist='flatsigned', permute=False): keeptrying = True plusedges = bfutils.dens2edgenum(density, n) while keeptrying: G = gk.ringmore(n, plusedges, permute=permute) 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 checkconflict(H,G_test, au = None): if not au: allundersamples = bfu.call_undersamples(G_test) else: allundersamples = au for graph in allundersamples: if isedgesubset(graph,H): return False return True
def checkconflict(H, G_test, au=None): if not au: allundersamples = bfu.call_undersamples(G_test) else: allundersamples = au for graph in allundersamples: if isedgesubset(graph, H): return False return True
def checkequality(H, G_test, au=None): if not au: allundersamples = bfu.call_undersamples(G_test) else: allundersamples = au for graph in allundersamples: if graph == H: return True return False
def checkequality(H,G_test, au = None): if not au: allundersamples = bfu.call_undersamples(G_test) else: allundersamples = au for graph in allundersamples: if graph == H: return True return False
def determine_reachable_graphs(n): reachableList = [] gts = determine_domain(n) for graph in gts: reachable_graphs = bfutils.call_undersamples(graph)[1:] #because 0 returns itself and that's boring and we want undersampling rates higher than 1 for rg in reachable_graphs: if rg not in reachableList: reachableList.append(rg) return reachableList
def determine_SCC_reachable_graphs(n): reachableList = [] gts = determine_SCC_domain(n) for graph in gts: reachable_graphs = bfutils.call_undersamples(graph)[1:] #because 0 returns itself for rg in reachable_graphs: if rg not in reachableList: reachableList.append(rg) return reachableList
def generate_H(num_nodes): numextraedge = np.random.randint(low = 1,high = 3) g = bfutils.ringmore(num_nodes,numextraedge) #ground truth gs= bfutils.call_undersamples(g) # all possible undersamples for g randomu = np.random.randint(low = 1,high = len(gs)) #now we can make the H H = bfutils.undersample(g,randomu) #print H return H
def checkconflict_(Hnum, G_test, au = None): if not au: allundersamples = bfu.call_undersamples(G_test) else: allundersamples = au #Hnum = bfu.ug2num(H) for graph in allundersamples: gnum = bfu.ug2num(graph) if gnum[0]&Hnum[0] == gnum[0] and gnum[1]&Hnum[1] == gnum[1]: return False return True
def checkconflict_(Hnum, G_test, au=None): if not au: allundersamples = bfu.call_undersamples(G_test) else: allundersamples = au #Hnum = bfu.ug2num(H) for graph in allundersamples: gnum = bfu.ug2num(graph) if gnum[0] & Hnum[0] == gnum[0] and gnum[1] & Hnum[1] == gnum[1]: return False return True
def fastest_g(L): x = [] y = [] for l in L: d = zkl.load(l) for i in range(0,100): gs = bfutils.call_undersamples(d[i]['gt']) #this helps us determine how far u will go for u in range(1,len(d[i]['solutions'])+1): g2 = bfutils.undersample(d[i]['gt'],u) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][u]['ms']) #add the time return x,map(lambda x: x/1000./60., y)
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 fastest_g(L): x = [] y = [] for l in L: d = zkl.load(l) for i in range(0, 100): gs = bfutils.call_undersamples( d[i]['gt']) #this helps us determine how far u will go for u in range(1, len(d[i]['solutions']) + 1): g2 = bfutils.undersample(d[i]['gt'], u) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][u]['ms']) #add the time return x, map(lambda x: x / 1000. / 60., y)
def gen_x_y(L): x = [] y = [] for l in L: d = zkl.load(l) for i in range(0,100): gs = bfutils.call_undersamples(d[i]['gt']) #this helps us determine how far u will go for u in range(1,len(d[i]['solutions'])+1): #for u in range(1,min([len(gs),4])): g2 = bfutils.undersample(d[i]['gt'],u) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][u]['ms']) #add the time return x,y
def gen_x_y(L): x = [] y = [] for l in L: d = zkl.load(l) for i in range(0, 100): gs = bfutils.call_undersamples( d[i]['gt']) #this helps us determine how far u will go for u in range(1, len(d[i]['solutions']) + 1): #for u in range(1,min([len(gs),4])): g2 = bfutils.undersample(d[i]['gt'], u) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][u]['ms']) #add the time return x, y
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', permute=False): keeptrying = True plusedges = bfutils.dens2edgenum(density,n) while keeptrying: G = gk.ringmore(n, plusedges, permute=permute) 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 = bfutils.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 wrapper_rate_agnostic(fold, n=10, k=10): scipy.random.seed() l = {} while True: try: g = bfutils.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}
import bfutils as bfu import matplotlib.pyplot as plt NODES = 6 DENSITY = 0.2 UMAX = 6 REPEATS = 100 d = zkl.load('leibnitz_nodes_6_density_0.2_ra_.zkl') x = [] y = [] for i in range(0, REPEATS): gs = bfutils.call_undersamples( d[i]['gt']) #this helps us determine how far u will go for u in range(1, min([len(gs), UMAX])): g2 = bfutils.undersample(d[i]['gt'], u) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][u]['ms']) #add the time print len(x) print len(y) fig = plt.figure() ax = plt.gca() ax.scatter(x, y) ax.set_yscale('log') plt.xlabel('edge density of H') plt.ylabel('log scale time') plt.title('Number of Nodes: %s , Density: %s ,UMAX: %s' % (NODES, DENSITY, UMAX))
def determine_reachable_combination(X,Y): print "tbd" reachableList = [] gts = determine_domain(n) for graph in gts: reachable_graphs = bfutils.call_undersamples(graph)[1:]
def getrates(g,H): n = len(H) au = bfu.call_undersamples(g) return list(np.where(map(lambda x: x == H, au))[0])
def getrates(g, H): n = len(H) au = bfu.call_undersamples(g) return list(np.where(map(lambda x: x == H, au))[0])