diff --git a/README.adoc b/README.adoc index a450bab..d660955 100644 --- a/README.adoc +++ b/README.adoc @@ -1,6 +1,6 @@ -= Neo4j Graph Algorithms Jupyter Notebooks += Neo4j Graph Data Science Jupyter Notebooks -This repository contains Jupyter Notebooks for each of the https://neo4j-contrib.github.io/neo4j-graph-algorithms/[Neo4j graph algorithms^]. +This repository contains Jupyter Notebooks for each of the https://neo4j.com/docs/graph-data-science/current/[Neo4j graph algorithms^]. == Path finding @@ -40,10 +40,10 @@ pip install -r requirements.txt === Neo4j -We'll also need to have a Neo4j server, with the Graph Algorithms library installed, running locally. +We'll also need to have a Neo4j server, with the Graph Data Science library installed, running locally. The easiest way to do this is to download the Neo4j Desktop from http://neo4j.com/download[neo4j.com/download^]. -Once we've done that we can create a project and then install Graph Algorithms from the Plugins section. +Once we've done that we can create a project and then install Graph Data Science from the Plugins section. image::images/installation.png[] diff --git a/empty.py b/empty.py index fbfb60c..d92cb87 100644 --- a/empty.py +++ b/empty.py @@ -1,11 +1,11 @@ import os -from neo4j.v1 import GraphDatabase, basic_auth +from neo4j import GraphDatabase host = os.environ.get("NEO4J_HOST", "bolt://localhost") user = os.environ.get("NEO4J_USER", "neo4j") password = os.environ.get("NEO4J_PASSWORD", "neo") -driver = GraphDatabase.driver(host, auth=basic_auth(user, password)) +driver = GraphDatabase.driver(host, auth=(user, password)) def clear_db(): diff --git a/images/installation.png b/images/installation.png index 7a80525..1feff94 100644 Binary files a/images/installation.png and b/images/installation.png differ diff --git a/notebooks/AllPairsShortestPath.ipynb b/notebooks/AllPairsShortestPath.ipynb index b1173c6..fe93971 100644 --- a/notebooks/AllPairsShortestPath.ipynb +++ b/notebooks/AllPairsShortestPath.ipynb @@ -17,7 +17,7 @@ "metadata": {}, "outputs": [], "source": [ - "from neo4j.v1 import GraphDatabase, basic_auth\n", + "from neo4j import GraphDatabase\n", "import pandas as pd\n", "import os" ] @@ -38,7 +38,7 @@ "host = os.environ.get(\"NEO4J_HOST\", \"bolt://localhost\") \n", "user = os.environ.get(\"NEO4J_USER\", \"neo4j\")\n", "password = os.environ.get(\"NEO4J_PASSWORD\", \"neo\")\n", - "driver = GraphDatabase.driver(host, auth=basic_auth(user, password))" + "driver = GraphDatabase.driver(host, auth=(user, password))" ] }, { @@ -92,7 +92,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -126,54 +126,54 @@ " 0\n", " A\n", " F\n", - " 160.0\n", + " 100.0\n", " \n", " \n", " 1\n", - " A\n", - " E\n", - " 120.0\n", + " C\n", + " F\n", + " 90.0\n", " \n", " \n", " 2\n", " B\n", " F\n", - " 110.0\n", + " 90.0\n", " \n", " \n", " 3\n", - " C\n", - " F\n", - " 110.0\n", + " A\n", + " E\n", + " 80.0\n", " \n", " \n", " 4\n", - " A\n", - " D\n", - " 90.0\n", + " B\n", + " E\n", + " 70.0\n", " \n", " \n", " 5\n", - " B\n", + " C\n", " E\n", " 70.0\n", " \n", " \n", " 6\n", + " A\n", " D\n", - " F\n", - " 70.0\n", + " 50.0\n", " \n", " \n", " 7\n", - " C\n", - " E\n", - " 70.0\n", + " A\n", + " B\n", + " 50.0\n", " \n", " \n", " 8\n", - " A\n", - " B\n", + " D\n", + " F\n", " 50.0\n", " \n", " \n", @@ -188,29 +188,38 @@ ], "text/plain": [ " source target distance\n", - "0 A F 160.0\n", - "1 A E 120.0\n", - "2 B F 110.0\n", - "3 C F 110.0\n", - "4 A D 90.0\n", - "5 B E 70.0\n", - "6 D F 70.0\n", - "7 C E 70.0\n", - "8 A B 50.0\n", + "0 A F 100.0\n", + "1 C F 90.0\n", + "2 B F 90.0\n", + "3 A E 80.0\n", + "4 B E 70.0\n", + "5 C E 70.0\n", + "6 A D 50.0\n", + "7 A B 50.0\n", + "8 D F 50.0\n", "9 A C 50.0" ] }, - "execution_count": 4, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "streaming_query = \"\"\"\n", - "CALL algo.allShortestPaths.stream('cost',{nodeQuery:'Loc',defaultValue:1.0})\n", + "CALL gds.alpha.allShortestPaths.stream({\n", + " nodeProjection:'Loc',\n", + " relationshipProjection:{\n", + " ALL:{\n", + " type:'*',\n", + " orientation:'NATURAL',\n", + " properties: {cost:{property:'cost', defaultValue:1.0}}\n", + " }\n", + " },\n", + " relationshipWeightProperty:'cost'})\n", "YIELD sourceNodeId, targetNodeId, distance\n", "WITH sourceNodeId, targetNodeId, distance \n", - "WHERE algo.isFinite(distance) = true\n", + "WHERE gds.util.isFinite(distance) = true\n", "\n", "MATCH (source:Loc) WHERE id(source) = sourceNodeId\n", "MATCH (target:Loc) WHERE id(target) = targetNodeId\n", @@ -222,8 +231,8 @@ "\"\"\"\n", "\n", "with driver.session() as session:\n", - " result = session.read_transaction(lambda tx: tx.run(streaming_query)) \n", - " df = pd.DataFrame([r.values() for r in result], columns=result.keys())\n", + " result = session.run(streaming_query)\n", + " df = pd.DataFrame([dict(row) for row in result])\n", "\n", "df" ] @@ -233,11 +242,36 @@ "metadata": {}, "source": [ "This query returned the top 10 pairs of nodes that are the furthest away from each other.\n", - "\"F\" and \"E\" seem to be quite distant from the others." + "Node \"F\" seem to be quite distant from the others." ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "scispacy", + "language": "python", + "name": "scispacy" + }, + "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.6.10" + } + }, "nbformat": 4, "nbformat_minor": 2 } diff --git a/notebooks/BetweennessCentrality.ipynb b/notebooks/BetweennessCentrality.ipynb index 2016a9f..fe5ac5c 100644 --- a/notebooks/BetweennessCentrality.ipynb +++ b/notebooks/BetweennessCentrality.ipynb @@ -7,8 +7,6 @@ "# Betweenness Centrality\n", "_Betweenness Centrality_ is a way of detecting the amount of influence a node has over the flow of information in a graph.\n", "\n", - "image::../images/betweenness_centrality.png[]\n", - "\n", "It is often used to find nodes that serve as a bridge from one part of a graph to another.\n", "In the above example Alice is the main connection in the graph.\n", "If Alice is removed all connections in the graph would be cut off.\n", @@ -19,11 +17,11 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "from neo4j.v1 import GraphDatabase, basic_auth\n", + "from neo4j import GraphDatabase\n", "import pandas as pd\n", "import os" ] @@ -37,14 +35,14 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "host = os.environ.get(\"NEO4J_HOST\", \"bolt://localhost\") \n", "user = os.environ.get(\"NEO4J_USER\", \"neo4j\")\n", "password = os.environ.get(\"NEO4J_PASSWORD\", \"neo\")\n", - "driver = GraphDatabase.driver(host, auth=basic_auth(user, password))" + "driver = GraphDatabase.driver(host, auth=(user, password))" ] }, { @@ -56,17 +54,9 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Stats: {}\n" - ] - } - ], + "outputs": [], "source": [ "create_graph_query = '''\n", "MERGE (nAlice:User {id:'Alice'})\n", @@ -97,99 +87,24 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": null, "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", - "
usercentrality
0Alice4.0
1Charles2.0
2Bridget0.0
3Doug0.0
4Mark0.0
5Michael0.0
\n", - "
" - ], - "text/plain": [ - " user centrality\n", - "0 Alice 4.0\n", - "1 Charles 2.0\n", - "2 Bridget 0.0\n", - "3 Doug 0.0\n", - "4 Mark 0.0\n", - "5 Michael 0.0" - ] - }, - "execution_count": 133, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "streaming_query = \"\"\"\n", - "CALL algo.betweenness.stream('User','MANAGE',{direction:'out'}) \n", + "CALL gds.alpha.betweenness.stream({\n", + " nodeProjection:'User',\n", + " relationshipProjection:'MANAGE'\n", + "})\n", "YIELD nodeId, centrality\n", - "\n", - "MATCH (user:User) WHERE id(user) = nodeId\n", - "\n", - "RETURN user.id AS user,centrality\n", + "RETURN gds.util.asNode(nodeId).id AS user,centrality\n", "ORDER BY centrality DESC\n", "LIMIT 20;\n", "\"\"\"\n", "\n", "with driver.session() as session:\n", - " result = session.read_transaction(lambda tx: tx.run(streaming_query)) \n", - " df = pd.DataFrame([r.values() for r in result], columns=result.keys())\n", + " result = session.run(streaming_query) \n", + " df = pd.DataFrame([dict(r) for r in result])\n", "\n", "df" ] @@ -212,13 +127,16 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "write_query = \"\"\"\n", - "CALL algo.betweenness('User','MANAGE', {direction:'out',write:true, writeProperty:'centrality'}) \n", - "YIELD nodes, minCentrality, maxCentrality, sumCentrality, loadMillis, computeMillis, writeMillis;\n", + "CALL gds.alpha.betweenness.write(\n", + " {nodeProjection:'User',\n", + " relationshipProjection:'MANAGE',\n", + " writeProperty:'centrality'}) \n", + "YIELD nodes, minCentrality, maxCentrality, sumCentrality, createMillis, computeMillis, writeMillis;\n", "\"\"\"\n", "\n", "with driver.session() as session:\n", @@ -239,38 +157,9 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - " \n", - "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%html\n", "