code: def _reorder_matrix(mat, labels, reorder):
      """Help for plot_matrix.
  
      This function reorders the provided matrix.
      """
      if not labels:
          raise ValueError("Labels are needed to show the reordering.")
  
      linkage_matrix = linkage(mat, method=reorder)
      ordered_linkage = optimal_leaf_ordering(linkage_matrix, mat)
      index = leaves_list(ordered_linkage)
      # make sure labels is an ndarray and copy it
      labels = np.array(labels).copy()
      mat = mat.copy()
      # and reorder labels and matrix
      labels = labels[index].tolist()
      mat = mat[index, :][:, index]
      return mat, labels
  
imports: import warnings
  import matplotlib.patches
  import matplotlib.pyplot
  import numpy
  import pandas
  from mpl_toolkits.axes_grid1 import make_axes_locatable
  from scipy.cluster.hierarchy import leaves_list, linkage, optimal_leaf_ordering
  from nilearn.glm.first_level.experimental_paradigm import check_events
  from .._utils import fill_doc
label_desc: Use image node_id 7 for calling a global function with example usage: _reorder_matrix(mat, labels, reorder) and returns: mat,  labels