code: def wgraph_from_coo_matrix(x):
      """
      Instantiates a weighted graph from a (sparse) coo_matrix
  
      Parameters
      ----------
      x: scipy.sparse.coo_matrix instance, the input matrix
  
      Returns
      -------
      wg: WeightedGraph instance
      """
      if x.shape[0] != x.shape[1]:
          raise ValueError("the input coo_matrix is not square")
      i, j = x.nonzero()
      edges = np.vstack((i, j)).T
      weights = x.data
      wg = WeightedGraph(x.shape[0], edges, weights)
      return wg
  
imports: import numpy
  from scipy.sparse import coo_matrix
label_desc: Use image node_id 1 for calling a global function with example usage: wgraph_from_coo_matrix(x) and returns: wg