Skip to content

Commit 8ac11b5

Browse files
supracdjrgemignani
authored andcommitted
Added Networkx Support in python driver (#1716)
Co-authored-by: munmud <moontasir042@gmail.com> Resolved - Conflicts: .github/workflows/python-driver.yaml drivers/python/requirements.txt drivers/python/setup.py
1 parent 7bdb9ec commit 8ac11b5

File tree

10 files changed

+1148
-2
lines changed

10 files changed

+1148
-2
lines changed

.github/workflows/python-driver.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ jobs:
4040
- name: Test
4141
run: |
4242
python test_age_py.py -db "postgres" -u "postgres" -pass "agens"
43-
python -m unittest -v test_agtypes.py
43+
python test_networkx.py -db "postgres" -u "postgres" -pass "agens"
44+
python -m unittest -v test_agtypes.py

drivers/python/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,74 @@ SET search_path = ag_catalog, "$user", public;
7777

7878
### License
7979
Apache-2.0 License
80+
81+
82+
## Networkx
83+
### Netowkx Unit test
84+
```
85+
python test_networkx.py \
86+
-host "127.0.0.1" \
87+
-db "postgres" \
88+
-u "postgres" \
89+
-pass "agens" \
90+
-port 5432
91+
```
92+
Here the following value required
93+
- `-host` : host name (optional)
94+
- `-db` : database name
95+
- `-u` : user name
96+
- `-pass` : password
97+
- `-port` : port (optional)
98+
99+
### Networkx to AGE
100+
Insert From networkx directed graph into an Age database.
101+
#### Parameters
102+
103+
- `connection` (psycopg2.connect): Connection object to the Age database.
104+
105+
- `G` (networkx.DiGraph): Networkx directed graph to be converted and inserted.
106+
107+
- `graphName` (str): Name of the age graph.
108+
109+
#### Returns
110+
111+
None
112+
113+
#### Example
114+
115+
```python
116+
117+
# Create a Networkx DiGraph
118+
G = nx.DiGraph()
119+
G.add_node(1)
120+
G.add_node(2)
121+
G.add_edge(1, 2)
122+
123+
# Convert and insert the graph into the Age database
124+
graphName = "sample_graph"
125+
networkx_to_age(connection, G, graphName)
126+
```
127+
128+
129+
130+
### AGE to Netowkx
131+
132+
Converts data from a Apache AGE graph database into a Networkx directed graph.
133+
134+
#### Parameters
135+
136+
- `connection` (psycopg2.connect): Connection object to the PostgreSQL database.
137+
- `graphName` (str): Name of the graph.
138+
- `G` (None | nx.DiGraph): Optional Networkx directed graph. If provided, the data will be added to this graph.
139+
- `query` (str | None): Optional Cypher query to retrieve data from the database.
140+
141+
#### Returns
142+
143+
- `nx.DiGraph`: Networkx directed graph containing the converted data.
144+
145+
#### Example
146+
147+
```python
148+
# Call the function to convert data into a Networkx graph
149+
graph = age_to_networkx(connection, graphName="MyGraph" )
150+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an
11+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12+
# KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations
14+
# under the License.
15+
16+
from .networkx_to_age import networkx_to_age
17+
from .age_to_networkx import age_to_networkx
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an
11+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12+
# KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations
14+
# under the License.
15+
16+
from age import *
17+
import psycopg2
18+
import networkx as nx
19+
from age.models import Vertex, Edge, Path
20+
from .lib import *
21+
22+
23+
def age_to_networkx(connection: psycopg2.connect,
24+
graphName: str,
25+
G: None | nx.DiGraph = None,
26+
query: str | None = None
27+
) -> nx.DiGraph:
28+
"""
29+
@params
30+
---------------------
31+
connection - (psycopg2.connect) Connection object
32+
graphName - (str) Name of the graph
33+
G - (networkx.DiGraph) Networkx directed Graph [optional]
34+
query - (str) Cypher query [optional]
35+
36+
@returns
37+
------------
38+
Networkx directed Graph
39+
40+
"""
41+
42+
# Check if the age graph exists
43+
checkIfGraphNameExistInAGE(connection, graphName)
44+
45+
# Create an empty directed graph
46+
if G == None:
47+
G = nx.DiGraph()
48+
49+
def addNodeToNetworkx(node):
50+
"""Add Nodes in Networkx"""
51+
G.add_node(node.id,
52+
label=node.label,
53+
properties=node.properties)
54+
55+
def addEdgeToNetworkx(edge):
56+
"""Add Edge in Networkx"""
57+
G.add_edge(edge.start_id,
58+
edge.end_id,
59+
label=edge.label,
60+
properties=edge.properties)
61+
62+
def addPath(path):
63+
"""Add Edge in Networkx"""
64+
for x in path:
65+
if (type(x) == Path):
66+
addPath(x)
67+
for x in path:
68+
if (type(x) == Vertex):
69+
addNodeToNetworkx(x)
70+
for x in path:
71+
if (type(x) == Edge):
72+
addEdgeToNetworkx(x)
73+
74+
# Setting up connection to work with Graph
75+
age.setUpAge(connection, graphName)
76+
77+
if (query == None):
78+
addAllNodesIntoNetworkx(connection, graphName, G)
79+
addAllEdgesIntoNetworkx(connection, graphName, G)
80+
else:
81+
with connection.cursor() as cursor:
82+
cursor.execute(query)
83+
rows = cursor.fetchall()
84+
for row in rows:
85+
for x in row:
86+
if type(x) == Path:
87+
addPath(x)
88+
elif type(x) == Edge:
89+
addEdgeToNetworkx(x)
90+
elif type(x) == Vertex:
91+
addNodeToNetworkx(x)
92+
return G

0 commit comments

Comments
 (0)