diff --git a/notebooks/cognee_demo.ipynb b/notebooks/cognee_demo.ipynb index f95a082cd..312ca0e65 100644 --- a/notebooks/cognee_demo.ipynb +++ b/notebooks/cognee_demo.ipynb @@ -794,14 +794,7 @@ "from cognee.shared.utils import render_graph\n", "from cognee.infrastructure.databases.graph import get_graph_engine\n", "import graphistry\n", - "from dotenv import load_dotenv \n", - "\n", - "load_dotenv()\n", - "os.environ[\"GRAPHISTRY_USERNAME\"] = \"vasilije\"\n", - "os.environ[\"GRAPHISTRY_PASSWORD\"] = \"wV!4yzNGjDLCpwV\"\n", - "\n", - "\n", - "\n", + "# from dotenv import load_dotenv \n", "graphistry.login(username=os.getenv(\"GRAPHISTRY_USERNAME\"), password=os.getenv(\"GRAPHISTRY_PASSWORD\"))\n", "\n", "graph_engine = await get_graph_engine()\n", @@ -821,683 +814,6 @@ ], "execution_count": 12 }, - { - "metadata": { - "ExecuteTime": { - "end_time": "2024-12-29T17:27:20.635684Z", - "start_time": "2024-12-29T17:27:18.253572Z" - } - }, - "cell_type": "code", - "source": "!pip install cairosvg", - "id": "1995cab3f32a393b", - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Collecting cairosvg\r\n", - " Obtaining dependency information for cairosvg from https://files.pythonhosted.org/packages/01/a5/1866b42151f50453f1a0d28fc4c39f5be5f412a2e914f33449c42daafdf1/CairoSVG-2.7.1-py3-none-any.whl.metadata\r\n", - " Downloading CairoSVG-2.7.1-py3-none-any.whl.metadata (2.7 kB)\r\n", - "Collecting cairocffi (from cairosvg)\r\n", - " Obtaining dependency information for cairocffi from https://files.pythonhosted.org/packages/93/d8/ba13451aa6b745c49536e87b6bf8f629b950e84bd0e8308f7dc6883b67e2/cairocffi-1.7.1-py3-none-any.whl.metadata\r\n", - " Downloading cairocffi-1.7.1-py3-none-any.whl.metadata (3.3 kB)\r\n", - "Collecting cssselect2 (from cairosvg)\r\n", - " Obtaining dependency information for cssselect2 from https://files.pythonhosted.org/packages/9d/3a/e39436efe51894243ff145a37c4f9a030839b97779ebcc4f13b3ba21c54e/cssselect2-0.7.0-py3-none-any.whl.metadata\r\n", - " Downloading cssselect2-0.7.0-py3-none-any.whl.metadata (2.9 kB)\r\n", - "Requirement already satisfied: defusedxml in /Users/vasilije/cognee/.venv/lib/python3.11/site-packages (from cairosvg) (0.7.1)\r\n", - "Requirement already satisfied: pillow in /Users/vasilije/cognee/.venv/lib/python3.11/site-packages (from cairosvg) (11.0.0)\r\n", - "Requirement already satisfied: tinycss2 in /Users/vasilije/cognee/.venv/lib/python3.11/site-packages (from cairosvg) (1.4.0)\r\n", - "Requirement already satisfied: cffi>=1.1.0 in /Users/vasilije/cognee/.venv/lib/python3.11/site-packages (from cairocffi->cairosvg) (1.17.1)\r\n", - "Requirement already satisfied: webencodings in /Users/vasilije/cognee/.venv/lib/python3.11/site-packages (from cssselect2->cairosvg) (0.5.1)\r\n", - "Requirement already satisfied: pycparser in /Users/vasilije/cognee/.venv/lib/python3.11/site-packages (from cffi>=1.1.0->cairocffi->cairosvg) (2.22)\r\n", - "Downloading CairoSVG-2.7.1-py3-none-any.whl (43 kB)\r\n", - "\u001B[2K \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m43.2/43.2 kB\u001B[0m \u001B[31m1.6 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n", - "\u001B[?25hDownloading cairocffi-1.7.1-py3-none-any.whl (75 kB)\r\n", - "\u001B[2K \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m75.6/75.6 kB\u001B[0m \u001B[31m4.3 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n", - "\u001B[?25hDownloading cssselect2-0.7.0-py3-none-any.whl (15 kB)\r\n", - "Installing collected packages: cssselect2, cairocffi, cairosvg\r\n", - "Successfully installed cairocffi-1.7.1 cairosvg-2.7.1 cssselect2-0.7.0\r\n", - "\r\n", - "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip is available: \u001B[0m\u001B[31;49m23.2.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m24.3.1\u001B[0m\r\n", - "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip install --upgrade pip\u001B[0m\r\n" - ] - } - ], - "execution_count": 25 - }, - { - "metadata": { - "ExecuteTime": { - "end_time": "2024-12-29T22:05:06.689621Z", - "start_time": "2024-12-29T22:05:06.688268Z" - } - }, - "cell_type": "code", - "source": "\n", - "id": "7014c8acf720b50", - "outputs": [], - "execution_count": 70 - }, - { - "metadata": { - "ExecuteTime": { - "end_time": "2024-12-29T22:27:10.842044Z", - "start_time": "2024-12-29T22:27:10.826453Z" - } - }, - "cell_type": "code", - "source": [ - "from PIL import Image\n", - "import potrace\n", - "\n", - "def png_to_svg(png_path, threshold=128):\n", - " \"\"\"\n", - " Converts a PNG image to an SVG string using potrace and prints the output.\n", - "\n", - " :param png_path: Path to the input PNG file.\n", - " :param threshold: Grayscale threshold for converting to black-and-white (default: 128).\n", - " :return: The SVG content as a string.\n", - " \"\"\"\n", - " # Load the PNG image and convert to grayscale\n", - " img = Image.open(png_path).convert(\"L\")\n", - "\n", - " # Convert image to black-and-white\n", - " img = img.point(lambda p: p > threshold and 255)\n", - "\n", - " # Convert the image to a bitmap for potrace\n", - " bitmap = potrace.Bitmap(img)\n", - "\n", - " # Trace the bitmap into a path\n", - " path = bitmap.trace()\n", - "\n", - " # Generate SVG output\n", - " svg_content = '\\n'\n", - " svg_content += '\\n'\n", - " for curve in path:\n", - " svg_content += f' \\n'\n", - " svg_content += '\\n'\n", - "\n", - " # Print the SVG content\n", - " print(svg_content)\n", - "\n", - " # Return the SVG content as a string\n", - " return svg_content\n" - ], - "id": "494ff0eafc9892db", - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'potrace'", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mModuleNotFoundError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[87], line 2\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mPIL\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Image\n\u001B[0;32m----> 2\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpotrace\u001B[39;00m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mpng_to_svg\u001B[39m(png_path, threshold\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m128\u001B[39m):\n\u001B[1;32m 5\u001B[0m \u001B[38;5;250m \u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m 6\u001B[0m \u001B[38;5;124;03m Converts a PNG image to an SVG string using potrace and prints the output.\u001B[39;00m\n\u001B[1;32m 7\u001B[0m \n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 10\u001B[0m \u001B[38;5;124;03m :return: The SVG content as a string.\u001B[39;00m\n\u001B[1;32m 11\u001B[0m \u001B[38;5;124;03m \"\"\"\u001B[39;00m\n", - "\u001B[0;31mModuleNotFoundError\u001B[0m: No module named 'potrace'" - ] - } - ], - "execution_count": 87 - }, - { - "metadata": { - "ExecuteTime": { - "end_time": "2024-12-29T22:27:11.546173Z", - "start_time": "2024-12-29T22:27:11.533929Z" - } - }, - "cell_type": "code", - "source": [ - "import os\n", - "from typing import BinaryIO, Union\n", - "\n", - "import requests\n", - "import hashlib\n", - "from datetime import datetime, timezone\n", - "import graphistry\n", - "import networkx as nx\n", - "import numpy as np\n", - "import pandas as pd\n", - "import matplotlib.pyplot as plt\n", - "import tiktoken\n", - "import nltk\n", - "import base64\n", - "import networkx as nx\n", - "from bokeh.io import output_file, save\n", - "from bokeh.plotting import figure, from_networkx\n", - "from bokeh.models import Circle, MultiLine, HoverTool, ColumnDataSource, Range1d\n", - "\n", - "\n", - "from cognee.base_config import get_base_config\n", - "from cognee.infrastructure.databases.graph import get_graph_engine\n", - "\n", - "from uuid import uuid4\n", - "import pathlib\n", - "\n", - "from cognee.shared.exceptions import IngestionError\n", - "\n", - "\n", - "async def create_cognee_style_network_with_logo(\n", - " G,\n", - " output_filename: str = \"cognee_network_with_logo.html\",\n", - " title: str = \"Cognee-Style Network\",\n", - " node_attribute: str = \"group\",\n", - " layout_func=nx.spring_layout,\n", - " layout_scale: float = 3.0,\n", - " logo_alpha: float = 0.1, # Transparency of the logo\n", - "):\n", - " \"\"\"\n", - " Create a Cognee-inspired network visualization with an embedded logo as a watermark.\n", - "\n", - " :param G: Graph data.\n", - " :param output_filename: The HTML file where the visualization will be saved.\n", - " :param title: Title of the visualization.\n", - " :param node_attribute: Node attribute used to color the nodes.\n", - " :param layout_func: Layout function from NetworkX (e.g., nx.spring_layout).\n", - " :param layout_scale: Scale of the layout.\n", - " :param logo_alpha: Transparency of the logo (0 = fully transparent, 1 = fully opaque).\n", - " \"\"\"\n", - "\n", - " # Ensure every node has the specified attribute; otherwise \"Unknown\"\n", - " \n", - " (nodes, edges) = G\n", - " networkx_graph = nx.MultiDiGraph()\n", - "\n", - " networkx_graph.add_nodes_from(nodes)\n", - " networkx_graph.add_edges_from(edges)\n", - "\n", - " G = networkx_graph\n", - " new_G = nx.MultiDiGraph() if isinstance(G, nx.MultiDiGraph) else nx.Graph()\n", - " \n", - " # Convert nodes and their attributes\n", - " for node, data in G.nodes(data=True):\n", - " print(data.items())\n", - " import uuid\n", - " serializable_data = {k: str(v) if isinstance(v, uuid.UUID) else v for k, v in data.items()}\n", - " new_G.add_node(str(node), **serializable_data)\n", - " \n", - " # Convert edges and their attributes\n", - " for u, v, data in G.edges(data=True):\n", - " import uuid\n", - " serializable_data = {k: str(v) if isinstance(v, uuid.UUID) else v for k, v in data.items()}\n", - " new_G.add_edge(str(u), str(v), **serializable_data)\n", - " \n", - " G = new_G # Use the new graph\n", - "\n", - " # Prepare Bokeh output\n", - " output_file(output_filename)\n", - "\n", - " # Create a figure with light gray background\n", - " p = figure(\n", - " title=title,\n", - " tools=\"pan,wheel_zoom,save,reset,hover\",\n", - " active_scroll=\"wheel_zoom\",\n", - " width=1200,\n", - " height=900,\n", - " background_fill_color=\"#F4F4F4\", # Light Gray\n", - " toolbar_location=\"below\",\n", - " x_range=Range1d(-layout_scale, layout_scale),\n", - " y_range=Range1d(-layout_scale, layout_scale),\n", - " )\n", - " p.toolbar.logo = None\n", - " p.axis.visible = False\n", - " p.grid.visible = False\n", - " p.outline_line_color = None\n", - "\n", - " import cairosvg\n", - " \n", - " svg_logo =\"\"\"\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \"\"\"\n", - "\n", - "\n", - " png_data = cairosvg.svg2png(bytestring=svg_logo.encode(\"utf-8\"))\n", - " logo_base64 = base64.b64encode(png_data).decode(\"utf-8\")\n", - " logo_url = f\"data:image/png;base64,{logo_base64}\"\n", - "\n", - " # Add the logo as an image at the center of the graph\n", - " # Position the logo behind the graph using CSS layering\n", - " p.image_url(\n", - " url=[logo_url],\n", - " x=-layout_scale * 0.5, # Adjusted position to center\n", - " y=layout_scale * 0.5, # Adjusted position to center\n", - " w=layout_scale * 1.2, # Width of the logo\n", - " h=layout_scale * 1.2, # Height of the logo\n", - " anchor=\"center\",\n", - " global_alpha=logo_alpha, # Transparency for watermark effect\n", - " )\n", - " p.image_url(\n", - " url=[logo_url],\n", - " x=-layout_scale * -0.5, # Adjusted position to center\n", - " y=layout_scale * -0.5, # Adjusted position to center\n", - " w=layout_scale * 1.2, # Width of the logo\n", - " h=layout_scale * 1.2, # Height of the logo\n", - " anchor=\"center\",\n", - " global_alpha=logo_alpha, # Transparency for watermark effect\n", - " )\n", - "\n", - " # Generate graph layout\n", - " layout_positions = {str(node): position for node, position in layout_func(G).items()}\n", - "\n", - " graph_renderer = from_networkx(\n", - " G,\n", - " layout_positions,\n", - " scale=layout_scale,\n", - " center=(0, 0),\n", - " )\n", - "\n", - " # Compute node sizes based on centrality\n", - " centrality = nx.degree_centrality(G)\n", - " node_radii = [0.02 + 0.1 * centrality[node] for node in G.nodes()]\n", - " graph_renderer.node_renderer.data_source.data[\"radius\"] = node_radii\n", - "\n", - " # Apply Cognee-inspired colors for nodes\n", - " cognee_colors = [\"#6510F4\", \"#0DFF00\", \"#FFFFFF\"] # Violet, Green, White\n", - " unique_attrs = list({\n", - " G.nodes[node].get(node_attribute, G.nodes[node].get('id', 'Unknown')) \n", - " for node in G.nodes()\n", - " })\n", - " color_map = {attr: cognee_colors[i % len(cognee_colors)] for i, attr in enumerate(unique_attrs)}\n", - " node_colors = [\n", - " color_map[G.nodes[node].get(node_attribute, G.nodes[node].get('id', 'Unknown'))] \n", - " for node in G.nodes()\n", - "]\n", - "\n", - " graph_renderer.node_renderer.data_source.data[\"fill_color\"] = node_colors\n", - "\n", - " # Style nodes\n", - " graph_renderer.node_renderer.glyph = Circle(\n", - " radius=\"radius\",\n", - " fill_color=\"fill_color\",\n", - " fill_alpha=0.9,\n", - " line_color=\"#000000\", # Abyss Black outline\n", - " line_width=1.5,\n", - " )\n", - " graph_renderer.node_renderer.hover_glyph = Circle(\n", - " radius=\"radius\",\n", - " fill_color=\"#FFFFFF\", # White fill on hover\n", - " fill_alpha=1.0,\n", - " line_color=\"#6510F4\", # Violet outline on hover\n", - " line_width=2.5,\n", - " )\n", - "\n", - " # Style edges\n", - " graph_renderer.edge_renderer.glyph = MultiLine(\n", - " line_color=\"#000000\", # Abyss Black edges\n", - " line_alpha=0.3,\n", - " line_width=1.5,\n", - " )\n", - " graph_renderer.edge_renderer.hover_glyph = MultiLine(\n", - " line_color=\"#0DFF00\", # Green on hover\n", - " line_alpha=0.8,\n", - " line_width=2.0,\n", - " )\n", - "\n", - " # Hover tool for node tooltips\n", - " graph_renderer.node_renderer.data_source.data[node_attribute] = [\n", - " G.nodes[node].get(node_attribute, G.nodes[node].get('id', 'Unknown')) \n", - " for node in G.nodes()\n", - " ]\n", - " hover_tool = HoverTool(\n", - " tooltips=[\n", - " (\"Node\", \"@index\"),\n", - " (node_attribute.capitalize(), f\"@{node_attribute}\"),\n", - " (\"Centrality\", \"@radius{0.00}\"),\n", - " ],\n", - " renderers=[graph_renderer],\n", - " )\n", - " p.add_tools(hover_tool)\n", - "\n", - " # Add the graph to the plot\n", - " p.renderers.append(graph_renderer)\n", - " from bokeh.io import output_notebook, show\n", - "\n", - " # Save the result\n", - " output_notebook()\n", - "\n", - " # Display the plot in the notebook\n", - " show(p)\n", - " print(f\"Cognee-style network with logo saved to '{output_filename}'\")\n", - " from bokeh.embed import file_html\n", - " from bokeh.resources import CDN\n", - " \n", - " html_content = file_html(p, CDN, title=\"Cognee-style Network\")\n", - "\n", - " # Save HTML to file if needed\n", - " with open(output_filename, \"w\") as f:\n", - " f.write(html_content)\n", - " print(f\"HTML plot saved to '{output_filename}'\")\n", - "\n", - " # Return the HTML content as a string\n", - " return html_content\n" - ], - "id": "296c8298f25d53d8", - "outputs": [], - "execution_count": 88 - }, - { - "metadata": { - "ExecuteTime": { - "end_time": "2024-12-30T09:50:38.625634Z", - "start_time": "2024-12-30T09:50:38.614895Z" - } - }, - "cell_type": "code", - "source": [ - "import networkx as nx\n", - "from bokeh.plotting import figure, output_file, show\n", - "from bokeh.models import Circle, MultiLine, HoverTool, Range1d\n", - "from bokeh.io import output_notebook\n", - "from bokeh.embed import file_html\n", - "from bokeh.resources import CDN\n", - "from bokeh.plotting import figure, from_networkx\n", - "import base64\n", - "import cairosvg\n", - "import logging\n", - "\n", - "logging.basicConfig(level=logging.INFO)\n", - "\n", - "def convert_to_serializable_graph(G):\n", - " \"\"\"\n", - " Convert a graph into a serializable format with stringified node and edge attributes.\n", - " \"\"\"\n", - " (nodes, edges) = G\n", - " networkx_graph = nx.MultiDiGraph()\n", - "\n", - " networkx_graph.add_nodes_from(nodes)\n", - " networkx_graph.add_edges_from(edges)\n", - " G = networkx_graph\n", - " new_G = nx.MultiDiGraph() if isinstance(G, nx.MultiDiGraph) else nx.Graph()\n", - " for node, data in G.nodes(data=True):\n", - " serializable_data = {k: str(v) for k, v in data.items()}\n", - " new_G.add_node(str(node), **serializable_data)\n", - " for u, v, data in G.edges(data=True):\n", - " serializable_data = {k: str(v) for k, v in data.items()}\n", - " new_G.add_edge(str(u), str(v), **serializable_data)\n", - " return new_G\n", - "\n", - "def generate_layout_positions(G, layout_func, layout_scale):\n", - " \"\"\"\n", - " Generate layout positions for the graph using the specified layout function.\n", - " \"\"\"\n", - " positions = layout_func(G)\n", - " return {str(node): (x * layout_scale, y * layout_scale) for node, (x, y) in positions.items()}\n", - "\n", - "def assign_node_colors(G, node_attribute, palette):\n", - " \"\"\"\n", - " Assign colors to nodes based on a specified attribute and a given palette.\n", - " \"\"\"\n", - " unique_attrs = set(G.nodes[node].get(node_attribute, \"Unknown\") for node in G.nodes)\n", - " color_map = {attr: palette[i % len(palette)] for i, attr in enumerate(unique_attrs)}\n", - " return [color_map[G.nodes[node].get(node_attribute, \"Unknown\")] for node in G.nodes], color_map\n", - "\n", - "def embed_logo(p, layout_scale, logo_alpha):\n", - " \"\"\"\n", - " Embed a logo into the graph visualization as a watermark.\n", - " \"\"\"\n", - " svg_logo=\"\"\"\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \"\"\" # Add your SVG content here\n", - " png_data = cairosvg.svg2png(bytestring=svg_logo.encode(\"utf-8\"))\n", - " logo_base64 = base64.b64encode(png_data).decode(\"utf-8\")\n", - " logo_url = f\"data:image/png;base64,{logo_base64}\"\n", - " p.image_url(\n", - " url=[logo_url],\n", - " x=-layout_scale * 0.5,\n", - " y=layout_scale * 0.5,\n", - " w=layout_scale,\n", - " h=layout_scale,\n", - " anchor=\"center\",\n", - " global_alpha=logo_alpha,\n", - " )\n", - "\n", - "def style_and_render_graph(p, G, layout_positions, node_attribute, node_colors, centrality):\n", - " \"\"\"\n", - " Apply styling and render the graph into the plot.\n", - " \"\"\"\n", - " graph_renderer = from_networkx(G, layout_positions)\n", - " node_radii = [0.02 + 0.1 * centrality[node] for node in G.nodes()]\n", - " graph_renderer.node_renderer.data_source.data[\"radius\"] = node_radii\n", - " graph_renderer.node_renderer.data_source.data[\"fill_color\"] = node_colors\n", - " graph_renderer.node_renderer.glyph = Circle(\n", - " radius=\"radius\",\n", - " fill_color=\"fill_color\",\n", - " fill_alpha=0.9,\n", - " line_color=\"#000000\",\n", - " line_width=1.5,\n", - " )\n", - " graph_renderer.edge_renderer.glyph = MultiLine(\n", - " line_color=\"#000000\",\n", - " line_alpha=0.3,\n", - " line_width=1.5,\n", - " )\n", - " p.renderers.append(graph_renderer)\n", - " return graph_renderer\n", - "\n", - "def create_cognee_style_network_with_logo(\n", - " G,\n", - " output_filename=\"cognee_netwdzsfsdfsdgo.html\",\n", - " title=\"Cognee-Style Network\",\n", - " node_attribute=\"group\",\n", - " layout_func=nx.spring_layout,\n", - " layout_scale=3.0,\n", - " logo_alpha=0.1,\n", - "):\n", - " \"\"\"\n", - " Create a Cognee-inspired network visualization with an embedded logo.\n", - " \"\"\"\n", - " logging.info(\"Converting graph to serializable format...\")\n", - " G = convert_to_serializable_graph(G)\n", - "\n", - " logging.info(\"Generating layout positions...\")\n", - " layout_positions = generate_layout_positions(G, layout_func, layout_scale)\n", - "\n", - " logging.info(\"Assigning node colors...\")\n", - " palette = [\"#6510F4\", \"#0DFF00\", \"#FFFFFF\"]\n", - " node_colors, color_map = assign_node_colors(G, node_attribute, palette)\n", - "\n", - " logging.info(\"Calculating centrality...\")\n", - " centrality = nx.degree_centrality(G)\n", - "\n", - " logging.info(\"Preparing Bokeh output...\")\n", - " output_file(output_filename)\n", - " p = figure(\n", - " title=title,\n", - " tools=\"pan,wheel_zoom,save,reset,hover\",\n", - " active_scroll=\"wheel_zoom\",\n", - " width=1200,\n", - " height=900,\n", - " background_fill_color=\"#F4F4F4\",\n", - " x_range=Range1d(-layout_scale, layout_scale),\n", - " y_range=Range1d(-layout_scale, layout_scale),\n", - " )\n", - " p.toolbar.logo = None\n", - " p.axis.visible = False\n", - " p.grid.visible = False\n", - "\n", - " logging.info(\"Embedding logo into visualization...\")\n", - " embed_logo(p, layout_scale, logo_alpha)\n", - "\n", - " logging.info(\"Styling and rendering graph...\")\n", - " style_and_render_graph(p, G, layout_positions, node_attribute, node_colors, centrality)\n", - "\n", - " logging.info(\"Adding hover tool...\")\n", - " hover_tool = HoverTool(\n", - " tooltips=[\n", - " (\"Node\", \"@index\"),\n", - " (node_attribute.capitalize(), f\"@{node_attribute}\"),\n", - " (\"Centrality\", \"@radius{0.00}\"),\n", - " ],\n", - " )\n", - " p.add_tools(hover_tool)\n", - " # from bokeh.io import output_notebook, show\n", - " # \n", - " # # Save the result\n", - " # output_notebook()\n", - " # \n", - " # # Display the plot in the notebook\n", - " # show(p)\n", - "\n", - " logging.info(f\"Saving visualization to {output_filename}...\")\n", - " html_content = file_html(p, CDN, title)\n", - " with open(output_filename, \"w\") as f:\n", - " f.write(html_content)\n", - " # \n", - " # logging.info(\"Visualization complete.\")\n", - " print(html_content)\n", - "\n", - " # return html_content\n", - "\n", - "\n", - "def graph_to_tuple(graph):\n", - " \"\"\"\n", - " Converts a networkx graph to a tuple of (nodes, edges).\n", - "\n", - " :param graph: A networkx graph.\n", - " :return: A tuple (nodes, edges).\n", - " \"\"\"\n", - " nodes = list(graph.nodes(data=True)) # Get nodes with attributes\n", - " edges = list(graph.edges(data=True)) # Get edges with attributes\n", - " return (nodes, edges)" - ], - "id": "bf001eeda0f4f450", - "outputs": [], - "execution_count": 110 - }, - { - "metadata": { - "ExecuteTime": { - "end_time": "2024-12-30T09:50:39.237084Z", - "start_time": "2024-12-30T09:50:39.184107Z" - } - }, - "cell_type": "code", - "source": [ - "\n", - "import networkx as nx\n", - "\n", - "# graph_data = await graph_engine.get_graph_data()\n", - "# \n", - "# print(graph_data)\n", - "G = nx.random_geometric_graph(50, 0.3)\n", - "# Assign random group attributes for coloring\n", - "for i, node in enumerate(G.nodes()):\n", - " G.nodes[node][\"group\"] = f\"Group {i % 3 + 1}\"\n", - "def graph_to_tuple(graph):\n", - " \"\"\"\n", - " Converts a networkx graph to a tuple of (nodes, edges).\n", - " \n", - " :param graph: A networkx graph.\n", - " :return: A tuple (nodes, edges).\n", - " \"\"\"\n", - " nodes = list(graph.nodes(data=True)) # Get nodes with attributes\n", - " edges = list(graph.edges(data=True)) # Get edges with attributes\n", - " return (nodes, edges)\n", - "\n", - "G= graph_to_tuple(G)\n", - " \n", - "print(G)\n", - "\n", - "create_cognee_style_network_with_logo(\n", - " G,\n", - " output_filename=\"cognee_style_network_with_logo.html\",\n", - " title=\"Cognee-Graph Network\",\n", - " node_attribute=\"group\",\n", - " layout_func=nx.spring_layout,\n", - " layout_scale=3.0, \n", - ")" - ], - "id": "75ff7ff88fd5894e", - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:root:Converting graph to serializable format...INFO:root:Generating layout positions...INFO:root:Assigning node colors...INFO:root:Calculating centrality...INFO:root:Preparing Bokeh output...INFO:bokeh.io.state:Session output file 'cognee_style_network_with_logo.html' already exists, will be overwritten.INFO:root:Embedding logo into visualization...INFO:root:Styling and rendering graph...INFO:root:Adding hover tool...INFO:root:Saving visualization to cognee_style_network_with_logo.html..." - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "([(0, {'pos': [0.6391927561230611, 0.9577081939968699], 'group': 'Group 1'}), (1, {'pos': [0.7742314200503037, 0.8238027878749569], 'group': 'Group 2'}), (2, {'pos': [0.5512540135677992, 0.9153577565777516], 'group': 'Group 3'}), (3, {'pos': [0.25732444952662925, 0.1490908794049587], 'group': 'Group 1'}), (4, {'pos': [0.6279736736286188, 0.10785263935795697], 'group': 'Group 2'}), (5, {'pos': [0.914880874135223, 0.74802072749348], 'group': 'Group 3'}), (6, {'pos': [0.26667034242888454, 0.35251088926605467], 'group': 'Group 1'}), (7, {'pos': [0.5120452199291847, 0.03985698500510293], 'group': 'Group 2'}), (8, {'pos': [0.6328184134196475, 0.7746435028883368], 'group': 'Group 3'}), (9, {'pos': [0.9987954567088807, 0.14034841131467046], 'group': 'Group 1'}), (10, {'pos': [0.12058819726432679, 0.13241169932006103], 'group': 'Group 2'}), (11, {'pos': [0.22508864454761268, 0.6079455800309562], 'group': 'Group 3'}), (12, {'pos': [0.11725977735714843, 0.1411642545174081], 'group': 'Group 1'}), (13, {'pos': [0.919394680890905, 0.42166241371429103], 'group': 'Group 2'}), (14, {'pos': [0.25836207293422575, 0.7258827350068001], 'group': 'Group 3'}), (15, {'pos': [0.5172530162885245, 0.3000006396486069], 'group': 'Group 1'}), (16, {'pos': [0.15298901445139557, 0.6194415824148096], 'group': 'Group 2'}), (17, {'pos': [0.5503858383395439, 0.2963881895674162], 'group': 'Group 3'}), (18, {'pos': [0.3129369332017773, 0.9521497094216804], 'group': 'Group 1'}), (19, {'pos': [0.06277239466252482, 0.38715809100060106], 'group': 'Group 2'}), (20, {'pos': [0.5823930417754898, 0.5818352592143017], 'group': 'Group 3'}), (21, {'pos': [0.11925372303762072, 0.23308929448109605], 'group': 'Group 1'}), (22, {'pos': [0.9639456495249757, 0.5916791753212867], 'group': 'Group 2'}), (23, {'pos': [0.3588059198785577, 0.04938679848326155], 'group': 'Group 3'}), (24, {'pos': [0.5490274008918267, 0.06720020630140588], 'group': 'Group 1'}), (25, {'pos': [0.602429225429183, 0.4562231010090445], 'group': 'Group 2'}), (26, {'pos': [0.09188236933925253, 0.047929414091373634], 'group': 'Group 3'}), (27, {'pos': [0.06377074952137485, 0.2797118663108954], 'group': 'Group 1'}), (28, {'pos': [0.4906626665413346, 0.4663320694792623], 'group': 'Group 2'}), (29, {'pos': [0.6172692666513304, 0.6263884205262166], 'group': 'Group 3'}), (30, {'pos': [0.49741178043085055, 0.01644316714237193], 'group': 'Group 1'}), (31, {'pos': [0.7988288141788339, 0.059965469974182284], 'group': 'Group 2'}), (32, {'pos': [0.7217970607301775, 0.2708413010090548], 'group': 'Group 3'}), (33, {'pos': [0.011511237425662069, 0.028343774982744208], 'group': 'Group 1'}), (34, {'pos': [0.38128826432700813, 0.2895041685619608], 'group': 'Group 2'}), (35, {'pos': [0.07417032576014304, 0.636863466707599], 'group': 'Group 3'}), (36, {'pos': [0.13676493570949355, 0.376877180606942], 'group': 'Group 1'}), (37, {'pos': [0.9323413821739948, 0.8826597647832115], 'group': 'Group 2'}), (38, {'pos': [0.3894337987935055, 0.26448206953319053], 'group': 'Group 3'}), (39, {'pos': [0.8254125197486137, 0.05070075106309235], 'group': 'Group 1'}), (40, {'pos': [0.2743690977404899, 0.29681368611004266], 'group': 'Group 2'}), (41, {'pos': [0.941395039956768, 0.6449589631657039], 'group': 'Group 3'}), (42, {'pos': [0.611788621954416, 0.4739744957109453], 'group': 'Group 1'}), (43, {'pos': [0.2942512190192522, 0.0937049487795869], 'group': 'Group 2'}), (44, {'pos': [0.23196649324366891, 0.16136392127068122], 'group': 'Group 3'}), (45, {'pos': [0.08688565032870654, 0.75331756935591], 'group': 'Group 1'}), (46, {'pos': [0.1510509171930553, 0.9969999993249501], 'group': 'Group 2'}), (47, {'pos': [0.5439996268152169, 0.07151361555903424], 'group': 'Group 3'}), (48, {'pos': [0.17916289804000352, 0.41119976642291267], 'group': 'Group 1'}), (49, {'pos': [0.8746527854119696, 0.5086303883085483], 'group': 'Group 2'})], [(0, 1, {}), (0, 2, {}), (0, 8, {}), (1, 2, {}), (1, 5, {}), (1, 8, {}), (1, 22, {}), (1, 29, {}), (1, 37, {}), (1, 41, {}), (2, 8, {}), (2, 18, {}), (2, 29, {}), (3, 6, {}), (3, 7, {}), (3, 10, {}), (3, 12, {}), (3, 21, {}), (3, 23, {}), (3, 26, {}), (3, 27, {}), (3, 30, {}), (3, 33, {}), (3, 34, {}), (3, 36, {}), (3, 38, {}), (3, 40, {}), (3, 43, {}), (3, 44, {}), (3, 47, {}), (3, 48, {}), (4, 7, {}), (4, 15, {}), (4, 17, {}), (4, 23, {}), (4, 24, {}), (4, 30, {}), (4, 31, {}), (4, 32, {}), (4, 38, {}), (4, 39, {}), (4, 47, {}), (5, 8, {}), (5, 22, {}), (5, 37, {}), (5, 41, {}), (5, 49, {}), (6, 10, {}), (6, 11, {}), (6, 12, {}), (6, 15, {}), (6, 16, {}), (6, 17, {}), (6, 19, {}), (6, 21, {}), (6, 27, {}), (6, 28, {}), (6, 34, {}), (6, 36, {}), (6, 38, {}), (6, 40, {}), (6, 43, {}), (6, 44, {}), (6, 48, {}), (7, 15, {}), (7, 17, {}), (7, 23, {}), (7, 24, {}), (7, 30, {}), (7, 31, {}), (7, 34, {}), (7, 38, {}), (7, 43, {}), (7, 47, {}), (8, 20, {}), (8, 29, {}), (9, 13, {}), (9, 31, {}), (9, 39, {}), (10, 12, {}), (10, 19, {}), (10, 21, {}), (10, 23, {}), (10, 26, {}), (10, 27, {}), (10, 33, {}), (10, 36, {}), (10, 38, {}), (10, 40, {}), (10, 43, {}), (10, 44, {}), (10, 48, {}), (11, 14, {}), (11, 16, {}), (11, 19, {}), (11, 35, {}), (11, 36, {}), (11, 45, {}), (11, 48, {}), (12, 19, {}), (12, 21, {}), (12, 23, {}), (12, 26, {}), (12, 27, {}), (12, 33, {}), (12, 36, {}), (12, 38, {}), (12, 40, {}), (12, 43, {}), (12, 44, {}), (12, 48, {}), (13, 22, {}), (13, 32, {}), (13, 41, {}), (13, 49, {}), (14, 16, {}), (14, 18, {}), (14, 35, {}), (14, 45, {}), (14, 46, {}), (15, 17, {}), (15, 20, {}), (15, 23, {}), (15, 24, {}), (15, 25, {}), (15, 28, {}), (15, 30, {}), (15, 32, {}), (15, 34, {}), (15, 38, {}), (15, 40, {}), (15, 42, {}), (15, 47, {}), (16, 19, {}), (16, 35, {}), (16, 36, {}), (16, 45, {}), (16, 48, {}), (17, 20, {}), (17, 24, {}), (17, 25, {}), (17, 28, {}), (17, 30, {}), (17, 32, {}), (17, 34, {}), (17, 38, {}), (17, 40, {}), (17, 42, {}), (17, 47, {}), (18, 46, {}), (19, 21, {}), (19, 27, {}), (19, 35, {}), (19, 36, {}), (19, 40, {}), (19, 44, {}), (19, 48, {}), (20, 25, {}), (20, 28, {}), (20, 29, {}), (20, 42, {}), (21, 26, {}), (21, 27, {}), (21, 33, {}), (21, 34, {}), (21, 36, {}), (21, 38, {}), (21, 40, {}), (21, 43, {}), (21, 44, {}), (21, 48, {}), (22, 37, {}), (22, 41, {}), (22, 49, {}), (23, 24, {}), (23, 26, {}), (23, 30, {}), (23, 34, {}), (23, 38, {}), (23, 40, {}), (23, 43, {}), (23, 44, {}), (23, 47, {}), (24, 30, {}), (24, 31, {}), (24, 32, {}), (24, 34, {}), (24, 38, {}), (24, 39, {}), (24, 43, {}), (24, 47, {}), (25, 28, {}), (25, 29, {}), (25, 32, {}), (25, 34, {}), (25, 38, {}), (25, 42, {}), (25, 49, {}), (26, 27, {}), (26, 33, {}), (26, 43, {}), (26, 44, {}), (27, 33, {}), (27, 36, {}), (27, 40, {}), (27, 43, {}), (27, 44, {}), (27, 48, {}), (28, 29, {}), (28, 34, {}), (28, 38, {}), (28, 40, {}), (28, 42, {}), (29, 42, {}), (29, 49, {}), (30, 34, {}), (30, 38, {}), (30, 43, {}), (30, 47, {}), (31, 32, {}), (31, 39, {}), (31, 47, {}), (32, 39, {}), (32, 42, {}), (32, 47, {}), (32, 49, {}), (33, 43, {}), (33, 44, {}), (34, 36, {}), (34, 38, {}), (34, 40, {}), (34, 42, {}), (34, 43, {}), (34, 44, {}), (34, 47, {}), (34, 48, {}), (35, 36, {}), (35, 45, {}), (35, 48, {}), (36, 38, {}), (36, 40, {}), (36, 44, {}), (36, 48, {}), (37, 41, {}), (38, 40, {}), (38, 43, {}), (38, 44, {}), (38, 47, {}), (38, 48, {}), (39, 47, {}), (40, 43, {}), (40, 44, {}), (40, 48, {}), (41, 49, {}), (42, 49, {}), (43, 44, {}), (43, 47, {}), (44, 48, {}), (45, 46, {})])\n", - "\n", - "\n", - " \n", - " \n", - " Cognee-Graph Network\n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - " \n", - "\n" - ] - } - ], - "execution_count": 111 - }, { "metadata": { "ExecuteTime": { @@ -2142,7 +1458,7 @@ "cell_type": "code", "outputs": [], "execution_count": null, - "source": " 0.0667", + "source": "", "id": "d042efe5d38144fa" }, { @@ -2150,7 +1466,7 @@ "cell_type": "code", "outputs": [], "execution_count": null, - "source": "0.085", + "source": "", "id": "9436af97520e0ae" } ],