docstr: Returns the :class:`TFTExplainabilityResult
  <darts.explainability.explainability_result.TFTExplainabilityResult>` result for all series in
  `foreground_series`. If `foreground_series` is `None`, will use the `background` input
  from `TFTExplainer` creation (either the `background` passed to creation, or the series stored in the
  `TFTModel` in case it was only trained on a single series).
  For each series, the results contain the attention heads, encoder variable importances, decoder variable
  importances, and static covariates importances.
  
  Parameters
  ----------
  foreground_series
      Optionally, one or a sequence of target `TimeSeries` to be explained. Can be multivariate.
      If not provided, the background `TimeSeries` will be explained instead.
  foreground_past_covariates
      Optionally, one or a sequence of past covariates `TimeSeries` if required by the forecasting model.
  foreground_future_covariates
      Optionally, one or a sequence of future covariates `TimeSeries` if required by the forecasting model.
  horizons
      This parameter is not used by the `TFTExplainer`.
  target_components
      This parameter is not used by the `TFTExplainer`.
  
  Returns
  -------
  TFTExplainabilityResult
      The explainability result containing the attention heads, encoder variable importances, decoder variable
      importances, and static covariates importances.
  
  Examples
  --------
  >>> explainer = TFTExplainer(model)  # requires `background` if model was trained on multiple series
  
  Optionally, give a foreground input to generate the explanation on a new input.
  Otherwise, leave it empty to compute the explanation on the background from `TFTExplainer` creation
  
  >>> explain_results = explainer.explain(
  >>>     foreground_series=foreground_series,
  >>>     foreground_past_covariates=foreground_past_covariates,
  >>>     foreground_future_covariates=foreground_future_covariates,
  >>> )
  >>> attn = explain_results.get_attention()
  >>> importances = explain_results.get_feature_importances()
