Files
semantica/docs/cookbook/advanced/Advanced_Graph_Analytics.ipynb
T
KaifAhmad1 c469f5455b feat(graph_store): Add Graph Store module to cookbook and examples
- Add new Graph_Store.ipynb introduction notebook
- Update Advanced_Graph_Analytics.ipynb with graph store persistence
- Update Fraud_Detection.ipynb with graph database storage
- Update Transaction_Network_Analysis.ipynb with blockchain graph storage
- Update Criminal_Network_Analysis.ipynb with criminal network persistence
- Update Welcome_to_Semantica.ipynb with Graph Store module documentation
- Update docs/cookbook.md, docs/examples.md, docs/CodeExamples.md
- Sync all notebooks to docs/cookbook directory
2025-11-26 16:55:55 +05:30

7.8 KiB

Advanced Graph Analytics

Overview

This notebook demonstrates advanced graph analytics using GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator, Deduplicator, and GraphStore for persistent storage.

Learning Objectives

  • Use GraphAnalyzer for comprehensive graph analysis
  • Use CentralityCalculator for advanced centrality measures
  • Use CommunityDetector for community detection
  • Use ConnectivityAnalyzer for connectivity analysis
  • Use GraphValidator and Deduplicator for graph quality
  • Use GraphStore to persist graphs to Neo4j, KuzuDB, or FalkorDB

Workflow: Graph Analysis → Centrality → Communities → Connectivity → Validation → Deduplication → Persist to Graph Store

In [ ]:
from semantica.kg import GraphBuilder, GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator, Deduplicator

builder = GraphBuilder()
analyzer = GraphAnalyzer()

entities = [
    {"id": "e1", "type": "Organization", "name": "Apple Inc.", "properties": {}},
    {"id": "e2", "type": "Person", "name": "Tim Cook", "properties": {}},
    {"id": "e3", "type": "Location", "name": "Cupertino", "properties": {}}
]

relationships = [
    {"source": "e2", "target": "e1", "type": "CEO_of", "properties": {}},
    {"source": "e1", "target": "e3", "type": "located_in", "properties": {}}
]

kg = builder.build(entities, relationships)

metrics = analyzer.compute_metrics(kg)

print(f"Graph metrics:")
print(f"  Entities: {metrics.get('entity_count', 0)}")
print(f"  Relationships: {metrics.get('relationship_count', 0)}")
print(f"  Density: {metrics.get('density', 0):.3f}")

Step 2: Advanced Centrality Measures

Calculate multiple centrality measures.

In [ ]:
centrality_calculator = CentralityCalculator()

degree_centrality = centrality_calculator.calculate_centrality(kg, measure="degree")
betweenness_centrality = centrality_calculator.calculate_centrality(kg, measure="betweenness")

print(f"Degree centrality: {len(degree_centrality)} entities")
print(f"Betweenness centrality: {len(betweenness_centrality)} entities")

Step 3: Community Detection

Detect communities in the graph.

In [ ]:
community_detector = CommunityDetector()

communities = community_detector.detect_communities(kg)

print(f"Detected {len(communities)} communities")
for i, community in enumerate(communities[:3], 1):
    print(f"  Community {i}: {len(community)} entities")

Step 4: Connectivity Analysis

Analyze graph connectivity.

In [ ]:
connectivity_analyzer = ConnectivityAnalyzer()

connectivity = connectivity_analyzer.analyze_connectivity(kg)

print(f"Connectivity analysis:")
print(f"  Is connected: {connectivity.get('is_connected', False)}")
print(f"  Components: {len(connectivity.get('components', []))}")

Step 5: Graph Validation and Deduplication

Validate and deduplicate the graph.

In [ ]:
graph_validator = GraphValidator()
deduplicator = Deduplicator()

validation_result = graph_validator.validate(kg)
deduplicated_kg = deduplicator.deduplicate(kg)

print(f"Graph validation: {validation_result.get('valid', False)}")
print(f"Deduplicated entities: {len(deduplicated_kg.get('entities', []))}")

Step 6: Persist to Graph Store

Store the analyzed graph in a persistent graph database using GraphStore.

In [ ]:
from semantica.graph_store import GraphStore

# Initialize graph store (using KuzuDB for embedded storage)
graph_store = GraphStore(backend="kuzu", database_path="./analytics_graph_db")
graph_store.connect()

# Store entities as nodes
for entity in entities:
    node = graph_store.create_node(
        labels=[entity["type"]],
        properties={"name": entity["name"], "original_id": entity["id"]}
    )
    print(f"Stored node: {entity['name']}")

# Store relationships
for rel in relationships:
    # In a real scenario, you'd lookup node IDs first
    print(f"Relationship: {rel['source']} -{rel['type']}-> {rel['target']}")

# Query using Cypher
results = graph_store.execute_query("MATCH (n) RETURN n.name, labels(n) LIMIT 10")
print(f"\nStored {len(results.get('records', []))} nodes in graph store")

# Get statistics
stats = graph_store.get_stats()
print(f"Graph store stats: {stats}")

graph_store.close()

Summary

You've learned advanced graph analytics:

  • GraphAnalyzer: Comprehensive graph analysis and metrics
  • CentralityCalculator: Multiple centrality measures
  • CommunityDetector: Community detection
  • ConnectivityAnalyzer: Connectivity analysis
  • GraphValidator: Graph validation
  • Deduplicator: Graph deduplication
  • GraphStore: Persist graphs to Neo4j, KuzuDB, or FalkorDB