def discover_sample_ordering(graph):
    '''
    Try to get the order of variable nodes
    for sampling. This would be easier in
    the underlying BBN but lets try on
    the factor graph.
    '''
    iterations = 0
    ordering = []
    pmf_ordering = []
    accounted_for = set()
    variable_nodes = [n for n in graph.nodes if isinstance(n, VariableNode)]
    factor_nodes = [n for n in graph.nodes if isinstance(n, FactorNode)]
    required = len([n for n in graph.nodes if isinstance(n, VariableNode)])
    # Firstly any leaf factor nodes will
    # by definition only have one variable
    # node connection, therefore these
    # variables can be set first.
    for node in graph.get_leaves():
        if isinstance(node, FactorNode):
            ordering.append(node.neighbours[0])
            accounted_for.add(node.neighbours[0].name)
            pmf_ordering.append(node.func)

    # Now for each factor node whose variables
    # all but one are already in the ordering,
    # we can add that one variable. This is
    # actuall
    while required < len(ordering):
        for node in factor_nodes:
            args = set(get_args(node.func))
            new_args = args.difference(accounted_for)
            if len(new_args) == 1:
                arg_name = list(new_args)[0]
                var_node = node.get_neighbour_by_name(arg_name)
                ordering.append(var_node)
                accounted_for.add(var_node.name)
                pmf_ordering.append(node.func)
    return zip(ordering, pmf_ordering)
dataset/ETHPy150Open eBay/bayesian-belief-networks/bayesian/factor_graph.py/discover_sample_ordering/swapped operands of `<`