-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update convert to network task to take single dataframe * Simplify calculate measures tasks to take single network * Clean up calculate measures tasks * Remove calculate cluster tasks * Fix README capitalization
- Loading branch information
1 parent
40ff0bc
commit 9bf0695
Showing
8 changed files
with
56 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 18 additions & 33 deletions
51
src/abm_colony_collection/calculate_centrality_measures.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,22 @@ | ||
import networkx as nx | ||
import numpy as np | ||
import pandas as pd | ||
|
||
|
||
def calculate_centrality_measures(networks: dict) -> pd.DataFrame: | ||
all_measures = [] | ||
|
||
for (seed, tick), network in networks.items(): | ||
degree_centralities = list(nx.degree_centrality(network).values()) | ||
closeness_centralities = list(nx.closeness_centrality(network).values()) | ||
betweenness_centralities = list(nx.betweenness_centrality(network).values()) | ||
|
||
degree_centrality_mean = np.mean(degree_centralities) | ||
closeness_centrality_mean = np.mean(closeness_centralities) | ||
betweenness_centrality_mean = np.mean(betweenness_centralities) | ||
|
||
degree_centrality_std = np.std(degree_centralities, ddof=1) | ||
closeness_centrality_std = np.std(closeness_centralities, ddof=1) | ||
betweenness_centrality_std = np.std(betweenness_centralities, ddof=1) | ||
|
||
all_measures.append( | ||
{ | ||
"SEED": seed, | ||
"TICK": tick, | ||
"DEGREE_CENTRALITY_MEAN": degree_centrality_mean, | ||
"CLOSENESS_CENTRALITY_MEAN": closeness_centrality_mean, | ||
"BETWEENNESS_CENTRALITY_MEAN": betweenness_centrality_mean, | ||
"DEGREE_CENTRALITY_STD": degree_centrality_std, | ||
"CLOSENESS_CENTRALITY_STD": closeness_centrality_std, | ||
"BETWEENNESS_CENTRALITY_STD": betweenness_centrality_std, | ||
} | ||
) | ||
|
||
all_measures_df = pd.DataFrame(all_measures) | ||
|
||
return all_measures_df | ||
def calculate_centrality_measures(network: nx.Graph) -> pd.DataFrame: | ||
# Calculate different centrality measures for network. | ||
degree_centralities = nx.degree_centrality(network) | ||
closeness_centralities = nx.closeness_centrality(network) | ||
betweenness_centralities = nx.betweenness_centrality(network) | ||
|
||
# Extract centrality measures for each node in network. | ||
measures = [ | ||
{ | ||
"ID": node, | ||
"DEGREE_CENTRALITY": degree_centralities[node], | ||
"CLOSENESS_CENTRALITY": closeness_centralities[node], | ||
"BETWEENNESS_CENTRALITY": betweenness_centralities[node], | ||
} | ||
for node in network.nodes | ||
] | ||
|
||
return pd.DataFrame(measures) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,15 @@ | ||
import numpy as np | ||
import networkx as nx | ||
import pandas as pd | ||
|
||
|
||
def calculate_degree_measures(networks: dict) -> pd.DataFrame: | ||
all_measures = [] | ||
def calculate_degree_measures(network: nx.Graph) -> pd.DataFrame: | ||
# Extract degree for each node in network. | ||
measures = [ | ||
{ | ||
"ID": node, | ||
"DEGREE": degree, | ||
} | ||
for node, degree in network.degree() | ||
] | ||
|
||
for (seed, tick), network in networks.items(): | ||
degrees = sorted([d for n, d in network.degree()], reverse=True) | ||
degree_mean = np.mean(degrees) | ||
degree_std = np.std(degrees, ddof=1) | ||
|
||
all_measures.append( | ||
{ | ||
"SEED": seed, | ||
"TICK": tick, | ||
"DEGREES": degrees, | ||
"DEGREE_MEAN": degree_mean, | ||
"DEGREE_STD": degree_std, | ||
} | ||
) | ||
|
||
all_measures_df = pd.DataFrame(all_measures) | ||
|
||
return all_measures_df | ||
return pd.DataFrame(measures) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,23 @@ | ||
from typing import Union | ||
|
||
import networkx as nx | ||
import numpy as np | ||
import pandas as pd | ||
|
||
|
||
def calculate_distance_measures(networks: dict) -> pd.DataFrame: | ||
all_measures = [] | ||
|
||
for (seed, tick), network in networks.items(): | ||
if not nx.is_connected(network): | ||
subnetworks = [ | ||
network.subgraph(component) for component in nx.connected_components(network) | ||
] | ||
|
||
radius = np.mean([nx.radius(subnetwork) for subnetwork in subnetworks]) | ||
diameter = np.mean([nx.diameter(subnetwork) for subnetwork in subnetworks]) | ||
|
||
eccentricities = [nx.eccentricity(subnetwork) for subnetwork in subnetworks] | ||
eccentricity = np.mean([np.mean(list(ecc.values())) for ecc in eccentricities]) | ||
def calculate_distance_measures(network: nx.Graph) -> pd.DataFrame: | ||
measures: list[dict[str, Union[int, float]]] = [] | ||
|
||
shortest_paths = [ | ||
nx.average_shortest_path_length(subnetwork) for subnetwork in subnetworks | ||
] | ||
shortest_path = np.mean(shortest_paths) | ||
else: | ||
radius = nx.radius(network) | ||
diameter = nx.diameter(network) | ||
for component in nx.connected_components(network): | ||
# Calculate eccentricity for connected subnetwork. | ||
eccentricity = nx.eccentricity(network.subgraph(component)) | ||
|
||
ecc = nx.eccentricity(network) | ||
eccentricity = np.mean(list(ecc.values())) | ||
|
||
shortest_path = nx.average_shortest_path_length(network) | ||
|
||
all_measures.append( | ||
# Extract distance measures for each node in subnetwork. | ||
measures = measures + [ | ||
{ | ||
"SEED": seed, | ||
"TICK": tick, | ||
"RADIUS": radius, | ||
"DIAMETER": diameter, | ||
"ECCENTRICITY": eccentricity, | ||
"SHORTEST_PATH": shortest_path, | ||
"ID": node, | ||
"ECCENTRICITY": eccentricity[node], | ||
} | ||
) | ||
|
||
all_measures_df = pd.DataFrame(all_measures) | ||
for node in component | ||
] | ||
|
||
return all_measures_df | ||
return pd.DataFrame(measures) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters