Spaces:
Running
Running
| from typing import List, Dict, Any | |
| from tinytroupe.social_network import NetworkTopology, Community | |
| class NetworkAnalyzer: | |
| """ | |
| Provide analytics for understanding network dynamics. | |
| """ | |
| def calculate_centrality_metrics(network: NetworkTopology) -> Dict[str, Dict[str, float]]: | |
| """ | |
| Calculates various centrality metrics for each persona in the network. | |
| """ | |
| # Simplistic implementation without NetworkX for now to avoid dependency issues in basic environment | |
| metrics = {name: {"degree": 0.0} for name in network.nodes} | |
| for edge in network.edges: | |
| metrics[edge.source_id]["degree"] += 1 | |
| metrics[edge.target_id]["degree"] += 1 | |
| n = max(len(network.nodes), 1) | |
| for name in metrics: | |
| metrics[name]["degree"] /= n | |
| return metrics | |
| def detect_communities(network: NetworkTopology) -> List[Community]: | |
| """ | |
| Detects communities within the social network. | |
| """ | |
| # Placeholder for community detection logic | |
| return network.communities | |
| def identify_key_influencers(network: NetworkTopology, top_k: int = 10) -> List[str]: | |
| """ | |
| Identifies the top K influencers in the network. | |
| """ | |
| metrics = NetworkAnalyzer.calculate_centrality_metrics(network) | |
| sorted_influencers = sorted(metrics.keys(), key=lambda x: metrics[x]["degree"], reverse=True) | |
| return sorted_influencers[:top_k] | |
| def calculate_density(network: NetworkTopology) -> float: | |
| """ | |
| Calculates the density of the network. | |
| """ | |
| n = len(network.nodes) | |
| if n < 2: return 0.0 | |
| max_edges = n * (n - 1) / 2 | |
| return len(network.edges) / max_edges | |