docstr: Convert TSDataset to a PyTorch DataLoader with or without rolling. We recommend to use
  to_torch_data_loader(default roll=True) if you don't need to output the rolled numpy array.
  It is much more efficient than rolling separately, especially when the dataframe or lookback
  is large.
  
  :param batch_size: int, the batch_size for a Pytorch DataLoader. It defaults to 32.
  :param roll: Boolean. Whether to roll the dataframe before converting to DataLoader.
         If True, you must also specify lookback and horizon for rolling. If False, you must
         have called tsdataset.roll() before calling to_torch_data_loader(). Default to True.
  :param lookback: int, lookback value. Default to 'auto',
         the mode of time series' cycle length will be taken as the lookback.
  :param horizon: int or list,
         if `horizon` is an int, we will sample `horizon` step
         continuously after the forecasting point.
         if `horizon` is a list, we will sample discretely according
         to the input list.
         specially, when `horizon` is set to 0, ground truth will be generated as None.
  :param feature_col: str or list, indicates the feature col name. Default to None,
         where we will take all available feature in rolling.
  :param target_col: str or list, indicates the target col name. Default to None,
         where we will take all target in rolling. it should be a subset of target_col
         you used to initialize the tsdataset.
  :param shuffle: if the dataloader is shuffled. default to True.
  :param time_enc: bool,
         This parameter should be set to True only when you are using Autoformer model. With
         time_enc to be true, 2 additional numpy ndarray will be returned when you call
         `.to_numpy()`. Be sure to have a time type for dt_col if you set time_enc to True.
  :param label_len: int,
         This parameter should be set to True only when you are using Autoformer model. This
         indicates the length of overlap area of output(y) and input(x) on time axis.
  :param is_predict: bool,
         This parameter should be set to True only when you are processing test data without
         accuracy evaluation. This indicates if the dataset will be sampled as a prediction
         dataset(without groud truth).
  
  :return: A pytorch DataLoader instance. The data returned from dataloader is in the
           following form:
          1. a 3d numpy ndarray when is_predict=True or horizon=0
          and time_enc=False
          2. a 2-dim tuple of 3d numpy ndarray (x, y) when is_predict=False
          and horizon != 0 and time_enc=False
          3. a 4-dim tuple of 3d numpy ndarray (x, y, x_enc, y_enc) when
          time_enc=True
  
  to_torch_data_loader() can be called by:
  
  >>> # Here is a df example:
  >>> # id        datetime      value   "extra feature 1"   "extra feature 2"
  >>> # 00        2019-01-01    1.9     1                   2
  >>> # 01        2019-01-01    2.3     0                   9
  >>> # 00        2019-01-02    2.4     3                   4
  >>> # 01        2019-01-02    2.6     0                   2
  >>> tsdataset = TSDataset.from_pandas(df, dt_col="datetime",
  >>>                                   target_col="value", id_col="id",
  >>>                                   extra_feature_col=["extra feature 1",
  >>>                                                      "extra feature 2"])
  >>> horizon, lookback = 1, 1
  >>> data_loader = tsdataset.to_torch_data_loader(batch_size=32,
  >>>                                              lookback=lookback,
  >>>                                              horizon=horizon)
  >>> # or roll outside. That might be less efficient than the way above.
  >>> tsdataset.roll(lookback=lookback, horizon=horizon, id_sensitive=False)
  >>> x, y = tsdataset.to_numpy()
  >>> print(x, y) # x = [[[1.9, 1, 2 ]], [[2.3, 0, 9 ]]] y = [[[ 2.4 ]], [[ 2.6 ]]]
  >>> data_loader = tsdataset.to_torch_data_loader(batch_size=32, roll=False)
