Coverage for src/abm_colony_collection/calculate_centrality_measures.py: 100%

8 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2025-09-15 20:34 +0000

1import networkx as nx 

2import pandas as pd 

3 

4 

5def calculate_centrality_measures(network: nx.Graph) -> pd.DataFrame: 

6 """ 

7 Calculate centrality measures for each node in network. 

8 

9 Measures include: 

10 

11 - Degree centrality = quantifies how many neighbors a node has 

12 - Closeness centrality = quantifies how close a node is to all other nodes 

13 in the network 

14 - Betweenness centrality = quantifies the extent to which a vertex lies on 

15 shortest paths between other vertices 

16 

17 Parameters 

18 ---------- 

19 network 

20 The network object. 

21 

22 Returns 

23 ------- 

24 : 

25 Centrality measures for each node in the network. 

26 """ 

27 

28 # Calculate different centrality measures for network. 

29 degree_centralities = nx.degree_centrality(network) 

30 closeness_centralities = nx.closeness_centrality(network) 

31 betweenness_centralities = nx.betweenness_centrality(network) 

32 

33 # Extract centrality measures for each node in network. 

34 measures = [ 

35 { 

36 "ID": node, 

37 "DEGREE_CENTRALITY": degree_centralities[node], 

38 "CLOSENESS_CENTRALITY": closeness_centralities[node], 

39 "BETWEENNESS_CENTRALITY": betweenness_centralities[node], 

40 } 

41 for node in network.nodes 

42 ] 

43 

44 return pd.DataFrame(measures)