code: def _handle_axis(a, q, fn, keepdims=False, axis=None):
      nd = a.ndim
      axis_arg = deepcopy(axis)
      if axis is not None:
          axis = _to_positive_axis(axis, nd)
  
          if len(axis) == 1:
              axis_arg = axis[0]
          else:
              keep = set(range(nd)) - set(axis)
              nkeep = len(keep)
  
              for i, s in enumerate(sorted(keep)):
                  a = np.moveaxis(a, s, i)
              a = a.reshape(a.shape[:nkeep] + (-1,))
              axis_arg = -1
  
      ret = fn(a, q, axis=axis_arg)
  
      if keepdims:
          if axis is None:
              index_ret = (None,) * nd
          else:
              index_ret = tuple(
                  None if i in axis else slice(None) for i in range(nd)
              )
          ret = ret[(Ellipsis,) + index_ret]
  
      return ret
  
imports: from typing import Optional, Union, Tuple, Sequence
  import numpy
  import math
  import ivy
  from ivy.func_wrapper import with_unsupported_dtypes
  from .None import backend_version
  from ..statistical import _infer_dtype
  from copy import deepcopy
label_desc: Use image node_id 8 for calling a global function with example usage: _handle_axis(a, q, fn, keepdims, axis) and returns: ret