brent.parsers module

Source code
from .bif import bif

__all__ = ['bif']

Functions

def bif(bif)

Parses a Bayesian Interchange Format (bif) string into a probability table and a set of links

Source code
def bif(bif: str) -> Tuple[pd.DataFrame, List[Tuple[str, str]]]:
    """
    Parses a Bayesian Interchange Format (bif) string into a probability table and a set of links
    """
    probability_table = pd.DataFrame({'key': [1], 'prob': [1]})

    if parse_network_type(bif) != 'unknown':
        logger.warning("BIF Parser does not take into account known networks")

    variable_states = dict(parse_variables(bif))
    edges = []

    for varname, probs in parse_unconditional_probabilities(bif):
        var_df = pd.DataFrame({varname: variable_states[varname], 'prob': probs})
        probability_table = join_independent(probability_table, var_df)

    for varname, conditionals, probabilities in parse_conditional_probabilities(bif):
        cond_df = (pd.DataFrame(probabilities, columns=conditionals + variable_states[varname])
                   .melt(id_vars=conditionals, var_name=varname, value_name='prob')
                   .set_index(conditionals)
                   )
        probability_table = join_dependent(probability_table, cond_df)
        edges += [(conditional, varname) for conditional in conditionals]

    return probability_table, edges