sktime: [BUG] HTML view of transformations are not showing as a flow and instead shown as class definition directly
Code
Reused from https://github.com/sktime/sktime/discussions/5903 (I still have that question, need to resolve that sometime)
from sklearn.base import BaseEstimator
from sklearn.preprocessing import RobustScaler, QuantileTransformer
from sktime.forecasting.arima import ARIMA
from sktime.forecasting.base import BaseForecaster, ForecastingHorizon
from sktime.forecasting.compose import (
ForecastingPipeline,
ForecastX,
TransformedTargetForecaster,
)
from sktime.forecasting.exp_smoothing import ExponentialSmoothing
from sktime.transformations.series.adapt import TabularToSeriesAdaptor
from sktime.transformations.base import BaseTransformer
from sktime.transformations.compose import FeatureUnion, TransformerPipeline
from sktime.transformations.series.boxcox import BoxCoxTransformer, LogTransformer
from sktime.transformations.series.detrend import Detrender, Deseasonalizer
from sktime.transformations.series.difference import Differencer
from sktime.transformations.series.subset import ColumnSelect
def create_pipeline(
endogenous_forecaster: BaseForecaster,
prediction_horizon: ForecastingHorizon,
future_known_exogenous: list[str] | None = None,
future_unknown_exogenous: list[str] | None = None,
future_unknown_forecaster: BaseForecaster | None = None,
endogenous_transformations: list[BaseTransformer | BaseEstimator] | None = None,
future_known_transformations: list[BaseTransformer | BaseEstimator] | None = None,
future_unknown_transformations: list[BaseTransformer | BaseEstimator] | None = None,
) -> BaseForecaster:
if not endogenous_transformations:
y_pipe = endogenous_forecaster
else:
for counter, endogenous_transformation in enumerate(endogenous_transformations):
if isinstance(endogenous_transformation, BaseTransformer):
continue
endogenous_transformations[counter] = TabularToSeriesAdaptor(endogenous_transformation)
y_pipe = TransformedTargetForecaster(
[TransformerPipeline(endogenous_transformations), endogenous_forecaster]
)
if not future_known_exogenous:
known_X_pipe = None
elif not future_known_transformations:
known_X_pipe = future_known_transformations
else:
for counter, future_known_transformation in enumerate(future_known_transformations):
if isinstance(future_known_transformation, BaseTransformer):
continue
future_known_transformations[counter] = TabularToSeriesAdaptor(
endogenous_transformation
)
known_X_pipe = TransformerPipeline(
[ColumnSelect(columns=future_known_exogenous), *future_known_transformations]
)
if not future_unknown_exogenous:
unknown_X_trafo_pipe = None
elif not future_unknown_transformations:
unknown_X_trafo_pipe = None
else:
for counter, future_unknown_transformation in enumerate(future_unknown_transformations):
if isinstance(future_unknown_transformation, BaseTransformer):
continue
future_unknown_transformations[counter] = TabularToSeriesAdaptor(
endogenous_transformation
)
unknown_X_trafo_pipe = TransformerPipeline(
[ColumnSelect(columns=future_unknown_exogenous), *future_unknown_transformations]
)
if not future_unknown_exogenous:
unknown_X_fcst_pipe = None
elif not future_unknown_forecaster:
unknown_X_fcst_pipe = endogenous_forecaster
else:
unknown_X_fcst_pipe = future_unknown_forecaster
e2e_trafo_pipe_steps: list[BaseTransformer] = []
if known_X_pipe and not unknown_X_trafo_pipe:
e2e_trafo_pipe_steps.append(known_X_pipe)
elif not known_X_pipe and unknown_X_trafo_pipe:
e2e_trafo_pipe_steps.append(unknown_X_trafo_pipe)
elif known_X_pipe and unknown_X_trafo_pipe:
e2e_trafo_pipe_steps.append(FeatureUnion([known_X_pipe, unknown_X_trafo_pipe]))
if e2e_trafo_pipe_steps:
e2e_trafo_pipe = TransformerPipeline(e2e_trafo_pipe_steps)
else:
e2e_trafo_pipe = None
if not unknown_X_fcst_pipe:
e2e_fcst_pipe = y_pipe
else:
e2e_fcst_pipe = ForecastX(
y_pipe,
unknown_X_fcst_pipe,
fh_X=prediction_horizon,
forecaster_X_exogeneous="complement",
)
if e2e_trafo_pipe:
e2e_pipe = ForecastingPipeline([e2e_trafo_pipe, e2e_fcst_pipe])
else:
e2e_pipe = e2e_fcst_pipe
return e2e_pipe
create_pipeline(
ARIMA(),
ForecastingHorizon(values=range(1, 4)),
future_known_exogenous=["alpha", "beta"],
future_unknown_exogenous=["gamma", "delta"],
future_unknown_forecaster=ExponentialSmoothing(),
endogenous_transformations=[
LogTransformer(),
Detrender(model="additive"),
Deseasonalizer(model="additive"),
],
future_known_transformations=[RobustScaler(), BoxCoxTransformer()],
future_unknown_transformations=[QuantileTransformer(), Differencer()],
)
Result
Expectation
Not sure if it’s a bug, but I was expecting a DAG flow for TransformerPipeline component.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (12 by maintainers)
Commits related to this issue
- [ENH] html display of estimators - tag and consistent handling of via `_steps_attr` in `HeterogenousMetaEstimator` descendants (#7233) Implements #7222 Fixes #7152 #### What does this impleme... — committed to ksharma6/sktime by mateuszkasprowicz 2 years ago
- [ENH] html display of estimators - tag and consistent handling of via `_steps_attr` in `HeterogenousMetaEstimator` descendants (#7233) Implements #7222 Fixes #7152 #### What does this impleme... — committed to ericjb/sktime by mateuszkasprowicz 2 years ago
- [ENH] html display of estimators - tag and consistent handling of via `_steps_attr` in `HeterogenousMetaEstimator` descendants (#7233) Implements #7222 Fixes #7152 #### What does this impleme... — committed to sktime/sktime by mateuszkasprowicz 2 years ago
Moved this to a separate issue: https://github.com/sktime/sktime/issues/7222 IMO to be resolved before this to have a general solution. Btw, then I think I could make a small tweak to
_get_visual_blockinsktime.utils._estimator_html_reprinstead of overriding_sk_visual_block_in_HeterogenousMetaEstimator.I am happy with parallel ones, thank you.
Probably being too picky, but it’d be good to have another box outside FeatureUnion. FeatureUnion is actually an input to TransformerPipeline (the outside one) and both that TransformerPipeline and ForecastX together form ForecastingPipeline. In current form, the parent-child relation between FeatureUnoin and outer TransformerPipeline is not clear.