artefactual.scoring.Pooling#

class artefactual.scoring.Pooling[source]#

Bases: object

Pools the token axis of NaN-padded entropy contributions; the last axis is ranks.

Subclass it and implement __call__. The class name, lower-cased, is the pooling’s name, which names a one-feature result and prefixes a wider one (name_0, name_1, …). Override name to rename it, feature_names to name a result after the rank columns, width to declare the feature count instead of probing it, and set constant_width = True when the count does not depend on k.

Subclasses are @dataclasses.dataclass(frozen=True) classes whose fields are the parameters, so instances compare equal when they have the same type and the same fields, and a sklearn.base.clone of an estimator holding one compares equal to the original. A field holding an array is declared dataclasses.field(compare=False): it is then left out of the comparison and the hash.

Examples

>>> @dataclasses.dataclass(frozen=True)
... class Median(Pooling):
...     def __call__(self, X, axis):
...         return np.nanmedian(np.nansum(X, axis=-1, keepdims=True), axis=axis)
>>> EntropyTransformer(Median())  

Methods

feature_names

One name per feature, given the rank column names.

width

Features the pooling returns for k candidates per token.

property constant_width: bool#

Whether the feature count is the same whatever k is, as EPR’s single feature is.

Probed at k of 1, 2 and 3, so a width that changes only beyond that is missed: override it with a bool to declare it.

feature_names(ranks)[source]#

One name per feature, given the rank column names.

The default is [name] for one feature and name_0, name_1, … for more. A constant-width pooling may be asked with a placeholder rank when k is unknown.

Return type:

list[str]

property name: str#

The feature name of a one-feature pooling and the prefix of a wider one.

width(k)[source]#

Features the pooling returns for k candidates per token.

Read off the output of the pooling on a zeros array of shape (1, 2, k) pooled over axis 1, the axis EntropyTransformer.transform pools. Override it to declare the count instead; a pooling that fails on zeros must.

Raises:

ValueError – If the pooling fails on that probe or does not return (n_sequences, n_features).

Return type:

int