Skip to content

Format notebooks with ruff #768

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

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions doc/modules/ROOT/pages/tutorials/centrality-algorithms.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ connection to the database.

[source, python, role=no-test]
----
from graphdatascience import GraphDataScience
import pandas as pd
import os

import pandas as pd

from graphdatascience import GraphDataScience
----

[source, python, role=no-test]
Expand Down Expand Up @@ -101,8 +103,8 @@ gds.run_cypher(

gds.run_cypher(
"""
UNWIND $rels AS rel
MATCH (source:City {node_id: rel.Origin}), (target:City {node_id: rel.Destination})
UNWIND $rels AS rel
MATCH (source:City {node_id: rel.Origin}), (target:City {node_id: rel.Destination})
CREATE (source)-[:HAS_FLIGHT_TO]->(target)
""",
params={"rels": routes_df.to_dict("records")},
Expand Down
12 changes: 7 additions & 5 deletions doc/modules/ROOT/pages/tutorials/community-detection.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ connection to the database.

[source, python, role=no-test]
----
from graphdatascience import GraphDataScience
import pandas as pd
import os

import pandas as pd

from graphdatascience import GraphDataScience
----

[source, python, role=no-test]
Expand Down Expand Up @@ -114,8 +116,8 @@ gds.run_cypher(

gds.run_cypher(
"""
UNWIND $rels AS rel
MATCH (source:Subreddit {name: rel.SOURCE_SUBREDDIT}), (target:Subreddit {name: rel.TARGET_SUBREDDIT})
UNWIND $rels AS rel
MATCH (source:Subreddit {name: rel.SOURCE_SUBREDDIT}), (target:Subreddit {name: rel.TARGET_SUBREDDIT})
CREATE (source)-[:HYPERLINKED_TO]->(target)
""",
params={"rels": relationship_df.to_dict("records")},
Expand Down Expand Up @@ -232,7 +234,7 @@ We can also check that the property was written by the below command.
----
gds.run_cypher(
"""
MATCH (n) WHERE 'louvainCommunityId' IN keys(n)
MATCH (n) WHERE 'louvainCommunityId' IN keys(n)
RETURN n.name, n.louvainCommunityId LIMIT 10
"""
)
Expand Down
1 change: 1 addition & 0 deletions doc/modules/ROOT/pages/tutorials/fastrp-and-knn.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ our GDS client connection to the database.
[source, python, role=no-test]
----
import os

from graphdatascience import GraphDataScience

# Get Neo4j DB URI and credentials from environment if applicable
Expand Down
12 changes: 8 additions & 4 deletions doc/modules/ROOT/pages/tutorials/gds-sessions-self-managed.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ version `+1.12a1+` or later.
[source, python, role=no-test]
----
from datetime import timedelta

%pip install "graphdatascience>=1.12a1"
----

Expand All @@ -50,7 +51,8 @@ is the main entry point for managing GDS Sessions.
[source, python, role=no-test]
----
import os
from graphdatascience.session import GdsSessions, AuraAPICredentials

from graphdatascience.session import AuraAPICredentials, GdsSessions

client_id = os.environ["AURA_API_CLIENT_ID"]
client_secret = os.environ["AURA_API_CLIENT_SECRET"]
Expand Down Expand Up @@ -83,11 +85,13 @@ delete the session ourselves.
[source, python, role=no-test]
----
import os
from graphdatascience.session import DbmsConnectionInfo, AlgorithmCategory, CloudLocation
from datetime import timedelta

from graphdatascience.session import AlgorithmCategory, CloudLocation, DbmsConnectionInfo

# Identify the Neo4j DBMS
db_connection = DbmsConnectionInfo(uri=os.environ["NEO4J_URI"], username=os.environ["NEO4J_USER"], password=os.environ["NEO4J_PASSWORD"])
db_connection = DbmsConnectionInfo(
uri=os.environ["NEO4J_URI"], username=os.environ["NEO4J_USER"], password=os.environ["NEO4J_PASSWORD"]
)
# Specify where to create the GDS session
cloud_location = CloudLocation(provider="gcp", region="europe-west1")

Expand Down
10 changes: 7 additions & 3 deletions doc/modules/ROOT/pages/tutorials/gds-sessions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ is the main entry point for managing GDS Sessions.
[source, python, role=no-test]
----
import os
from graphdatascience.session import GdsSessions, AuraAPICredentials

from graphdatascience.session import AuraAPICredentials, GdsSessions

client_id = os.environ["AURA_API_CLIENT_ID"]
client_secret = os.environ["AURA_API_CLIENT_SECRET"]
Expand Down Expand Up @@ -84,10 +85,13 @@ delete the session ourselves.
----
import os
from datetime import timedelta
from graphdatascience.session import DbmsConnectionInfo, AlgorithmCategory

from graphdatascience.session import AlgorithmCategory, DbmsConnectionInfo

# Identify the AuraDB instance
db_connection = DbmsConnectionInfo(uri=os.environ["AURA_DB_ADDRESS"], username=os.environ["AURA_DB_USER"], password=os.environ["AURA_DB_PW"])
db_connection = DbmsConnectionInfo(
uri=os.environ["AURA_DB_ADDRESS"], username=os.environ["AURA_DB_USER"], password=os.environ["AURA_DB_PW"]
)
# Create a GDS session!
memory = sessions.estimate(
node_count=20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ connection to the database.
----
# Import our dependencies
import os

from graphdatascience import GraphDataScience
----

Expand Down
11 changes: 6 additions & 5 deletions doc/modules/ROOT/pages/tutorials/import-sample-export-gnn.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ connection to the database.
[source, python, role=no-test]
----
import os
import pandas as pd
from graphdatascience import GraphDataScience
import random

import numpy as np
import torch
from torch_geometric.data import Data
import torch.nn.functional as F
from torch_geometric.data import Data
from torch_geometric.nn import GCNConv
from torch_geometric.transforms import RandomNodeSplit
import random
import numpy as np

from graphdatascience import GraphDataScience
----

[source, python, role=no-test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ connection to the database.

[source, python, role=no-test]
----
import collections
import os
from graphdatascience import GraphDataScience

import pandas as pd
import torch
import torch.optim as optim
from torch_geometric.data import Data, download_url
from torch_geometric.nn import TransE
import collections
from tqdm import tqdm
import pandas as pd

from graphdatascience import GraphDataScience
----

[source, python, role=no-test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ the `+graphdatascience+` package and import the client class.
[source, python, role=no-test]
----
import os

from graphdatascience import GraphDataScience
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ install the `+graphdatascience+` package and import the client class.
[source, python, role=no-test]
----
import os

from graphdatascience import GraphDataScience
----

Expand Down
24 changes: 13 additions & 11 deletions examples/centrality-algorithms.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"\n",
"This notebook will show how you can apply eigenvector centrality, betweenness centrality, degree centrality and closeness centrality on a graph dataset.\n",
"\n",
"### Setup\n",
"## Setup\n",
"\n",
"We start by importing our dependencies and setting up our GDS client connection to the database."
]
Expand All @@ -51,9 +51,11 @@
"metadata": {},
"outputs": [],
"source": [
"from graphdatascience import GraphDataScience\n",
"import os\n",
"\n",
"import pandas as pd\n",
"import os"
"\n",
"from graphdatascience import GraphDataScience"
]
},
{
Expand Down Expand Up @@ -92,7 +94,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importing the dataset\n",
"## Importing the dataset\n",
"\n",
"We import the dataset as a pandas dataframe first. We deal with two files here. The file `reachability-meta.csv.gz` stores the names of the cities and their information while the file `reachability.txt.gz` stores the edges of the graph. An edge exists from city `i` to city `j` if the estimated airline travel time is less than a threshold.\n"
]
Expand Down Expand Up @@ -146,8 +148,8 @@
"\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",
" 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",
Expand All @@ -174,7 +176,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Eigenvector Centrality\n",
"## Eigenvector Centrality\n",
"\n",
"[Eigenvector centrality](https://neo4j.com/docs/graph-data-science/current/algorithms/eigenvector-centrality/) measures the importance or influence of a node based on its connections to other nodes in the network. A higher eigenvector centrality score suggests that a node is more central and influential within the network.\n",
"\n",
Expand Down Expand Up @@ -289,7 +291,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Betweenness Centrality\n",
"## Betweenness Centrality\n",
"\n",
"[Betweenness Centrality](https://neo4j.com/docs/graph-data-science/current/algorithms/betweenness-centrality/) quantifies the importance of a node as a bridge or intermediary in the network. It measures how often a node lies on the shortest path between other pairs of nodes. \n",
"\n",
Expand Down Expand Up @@ -367,7 +369,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Degree Centrality\n",
"## Degree Centrality\n",
"\n",
"[Degree Centrality](https://neo4j.com/docs/graph-data-science/current/algorithms/degree-centrality/) measures the number of connections (edges) a node has in the network. \n",
"\n",
Expand Down Expand Up @@ -445,7 +447,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cleanup\n",
"## Cleanup\n",
"\n",
"Before finishing we can clean up the example data from both the GDS in-memory state and the database."
]
Expand Down Expand Up @@ -474,7 +476,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### References\n",
"## 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",
Expand Down
26 changes: 14 additions & 12 deletions examples/community-detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"\n",
"The tasks we cover here include performing initial graph preprocessing using Weakly Connected Components and then performing community detection on the largest component using the Louvain algorithm.\n",
"\n",
"### Setup\n",
"## Setup\n",
"\n",
"We start by importing our dependencies and setting up our GDS client connection to the database."
]
Expand All @@ -56,9 +56,11 @@
"metadata": {},
"outputs": [],
"source": [
"from graphdatascience import GraphDataScience\n",
"import os\n",
"\n",
"import pandas as pd\n",
"import os"
"\n",
"from graphdatascience import GraphDataScience"
]
},
{
Expand Down Expand Up @@ -100,7 +102,7 @@
"id": "48bd8af1",
"metadata": {},
"source": [
"### Importing the dataset\n",
"## Importing the dataset\n",
"\n",
"We import the dataset as a pandas dataframe first. We work with only a subset of the dataset. The sampled data is only till 1st March 2014. "
]
Expand Down Expand Up @@ -187,8 +189,8 @@
"\n",
"gds.run_cypher(\n",
" \"\"\"\n",
" UNWIND $rels AS rel \n",
" MATCH (source:Subreddit {name: rel.SOURCE_SUBREDDIT}), (target:Subreddit {name: rel.TARGET_SUBREDDIT}) \n",
" UNWIND $rels AS rel\n",
" MATCH (source:Subreddit {name: rel.SOURCE_SUBREDDIT}), (target:Subreddit {name: rel.TARGET_SUBREDDIT})\n",
" CREATE (source)-[:HYPERLINKED_TO]->(target)\n",
" \"\"\",\n",
" params={\"rels\": relationship_df.to_dict(\"records\")},\n",
Expand Down Expand Up @@ -226,7 +228,7 @@
"id": "9c259471",
"metadata": {},
"source": [
"### Weakly Connected Components\n",
"## Weakly Connected Components\n",
"\n",
"A graph dataset need not always be connected. That is, there may not exist a path from every node to \n",
"every other node in the graph dataset (subgraphs in it may not be connected to each other at all). Hence, we \n",
Expand Down Expand Up @@ -332,7 +334,7 @@
"id": "17942d04",
"metadata": {},
"source": [
"### Community Detection using Louvain\n",
"## Community Detection using Louvain\n",
"\n",
"We use the [Louvain](https://neo4j.com/docs/graph-data-science/current/algorithms/louvain/) algorithm to detect communities in our subgraph and assign a `louvainCommunityId` to each community."
]
Expand Down Expand Up @@ -382,7 +384,7 @@
"source": [
"gds.run_cypher(\n",
" \"\"\"\n",
" MATCH (n) WHERE 'louvainCommunityId' IN keys(n) \n",
" MATCH (n) WHERE 'louvainCommunityId' IN keys(n)\n",
" RETURN n.name, n.louvainCommunityId LIMIT 10\n",
" \"\"\"\n",
")"
Expand Down Expand Up @@ -424,7 +426,7 @@
"id": "5ed56f82",
"metadata": {},
"source": [
"### Further ideas\n",
"## Further ideas\n",
"\n",
"* Inspect the produced communities using [Bloom](https://neo4j.com/docs/bloom-user-guide/current/). You can use rule-based styling based on the community property.\n",
"* Try to tune more parameters of Louvain and see how the communities differ.\n",
Expand All @@ -437,7 +439,7 @@
"id": "6e00ed7b",
"metadata": {},
"source": [
"### Cleanup\n",
"## Cleanup\n",
"\n",
"Before finishing we can clean up the example data from both the GDS in-memory state and the database."
]
Expand Down Expand Up @@ -471,7 +473,7 @@
"id": "65dcb952",
"metadata": {},
"source": [
"### References\n",
"## References\n",
"\n",
"Srijan Kumar, William L. Hamilton, Jure Leskovec, and Dan Jurafsky. 2018. Community Interaction and Conflict on the Web. In Proceedings of the 2018 World Wide Web Conference (WWW '18). International World Wide Web Conferences Steering Committee, Republic and Canton of Geneva, CHE, 933–943. https://doi.org/10.1145/3178876.3186141"
]
Expand Down
1 change: 1 addition & 0 deletions examples/fastrp-and-knn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"outputs": [],
"source": [
"import os\n",
"\n",
"from graphdatascience import GraphDataScience\n",
"\n",
"# Get Neo4j DB URI and credentials from environment if applicable\n",
Expand Down
Loading