{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/17_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": [
    "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",
    "generator = OntologyGenerator()\n",
    "\n",
    "ontology = generator.generate(entities, relationships)\n",
    "\n",
    "visualization = ontology_visualizer.visualize_hierarchy(ontology, output=\"interactive\")\n"
   ]
  },
  {
   "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",
    "semantic_network = {\n",
    "    \"nodes\": [\n",
    "        {\"id\": \"n1\", \"label\": \"Node 1\", \"type\": \"Entity\"},\n",
    "        {\"id\": \"n2\", \"label\": \"Node 2\", \"type\": \"Entity\"}\n",
    "    ],\n",
    "    \"edges\": [\n",
    "        {\"source\": \"n1\", \"target\": \"n2\", \"label\": \"related_to\"}\n",
    "    ]\n",
    "}\n",
    "\n",
    "sem_viz = SemanticNetworkVisualizer()\n",
    "sem_viz.visualize_network(semantic_network, output=\"interactive\")\n",
    "sem_viz.visualize_node_types(semantic_network, output=\"interactive\")\n",
    "sem_viz.visualize_edge_types(semantic_network, output=\"interactive\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Advanced Embedding Visualization\n",
    "\n",
    "Compare multi-modal embeddings and assess embedding quality."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from semantica.visualization import EmbeddingVisualizer\n",
    "\n",
    "# Synthetic multi-modal embeddings (text, image, audio)\n",
    "text_emb = np.random.rand(50, 128)\n",
    "image_emb = np.random.rand(50, 128)\n",
    "audio_emb = np.random.rand(50, 128)\n",
    "\n",
    "emb_viz = EmbeddingVisualizer()\n",
    "emb_viz.visualize_multimodal_comparison(text_emb, image_emb, audio_emb, output=\"interactive\")\n",
    "\n",
    "# Embedding quality metrics\n",
    "quality_fig = emb_viz.visualize_quality_metrics(text_emb, output=\"interactive\")\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 and quality metrics\n",
    "- **SemanticNetworkVisualizer**: Visualize semantic network structure and type distributions\n",
    "\n",
    "Next: Learn how to detect conflicts in the Conflict_Detection notebook.\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
