code: def _hdi(ary, hdi_prob, circular, skipna):
      """Compute hpi over the flattened array."""
      ary = ary.flatten()
      if skipna:
          nans = np.isnan(ary)
          if not nans.all():
              ary = ary[~nans]
      n = len(ary)
  
      if circular:
          mean = st.circmean(ary, high=np.pi, low=-np.pi)
          ary = ary - mean
          ary = np.arctan2(np.sin(ary), np.cos(ary))
  
      ary = np.sort(ary)
      interval_idx_inc = int(np.floor(hdi_prob * n))
      n_intervals = n - interval_idx_inc
      interval_width = np.subtract(
          ary[interval_idx_inc:], ary[:n_intervals], dtype=np.float_
      )
  
      if len(interval_width) == 0:
          raise ValueError(
              "Too few elements for interval calculation. "
          )
  
      min_idx = np.argmin(interval_width)
      hdi_min = ary[min_idx]
      hdi_max = ary[min_idx + interval_idx_inc]
  
      if circular:
          hdi_min = hdi_min + mean
          hdi_max = hdi_max + mean
          hdi_min = np.arctan2(np.sin(hdi_min), np.cos(hdi_min))
          hdi_max = np.arctan2(np.sin(hdi_max), np.cos(hdi_max))
  
      hdi_interval = np.array([hdi_min, hdi_max])
  
      return hdi_interval
  
imports: import itertools
  import warnings
  from copy import deepcopy
  from typing import List, Optional, Tuple, Union, Mapping, cast, Callable
  import numpy
  import pandas
  import scipy.stats
  from xarray_einstats import stats
  import xarray
  from scipy.optimize import minimize
  from typing_extensions import Literal
  from ..None import _log
  from ..data import InferenceData, convert_to_dataset, convert_to_inference_data, extract
  from ..rcparams import rcParams, ScaleKeyword, ICKeyword
  from ..utils import Numba, _numba_var, _var_names, get_coords
  from .density_utils import get_bins
  from .density_utils import histogram
  from .density_utils import kde
  from .diagnostics import _mc_error, _multichain_statistics, ess
  from .stats_utils import ELPDData, _circular_standard_deviation, smooth_data
  from .stats_utils import get_log_likelihood
  from .stats_utils import get_log_prior
  from .stats_utils import logsumexp
  from .stats_utils import make_ufunc
  from .stats_utils import stats_variance_2d
  from .stats_utils import wrap_xarray_ufunc
  from ..sel_utils import xarray_var_iter
  from ..labels import BaseLabeller
label_desc: Use image node_id 5 for calling a global function with example usage: _hdi(ary, hdi_prob, circular, skipna) and returns: hdi_interval