Exemple #1
0
        t = line[:-1].split(delim)
        nof_cols = len(t)
        if not nof_cols0: nof_cols0 = nof_cols
        if nof_cols != nof_cols0:
            return False, 0, 0, msg(lineno, nof_cols0, nof_cols, t)

    return True, lineno + 1 - nof_empty, nof_cols0, ""


# And if so (and even if not) makes the result tab limited


def main(infn, delim=None, keep_empty=False):

    if delim == '\\t': delim = '\t'

    ok, nof_rows, nof_cols, msg = formatcheck(infn, delim, keep_empty)

    print int(ok)
    print ok and ("%d %d" % (nof_rows, nof_cols)) or msg


if __name__ == "__main__":
    from coliche import che
    che(
        main, """datafile
        -d --delim delim : field separator (default whitespaces)
        -k --keep-empty (bool) : don't skip empty lines
        """)
Exemple #2
0
import propreg

import sys
def main(N, H0):
    mlines = ("I %s %s" % (H0, H0),
              "O %s %s" % (H0, H0))
    h0 = float(H0)
    for f1 in xrange(N+1):
        p = float(f1)/N
        f0 = N-f1
        p0, p1 = propreg.proptest((f0, f1), mlines)
        print p, p0

if __name__ == '__main__':
    from coliche import che

    che(main,
        """ N (int); H0;
        """)
Exemple #3
0
if __name__ == '__main__':
    import coliche
    import bnsearch
    import constraints

    def main(bdtfile,
             scoretype='BDeu',
             ess=1.0,
             outfile=None,
             constraint_file="",
             cachefile=None):
        bn, sc = bnsearch.empty_net(bdtfile,
                                    scoretype,
                                    ess,
                                    cachefile=cachefile)
        cstrs = constraints.Constraints(constraint_file)
        kruskal(bn, sc, cstrs)
        if outfile:
            bn.save(outfile)
        sc.score_new(bn)
        print sc.score()

    coliche.che(
        main, ''' bdtfile;
                -g --goodness scoretype BDeu|fNML|AIC|BIC : default: BDeu
                -e --ess ess (float) : default 1.0
                -c constraint_file : a file with arcs marked with (+) or -
                -o outfile : file to save the model found
                -m --cachefile cachefile: local scores
                ''')
Exemple #4
0
                    bn.delarc(a)
                    #print 'ADEL', bn.arcs()
                    #for v in bn.vars(): print v, bn.path_in_counts[v]

        sc.score_new(bn)

        if (iters and t > iters): break
        if 'SIGUSR2' in sigpool.flags: break
        if 'SIGUSR1' in sigpool.flags:
            if outfile: good_nets[-1][1].save(outfile)
            print good_nets[-1][0]
            sigpool.flags.remove('SIGUSR1')

    if outfile:
        good_nets[-1][1].save(outfile)

    print good_nets[-1][0]


coliche.che(
    main, ''' bdtfile;
                -g --goodness scoretype BDeu|fNML|AIC|BIC : default: BDeu
                -e --ess ess (float) : default 1.0
                -t time : like -t '4d 3h 5m 5[s]' (default till SIGUSR2)
                -i --iters iters (int) :number of iterations (default infinite)
                -c constraint_file : a file with arcs marked with + or -
                -o outfile : file to save the model found
                -s --start startbn : file to start search from
                -m --cachefile cachefile: local scores
                ''')
Exemple #5
0
#!/usr/bin/env python

from bn.bn import load as load_bn
from bn.learn.constraints import Constraints

import coliche
from itertools import combinations


def gen_forbidden_arcs(bn):

    sarcs = frozenset(tuple(arc) for arc in map(sorted, bn.arcs()))

    for arc in combinations(bn.vars(), 2):
        if not arc in sarcs:
            yield arc
            yield tuple(reversed(arc))


def main(bnfile, cstrfile):
    bn = load_bn(bnfile)
    cstrs = Constraints()
    for arc in gen_forbidden_arcs(bn):
        cstrs.no.add(arc)
    cstrs.save(cstrfile)


if __name__ == '__main__':
    coliche.che(main, 'bnfile; cstrfile')
Exemple #6
0
        l = f.readline()

    self.thetas = thetas
    f.close()
    return self


def bnt(bnm):  # strangely needed for inout
    return bnm.bn


