code: def _parent_child_df(s_matrix):
      """Extract the parent and child connections in a hierarchy.
  
      This function takes the summation S matrix for a given hierarchy and
      returns a dataframe containing a the parent and child node id for each
      connection in a hierarchy.
  
      Parameters
      ----------
      s_matrix :  The summation matrix for a given hierarchy from the function
          _get_s_matrix().
  
      Returns
      -------
      df : A two column pd.DataFrame with rows equal to the number of
          connections in a hierarchy.
      """
      parent_child = []
      total_count = s_matrix.index.to_frame()
      total_count = (total_count == "__total").sum(axis=1)
  
      # for each bottom node
      for i in s_matrix.columns:
          # get all connections
          connected_nodes = s_matrix[(s_matrix[i] == 1)].sum(axis=1)
          # for non-flattened hierarchies make sure "__totals" are above
          connected_nodes = (connected_nodes + total_count).dropna()
          connected_nodes = connected_nodes.sort_values(ascending=False)
  
          # starting from top add list of [parent, child]
          for j in range(len(connected_nodes.index) - 1):
              parent_child.append(
                  [
                      connected_nodes.index[j],
                      connected_nodes.index[j + 1],
                  ]
              )
  
      df = pd.DataFrame(parent_child)
      df.columns = ["parent", "child"]
  
      df = (
          df.drop_duplicates()
          .sort_values(["parent", "child"])
          .reset_index(drop=True)
      )
  
      return df
  
imports: import numpy
  import pandas
  from numpy.linalg import inv
  from sktime.transformations.base import BaseTransformer
  from sktime.transformations.hierarchical.aggregate import _check_index_no_total
  from sktime.utils.warnings import warn
label_desc: Use image node_id 7 for calling a global function with example usage: _parent_child_df(s_matrix) and returns: df