docstr_tok: ["Convert","TSDataset","to","a","PyTorch","DataLoader","with","or","without","rolling",".","We","recommend","to","use","to_torch_data_loader","(","default","roll=True",")","if","you","do","n't","need","to","output","the","rolled","numpy","array",".","It","is","much","more","efficient","than","rolling","separately",",","especially","when","the","dataframe","or","lookback","is","large",".",":","param","batch_size",":","int",",","the","batch_size","for","a","Pytorch","DataLoader",".","It","defaults","to","32.",":","param","roll",":","Boolean",".","Whether","to","roll","the","dataframe","before","converting","to","DataLoader",".","If","True",",","you","must","also","specify","lookback","and","horizon","for","rolling",".","If","False",",","you","must","have","called","tsdataset.roll","(",")","before","calling","to_torch_data_loader","(",")",".","Default","to","True",".",":","param","lookback",":","int",",","lookback","value",".","Default","to","'auto","'",",","the","mode","of","time","series","'","cycle","length","will","be","taken","as","the","lookback",".",":","param","horizon",":","int","or","list",",","if","`","horizon","`","is","an","int",",","we","will","sample","`","horizon","`","step","continuously","after","the","forecasting","point",".","if","`","horizon","`","is","a","list",",","we","will","sample","discretely","according","to","the","input","list",".","specially",",","when","`","horizon","`","is","set","to","0",",","ground","truth","will","be","generated","as","None",".",":","param","feature_col",":","str","or","list",",","indicates","the","feature","col","name",".","Default","to","None",",","where","we","will","take","all","available","feature","in","rolling",".",":","param","target_col",":","str","or","list",",","indicates","the","target","col","name",".","Default","to","None",",","where","we","will","take","all","target","in","rolling",".","it","should","be","a","subset","of","target_col","you","used","to","initialize","the","tsdataset",".",":","param","shuffle",":","if","the","dataloader","is","shuffled",".","default","to","True",".",":","param","time_enc",":","bool",",","This","parameter","should","be","set","to","True","only","when","you","are","using","Autoformer","model",".","With","time_enc","to","be","true",",","2","additional","numpy","ndarray","will","be","returned","when","you","call","`",".to_numpy","(",")","`",".","Be","sure","to","have","a","time","type","for","dt_col","if","you","set","time_enc","to","True",".",":","param","label_len",":","int",",","This","parameter","should","be","set","to","True","only","when","you","are","using","Autoformer","model",".","This","indicates","the","length","of","overlap","area","of","output","(","y",")","and","input","(","x",")","on","time","axis",".",":","param","is_predict",":","bool",",","This","parameter","should","be","set","to","True","only","when","you","are","processing","test","data","without","accuracy","evaluation",".","This","indicates","if","the","dataset","will","be","sampled","as","a","prediction","dataset","(","without","groud","truth",")",".",":","return",":","A","pytorch","DataLoader","instance",".","The","data","returned","from","dataloader","is","in","the","following","form",":","1.","a","3d","numpy","ndarray","when","is_predict=True","or","horizon=0","and","time_enc=False","2.","a","2-dim","tuple","of","3d","numpy","ndarray","(","x",",","y",")","when","is_predict=False","and","horizon","!","=","0","and","time_enc=False","3.","a","4-dim","tuple","of","3d","numpy","ndarray","(","x",",","y",",","x_enc",",","y_enc",")","when","time_enc=True","to_torch_data_loader","(",")","can","be","called","by",":",">",">",">","#","Here","is","a","df","example",":",">",">",">","#","id","datetime","value","``","extra","feature","1","''","``","extra","feature","2","''",">",">",">","#","00","2019-01-01","1.9","1","2",">",">",">","#","01","2019-01-01","2.3","0","9",">",">",">","#","00","2019-01-02","2.4","3","4",">",">",">","#","01","2019-01-02","2.6","0","2",">",">",">","tsdataset","=","TSDataset.from_pandas","(","df",",","dt_col=","''","datetime","''",",",">",">",">","target_col=","''","value","''",",","id_col=","''","id","''",",",">",">",">","extra_feature_col=","[","``","extra","feature","1","''",",",">",">",">","``","extra","feature","2","''","]",")",">",">",">","horizon",",","lookback","=","1",",","1",">",">",">","data_loader","=","tsdataset.to_torch_data_loader","(","batch_size=32",",",">",">",">","lookback=lookback",",",">",">",">","horizon=horizon",")",">",">",">","#","or","roll","outside",".","That","might","be","less","efficient","than","the","way","above",".",">",">",">","tsdataset.roll","(","lookback=lookback",",","horizon=horizon",",","id_sensitive=False",")",">",">",">","x",",","y","=","tsdataset.to_numpy","(",")",">",">",">","print","(","x",",","y",")","#","x","=","[","[","[","1.9",",","1",",","2","]","]",",","[","[","2.3",",","0",",","9","]","]","]","y","=","[","[","[","2.4","]","]",",","[","[","2.6","]","]","]",">",">",">","data_loader","=","tsdataset.to_torch_data_loader","(","batch_size=32",",","roll=False",")"]
code: def to_torch_data_loader(
      self,
      batch_size=32,
      roll=True,
      lookback="auto",
      horizon=None,
      feature_col=None,
      target_col=None,
      shuffle=True,
      time_enc=False,
      label_len=0,
      is_predict=False,
  ):
      """
      Convert TSDataset to a PyTorch DataLoader with or without rolling. We recommend to use
      to_torch_data_loader(default roll=True) if you don't need to output the rolled numpy array.
      It is much more efficient than rolling separately, especially when the dataframe or lookback
      is large.
  
      :param batch_size: int, the batch_size for a Pytorch DataLoader. It defaults to 32.
      :param roll: Boolean. Whether to roll the dataframe before converting to DataLoader.
             If True, you must also specify lookback and horizon for rolling. If False, you must
             have called tsdataset.roll() before calling to_torch_data_loader(). Default to True.
      :param lookback: int, lookback value. Default to 'auto',
             the mode of time series' cycle length will be taken as the lookback.
      :param horizon: int or list,
             if `horizon` is an int, we will sample `horizon` step
             continuously after the forecasting point.
             if `horizon` is a list, we will sample discretely according
             to the input list.
             specially, when `horizon` is set to 0, ground truth will be generated as None.
      :param feature_col: str or list, indicates the feature col name. Default to None,
             where we will take all available feature in rolling.
      :param target_col: str or list, indicates the target col name. Default to None,
             where we will take all target in rolling. it should be a subset of target_col
             you used to initialize the tsdataset.
      :param shuffle: if the dataloader is shuffled. default to True.
      :param time_enc: bool,
             This parameter should be set to True only when you are using Autoformer model. With
             time_enc to be true, 2 additional numpy ndarray will be returned when you call
             `.to_numpy()`. Be sure to have a time type for dt_col if you set time_enc to True.
      :param label_len: int,
             This parameter should be set to True only when you are using Autoformer model. This
             indicates the length of overlap area of output(y) and input(x) on time axis.
      :param is_predict: bool,
             This parameter should be set to True only when you are processing test data without
             accuracy evaluation. This indicates if the dataset will be sampled as a prediction
             dataset(without groud truth).
  
      :return: A pytorch DataLoader instance. The data returned from dataloader is in the
               following form:
              1. a 3d numpy ndarray when is_predict=True or horizon=0
              and time_enc=False
              2. a 2-dim tuple of 3d numpy ndarray (x, y) when is_predict=False
              and horizon != 0 and time_enc=False
              3. a 4-dim tuple of 3d numpy ndarray (x, y, x_enc, y_enc) when
              time_enc=True
  
      to_torch_data_loader() can be called by:
  
      >>> # Here is a df example:
      >>> # id        datetime      value   "extra feature 1"   "extra feature 2"
      >>> # 00        2019-01-01    1.9     1                   2
      >>> # 01        2019-01-01    2.3     0                   9
      >>> # 00        2019-01-02    2.4     3                   4
      >>> # 01        2019-01-02    2.6     0                   2
      >>> tsdataset = TSDataset.from_pandas(df, dt_col="datetime",
      >>>                                   target_col="value", id_col="id",
      >>>                                   extra_feature_col=["extra feature 1",
      >>>                                                      "extra feature 2"])
      >>> horizon, lookback = 1, 1
      >>> data_loader = tsdataset.to_torch_data_loader(batch_size=32,
      >>>                                              lookback=lookback,
      >>>                                              horizon=horizon)
      >>> # or roll outside. That might be less efficient than the way above.
      >>> tsdataset.roll(lookback=lookback, horizon=horizon, id_sensitive=False)
      >>> x, y = tsdataset.to_numpy()
      >>> print(x, y) # x = [[[1.9, 1, 2 ]], [[2.3, 0, 9 ]]] y = [[[ 2.4 ]], [[ 2.6 ]]]
      >>> data_loader = tsdataset.to_torch_data_loader(batch_size=32, roll=False)
  
      """
      from torch.utils.data import TensorDataset, DataLoader
      import torch
      from bigdl.nano.utils.common import invalidInputError
  
      if roll:
          if horizon is None:
              invalidInputError(
                  False,
                  "You must input horizon if roll is True (default roll=True)!",
              )
          from bigdl.chronos.data.utils.roll_dataset import RollDataset
  
          feature_col = (
              _to_list(feature_col, "feature_col")
              if feature_col is not None
              else self.feature_col
          )
          target_col = (
              _to_list(target_col, "target_col")
              if target_col is not None
              else self.target_col
          )
  
          if time_enc and label_len == 0:
              label_len = max(lookback // 2, 1)
  
          # set scaler index for unscale_numpy
          self.scaler_index = [
              self.target_col.index(t) for t in target_col
          ]
          self.lookback, self.horizon, self.label_len = (
              lookback,
              horizon,
              label_len,
          )
  
          if self.lookback == "auto":
              self.lookback = self.get_cycle_length("mode", top_k=3)
          invalidInputError(
              not self._has_generate_agg_feature,
              "Currently to_torch_data_loader does not support "
              "'gen_global_feature' and 'gen_rolling_feature' methods.",
          )
  
          if isinstance(self.horizon, int):
              need_dflen = self.lookback + self.horizon
          else:
              need_dflen = self.lookback + max(self.horizon)
          if len(self.df) < need_dflen:
              invalidInputError(
                  False,
                  "The length of the dataset must be larger than the sum "
                  "of lookback and horizon, while get lookback+horizon="
                  f"{need_dflen} and the length of dataset is {len(self.df)}.",
              )
  
          torch_dataset = RollDataset(
              self.df,
              dt_col=self.dt_col,
              freq=self._freq,
              lookback=self.lookback,
              horizon=self.horizon,
              feature_col=feature_col,
              target_col=target_col,
              id_col=self.id_col,
              time_enc=time_enc,
              label_len=label_len,
              is_predict=is_predict,
          )
          # TODO gen_rolling_feature and gen_global_feature will be support later
          self.roll_target = target_col
          self.roll_feature = feature_col
  
          batch_size = (
              32 if batch_size is None else batch_size
          )  # _pytorch_fashion_inference
          return DataLoader(
              torch_dataset, batch_size=batch_size, shuffle=shuffle
          )
      else:
          if self.numpy_x is None:
              invalidInputError(
                  False,
                  "Please call 'roll' method before transforming a TSDataset to "
                  "torch DataLoader if roll is False!",
              )
          if self.numpy_y is None:
              x = self.numpy_x
              return DataLoader(
                  TensorDataset(torch.from_numpy(x).float()),
                  batch_size=batch_size,
                  shuffle=shuffle,
              )
          elif self.numpy_x_timeenc is None:
              x, y = self.to_numpy()
              return DataLoader(
                  TensorDataset(
                      torch.from_numpy(x).float(),
                      torch.from_numpy(y).float(),
                  ),
                  batch_size=batch_size,
                  shuffle=shuffle,
              )
          else:
              x, y, x_enc, y_enc = self.to_numpy()
              return DataLoader(
                  TensorDataset(
                      torch.from_numpy(x).float(),
                      torch.from_numpy(y).float(),
                      torch.from_numpy(x_enc).float(),
                      torch.from_numpy(y_enc).float(),
                  ),
                  batch_size=batch_size,
                  shuffle=shuffle,
              )
  
imports: import pandas
  import numpy
  import functools
  import logging
  from bigdl.chronos.data.utils.feature import generate_dt_features, generate_global_features
  from bigdl.chronos.data.utils.impute import impute_timeseries_dataframe
  from bigdl.chronos.data.utils.deduplicate import deduplicate_timeseries_dataframe
  from bigdl.chronos.data.utils.roll import roll_timeseries_dataframe
  from bigdl.chronos.data.utils.time_feature import time_features, gen_time_enc_arr
  from bigdl.chronos.data.utils.scale import unscale_timeseries_numpy, scale_timeseries_numpy
  from bigdl.chronos.data.utils.resample import resample_timeseries_dataframe
  from bigdl.chronos.data.utils.split import split_timeseries_dataframe
  from bigdl.chronos.data.utils.cycle_detection import cycle_length_est
  from bigdl.chronos.data.utils.quality_inspection import quality_check_timeseries_dataframe, _abnormal_value_repair
  from bigdl.chronos.data.utils.utils import _to_list, _check_type, _check_col_within, _check_col_no_na, _check_is_aligned, _check_dt_is_sorted
label_desc: Use image node_id 13 for calling the TSDataset obj's underlying member method code with example usage: obj.to_torch_data_loader(batch_size, roll, lookback, horizon, feature_col, target_col, shuffle, time_enc, label_len, is_predict) and returns: DataLoader, DataLoader, DataLoader, DataLoader