# maybe learning should be put to another file
def main(vdfile, bnfile, datfile, outfile, ess=1.0, type="bdeu"):
    valcs = bn.vd.load(vdfile)
    bns = bn.bn.load(bnfile, do_pic=False)
    dat = disdat.RowData(vdfile, datfile)
    bnm = BnModel(bns, valcs)
    bnm.init_param_learning(valcs.vcs, type.lower(), ess)
    bnm.learn_params(dat)
    bnm.save(valcs.varnames, outfile)


if __name__ == '__main__':
    from coliche import che
    che(
        main, ''' vdfile; bnfile; datfile; outfile
        -t --type type bdeu|cnml|dir|ml|bds|bdq : bdeu, bds, bdq, ml, cnml or dir: default bdeu
        -e --ess ess (float) : a parameter if needed: default 1.0
        ''')
Exemple #7
0
        print('Unknown inferer type:', irtype)
        return

    while True:
        l = raw_input().strip()
        if not l: continue
        if l.startswith("q"): break

        argv = l.split()
        c = argv.pop(0)

        if c == 'e':
            r = ir.evidence(argv)
        elif c == 'w':
            r = ir.watch(argv)
        elif c == 'r':
            r = ir.reset()
        elif c == 'i':
            r = ir.infer()
        else:
            r = ir.nocmd()
        if r: print "ERROR:", r


coliche.che(
    main, """irtype
  --ifrdir ifrdir : if irtype is jt
  -f --fmt vdfile : needed but for jt
  --bnmodel bnmfile : if irtype is gibbs or bucketelim or lhweighting
  """)
Exemple #8
0
    bn = load(bnfile)
    stages = []
    varsleft = set(bn.vars())
    while len(varsleft) > 0:
        sources = set(v for v in varsleft if len(bn.parents(v)) == 0)
        stages.append(sources)
        varsleft -= sources
        for v in sources:
            for c in bn.children(v):
                bn.delarc((v, c))

    return stages


def fac(n):
    r = 1
    for x in xrange(1, n + 1):
        r *= x
    return r


import coliche
stages = coliche.che(main, 'bnfile')
print 'THIS DOES NOT WORK'
print stages
if stages != None:
    res = 1
    for s in stages:
        res *= fac(len(s))
    print res
Exemple #9
0
            # print "Rs[n-r]",Rs[n-r]
                
            yield bnt * Rs[r]
                
    return sum(rts())

def Rs(N,K):
    l = list(xlogs(N+1))
    lf = list(xlogfacs(l))

    
    Rs = [1]*(N+1)
    yield Rs
    for k in xrange(2,K+1):
        Rs = [R(k,n,Rs,l,lf) for n in xrange(N+1)]
        yield Rs

if __name__ == '__main__':
    import coliche

    def main(N,K,filename):
        f = open(filename,"w")
        for rk in Rs(N,K):
            #print >>f, " ".join(["%8.2f" % r for r in rk])    
            print >>f, " ".join(map(str, rk))    
        f.close()

    coliche.che(main, "N (int); K (int); filename")


Exemple #10
0
           'task_index' : '--task-index %d' % task_index,
           'cstr'       : '-c %s' % cstrfile if cstrfile != None else '',
           'ext'        : '.exe' if sys.platform == 'win32' else ''
}

    scorecmd = """
{binpath/}get_local_scores{ext} {vdfile} {datfile} {score} {rdir/}res {nof_tasks} {task_index} {cstr}
{binpath/}split_local_scores{ext}   {nof_vars} {rdir}
{binpath/}reverse_local_scores{ext} {nof_vars} {rdir}
{binpath/}get_best_parents{ext}     {nof_vars} {rdir}
{binpath/}get_best_sinks{ext}       {nof_vars} {rdir} {rdir/}sinks
{binpath/}get_best_order{ext}       {nof_vars} {rdir}/sinks {rdir/}ord
{binpath/}get_best_net{ext}         {nof_vars} {rdir} {rdir/}ord {rdir/}net
{binpath/}score_net{ext}            {rdir/}net {rdir}
""".format(**kws) 
        
    if os.path.exists(resdir):
        if not os.path.isdir(resdir):
            sys.exit('%s is not a directory' % resir)
    else:
        os.makedirs(resdir)
    subprocess.call(scorecmd, shell=True)

