class_docstr: A class to infer appropriate Fourier series orders in different
  seasonality components.
  
  The method allows users to:
  
      - optionally remove the trend with different methods.
        Available methods are in `TrendAdjustMethodEnum`.
      - optionally do an aggregation.
      - fits the seasonality component with different Fourier series orders.
      - calculates the AIC/BIC of the fits.
      - choose the most appropriate order with AIC or BIC and an optional tolerance.
      - plot the investigations.
  
  Attributes
  ----------
  df : `pandas.DataFrame` or None
      The input timeseries.
  time_col : `str` or None
      The column name for timestamps in ``df``.
  value_col : `str` or None
      The column name for values in ``df``.
  fourier_series_orders : `dict` or None
      The inferred Fourier series orders.
      The keys are the seasonality component names.
      The values are the inferred best orders according to the config.
  df_features : `pandas.DataFrame` or None
      The cached dataframe with time features.
      Building this df is slow for large dataset.
      We cache it the first time we build it for subsequent uses.
docstr: Infers the most appropriate Fourier series order.
  Can infer multiple seasonality components with multiple configs at the same time.
  The configurations for each component are passed as a list of
  `~greykite.algo.common.seasonality_inferrer.SeasonalityInferConfig` object.
  To override a parameter for all configs, pass it via this function's parameter.
  
  For each seasonality component,
  the method first does an optional trend removal via grouped average or spline fit.
  For example, for yearly seasonality, one option is to remove the average of each year
  from the time series.
  The seasonality pattern is clearer and dominates after the trend removal.
  
  Next it does an optional aggregation to emphasize the current seasonality.
  For example, for yearly seasonality, it can do a weekly aggregation so that
  the weekly seasonality won't be mixed when modeling yearly seasonality.
  
  Then it fits seasonality model using Fourier series with orders up to a certain max_order,
  and computes the AIC/BIC of the models.
  
  The final order will be selected based on the criterion with a tolerance adjustment.
  A pre-specified offset can also be added to the selected order for adjustment.
  
  Parameters
  ----------
  df : `pandas.DataFrame`
      The input timeseries.
  configs : `list` [`SeasonalityInferConfig`]
      A list of `~greykite.algo.common.seasonality_inferrer.SeasonalityInferConfig`
      objects. Each element corresponds to the config for a seasonality component.
      For example, if you would like to infer seasonality orders for yearly seasonality and
      weekly seasonality, you need to provide a list of two configs.
  time_col : `str`
      The column name for timestamps in ``df``.
  value_col : `str`
      The column name for values in ``df``.
  adjust_trend_method : `str` or None, default None
      The methods used to adjust trend.
      Supported methods are in `AdjustTrendMethodEnum`.
      If not None, value is used to override all configs.
  adjust_trend_param : `dict` or None, default None
      Additional parameters for adjusting trend.
      For valid options, see
      `~greykite.algo.common.seasonality_inferrer.SeasonalityInferrer._adjust_trend`.
      If not None, value is used to override all configs.
  fit_algorithm : `str` or None, default None
      The algorithms used to fit the seasonality.
      Supported algorithms are "linear", "ridge" and "sgd".
      If not None, value is used to override all configs.
  plotting : `bool` or None, default None
      Whether to generate plots.
      If True, the returned dictionary will have plot via the "fig" key.
      Can turn this off to speed up the process.
      If not None, value is used to override all configs.
  tolerance : `float` or None, default None
      A tolerance on the criterion to allow a smaller order.
      For example, if AIC's minimum is 100 and ``tolerance`` is 0.1,
      then the function will find the smallest order that has AIC less than or equal to 110.
      If not None, value is used to override all configs.
  aggregation_period : `str` or None, default None
      The aggregation periods before fitting the Fourier series.
      Having aggregation to eliminate shorter seasonal periods may help get more accurate orders.
      But also make sure the number of observations after aggregation is sufficient.
      (At least 2 *  max_order + 1 to have a unique solution for the regression problem)
      If not None, value is used to override all configs.
  offset : `int` or None, default None
      The offset order to be added to the inferred orders.
      The orders after applying offsets can not be negative.
      If not None, value is used to override all configs.
  criterion : `str` or None, default None
      The criteria to pick the most appropriate orders.
      If not None, value is used to override all configs.
  
  Returns
  -------
  result : `dict`
      The result dictionary with the following keys:
  
          - "result": a list of result dictionaries from the inferring methods.
            The keys are:
  
              - "seas_name": the seasonality name.
              - "orders": the Fourier series orders fitted.
              - "aics": the fitted AICs.
              - "bics": the fitted BICs.
              - "best_aic_order": the order corresponding to the best feasible AIC.
              - "best_bic_order": the order corresponding to the best feasible BIC.
              - "fig": the diagnostic figure.
  
          - "best_orders": a dictionary of seasonality component names and their
            inferred Fourier series orders.
