def edgemask(gl, H, cds): nl = len(gl) ss = set() mask = np.zeros((nl, nl), 'int') np.fill_diagonal(mask, -1) idx = np.triu_indices(nl, 1) for i, j in zip(idx[0], idx[1]): if gl[i] & gl[j]: mask[i, j] = -1 mask[j, i] = -1 continue if skip_conflict(gl[i], gl[j], cds): gnum = gl[i] | gl[j] cache[gnum] = False continue gnum = gl[i] | gl[j] if gnum in cache: if cache[gnum]: mask[i, j] = gnum mask[j, i] = gnum else: cache[gnum] = False g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): ss.add(gnum) mask[i, j] = gnum mask[j, i] = gnum cache[gnum] = True return mask, ss
def edgemask(gl, H, cds): """given a list of encoded graphs and observed undersampled graph H returns a matrix with -1 on diagonal, 0 at the conflicting graph combination and encoded graph at non-conflicted positions. Furthermore, returns a set of graphs that are in the equivalence class of H Arguments: - `gl`: list of integer encoded graphs - `H`: the observed undersampled graph """ n = len(H) nl = len(gl) s = set() mask = np.zeros((nl, nl), 'int') np.fill_diagonal(mask, -1) for i in xrange(nl): for j in xrange(i + 1, nl): if gl[i] & gl[j]: continue if skip_conflict(gl[i], gl[j], cds): continue gnum = gl[i] | gl[j] g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): s.add(gnum) mask[i, j] = gnum mask[j, i] = gnum return mask, s
def edgemask(gl, H, cds): nl= len(gl) ss = set() mask = np.zeros((nl,nl),'int') np.fill_diagonal(mask,-1) idx = np.triu_indices(nl,1) for i,j in zip(idx[0], idx[1]): if gl[i] & gl[j]: mask[i,j] = -1 mask[j,i] = -1 continue if skip_conflict(gl[i], gl[j], cds): gnum = gl[i] | gl[j] cache[gnum] = False continue gnum = gl[i] | gl[j] if gnum in cache: if cache[gnum]: mask[i,j] = gnum mask[j,i] = gnum else: cache[gnum] = False g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): ss.add(gnum) mask[i,j] = gnum mask[j,i] = gnum cache[gnum] = True return mask, ss
def ledgemask(gl,H, cds): """given a list of encoded graphs and observed undersampled graph H returns a matrix with -1 on diagonal, 0 at the conflicting graph combination and encoded graph at non-conflicted positions. Furthermore, returns a set of graphs that are in the equivalence class of H Arguments: - `gl`: list of integer encoded graphs - `H`: the observed undersampled graph """ n = len(H) nl= len(gl) s = set() mask = np.zeros((nl,nl),'int') np.fill_diagonal(mask,-1) for i in xrange(nl): for j in xrange(i+1,nl): if gl[i] & gl[j]: continue gnum = gl[i] | gl[j] if skip_conflictors(gnum, cds): continue g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): s.add(gnum) mask[i,j] = gnum mask[j,i] = gnum return mask, s
def addedges(g, H, edges): if edges: nedges = prune_conflicts(H, g, edges) n = len(nedges) if n == 0: return None for i in range(n): gk.addanedge(g, nedges[i]) if bfu.call_u_equals(g, H): s.add(bfu.g2num(g)) s.add(addedges(g, H, nedges[:i] + nedges[i + 1:])) gk.delanedge(g, nedges[i])
def addedges(g,H,edges): if edges: nedges = prune_conflicts(H, g, edges) n = len(nedges) if n == 0: return None for i in range(n): gk.addanedge(g,nedges[i]) if bfu.call_u_equals(g, H): s.add(bfu.g2num(g)) addedges(g,H,nedges[:i]+nedges[i+1:]) gk.delanedge(g,nedges[i])
def prune(gl, H): l = [] ss = set() for gnum in gl: if gnum in cache: if cache[gnum]: l.append(gnum) else: cache[gnum] = False g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): ss.add(gnum) l.append(gnum) cache[gnum] = True return l
def prune(gl, H): l = [] ss = set() for gnum in gl: if gnum in cache: if cache[gnum]: l.append(gnum) else: cache[gnum] = False g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): ss.add(gnum) l.append(gnum) cache[gnum] = True return l
def add2set_(ds, H, cp, ccf, iter=1, verbose=True, capsize=100): n = len(H) n2 = n * n + n dsr = {} s = set() ss = set() pbar = start_progress_bar(iter, len(ds), verbose=verbose) c = 0 for gnum in ds: g = bfu.num2CG(gnum, n) gnum = bfu.g2num(g) c += 1 pbar.update(c) glist = [] elist = [] eset = set() for e in ds[gnum]: if not e[1] in g[e[0]]: gk.addanedge(g, e) num = bfu.g2num(g) ekey = (1 << (n2 - int(e[0], 10) * n - int(e[1], 10))) if ekey in ccf and skip_conflictors(num, ccf[ekey]): gk.delanedge(g, e) continue if not num in s: if not bfu.call_u_conflicts(g, H): #cf, gl2 = bfu.call_u_conflicts2(g, H) #if not cf: glist.append((num, ekey)) elist.append(e) eset.add(ekey) s.add(num) if bfu.call_u_equals(g, H): ss.add(num) if capsize <= len(ss): break #if bfu.call_u_equals2(g, gl2, H): ss.add(num) gk.delanedge(g, e) for gn, e in glist: if e in cp: dsr[gn] = [ekey2e(k, n) for k in eset - cp[e]] else: dsr[gn] = elist if capsize <= len(ss): return dsr, ss pbar.finish() return dsr, ss
def add2set_(ds, H, cp, ccf, iter=1, verbose=True, capsize=100): n = len(H) n2 = n*n +n dsr = {} s = set() ss = set() pbar = start_progress_bar(iter, len(ds), verbose = verbose) c = 0 for gnum in ds: g = bfu.num2CG(gnum, n) c += 1 pbar.update(c) glist = [] elist = [] eset = set() for e in ds[gnum]: if not e[1] in g[e[0]]: gk.addanedge(g,e) num = bfu.g2num(g) ekey = (1<<(n2 - int(e[0],10)*n - int(e[1],10))) if ekey in ccf and skip_conflictors(num,ccf[ekey]): gk.delanedge(g,e) continue if not num in s: s.add(num) if not bfu.call_u_conflicts(g, H): #cf, gl2 = bfu.call_u_conflicts2(g, H) #if not cf: glist.append((num,ekey)) elist.append(e) eset.add(ekey) if bfu.call_u_equals(g, H): ss.add(num) #if bfu.call_u_equals2(g, gl2, H): ss.add(num) if capsize <= len(ss): break gk.delanedge(g,e) for gn,e in glist: if e in cp: dsr[gn] = [ekey2e(k,n) for k in eset - cp[e]] else: dsr[gn] = elist if capsize <= len(ss): return dsr, ss pbar.finish() return dsr, ss
def patchmerge(ds, H, cds): n = len(H) l = set() s = set() o = set() for gkey in ds: for num in ds[gkey]: if gkey & num: continue gnum = gkey | num if gnum is s: continue if skip_conflictors(gnum, cds): continue g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): l.add(gnum) if bfu.call_u_equals(g, H): s.add(gnum) elif not gnum in o and bfu.overshoot(g, H): o.add(gnum) return l, s, o
def patchmerge(ds, H, cds): n = len(H) l = set() s = set() o = set() for gkey in ds: for num in ds[gkey]: if gkey & num: continue gnum = gkey | num if gnum is s: continue if skip_conflictors(gnum, cds): continue g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): l.add(gnum) if bfu.call_u_equals(g, H): s.add(gnum) elif not gnum in o and bfu.overshoot(g, H): o.add(gnum) return l, s, o
def edgemask2(gl,H, cds): n = len(H) nl= len(gl) s = set() o = set() mask = np.zeros((nl,nl),'int') np.fill_diagonal(mask,-1) for i in xrange(nl): for j in xrange(i+1,nl): if gl[i] & gl[j]: continue gnum = gl[i] | gl[j] if skip_conflictors(gnum, cds): continue g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): s.add(gnum) mask[i,j] = gnum mask[j,i] = gnum elif bfu.overshoot(g, H): o.add(gnum) return mask, s, o # mask, found eqc members, overshoots
def edgemask2(gl, H, cds): n = len(H) nl = len(gl) s = set() o = set() mask = np.zeros((nl, nl), 'int') np.fill_diagonal(mask, -1) for i in xrange(nl): for j in xrange(i + 1, nl): if gl[i] & gl[j]: continue gnum = gl[i] | gl[j] if skip_conflictors(gnum, cds): continue g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): if bfu.call_u_equals(g, H): s.add(gnum) mask[i, j] = gnum mask[j, i] = gnum elif bfu.overshoot(g, H): o.add(gnum) return mask, s, o # mask, found eqc members, overshoots
def quadmerge_(glist, H, ds): n = len(H) gl = set() ss = set() conflicts = set() for gi in combinations(glist, 2): if gi[0] & gi[1]: continue #if skip_conflict(gi[0], gi[1], ds): continue gnum = gi[0] | gi[1] if gnum in conflicts: continue if skip_conflictors(gnum, ds): conflicts.add(gnum) continue if gnum in gl: continue g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): gl.add(gnum) if bfu.call_u_equals(g, H): ss.add(gnum) else: conflicts.add(gnum) return gl, ss
def quadmerge_(glist, H, ds): n = len(H) gl = set() ss = set() conflicts = set() for gi in combinations(glist, 2): if gi[0] & gi[1]: continue #if skip_conflict(gi[0], gi[1], ds): continue gnum = gi[0] | gi[1] if gnum in conflicts: continue if skip_conflictors(gnum, ds): conflicts.add(gnum) continue if gnum in gl: continue g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): gl.add(gnum) if bfu.call_u_equals(g, H): ss.add(gnum) else: conflicts.add(gnum) return gl, ss
def add2set_loop(ds, H, cp, ccf, iter=1, verbose=True, capsize=100, currsize=0): n = len(H) n2 = n*n +n dsr = {} s = set() ss = set() pbar = start_progress_bar(iter, len(ds), verbose = verbose) c = 0 for gnum in ds: c += 1 pbar.update(c) gset = set() eset = set() for sloop in ds[gnum]: if sloop & gnum == sloop: continue num = sloop | gnum if sloop in ccf and skip_conflictors(num,ccf[sloop]):continue if not num in s: g = bfu.num2CG(num, n) if not bfu.call_u_conflicts(g, H): s.add(num) gset.add((num,sloop)) eset.add(sloop) if bfu.call_u_equals(g, H): ss.add(num) if capsize <= len(ss)+currsize: return dsr, ss #for gn,e in gset: # if e in cp: # dsr[gn] = eset - cp[e] - set([e]) # else: # dsr[gn] = eset - set([e]) for gn in gset: dsr[gn[0]] = eset - set([gn[1]]) pbar.finish() return dsr, ss
def lquadmerge(gl, H, cds): n = len(H) l = set() s = set() mask, ss = ledgemask(gl, H, cds) s = s | ss ds = edgeds(mask) #pp = pprint.PrettyPrinter(indent=1) #pp.pprint(ds) for idx in ds: for gn in ds[idx]: if mask[idx]&gn: continue if skip_conflictors(mask[idx], gn, cds): continue gnum = mask[idx] | gn if gnum in l or gnum in ss: continue g = bfu.num2CG(gnum,n) if not bfu.call_u_conflicts(g, H): l.add(gnum) if bfu.call_u_equals(g, H): s.add(gnum) return list(l), s
def quadmerge(gl, H, cds): n = len(H) l = set() s = set() mask, ss = edgemask(gl, H, cds) s = s | ss ds = edgeds(mask) #pp = pprint.PrettyPrinter(indent=1) #pp.pprint(ds) for idx in ds: for gn in ds[idx]: if mask[idx] & gn: continue if skip_conflict(mask[idx], gn, cds): continue gnum = mask[idx] | gn if gnum in l or gnum in ss: continue g = bfu.num2CG(gnum, n) if not bfu.call_u_conflicts(g, H): l.add(gnum) if bfu.call_u_equals(g, H): s.add(gnum) return list(l), s