from coliche import che
che(main,
    """vdfile;datfile;score;resdir; 
      --nof-tasks nof_tasks(int): default: 1
      --task-index task_index: default: 0
      -c --constraints cstrfile
""")
Exemple #11
0
         topN=0,
         dump_worklist=False):

    cstrs = Constraints(constraint_file)
    bn0 = bnmodule.load(bnfile)
    sc = scorefactory.getscorer(data.Data(bdtfile),
                                scoretype,
                                ess,
                                cachefile=cachefile)
    c = 0
    for (_bnas, s) in hpcrawl([bn0],
                              sc,
                              cstrs=cstrs,
                              topN=topN,
                              dump_worklist=dump_worklist):
        print c, s
        # if c == 20000:
        #     bnmodule.BN(bn0.varc,_bnas,do_pic=False).save('X%d.bn' % c)
        c += 1


coliche.che(
    main, """bnfile; bdtfile; 
                -g --goodness scoretype BDeu|fNML|AIC|BIC : default: BDeu
                -e --ess ess (float) : default 1.0
                -c constraint_file : a file with arcs marked with + or -
                -m --cachefile cachefile: local scores
                -d --dump_worklist (bool): default: False
                -t --top topN (int): default: 0 - all
                """)
Exemple #12
0
#!/usr/bin/env python
import bnsearch


def main(bdtfile, scoretype='BDeu', ess=1.0, outfile=None, cachefile=None):

    bn, sc = bnsearch.empty_net(bdtfile, scoretype, ess, cachefile=cachefile)

    if outfile:
        bn.save(outfile)

    print sc.score()


if __name__ == '__main__':

    import coliche

    coliche.che(
        main, ''' bdtfile;
                -g --goodness scoretype BDeu|fNML|AIC|BIC : default: BDeu
                -e --ess ess (float) : default 1.0
                -o outfile : file to save the model found
                -m --cachefile cachefile: local scores
''')
Exemple #13
0
        nof_vars = bn0.varc
        sc = scorefactory.getscorer(bn.learn.data.Data(bdtfile), scoretype,
                                    ess)

        elite = [(-sys.float_info.max, None)] * topN
        for bne in eqvnets(bn0):
            sc.score_new(bne)
            for s, (bnw, seq) in nsearch.nsearch(bne, sc, level, topN, cstrs):
                if bnw.arcs() not in elite:
                    # print "in", bnw.arcs()
                    weakest = heappushpop(elite, (s, bnw.arcs()))
                    # print "out", weakest

        elite.sort(reverse=True)

        if not os.path.exists(resdir): os.mkdir(resdir)
        i = 0
        for (s, bwe) in elite:
            if bwe != None:
                bn.bn.BN(nof_vars, bwe,
                         False).save(os.path.join(resdir, '%d.bn' % i))
                i += 1
                print s

    coliche.che(
        main, """bnfile; bdtfile; level (int); topN (int); resdir
    -g --goodness scoretype BDeu|fNML|AIC|BIC : default: BDeu
    -e --ess ess (float) : default 1.0
    -c constraint_file : a file with arcs marked with + or -
    """)
Exemple #14
0
import propreg

import sys
def main(N, p, nof_points=50, nof_reps=10000):
    h0 = float(H0)
    for f1 in xrange(N+1):
        f0 = N-f1
        pf1 = C(N,f1)*(p**f1)(1-p)**f0

        for 
        mlines = ("I %s %s" % (, H0), "O %s %s" % (H0, H0))
        p0, p1 = propreg.proptest((f0, f1), mlines)
        print p, p0

if __name__ == '__main__':
    from coliche import che
    
    che(main,
        """ N (int); p;
        """)
Exemple #15
0
#!/usr/bin/env python

comb = []
def calc_combs(n):
	for k in xrange(n+1):
		comb.append([1])
		for j in xrange(1,k):
			comb[k].append(comb[k-1][j-1] + comb[k-1][j])
		if k> 0: comb[k].append(1)

def b(N):
	B=[]
	B.append(1)
	for n in xrange(1,N+1):
		B.append(0)
		for k in xrange(1,n+1):
			B[-1]+=(-1)**(k+1)*comb[n][k]*2**(k*(n-k))*B[n-k]
	return B[-1]

import sys, math, coliche

def main(n):
    calc_combs(n)
    nof = b(n)
    print(nof, math.log(nof))

