MCPcopy
hub / github.com/dmlc/dgl / test_create

Function test_create

tests/python/common/test_heterograph.py:182–333  ·  view source on GitHub ↗
(idtype)

Source from the content-addressed store, hash-verified

180
181@parametrize_idtype
182def test_create(idtype):
183 device = F.ctx()
184 g0 = create_test_heterograph(idtype)
185 g1 = create_test_heterograph1(idtype)
186 g2 = create_test_heterograph2(idtype)
187 assert set(g0.ntypes) == set(g1.ntypes) == set(g2.ntypes)
188 assert (
189 set(g0.canonical_etypes)
190 == set(g1.canonical_etypes)
191 == set(g2.canonical_etypes)
192 )
193
194 # Create a bipartite graph from a SciPy matrix
195 src_ids = np.array([2, 3, 4])
196 dst_ids = np.array([1, 2, 3])
197 eweight = np.array([0.2, 0.3, 0.5])
198 sp_mat = ssp.coo_matrix((eweight, (src_ids, dst_ids)))
199 g = dgl.bipartite_from_scipy(
200 sp_mat,
201 utype="user",
202 etype="plays",
203 vtype="game",
204 idtype=idtype,
205 device=device,
206 )
207 assert g.idtype == idtype
208 assert g.device == device
209 assert g.num_src_nodes() == 5
210 assert g.num_dst_nodes() == 4
211 assert g.num_edges() == 3
212 src, dst = g.edges()
213 assert F.allclose(src, F.tensor([2, 3, 4], dtype=idtype))
214 assert F.allclose(dst, F.tensor([1, 2, 3], dtype=idtype))
215 g = dgl.bipartite_from_scipy(
216 sp_mat,
217 utype="_U",
218 etype="_E",
219 vtype="_V",
220 eweight_name="w",
221 idtype=idtype,
222 device=device,
223 )
224 assert F.allclose(g.edata["w"], F.tensor(eweight))
225
226 # Create a bipartite graph from a NetworkX graph
227 nx_g = nx.DiGraph()
228 nx_g.add_nodes_from(
229 [1, 3], bipartite=0, feat1=np.zeros((2)), feat2=np.ones((2))
230 )
231 nx_g.add_nodes_from([2, 4, 5], bipartite=1, feat3=np.zeros((3)))
232 nx_g.add_edge(1, 4, weight=np.ones((1)), eid=np.array([1]))
233 nx_g.add_edge(3, 5, weight=np.ones((1)), eid=np.array([0]))
234 g = dgl.bipartite_from_networkx(
235 nx_g,
236 utype="user",
237 etype="plays",
238 vtype="game",
239 idtype=idtype,

Callers

nothing calls this directly

Calls 14

create_test_heterograph1Function · 0.85
_test_validate_bipartiteFunction · 0.85
num_src_nodesMethod · 0.80
num_dst_nodesMethod · 0.80
create_test_heterographFunction · 0.70
create_test_heterograph2Function · 0.70
ctxMethod · 0.45
num_edgesMethod · 0.45
edgesMethod · 0.45
add_edgeMethod · 0.45
num_nodesMethod · 0.45
graphMethod · 0.45

Tested by

no test coverage detected