diff --git a/examples/visualize.ipynb b/examples/visualize.ipynb index d69e73c7b..4ac4e1d18 100644 --- a/examples/visualize.ipynb +++ b/examples/visualize.ipynb @@ -65,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "26fb276b", "metadata": {}, "outputs": [], @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "937f1476", "metadata": {}, "outputs": [], @@ -94,12 +94,175 @@ "gds = GraphDataScience(NEO4J_URI, auth=NEO4J_AUTH, database=NEO4J_DB)" ] }, + { + "cell_type": "markdown", + "id": "a96b84b5", + "metadata": {}, + "source": [ + "## Built-in visualization" + ] + }, + { + "cell_type": "markdown", + "id": "aa1f778c", + "metadata": {}, + "source": [ + "### IMDB" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "68125987", + "metadata": {}, + "outputs": [], + "source": [ + "G_imdb = gds.graph.load_imdb()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "acc92487", + "metadata": {}, + "outputs": [], + "source": [ + "G_imdb.visualize()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21b17ba8", + "metadata": {}, + "outputs": [], + "source": [ + "G_imdb.visualize(directed=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a2f6d726", + "metadata": {}, + "outputs": [], + "source": [ + "G_imdb.visualize(directed=False, theme=\"light\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29534589", + "metadata": {}, + "outputs": [], + "source": [ + "G_imdb.visualize(directed=False, node_count=200)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b751ec2c", + "metadata": {}, + "outputs": [], + "source": [ + "G_imdb.drop()" + ] + }, + { + "cell_type": "markdown", + "id": "c6713a9d", + "metadata": {}, + "source": [ + "### Cora" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fe00c465", + "metadata": {}, + "outputs": [], + "source": [ + "G_cora = gds.graph.load_cora()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "258747fc", + "metadata": {}, + "outputs": [], + "source": [ + "G_cora.visualize()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d5a0704", + "metadata": {}, + "outputs": [], + "source": [ + "G_cora.visualize(color_property=\"subject\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7dfe9e57", + "metadata": {}, + "outputs": [], + "source": [ + "G_cora.visualize(color_property=\"subject\", include_node_properties=[\"subject\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "db635dd6", + "metadata": {}, + "outputs": [], + "source": [ + "gds.degree.mutate(G_cora, mutateProperty=\"deg\")\n", + "G_cora.visualize(color_property=\"subject\", size_property=\"deg\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6bc16f4", + "metadata": {}, + "outputs": [], + "source": [ + "G_cora.visualize(color_property=\"subject\", center_nodes=[12350])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "447f1a2d", + "metadata": {}, + "outputs": [], + "source": [ + "G_cora.drop()" + ] + }, + { + "cell_type": "markdown", + "id": "56dc02c4", + "metadata": {}, + "source": [ + "## Using PyVis" + ] + }, { "cell_type": "markdown", "id": "b1a8ab78", "metadata": {}, "source": [ - "## Sampling Cora\n", + "### Sampling Cora\n", "\n", "Next we use the [built-in Cora loader](https://neo4j.com/docs/graph-data-science-client/current/common-datasets/#_cora) to get the data into GDS.\n", "The nodes in the Cora dataset is represented by academic papers, and the relationships connecting them are citations.\n", @@ -109,7 +272,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "a575da60", "metadata": {}, "outputs": [], @@ -127,24 +290,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "428ac7b8", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Metadata for our loaded Cora graph `G`: Graph(name=cora, node_count=2708, relationship_count=5429)\n", - "Node labels present in `G`: ['Paper']\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"Metadata for our loaded Cora graph `G`: {G}\")\n", "print(f\"Node labels present in `G`: {G.node_labels()}\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6aceb0a", + "metadata": {}, + "outputs": [], + "source": [ + "G.visualize(color_property=\"subject\")" + ] + }, { "cell_type": "markdown", "id": "3e8b8a83", @@ -159,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "3d116738", "metadata": {}, "outputs": [], @@ -178,19 +342,10 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "d66ea2d5", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Number of nodes in our sample: 406\n", - "Number of relationships in our sample: 532\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"Number of nodes in our sample: {G_sample.node_count()}\")\n", "print(f\"Number of relationships in our sample: {G_sample.relationship_count()}\")" @@ -207,30 +362,10 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "3d80d653", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "mutateMillis 0\n", - "nodePropertiesWritten 406\n", - "ranIterations 20\n", - "didConverge False\n", - "centralityDistribution {'min': 0.14999961853027344, 'max': 2.27294921...\n", - "postProcessingMillis 1\n", - "preProcessingMillis 0\n", - "computeMillis 7\n", - "configuration {'mutateProperty': 'rank', 'jobId': '5ca450ff-...\n", - "Name: 0, dtype: object" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "gds.pageRank.mutate(G_sample, mutateProperty=\"rank\")" ] @@ -240,7 +375,7 @@ "id": "722caa7b", "metadata": {}, "source": [ - "## Exporting the sampled Cora graph\n", + "### Exporting the sampled Cora graph\n", "\n", "We can now export the topology and node properties of our sampled graph that we want to visualize.\n", "\n", @@ -249,129 +384,10 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "d3c76b25", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
sourceNodeIdtargetNodeIdrelationshipType
03133631349CITES
131336686532CITES
2313361129442CITES
331349686532CITES
43135331336CITES
............
5273496131043CITES
5283496122883CITES
5291028799513CITES
5301028849513CITES
5317677631136631CITES
\n", - "

532 rows × 3 columns

\n", - "
" - ], - "text/plain": [ - " sourceNodeId targetNodeId relationshipType\n", - "0 31336 31349 CITES\n", - "1 31336 686532 CITES\n", - "2 31336 1129442 CITES\n", - "3 31349 686532 CITES\n", - "4 31353 31336 CITES\n", - ".. ... ... ...\n", - "527 34961 31043 CITES\n", - "528 34961 22883 CITES\n", - "529 102879 9513 CITES\n", - "530 102884 9513 CITES\n", - "531 767763 1136631 CITES\n", - "\n", - "[532 rows x 3 columns]" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "sample_topology_df = gds.graph.relationships.stream(G_sample)\n", "display(sample_topology_df)" @@ -392,129 +408,10 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "1c52e3b2", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
nodeIdranksubject
01640.2459644.0
14340.1585002.0
216940.9612405.0
319490.2249126.0
419520.1500006.0
............
40111541030.3194983.0
40211541240.6277060.0
40311541690.1547840.0
40411542510.1876750.0
40511542760.2775000.0
\n", - "

406 rows × 3 columns

\n", - "
" - ], - "text/plain": [ - " nodeId rank subject\n", - "0 164 0.245964 4.0\n", - "1 434 0.158500 2.0\n", - "2 1694 0.961240 5.0\n", - "3 1949 0.224912 6.0\n", - "4 1952 0.150000 6.0\n", - ".. ... ... ...\n", - "401 1154103 0.319498 3.0\n", - "402 1154124 0.627706 0.0\n", - "403 1154169 0.154784 0.0\n", - "404 1154251 0.187675 0.0\n", - "405 1154276 0.277500 0.0\n", - "\n", - "[406 rows x 3 columns]" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "sample_node_properties_df = gds.graph.nodeProperties.stream(\n", " G_sample,\n", @@ -529,46 +426,18 @@ "id": "ff81f977", "metadata": {}, "source": [ + "### Render visualization\n", + "\n", "Now that we have all the data we want to visualize, we can create a network with PyVis.\n", "We color each node according to its \"subject\", and size it according to its \"rank\"." ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "93cb3bdf", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "cora-sample.html\n" - ] - }, - { - "data": { - "text/html": [ - "\n", - " \n", - " " - ], - "text/plain": [ - "" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "net = Network(notebook = True,\n", "cdn_resources=\"remote\",\n", @@ -619,7 +488,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "33523a0b", "metadata": {}, "outputs": [], @@ -630,22 +499,8 @@ } ], "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.6" + "name": "python" } }, "nbformat": 4,