code: def posthoc_npm_test(
      a: Union[list, np.ndarray, DataFrame],
      val_col: str = None,
      group_col: str = None,
      sort: bool = False,
  ) -> DataFrame:
      """Calculate pairwise comparisons using Nashimoto and Wright´s all-pairs
      comparison procedure (NPM test) for simply ordered mean ranksums.
  
      NPM test is basically an extension of Nemenyi´s procedure for testing
      increasingly ordered alternatives [1]_.
  
      Parameters
      ----------
      a : array_like or pandas DataFrame object
          An array, any object exposing the array interface or a pandas
          DataFrame.
  
      val_col : str, optional
          Name of a DataFrame column that contains dependent variable values (test
          or response variable). Values should have a non-nominal scale. Must be
          specified if `a` is a pandas DataFrame object.
  
      group_col : str, optional
          Name of a DataFrame column that contains independent variable values
          (grouping or predictor variable). Values should have a nominal scale
          (categorical). Must be specified if `a` is a pandas DataFrame object.
  
      sort : bool, optional
          If True, sort data by block and group columns.
  
      p_adjust : str, optional
          Method for adjusting p values. See `statsmodels.sandbox.stats.multicomp`
          for details. Available methods are:
          'bonferroni' : one-step correction
          'sidak' : one-step correction
          'holm-sidak' : step-down method using Sidak adjustments
          'holm' : step-down method using Bonferroni adjustments
          'simes-hochberg' : step-up method  (independent)
          'hommel' : closed method based on Simes tests (non-negative)
          'fdr_bh' : Benjamini/Hochberg  (non-negative)
          'fdr_by' : Benjamini/Yekutieli (negative)
          'fdr_tsbh' : two stage fdr correction (non-negative)
          'fdr_tsbky' : two stage fdr correction (non-negative)
  
      Returns
      -------
      result : pandas.DataFrame
          P values.
  
      Notes
      -----
      The p values are estimated from the studentized range distribution. If
      the medians are already increasingly ordered, than the NPM-test
      simplifies to the ordinary Nemenyi test
  
      References
      ----------
      .. [1] Nashimoto, K., Wright, F.T., (2005), Multiple comparison procedures for
          detecting differences in simply ordered means. Comput. Statist. Data
          Anal. 48, 291--306.
  
      Examples
      --------
      >>> x = np.array([[102,109,114,120,124],
                        [110,112,123,130,145],
                        [132,141,156,160,172]])
      >>> sp.posthoc_npm_test(x)
      """
      x, _val_col, _group_col = __convert_to_df(a, val_col, group_col)
      x = x.sort_values(by=[_group_col], ascending=True) if sort else x
  
      groups = x[_group_col].unique()
      x["ranks"] = x[_val_col].rank()
      ri = x.groupby(_group_col)["ranks"].mean()
      ni = x.groupby(_group_col)[_val_col].count()
      k = groups.size
      n = x.shape[0]
      sigma = np.sqrt(n * (n + 1) / 12.0)
      df = np.inf
  
      def compare(m, u):
          a = [
              (ri.loc[groups[u]] - ri.loc[groups[_mi]])
              / (
                  sigma
                  / np.sqrt(2)
                  * np.sqrt(
                      1.0 / ni.loc[groups[_mi]]
                      + 1.0 / ni.loc[groups[u]]
                  )
              )
              for _mi in m
          ]
          return np.array(a)
  
      stat = np.zeros((k, k))
  
      for i in range(k - 1):
          for j in range(i + 1, k):
              u = j
              m = np.arange(i, u)
              tmp = compare(m, u)
              stat[j, i] = np.max(tmp)
  
      stat[stat < 0] = 0
  
      p_values = psturng(stat, k, df)
      tri_lower = np.tril_indices(p_values.shape[0], -1)
      p_values[tri_lower] = p_values.T[tri_lower]
  
      np.fill_diagonal(p_values, 1)
      return DataFrame(p_values, index=groups, columns=groups)
  
imports: import itertools
  from typing import Tuple, Union
  import numpy
  import scipy.stats
  from statsmodels.sandbox.stats.multicomp import multipletests
  from statsmodels.stats.multicomp import pairwise_tukeyhsd
  from statsmodels.stats.libqsturng import psturng
  from pandas import DataFrame
label_desc: Use image node_id 8 for calling a global function with example usage: posthoc_npm_test(a, val_col, group_col, sort) and returns: DataFrame, np