Exemplo n.º 1
0
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
Exemplo n.º 2
0
def compat(G):
    n = len(G)
    num = g2num(G)
    # sample all the graph for gStar
    star_l = all_undersamples(G)
    hits = {}
    # brute force all graphs
    for i in range(0, 2 ** (n**2)):
        tmpG = num2CG(i, n)
        tmp_l = all_undersamples(tmpG)
        c = compatible(tmp_l, star_l)
        if len(sum(c)) > 0:
            hits[i] = c
    return hits
Exemplo n.º 3
0
def compatibleAtU(uGstar):
    compat = []
    n = len(uGstar)
    numG = 2 ** (n ** 2)
    # pbar = Percentage()
    for i in range(1, numG):
        G = num2CG(i, n)
        # pbar.update(i+1)
        if len(ecj.scc(G)) > 1:
            continue
        l = searchMatch(uGstar, G, iter=5)
        if l:
            compat.append((l, G))
    # pbar.finish()
    return compat
Exemplo n.º 4
0
def cc_all(i, nodes, steps):
    # print i
    g = num2CG(i, nodes)
    return cc_undersamples(g, steps=steps)
Exemplo n.º 5
0
def iall(i, nodes):
    print i
    g = num2CG(i, nodes)
    return compact_call_undersamples(g)
Exemplo n.º 6
0
def ilength(i, nodes):
    print i
    g = num2CG(i, nodes)
    return len(call_undersamples(g))
Exemplo n.º 7
0
def icompat(i, nodes):
    print i
    g = num2CG(i, nodes)
    return compat(g)
Exemplo n.º 8
0
def randSCC(n):
    G = num2CG(scipy.random.randint(2 ** (n ** 2)), n)
    while (len(ecj.scc(G)) > 1) or gcd4scc(G) > 1:
        G = num2CG(scipy.random.randint(2 ** (n ** 2)), n)
    return G
Exemplo n.º 9
0
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}