code: def cluster_external_internal_edges(
      G, cluster_df, cluster_name="cluster"
  ):
      nclusters = pd.unique(cluster_df[cluster_name])
  
      nexternal_edges = []
      ninternal_edges = []
      for cl in nclusters:
          nodes_in_cluster = list(
              cluster_df.index[cluster_df[cluster_name] == cl].values
          )
          external = list(nx.edge_boundary(G, nodes_in_cluster))
          nexternal_edges.append(len(external))
          internal = G.subgraph(nodes_in_cluster)
          ninternal_edges.append(len(internal.edges()))
      df = pd.DataFrame(
          {
              "cluster": nclusters,
              "nexternal_edges": nexternal_edges,
              "ninternal_edges": ninternal_edges,
          }
      )
      df["ratio"] = df["ninternal_edges"] / df["nexternal_edges"]
      return df
  
file_path: stellargraph/demos/community_detection/utils.py
imports: import pandas
  import numpy
  import random
  import networkx
  from sklearn.cluster import DBSCAN
  from functools import reduce
  import matplotlib.pyplot
  import warnings
label_desc: Use image node_id 4 for calling a global function with example usage: cluster_external_internal_edges(G, cluster_df, cluster_name) and returns: df