_ncut.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import networkx as nx
  2. import numpy as np
  3. from scipy import sparse
  4. from . import _ncut_cy
  5. def DW_matrices(graph):
  6. """Returns the diagonal and weight matrices of a graph.
  7. Parameters
  8. ----------
  9. graph : RAG
  10. A Region Adjacency Graph.
  11. Returns
  12. -------
  13. D : csc_array
  14. The diagonal matrix of the graph. ``D[i, i]`` is the sum of weights of
  15. all edges incident on `i`. All other entries are `0`.
  16. W : csc_array
  17. The weight matrix of the graph. ``W[i, j]`` is the weight of the edge
  18. joining `i` to `j`.
  19. """
  20. # sparse.eighsh is most efficient with CSC-formatted input
  21. W = nx.to_scipy_sparse_array(graph, format='csc')
  22. entries = W.sum(axis=0)
  23. D = sparse.dia_array((entries, 0), shape=W.shape).tocsc()
  24. return D, W
  25. def ncut_cost(cut, D, W):
  26. """Returns the N-cut cost of a bi-partition of a graph.
  27. Parameters
  28. ----------
  29. cut : ndarray
  30. The mask for the nodes in the graph. Nodes corresponding to a `True`
  31. value are in one set.
  32. D : csc_array
  33. The diagonal matrix of the graph.
  34. W : csc_array
  35. The weight matrix of the graph.
  36. Returns
  37. -------
  38. cost : float
  39. The cost of performing the N-cut.
  40. References
  41. ----------
  42. .. [1] Normalized Cuts and Image Segmentation, Jianbo Shi and
  43. Jitendra Malik, IEEE Transactions on Pattern Analysis and Machine
  44. Intelligence, Page 889, Equation 2.
  45. """
  46. cut = np.array(cut)
  47. cut_cost = _ncut_cy.cut_cost(cut, W.data, W.indices, W.indptr, num_cols=W.shape[0])
  48. # D has elements only along the diagonal, one per node, so we can directly
  49. # index the data attribute with cut.
  50. assoc_a = D.data[cut].sum()
  51. assoc_b = D.data[~cut].sum()
  52. return (cut_cost / assoc_a) + (cut_cost / assoc_b)