def get_subplot_x_y(density_of_gt_file,undersampling): d = zkl.load(density_of_gt_file) x = [] y = [] for i in range(0,100): g2 = bfutils.undersample(d[i]['gt'],undersampling) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][undersampling]['ms']) #add the time return x,y
def get_subplot_x_y(density_of_gt_file, undersampling): d = zkl.load(density_of_gt_file) x = [] y = [] for i in range(0, 100): g2 = bfutils.undersample(d[i]['gt'], undersampling) #this is H x.append(traversal.density(g2)) #add the density of H y.append(d[i]['solutions'][undersampling]['ms']) #add the time return x, y
def ra_wrapper(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),':', traversal.density(g2),':', startTime = int(round(time.time() * 1000)) #s = ur.liteqclass(g2, verbose=False, capsize=CAPSIZE) s = ur.eqclass(g2) 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 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 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 fan_wrapper(fold, n=10, k=10): scipy.random.seed() curr_proc = current_process() curr_proc.daemon = False output = Queue() while True: try: g = bfutils.ringmore(n, k) gdens = traversal.density(g) g2 = bfutils.increment_u(g, g) #g2 = bfutils.undersample(g,2) def inside_wrapper(): scipy.random.seed() try: startTime = int(round(time.time() * 1000)) s = traversal.v2g22g1(g2, capsize=CAPSIZE) #s = traversal.backtrack_more2(g2, rate=2, capsize=CAPSIZE) endTime = int(round(time.time() * 1000)) print "{:2}: {:8} : {:4} {:10} seconds".\ format(fold, round(gdens,3), len(s), round((endTime-startTime)/1000.,3)) output.put({'gt': g, 'eq': s, 'ms': endTime - startTime}) except MemoryError: print 'memory error...' raise pl = [Process(target=inside_wrapper) for x in range(INPNUM)] for e in pl: e.start() while True: if killall(pl): break r = output.get() except MemoryError: print 'memory error... retrying' for p in pl: p.terminate() p.join() continue break for p in pl: p.join() return r
def fan_wrapper(fold,n=10,k=10): scipy.random.seed() curr_proc=current_process() curr_proc.daemon=False output = Queue() while True: try: g = bfutils.ringmore(n,k) gdens = traversal.density(g) g2 = bfutils.increment_u(g,g) #g2 = bfutils.undersample(g,2) def inside_wrapper(): scipy.random.seed() try: startTime = int(round(time.time() * 1000)) s = traversal.v2g22g1(g2, capsize=CAPSIZE) #s = traversal.backtrack_more2(g2, rate=2, capsize=CAPSIZE) endTime = int(round(time.time() * 1000)) print "{:2}: {:8} : {:4} {:10} seconds".\ format(fold, round(gdens,3), len(s), round((endTime-startTime)/1000.,3)) output.put({'gt':g,'eq':s,'ms':endTime-startTime}) except MemoryError: print 'memory error...' raise pl = [Process(target=inside_wrapper) for x in range(INPNUM)] for e in pl: e.start() while True: if killall(pl): break r = output.get() except MemoryError: print 'memory error... retrying' for p in pl: p.terminate() p.join() continue break for p in pl: p.join() return r
def wrapper(fold, n=10, dens=0.1, urate=URATE): scipy.random.seed() rate = urate 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[:, ::rate], pval=0.0001) elif EST == 'svar': g2 = lm.data2graph(data[:, ::rate]) 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(bfu.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 }
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)) plt.xlim(0, 1) plt.show()
import sys sys.path.append('./tools/') import traversal, bfutils import numpy as np from ortools.constraint_solver import pywrapcp U = 3 # undersampling rate: 1 means no undersampling N = 4 # number of nodes k = 1 # number of extra edges solver = pywrapcp.Solver("MSL") # generate a random graph and undersample g = bfutils.ringmore(N,k) gdens = traversal.density(g) g2 = bfutils.undersample(g,U-1) # undersampled edges dedgeu = {} bedgeu = {} for i in range(N): for j in range(N): dedgeu[(i,j)] = 0 bedgeu[(i,j)] = 0 v = str(i+1) w = str(j+1) if w in g2[v]: if (0,1) in g2[v][w]: dedgeu[(i,j)] = 1 if (2,0) in g2[v][w]: bedgeu[(i,j)] = 1
def wrapper(fold,n=10,dens=0.1, urate=URATE): scipy.random.seed() rate = urate 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[:,::rate], pval=0.0001) elif EST=='svar': g2 = lm.data2graph(data[:,::rate]) 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(bfu.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}
import sys sys.path.append('./tools/') import traversal, bfutils import numpy as np from ortools.constraint_solver import pywrapcp U = 2 # undersampling rate: 1 means no undersampling N = 10 # number of nodes k = 25 # number of extra edges solver = pywrapcp.Solver("MSL") # generate a random graph and undersample g = bfutils.ringmore(N,k) gdens = traversal.density(g) g2 = bfutils.undersample(g,U-1) # undersampled edges dedgeu = {} bedgeu = {} for i in range(N): for j in range(N): dedgeu[(i,j)] = 0 bedgeu[(i,j)] = 0 v = str(i+1) w = str(j+1) if w in g2[v]: if (0,1) in g2[v][w]: dedgeu[(i,j)] = 1 if (2,0) in g2[v][w]: bedgeu[(i,j)] = 1