spacenet.centrality.closeness#

spacenet.centrality.closeness(spatial_network, nodes=None, edge_weight_name='Distance', max_distance=inf, add_as_node_label=False, node_label_name='closeness')#

Computes the closeness centrality for the nodes in the spatial network. The closeness centrality of a node is defined as the reciprocal of the average shortest path distance from that node to all other nodes in the network, considering only paths that are within a specified maximum distance.

Parameters:
spatial_networkNetworkX graph

The spatial network for which to calculate the node closeness centrality.

nodeslist or np.ndarray, optional

The set of nodes to compute the centrality. Default is ‘None’, computing for all nodes.

max_distancefloat, optional

The maximum distance to consider when computing the centrality. Only pairs of nodes that are within this distance of each other will be included in the computation. Default is np.inf (no limit).

edge_weight_namestr, optional

The name of the edge attribute in the graph that corresponds to the distance between nodes. Default is ‘Distance’.

add_as_node_labelbool, optional

Whether to add the computed centrality values as a node attribute in the spatial network. Default is False.

node_label_namestr, optional

The name of the node attribute to which to add the computed centrality values if add_as_node_label is True. Default is ‘closeness’.

Returns:
closenss_valuesnp.ndarray

An array of closeness centrality values for the specified nodes in the spatial network. The order of the values corresponds to the order of the nodes in the ‘nodes’ parameter.

nodesnp.ndarray

An array of the node ids for which the node reach centrality values were computed. The order of the nodes corresponds to the order of the values in the ‘closenss_values’ array.

Examples

You can compute the closeness centrality of nodes in a spatial network using the closeness function. Below is an example of how to use this function to compute the closeness centrality for all nodes in a spatial network generated from a set of points.

import spacenet as sn

# Load the spiral dataset and extract the 'x' and 'y' columns as points
spiral_data = sn.datasets.load_dataset('spiral')
points = spiral_data[['x', 'y']].to_numpy()

# generate a spatial network
G = sn.utils.spatial_network_from_points(points,max_edge_distance=50)

# compute the closeness for each node in the spatial network and add it as a node label
closeness_vals,node_ids=sn.centrality.closeness(G,max_distance=200,add_as_node_label=True,node_label_name='closeness')

# plot the spatial network with the closeness node label
sn.utils.plot_spatial_network(G,node_label_name='closeness')