code: def get_hierarchy_from_df(df, model_args):
      """Extracts hierarchy from the raw df, using the provided spec and args.
  
      The "hierarchy" model arg is a list of format
      [<level 1>, <level 2>, ..., <level n>]
      where each element is a level in the hierarchy.
  
      We return a tuple (nixtla_df, hier_df, hier_dict) where:
      nixtla_df is a dataframe in the format nixtla packages uses for training
      hier_df is a matrix of 0s and 1s showing the hierarchical structure
      hier_dict is a dictionary with the hierarchical structure. See the unit test
      in tests/unit/ml_handlers/test_time_series_utils.py for an example.
      """
      if HierarchicalReconciliation is not None:
          spec = spec_hierarchy_from_list(model_args["hierarchy"])
  
          nixtla_df = df.rename(
              {model_args["order_by"]: "ds", model_args["target"]: "y"},
              axis=1,
          )
          nixtla_df["ds"] = pd.to_datetime(nixtla_df["ds"])
          for col in model_args["group_by"]:
              nixtla_df[col] = nixtla_df[col].astype(
                  str
              )  # grouping columns need to be string format
          nixtla_df.insert(0, "Total", "total")
  
          nixtla_df, hier_df, hier_dict = aggregate(
              nixtla_df, spec
          )  # returns (nixtla_df, hierarchy_df, hierarchy_dict)
          return nixtla_df, hier_df, hier_dict
      else:
          log.logger.warning(
              "HierarchicalForecast is not installed, but `get_hierarchy_from_df` has been called. This should never happen."
          )
  
file_path: mindsdb/mindsdb/integrations/utilities/time_series_utils.py
imports: import numpy
  import pandas
  from pandas.tseries.frequencies import to_offset
  from sklearn.metrics import r2_score
  from mindsdb.utilities import log
label_desc: Use image node_id 7 for calling a global function with example usage: get_hierarchy_from_df(df, model_args) and returns: nixtla_df,  hier_df,  hier_dict