MCPcopy Create free account
hub / github.com/PythonOT/POT / get_graph_representants

Function get_graph_representants

ot/gromov/_quantized.py:384–454  ·  view source on GitHub ↗

r""" Get representative node for each partition given by :math:`\mathbf{part} \in R^{n}` of a graph with structure matrix :math:`\mathbf{C} \in R^{n \times n}`. Selection is either done randomly or using 'pagerank' algorithm from networkx. Parameters ---------- C : array-lik

(C, part, rep_method="pagerank", random_state=0, nx=None)

Source from the content-addressed store, hash-verified

382
383
384def get_graph_representants(C, part, rep_method="pagerank", random_state=0, nx=None):
385 r"""
386 Get representative node for each partition given by :math:`\mathbf{part} \in R^{n}`
387 of a graph with structure matrix :math:`\mathbf{C} \in R^{n \times n}`.
388 Selection is either done randomly or using 'pagerank' algorithm from networkx.
389
390 Parameters
391 ----------
392 C : array-like, shape (n, n)
393 structure matrix.
394 part : array-like, shape (n,)
395 Array of partition assignment for each node.
396 rep_method : str, optional. Default is 'pagerank'.
397 Selection method for representant in each partition. Can be either 'random'
398 i.e random sampling within each partition, or 'pagerank' to select a
399 node with maximal pagerank.
400 random_state: int, optional
401 Random seed for the partitioning algorithm
402 nx : backend, optional
403 POT backend
404
405 Returns
406 -------
407 rep_indices : list, shape (npart,)
408 indices for representative node of each partition sorted
409 according to partition identifiers.
410
411 References
412 ----------
413 .. [68] Chowdhury, S., Miller, D., & Needham, T. (2021).
414 Quantized gromov-wasserstein. ECML PKDD 2021. Springer International Publishing.
415
416 """
417 if nx is None:
418 nx = get_backend(C, part)
419
420 rep_indices = []
421 part_ids = nx.unique(part)
422 n_part_ids = part_ids.shape[0]
423 if n_part_ids == C.shape[0]:
424 rep_indices = nx.arange(n_part_ids)
425
426 elif rep_method == "random":
427 random.seed(random_state)
428 for id_, part_id in enumerate(part_ids):
429 indices = nx.where(part == part_id)[0]
430 rep_indices.append(random.choice(indices))
431
432 elif rep_method == "pagerank":
433 C0, part0 = C, part
434 C = nx.to_numpy(C0)
435 part = nx.to_numpy(part0)
436 part_ids = np.unique(part)
437
438 for id_ in part_ids:
439 indices = np.where(part == id_)[0]
440 C_id = C[indices, :][:, indices]
441 graph = from_numpy_array(C_id)

Calls 7

get_backendFunction · 0.85
to_numpyMethod · 0.80
uniqueMethod · 0.45
arangeMethod · 0.45
seedMethod · 0.45
whereMethod · 0.45
argmaxMethod · 0.45

Tested by

no test coverage detected