{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/semantica-agi/semantica/blob/main/cookbook/introduction/16_Visualization.ipynb)\n", "\n", "# Visualization\n", "\n", "## Overview\n", "\n", "This notebook demonstrates how to visualize knowledge graphs, ontologies, and embeddings using Semantica's visualization modules. You'll learn to use `KGVisualizer`, `OntologyVisualizer`, and `EmbeddingVisualizer`.\n", "\n", "**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/visualization/)\n", "\n", "### Learning Objectives\n", "\n", "- Use `KGVisualizer` to visualize knowledge graphs\n", "- Use `OntologyVisualizer` to visualize ontologies\n", "- Use `EmbeddingVisualizer` to visualize embeddings\n", "\n", "## Installation\n", "\n", "Install Semantica from PyPI:\n", "\n", "```bash\n", "pip install semantica\n", "# Or with all optional dependencies:\n", "pip install semantica[all]\n", "```\n", "\n", "---\n", "\n", "## Step 1: Knowledge Graph Visualization\n", "\n", "Visualize knowledge graphs.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -q semantica" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.visualization import KGVisualizer\n", "from semantica.kg import GraphBuilder\n", "\n", "kg_visualizer = KGVisualizer()\n", "builder = GraphBuilder()\n", "\n", "entities = [\n", " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}},\n", " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Tim Cook\", \"properties\": {}}\n", "]\n", "\n", "relationships = [\n", " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"CEO_of\", \"properties\": {}}\n", "]\n", "\n", "kg = builder.build(entities, relationships)\n", "\n", "visualization = kg_visualizer.visualize_network(kg, output=\"interactive\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Ontology Visualization\n", "\n", "Visualize ontologies.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.visualization import OntologyVisualizer\n", "from semantica.ontology import OntologyGenerator\n", "\n", "ontology_visualizer = OntologyVisualizer()\n", "# Initialize generator with min_occurrences=1 to allow single-instance classes\n", "generator = OntologyGenerator(min_occurrences=1)\n", "\n", "# Generate ontology using the correct method signature (dictionary input)\n", "ontology = generator.generate_ontology({\"entities\": entities, \"relationships\": relationships})\n", "\n", "# Visualize the hierarchy\n", "visualization = ontology_visualizer.visualize_hierarchy(ontology, output=\"interactive\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Embedding Visualization\n", "\n", "Visualize embeddings.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.visualization import EmbeddingVisualizer\n", "from semantica.embeddings import EmbeddingGenerator\n", "import numpy as np\n", "\n", "embedding_visualizer = EmbeddingVisualizer()\n", "generator = EmbeddingGenerator()\n", "\n", "texts = [\"Apple Inc.\", \"Microsoft Corporation\", \"Amazon\"]\n", "embeddings = generator.generate_embeddings(texts, data_type=\"text\")\n", "labels = [\"Apple\", \"Microsoft\", \"Amazon\"]\n", "\n", "visualization = embedding_visualizer.visualize_2d_projection(embeddings, labels, method=\"umap\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Semantic Network Visualization\n", "\n", "Visualize semantic networks: structure, node types, and edge types." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.visualization import SemanticNetworkVisualizer\n", "\n", "# Initialize (uses new defaults: Vibrant colors, Kamada-Kawai layout)\n", "viz = SemanticNetworkVisualizer()\n", "\n", "# Your semantic network data\n", "semantic_network = {\n", " \"nodes\": [\n", " {\"id\": \"n1\", \"label\": \"Python\", \"type\": \"Language\"},\n", " {\"id\": \"n2\", \"label\": \"Code\", \"type\": \"Concept\"}\n", " ],\n", " \"edges\": [\n", " {\"source\": \"n1\", \"target\": \"n2\", \"label\": \"writes\"}\n", " ]\n", "}\n", "\n", "# This will now display the interactive graph in the notebook cell\n", "viz.visualize_network(\n", " semantic_network, \n", " output=\"html\", \n", " file_path=\"network_graph.html\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "You've learned how to visualize data:\n", "\n", "- **KGVisualizer**: Visualize knowledge graphs\n", "- **OntologyVisualizer**: Visualize ontologies\n", "- **EmbeddingVisualizer**: Visualize embeddings, multi-modal\n", "- **SemanticNetworkVisualizer**: Visualize semantic network structure and type distributions\n", "\n", "Next: Learn how to detect conflicts in the Conflict_Detection notebook.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }