def test__liteqclass(self): u = 1 set_of_u_1 = zkl.load("{}/liteqclass_output_n_5_u_1.zkl".format(self._ABS_PATH)) g2 = bfutils.undersample(self._G, u) s = ur.liteqclass(g2, verbose=False, capsize=1000) self.assertEqual(s, set_of_u_1) u = 2 set_of_u_2 = zkl.load("{}/liteqclass_output_n_5_u_2.zkl".format(self._ABS_PATH)) g2 = bfutils.undersample(self._G, u) s = ur.liteqclass(g2, verbose=False, capsize=1000) self.assertEqual(s, set_of_u_2)
def supergraphs_in_eq(g, g2, rate=1): '''Find all supergraphs of g that are also in the same equivalence class with respect to g2 and the rate. Currently works only for bfu.undersample by 1 ''' if bfu.undersample(g, rate) != g2: raise ValueError('g is not in equivalence class of g2') s = set() def addnodes(g, g2, edges): if edges: masks = [] for e in edges: if ok2addanedge(e[0], e[1], g, g2, rate=rate): masks.append(True) else: masks.append(False) nedges = [edges[i] for i in range(len(edges)) if masks[i]] n = len(nedges) if n: for i in range(n): mask = addanedge(g, nedges[i]) s.add(g2num(g)) addnodes(g, g2, nedges[:i] + nedges[i + 1:]) delanedge(g, nedges[i], mask) edges = gk.edgelist(gk.complement(g)) addnodes(g, g2, edges) return s
def ok2addaVpath(e, p, g, g2, rate=2): mask = addaVpath(g, e, p) if not gk.isedgesubset(bfu.undersample(g, rate), g2): cleanVedges(g, e, p, mask) return False cleanVedges(g, e, p, mask) return True
def estOE(d): gt = d['gt']['graph'] gt = bfu.undersample(gt, 1) e = gk.OCE(d['estimate'], gt) N = np.double(len(gk.edgelist(gt))) +\ np.double(len(gk.bedgelist(gt))) return (e['directed'][0] + e['bidirected'][0]) / N
def simple_loops(g, u): """ iterator over the list of simple loops of graph g at the undersample rate u """ gx = graph2nx(num2CG(g2num(undersample(g, u)), len(g))) for l in networkx.simple_cycles(gx): yield l
def estCOE(d): gt = d['gt']['graph'] gt = bfu.undersample(gt, 1) e = gk.OCE(d['estimate'], gt) n = len(gt) N = np.double(n ** 2 + (n - 1) ** 2 / 2.0 - len(gk.edgelist(gt)) - len(gk.bedgelist(gt))) return (e['directed'][1] + e['bidirected'][1]) / N
def unfoldplot(G, steps=7, repeats=5, gap=0.5, R=1, hg=0.1, wgap=0.7, name='AAA', stl=''): u = 0 dbnprint(undersample(G, u), repeats, w_gap=wgap, h_gap=hg, mname=name + str(u), type='hid', stl=stl) print "\\node[left=" + str(gap) + "cm of " + name + str(u) + ",scale=0.7] (C) {" print "\\begin{tikzpicture}" cdbnprint(G, mtype='hid', bend=15, curve=5, R=R) print "\\end{tikzpicture}" print "};" for u in range(1, steps): dbnprint(undersample(G, u), repeats, w_gap=wgap, h_gap=hg, mname=name + str(u), type='ahid', stl=', below=0.25cm of ' + name + str(u - 1)) print "\\node[left=" + str(gap) + "cm of " + name + str(u) + ",scale=0.7] () {" print "\\begin{tikzpicture}" cdbnprint(undersample(G, u), mtype='lahid', bend=15, curve=5, R=R) print "\\end{tikzpicture}" print "};" emacs_vars()
def SM_converging(Gstar, G): """Gstar is the undersampled reference graph, while G is the starting graph. The code searches over all undersampled version of G to find all matches with Gstar """ compat = [] GG = G Gprev = G if G == Gstar: return [0] j = 1 G = undersample(GG, j) while not (G == Gprev): if Gstar == G: compat.append(j) j += 1 Gprev = G G = undersample(GG, j) return compat
def cdbnwrap(G, u, name='AAA', R=1, gap=0.5): output = StringIO.StringIO() print >>output, "\\node[right=" + str(gap) + "cm of " + name + str(u - 1)\ + ",scale=0.7](" + name + str(u) + "){" print >>output, "\\begin{tikzpicture}" s = cdbnprint(undersample(G, u), mtype='lahid', bend=25, curve=10, R=R) print >>output, s.getvalue() s.close() print >>output, "\\end{tikzpicture}" print >>output, "};" return output
def nodesearch(g, g2, order, inlist, s, cds, pool, pc): if order: if bfu.undersample(g, rate) == g2: s.add(g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements') s.update(supergraphs_in_eq(g, g2, rate=rate)) return g key = order[0] if pc: tocheck = [x for x in pc if x in cds[len(inlist) - 1][inlist[0]]] else: tocheck = cds[len(inlist) - 1][inlist[0]] if len(order) > 1: kk = order[1] pc = predictive_check(g, g2, pool[len(inlist)], c[edge_function_idx(kk)], kk) else: pc = set() adder, remover, masker = f[edge_function_idx(key)] checks_ok = c[edge_function_idx(key)] for n in tocheck: if not checks_ok(key, n, g, g2, rate=rate): continue masked = np.prod(masker(g, key, n)) if masked: nodesearch(g, g2, order[1:], [n] + inlist, s, cds, pool, pc) else: mask = adder(g, key, n) nodesearch(g, g2, order[1:], [n] + inlist, s, cds, pool, pc) remover(g, key, n, mask) elif bfu.undersample(g, rate) == g2: s.add(g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements') return g
def nodesearch(g, g2, edges, s): if edges: if bfu.undersample(g, rate) == g2: s.add(g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements') return g e = edges[0] for n in ln: if (n, e) in single_cache: continue if not ok2addaVpath(e, n, g, g2, rate=rate): continue mask = addaVpath(g, e, n) r = nodesearch(g, g2, edges[1:], s) delaVpath(g, e, n, mask) elif bfu.undersample(g, rate) == g2: s.add(g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements in eqclass') return 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 addnodes(g, g2, edges): if edges: masks = [] for e in edges: if ok2addanedge_(e[0], e[1], g, g2, rate=rate): masks.append(True) else: masks.append(False) nedges = [edges[i] for i in range(len(edges)) if masks[i]] n = len(nedges) if n: for i in range(n): mask = addanedge(g, nedges[i]) if bfu.undersample(g, rate) == g2: s.add(g2num(g)) addnodes(g, g2, nedges[:i] + nedges[i + 1:]) delanedge(g, nedges[i], mask) return s else: return noop else: return noop
def g_single(G, u, scale=0.7, R=1, gap=0.5, mtype="lahid", layout=None): g = undersample(G, u) return gsingle(g, scale=scale, R=R, gap=gap, mtype=mtype, layout=layout)
def cdbn_single(G, u, scale=0.7, R=1, gap=0.5, mtype="lahid"): return cdbnsingle(undersample(G, u), scale=scale, R=R, gap=gap, mtype=mtype)
def ok2addanedge2(s, e, g, g2, rate=1): mask = addanedge(g, (s, e)) value = bfu.undersample(g, rate) == g2 delanedge(g, (s, e), mask) return value
def ok2addanedge_sub(s, e, g, g2, rate=1): mask = addanedge(g, (s, e)) value = gk.isedgesubset(bfu.undersample(g, rate), g2) delanedge(g, (s, e), mask) return value
def test__call_undersample(self): u = 1 g_u_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)}}} g2 = bfutils.undersample(self._G, u) self.assertEqual(g_u_1, g2) u = 2 g_u_2 = {'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)}}} g2 = bfutils.undersample(self._G, u) self.assertEqual(g_u_2, g2) u = 4 g_u_4 = {'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)}}} g2 = bfutils.undersample(self._G, u) self.assertEqual(g_u_4, g2)
def SM_fixed(Gstar, G, iter=5): compat = [] for j in range(0, iter): if Gstar == undersample(G, j): compat.append(j) return compat
def wrapper(fold, n=10, dens=0.1): scipy.random.seed() rate = 2 r = None s = set() counter = 0 while not s: scipy.random.seed() sst = 0.9 r = None while not r: r = lm.getAring(n, dens, sst, False, dist=DIST) print sst, sys.stdout.flush() if sst < 0.03: sst -= 0.001 else: sst -= 0.01 if sst < 0: sst = 0.02 # pprint.pprint(r['transition'].round(2),width=200) # d = zkl.load('leibnitz_nodes_'+str(n)+'_OCE_model_.zkl') # r = d[dens][fold] g = r['graph'] true_g2 = bfu.undersample(g, rate - 1) data = lm.drawsamplesLG(r['transition'], samples=BURNIN + SAMPLESIZE * 2, nstd=np.double(NOISE_STD)) data = data[:, BURNIN:] if np.max(data) > 1000.: pprint.pprint(r['transition'].round(2), width=200) # raise ValueError startTime = int(round(time.time() * 1000)) if EST == 'pc': g2 = pc.dpc(data[:, ::2], pval=0.0001) elif EST == 'svar': g2 = lm.data2graph(data[:, ::2]) if trv.density(g2) < 0.7: print gk.OCE(g2, true_g2) # s = examine_bidirected_flips(g2, depth=DEPTH) s = find_nearest_reachable(g2, max_depth=1) # s = trv.v2g22g1(g2, capsize=CAPSIZE, verbose=False) # s = trv.edge_backtrack2g1_directed(g2, capsize=CAPSIZE) # s = timeout(trv.v2g22g1, # s = timeout(trv.edge_backtrack2g1_directed, # args=(g2,CAPSIZE), # timeout_duration=1000, default=set()) print 'o', sys.stdout.flush() if -1 in s: s = set() endTime = int(round(time.time() * 1000)) # if counter > 3: # print 'not found' # return None counter += 1 print '' oce = [gk.OCE(num2CG(x, n), g) for x in s] cum_oce = [sum(x['directed']) + sum(x['bidirected']) for x in oce] idx = np.argmin(cum_oce) print "{:2}: {:8} : {:4} {:10} seconds".\ format(fold, round(dens, 3), cum_oce[idx], round((endTime - startTime) / 1000., 3)) # np.set_printoptions(formatter={'float': lambda x: format(x, '6.3f')+", "}) # pprint.pprint(r['transition'].round(2)) # np.set_printoptions() return {'gt': r, 'eq': s, 'OCE': oce[idx], 'tries_till_found': counter, 'estimate': g2, 'graphs_tried': counter, 'strength': sst + 0.01, 'ms': endTime - startTime}
def backtrack_more(g2, rate=1, capsize=None): ''' computes all g1 that are in the equivalence class for g2 ''' if bfu.isSclique(g2): print 'Superclique - any SCC with GCD = 1 fits' return set([-1]) single_cache = {} if rate == 1: ln = [n for n in g2] else: ln = [] for x in itertools.combinations_with_replacement(g2.keys(), rate): ln.extend(itertools.permutations(x, rate)) ln = set(ln) @memo # memoize the search def nodesearch(g, g2, edges, s): if edges: if bfu.undersample(g, rate) == g2: s.add(g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements') return g e = edges[0] for n in ln: if (n, e) in single_cache: continue if not ok2addaVpath(e, n, g, g2, rate=rate): continue mask = addaVpath(g, e, n) r = nodesearch(g, g2, edges[1:], s) delaVpath(g, e, n, mask) elif bfu.undersample(g, rate) == g2: s.add(g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements in eqclass') return g # find all directed g1's not conflicting with g2 n = len(g2) edges = gk.edgelist(g2) random.shuffle(edges) g = cloneempty(g2) for e in edges: for n in ln: mask = addaVpath(g, e, n) if not gk.isedgesubset(bfu.undersample(g, rate), g2): single_cache[(n, e)] = False delaVpath(g, e, n, mask) s = set() try: nodesearch(g, g2, edges, s) except ValueError: s.add(0) return s