docstr: Fit the underlying forecasting model (if applicable) and the fittable scorers, if any.
  
  Train the model (if not already fitted and ``allow_model_training`` is set to True) and the
  scorer(s) (if fittable) on the given time series.
  
  Once the model is fitted, the series historical forecasts are computed,
  representing what would have been forecasted by this model on the series.
  
  The prediction and the series are then used to train the scorer(s).
  
  Parameters
  ----------
  series
      One or multiple (if the model supports it) target series to be
      trained on (generally assumed to be anomaly-free).
  past_covariates
      Optional past-observed covariate series or sequence of series. This applies only if the model
      supports past covariates.
  future_covariates
      Optional future-known covariate series or sequence of series. This applies only if the model
      supports future covariates.
  allow_model_training
      Boolean value that indicates if the forecasting model needs to be fitted on the given series.
      If set to False, the model needs to be already fitted.
      Default: False
  forecast_horizon
      The forecast horizon for the predictions.
  start
      The first point of time at which a prediction is computed for a future time.
      This parameter supports 3 different data types: ``float``, ``int`` and ``pandas.Timestamp``.
      In the case of ``float``, the parameter will be treated as the proportion of the time series
      that should lie before the first prediction point.
      In the case of ``int``, the parameter will be treated as an integer index to the time index of
      `series` that will be used as first prediction time.
      In case of ``pandas.Timestamp``, this time stamp will be used to determine the first prediction time
      directly.
      Default: 0.5
  num_samples
      Number of times a prediction is sampled from a probabilistic model. Should be left set to 1 for
      deterministic models.
  model_fit_kwargs
      Parameters to be passed on to the forecast model ``fit()`` method.
  
  Returns
  -------
  self
      Fitted model
