def __init__(self, g, B, timespan_secs): super(UBSampler, self).__init__(g, timespan_secs) non_leaf_roots = (n for n in g.nodes_iter() if g.out_degree(n) > 0) self.nodes_sorted_by_upperbound = sorted( non_leaf_roots, key=lambda r: quota_upperbound( IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), r, B), reverse=True)
def __init__(self, g, B, timespan_secs): super(UBSampler, self).__init__(g, timespan_secs) non_leaf_roots = (n for n in g.nodes_iter() if g.out_degree(n) > 0) self.nodes_sorted_by_upperbound = sorted( non_leaf_roots, key=lambda r: quota_upperbound( IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), r, B), reverse=True )
def test_bs_ensure_result_is_tree(self): params = pkl.load( open(make_path('test/data/quota_test_cases/params.pkl')))[0] root = params['roots'][0] preprune_secs = params['preprune_secs'] mg = IU.get_topic_meta_graph_from_synthetic( make_path('test/data/quota_test_cases/interactions.json'), preprune_secs) dag = IU.get_rooted_subgraph_within_timespan(mg, root, preprune_secs) t = charikar_algo(dag, root, dag.nodes(), k=20, level=2) assert_true(nx.is_arborescence(t))
def __init__(self, g, B, timespan_secs, node_score_func=log_x_density): super(AdaptiveSampler, self).__init__(g, timespan_secs) non_leaf_roots = [n for n in g.nodes_iter() if g.out_degree(n) > 0] print("AdaptiveSampler: #roots to explore {}".format( len(non_leaf_roots))) print("AdaptiveSampler: getting upperbounds...") upperbounds = map( lambda r: quota_upperbound( IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), r, B), non_leaf_roots) print("AdaptiveSampler: sorting the roots by upperbound... ") inds = np.argsort(np.asarray(upperbounds))[::-1] # descending order self.roots_sorted_by_upperbound = [non_leaf_roots[i] for i in inds] self.root2upperbound = { r: u for r, u in zip(non_leaf_roots, upperbounds) } # self.roots_sorted_by_upperbound = sorted( # non_leaf_roots, # key=lambda r: quota_upperbound( # IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), # r, B), # reverse=True # ) self.node_score_func = node_score_func # self.root2upperbound = {r: quota_upperbound( # IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), # r, B) # for r in non_leaf_roots # } # updated at each iteration # nodes that are partially/fully computed # excluding leaves self.covered_nodes = set() # exclude leaves # self.roots_to_explore = sorted((n for n in g.nodes_iter() # if g.out_degree(n) > 0)) self.n_nodes_to_cover = len(self.roots_sorted_by_upperbound) self.node2score = {}
def test_bs_ensure_result_is_tree(self): params = pkl.load( open(make_path('test/data/quota_test_cases/params.pkl')) )[0] root = params['roots'][0] preprune_secs = params['preprune_secs'] mg = IU.get_topic_meta_graph_from_synthetic( make_path('test/data/quota_test_cases/interactions.json'), preprune_secs ) dag = IU.get_rooted_subgraph_within_timespan( mg, root, preprune_secs ) t = charikar_algo(dag, root, dag.nodes(), k=20, level=2) assert_true(nx.is_arborescence(t))
def __init__(self, g, B, timespan_secs, node_score_func=log_x_density): super(AdaptiveSampler, self).__init__(g, timespan_secs) non_leaf_roots = [n for n in g.nodes_iter() if g.out_degree(n) > 0] print("AdaptiveSampler: #roots to explore {}".format(len(non_leaf_roots))) print("AdaptiveSampler: getting upperbounds...") upperbounds = map(lambda r: quota_upperbound( IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), r, B), non_leaf_roots) print("AdaptiveSampler: sorting the roots by upperbound... ") inds = np.argsort(np.asarray(upperbounds))[::-1] # descending order self.roots_sorted_by_upperbound = [non_leaf_roots[i] for i in inds] self.root2upperbound = {r: u for r, u in zip(non_leaf_roots, upperbounds)} # self.roots_sorted_by_upperbound = sorted( # non_leaf_roots, # key=lambda r: quota_upperbound( # IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), # r, B), # reverse=True # ) self.node_score_func = node_score_func # self.root2upperbound = {r: quota_upperbound( # IU.get_rooted_subgraph_within_timespan(g, r, timespan_secs), # r, B) # for r in non_leaf_roots # } # updated at each iteration # nodes that are partially/fully computed # excluding leaves self.covered_nodes = set() # exclude leaves # self.roots_to_explore = sorted((n for n in g.nodes_iter() # if g.out_degree(n) > 0)) self.n_nodes_to_cover = len(self.roots_sorted_by_upperbound) self.node2score = {}
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'))
def root_and_dag(self, r): return r, IU.get_rooted_subgraph_within_timespan( self.g, r, self.timespan_secs)
def root_and_dag(self, r): return r, IU.get_rooted_subgraph_within_timespan( self.g, r, self.timespan_secs )