diff --git a/docs/core_concepts/configuration.md b/docs/core_concepts/configuration.md index 5261fdb..02a54b4 100644 --- a/docs/core_concepts/configuration.md +++ b/docs/core_concepts/configuration.md @@ -142,6 +142,35 @@ Don't infer the resolved method per column from the constructor arguments alone. [Fit-time logging](outputs_and_inspection.md#fit-time-logging). ``` +## Representation parameters + +Use `representation_params` to set options for every feature that selects a given +representation. To override those options for one feature, give its +`feature_preprocessing` entry a `method` and the options to change. + +```python +pre = Preprocessor( + numerical_method="quantile", + representation_params={ + "quantile": {"n_quantiles": 128, "output_distribution": "normal"}, + }, + feature_preprocessing={ + "income": {"method": "quantile", "n_quantiles": 32}, + "city": {"method": "one-hot", "drop": "first"}, + }, +) +``` + +`income` uses 32 quantiles and inherits the normal output distribution. Other +quantile features use 128 quantiles. `city` drops the first category from its +one-hot output. Method names and aliases follow the same rules as the global +method selectors. + +PreTab applies shared constructor defaults first, then `representation_params`, +then options for the individual feature. It validates explicit options against +the representation's registered `allowed_args`. `task`, `target_aware`, and +`placement_strategy` are set on the `Preprocessor` and apply across features. + ## Presets Presets are transparent, named bundles of parameters for common intents. They set the same @@ -218,7 +247,8 @@ The resolution order is deterministic. Later layers win. 1. Library defaults. 2. A `preset`, if given. 3. Explicit constructor arguments (`numerical_method`, `output_dim`, and so on). -4. Per-column `feature_preprocessing` entries, for the columns they name. +4. `representation_params`, for the methods they name. +5. Per-column `feature_preprocessing` entries, for the columns they name. ```{warning} Configuration is validated at `fit` time, not silently coerced. An invalid combination, such @@ -237,6 +267,7 @@ in depth. | ------------------------------------------------------------------------- | -------------------------------------- | -------------------------------------------------------------------- | | `numerical_method`, `categorical_method` | `"ple"`, `"int"` | this page | | `feature_preprocessing` | `None` | this page | +| `representation_params` | `None` | this page | | `output_dim` | `7` | [Resolution and placement](resolution_and_placement.md) | | `adaptive`, `min_output_dim`, `max_output_dim` | `False`, `5`, `10` | [Resolution and placement](resolution_and_placement.md) | | `target_aware`, `placement_strategy` | `True`, `"cart"` | [Target awareness](target_awareness.md) | diff --git a/pretab/compose/config.py b/pretab/compose/config.py index 4408cb2..545ce9d 100644 --- a/pretab/compose/config.py +++ b/pretab/compose/config.py @@ -16,6 +16,7 @@ from __future__ import annotations +from collections.abc import Mapping from dataclasses import dataclass from ..core.parameters import validate_placement, validate_task @@ -25,6 +26,7 @@ CATEGORICAL_METHODS, NUMERICAL_ALIASES, NUMERICAL_METHODS, + get_spec, resolve_method, ) @@ -33,6 +35,7 @@ # Valid values for the high-level ``missing_policy`` orchestration knob. ``None`` # keeps the explicit imputation parameters authoritative (historical behaviour). MISSING_POLICIES = frozenset({"error", "propagate", "impute", "impute_with_indicator", "separate_state"}) +_SHARED_PLACEMENT_PARAMS = frozenset({"task", "target_aware", "placement_strategy"}) def _normalize_method(method, canonical, aliases) -> str: @@ -42,6 +45,49 @@ def _normalize_method(method, canonical, aliases) -> str: return resolve_method(method, canonical, aliases) +def _validate_representation_params(method, params, source): + """Check explicit overrides against the same registry used by the factory.""" + allowed = set(get_spec(method).allowed_args) - _SHARED_PLACEMENT_PARAMS + unknown = params.keys() - allowed + if unknown: + raise invalid_param_error( + "Preprocessor", + source, + params, + f"unsupported parameters for {method!r}: {sorted(unknown, key=str)}", + valid=allowed, + ) + + +def _normalize_representation_params(overrides): + if overrides is None: + return {} + if not isinstance(overrides, Mapping): + raise invalid_param_error("Preprocessor", "representation_params", overrides, "must be a mapping") + + normalized = {} + methods = NUMERICAL_METHODS.keys() | CATEGORICAL_METHODS + for name, params in overrides.items(): + if not isinstance(name, str) or not isinstance(params, Mapping): + raise invalid_param_error( + "Preprocessor", "representation_params", overrides, "must map method names to parameter mappings" + ) + method = resolve_method(name, NUMERICAL_METHODS, NUMERICAL_ALIASES) + if method not in NUMERICAL_METHODS: + method = resolve_method(name, CATEGORICAL_METHODS, CATEGORICAL_ALIASES) + if method not in methods or not get_spec(method).preprocessor_compatible: + raise invalid_param_error( + "Preprocessor", "representation_params", name, "must name a per-column method", valid=methods + ) + if method in normalized: + raise invalid_param_error( + "Preprocessor", "representation_params", name, f"duplicates the settings for {method!r}" + ) + _validate_representation_params(method, params, "representation_params") + normalized[method] = dict(params) + return normalized + + @dataclass(frozen=True) class PreprocessorConfig: """Frozen, normalized configuration derived from Preprocessor parameters. @@ -54,6 +100,7 @@ class PreprocessorConfig: numerical_method: str categorical_method: str feature_preprocessing: dict + representation_params: dict output_dim: int degree: int target_aware: bool @@ -96,6 +143,7 @@ def from_params( add_missing_indicator, missing_policy, verbose, + representation_params=None, ) -> PreprocessorConfig: """Normalize and validate raw Preprocessor parameters into a config. @@ -121,6 +169,7 @@ def from_params( numerical_method=_normalize_method(numerical_method, NUMERICAL_METHODS, NUMERICAL_ALIASES), categorical_method=_normalize_method(categorical_method, CATEGORICAL_METHODS, CATEGORICAL_ALIASES), feature_preprocessing=dict(feature_preprocessing or {}), + representation_params=_normalize_representation_params(representation_params), output_dim=output_dim, degree=degree, target_aware=target_aware, @@ -159,8 +208,26 @@ def method_for(self, feature, *, is_numerical: bool) -> str: """ default = self.numerical_method if is_numerical else self.categorical_method raw = self.feature_preprocessing.get(feature, default) + if isinstance(raw, Mapping): + raw = raw.get("method") + if not isinstance(raw, str): + raise invalid_param_error( + "Preprocessor", "feature_preprocessing", raw, f"column {feature!r} requires a string method name" + ) return self.resolve_numerical(raw) if is_numerical else self.resolve_categorical(raw) + def representation_for(self, feature, *, is_numerical: bool) -> tuple[str, dict]: + """Resolve a column's method and merge shared and feature-specific options.""" + method = self.method_for(feature, is_numerical=is_numerical) + params = self.representation_params.get(method, {}) + override = self.feature_preprocessing.get(feature) + methods = NUMERICAL_METHODS if is_numerical else CATEGORICAL_METHODS + if isinstance(override, Mapping) and method in methods: + feature_params = {name: value for name, value in override.items() if name != "method"} + _validate_representation_params(method, feature_params, f"feature_preprocessing[{feature!r}]") + params = params | feature_params + return method, params + @property def seed_kwargs(self) -> dict: """Return ``{"random_state": ...}`` only when a seed was explicitly set. diff --git a/pretab/compose/factory.py b/pretab/compose/factory.py index c518edc..4e744f7 100644 --- a/pretab/compose/factory.py +++ b/pretab/compose/factory.py @@ -137,6 +137,8 @@ def get_numerical_transformer_steps( imputer_kwargs: dict | None = None, add_missing_indicator: bool = False, scaling: str | None = None, + *, + representation_params: dict | None = None, **kwargs, ): """Return the ordered ``(name, transformer)`` steps for a numerical ``method``.""" @@ -176,6 +178,7 @@ def get_numerical_transformer_steps( spec = get_spec(method) cls = spec.transformer_cls filtered = _filter_kwargs(spec.allowed_args, kwargs) + filtered.update(representation_params or {}) placement = _placement_kwargs(spec, kwargs) if method == "box-cox": @@ -191,7 +194,7 @@ def get_numerical_transformer_steps( # can sit outside their [5, 50] basis range); the legacy families keep # their own wider bounds, so only clamp for B/M/I. if method in _BMI_SPLINE_METHODS: - output_dim = kwargs.get("output_dim") + output_dim = spline_kwargs.get("output_dim") if output_dim is not None: spline_kwargs["output_dim"] = _clamp_spline_basis(output_dim) @@ -211,6 +214,8 @@ def get_categorical_transformer_steps( imputer_strategy: str = "most_frequent", imputer_kwargs: dict | None = None, add_missing_indicator: bool = False, + *, + representation_params: dict | None = None, **kwargs, ): """Return the ordered ``(name, transformer)`` steps for a categorical ``method``.""" @@ -234,13 +239,14 @@ def get_categorical_transformer_steps( spec = get_spec(method) cls = spec.transformer_cls + overrides = representation_params or {} if method == "int": - steps.append(("continuous_ordinal", cls())) + steps.append(("continuous_ordinal", cls(**overrides))) elif method == "one-hot": # Default to ignoring unseen categories so transform never crashes on # categories absent at fit time; callers can override via kwargs. - onehot_kwargs = {"handle_unknown": "ignore", **kwargs} + onehot_kwargs = {"handle_unknown": "ignore", **kwargs, **overrides} categories = onehot_kwargs.get("categories", "auto") drop = onehot_kwargs.get("drop") if ( @@ -255,25 +261,28 @@ def get_categorical_transformer_steps( steps.append(("onehot", cls(**onehot_kwargs))) steps.append(("to_float", ToFloatTransformer())) elif method == "pretrained": - steps.append(("pretrained", cls())) + steps.append(("pretrained", cls(**overrides))) elif method == "none": - steps.append(("none", cls())) + steps.append(("none", cls(**overrides))) elif method == "onehot_from_ordinal": - steps.append(("onehot_from_ordinal", cls())) + steps.append(("onehot_from_ordinal", cls(**overrides))) else: call_kwargs = _filter_kwargs(spec.allowed_args, kwargs) call_kwargs.update(_placement_kwargs(spec, kwargs)) + call_kwargs.update(overrides) steps.append((method, cls(**call_kwargs))) return steps -def create_transformer(method: str, *, is_numerical: bool, config: PreprocessorConfig) -> Pipeline | FeatureUnion: +def create_transformer( + method: str, *, is_numerical: bool, config: PreprocessorConfig, representation_params: dict | None = None +) -> Pipeline | FeatureUnion: """Build the per-column :class:`~sklearn.pipeline.Pipeline` for one feature. ``method`` is the resolved method name; ``is_numerical`` selects the numerical or categorical construction path. All width / placement / seeding knobs are - taken from ``config``. + taken from ``config`` and then updated by validated representation parameters. Raises ------ @@ -306,6 +315,7 @@ def create_transformer(method: str, *, is_numerical: bool, config: PreprocessorC degree=config.degree, scaling=config.scaling, placement_strategy=config.placement_strategy, + representation_params=representation_params, **config.seed_kwargs, ) else: @@ -328,6 +338,7 @@ def create_transformer(method: str, *, is_numerical: bool, config: PreprocessorC method, add_imputer=plan["add_imputer"], imputer_strategy=plan["strategy"], + representation_params=representation_params, **constructor_kwargs, ) @@ -403,8 +414,8 @@ def build_column_transformer( transformers = [] for (kind, feature), name in zip(blocks, _step_names(blocks), strict=True): is_numerical = kind == "num" - method = config.method_for(feature, is_numerical=is_numerical) - pipeline = create_transformer(method, is_numerical=is_numerical, config=config) + method, params = config.representation_for(feature, is_numerical=is_numerical) + pipeline = create_transformer(method, is_numerical=is_numerical, config=config, representation_params=params) transformers.append((name, pipeline, [str(feature)])) return ColumnTransformer( transformers=transformers, diff --git a/pretab/compose/inspection.py b/pretab/compose/inspection.py index f6ca0cf..9fe9c4f 100644 --- a/pretab/compose/inspection.py +++ b/pretab/compose/inspection.py @@ -300,9 +300,8 @@ def build_feature_info(column_transformer, *, embeddings, embedding_dimensions): step = representation_pipeline.named_steps["onehot"] if hasattr(step, "categories_"): categories = sum(len(cat) for cat in step.categories_) - dimension = categories - if separate_state_dimension is not None: - dimension = separate_state_dimension + span = column_transformer.output_indices_[name] + dimension = span.stop - span.start categorical_feature_info[feature_name] = { "preprocessing": preprocessing_type, "dimension": dimension, diff --git a/pretab/compose/registry.py b/pretab/compose/registry.py index 22d168b..93d2b0c 100644 --- a/pretab/compose/registry.py +++ b/pretab/compose/registry.py @@ -95,8 +95,8 @@ class TransformerSpec: transforms prepend a positive-range scaler) record their primary class here; the extra wiring lives in :mod:`pretab.compose.factory`. allowed_args : tuple of str - Constructor argument names the method accepts. Used to filter the shared - Preprocessor keyword arguments down to what the class understands. + Constructor argument names exposed through the Preprocessor. Used to + filter shared arguments and validate representation-specific overrides. feature_kind : frozenset of str The feature kinds the method applies to (``"numerical"`` and/or ``"categorical"``). Only ``none`` (passthrough) applies to both. @@ -195,13 +195,17 @@ def _spec(name, cls, allowed_args=(), **kwargs): # ordering), then the categorical-only methods. _SPECS: tuple[TransformerSpec, ...] = ( # --- numerical: scalers / distribution transforms (no placement) --- - _spec("standardization", StandardScaler), - _spec("minmax", MinMaxScaler), - _spec("quantile", QuantileTransformer, ("n_quantiles", "output_distribution", "random_state")), + _spec("standardization", StandardScaler, ("with_mean", "with_std")), + _spec("minmax", MinMaxScaler, ("feature_range", "clip")), + _spec( + "quantile", + QuantileTransformer, + ("n_quantiles", "output_distribution", "random_state", "subsample", "ignore_implicit_zeros"), + ), _spec("polynomial", PolynomialFeatures, ("degree", "interaction_only", "include_bias")), - _spec("robust", RobustScaler), - _spec("box-cox", PowerTransformer), - _spec("yeo-johnson", PowerTransformer), + _spec("robust", RobustScaler, ("with_centering", "with_scaling", "quantile_range", "unit_variance")), + _spec("box-cox", PowerTransformer, ("standardize",)), + _spec("yeo-johnson", PowerTransformer, ("standardize",)), # --- numerical: piecewise-linear encoding (always target-aware) --- _spec( "ple", @@ -314,30 +318,37 @@ def _spec(name, cls, allowed_args=(), **kwargs): _spec( "bspline", BSplineTransformer, - ("degree", "task", "adaptive", "min_output_dim", "max_output_dim", "random_state"), + ("output_dim", "degree", "task", "adaptive", "min_output_dim", "max_output_dim", "random_state"), **_BOTH_MODE, ), _spec( "mspline", MSplineTransformer, - ("degree", "task", "adaptive", "min_output_dim", "max_output_dim", "random_state"), + ("output_dim", "degree", "task", "adaptive", "min_output_dim", "max_output_dim", "random_state"), **_BOTH_MODE, ), _spec( "ispline", ISplineTransformer, - ("degree", "task", "adaptive", "min_output_dim", "max_output_dim", "random_state"), + ("output_dim", "degree", "task", "adaptive", "min_output_dim", "max_output_dim", "random_state"), **_BOTH_MODE, ), # --- numerical / categorical: passthrough --- _spec("none", NoTransformer, feature_kind=frozenset({NUMERICAL, CATEGORICAL})), # --- categorical-only methods --- _spec("int", ContinuousOrdinalTransformer, feature_kind=frozenset({CATEGORICAL})), - _spec("one-hot", OneHotEncoder, feature_kind=frozenset({CATEGORICAL}), sparse_output=True), + _spec( + "one-hot", + OneHotEncoder, + ("categories", "drop", "sparse_output", "dtype", "handle_unknown", "min_frequency", "max_categories"), + feature_kind=frozenset({CATEGORICAL}), + sparse_output=True, + ), _spec("onehot_from_ordinal", OneHotFromOrdinalTransformer, feature_kind=frozenset({CATEGORICAL})), _spec( "pretrained", LanguageEmbeddingTransformer, + ("model_name",), feature_kind=frozenset({CATEGORICAL}), optional_dependency="embeddings", ), diff --git a/pretab/extension.py b/pretab/extension.py index 7129eed..6104715 100644 --- a/pretab/extension.py +++ b/pretab/extension.py @@ -167,8 +167,9 @@ class without one raises ``TypeError``. For a class without a ``uses_target``, and :class:`~pretab.CrossFittedTransformer` refits the block on every fold of a wrapped :class:`~pretab.Preprocessor`. allowed_args : iterable of str, default=() - Constructor argument names the shared Preprocessor keyword arguments are - filtered down to for this method. + Constructor argument names exposed through the Preprocessor. They filter + shared arguments and allow overrides in ``representation_params`` and + ``feature_preprocessing``. placement_strategies : iterable of str, default=() Placement strategies the method honours (empty for methods without data-driven placement). ``Preprocessor`` passes its ``placement_strategy`` diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index aaba9df..3f06527 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -172,8 +172,16 @@ class Preprocessor(TransformerMixin, BaseEstimator): feature_preprocessing : dict, optional Mapping of individual column names to a method, overriding the global ``numerical_method`` / ``categorical_method`` for those columns only, e.g. - ``{"age": "cubicspline", "city": "pretrained"}``. Columns absent from the dict fall back to - the global defaults. + ``{"age": "cubicspline", "city": "pretrained"}``. Use a mapping with a ``"method"`` key + and constructor options to configure a single feature, e.g. + ``{"age": {"method": "quantile", "n_quantiles": 32}}``. Columns absent from the dict use + the global method and its options. + representation_params : dict, optional + Mapping of method names to constructor options used by every feature that selects that + method, e.g. ``{"quantile": {"n_quantiles": 128, "output_distribution": "normal"}}``. + These options override shared constructor defaults such as ``output_dim``. Options in + ``feature_preprocessing`` take precedence. ``task``, ``target_aware`` and + ``placement_strategy`` are configured on the preprocessor itself. output_dim : int, default=7 The single width knob shared by every numerical method: the number of non-bias output columns produced per input feature (bins for PLE/binning, centers for the feature maps, @@ -459,6 +467,9 @@ class Preprocessor(TransformerMixin, BaseEstimator): ['cat_gender', 'num_age'] """ + # Specs and pickles from before this option was added have no instance attribute. + representation_params = None + def __init__( self, numerical_method=UNSET, @@ -490,6 +501,7 @@ def __init__( dtype=None, verbose=0, preset=None, + representation_params=None, ): """ Initialize the Preprocessor with various transformation options for tabular data. @@ -501,6 +513,7 @@ def __init__( self.numerical_method = numerical_method self.categorical_method = categorical_method self.feature_preprocessing = feature_preprocessing + self.representation_params = representation_params self.output_dim = output_dim self.degree = degree self.target_aware = target_aware @@ -581,6 +594,7 @@ def fit(self, X, y=None, embeddings=None): numerical_method=resolved["numerical_method"], categorical_method=resolved["categorical_method"], feature_preprocessing=resolved["feature_preprocessing"], + representation_params=resolved["representation_params"], output_dim=resolved["output_dim"], degree=resolved["degree"], target_aware=resolved["target_aware"], diff --git a/tests/extension/test_registration_discovery.py b/tests/extension/test_registration_discovery.py index e8279df..7b1db73 100644 --- a/tests/extension/test_registration_discovery.py +++ b/tests/extension/test_registration_discovery.py @@ -164,6 +164,17 @@ def test_register_categorical_end_to_end_through_preprocessor(): assert pipeline.named_steps["cat_reg"].degree == 2 +def test_registered_representation_accepts_explicit_options(): + register_representation("cat_reg", _CatStringLength, allowed_args=("degree",)) + X = pd.DataFrame({"city": ["Rome", "Berlin"], "country": ["Italy", "Germany"]}) + pre = Preprocessor( + categorical_method="cat_reg", + representation_params={"cat_reg": {"degree": 3}}, + feature_preprocessing={"country": {"method": "cat_reg", "degree": 4}}, + ) + np.testing.assert_array_equal(pre.fit_transform(X), [[12.0, 20.0], [18.0, 28.0]]) + + def test_duplicate_registration_requires_override(): register_representation("square_reg", _Square) with pytest.raises(ValueError, match="already registered"): diff --git a/tests/integration/test_preprocessor.py b/tests/integration/test_preprocessor.py index c28c73b..4aebbc2 100644 --- a/tests/integration/test_preprocessor.py +++ b/tests/integration/test_preprocessor.py @@ -172,6 +172,7 @@ def test_dict_keys_reflect_column_names(sample_data): "numerical_method", "categorical_method", "feature_preprocessing", + "representation_params", "output_dim", "adaptive", "min_output_dim", diff --git a/tests/integration/test_representation_parameters.py b/tests/integration/test_representation_parameters.py new file mode 100644 index 0000000..373da1b --- /dev/null +++ b/tests/integration/test_representation_parameters.py @@ -0,0 +1,118 @@ +"""Representation parameters flow through the public Preprocessor API.""" + +import sys +from types import ModuleType + +import numpy as np +import pandas as pd +import pytest +from sklearn.base import clone +from sklearn.preprocessing import OneHotEncoder, PolynomialFeatures, QuantileTransformer + +from pretab import Preprocessor +from pretab.exceptions import InvalidParamError + + +def test_representation_options_override_shared_defaults_per_method_and_feature(): + x = np.linspace(0.1, 0.9, 24) + X = pd.DataFrame({"shared": x, "narrow": x, "string_override": x, "rank": x**2}) + pre = Preprocessor( + numerical_method="polynomial", + degree=4, + scaling="none", + representation_params={ + "poly": {"degree": 3, "include_bias": False}, + "quantile": {"n_quantiles": 8, "output_distribution": "normal"}, + }, + feature_preprocessing={ + "narrow": {"method": "polynomial", "degree": 2}, + "string_override": "poly", + "rank": {"method": "quantile", "n_quantiles": 4}, + }, + ) + fitted = clone(pre) + assert isinstance(fitted, Preprocessor) + fitted.fit(X) + blocks = fitted.transform(X, return_array=False) + for feature, degree in [("shared", 3), ("narrow", 2), ("string_override", 3)]: + expected = PolynomialFeatures(degree=degree, include_bias=False).fit_transform(X[[feature]]) + np.testing.assert_allclose(blocks[f"num_{feature}"], expected) + expected_rank = QuantileTransformer(n_quantiles=4, output_distribution="normal").fit_transform(X[["rank"]]) + np.testing.assert_allclose(blocks["num_rank"], expected_rank) + + restored = Preprocessor.from_spec(fitted.to_spec()) + assert restored.get_params()["representation_params"] == pre.representation_params + np.testing.assert_array_equal(restored.transform(X), fitted.transform(X)) + + +def test_one_hot_options_keep_block_width_and_metadata_in_sync(): + X = pd.DataFrame({"city": ["b", "a", "c", "b"], "region": ["y", "z", "x", "y"]}) + pre = Preprocessor( + categorical_method="one-hot", + representation_params={"ohe": {"drop": "first", "sparse_output": False}}, + feature_preprocessing={"region": {"method": "one_hot", "drop": None}}, + ).fit(X) + + blocks = pre.transform(X, return_array=False) + _, metadata, _ = pre.get_feature_info(verbose=False) + for feature, drop in [("city", "first"), ("region", None)]: + expected = OneHotEncoder(drop=drop, sparse_output=False).fit_transform(X[[feature]]) + np.testing.assert_array_equal(blocks[f"cat_{feature}"], expected) + assert metadata[feature]["dimension"] == expected.shape[1] + assert metadata[feature]["categories"] == 3 + + +def test_pretrained_model_names_follow_method_and_feature_overrides(monkeypatch): + class StubSentenceTransformer: + def __init__(self, model_name): + self.dim = {"shared-model": 2, "feature-model": 3}[model_name] + + def encode(self, values, *, convert_to_numpy): + return np.zeros((len(values), self.dim)) + + dependency = ModuleType("sentence_transformers") + monkeypatch.setattr(dependency, "SentenceTransformer", StubSentenceTransformer, raising=False) + monkeypatch.setitem(sys.modules, "sentence_transformers", dependency) + X = pd.DataFrame({"city": ["b", "a", "c"], "region": ["y", "z", "x"]}) + pre = Preprocessor( + categorical_method="pretrained", + representation_params={"pretrained": {"model_name": "shared-model"}}, + feature_preprocessing={"region": {"method": "pretrained", "model_name": "feature-model"}}, + ).fit(X) + + blocks = pre.transform(X, return_array=False) + assert blocks["cat_city"].shape == (len(X), 2) + assert blocks["cat_region"].shape == (len(X), 3) + + +@pytest.mark.parametrize( + "options, message", + [ + ({"representation_params": []}, "must be a mapping"), + ({"representation_params": {"quantile": 8}}, "parameter mappings"), + ({"representation_params": {"unknown": {}}}, "per-column method"), + ({"representation_params": {"poly": {}, "polynomial": {}}}, "duplicates"), + ({"representation_params": {"quantile": {"n_quantile": 8}}}, "n_quantile"), + ({"feature_preprocessing": {"x": {"degree": 2}}}, "requires a string method name"), + ({"feature_preprocessing": {"x": {"method": "polynomial", "degrees": 2}}}, "degrees"), + ({"feature_preprocessing": {"x": {"method": "rbf", "target_aware": False}}}, "target_aware"), + ], +) +def test_invalid_explicit_representation_options_fail_clearly(options, message): + X = pd.DataFrame({"x": np.linspace(0.1, 0.9, 24)}) + with pytest.raises(InvalidParamError, match=message): + Preprocessor(numerical_method="polynomial", **options).fit(X) + + +def test_spec_written_before_representation_options_can_still_be_loaded_and_cloned(): + X = pd.DataFrame({"x": [0.1, 0.2, 0.4]}) + fitted = Preprocessor(numerical_method="minmax").fit(X) + spec = fitted.to_spec() + spec["params"].pop("representation_params") + spec["state"].pop("representation_params") + + restored = Preprocessor.from_spec(spec) + cloned = clone(restored) + assert isinstance(cloned, Preprocessor) + assert cloned.get_params()["representation_params"] is None + np.testing.assert_array_equal(restored.transform(X), fitted.transform(X))