code: def _somers_d(A):
      """Calculate Somers' D and p-value from contingency table."""
      # See [3] page 1740
  
      # contingency table must be truly 2D
      if A.shape[0] <= 1 or A.shape[1] <= 1:
          return np.nan, np.nan
  
      NA = A.sum()
      NA2 = NA**2
      PA = _P(A)
      QA = _Q(A)
      Sri2 = (A.sum(axis=1) ** 2).sum()
  
      d = (PA - QA) / (NA2 - Sri2)
  
      S = _a_ij_Aij_Dij2(A) - (PA - QA) ** 2 / NA
      if S == 0:  # Avoid divide by zero
          return d, 0
      Z = (PA - QA) / (4 * (S)) ** 0.5
      p = 2 * norm.sf(abs(Z))  # 2-sided p-value
  
      return d, p
  
imports: from collections import namedtuple
  from dataclasses import make_dataclass
  import numpy
  import warnings
  from itertools import combinations
  import scipy.stats
  from scipy.optimize import shgo
  from .None import distributions
  from ._continuous_distns import chi2, norm
  from scipy.special import gamma, kv, gammaln
  from .None import _wilcoxon_data
label_desc: Use image node_id 13 for calling a global function with example usage: _somers_d(A) and returns: d,  p, np,  np, d,  int