coliche.che(main,"n (int)")
Exemple #16
0
-t --tab tabstring : convert tabs to (default:) 8 spaces
-k --keep-empty    : consider lines with whitespace only as proper
-n --names names   : "GEN", "ROW", "COL" or filename,  default "ROW"
-s --start start (int) : if -n GEN : default 0 -> V0, V1, ..
-m --miss missmark : same as -c " .. :: miss miss-mark" : default ""
"""

if __name__ == "__main__":
    import varset, varcmd
    from coliche import che

    din_strusage = ("""
    datafile; result_dir;""" + strusage + """
    -f --cmd-file cmd-file : read commands from file first
    -c --cmd commands      : separated by %s
    """ + varset.strusage + varcmd.strusage)

    r = che(din, din_strusage)

    resdir = che(lambda *ps, **ks: ps[1], din_strusage)
    print >> file(os.path.join(resdir, "din.cmd"), "w"), " ".join(sys.argv)

    ok = r[0]

    if ok:
        print 1
        print r[1], r[2]
    else:
        print 0
        print r[3]
Exemple #17
0
    return sum(xp * yp * sum(vrsims(x, y, pprobs, cfgextractors, bnm))
               for (x, xp), (y,
                             yp) in product(zip(xs, xprobs), zip(ys, yprobs)))


def main(vdfile, bnmfile, ifrdir, matrix=False):
    valcs = bn.vd.load(vdfile)
    bnm = bn.model.bnmodel.load(bnmfile, valcs)
    ifr = bn.infer.ifr.load(ifrdir)
    pfs = [build_pprobs(ifr, bnm, valcs, i) for i in bnm.bn.vars()]
    cfgxs = [build_cfgextractors(bnm.bn, i) for i in bnm.bn.vars()]
    dats = [map(int, l.split()) for l in sys.stdin]
    N = len(dats)
    combs = combinations_with_replacement(enumerate(dats), 2)
    if matrix:
        K = [[0.0] * N for i in range(N)]
        for ((i1, d1), (i2, d2)) in combs:
            K[i1][i2] = K[i2][i1] = sim(d1, d2, pfs, cfgxs, bnm)

        for i in range(N):
            print(' '.join(map(str, K[i])))
    else:
        for ((i1, d1), (i2, d2)) in combs:
            print(i1, i2, sim(d1, d2, pfs, cfgxs, bnm))


if __name__ == '__main__':
    from coliche import che
    che(main, """vdfile; bnmfile; ifrdir
    -m --matrix : print in matrix form""")
Exemple #18
0
#!/usr/bin/env python
import bnsearch
import coliche

def main(bdtfile, ess=1.0, outfile=None):

    bn, sc = bnsearch.empty_net(bdtfile, ess)

    if outfile:
        bn.save(outfile)

    print sc.score()

if __name__ == '__main__':
    
    coliche.che(main,
                ''' bdtfile;
                -e --ess ess (float) : default 1.0
                -o outfile : file to save the model found''')
    
Exemple #19
0
                raise Exception("Model cannot be out of [0,1]")
            io_p = out_p
        else:
            io_p = in_p
            
        
        lnom_io = lnom_p(io_p(a,b,f1,N),frqs)
        reg_io  = R_io(a,b, N, io_p)
        lreg_io = math.log(reg_io)

        # print io_p(a,b,f1,N), lnom_io, reg_io, lreg_io
        lprobs.append((lnom_io != None) and (lnom_io - lreg_io) or None)

    if len(lprobs)<2:
        raise Exception("At least 2 models required to select among them!")
        
    return logmath.lognorm(lprobs)
        
if __name__ == '__main__':
    import coliche

    def main(f0, f1, mfile):
            print proptest((f0, f1), file(mfile))
        
    coliche.che(main,
                """
                f0 (int)  : number of zeros
                f1 (int)  : number of ones (successes)
                mfile     : modelfile with lines "[I|O] lower_limit upper_limit"
                """)
Exemple #20
0
           repfile,
           commands=None,
           cmd_file=None,
           miss_mark="",
           **cmdsyntax):

    sx = varcmd.syntax(cmdsyntax)
    vns = vars.vars(vnfile)
    cmds = varcmd.prepend(sx, "miss " + miss_mark, commands)
    vscmds = varcmd.varscmds(vns, cmds, sx, cmd_file, ("miss", ))

    repf = repfile == "-" and sys.stdout or file(repfile, "w")

    for (vcmds, dl) in izip(vscmds, file(datfile)):
        acs = vcmds['miss']
        nof_mvals = sum(int(d.strip() in acs) for d in dl[:-1].split("\t"))
        print >> repf, nof_mvals


if __name__ == "__main__":
    from coliche import che
    che(
        misrep, """
        vnfile   : the file containing one variable name per row
        datfile  : the file containing one variable per row
        repfile  : reports number of missing values per variable
        -m --miss miss_mark : same as -c " .. :: miss miss-mark" : default ""
        -f --cmd-file cmd-file : read commands from file first
        -c --cmd commands : separated by %s 
        """ + varset.strusage + varcmd.strusage)
Exemple #21
0
           'task_index' : '--task-index %d' % task_index,
           'cstr'       : '-c %s' % cstrfile if cstrfile != None else '',
           'ext'        : '.exe' if sys.platform == 'win32' else ''
}

    scorecmd = """
{binpath/}get_local_scores{ext} {vdfile} {datfile} {score} {rdir/}res {nof_tasks} {task_index} {cstr}
{binpath/}split_local_scores{ext}   {nof_vars} {rdir}
{binpath/}reverse_local_scores{ext} {nof_vars} {rdir}
{binpath/}get_best_parents{ext}     {nof_vars} {rdir}
{binpath/}get_best_sinks{ext}       {nof_vars} {rdir} {rdir/}sinks
{binpath/}get_best_order{ext}       {nof_vars} {rdir}/sinks {rdir/}ord
{binpath/}get_best_net{ext}         {nof_vars} {rdir} {rdir/}ord {rdir/}net
{binpath/}score_net{ext}            {rdir/}net {rdir}
""".format(**kws) 
        
    if os.path.exists(resdir):
        if not os.path.isdir(resdir):
            sys.exit('%s is not a directory' % resir)
    else:
        os.makedirs(resdir)
    subprocess.call(scorecmd, shell=True)

from coliche import che
che(main,
    """vdfile;datfile;score;resdir; 
      --nof-tasks nof_tasks(int): default: 1
      --task-index task_index: default: 0
      -c --constraints cstrfile
