blip.py
def parse_res(filename: str, treewidth: int, outfile: str, cwidth=-1,
              add_extra_tuples=False, augfile: str = "augmented.jkl",
              datfile=None, retry=True, debug=False) -> TWBayesianNetwork:
    """
    Parse a .res file containing a solution BN. Optionally merge the parent set
    tuples from the jkl file `filename` and the the res file `outfile` and
    save as a temporary file `augfile` (only when `add_extra_tuples` is True)

    :param filename: input jkl file
    :param treewidth: treewidth bound
    :param outfile: res file containing BN
    :param cwidth: cwidth bound, use -1 to ignore and simply parse a TWBN
    :param add_extra_tuples: whether to inject tuples from outfile into filename
    :param augfile: name of temporary file containing merged set of tuples,
                    (only applicable if add_extra_tuples is True)
    :param datfile: path to data file (with header rows)
    :param retry: keep retrying until file is non-empty
    :param debug: debugging
    :return: parsed BN as a TWBayesianNetwork
    """
    elim_order = None
    tuples = []
    extra_tuples = dict()
    score = None
    # keep retrying until file is non-empty
    while retry and os.path.getsize(outfile) == 0:
        sleep(PARSE_RETRY_INTERVAL)
    with open(outfile) as out:
        for line in out:
            if not line.strip():
                continue
            elif line.startswith("Score:"):
                score = float(line.split()[1].strip())
                break
            elif line.startswith("elim-order:"):
                elim_order = line.split()[1].strip("()").split(",")
                elim_order = list(map(int, elim_order))
                if debug: print("elim-order:", elim_order)
            else:
                vertex, rest = line.split(":")
                vertex = int(vertex)
                rest = rest.strip().split()
                if len(rest) == 1:
                    local_score = float(rest[0])
                    parents = frozenset()
                else:
                    local_score, parents = rest
                    local_score = float(local_score)
                    parents = frozenset(map(int, parents.strip("()").split(",")))
                # bn.add(int(vertex), map(int, parents))
                tuples.append((vertex, parents))
                if add_extra_tuples:
                    extra_tuples[vertex] = (parents, local_score)
                if debug: print(f"v:{vertex}, sc:{local_score}, par:{parents})")
    if add_extra_tuples:
        inject_tuples(filename, extra_tuples, augfile, True)
        if debug: print("temporary merged file saved as", augfile)
        input_file = augfile
    else:
        input_file = filename
    if cwidth > 0:
        assert datfile, "datfile needed for cwidth"
        bn = CWBayesianNetwork(cwidth=cwidth, input_file=input_file, datfile=datfile)
    else:
        bn = TWBayesianNetwork(tw=treewidth, input_file=input_file)
    if elim_order is not None: bn.elim_order = elim_order
    for node, parents in tuples: bn.add(node, parents)
    bn.done()
    if score is not None:
        bn._score = score
    else:
        print(f"warning: score not found in {outfile}")
    return bn

    Parse a .res file containing a solution BN. Optionally merge the parent set
    tuples from the jkl file `filename` and the the res file `outfile` and
    save as a temporary file `augfile` (only when `add_extra_tuples` is True)

    :param filename: input jkl file
    :param treewidth: treewidth bound
    :param outfile: res file containing BN
    :param cwidth: cwidth bound, use -1 to ignore and simply parse a TWBN
    :param add_extra_tuples: whether to inject tuples from outfile into filename
    :param augfile: name of temporary file containing merged set of tuples,
                    (only applicable if add_extra_tuples is True)
    :param datfile: path to data file (with header rows)
    :param retry: keep retrying until file is non-empty
    :param debug: debugging
    :return: parsed BN as a TWBayesianNetwork
    
Parse a .res file containing a solution BN.
Parse a . res file containing a solution BN .
def parse_res(filename: str, treewidth: int, outfile: str, cwidth=-1,
              add_extra_tuples=False, augfile: str = "augmented.jkl",
              datfile=None, retry=True, debug=False) -> TWBayesianNetwork:
    elim_order = None
    tuples = []
    extra_tuples = dict()
    score = None
    while retry and os.path.getsize(outfile) == 0:
        sleep(PARSE_RETRY_INTERVAL)
    with open(outfile) as out:
        for line in out:
            if not line.strip():
                continue
            elif line.startswith("Score:"):
                score = float(line.split()[1].strip())
                break
            elif line.startswith("elim-order:"):
                elim_order = line.split()[1].strip("()").split(",")
                elim_order = list(map(int, elim_order))
                if debug: print("elim-order:", elim_order)
            else:
                vertex, rest = line.split(":")
                vertex = int(vertex)
                rest = rest.strip().split()
                if len(rest) == 1:
                    local_score = float(rest[0])
                    parents = frozenset()
                else:
                    local_score, parents = rest
                    local_score = float(local_score)
                    parents = frozenset(map(int, parents.strip("()").split(",")))
                tuples.append((vertex, parents))
                if add_extra_tuples:
                    extra_tuples[vertex] = (parents, local_score)
                if debug: print(f"v:{vertex}, sc:{local_score}, par:{parents})")
    if add_extra_tuples:
        inject_tuples(filename, extra_tuples, augfile, True)
        if debug: print("temporary merged file saved as", augfile)
        input_file = augfile
    else:
        input_file = filename
    if cwidth > 0:
        assert datfile, "datfile needed for cwidth"
        bn = CWBayesianNetwork(cwidth=cwidth, input_file=input_file, datfile=datfile)
    else:
        bn = TWBayesianNetwork(tw=treewidth, input_file=input_file)
    if elim_order is not None: bn.elim_order = elim_order
    for node, parents in tuples: bn.add(node, parents)
    bn.done()
    if score is not None:
        bn._score = score
    else:
        print(f"warning: score not found in {outfile}")
    return bn
