Files

6.1 KiB

Open In Colab

Visualization

Overview

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.

Documentation: API Reference

Learning Objectives

  • Use KGVisualizer to visualize knowledge graphs
  • Use OntologyVisualizer to visualize ontologies
  • Use EmbeddingVisualizer to visualize embeddings

Installation

Install Semantica from PyPI:

pip install semantica
# Or with all optional dependencies:
pip install semantica[all]

Step 1: Knowledge Graph Visualization

Visualize knowledge graphs.

In [ ]:
!pip install -q semantica
In [ ]:
from semantica.visualization import KGVisualizer
from semantica.kg import GraphBuilder

kg_visualizer = KGVisualizer()
builder = GraphBuilder()

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

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

kg = builder.build(entities, relationships)

visualization = kg_visualizer.visualize_network(kg, output="interactive")

Step 2: Ontology Visualization

Visualize ontologies.

In [ ]:
from semantica.visualization import OntologyVisualizer
from semantica.ontology import OntologyGenerator

ontology_visualizer = OntologyVisualizer()
# Initialize generator with min_occurrences=1 to allow single-instance classes
generator = OntologyGenerator(min_occurrences=1)

# Generate ontology using the correct method signature (dictionary input)
ontology = generator.generate_ontology({"entities": entities, "relationships": relationships})

# Visualize the hierarchy
visualization = ontology_visualizer.visualize_hierarchy(ontology, output="interactive")

Step 3: Embedding Visualization

Visualize embeddings.

In [ ]:
from semantica.visualization import EmbeddingVisualizer
from semantica.embeddings import EmbeddingGenerator
import numpy as np

embedding_visualizer = EmbeddingVisualizer()
generator = EmbeddingGenerator()

texts = ["Apple Inc.", "Microsoft Corporation", "Amazon"]
embeddings = generator.generate_embeddings(texts, data_type="text")
labels = ["Apple", "Microsoft", "Amazon"]

visualization = embedding_visualizer.visualize_2d_projection(embeddings, labels, method="umap")

Step 4: Semantic Network Visualization

Visualize semantic networks: structure, node types, and edge types.

In [ ]:
from semantica.visualization import SemanticNetworkVisualizer

# Initialize (uses new defaults: Vibrant colors, Kamada-Kawai layout)
viz = SemanticNetworkVisualizer()

# Your semantic network data
semantic_network = {
    "nodes": [
        {"id": "n1", "label": "Python", "type": "Language"},
        {"id": "n2", "label": "Code", "type": "Concept"}
    ],
    "edges": [
        {"source": "n1", "target": "n2", "label": "writes"}
    ]
}

# This will now display the interactive graph in the notebook cell
viz.visualize_network(
    semantic_network, 
    output="html", 
    file_path="network_graph.html"
)

Summary

You've learned how to visualize data:

  • KGVisualizer: Visualize knowledge graphs
  • OntologyVisualizer: Visualize ontologies
  • EmbeddingVisualizer: Visualize embeddings, multi-modal
  • SemanticNetworkVisualizer: Visualize semantic network structure and type distributions

Next: Learn how to detect conflicts in the Conflict_Detection notebook.