""")
Exemple #22
0
        c_cread = self.libcdata.data_cread
        c_cread.restype = c_void_p

        self.libcdata.data_nof_vars.argtypes = [c_void_p]

        self.libcdata.data_nof_vals.argtypes = [c_void_p, c_int]

        open(filename).close() # throws exception if not working
        self.dt = c_cread(filename)

    def __del__(self):
        if self.dt:
            pass
            # libcdata.data_free(pointer(self.dt))

    def nof_vars(self):
        return self.libcdata.data_nof_vars(self.dt)

    def nof_vals(self, v):
        return self.libcdata.data_nof_vals(self.dt, v)

import coliche

def test(bdtfile):
    dt = Data(bdtfile)
    print dt.nof_vars(), [dt.nof_vals(v) for v in xrange(dt.nof_vars())]    
    

coliche.che(test,'bdtfile')
Exemple #23
0
#!/usr/bin/python

import genctb, deptest

if __name__ == '__main__':

    import sys, coliche

    def main(pfile, N):
        try:
            ps = [map(float, l.split()) for l in file(pfile)]
            normalizer = sum(map(sum,ps))
            ps = [[p/normalizer for p in r] for r in ps]
            r = [0,0]
            while True:
                ctb = genctb.genctb(ps,N)
                dt = deptest.deptest_ctb(ctb)
                r[dt.index(max(dt))] += 1
                print r, float(r[0])/sum(r)
        except Exception, msg:
            raise
            sys.exit("ERROR: "+ str(msg))
            
    coliche.che(main, "pfile; N (int)")
Exemple #24
0
def initial_search_status(bn, scr):
    curr_score = scr.score()
    return {"scr" : scr,
            "curr_bn" : bn,
            "curr_score" : curr_score,
            "best_bn" : bn.copy(),
            "best_score" : curr_score,
            "iters" : 0,
            "t_no_acts" : 0,
            "t_no_acps" : 0,
            "t_no_impr" : 0}

def localsearch(search_status, stepping, stopping):
    while True :
        stepping(search_status)
        if stopping(search_status) : break

def empty_net(bdtfile, ess):
    libdata = CDLL("/home/tsilande/projects/bn/learn_ctypes/data.so")
    data  = libdata.data_cread(bdtfile)
    bn = BN(libdata.nof_vars(data))
    scr = Score(bn, data, ess)
    return bn, scr

def main(bdtfile, ess):
    bn, scr = empty_net(bdtfile, ess)
    print scr.score(), bn.arcs()
    
if __name__ == '__main__':
    coliche.che(main, "bdtfile; ess (float)")
Exemple #25
0
from itertools import izip, count
import sys

import vars, varset, varcmd

def varsel(vnfile, datfile, vnfile_out, datfile_out,
           commands = '', cmd_file = None,
           **cmdsyntax):

    sx = varcmd.syntax(cmdsyntax)
    varnames = vars.vars(vnfile)
    vscmds   = varcmd.varscmds(varnames,commands,sx,cmd_file)
    vnf  = file(vnfile_out,"w")
    datf = file(datfile_out,"w")
    for (vl, dl, vcmds) in izip(file(vnfile), file(datfile), vscmds):
        if "in" in vcmds:
            vnf.write(vl)
            datf.write(dl)
    
if __name__ == "__main__":
    from coliche import che
    che(varsel,
        """
        vnfile   : the file containing one variable name per row
        datfile  : the file containing one variable per row
        vnfile-out
        datfile-out
        -f --cmd-file cmd-file : read commands from file first
        -c --cmd commands : separated by %s
        """ + varset.strusage + varcmd.strusage)
Exemple #26
0
        for qe_tbi in to_be_inserted:
            pri = -score + qe_tbi['delta'] + plty * qe_tbi['nof_arcs']
            heappush(hq, (pri, qe_tbi))

        if max_heap_size > 0 and len(hq) > max_heap_size:
            print "truncating hq"
            hq = hq[:min_heap_size + 1]

    print t, best_score, best_net.arcs()


def main(bdtfile,
         ess=1.0,
         max_heap_size=5000,
         min_heap_size=4000,
         max_iters=-1):
    data = Data(bdtfile)
    qlearn(data, ess, max_heap_size, min_heap_size, max_iters)


if __name__ == '__main__':
    import coliche
    coliche.che(
        main, """
                bdtfile : data in bdt-format
                -e --ess ess (float) : ESS (default 1.0)
                --max_heap_size max_heap_size (int) : give negative for infinite (default 5000)
                --min_heap_size min_heap_size (int) : (default 4000)
                --max_iters max_iters (int) : give negative for infinite (default -1)
                """)
Exemple #27
0
    cs = [{} for vn in varnames]
    for (vs, cmd, acs) in cmds:
        for v in vs:
            actions.get(cmd, def_action)(cs[v], cmd, acs)
    return cs


strusage = """
--cmd-sep   cmd-sep       : separator for commands: default "\%s"
--var-cmd-sep var-cmd-sep : token to separate vars and command: default "%s"
        """ % (cmd_sep, var_cmd_sep)

# JUST TO TEST

if __name__ == "__main__":

    from coliche import che

    def main(vnfile, commands="", cmd_file=None, **sx):
        sx = syntax(sx)
        vns = vars.vars(vnfile)
        for t in varscmds(vns, commands, sx, cmd_file):
            print t

    che(
        main, """
        vnfile          : one variable name per row 
        -f --cmd-file cmd-file : read commands from file first
        -c --cmd commands : separated by %s
        """ + strusage)
Exemple #28
0
        fromstart = l_to if no_within else l_to+1
            
        for l_from in xrange(fromstart, nof_levels):
            for (x,y) in product(xrange(nof_vars), xrange(nof_vars)):
                print >>cstrf, '-', x+l_from*nof_vars, y+l_to*nof_vars
    cstrf.close()

    # create data
    
    if datafile != None and outdatafile != None:
        dat = disdat.RowData(vdfile, datafile)
        ds = [dat.dats() for l in xrange(nof_levels)]
        for l in xrange(nof_levels): # skip initial l from ds[l]
            for ll in xrange(l): ds[l].next()

        datf = file(outdatafile,'w')
        for drows in izip(*ds):
            print >>datf, '\t'.join(map(str, chain(*drows)))
        datf.close()
    
if __name__ == '__main__':
    import coliche

    coliche.che(main,
                """vdfile; outvdfile; constraintfile
                -d datafile: datafile
                -o outdatafile: outdatafile
                -l --levels nof_levels (int) : default:2
                --no_within     : disallow arcs within timestep
