def main(bnfile, bdtfile, scoretype='BDeu', ess=1.0, constraint_file="", cachefile=None, 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
def main(bnfile): 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 main(bdtfile, scoretype='BDeu', ess=1.0, time=None, iters=None, outfile=None, constraint_file="", startbn=None, cachefile=None): sigpool.watch('SIGUSR2') sigpool.watch('SIGUSR1') if time: sigpool.wait_n_raise(sigpool.str2time(time), 'SIGUSR2') cstrs = Constraints(constraint_file) if startbn != None: bn = bnmodule.load(startbn, do_pic=False) sc = scorefactory.getscorer(bdtfile, scoretype, ess, cachefile=cachefile) forests_left = False else: bn, sc = bnsearch.empty_net(bdtfile, scoretype, ess, cachefile=cachefile) bestforests.kruskal(bn, sc, cstrs) fry = bestforests.Forest(bn) forests_left = True bn = fry.next() if constraint_file: # should check if compatible with start for a in cstrs.must: bn.addarc(a) sc.score_new(bn) bn.picall() good_nets = [(sc.score(), bn.copy())] t = 0L while True: greedysearch.greedysearch(bn, sc, 1000, cstrs) gs = sc.score() t += 1 if gs > good_nets[0][0] and bn not in [gn for (sn, gn) in good_nets]: good_nets.append((gs, bn.copy())) good_nets.sort() if len(good_nets) > 2 * 10: good_nets = good_nets[10:] start_from_forest = random.choice((0, 1)) # stupid if out of forests if forests_left and start_from_forest: try: bn = fry.next() bn.picall() except StopIteration: forests_left = False if (not start_from_forest) or (not forests_left): bn = random.choice(good_nets)[1].copy() # bn = good_nets[0][1].copy() sc.score_new(bn) sas = bnsearch.score_arcs(bn, sc) sas.reverse() eas = list(enumerate(sas)) for x in xrange(len(sas) / 2): i, n, sa = wheelselect(eas) ii, (s, a) = eas.pop(i) if not a in cstrs.must: #print 'DEL', a 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]
def main(bnfile): print 'DAG' if is_dag(load(bnfile, do_pic=False)) else 'not DAG'
def main(bnfile): bn = load(bnfile) print(" ".join(map(str, gen_totord(bn))))