Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Centrality Algorithms Example Jupyter Notebook #386

Merged
merged 7 commits into from
Jun 20, 2023

Conversation

kedarghule
Copy link
Contributor

This PR adds an example jupyter notebook for centrality algorithms on the airline travel reachability network dataset. The notebook explores eigenvector centrality, betweenness centrality & degree centrality.

This is in reference to my comment in the issue: #254

Thank you :)

@netlify
Copy link

netlify bot commented May 31, 2023

Deploy Preview for neo4j-graph-data-science-client canceled.

Name Link
🔨 Latest commit 34d5ea1
🔍 Latest deploy log https://app.netlify.com/sites/neo4j-graph-data-science-client/deploys/64915023b2c88b000835a802

@Mats-SX Mats-SX self-assigned this Jun 1, 2023
@kedarghule
Copy link
Contributor Author

Umm. I don't know why one check is not running :/

@Mats-SX
Copy link
Contributor

Mats-SX commented Jun 8, 2023

@kedarghule The check not running is normal. For security reasons we do not run PR builds from non-members. There have been several attacks made on our public repositories to attempt to expose secrets from our build system.

I apologise for the delayed review. We are in the process of releasing GDS 2.4.0 which is taking up most of my time lately. We are grateful for your contribution, and you are not forgotten :)

All the best
Mats

@kedarghule
Copy link
Contributor Author

@kedarghule The check not running is normal. For security reasons we do not run PR builds from non-members. There have been several attacks made on our public repositories to attempt to expose secrets from our build system.

I apologise for the delayed review. We are in the process of releasing GDS 2.4.0 which is taking up most of my time lately. We are grateful for your contribution, and you are not forgotten :)

All the best Mats

Alright @Mats-SX and sure, no problem :)

Copy link
Contributor

@Mats-SX Mats-SX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this contribution! It's a great notebook. I only have a few comments.

I will push a commit that clears output cells and applies our code style rules to the Python code. For reference, we have two scripts in the scripts directory: checkstyle and makestyle that can be used (just run them as scripts/checkstyle without arguments) to manage this in future contributions.

I will also create a sibling PR to allow our CI to run.

Comment on lines +324 to +133
"gds.run_cypher(\n",
" \"UNWIND $nodes AS node CREATE (n:City {node_id: node.node_id, name: node.name, population: node.metro_pop})\",\n",
" params={\"nodes\": nodes_info_df.to_dict(\"records\")},\n",
")\n",
"\n",
"gds.run_cypher(\n",
" \"\"\"\n",
" UNWIND $rels AS rel \n",
" MATCH (source:City {node_id: rel.Origin}), (target:City {node_id: rel.Destination}) \n",
" CREATE (source)-[:HAS_FLIGHT_TO]->(target)\n",
" \"\"\",\n",
" params={\"rels\": routes_df.to_dict(\"records\")},\n",
")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method of importing data to the database is a good choice for a graph of this size (small).

"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we load this data (nodes and edges) into a Graph Database and a GDS graph."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relating to the comment on the next section, I think it would be good to add a small note here that for graphs of larger size (like, 100k to millions of nodes) other importing measures should be considered (batching, neo4j-admin import or Arrow CREATE DATABASE).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just have something like

Since this graph is very small, a straight-forward Cypher UNWIND query is the simplest way to create our graph in the database.
Larger graphs may need a more sophisticated importing technique.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Done :)

Comment on lines 366 to 148
"# We can use convenience methods on `G` to check if the projection looks correct\n",
"print(f\"Graph '{G.name()}' node count: {G.node_count()}\")\n",
"print(f\"Graph '{G.name()}' node labels: {G.node_labels()}\")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to also print the relationshipCount here, and maybe comment that the counts match the expected counts from the SNAP website

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment on lines 442 to 199
"if eigenvector_centrality_result.didConverge: \n",
" print(f\"The number of iterations taken by Eigenvector Centrality to run is {eigenvector_centrality_result.ranIterations}.\")\n",
"else:\n",
" print(\"Algorithm did not converge!\")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very elegant!

Comment on lines 460 to 473
{
"data": {
"text/plain": [
"{'p99': 0.08469437807798386,\n",
" 'min': 0.0012630745768547058,\n",
" 'max': 0.0856785699725151,\n",
" 'mean': 0.041094380038741385,\n",
" 'p90': 0.07575749605894089,\n",
" 'p50': 0.0386042520403862,\n",
" 'p999': 0.0856785699725151,\n",
" 'p95': 0.08167456835508347,\n",
" 'p75': 0.05822562426328659}"
]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better if we commit the notebook without cell outputs. Could you clear these and commit with empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think you already did this for me. Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed that our makestyle script does this automatically :)

Comment on lines +503 to +225
"source": [
"gds.graph.nodeProperties.write(G, [\"eigenvectorCentrality\"])"
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have a small markdown cell before this one that tells the reader that we are about to write our results back to the database

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment on lines +671 to +312
"source": [
"gds.graph.nodeProperties.write(G, [\"betweennessCentrality\"])"
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also add a small sentence about this operation

Comment on lines 958 to 442
"source": [
"### References\n",
"- For the network:\n",
"Brendan J. Frey and Delbert Dueck. \"Clustering by passing messages between data points.\" Science 315.5814 (2007): 972-976.\n",
"\n",
"- For the city metadata (metropolitan population, latitude, and longitude):\n",
"Austin R. Benson, David F. Gleich, and Jure Leskovec. \"Higher-order Organization of Complex Networks.\" Science, 353.6295 (2016): 163–166.\n",
"\n",
"- Notebook contributed by [Kedar Ghule](https://github.com/kedarghule)"
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good list! Maybe let's also add the link to the SNAP dataset page that we had in the introduction for completeness

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@Mats-SX Mats-SX force-pushed the centrality-notebook-kedar branch from 2dc053c to 2fdd333 Compare June 19, 2023 12:00
@Mats-SX Mats-SX force-pushed the centrality-notebook-kedar branch from 2fdd333 to 5bbbb46 Compare June 19, 2023 12:20
@Mats-SX Mats-SX mentioned this pull request Jun 19, 2023
@kedarghule
Copy link
Contributor Author

@Mats-SX I have addressed your comments for the PR and made a new commit. Please let me know if anything else is needed or if I missed anything.

Also the latest commit won't run a check again :/

@Mats-SX Mats-SX enabled auto-merge June 20, 2023 07:07
Comment on lines 472 to 487
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff was added back in, which is normal when the notebook is run. I will clear it out again.

@Mats-SX
Copy link
Contributor

Mats-SX commented Jun 20, 2023

@kedarghule Thank you! I've kicked off another CI build.

@Mats-SX Mats-SX merged commit a410c14 into neo4j:main Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants