Buckets:
| #!/usr/bin/env python3 | |
| """ | |
| CA10 Knowledge Graphs - Main Execution Script | |
| This script runs all components of the Knowledge Graphs project | |
| """ | |
| import os | |
| import sys | |
| import matplotlib.pyplot as plt | |
| import matplotlib | |
| from datetime import datetime | |
| import warnings | |
| warnings.filterwarnings("ignore") | |
| # Set matplotlib backend for saving figures | |
| matplotlib.use('Agg') | |
| # Add src to path | |
| sys.path.append(os.path.join(os.path.dirname(__file__), 'src')) | |
| # Import all necessary modules | |
| from core.knowledge_graph import KnowledgeGraph, create_sample_kg | |
| from core.scientific_kg import AdvancedScientificKG, create_advanced_scientific_kg | |
| from models.embeddings import TransE, DistMult, ComplEx, KGEmbeddingTrainer | |
| def setup_directories(): | |
| """Create necessary directories""" | |
| dirs = ['logs', 'results', 'visualization'] | |
| for dir_name in dirs: | |
| os.makedirs(dir_name, exist_ok=True) | |
| print("โ Directories created successfully") | |
| def save_figure(fig, filename, title=""): | |
| """Save figure to visualization directory""" | |
| if os.getenv('SAVE_VISUALIZATIONS', 'false').lower() == 'true': | |
| viz_dir = os.getenv('VISUALIZATION_DIR', 'visualization') | |
| filepath = os.path.join(viz_dir, filename) | |
| fig.savefig(filepath, dpi=300, bbox_inches='tight') | |
| print(f"๐พ Saved visualization: {filepath}") | |
| if title: | |
| print(f" Title: {title}") | |
| plt.close(fig) | |
| def run_basic_knowledge_graph(): | |
| """Run basic knowledge graph creation and visualization""" | |
| print("\n๐ฌ Running Basic Knowledge Graph Analysis") | |
| print("=" * 50) | |
| # Create sample knowledge graph | |
| kg = create_sample_kg() | |
| kg.print_stats() | |
| # Visualize basic KG | |
| fig = plt.figure(figsize=(15, 10)) | |
| kg.visualize(max_entities=15, layout="spring") | |
| plt.title("๐ฌ Basic Knowledge Graph Visualization\nScientists and Their Discoveries", | |
| fontsize=14, fontweight='bold') | |
| plt.tight_layout() | |
| save_figure(fig, "basic_knowledge_graph.png", "Basic Knowledge Graph") | |
| return kg | |
| def run_advanced_scientific_kg(): | |
| """Run advanced scientific knowledge graph analysis""" | |
| print("\n๐งช Running Advanced Scientific Knowledge Graph Analysis") | |
| print("=" * 60) | |
| # Create advanced scientific KG | |
| scientific_kg = create_advanced_scientific_kg() | |
| # Print statistics | |
| print(f"Advanced Scientific KG Statistics:") | |
| print(f" โข Total Entities: {len(scientific_kg.entities)}") | |
| print(f" โข Scientific Entities: {len(scientific_kg.scientific_entities)}") | |
| print(f" โข Total Triples: {len(scientific_kg.triples)}") | |
| # Analyze temporal patterns | |
| temporal_data = scientific_kg.analyze_temporal_patterns() | |
| print(f" โข Temporal Events: {len(temporal_data['years'])} years") | |
| # Calculate network metrics | |
| metrics = scientific_kg.calculate_network_metrics() | |
| print(f" โข Influence Network Nodes: {len(metrics.get('influence', {}).get('centrality', {}))}") | |
| print(f" โข Collaboration Network Nodes: {len(metrics.get('collaboration', {}).get('centrality', {}))}") | |
| # Find influential entities | |
| influential = scientific_kg.find_influential_entities(5) | |
| print(f"\n๐ Top 5 Most Influential Entities:") | |
| for i, (entity_id, score) in enumerate(influential, 1): | |
| entity_name = scientific_kg.entities[entity_id].name | |
| print(f" {i}. {entity_name} (Score: {score:.3f})") | |
| # Detect collaboration clusters | |
| clusters = scientific_kg.detect_collaboration_clusters() | |
| print(f"\n๐ค Collaboration Clusters: {len(clusters)}") | |
| for i, cluster in enumerate(clusters[:3], 1): # Show top 3 clusters | |
| cluster_names = [scientific_kg.entities[eid].name for eid in cluster] | |
| print(f" Cluster {i}: {', '.join(cluster_names)}") | |
| # Visualize advanced network | |
| scientific_kg.visualize_advanced_network(layout="spring", max_entities=30) | |
| fig = plt.gcf() | |
| save_figure(fig, "advanced_scientific_network.png", "Advanced Scientific Network Analysis") | |
| # Export analysis | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| analysis_file = scientific_kg.export_analysis(f"results/scientific_kg_analysis_{timestamp}.json") | |
| return scientific_kg | |
| def run_embedding_models(): | |
| """Run knowledge graph embedding models""" | |
| print("\n๐ง Running Knowledge Graph Embedding Models") | |
| print("=" * 50) | |
| # Create knowledge graph for embeddings | |
| kg = create_sample_kg() | |
| # Test different embedding models | |
| models = { | |
| 'TransE': TransE(len(kg.entities), len(kg.relations), embedding_dim=50), | |
| 'DistMult': DistMult(len(kg.entities), len(kg.relations), embedding_dim=50), | |
| 'ComplEx': ComplEx(len(kg.entities), len(kg.relations), embedding_dim=50) | |
| } | |
| results = {} | |
| for model_name, model in models.items(): | |
| print(f"\n๐ Training {model_name} model...") | |
| trainer = KGEmbeddingTrainer(model, kg) | |
| # Train the model | |
| losses = trainer.train(epochs=50, lr=0.001, batch_size=64) | |
| # Evaluate the model | |
| eval_results = trainer.evaluate_link_prediction(k=3) | |
| results[model_name] = eval_results | |
| print(f" Hits@3: {eval_results['hits@3']:.3f}") | |
| print(f" Mean Rank: {eval_results['mean_rank']:.2f}") | |
| # Visualize embeddings | |
| embeddings_2d = trainer.visualize_embeddings(method="pca", max_entities=30) | |
| fig = plt.gcf() | |
| save_figure(fig, f"{model_name.lower()}_embeddings.png", f"{model_name} Entity Embeddings") | |
| # Find similar entities | |
| if len(kg.entities) > 0: | |
| sample_entity = list(kg.entities.keys())[0] | |
| similar = trainer.find_similar_entities(sample_entity, k=5) | |
| print(f" Similar to '{kg.entities[sample_entity].name}':") | |
| for entity_id, similarity in similar: | |
| entity_name = kg.entities[entity_id].name | |
| print(f" โข {entity_name} (similarity: {similarity:.3f})") | |
| # Compare models | |
| print(f"\n๐ Model Comparison:") | |
| for model_name, result in results.items(): | |
| print(f" {model_name}: Hits@3={result['hits@3']:.3f}, Mean Rank={result['mean_rank']:.2f}") | |
| return results | |
| def run_reasoning_examples(): | |
| """Run knowledge graph reasoning examples""" | |
| print("\n๐ Running Knowledge Graph Reasoning Examples") | |
| print("=" * 50) | |
| kg = create_sample_kg() | |
| # Example queries | |
| queries = [ | |
| ("Find all entities related to Einstein", lambda: kg.query_triples(subject="einstein")), | |
| ("Find all discoveries", lambda: kg.query_triples(predicate="discovered")), | |
| ("Find all physics concepts", lambda: [t for t in kg.triples if t.object in ["relativity", "gravity"]]), | |
| ] | |
| for query_name, query_func in queries: | |
| print(f"\n๐ {query_name}:") | |
| results = query_func() | |
| for result in results[:5]: # Show first 5 results | |
| print(f" โข {result}") | |
| if len(results) > 5: | |
| print(f" ... and {len(results) - 5} more results") | |
| # Subgraph extraction | |
| print(f"\n๐ Subgraph Analysis:") | |
| einstein_neighbors = kg.get_neighbors("einstein") | |
| if einstein_neighbors: | |
| subgraph = kg.get_subgraph(list(einstein_neighbors), max_hops=1) | |
| print(f" Einstein's subgraph: {len(subgraph.entities)} entities, {len(subgraph.triples)} triples") | |
| # Visualize subgraph | |
| fig = plt.figure(figsize=(12, 8)) | |
| subgraph.visualize(max_entities=10, layout="spring") | |
| plt.title("Einstein's Knowledge Subgraph", fontsize=14, fontweight='bold') | |
| plt.tight_layout() | |
| save_figure(fig, "einstein_subgraph.png", "Einstein's Knowledge Subgraph") | |
| def generate_summary_report(): | |
| """Generate a summary report""" | |
| print("\n๐ Generating Summary Report") | |
| print("=" * 30) | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| report_content = f""" | |
| # CA10 Knowledge Graphs - Execution Report | |
| Generated: {timestamp} | |
| ## Project Overview | |
| This report summarizes the execution of the CA10 Knowledge Graphs project, including: | |
| - Basic Knowledge Graph creation and visualization | |
| - Advanced Scientific Knowledge Graph analysis | |
| - Knowledge Graph Embedding models (TransE, DistMult, ComplEx) | |
| - Reasoning examples and subgraph analysis | |
| ## Generated Files | |
| - Basic Knowledge Graph visualization: basic_knowledge_graph.png | |
| - Advanced Scientific Network analysis: advanced_scientific_network.png | |
| - TransE embeddings: transe_embeddings.png | |
| - DistMult embeddings: distmult_embeddings.png | |
| - ComplEx embeddings: complex_embeddings.png | |
| - Einstein's subgraph: einstein_subgraph.png | |
| ## Key Findings | |
| - Successfully created and analyzed multiple knowledge graph structures | |
| - Implemented and trained three different embedding models | |
| - Performed reasoning queries and subgraph extraction | |
| - Generated comprehensive visualizations for analysis | |
| ## Technical Details | |
| - Python environment with PyTorch, NetworkX, and visualization libraries | |
| - All visualizations saved in high resolution (300 DPI) | |
| - Results exported in JSON format for further analysis | |
| """ | |
| report_file = f"results/execution_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md" | |
| with open(report_file, 'w') as f: | |
| f.write(report_content) | |
| print(f"๐ Report saved to: {report_file}") | |
| def main(): | |
| """Main execution function""" | |
| print("๐ฏ CA10 Knowledge Graphs - Complete Execution") | |
| print("=" * 50) | |
| try: | |
| # Setup | |
| setup_directories() | |
| # Run all components | |
| basic_kg = run_basic_knowledge_graph() | |
| advanced_kg = run_advanced_scientific_kg() | |
| embedding_results = run_embedding_models() | |
| run_reasoning_examples() | |
| # Generate summary | |
| generate_summary_report() | |
| print("\nโ All components executed successfully!") | |
| print("=" * 50) | |
| print("๐ Check the following directories for outputs:") | |
| print(" โข visualization/ - All generated plots and figures") | |
| print(" โข results/ - Analysis results and reports") | |
| print(" โข logs/ - Execution logs") | |
| except Exception as e: | |
| print(f"\nโ Error during execution: {str(e)}") | |
| import traceback | |
| traceback.print_exc() | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 10.5 kB
- Xet hash:
- 038e22441186e2965ddbddb525db930891076846600545913841ed88666c1839
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.