class_docstr: Module level tests for all sktime forecasters.
class_docstr_tok: ["Module","level","tests","for","all","sktime","forecasters","."]
docstr: Check that hierarchical forecasting works, also see bug #3961.
  
  Arguments
  ---------
  estimator_instance : instance of BaseForecaster
  n_columns : number of columns, of the endogeneous data y_train
  
  Raises
  ------
  Exception - if fit/predict does not complete without error
  AssertionError - if forecast is not expected mtype pd_multiindex_hier,
      and does not have expected row and column indices
docstr_tok: ["Check","that","hierarchical","forecasting","works",",","also","see","bug","#","3961",".","Arguments","--","--","--","--","-","estimator_instance",":","instance","of","BaseForecaster","n_columns",":","number","of","columns",",","of","the","endogeneous","data","y_train","Raises","--","--","--","Exception","-","if","fit\/predict","does","not","complete","without","error","AssertionError","-","if","forecast","is","not","expected","mtype","pd_multiindex_hier",",","and","does","not","have","expected","row","and","column","indices"]
code: def test_hierarchical_with_exogeneous(
      self, estimator_instance, n_columns
  ):
      """Check that hierarchical forecasting works, also see bug #3961.
  
      Arguments
      ---------
      estimator_instance : instance of BaseForecaster
      n_columns : number of columns, of the endogeneous data y_train
  
      Raises
      ------
      Exception - if fit/predict does not complete without error
      AssertionError - if forecast is not expected mtype pd_multiindex_hier,
          and does not have expected row and column indices
      """
      from sktime.datatypes import check_is_mtype
      from sktime.datatypes._utilities import get_window
      from sktime.utils._testing.hierarchical import _make_hierarchical
  
      y_train = _make_hierarchical(
          hierarchy_levels=(2, 4),
          n_columns=n_columns,
          min_timepoints=22,
          max_timepoints=22,
          index_type="period",
      )
      X = _make_hierarchical(
          hierarchy_levels=(2, 4),
          n_columns=2,
          min_timepoints=24,
          max_timepoints=24,
          index_type="period",
      )
      X.columns = ["foo", "bar"]
      X_train = get_window(X, lag=2)
      X_test = get_window(X, window_length=2)
      fh = [1, 2]
  
      estimator_instance.fit(y=y_train, X=X_train, fh=fh)
      y_pred = estimator_instance.predict(X=X_test)
  
      assert isinstance(y_pred, pd.DataFrame)
      assert check_is_mtype(
          y_pred, "pd_multiindex_hier", msg_return_dict="list"
      )
      msg = (
          "returned columns after predict are not as expected. "
          f"expected: {y_train.columns}. Found: {y_pred.columns}"
      )
      assert np.all(y_pred.columns == y_train.columns), msg
  
      # check consistency of forecast hierarchy with training data
      # some forecasters add __total levels, e.g., ReconcilerForecaster
      # if = not such a forecaster; else = levels are added
      if len(y_pred.index) == len(X_test.index):
          # the indices should be equal iff no levels are added
          assert np.all(y_pred.index == X_test.index)
      else:
          # if levels are added, all expected levels and times should be contained
          assert set(X_test.index).issubset(y_pred.index)
  
      if estimator_instance.get_tag("capability:pred_int"):
          y_pred_int = estimator_instance.predict_interval(X=X_test)
  
          assert isinstance(y_pred_int, pd.DataFrame)
          assert check_is_mtype(
              y_pred_int, "pd_multiindex_hier", msg_return_dict="list"
          )
  
          if len(y_pred_int.index) == len(X_test.index):
              assert np.all(y_pred_int.index == X_test.index)
          else:
              assert set(X_test.index).issubset(y_pred_int.index)
  
          y_pred_q = estimator_instance.predict_quantiles(X=X_test)
  
          assert isinstance(y_pred_q, pd.DataFrame)
          assert check_is_mtype(
              y_pred_q, "pd_multiindex_hier", msg_return_dict="list"
          )
  
          if len(y_pred_q.index) == len(X_test.index):
              assert np.all(y_pred_q.index == X_test.index)
          else:
              assert set(X_test.index).issubset(y_pred_q.index)
  
imports: import numpy
  import pandas
  import pytest
  from sktime.datatypes import check_is_mtype
  from sktime.datatypes._utilities import get_cutoff
  from sktime.exceptions import NotFittedError
  from sktime.forecasting.base._delegate import _DelegatedForecaster
  from sktime.forecasting.base._fh import ForecastingHorizon
  from sktime.forecasting.tests._config import TEST_ALPHAS, TEST_FHS, TEST_OOS_FHS, TEST_STEP_LENGTHS_INT, TEST_WINDOW_LENGTHS_INT, VALID_INDEX_FH_COMBINATIONS
  from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
  from sktime.split import ExpandingWindowSplitter, SlidingWindowSplitter, temporal_train_test_split
  from sktime.tests.test_all_estimators import BaseFixtureGenerator, QuickTester
  from sktime.utils._testing.forecasting import _assert_correct_columns, _assert_correct_pred_time_index, _get_expected_index_for_update_predict, _get_n_columns, _make_fh, make_forecasting_problem
  from sktime.utils._testing.series import _make_series
  from sktime.utils.validation.forecasting import check_fh
label_desc: Use image node_id 25 for calling the TestAllForecasters obj's underlying member method code with example usage: obj.test_hierarchical_with_exogeneous(estimator_instance, n_columns) without return types