|
| 1 | +import pandas as pd |
| 2 | +import torch |
| 3 | + |
| 4 | +from pytorch_forecasting._registry import all_objects |
| 5 | +from pytorch_forecasting.adapters.scaler_strategy import ( |
| 6 | + ScalerStrategy, |
| 7 | +) |
| 8 | +from pytorch_forecasting.adapters.utils import ( |
| 9 | + ArrayLike, |
| 10 | + _to_numpy, |
| 11 | + _to_tensor, |
| 12 | +) |
| 13 | +from pytorch_forecasting.data.encoders import ( |
| 14 | + MultiNormalizer, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def get_scaler_strategy(scaler) -> ScalerStrategy: |
| 19 | + """Single dispatch point: the only place that inspects scaler type.""" |
| 20 | + discovered = all_objects( |
| 21 | + object_types="scaler_strategy", |
| 22 | + return_names=False, |
| 23 | + ) |
| 24 | + for strategy_cls in discovered: |
| 25 | + if strategy_cls._is_applicable(scaler): |
| 26 | + return strategy_cls() |
| 27 | + return ScalerStrategy() |
| 28 | + |
| 29 | + |
| 30 | +class ScalerAdapter: |
| 31 | + """ |
| 32 | + Unified array-in / tensor-out interface for single and multi-target scalers. |
| 33 | +
|
| 34 | + Accepts torch.Tensor, np.ndarray, or pd.Series as input. Output is always |
| 35 | + a torch.Tensor. Type-specific behavior (sklearn scalers, GroupNormalizer, |
| 36 | + NaNLabelEncoder, EncoderNormalizer, ...) is delegated to a strategy chosen |
| 37 | + once at construction time (see ``adapters/scaler_strategy.py``). |
| 38 | +
|
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + scaler : object |
| 43 | + The underlying scaling/encoding instance. Accepted types, their expected |
| 44 | + origins, and assumed API contracts are: |
| 45 | +
|
| 46 | + * scikit-learn scalers (from ``sklearn.preprocessing``): |
| 47 | + Implements ``.fit(X)` and ``.transform(X)``. Expects 2D |
| 48 | + numpy arrays of shape ``(n_samples, 1)``. Outputs numpy arrays. |
| 49 | +
|
| 50 | + * ``TorchNormalizer`` (from ``pytorch_forecasting.data.encoders``): |
| 51 | + Implements `.fit(data)` and `.transform(data)`. Expects 1D |
| 52 | + tensors or numpy arrays. Output can be tensor or array. |
| 53 | +
|
| 54 | + *``EncoderNormalizer`` (from ``pytorch_forecasting.data.encoders``): |
| 55 | + Implements `.fit(data)` and `.transform(data)`. Expects 1D |
| 56 | + tensors or numpy arrays. Output can be tensor or array. |
| 57 | + `EncoderNormalizer` signals that it must be fit per-sequence. |
| 58 | +
|
| 59 | + * ``NaNLabelEncoder`` (from `pytorch_forecasting.data.encoders`): |
| 60 | + Implements ``.fit(data)`` and ``.transform(data)``. Expects a |
| 61 | + 1D ``pd.Series`` (or 1D array). Used for categorical encoding. |
| 62 | +
|
| 63 | + * ``GroupNormalizer`` (from ``pytorch_forecasting.data.encoders``): |
| 64 | + Implements ``.fit(data, X)`` and ``.transform(data, X)``. Expects |
| 65 | + `data` as a 1D ``pd.Series`` and `X` as a ``pd.DataFrame`` containing |
| 66 | + required group columns to compute grouped statistics. |
| 67 | +
|
| 68 | + * ``MultiNormalizer`` (from ``pytorch_forecasting.data.encoders``): |
| 69 | + Implements ``.fit(data, X)`` and ``.transform(data.T, X)``. |
| 70 | + Expects 2D array-like inputs of shape ``(n_samples, n_targets)``. |
| 71 | + Must expose a ``.normalizers`` attribute (iterable) containing the |
| 72 | + individual sub-normalizers for each target. |
| 73 | + """ |
| 74 | + |
| 75 | + def __init__(self, scaler): |
| 76 | + self._scaler = scaler |
| 77 | + self.is_multi = isinstance(scaler, MultiNormalizer) |
| 78 | + |
| 79 | + if self.is_multi: |
| 80 | + self._sub_adapters = [ScalerAdapter(norm) for norm in scaler.normalizers] |
| 81 | + self._strategy = None |
| 82 | + self.is_label_encoder = False |
| 83 | + self.fit_per_sequence = any(a.fit_per_sequence for a in self._sub_adapters) |
| 84 | + else: |
| 85 | + self._strategy = get_scaler_strategy(scaler) if scaler is not None else None |
| 86 | + self.is_label_encoder = ( |
| 87 | + self._strategy.get_tag("is_label_encoder", None) |
| 88 | + if self._strategy |
| 89 | + else False |
| 90 | + ) |
| 91 | + self.fit_per_sequence = ( |
| 92 | + self._strategy.get_tag("fit_per_sequence", None) |
| 93 | + if self._strategy |
| 94 | + else False |
| 95 | + ) |
| 96 | + |
| 97 | + @property |
| 98 | + def label_encoder_mask(self) -> list[bool]: |
| 99 | + """Per-target bool list indicating which sub-normalizers are label encoders.""" |
| 100 | + if self.is_multi: |
| 101 | + return [sub.is_label_encoder for sub in self._sub_adapters] |
| 102 | + return [self.is_label_encoder] |
| 103 | + |
| 104 | + def _prepare_input(self, data: ArrayLike) -> ArrayLike: |
| 105 | + """Coerce data to the type the underlying scaler expects.""" |
| 106 | + if self.is_multi: |
| 107 | + arr = _to_numpy(data) |
| 108 | + return arr if arr.ndim == 2 else arr[:, None] |
| 109 | + return self._strategy.prepare_input(data) |
| 110 | + |
| 111 | + def fit(self, data: ArrayLike, X: pd.DataFrame = None) -> "ScalerAdapter": |
| 112 | + """Fit the scaler. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + data : tensor, ndarray, or Series |
| 117 | + Shape ``(n_samples,)`` for single-target or |
| 118 | + ``(n_samples, n_targets)`` for multi-target. |
| 119 | + X : pd.DataFrame, optional |
| 120 | + Group columns. Required when scaler is GroupNormalizer or |
| 121 | + when MultiNormalizer contains GroupNormalizer sub-normalizers. |
| 122 | + """ |
| 123 | + if self._scaler is None: |
| 124 | + return self |
| 125 | + |
| 126 | + prepared = self._prepare_input(data) |
| 127 | + if self.is_multi: |
| 128 | + self._scaler.fit(prepared, X) |
| 129 | + return self |
| 130 | + |
| 131 | + self._strategy.fit(self._scaler, prepared, X) |
| 132 | + return self |
| 133 | + |
| 134 | + def transform(self, data: ArrayLike, X: pd.DataFrame = None) -> torch.Tensor: |
| 135 | + """Transform data, always returning a torch.Tensor. |
| 136 | +
|
| 137 | + Parameters |
| 138 | + ---------- |
| 139 | + data : tensor, ndarray, or Series |
| 140 | + Shape ``(n_samples,)`` for single-target or |
| 141 | + ``(n_samples, n_targets)`` for multi-target. |
| 142 | + X : pd.DataFrame, optional |
| 143 | + Group columns. Required when scaler is GroupNormalizer or |
| 144 | + when MultiNormalizer contains GroupNormalizer sub-normalizers. |
| 145 | +
|
| 146 | + Returns |
| 147 | + ------- |
| 148 | + torch.Tensor |
| 149 | + Same shape as input. |
| 150 | + """ |
| 151 | + if self._scaler is None: |
| 152 | + return _to_tensor(data) |
| 153 | + prepared = self._prepare_input(data) |
| 154 | + |
| 155 | + if self.is_multi: |
| 156 | + results = self._scaler.transform(prepared.T, X) |
| 157 | + return torch.stack([_to_tensor(r) for r in results], dim=-1) |
| 158 | + |
| 159 | + return self._strategy.transform(self._scaler, prepared, data, X) |
| 160 | + |
| 161 | + def fit_transform(self, data: ArrayLike, X: pd.DataFrame = None) -> torch.Tensor: |
| 162 | + return self.fit(data, X).transform(data, X) |
| 163 | + |
| 164 | + def fit_transform_sequence( |
| 165 | + self, data: ArrayLike, X: pd.DataFrame = None |
| 166 | + ) -> torch.Tensor: |
| 167 | + """Fit-and-transform only per-sequence sub-normalizers; transform the rest. |
| 168 | +
|
| 169 | + Used at ``__getitem__`` time for encoder windows. Non-per-sequence |
| 170 | + normalizers use their already-fitted global state. |
| 171 | +
|
| 172 | + For single-target adapters this collapses to fit_transform |
| 173 | + (EncoderNormalizer) or transform (everything else). |
| 174 | +
|
| 175 | + Parameters |
| 176 | + ---------- |
| 177 | + data : tensor, ndarray, or Series |
| 178 | + Shape ``(enc_length,)`` or ``(enc_length, n_targets)``. |
| 179 | +
|
| 180 | + Returns |
| 181 | + ------- |
| 182 | + torch.Tensor |
| 183 | + Same shape as input. |
| 184 | + """ |
| 185 | + if not self.is_multi: |
| 186 | + return ( |
| 187 | + self.fit_transform(data, X) |
| 188 | + if self.fit_per_sequence |
| 189 | + else _to_tensor(data) |
| 190 | + ) |
| 191 | + |
| 192 | + t = _to_tensor(data) |
| 193 | + if t.ndim == 1: |
| 194 | + t = t.unsqueeze(-1) |
| 195 | + |
| 196 | + columns = [] |
| 197 | + for idx, sub in enumerate(self._sub_adapters): |
| 198 | + col = t[:, idx] |
| 199 | + col = sub.fit_transform(col, X) if sub.fit_per_sequence else col |
| 200 | + columns.append(col.unsqueeze(-1)) |
| 201 | + return torch.cat(columns, dim=-1) |
0 commit comments