Esempio n. 1
0
def calc_tree(node_i, r, dag, U,
              gen_tree_func,
              gen_tree_kws,
              print_summary,
              should_binarize_dag=False):
    print('root', r)
    logger.info('nodes procssed {}'.format(node_i))
    if len(dag.edges()) == 0:
        logger.debug("empty rooted sub graph")
        assert dag.number_of_nodes() == 1
        return dag

    if gen_tree_kws.get('dijkstra'):
        logger.debug('applying dijkstra')
        dag = remove_edges_via_dijkstra(
            dag,
            source=r,
            weight=IU.EDGE_COST_KEY
        )

    if should_binarize_dag:
        logger.debug('binarizing dag...')

        dag = binarize_dag(dag,
                           IU.VERTEX_REWARD_KEY,
                           IU.EDGE_COST_KEY,
                           dummy_node_name_prefix="d_")

    logger.debug('generating tree ')

    print(dag.number_of_nodes())
    tree = gen_tree_func(dag, r, U)

    if should_binarize_dag:
        tree = unbinarize_dag(tree,
                              edge_weight_key=IU.EDGE_COST_KEY)

    if len(tree.edges()) == 0:
        logger.debug("empty event tree")

    if print_summary:
        logger.debug('tree summary:\n{}'.format(get_summary(tree)))

    # post checking
    if tree.number_of_edges() == 0:
        assert tree.number_of_nodes() == 1, '#roots={}'.format(
            tree.number_of_nodes()
        )
    else:
        assert nx.is_arborescence(tree), 'not a tree'
    return tree
def calc_tree(node_i,
              r,
              dag,
              U,
              gen_tree_func,
              gen_tree_kws,
              print_summary,
              should_binarize_dag=False):
    print('root', r)
    logger.info('nodes procssed {}'.format(node_i))
    if len(dag.edges()) == 0:
        logger.debug("empty rooted sub graph")
        assert dag.number_of_nodes() == 1
        return dag

    if gen_tree_kws.get('dijkstra'):
        logger.debug('applying dijkstra')
        dag = remove_edges_via_dijkstra(dag, source=r, weight=IU.EDGE_COST_KEY)

    if should_binarize_dag:
        logger.debug('binarizing dag...')

        dag = binarize_dag(dag,
                           IU.VERTEX_REWARD_KEY,
                           IU.EDGE_COST_KEY,
                           dummy_node_name_prefix="d_")

    logger.debug('generating tree ')

    print(dag.number_of_nodes())
    tree = gen_tree_func(dag, r, U)

    if should_binarize_dag:
        tree = unbinarize_dag(tree, edge_weight_key=IU.EDGE_COST_KEY)

    if len(tree.edges()) == 0:
        logger.debug("empty event tree")

    if print_summary:
        logger.debug('tree summary:\n{}'.format(get_summary(tree)))

    # post checking
    if tree.number_of_edges() == 0:
        assert tree.number_of_nodes() == 1, '#roots={}'.format(
            tree.number_of_nodes())
    else:
        assert nx.is_arborescence(tree), 'not a tree'
    return tree
def sample_rooted_binary_graphs_within_timespan(
        meta_graph_pickle_path,
        sample_number,
        timespan,
        output_path):
    g = nx.read_gpickle(meta_graph_pickle_path)
    roots = sample_nodes(g, sample_number)
    results = []
    for i, r in enumerate(roots):
        print('done:', i)
        sub_g = InteractionsUtil.get_rooted_subgraph_within_timespan(
            g, r, timespan
        )
        binary_sub_g = binarize_dag(sub_g,
                                    InteractionsUtil.VERTEX_REWARD_KEY,
                                    InteractionsUtil.EDGE_COST_KEY,
                                    dummy_node_name_prefix="d_")
        
        if len(binary_sub_g.edges()) > 0:
            results.append(binary_sub_g)

    pkl.dump(results, open(output_path, 'w'))