Ejemplo n.º 1
0
def randH(n, d1, d2):
    g = gk.ringmore(n, d1)
    pairs = [x for x in itertools.combinations(g.keys(), 2)]
    for p in np.random.permutation(pairs)[:d2]:
        g[p[0]].setdefault(p[1], set()).add((2, 0))
        g[p[1]].setdefault(p[0], set()).add((2, 0))
    return g
def fan_wrapper(fold='None', n=10, dens=.2, sst=.9):
    scipy.random.seed()
    msl_time = None
    sat_time = None
    s = None
    c = None
    k = bfutils.dens2edgenum(dens, n=nodes)
    output = {}
    while True:
        try:
            output['gt'] = gk.ringmore(n, k)
            gdens = traversal.density(output['gt'])
            gu = bfutils.increment(output['gt'])
            output['transition'] = lm.transitionMatrix4(output['gt'],
                                                        minstrength=.9)
            if estimate_gu:
                data = lm.drawsamplesLG(output['transition'],
                                        nstd=np.double(NOISE_STD),
                                        samples=BURNIN + NSAMPLES * URATE)
                data = data[:, BURNIN:]
                if np.max(data) > 1000.:
                    raise NameError()
                gu_est, gu_time = gu_estimate(data[:, ::URATE], ALG)
            else:
                gu_est = None
                gu_time = None
            if estimate_g1:
                try:
                    s, msl_time = msl_caller(gu_est)
                except TimeoutError:
                    s = None
                    msl_time = None
                try:
                    c, sat_time = sat_caller(gu_est, fold)
                except TimeoutError:
                    c = None
                    sat_time = None
                if msl_time is not None:
                    print "msl: {:2}: {:8} : {:4}  {:10} seconds".\
                        format(fold, round(sst,3), len(s),
                           round(msl_time/1000.,3))
                if sat_time is not None:
                    print "sat: {:2}: {:8} : {:4}  {:10} seconds".\
                        format(fold, round(sst,3), len(c),
                           round(sat_time/1000.,3))
            else:
                s = None
                c = None
                msl_time = None
                sat_time = None
            output['gu'] = {'est': gu_est, 'truth': gu, 'ms': gu_time}
            output['MSL'] = {'eq': s, 'ms': msl_time}
            output['SAT'] = {'eq': c, 'ms': sat_time}
        except MemoryError:
            print 'memory error... retrying'
            continue
        break
    return output
Ejemplo n.º 3
0
def checker(n, ee):
    g = gk.ringmore(n, ee)
    g2 = bfu.increment(g)
    d = checkable(g2)
    t = [len(d[x]) for x in d]
    r = []
    n = len(g2)
    ee = len(gk.edgelist(g2))
    for i in range(1, len(t)):
        r.append(sum(np.log10(t[:i])) - ee * np.log10(n))
    return r
def fan_wrapper(fold='None', n=10, dens=.2, sst=.9, card=2):
    scipy.random.seed()
    msl_time = None
    sat_time = None
    s = None
    c = None
    k = bfutils.dens2edgenum(dens, n=nodes)
    output = {}
    while True:
        try:
            output['gt'] = gk.ringmore(n, k)
            gdens = traversal.density(output['gt'])
            gu = bfutils.increment(output['gt'])
            if estimate_gu:
                data = dg.ConstructDynBN(output['gt'], [card] * n, sst,
                                         BURNIN + NSAMPLES * URATE)
                data = data[:, BURNIN:]
                gu_est, gu_time = gu_estimate(data[:, ::URATE], ALG)
            else:
                gu_est = None
                gu_time = None
            if estimate_g1:
                try:
                    s, msl_time = msl_caller(gu_est)
                except TimeoutError:
                    s = None
                    msl_time = None
                try:
                    c, sat_time = sat_caller(gu_est, fold)
                except TimeoutError:
                    c = None
                    sat_time = None
                if msl_time is not None:
                    print "msl: {:2}: {:8} : {:4}  {:10} seconds".\
                        format(fold, round(sst,3), len(s),
                           round(msl_time/1000.,3))
                if sat_time is not None:
                    print "sat: {:2}: {:8} : {:4}  {:10} seconds".\
                        format(fold, round(sst,3), len(c),
                           round(sat_time/1000.,3))
            else:
                s = None
                c = None
                msl_time = None
                sat_time = None
            output['gu'] = {'est': gu_est, 'truth': gu, 'ms': gu_time}
            output['MSL'] = {'eq': s, 'ms': msl_time}
            output['SAT'] = {'eq': c, 'ms': sat_time}
        except MemoryError:
            print 'memory error... retrying'
            continue
        break
    return output
Ejemplo n.º 5
0
def checkerDS(n, ee):
    g = gk.ringmore(n, ee)
    g2 = bfu.increment(g)
    gg = checkable(g2)
    d, p, idx = conformanceDS(g2, gg, gg.keys())
    t = [len(x) for x in p]
    r = []
    n = len(g2)
    ee = len(gk.edgelist(g2))
    for i in range(1, len(t)):
        r.append(sum(np.log10(t[:i])) - ee * np.log10(n))
    return r
Ejemplo n.º 6
0
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 = gk.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
Ejemplo n.º 7
0
def getAring(n, density=0.1, st=0.5, verbose=True, dist='flatsigned'):
    keeptrying = True
    plusedges = bfutils.dens2edgenum(density, n)
    while keeptrying:
        G = gk.ringmore(n, plusedges)
        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))}
Ejemplo n.º 8
0
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}