def calculate_degree(graph: Graph, in_degree_property, out_degree_property, weight_property=None): """ Calculate the (potentially weighted) in and out degrees of a graph. The function will modify the given graph by adding two new node properties, one for the in degree and one for the out degree. Nothing is returned. Parameters: graph: a Graph in_degree_property: the property name for the in degree out_degree_property: the property name for the out degree weight_property: an edge property to use in calculating the weighted degree """ num_nodes = graph.num_nodes() nout = NUMAArray[np.uint64](num_nodes, AllocationPolicy.INTERLEAVED) nin = NUMAArray[np.uint64](num_nodes, AllocationPolicy.INTERLEAVED) do_all(range(num_nodes), initialize_in_degree(nin.as_numpy()), steal=False) # are we calculating weighted degree? if not weight_property: count_operator = count_in_and_out_degree(graph, nout.as_numpy(), nin.as_numpy()) else: count_operator = count_weighted_in_and_out_degree( graph, nout.as_numpy(), nin.as_numpy(), graph.get_edge_property(weight_property)) do_all(range(num_nodes), count_operator, steal=True) graph.add_node_property( pyarrow.table({ in_degree_property: nin, out_degree_property: nout }))
def initialize(graph: Graph, source: int, distance: np.ndarray): num_nodes = graph.num_nodes() for n in range(num_nodes): if n == source: distance[n] = 0 else: distance[n] = distance_infinity
def bfs_sync_pg(graph: Graph, source, property_name): next_level_number = 0 curr_level = InsertBag[np.uint64]() next_level = InsertBag[np.uint64]() timer = StatTimer("BFS Property Graph Numba: " + property_name) timer.start() distance = np.empty((graph.num_nodes(), ), dtype=np.uint32) initialize(graph, source, distance) next_level.push(source) while not next_level.empty(): curr_level.swap(next_level) next_level.clear() next_level_number += 1 do_all( curr_level, bfs_sync_operator_pg(graph, next_level, next_level_number, distance), steal=True, loop_name="bfs_sync_pg", ) timer.stop() graph.add_node_property(pyarrow.table({property_name: distance}))
def kcore_async(graph: Graph, k_core_num, property_name): num_nodes = graph.num_nodes() initial_worklist = InsertBag[np.uint64]() current_degree = NUMAArray[np.uint64](num_nodes, AllocationPolicy.INTERLEAVED) timer = StatTimer("Kcore: Property Graph Numba: " + property_name) timer.start() # Initialize do_all( range(num_nodes), compute_degree_count_operator(graph, current_degree.as_numpy()), steal=True, ) # Setup initial worklist do_all( range(num_nodes), setup_initial_worklist_operator(initial_worklist, current_degree.as_numpy(), k_core_num), steal=True, ) # Compute k-core for_each( initial_worklist, compute_async_kcore_operator(graph, current_degree.as_numpy(), k_core_num), steal=True, disable_conflict_detection=True, ) timer.stop() # Add the ranks as a new property to the property graph graph.add_node_property(pyarrow.table({property_name: current_degree}))
def cc_push_topo(graph: Graph, property_name): print("Executing Push algo\n") num_nodes = graph.num_nodes() timer = StatTimer("CC: Property Graph Numba: " + property_name) timer.start() # Stores the component id assignment comp_current = np.empty((num_nodes, ), dtype=np.uint32) comp_old = np.empty((num_nodes, ), dtype=np.uint32) # Initialize do_all( range(num_nodes), initialize_cc_push_operator(graph, comp_current, comp_old), steal=True, loop_name="initialize_cc_push", ) # Execute while component ids are updated changed = ReduceLogicalOr() changed.update(True) while changed.reduce(): changed.reset() do_all( range(num_nodes), cc_push_topo_operator(graph, changed, comp_current, comp_old), steal=True, loop_name="cc_push_topo", ) timer.stop() # Add the component assignment as a new property to the property graph graph.add_node_property(pyarrow.table({property_name: comp_current}))
def create_distance_array(g: Graph, source, length_property): a = np.empty(g.num_nodes(), dtype=dtype_of_pyarrow_array( g.get_edge_property(length_property))) # TODO(amp): Remove / 4 infinity = dtype_info(a.dtype).max / 4 a[:] = infinity a[source] = 0 return a
def test_write(graph): with TemporaryDirectory() as tmpdir: graph.write(tmpdir) del graph graph = Graph(tmpdir) assert graph.num_nodes() == 29946 assert graph.num_edges() == 43072 assert len(graph.loaded_node_schema()) == 17 assert len(graph.loaded_edge_schema()) == 3
def test_commit(graph): with TemporaryDirectory() as tmpdir: graph.path = tmpdir graph.write() del graph graph = Graph(tmpdir) assert graph.num_nodes() == 29092 assert graph.num_edges() == 39283 assert len(graph.loaded_node_schema()) == 17 assert len(graph.loaded_edge_schema()) == 3
def degree_assortativity_coefficient( graph: Graph, source_degree_type: DegreeType = DegreeType.OUT, destination_degree_type: DegreeType = DegreeType.IN, weight=None, ): """ Calculates and returns the degree assortativity of a given graph. Paramaters: * graph: the Graph to be analyzed * source_degree_type: description of degree type to consider for the source node on an edge expected values are DegreeType.IN or DegreeType.OUT * destination_degree_type: description the degree type to consider for the destination node on an edge expected values are DegreeType.IN or DegreeType.OUT * weight (optional): edge property to use if using weighted degrees """ # get the tables associated with the degree types of the source and destination nodes calculate_degree(graph, "temp_DegreeType.IN", "temp_DegreeType.OUT", weight) source_degree = graph.get_node_property("temp_" + str(source_degree_type)) destination_degree = graph.get_node_property("temp_" + str(destination_degree_type)) try: # Calculate the average in and out degrees of graph # (with respect to number of edges, not number of nodes) num_edges = graph.num_edges() source_average, destination_average = average_degree( graph, num_edges, source_degree, destination_degree) # Calculate the numerator (product of deviation from mean) # and the factors of the denominator (square deviation from mean) product_of_dev = ReduceSum[float](0) square_of_source_dev = ReduceSum[float](0) square_of_destination_dev = ReduceSum[float](0) do_all( range(graph.num_nodes()), degree_assortativity_coefficient_operator( graph, source_degree, source_average, destination_degree, destination_average, product_of_dev, square_of_source_dev, square_of_destination_dev, ), steal=True, loop_name="degree assortativity coefficient calculation", ) return product_of_dev.reduce() / sqrt( square_of_source_dev.reduce() * square_of_destination_dev.reduce()) finally: graph.remove_node_property("temp_DegreeType.IN") graph.remove_node_property("temp_DegreeType.OUT")
def pagerank_pull_sync_residual(graph: Graph, maxIterations, tolerance, property_name): num_nodes = graph.num_nodes() rank = NUMAArray[float](num_nodes, AllocationPolicy.INTERLEAVED) nout = NUMAArray[np.uint64](num_nodes, AllocationPolicy.INTERLEAVED) delta = NUMAArray[float](num_nodes, AllocationPolicy.INTERLEAVED) residual = NUMAArray[float](num_nodes, AllocationPolicy.INTERLEAVED) # Initialize do_all( range(num_nodes), initialize_residual_operator(rank.as_numpy(), nout.as_numpy(), delta.as_numpy(), residual.as_numpy(),), steal=True, loop_name="initialize_pagerank_pull_residual", ) # Compute out-degree for each node do_all( range(num_nodes), compute_out_deg_operator(graph, nout.as_numpy()), steal=True, loop_name="Compute_out_degree", ) print("Out-degree of 0: ", nout[0]) changed = ReduceOr(True) iterations = 0 timer = StatTimer("Pagerank: Property Graph Numba: " + property_name) timer.start() while iterations < maxIterations and changed.reduce(): print("Iter: ", iterations, "\n") changed.reset() iterations += 1 do_all( range(num_nodes), compute_pagerank_pull_delta_operator( rank.as_numpy(), nout.as_numpy(), delta.as_numpy(), residual.as_numpy(), tolerance, changed, ), steal=True, loop_name="pagerank_delta", ) do_all( range(num_nodes), compute_pagerank_pull_residual_operator(graph, delta.as_numpy(), residual.as_numpy()), steal=True, loop_name="pagerank", ) timer.stop() # Add the ranks as a new property to the property graph graph.add_node_property(pyarrow.table({property_name: rank}))
def average_degree(graph: Graph, num_edges: int, source_degree, destination_degree): """ Calculate the average in or out degree for the source and destination nodes Returns the result as a tuple in the form (average degree for source, average degree for destination) """ sum_source_degrees = ReduceSum[np.uint64](0) sum_destination_degrees = ReduceSum[np.uint64](0) do_all( range(graph.num_nodes()), sum_degree_operator(graph, source_degree, sum_source_degrees, destination_degree, sum_destination_degrees), steal=True, ) return (sum_source_degrees.reduce() / num_edges, sum_destination_degrees.reduce() / num_edges)
def initialize_cc_push_operator(graph: Graph, comp_current: np.ndarray, comp_old: np.ndarray, nid): # Initialize each node in its own component comp_current[nid] = nid comp_old[nid] = graph.num_nodes()