docstr_tok: ["Returns","the",":","class",":","`","TFTExplainabilityResult","<","darts.explainability.explainability_result.TFTExplainabilityResult",">","`","result","for","all","series","in","`","foreground_series","`",".","If","`","foreground_series","`","is","`","None","`",",","will","use","the","`","background","`","input","from","`","TFTExplainer","`","creation","(","either","the","`","background","`","passed","to","creation",",","or","the","series","stored","in","the","`","TFTModel","`","in","case","it","was","only","trained","on","a","single","series",")",".","For","each","series",",","the","results","contain","the","attention","heads",",","encoder","variable","importances",",","decoder","variable","importances",",","and","static","covariates","importances",".","Parameters","--","--","--","--","--","foreground_series","Optionally",",","one","or","a","sequence","of","target","`","TimeSeries","`","to","be","explained",".","Can","be","multivariate",".","If","not","provided",",","the","background","`","TimeSeries","`","will","be","explained","instead",".","foreground_past_covariates","Optionally",",","one","or","a","sequence","of","past","covariates","`","TimeSeries","`","if","required","by","the","forecasting","model",".","foreground_future_covariates","Optionally",",","one","or","a","sequence","of","future","covariates","`","TimeSeries","`","if","required","by","the","forecasting","model",".","horizons","This","parameter","is","not","used","by","the","`","TFTExplainer","`",".","target_components","This","parameter","is","not","used","by","the","`","TFTExplainer","`",".","Returns","--","--","--","-","TFTExplainabilityResult","The","explainability","result","containing","the","attention","heads",",","encoder","variable","importances",",","decoder","variable","importances",",","and","static","covariates","importances",".","Examples","--","--","--","--",">",">",">","explainer","=","TFTExplainer","(","model",")","#","requires","`","background","`","if","model","was","trained","on","multiple","series","Optionally",",","give","a","foreground","input","to","generate","the","explanation","on","a","new","input",".","Otherwise",",","leave","it","empty","to","compute","the","explanation","on","the","background","from","`","TFTExplainer","`","creation",">",">",">","explain_results","=","explainer.explain","(",">",">",">","foreground_series=foreground_series",",",">",">",">","foreground_past_covariates=foreground_past_covariates",",",">",">",">","foreground_future_covariates=foreground_future_covariates",",",">",">",">",")",">",">",">","attn","=","explain_results.get_attention","(",")",">",">",">","importances","=","explain_results.get_feature_importances","(",")"]
code: def explain(
      self,
      foreground_series: Optional[
          Union[TimeSeries, Sequence[TimeSeries]]
      ] = None,
      foreground_past_covariates: Optional[
          Union[TimeSeries, Sequence[TimeSeries]]
      ] = None,
      foreground_future_covariates: Optional[
          Union[TimeSeries, Sequence[TimeSeries]]
      ] = None,
      horizons: Optional[Sequence[int]] = None,
      target_components: Optional[Sequence[str]] = None,
  ) -> TFTExplainabilityResult:
      """Returns the :class:`TFTExplainabilityResult
      <darts.explainability.explainability_result.TFTExplainabilityResult>` result for all series in
      `foreground_series`. If `foreground_series` is `None`, will use the `background` input
      from `TFTExplainer` creation (either the `background` passed to creation, or the series stored in the
      `TFTModel` in case it was only trained on a single series).
      For each series, the results contain the attention heads, encoder variable importances, decoder variable
      importances, and static covariates importances.
  
      Parameters
      ----------
      foreground_series
          Optionally, one or a sequence of target `TimeSeries` to be explained. Can be multivariate.
          If not provided, the background `TimeSeries` will be explained instead.
      foreground_past_covariates
          Optionally, one or a sequence of past covariates `TimeSeries` if required by the forecasting model.
      foreground_future_covariates
          Optionally, one or a sequence of future covariates `TimeSeries` if required by the forecasting model.
      horizons
          This parameter is not used by the `TFTExplainer`.
      target_components
          This parameter is not used by the `TFTExplainer`.
  
      Returns
      -------
      TFTExplainabilityResult
          The explainability result containing the attention heads, encoder variable importances, decoder variable
          importances, and static covariates importances.
  
      Examples
      --------
      >>> explainer = TFTExplainer(model)  # requires `background` if model was trained on multiple series
  
      Optionally, give a foreground input to generate the explanation on a new input.
      Otherwise, leave it empty to compute the explanation on the background from `TFTExplainer` creation
  
      >>> explain_results = explainer.explain(
      >>>     foreground_series=foreground_series,
      >>>     foreground_past_covariates=foreground_past_covariates,
      >>>     foreground_future_covariates=foreground_future_covariates,
      >>> )
      >>> attn = explain_results.get_attention()
      >>> importances = explain_results.get_feature_importances()
      """
      if target_components is not None or horizons is not None:
          logger.warning(
              "`horizons`, and `target_components` are not supported by `TFTExplainer` and will be ignored."
          )
      super().explain(
          foreground_series,
          foreground_past_covariates,
          foreground_future_covariates,
      )
      (
          foreground_series,
          foreground_past_covariates,
          foreground_future_covariates,
          _,
          _,
          _,
          _,
      ) = self._process_foreground(
          foreground_series,
          foreground_past_covariates,
          foreground_future_covariates,
      )
      horizons, _ = self._process_horizons_and_targets(None, None)
      preds = self.model.predict(
          n=self.n,
          series=foreground_series,
          past_covariates=foreground_past_covariates,
          future_covariates=foreground_future_covariates,
      )
      # get the weights and the attention head from the trained model for the prediction
      # aggregate over attention heads
      attention_heads = (
          self.model.model._attn_out_weights.detach()
          .cpu()
          .numpy()
          .sum(axis=-2)
      )
      # get the variable importances (pd.DataFrame with rows corresponding to the number of input series)
      encoder_importance = self._encoder_importance
      decoder_importance = self._decoder_importance
      static_covariates_importance = self._static_covariates_importance
  
      horizon_idx = [h - 1 for h in horizons]
  
      results = []
      icl = self.model.input_chunk_length
      for idx, (series, pred_series) in enumerate(
          zip(foreground_series, preds)
      ):
          times = series.time_index[-icl:].union(pred_series.time_index)
          attention = TimeSeries.from_times_and_values(
              values=np.take(
                  attention_heads[idx], horizon_idx, axis=0
              ).T,
              times=times,
              columns=[f"horizon {str(i)}" for i in horizons],
          )
          results.append(
              {
                  "attention": attention,
                  "encoder_importance": encoder_importance.iloc[
                      idx : idx + 1
                  ],
                  "decoder_importance": decoder_importance.iloc[
                      idx : idx + 1
                  ],
                  "static_covariates_importance": static_covariates_importance.iloc[
                      idx : idx + 1
                  ],
              }
          )
      return TFTExplainabilityResult(
          explanations=results[0] if len(results) == 1 else results
      )
  
imports: from typing import Dict, List, Optional, Sequence, Union
  import matplotlib.axes
  import matplotlib.pyplot
  import numpy
  import pandas
  from torch import Tensor
  from darts import TimeSeries
  from darts.explainability import TFTExplainabilityResult
  from darts.explainability.explainability import _ForecastingModelExplainer
  from darts.logging import get_logger, raise_log
  from darts.models import TFTModel
  from darts.utils.timeseries_generation import generate_index
label_desc: Use image node_id 2 for calling the TFTExplainer obj's underlying member method code with example usage: obj.explain(foreground_series, foreground_past_covariates, foreground_future_covariates, horizons, target_components) and returns: TFTExplainabilityResult