def cc_push_topo(graph: PropertyGraph, 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 = GReduceLogicalOr() 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 initialize(graph: PropertyGraph, 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 kcore_async(graph: PropertyGraph, k_core_num, property_name): num_nodes = graph.num_nodes() initial_worklist = InsertBag[np.uint64]() current_degree = LargeArray[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 calculate_degree(graph: PropertyGraph, 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 PropertyGraph 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 = LargeArray[np.uint64](num_nodes, AllocationPolicy.INTERLEAVED) nin = LargeArray[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 degree_assortativity_coefficient( graph: PropertyGraph, 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 PropertyGraph 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 = GAccumulator[float](0) square_of_source_dev = GAccumulator[float](0) square_of_destination_dev = GAccumulator[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 test_commit(property_graph): # TODO(amp): mark_all_properties_persistent shouldn't be required. Why is it? property_graph.mark_all_properties_persistent() with TemporaryDirectory() as tmpdir: property_graph.path = tmpdir property_graph.write() del property_graph property_graph = PropertyGraph(tmpdir) assert property_graph.num_nodes() == 29092 assert property_graph.num_edges() == 39283 assert len(property_graph.node_schema()) == 31 assert len(property_graph.edge_schema()) == 19
def pagerank_pull_sync_residual(graph: PropertyGraph, maxIterations, tolerance, property_name): num_nodes = graph.num_nodes() rank = LargeArray[float](num_nodes, AllocationPolicy.INTERLEAVED) nout = LargeArray[np.uint64](num_nodes, AllocationPolicy.INTERLEAVED) delta = LargeArray[float](num_nodes, AllocationPolicy.INTERLEAVED) residual = LargeArray[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 = GReduceLogicalOr(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: PropertyGraph, 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 = GAccumulator[np.uint64](0) sum_destination_degrees = GAccumulator[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: PropertyGraph, 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()