code: def _axis_nan_policy_test(
      hypotest,
      args,
      kwds,
      n_samples,
      n_outputs,
      paired,
      unpacker,
      nan_policy,
      axis,
      data_generator,
  ):
      # Tests the 1D and vectorized behavior of hypothesis tests against a
      # reference implementation (nan_policy_1d with np.ndenumerate)
  
      # Some hypothesis tests return a non-iterable that needs an `unpacker` to
      # extract the statistic and p-value. For those that don't:
      if not unpacker:
  
          def unpacker(res):
              return res
  
      rng = np.random.default_rng(0)
  
      # Generate multi-dimensional test data with all important combinations
      # of patterns of nans along `axis`
      n_repetitions = 3  # number of repetitions of each pattern
      data_gen_kwds = {
          "n_samples": n_samples,
          "n_repetitions": n_repetitions,
          "axis": axis,
          "rng": rng,
          "paired": paired,
      }
      if data_generator == "mixed":
          inherent_size = 6  # number of distinct types of patterns
          data = _mixed_data_generator(**data_gen_kwds)
      elif data_generator == "all_nans":
          inherent_size = 2  # hard-coded in _homogeneous_data_generator
          data_gen_kwds["all_nans"] = True
          data = _homogeneous_data_generator(**data_gen_kwds)
      elif data_generator == "all_finite":
          inherent_size = 2  # hard-coded in _homogeneous_data_generator
          data_gen_kwds["all_nans"] = False
          data = _homogeneous_data_generator(**data_gen_kwds)
  
      output_shape = [n_repetitions] + [inherent_size] * n_samples
  
      # To generate reference behavior to compare against, loop over the axis-
      # slices in data. Make indexing easier by moving `axis` to the end and
      # broadcasting all samples to the same shape.
      data_b = [np.moveaxis(sample, axis, -1) for sample in data]
      data_b = [
          np.broadcast_to(sample, output_shape + [sample.shape[-1]])
          for sample in data_b
      ]
      statistics = np.zeros(output_shape)
      pvalues = np.zeros(output_shape)
  
      for i, _ in np.ndenumerate(statistics):
          data1d = [sample[i] for sample in data_b]
          with np.errstate(divide="ignore", invalid="ignore"):
              try:
                  res1d = nan_policy_1d(
                      hypotest,
                      data1d,
                      unpacker,
                      *args,
                      n_outputs=n_outputs,
                      nan_policy=nan_policy,
                      paired=paired,
                      _no_deco=True,
                      **kwds,
                  )
  
                  # Eventually we'll check the results of a single, vectorized
                  # call of `hypotest` against the arrays `statistics` and
                  # `pvalues` populated using the reference `nan_policy_1d`.
                  # But while we're at it, check the results of a 1D call to
                  # `hypotest` against the reference `nan_policy_1d`.
                  res1db = unpacker(
                      hypotest(
                          *data1d, *args, nan_policy=nan_policy, **kwds
                      )
                  )
                  assert_equal(res1db[0], res1d[0])
                  if len(res1db) == 2:
                      assert_equal(res1db[1], res1d[1])
  
              # When there is not enough data in 1D samples, many existing
              # hypothesis tests raise errors instead of returning nans .
              # For vectorized calls, we put nans in the corresponding elements
              # of the output.
              except (
                  RuntimeWarning,
                  UserWarning,
                  ValueError,
                  ZeroDivisionError,
              ) as e:
                  # whatever it is, make sure same error is raised by both
                  # `nan_policy_1d` and `hypotest`
                  with pytest.raises(type(e), match=re.escape(str(e))):
                      nan_policy_1d(
                          hypotest,
                          data1d,
                          unpacker,
                          *args,
                          n_outputs=n_outputs,
                          nan_policy=nan_policy,
                          paired=paired,
                          _no_deco=True,
                          **kwds,
                      )
                  with pytest.raises(type(e), match=re.escape(str(e))):
                      hypotest(
                          *data1d, *args, nan_policy=nan_policy, **kwds
                      )
  
                  if any(
                      [
                          str(e).startswith(message)
                          for message in too_small_messages
                      ]
                  ):
                      res1d = np.full(n_outputs, np.nan)
                  elif any(
                      [
                          str(e).startswith(message)
                          for message in inaccuracy_messages
                      ]
                  ):
                      with suppress_warnings() as sup:
                          sup.filter(RuntimeWarning)
                          sup.filter(UserWarning)
                          res1d = nan_policy_1d(
                              hypotest,
                              data1d,
                              unpacker,
                              *args,
                              n_outputs=n_outputs,
                              nan_policy=nan_policy,
                              paired=paired,
                              _no_deco=True,
                              **kwds,
                          )
                  else:
                      raise e
          statistics[i] = res1d[0]
          if len(res1d) == 2:
              pvalues[i] = res1d[1]
  
      # Perform a vectorized call to the hypothesis test.
      # If `nan_policy == 'raise'`, check that it raises the appropriate error.
      # If not, compare against the output against `statistics` and `pvalues`
      if nan_policy == "raise" and not data_generator == "all_finite":
          message = "The input contains nan values"
          with pytest.raises(ValueError, match=message):
              hypotest(
                  *data, axis=axis, nan_policy=nan_policy, *args, **kwds
              )
  
      else:
          with suppress_warnings() as sup, np.errstate(
              divide="ignore", invalid="ignore"
          ):
              sup.filter(
                  RuntimeWarning, "Precision loss occurred in moment"
              )
              sup.filter(
                  UserWarning,
                  "Sample size too small for normal " "approximation.",
              )
              res = unpacker(
                  hypotest(
                      *data,
                      axis=axis,
                      nan_policy=nan_policy,
                      *args,
                      **kwds,
                  )
              )
          assert_allclose(res[0], statistics, rtol=1e-15)
          assert_equal(res[0].dtype, statistics.dtype)
  
          if len(res) == 2:
              assert_allclose(res[1], pvalues, rtol=1e-15)
              assert_equal(res[1].dtype, pvalues.dtype)
  
imports: from itertools import product, combinations_with_replacement, permutations
  import re
  import pickle
  import pytest
  import numpy
  from numpy.testing import assert_allclose, assert_equal, suppress_warnings
  from scipy import stats
  from scipy.stats import norm
  from scipy.stats._axis_nan_policy import _masked_arrays_2_sentinel_arrays
  from scipy._lib._util import AxisError
label_desc: Use image node_id 8 for calling a global function with example usage: _axis_nan_policy_test(hypotest, args, kwds, n_samples, n_outputs, paired, unpacker, nan_policy, axis, data_generator) and returns: res