""")
Exemple #29
0
#!/usr/bin/env python
# Check that all forsts have same score

if __name__ == '__main__':
    import coliche
    import bnsearch
    import bestforests

    def main(bdtfile, ess=1.0, outfile=None, constraint_file=""):
        bn, sc = bnsearch.empty_net(bdtfile, ess)
        bestforests.kruskal(bn, sc)
        sc.score_new(bn)
        print sc.score()
        for forest in bestforests.Forest(bn):
            sc.score_new(forest)
            print sc.score()

    coliche.che(
        main, ''' bdtfile;
                -e --ess ess (float) : default 1.0
                ''')
Exemple #30
0
                if f == t: continue

                if op == '+':
                    sf_must.add(t)
                    if t in sf_nope:
                        sf_nope.remove(t)
                elif op == '-':
                    sf_nope.add(t)
                    if t in sf_must:
                        sf_must.remove(t)
                elif op == '?':
                    if t in sf_must:
                        sf_must.remove(t)
                    if t in sf_nope:
                        sf_nope.remove(t)
                else:
                    print 'Whaaat?'

    for (fn, cs) in cstrs.iteritems():
        fi = names.index(fn)
        for t in cs[0]:
            print '+', fi, t
        for t in cs[1]:
            print '-', fi, t


if __name__ == "__main__":
    import coliche
    coliche.che(main, """vdfile
                """)
Exemple #31
0

def gen1(ord, bnm):
    bns = bnm.bn
    d = [0]*len(ord)
    for o in ord:
        d[o] = wheel(bnm.thetd(o,d))
    return d

def gen(bnm, N):
    ord = order(bnm.bn)
    for n in xrange(N):
        yield gen1(ord, bnm)


def main(vdfile, modelfile, N, outfile="-"):
    valcs = bn.vd.load(vdfile)
    bnm = bnmodel.load(modelfile, valcs)

    outf = outfile == "-" and sys.stdout or file(outfile, "w")
    for d in gen(bnm, N):
        print >> outf, " ".join(map(str, d))
    if outfile != "-": outf.close()

if __name__ == '__main__':
    from coliche import che
    che(main,
        '''vdfile; modelfile; N (int)
        -o outfile
        ''')
Exemple #32
0
import networkx as nx

def bn2dg(bn):
    dg = nx.DiGraph()
    dg.add_nodes_from(bn.vars())
    dg.add_edges_from(bn.arcs())
    return dg

def is_dag(bn):
    dg = bn2dg(bn)
    return nx.is_directed_acyclic_graph(dg)

if __name__ == '__main__':
    import coliche
    from bn.bn import load
    def main(bnfile):
        print 'DAG' if is_dag(load(bnfile, do_pic=False)) else 'not DAG'
    coliche.che(main, 'bnfile')
    
Exemple #33
0
    frqsXY = reduce(operator.__concat__, ctb, [])
    lpXY = lognml(frqsXY)

    return lpXY

def nml_gi(ctb):
	return math.exp(lognml_gi(ctb))
	
def nml_f(ctb):
	return math.exp(lognml_f(ctb))

def deptest_ctb(ctb):
    return logmath.lognorm([lognml_gi(ctb), lognml_f(ctb)])
    
def deptest(ctblines):
    return deptest_ctb(contab.get_ctb(ctblines))
        
if __name__ == '__main__':

    import sys, coliche

    def main(ctbfile):
        try:
            ps = deptest(file(ctbfile))
            print " ".join(["%.4f" % p for p in ps])
        except Exception, msg:
            sys.exit("ERROR: "+ str(msg))
            
    coliche.che(main, "ctbfile")
Exemple #34
0
    ifr = ifrload(ifrdir)

    gen_subsets.use_just_one_set = resdir is None
    parsetscores = [get_parsetscores(i, resdir, bnm) for i in bnm.bn.vars()]
    potss = [list(gen_subpots(valcs, bnm, ifr, i)) for i in bnm.bn.vars()]
    cfgextractors = [get_cfgextractors(i, bnm) for i in bnm.bn.vars()]

    dats = [map(int, l.split()) for l in sys.stdin]
    combs = combinations_with_replacement(enumerate(dats), 2)

    if matrix:
        K = [[0.0] * bnm.bn.varc for i in bnm.bn.vars()]
        for ((i1, d1), (i2, d2)) in combs:
            K[i1][i2] = K[i2][i1] = sim(d1, d2, cfgextractors, parsetscores,
                                        potss)

        for i in bnm.bn.vars():
            print(' '.join(map(str, K[i])))
    else:
        for ((i1, d1), (i2, d2)) in combs:
            print(i1, i2, sim(d1, d2, cfgextractors, parsetscores, potss))


if __name__ == '__main__':
    from coliche import che
    che(
        main, """vdfile; bnmfile; ifrdir
    -r --resdir resdir: directory to get the subsetscores
    -c --cix cix (int) : class index counting from 0 : default None
    -m --matrix : print in matrix form""")
Exemple #35
0
#!/usr/bin/env python
import bn.bn, data, scorefactory
import coliche


def main(bnfile, bdtfile, scoretype='BDeu', ess=1.0, cachefile=None):

    bns = bn.bn.load(bnfile, do_pic=False)
    bdt = data.Data(bdtfile)
    sc = scorefactory.getscorer(bdt, scoretype, ess, cachefile=cachefile)
    sc.score_new(bns)
    print sc.score()


if __name__ == '__main__':

    coliche.che(
        main, ''' bnfile; bdtfile
                -g --goodness scoretype BDeu|fNML|AIC|BIC : default: BDeu
                -e --ess ess (float) : default 1.0
                -m --cachefile cachefile: local scores
''')
Exemple #36
0
                dirarcs.add((p2, v))

    # move v-structure arcs to urcs
    urcs = set()
    for (x, y) in bns.arcs():
        if (x, y) not in dirarcs:
            deorient(x, y, urcs, bns)

    # orient what you can
    rorient(bns, urcs)

    return bns, urcs


def main(bnfile):
    bns = bn.bn.load(bnfile)
    return naivecause(bns)


if __name__ == '__main__':
    import bn, coliche

    (bns, urcs) = coliche.che(main, 'bnfile')

    for a in bns.arcs():
        print "%d -> %d" % a

    for (u1, u2) in urcs:
        if u1 > u2:
            print "%d -- %d" % (u1, u2)
Exemple #37
0
def infer_row(ir, d):
    ir.reset()

    for i, v in enumerate(d):
        if v != -1:
            ir.insert_evidence(i, v)

    return ir.infer()


def main(ifrdir, infile='', outfile=''):

    ir = ifr.load(ifrdir)
    inf = sys.stdin if infile == '' else open(infile)
    ouf = sys.stdout if outfile == '' else open(outfile, 'w')

    for l in inf:
        row = map(int, l.split())
        dstrs = infer_row(ir, row)
        ouf.write('\t'.join(' '.join(map(str, dstr)) for dstr in dstrs) + '\n')

    if infile != '': inf.close()
    if outfile != '': ouf.close()


coliche.che(
    main, """ifrdir
    -i --infile  infile : default stdin
    -o --outfile outfile : default stdout
""")
Exemple #38
0
            f1 = sum(s)
            f0 = N-f1

            p0, p1 = propreg.proptest((f0, f1), mlines)
            p0_avg = (r*p0_avg + p0) / (r+1)
            p1_avg = (r*p1_avg + p1) / (r+1)

            lb0 =   f1 * (h0>0.0 and math.log(h0)   or 0.0) \
                  + f0 * (h0<1.0 and math.log(1-h0) or 0.0)
            # lb1 = lfac(f1)+lfac(f0)-lfac(N+1)
            # lb1 = lmgprob(f0, f1, 1.0, 1.0)
            lb1 = lmgprob(f0, f1, 0.5, 0.5)

            b0, b1 = logmath.lognorm((lb0, lb1))
            b0_avg = (r*b0_avg + b0) / (r+1)
            b1_avg = (r*b1_avg + b1) / (r+1)
            
            
        print p, p0_avg, b0_avg

if __name__ == '__main__':
    from coliche import che
    
    che(main,
        """ N (int); H0;
        -n --nof-points nof-points (int)
        -r --nof-reps nof-reps (int)
        """)