docstr_tok: ["Infers","the","most","appropriate","Fourier","series","order",".","Can","infer","multiple","seasonality","components","with","multiple","configs","at","the","same","time",".","The","configurations","for","each","component","are","passed","as","a","list","of","`","~greykite.algo.common.seasonality_inferrer.SeasonalityInferConfig","`","object",".","To","override","a","parameter","for","all","configs",",","pass","it","via","this","function","'s","parameter",".","For","each","seasonality","component",",","the","method","first","does","an","optional","trend","removal","via","grouped","average","or","spline","fit",".","For","example",",","for","yearly","seasonality",",","one","option","is","to","remove","the","average","of","each","year","from","the","time","series",".","The","seasonality","pattern","is","clearer","and","dominates","after","the","trend","removal",".","Next","it","does","an","optional","aggregation","to","emphasize","the","current","seasonality",".","For","example",",","for","yearly","seasonality",",","it","can","do","a","weekly","aggregation","so","that","the","weekly","seasonality","wo","n't","be","mixed","when","modeling","yearly","seasonality",".","Then","it","fits","seasonality","model","using","Fourier","series","with","orders","up","to","a","certain","max_order",",","and","computes","the","AIC\/BIC","of","the","models",".","The","final","order","will","be","selected","based","on","the","criterion","with","a","tolerance","adjustment",".","A","pre-specified","offset","can","also","be","added","to","the","selected","order","for","adjustment",".","Parameters","--","--","--","--","--","df",":","`","pandas.DataFrame","`","The","input","timeseries",".","configs",":","`","list","`","[","`","SeasonalityInferConfig","`","]","A","list","of","`","~greykite.algo.common.seasonality_inferrer.SeasonalityInferConfig","`","objects",".","Each","element","corresponds","to","the","config","for","a","seasonality","component",".","For","example",",","if","you","would","like","to","infer","seasonality","orders","for","yearly","seasonality","and","weekly","seasonality",",","you","need","to","provide","a","list","of","two","configs",".","time_col",":","`","str","`","The","column","name","for","timestamps","in","``","df","``",".","value_col",":","`","str","`","The","column","name","for","values","in","``","df","``",".","adjust_trend_method",":","`","str","`","or","None",",","default","None","The","methods","used","to","adjust","trend",".","Supported","methods","are","in","`","AdjustTrendMethodEnum","`",".","If","not","None",",","value","is","used","to","override","all","configs",".","adjust_trend_param",":","`","dict","`","or","None",",","default","None","Additional","parameters","for","adjusting","trend",".","For","valid","options",",","see","`","~greykite.algo.common.seasonality_inferrer.SeasonalityInferrer._adjust_trend","`",".","If","not","None",",","value","is","used","to","override","all","configs",".","fit_algorithm",":","`","str","`","or","None",",","default","None","The","algorithms","used","to","fit","the","seasonality",".","Supported","algorithms","are","``","linear","''",",","``","ridge","''","and","``","sgd","''",".","If","not","None",",","value","is","used","to","override","all","configs",".","plotting",":","`","bool","`","or","None",",","default","None","Whether","to","generate","plots",".","If","True",",","the","returned","dictionary","will","have","plot","via","the","``","fig","''","key",".","Can","turn","this","off","to","speed","up","the","process",".","If","not","None",",","value","is","used","to","override","all","configs",".","tolerance",":","`","float","`","or","None",",","default","None","A","tolerance","on","the","criterion","to","allow","a","smaller","order",".","For","example",",","if","AIC","'s","minimum","is","100","and","``","tolerance","``","is","0.1",",","then","the","function","will","find","the","smallest","order","that","has","AIC","less","than","or","equal","to","110",".","If","not","None",",","value","is","used","to","override","all","configs",".","aggregation_period",":","`","str","`","or","None",",","default","None","The","aggregation","periods","before","fitting","the","Fourier","series",".","Having","aggregation","to","eliminate","shorter","seasonal","periods","may","help","get","more","accurate","orders",".","But","also","make","sure","the","number","of","observations","after","aggregation","is","sufficient",".","(","At","least","2","*","max_order","+","1","to","have","a","unique","solution","for","the","regression","problem",")","If","not","None",",","value","is","used","to","override","all","configs",".","offset",":","`","int","`","or","None",",","default","None","The","offset","order","to","be","added","to","the","inferred","orders",".","The","orders","after","applying","offsets","can","not","be","negative",".","If","not","None",",","value","is","used","to","override","all","configs",".","criterion",":","`","str","`","or","None",",","default","None","The","criteria","to","pick","the","most","appropriate","orders",".","If","not","None",",","value","is","used","to","override","all","configs",".","Returns","--","--","--","-","result",":","`","dict","`","The","result","dictionary","with","the","following","keys",":","-","``","result","''",":","a","list","of","result","dictionaries","from","the","inferring","methods",".","The","keys","are",":","-","``","seas_name","''",":","the","seasonality","name",".","-","``","orders","''",":","the","Fourier","series","orders","fitted",".","-","``","aics","''",":","the","fitted","AICs",".","-","``","bics","''",":","the","fitted","BICs",".","-","``","best_aic_order","''",":","the","order","corresponding","to","the","best","feasible","AIC",".","-","``","best_bic_order","''",":","the","order","corresponding","to","the","best","feasible","BIC",".","-","``","fig","''",":","the","diagnostic","figure",".","-","``","best_orders","''",":","a","dictionary","of","seasonality","component","names","and","their","inferred","Fourier","series","orders","."]
code: def infer_fourier_series_order(
      self,
      df: pd.DataFrame,
      configs: List[SeasonalityInferConfig],
      time_col: str = TIME_COL,
      value_col: str = VALUE_COL,
      adjust_trend_method: Optional[str] = None,
      adjust_trend_param: Optional[dict] = None,
      fit_algorithm: Optional[str] = None,
      tolerance: Optional[float] = None,
      plotting: Optional[bool] = None,
      aggregation_period: Optional[str] = None,
      offset: Optional[int] = None,
      criterion: Optional[str] = None,
  ) -> dict:
      """Infers the most appropriate Fourier series order.
      Can infer multiple seasonality components with multiple configs at the same time.
      The configurations for each component are passed as a list of
      `~greykite.algo.common.seasonality_inferrer.SeasonalityInferConfig` object.
      To override a parameter for all configs, pass it via this function's parameter.
  
      For each seasonality component,
      the method first does an optional trend removal via grouped average or spline fit.
      For example, for yearly seasonality, one option is to remove the average of each year
      from the time series.
      The seasonality pattern is clearer and dominates after the trend removal.
  
      Next it does an optional aggregation to emphasize the current seasonality.
      For example, for yearly seasonality, it can do a weekly aggregation so that
      the weekly seasonality won't be mixed when modeling yearly seasonality.
  
      Then it fits seasonality model using Fourier series with orders up to a certain max_order,
      and computes the AIC/BIC of the models.
  
      The final order will be selected based on the criterion with a tolerance adjustment.
      A pre-specified offset can also be added to the selected order for adjustment.
  
      Parameters
      ----------
      df : `pandas.DataFrame`
          The input timeseries.
      configs : `list` [`SeasonalityInferConfig`]
          A list of `~greykite.algo.common.seasonality_inferrer.SeasonalityInferConfig`
          objects. Each element corresponds to the config for a seasonality component.
          For example, if you would like to infer seasonality orders for yearly seasonality and
          weekly seasonality, you need to provide a list of two configs.
      time_col : `str`
          The column name for timestamps in ``df``.
      value_col : `str`
          The column name for values in ``df``.
      adjust_trend_method : `str` or None, default None
          The methods used to adjust trend.
          Supported methods are in `AdjustTrendMethodEnum`.
          If not None, value is used to override all configs.
      adjust_trend_param : `dict` or None, default None
          Additional parameters for adjusting trend.
          For valid options, see
          `~greykite.algo.common.seasonality_inferrer.SeasonalityInferrer._adjust_trend`.
          If not None, value is used to override all configs.
      fit_algorithm : `str` or None, default None
          The algorithms used to fit the seasonality.
          Supported algorithms are "linear", "ridge" and "sgd".
          If not None, value is used to override all configs.
      plotting : `bool` or None, default None
          Whether to generate plots.
          If True, the returned dictionary will have plot via the "fig" key.
          Can turn this off to speed up the process.
          If not None, value is used to override all configs.
      tolerance : `float` or None, default None
          A tolerance on the criterion to allow a smaller order.
          For example, if AIC's minimum is 100 and ``tolerance`` is 0.1,
          then the function will find the smallest order that has AIC less than or equal to 110.
          If not None, value is used to override all configs.
      aggregation_period : `str` or None, default None
          The aggregation periods before fitting the Fourier series.
          Having aggregation to eliminate shorter seasonal periods may help get more accurate orders.
          But also make sure the number of observations after aggregation is sufficient.
          (At least 2 *  max_order + 1 to have a unique solution for the regression problem)
          If not None, value is used to override all configs.
      offset : `int` or None, default None
          The offset order to be added to the inferred orders.
          The orders after applying offsets can not be negative.
          If not None, value is used to override all configs.
      criterion : `str` or None, default None
          The criteria to pick the most appropriate orders.
          If not None, value is used to override all configs.
  
      Returns
      -------
      result : `dict`
          The result dictionary with the following keys:
  
              - "result": a list of result dictionaries from the inferring methods.
                The keys are:
  
                  - "seas_name": the seasonality name.
                  - "orders": the Fourier series orders fitted.
                  - "aics": the fitted AICs.
                  - "bics": the fitted BICs.
                  - "best_aic_order": the order corresponding to the best feasible AIC.
                  - "best_bic_order": the order corresponding to the best feasible BIC.
                  - "fig": the diagnostic figure.
  
              - "best_orders": a dictionary of seasonality component names and their
                inferred Fourier series orders.
      """
      # Checks and processes the input parameters.
      configs = self._process_params(
          configs=configs,
          adjust_trend_method=adjust_trend_method,
          adjust_trend_param=adjust_trend_param,
          fit_algorithm=fit_algorithm,
          tolerance=tolerance,
          plotting=plotting,
          aggregation_period=aggregation_period,
          offset=offset,
          criterion=criterion,
      )
      # Sets class attributes.
      self.df = df.copy()
      self.df[time_col] = pd.to_datetime(self.df[time_col])
      self.time_col = time_col
      self.value_col = value_col
      self.df_features = (
          None  # resets ``df_features`` for every new call.
      )
      # Iteratively infer the Fourier series order for each component.
      result = []
      for config in configs:
          # Adjusts the trend and does aggregation.
          df_adj = self._process_df(
              df=self.df,
              time_col=time_col,
              value_col=value_col,
              adjust_trend_method=config.adjust_trend_method,
              adjust_trend_params=config.adjust_trend_param,
              aggregation_period=config.aggregation_period,
          )
          # Infers the seasonality Fourier series order.
          seasonality_result = self._gets_seasonality_fitting_metrics(
              df=df_adj,
              time_col=time_col,
              value_col=value_col,
              seas_name=config.seas_name,
              col_name=config.col_name,
              period=config.period,
              max_order=config.max_order,
              fit_algorithm=config.fit_algorithm,
              plotting=config.plotting,
              tolerance=config.tolerance,
              offset=config.offset,
          )
          result.append(seasonality_result)
  
      # Calculates best orders according to the criteria.
      best_orders = {
          config.seas_name: result[i]["best_aic_order"]
          if config.criterion == "aic"
          else result[i]["best_bic_order"]
          for i, config in enumerate(configs)
      }
  
      self.fourier_series_orders = best_orders
  
      return {"result": result, "best_orders": best_orders}
  
imports: from dataclasses import dataclass
  from enum import Enum
  from typing import List
  from typing import Optional
  import numpy
  import pandas
  from plotly import graph_objs
  from plotly.colors import DEFAULT_PLOTLY_COLORS
  from plotly.subplots import make_subplots
  from sklearn.linear_model import LinearRegression
  from sklearn.linear_model import RidgeCV
  from sklearn.linear_model import SGDRegressor
  from greykite.common.constants import TIME_COL
  from greykite.common.constants import VALUE_COL
  from greykite.common.constants import TimeFeaturesEnum
  from greykite.common.features.timeseries_features import add_time_features_df
  from greykite.common.features.timeseries_features import fourier_series_multi_fcn
  from greykite.common.features.timeseries_features import get_default_origin_for_time_vars
label_desc: Use image node_id 2 for calling the SeasonalityInferrer obj's underlying member method code with example usage: obj.infer_fourier_series_order(df, configs, time_col, value_col, adjust_trend_method, adjust_trend_param, fit_algorithm, tolerance, plotting, aggregation_period, offset, criterion) and returns: dict