artefactual.scoring.EntropyTransformer#

class artefactual.scoring.EntropyTransformer(pooling)[source]#

Bases: TransformerMixin, BaseEstimator, EntropyContributionsMixin

Reduce per-candidate entropy contributions to the features a calibration was fit on.

Input is the (n_sequences, n_tokens, width) array LogProbParser emits, NaN-padded on both the token and rank axes, or a 2-D (n_sequences, width) table of per-rank logprobs, which is read as one token per row. Output is one feature row per sequence, as wide as the pooling makes it: 1 column for EPRPooling, 2 * width for WEPRPooling.

Learns nothing, but fit records the width of the input and the column names of a 2-D table, as scikit-learn’s validation does, so the features of a named table are named after its columns. A fitted step refuses input of another width: the estimators after it were fitted on the features of that width.

Parameters:

pooling (Pooling or callable) – Required: no pooling is chosen for you. A Pooling instance (EPRPooling(), WEPRPooling(), or a subclass of your own), or a callable f(x, axis) -> (…, n_features) pooling the token axis of the NaN-padded contributions x, whose last axis is the rank axis. Stored as given and wrapped when used, so a string raises TypeError from transform and get_feature_names_out.

Examples

>>> features = EntropyTransformer(WEPRPooling()).transform(
...     logprobs
... )  
>>> features.shape  # (n_sequences, 2 * width)  
>>> EntropyTransformer(pooling=my_pooling).transform(logprobs)  

Methods

applied_pooling

The Pooling that transform applies: pooling itself, or a callable wrapped.

check_pooling_is_the_fitted_one

The Pooling to apply, once it is the one the step was fitted with.

entropy_contributions

Entropy contribution s_kj = -p_kj * ln(p_kj) for each candidate of each token.

fit

Learns nothing; the input is validated so its width and column names are recorded.

fit_transform

Fit to data, then transform it.

get_feature_names_out

Names of the features transform returns.

get_metadata_routing

Get metadata routing of this object.

get_params

Get parameters for this estimator.

set_output

Set output container.

set_params

Set the parameters of this estimator.

transform

Reduce each sequence to one feature row.

transform_tokens

Reduce each token separately, keeping the token axis.

validate

X as a float (n, tokens, width) array.

applied_pooling()[source]#

The Pooling that transform applies: pooling itself, or a callable wrapped.

Raises:

TypeError – If pooling is neither a Pooling nor callable.

Return type:

Pooling

check_pooling_is_the_fitted_one()[source]#

The Pooling to apply, once it is the one the step was fitted with.

A downstream estimator is fitted on the features of the pooling in force at fit, and set_params does not refit, so a swapped pooling would feed it features it was not fitted on. A step that was never fitted has nothing to be held to.

Raises:

ValueError – If pooling differs from the one fit saw: refit.

Return type:

Pooling

fit(X, y=None)[source]#

Learns nothing; the input is validated so its width and column names are recorded.

Parameters:
  • X (array-like or None) – A 3-D cube sets n_features_in_, its last dimension; a 2-D table sets it too, and feature_names_in_ when its columns are named. None fits nothing.

  • y (array-like, default=None) – Ignored.

Returns:

self

Return type:

EntropyTransformer

get_feature_names_out(input_features=None)[source]#

Names of the features transform returns.

Parameters:

input_features (array-like of str, default=None) – Names of the rank columns, as the parser names them (rank0, …): one per column the step was fitted on. They must equal the column names the step was fitted on, if any. Without it the names are the fitted table’s columns, or rank0, … for the width the step was fitted on.

Returns:

What the pooling’s feature_names gives: [“epr”] for EPRPooling; wepr_mean_<rank> for each rank followed by wepr_max_<rank> for each rank for WEPRPooling; by default the pooling’s name for one feature and name_0, name_1, … for more. A rank is named by the column it was read from when the step was fitted on a named table.

Return type:

ndarray of str

Raises:
  • ValueError – If input_features does not have one entry per column fitted on or disagrees with the fitted column names; if the pooling’s width depends on the number of ranks and neither input_features, a fit nor fitted column names give it; or if its feature names do not match its width.

  • TypeError – If pooling is neither a Pooling nor callable.

transform(X)[source]#

Reduce each sequence to one feature row.

A sequence with no tokens has no entropy to measure. Its features are set to zero, which makes the downstream classifier return sigmoid(intercept_), its base rate, and a warning is raised rather than letting the degenerate input pass unremarked.

Parameters:

X (array-like of shape (n_sequences, n_tokens, width) or (n_sequences, width)) – Logprobs, NaN-padded; a nested list is accepted. A 2-D table is one token per row.

Returns:

1 column for EPRPooling, 2 * width for WEPRPooling, the pooling’s own width otherwise. After set_output(transform=”pandas”) or “polars”, a frame whose columns are get_feature_names_out().

Return type:

ndarray of shape (n_sequences, n_features)

Raises:
  • ValueError – If X holds infinity, or is a table that is not numeric 2-D.

  • TypeError – If pooling is neither a Pooling nor callable.

Warns:

EmptySequenceWarning – If any sequence carries no tokens.

transform_tokens(X)[source]#

Reduce each token separately, keeping the token axis.

Applies the same pooling over a one-token window per position, so token features are computed exactly as sequence features are. A padding token (all values NaN) is not passed to the pooling, so a pooling never sees a window the caller did not write.

Parameters:

X (ndarray of shape (n_sequences, n_tokens, width) or (n_sequences, width)) – Logprobs, NaN-padded. A 2-D table is one token per row.

Returns:

Padded tokens are NaN rows. Never wrapped by set_output.

Return type:

ndarray of shape (n_sequences, n_tokens, n_features)

Raises:

ValueError – If X holds infinity, or is a table that is not numeric 2-D.

validate(X, *, reset=False)[source]#

X as a float (n, tokens, width) array.

A 3-D input is the parser’s cube: NaN is the padding marker and passes, inf is no logprob and would reduce to a meaningless feature, so it is refused. Anything else is validated as a numeric 2-D table, which records (reset=True) or checks (reset=False) n_features_in_ and the column names, and is read as one token per row. A list converts, so the step takes what np.asarray would. Empty batches pass: the parser returns an empty cube for them.

Parameters:
  • X (array-like) – A 3-D cube or a 2-D table of per-rank logprobs.

  • reset (bool, default=False) – Record the width (and, for a table, the column names), as fit does, instead of checking them against the recorded ones.

Returns:

width is the input’s last dimension.

Return type:

ndarray of shape (n_sequences, n_tokens, width)

Raises:

ValueError – If a cube holds infinity, or a table is not numeric 2-D or disagrees with the fitted width or column names, or a cube’s last dimension differs from the fitted width.