docstr_tok: ["Fit","the","underlying","forecasting","model","(","if","applicable",")","and","the","fittable","scorers",",","if","any",".","Train","the","model","(","if","not","already","fitted","and","``","allow_model_training","``","is","set","to","True",")","and","the","scorer","(","s",")","(","if","fittable",")","on","the","given","time","series",".","Once","the","model","is","fitted",",","the","series","historical","forecasts","are","computed",",","representing","what","would","have","been","forecasted","by","this","model","on","the","series",".","The","prediction","and","the","series","are","then","used","to","train","the","scorer","(","s",")",".","Parameters","--","--","--","--","--","series","One","or","multiple","(","if","the","model","supports","it",")","target","series","to","be","trained","on","(","generally","assumed","to","be","anomaly-free",")",".","past_covariates","Optional","past-observed","covariate","series","or","sequence","of","series",".","This","applies","only","if","the","model","supports","past","covariates",".","future_covariates","Optional","future-known","covariate","series","or","sequence","of","series",".","This","applies","only","if","the","model","supports","future","covariates",".","allow_model_training","Boolean","value","that","indicates","if","the","forecasting","model","needs","to","be","fitted","on","the","given","series",".","If","set","to","False",",","the","model","needs","to","be","already","fitted",".","Default",":","False","forecast_horizon","The","forecast","horizon","for","the","predictions",".","start","The","first","point","of","time","at","which","a","prediction","is","computed","for","a","future","time",".","This","parameter","supports","3","different","data","types",":","``","float","``",",","``","int","``","and","``","pandas.Timestamp","``",".","In","the","case","of","``","float","``",",","the","parameter","will","be","treated","as","the","proportion","of","the","time","series","that","should","lie","before","the","first","prediction","point",".","In","the","case","of","``","int","``",",","the","parameter","will","be","treated","as","an","integer","index","to","the","time","index","of","`","series","`","that","will","be","used","as","first","prediction","time",".","In","case","of","``","pandas.Timestamp","``",",","this","time","stamp","will","be","used","to","determine","the","first","prediction","time","directly",".","Default",":","0.5","num_samples","Number","of","times","a","prediction","is","sampled","from","a","probabilistic","model",".","Should","be","left","set","to","1","for","deterministic","models",".","model_fit_kwargs","Parameters","to","be","passed","on","to","the","forecast","model","``","fit","(",")","``","method",".","Returns","--","--","--","-","self","Fitted","model"]
code: def fit(
      self,
      series: Union[TimeSeries, Sequence[TimeSeries]],
      past_covariates: Optional[
          Union[TimeSeries, Sequence[TimeSeries]]
      ] = None,
      future_covariates: Optional[
          Union[TimeSeries, Sequence[TimeSeries]]
      ] = None,
      allow_model_training: bool = False,
      forecast_horizon: int = 1,
      start: Union[pd.Timestamp, float, int] = 0.5,
      num_samples: int = 1,
      **model_fit_kwargs,
  ):
      """Fit the underlying forecasting model (if applicable) and the fittable scorers, if any.
  
      Train the model (if not already fitted and ``allow_model_training`` is set to True) and the
      scorer(s) (if fittable) on the given time series.
  
      Once the model is fitted, the series historical forecasts are computed,
      representing what would have been forecasted by this model on the series.
  
      The prediction and the series are then used to train the scorer(s).
  
      Parameters
      ----------
      series
          One or multiple (if the model supports it) target series to be
          trained on (generally assumed to be anomaly-free).
      past_covariates
          Optional past-observed covariate series or sequence of series. This applies only if the model
          supports past covariates.
      future_covariates
          Optional future-known covariate series or sequence of series. This applies only if the model
          supports future covariates.
      allow_model_training
          Boolean value that indicates if the forecasting model needs to be fitted on the given series.
          If set to False, the model needs to be already fitted.
          Default: False
      forecast_horizon
          The forecast horizon for the predictions.
      start
          The first point of time at which a prediction is computed for a future time.
          This parameter supports 3 different data types: ``float``, ``int`` and ``pandas.Timestamp``.
          In the case of ``float``, the parameter will be treated as the proportion of the time series
          that should lie before the first prediction point.
          In the case of ``int``, the parameter will be treated as an integer index to the time index of
          `series` that will be used as first prediction time.
          In case of ``pandas.Timestamp``, this time stamp will be used to determine the first prediction time
          directly.
          Default: 0.5
      num_samples
          Number of times a prediction is sampled from a probabilistic model. Should be left set to 1 for
          deterministic models.
      model_fit_kwargs
          Parameters to be passed on to the forecast model ``fit()`` method.
  
      Returns
      -------
      self
          Fitted model
      """
  
      raise_if_not(
          type(allow_model_training) is bool,
          f"`allow_model_training` must be Boolean, found type: {type(allow_model_training)}.",
      )
  
      # checks if model does not need training and all scorer(s) are not fittable
      if not allow_model_training and not self.scorers_are_trainable:
          logger.warning(
              f"The forecasting model {self.model.__class__.__name__} won't be trained"
              + " because the parameter `allow_model_training` is set to False, and no scorer"
              + " is fittable. ``.fit()`` method has no effect."
          )
          return
  
      list_series = _to_list(series)
  
      raise_if_not(
          all([isinstance(s, TimeSeries) for s in list_series]),
          "all input `series` must be of type Timeseries.",
      )
  
      list_past_covariates = self._prepare_covariates(
          past_covariates, list_series, "past"
      )
      list_future_covariates = self._prepare_covariates(
          future_covariates, list_series, "future"
      )
  
      model_fit_kwargs["past_covariates"] = list_past_covariates
      model_fit_kwargs["future_covariates"] = list_future_covariates
  
      # remove None elements from dictionary
      model_fit_kwargs = {
          k: v for k, v in model_fit_kwargs.items() if v
      }
  
      # fit forecasting model
      if allow_model_training:
          # the model has not been trained yet
  
          fit_signature_series = (
              inspect.signature(self.model.fit)
              .parameters["series"]
              .annotation
          )
  
          # checks if model can be trained on multiple time series or only on a time series
          # TODO: check if model can accept multivariate timeseries, raise error if given and model cannot
          if "Sequence[darts.timeseries.TimeSeries]" in str(
              fit_signature_series
          ):
              self.model.fit(series=list_series, **model_fit_kwargs)
          else:
              raise_if_not(
                  len(list_series) == 1,
                  f"Forecasting model {self.model.__class__.__name__} only accepts a single time series"
                  + " for the training phase and not a sequence of multiple of time series.",
              )
              self.model.fit(series=list_series[0], **model_fit_kwargs)
      else:
          raise_if_not(
              self.model._fit_called,
              f"Model {self.model.__class__.__name__} needs to be trained, consider training "
              + "it beforehand or setting "
              + "`allow_model_training` to True (default: False). "
              + "The model will then be trained on the provided series.",
          )
  
      # generate the historical_forecast() prediction of the model on the train set
      if self.scorers_are_trainable:
          # check if the window size of the scorers are lower than the max size allowed
          self._check_window_size(list_series, start)
  
          list_pred = []
          for idx, series in enumerate(list_series):
              if list_past_covariates is not None:
                  past_covariates = list_past_covariates[idx]
  
              if list_future_covariates is not None:
                  future_covariates = list_future_covariates[idx]
  
              list_pred.append(
                  self._predict_with_forecasting(
                      series,
                      past_covariates=past_covariates,
                      future_covariates=future_covariates,
                      forecast_horizon=forecast_horizon,
                      start=start,
                      num_samples=num_samples,
                  )
              )
  
      # fit the scorers
      for scorer in self.scorers:
          if hasattr(scorer, "fit"):
              scorer.fit_from_prediction(list_series, list_pred)
  
      return self
  
file_path: darts/darts/ad/anomaly_model/forecasting_am.py
imports: import inspect
  from typing import Dict, Optional, Sequence, Union
  import pandas
  from darts.ad.anomaly_model.anomaly_model import AnomalyModel
  from darts.ad.scorers.scorers import AnomalyScorer
  from darts.ad.utils import _assert_same_length, _assert_timeseries, _to_list
  from darts.logging import get_logger, raise_if_not
  from darts.models.forecasting.forecasting_model import ForecastingModel
  from darts.timeseries import TimeSeries
label_desc: Use image node_id 2 for calling the ForecastingAnomalyModel obj's underlying member method code with example usage: obj.fit(series, past_covariates, future_covariates, allow_model_training, forecast_horizon, start, num_samples) and returns: self