def _create_lagged_data_autoregression(
    target_series: Union[TimeSeries, Sequence[TimeSeries]],
    t_pred: int,
    shift: int,
    last_step_shift: int,
    series_matrix: np.ndarray,
    covariate_matrices: dict[str, np.ndarray],
    lags: dict[str, list[int]],
    component_lags: dict[str, dict[str, list[int]]],
    relative_cov_lags: dict[str, np.ndarray],
    uses_static_covariates: bool,
    last_static_covariates_shape: Optional[tuple[int, int]],
    num_samples: int,
) -> np.ndarray:
    """Extract lagged data from target, past covariates and future covariates for auto-regression
    with SKLearnModels.
    """
    series_length = len(target_series)
    X = []
    for series_type in ["target", "past", "future"]:
        if series_type not in lags:
            continue

        # extract series specific data
        values_matrix = (
            series_matrix
            if series_type == "target"
            else covariate_matrices[series_type]
        )

        if series_type not in component_lags:
            # for global lags over all components, directly extract lagged values from the data
            if series_type == "target":
                relative_lags = [
                    lag - (shift + last_step_shift) for lag in lags[series_type]
                ]
            else:
                relative_lags = relative_cov_lags[series_type] + t_pred

            lagged_data = values_matrix[:, relative_lags].reshape(
                series_length * num_samples, -1
            )
        else:
            # for component-specific lags, sort by lags and components and then extract
            tmp_X = _extract_component_lags_autoregression(
                series_type=series_type,
                values_matrix=values_matrix,
                shift=shift,
                last_step_shift=last_step_shift,
                t_pred=t_pred,
                lags=lags,
                component_lags=component_lags,
            )
            lagged_data = tmp_X.reshape(series_length * num_samples, -1)
        X.append(lagged_data)
    # concatenate retrieved lags
    X = np.concatenate(X, axis=1)

    if not uses_static_covariates:
        return X

    # Need to split up `X` into three equally-sized sub-blocks
    # corresponding to each timeseries in `series`, so that
    # static covariates can be added to each block; valid since
    # each block contains same number of observations:
    X = np.split(X, series_length, axis=0)
    X, _ = add_static_covariates_to_lagged_data(
        features=X,
        target_series=target_series,
        uses_static_covariates=uses_static_covariates,
        last_shape=last_static_covariates_shape,
    )

    # concatenate retrieved lags
    return np.concatenate(X, axis=0)
Extract lagged data from target, past covariates and future covariates for auto-regression
    with SKLearnModels.
    
darts/utils/data/tabularization/tabularization.py