-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_test.py
176 lines (154 loc) · 5.67 KB
/
pg_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import time
import gc
import pytest
import pandas as pd
import numpy as np
import cudf
from cudf.testing import assert_frame_equal, assert_series_equal
import cugraph
from cugraph.generators import rmat
from cugraph.tests import utils
import dgl
import cupy
dataset1 = {
"merchants": [
["merchant_id", "merchant_location", "merchant_size", "merchant_sales",
"merchant_num_employees", "merchant_name"],
[(11, 78750, 44, 123.2, 12, "north"),
(4, 78757, 112, 234.99, 18, "south"),
(21, 44145, 83, 992.1, 27, "east"),
(16, 47906, 92, 32.43, 5, "west"),
(86, 47906, 192, 2.43, 51, "west"),
]
],
"users": [
["user_id", "user_location", "vertical"],
[(89021, 78757, 0),
(32431, 78750, 1),
(89216, 78757, 1),
(78634, 47906, 0),
]
],
"taxpayers": [
["payer_id", "amount"],
[(11, 1123.98),
(4, 3243.7),
(21, 8932.3),
(16, 3241.77),
(86, 789.2),
(89021, 23.98),
(78634, 41.77),
]
],
"transactions": [
["user_id", "merchant_id", "volume", "time", "card_num", "card_type"],
[(89021, 11, 33.2, 1639084966.5513437, 123456, "MC"),
(89216, 4, None, 1639085163.481217, 8832, "CASH"),
(78634, 16, 72.0, 1639084912.567394, 4321, "DEBIT"),
(32431, 4, 103.2, 1639084721.354346, 98124, "V"),
]
],
"relationships": [
["user_id_1", "user_id_2", "relationship_type"],
[(89216, 89021, 9),
(89216, 32431, 9),
(32431, 78634, 8),
(78634, 89216, 8),
]
],
"referrals": [
["user_id_1", "user_id_2", "merchant_id", "stars"],
[(89216, 78634, 11, 5),
(89021, 89216, 4, 4),
(89021, 89216, 21, 3),
(89021, 89216, 11, 3),
(89021, 78634, 21, 4),
(78634, 32431, 11, 4),
]
],
}
def dataset1_PropertyGraph(request):
"""
Fixture which returns an instance of a PropertyGraph with vertex and edge
data added from dataset1, parameterized for different DataFrame types.
"""
dataframe_type = request
from cugraph.experimental import PropertyGraph
(merchants, users, taxpayers,
transactions, relationships, referrals) = dataset1.values()
pG = PropertyGraph()
# Vertex and edge data is added as one or more DataFrames; either a Pandas
# DataFrame to keep data on the CPU, a cuDF DataFrame to keep data on GPU,
# or a dask_cudf DataFrame to keep data on distributed GPUs.
# For dataset1: vertices are merchants and users, edges are transactions,
# relationships, and referrals.
# property_columns=None (the default) means all columns except
# vertex_col_name will be used as properties for the vertices/edges.
pG.add_vertex_data(dataframe_type(columns=merchants[0],
data=merchants[1]),
type_name="merchants",
vertex_col_name="merchant_id",
property_columns=None)
pG.add_vertex_data(dataframe_type(columns=users[0],
data=users[1]),
type_name="users",
vertex_col_name="user_id",
property_columns=None)
pG.add_vertex_data(dataframe_type(columns=taxpayers[0],
data=taxpayers[1]),
type_name="taxpayers",
vertex_col_name="payer_id",
property_columns=None)
pG.add_edge_data(dataframe_type(columns=transactions[0],
data=transactions[1]),
type_name="transactions",
vertex_col_names=("user_id", "merchant_id"),
property_columns=None)
pG.add_edge_data(dataframe_type(columns=relationships[0],
data=relationships[1]),
type_name="relationships",
vertex_col_names=("user_id_1", "user_id_2"),
property_columns=None)
pG.add_edge_data(dataframe_type(columns=referrals[0],
data=referrals[1]),
type_name="referrals",
vertex_col_names=("user_id_1",
"user_id_2"),
property_columns=None)
return pG
def toDGL(graph):
# input is cugraph graph
# output is DGL graph
edgelist = graph.edges()
src = cupy.asarray(edgelist['src'])
dst = cupy.asarray(edgelist['dst'])
g_dgl = dgl.graph((src, dst))
return g_dgl
if __name__ == '__main__':
pG= dataset1_PropertyGraph(cudf.DataFrame)
print ("edges prop")
print (pG._edge_prop_dataframe)
print ("node prop")
print (pG._vertex_prop_dataframe)
ndata = pG._vertex_prop_dataframe
print (ndata['_TYPE_']== 'merchants')
print (ndata[ndata['_TYPE_']== 'merchants'])
print (ndata[ndata['_TYPE_']== 'merchants']['merchant_id'])
edata = pG._edge_prop_dataframe
selected_edges = edata[edata['_TYPE_'] == 'transactions'].iloc[[1,2]]
print(selected_edges['_SRC_'])
print(ndata[ndata['_TYPE_']== 'merchants'].shape[0])
g = cugraph.Graph()
df = cudf.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
g.from_cudf_edgelist(df, source="a", destination="b")
print (g.nodes())
print (g.edges())
edgelist = g.edges()
print (edgelist)
#g_dgl = dgl.DGLGraph(edgelist)
print("dst", edgelist['dst'])
src = cupy.asarray(edgelist['src'])
dst = cupy.asarray(edgelist['dst'])
print (dst)
g_dgl = dgl.graph((src, dst))
print (g_dgl)