code: def louvain_partitions(
      G, weight="weight", resolution=1, threshold=0.0000001, seed=None
  ):
      """Yields partitions for each level of the Louvain Community Detection Algorithm
  
      Louvain Community Detection Algorithm is a simple method to extract the community
      structure of a network. This is a heuristic method based on modularity optimization. [1]_
  
      The partitions at each level (step of the algorithm) form a dendrogram of communities.
      A dendrogram is a diagram representing a tree and each level represents
      a partition of the G graph. The top level contains the smallest communities
      and as you traverse to the bottom of the tree the communities get bigger
      and the overall modularity increases making the partition better.
  
      Each level is generated by executing the two phases of the Louvain Community
      Detection Algorithm.
  
      Be careful with self-loops in the input graph. These are treated as
      previously reduced communities -- as if the process had been started
      in the middle of the algorithm. Large self-loop edge weights thus
      represent strong communities and in practice may be hard to add
      other nodes to.  If your input graph edge weights for self-loops
      do not represent already reduced communities you may want to remove
      the self-loops before inputting that graph.
  
      Parameters
      ----------
      G : NetworkX graph
      weight : string or None, optional (default="weight")
       The name of an edge attribute that holds the numerical value
       used as a weight. If None then each edge has weight 1.
      resolution : float, optional (default=1)
          If resolution is less than 1, the algorithm favors larger communities.
          Greater than 1 favors smaller communities
      threshold : float, optional (default=0.0000001)
       Modularity gain threshold for each level. If the gain of modularity
       between 2 levels of the algorithm is less than the given threshold
       then the algorithm stops and returns the resulting communities.
      seed : integer, random_state, or None (default)
       Indicator of random number generation state.
       See :ref:`Randomness<randomness>`.
  
      Yields
      ------
      list
          A list of sets (partition of `G`). Each set represents one community and contains
          all the nodes that constitute it.
  
      References
      ----------
      .. [1] Blondel, V.D. et al. Fast unfolding of communities in
         large networks. J. Stat. Mech 10008, 1-12(2008)
  
      See Also
      --------
      louvain_communities
      """
  
      partition = [{u} for u in G.nodes()]
      if nx.is_empty(G):
          yield partition
          return
      mod = modularity(
          G, partition, resolution=resolution, weight=weight
      )
      is_directed = G.is_directed()
      if G.is_multigraph():
          graph = _convert_multigraph(G, weight, is_directed)
      else:
          graph = G.__class__()
          graph.add_nodes_from(G)
          graph.add_weighted_edges_from(G.edges(data=weight, default=1))
  
      m = graph.size(weight="weight")
      partition, inner_partition, improvement = _one_level(
          graph, m, partition, resolution, is_directed, seed
      )
      improvement = True
      while improvement:
          # gh-5901 protect the sets in the yielded list from further manipulation here
          yield [s.copy() for s in partition]
          new_mod = modularity(
              graph,
              inner_partition,
              resolution=resolution,
              weight="weight",
          )
          if new_mod - mod <= threshold:
              return
          mod = new_mod
          graph = _gen_graph(graph, inner_partition)
          partition, inner_partition, improvement = _one_level(
              graph, m, partition, resolution, is_directed, seed
          )
  
file_path: networkx/networkx/algorithms/community/louvain.py
imports: from collections import defaultdict, deque
  import networkx
  from networkx.algorithms.community import modularity
  from networkx.utils import py_random_state
label_desc: Use image node_id 2 for calling a global function with example usage: louvain_partitions(G, weight, resolution, threshold, seed) without return types