def parse_res ( filename : str , treewidth : int , outfile : str , cwidth = - 1 , add_extra_tuples = False , augfile : str = "augmented.jkl" , datfile = None , retry = True , debug = False ) -> TWBayesianNetwork : elim_order = None tuples = [ ] extra_tuples = dict ( ) score = None while retry and os . path . getsize ( outfile ) == 0 : sleep ( PARSE_RETRY_INTERVAL ) with open ( outfile ) as out : for line in out : if not line . strip ( ) : continue elif line . startswith ( "Score:" ) : score = float ( line . split ( ) [ 1 ] . strip ( ) ) break elif line . startswith ( "elim-order:" ) : elim_order = line . split ( ) [ 1 ] . strip ( "()" ) . split ( "," ) elim_order = list ( map ( int , elim_order ) ) if debug : print ( "elim-order:" , elim_order ) else : vertex , rest = line . split ( ":" ) vertex = int ( vertex ) rest = rest . strip ( ) . split ( ) if len ( rest ) == 1 : local_score = float ( rest [ 0 ] ) parents = frozenset ( ) else : local_score , parents = rest local_score = float ( local_score ) parents = frozenset ( map ( int , parents . strip ( "()" ) . split ( "," ) ) ) tuples . append ( ( vertex , parents ) ) if add_extra_tuples : extra_tuples [ vertex ] = ( parents , local_score ) if debug : print ( f"v:{vertex}, sc:{local_score}, par:{parents})" ) if add_extra_tuples : inject_tuples ( filename , extra_tuples , augfile , True ) if debug : print ( "temporary merged file saved as" , augfile ) input_file = augfile else : input_file = filename if cwidth > 0 : assert datfile , "datfile needed for cwidth" bn = CWBayesianNetwork ( cwidth = cwidth , input_file = input_file , datfile = datfile ) else : bn = TWBayesianNetwork ( tw = treewidth , input_file = input_file ) if elim_order is not None : bn . elim_order = elim_order for node , parents in tuples : bn . add ( node , parents ) bn . done ( ) if score is not None : bn . _score = score else : print ( f"warning: score not found in {outfile}" ) return bn
Parse a .res file containing a solution BN.
Parse a . res file containing a solution BN .
"""
    Parse a .res file containing a solution BN. Optionally merge the parent set
    tuples from the jkl file `filename` and the the res file `outfile` and
    save as a temporary file `augfile` (only when `add_extra_tuples` is True)

    :param filename: input jkl file
    :param treewidth: treewidth bound
    :param outfile: res file containing BN
    :param cwidth: cwidth bound, use -1 to ignore and simply parse a TWBN
    :param add_extra_tuples: whether to inject tuples from outfile into filename
    :param augfile: name of temporary file containing merged set of tuples,
                    (only applicable if add_extra_tuples is True)
    :param datfile: path to data file (with header rows)
    :param retry: keep retrying until file is non-empty
    :param debug: debugging
    :return: parsed BN as a TWBayesianNetwork
    """ # keep retrying until file is non-empty # bn.add(int(vertex), map(int, parents))
parsed BN as a TWBayesianNetwork
parsed BN as a TWBayesianNetwork
filename
str
input jkl file
input jkl file
treewidth
int
None
outfile
str
res file containing BN
res file containing BN
cwidth
cwidth bound, use -1 to ignore and simply parse a TWBN
cwidth bound use - 1 to ignore and simply parse a TWBN
add_extra_tuples
whether to inject tuples from outfile into filename
whether to inject tuples from outfile into filename
augfile
str
name of temporary file containing merged set of tuples,
(only applicable if add_extra_tuples is True)
name of temporary file containing merged set of tuples ( only applicable if add_extra_tuples is True )
datfile
path to data file (with header rows)
path to data file ( with header rows )
retry
keep retrying until file is non-empty
keep retrying until file is non - empty
debug
None