diff --git a/gephipy-example.ipynb b/gephipy-example.ipynb index ad85f73..2e65ba7 100644 --- a/gephipy-example.ipynb +++ b/gephipy-example.ipynb @@ -1,9335 +1,298 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { "colab": { - "provenance": [] + "base_uri": "https://localhost:8080/" }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } + "id": "qgDKsAt_vlnz", + "outputId": "906f6f20-3952-4fa3-a394-0ee72deec789" + }, + "outputs": [], + "source": [ + "!pip install gephipy" + ] }, - "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "V757ca6jvwpS" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "qgDKsAt_vlnz", - "outputId": "906f6f20-3952-4fa3-a394-0ee72deec789" - }, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Requirement already satisfied: gephipy in /usr/local/lib/python3.12/dist-packages (0.0.2)\n", - "Requirement already satisfied: JPype1<2.0.0,>=1.6.0 in /usr/local/lib/python3.12/dist-packages (from gephipy) (1.6.0)\n", - "Requirement already satisfied: networkx<4.0,>=3.5 in /usr/local/lib/python3.12/dist-packages (from gephipy) (3.5)\n", - "Requirement already satisfied: packaging in /usr/local/lib/python3.12/dist-packages (from JPype1<2.0.0,>=1.6.0->gephipy) (25.0)\n" - ] - } - ], - "source": [ - "!pip install gephipy" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "start JVM\n" + ] + } + ], + "source": [ + "#\n", + "# Create a Gephy workspace\n", + "#\n", + "from gephipy import gephipy\n", + "\n", + "workspace = gephipy.create_workspace()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, + "id": "lktn7GSPvwY5", + "outputId": "99ad112d-9c56-40ed-99d7-43bc0bef3f12" + }, + "outputs": [ { - "cell_type": "code", - "source": [ - "#\n", - "# Create a Gephy workspace\n", - "#\n", - "from gephipy import gephipy\n", - "\n", - "workspace = gephipy.create_workspace()\n" - ], - "metadata": { - "id": "V757ca6jvwpS" - }, - "execution_count": 35, - "outputs": [] + "name": "stdout", + "output_type": "stream", + "text": [ + "Import done!\n" + ] + } + ], + "source": [ + "#\n", + "# Load a graph in Gephy\n", + "#\n", + "from gephipy import gephipy\n", + "import networkx as nx\n", + "\n", + "# Create a random graph with NetworkX\n", + "graphX = nx.erdos_renyi_graph(500,0.01)\n", + "\n", + "# Load it in Gephi\n", + "gephipy.networkx_to_gephi(workspace, graphX)\n", + "\n", + "print(\"Import done!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, + "id": "Uezb67nZebA5", + "outputId": "19f8945d-5de5-4a41-beed-a7fc3955b1e5" + }, + "outputs": [ { - "cell_type": "code", - "source": [ - "#\n", - "# Load a graph in Gephy\n", - "#\n", - "from gephipy import gephipy\n", - "import networkx as nx\n", - "\n", - "# Create a random graph with NetworkX\n", - "graphX = nx.erdos_renyi_graph(500,0.01)\n", - "\n", - "# Load it in Gephi\n", - "gephipy.networkx_to_gephi(workspace, graphX)\n", - "\n", - "print(\"Import done!\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "lktn7GSPvwY5", - "outputId": "99ad112d-9c56-40ed-99d7-43bc0bef3f12" - }, - "execution_count": 36, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Import done!\n" - ] - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "Metrics done!\n" + ] + } + ], + "source": [ + "#\n", + "# Compute some metrics\n", + "#\n", + "from gephipy import gephipy\n", + "from org.gephi.statistics.plugin import GraphDistance, Modularity\n", + "\n", + "# Get the Gephi graph model from the current workspace\n", + "graphModel = gephipy.get_graph_model(workspace)\n", + "\n", + "# Louvain\n", + "modularity = Modularity()\n", + "modularity.execute(graphModel)\n", + "\n", + "# Betweeness centrality\n", + "centrality = GraphDistance()\n", + "centrality.setDirected(True)\n", + "centrality.execute(graphModel)\n", + "\n", + "print(\"Metrics done!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "cell_type": "code", - "source": [ - "#\n", - "# Compute some metrics\n", - "#\n", - "from gephipy import gephipy\n", - "from org.gephi.statistics.plugin import GraphDistance, Modularity\n", - "\n", - "# Get the Gephi graph model from the current workspace\n", - "graphModel = gephipy.get_graph_model(workspace)\n", - "\n", - "# Louvain\n", - "modularity = Modularity()\n", - "modularity.execute(graphModel)\n", - "\n", - "# Betweeness centrality\n", - "centrality = GraphDistance()\n", - "centrality.setDirected(True)\n", - "centrality.execute(graphModel)\n", - "\n", - "print(\"Metrics done!\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "Uezb67nZebA5", - "outputId": "19f8945d-5de5-4a41-beed-a7fc3955b1e5" - }, - "execution_count": 37, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Metrics done!\n" - ] - } - ] + "id": "zdz9VNa8fP8-", + "outputId": "46972cb2-630c-45ab-ef09-70cc50f1d809" + }, + "outputs": [], + "source": [ + "#\n", + "# Apply appearance\n", + "#\n", + "from gephipy import gephipy\n", + "\n", + "from org.openide.util import Lookup\n", + "from org.gephi.appearance.api import AppearanceController\n", + "from org.gephi.appearance.plugin import RankingNodeSizeTransformer, PartitionElementColorTransformer\n", + "from org.gephi.appearance.plugin.palette import PaletteManager\n", + "\n", + "appearanceController = Lookup.getDefault().lookup(AppearanceController)\n", + "appearanceModel = appearanceController.getModel()\n", + "\n", + "# Size Make node size based on centrality\n", + "centralityColumn = graphModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS)\n", + "centralityRanking = appearanceModel.getNodeFunction(centralityColumn, RankingNodeSizeTransformer)\n", + "centralityTransformer = centralityRanking.getTransformer()\n", + "centralityTransformer.setMinSize(10)\n", + "centralityTransformer.setMaxSize(100)\n", + "appearanceController.transform(centralityRanking)\n", + "\n", + "# Color by community\n", + "communityColumn = graphModel.getNodeTable().getColumn(Modularity.MODULARITY_CLASS)\n", + "colorPartition = appearanceModel.getNodeFunction(communityColumn, PartitionElementColorTransformer)\n", + "partition = colorPartition.getPartition()\n", + "palette = PaletteManager.getInstance().generatePalette(partition.size(graphModel.getGraph()))\n", + "partition.setColors(graphModel.getGraph(), palette.getColors())\n", + "appearanceController.transform(colorPartition)\n", + "\n", + "print(\"Appearance done!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, + "id": "IOeaVb8kfbgn", + "outputId": "d4e7e13c-7ff7-4442-ea2c-4d2aa3bed7d0" + }, + "outputs": [ { - "cell_type": "code", - "source": [ - "#\n", - "# Apply appearance\n", - "#\n", - "from gephipy import gephipy\n", - "\n", - "from org.openide.util import Lookup\n", - "from org.gephi.appearance.api import AppearanceController\n", - "from org.gephi.appearance.plugin import RankingNodeSizeTransformer, PartitionElementColorTransformer\n", - "from org.gephi.appearance.plugin.palette import PaletteManager\n", - "\n", - "appearanceController = Lookup.getDefault().lookup(AppearanceController)\n", - "appearanceModel = appearanceController.getModel()\n", - "\n", - "# Size Make node size based on centrality\n", - "centralityColumn = graphModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS)\n", - "centralityRanking = appearanceModel.getNodeFunction(centralityColumn, RankingNodeSizeTransformer)\n", - "centralityTransformer = centralityRanking.getTransformer()\n", - "centralityTransformer.setMinSize(10)\n", - "centralityTransformer.setMaxSize(100)\n", - "appearanceController.transform(centralityRanking)\n", - "\n", - "# Color by community\n", - "communityColumn = graphModel.getNodeTable().getColumn(Modularity.MODULARITY_CLASS)\n", - "colorPartition = appearanceModel.getNodeFunction(communityColumn, PartitionElementColorTransformer)\n", - "partition = colorPartition.getPartition()\n", - "palette = PaletteManager.getInstance().generatePalette(partition.size(graphModel.getGraph()))\n", - "partition.setColors(graphModel.getGraph(), palette.getColors())\n", - "appearanceController.transform(colorPartition)\n", - "\n", - "print(\"Appearance done!\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "zdz9VNa8fP8-", - "outputId": "46972cb2-630c-45ab-ef09-70cc50f1d809" - }, - "execution_count": 38, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Appearance done!\n" - ] - } - ] + "name": "stderr", + "output_type": "stream", + "text": [ + "Running org.gephi.layout.plugin.forceAtlas2.ForceAtlas2@4d9ac0b4: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1724.12it/s]" + ] }, { - "cell_type": "code", - "source": [ - "#\n", - "# Run Layouts\n", - "#\n", - "from gephipy import gephipy\n", - "\n", - "from org.gephi.layout.plugin.forceAtlas2 import ForceAtlas2Builder\n", - "from org.gephi.layout.plugin.random import Random\n", - "from org.gephi.layout.plugin.noverlap import NoverlapLayoutBuilder\n", - "\n", - "# Random layout\n", - "random = Random().buildLayout()\n", - "random.setGraphModel(gephipy.get_graph_model(workspace))\n", - "random.initAlgo()\n", - "random.goAlgo()\n", - "random.endAlgo()\n", - "\n", - "# FA2 layout\n", - "fa2 = ForceAtlas2Builder().buildLayout()\n", - "fa2.setGraphModel(gephipy.get_graph_model(workspace))\n", - "fa2.resetPropertiesValues()\n", - "fa2.initAlgo()\n", - "for x in range(1000):\n", - " fa2.goAlgo()\n", - "fa2.endAlgo()\n", - "\n", - "# Noverlap layout\n", - "noverlap = NoverlapLayoutBuilder().buildLayout()\n", - "noverlap.setGraphModel(gephipy.get_graph_model(workspace))\n", - "noverlap.initAlgo()\n", - "noverlap.endAlgo()\n", - "\n", - "print(\"Layouts done!\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "IOeaVb8kfbgn", - "outputId": "d4e7e13c-7ff7-4442-ea2c-4d2aa3bed7d0" - }, - "execution_count": 39, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Layout done!\n" - ] - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "Layouts done!\n" + ] }, { - "cell_type": "code", - "source": [ - "#\n", - "# Export your graph / visualize your graph\n", - "#\n", - "from gephipy import gephipy\n", - "from IPython.display import display,HTML\n", - "\n", - "# Transform it to graphX\n", - "graphx = gephipy.gephi_to_networkx(workspace)\n", - "\n", - "# Export your graph\n", - "gephipy.export_gexf(workspace, \"my-gephi-graph.gexf\")\n", - "gephipy.export_pdf(workspace, \"my-gephi-graph.pdf\")\n", - "\n", - "gephipy.export_svg(workspace, \"my-gephi-graph.svg\")\n", - "with open(\"./my-gephi-graph.svg\", 'r') as file_svg:\n", - " svg = file_svg.read()\n", - " html_code = f\"\"\"\n", - " \n", - "