artefactual.preprocessing.LogProbParser#

class artefactual.preprocessing.LogProbParser(k=None)[source]#

Bases: TransformerMixin, BaseEstimator

First pipeline step: completion responses in, a dense logprob array out.

Accepts raw responses rather than arrays, so it opts out of scikit-learn’s input validation. Cross-validation over a pipeline starting with this parser therefore has to start from data this step has already parsed, since sklearn cannot index a response object by row. A numeric array is refused with a TypeError.

This step owns the rank axis – a token’s candidates ordered by likelihood, one column per rank. It sizes the output to k and refuses responses carrying fewer candidates, which is possible only here: downstream, every token has been padded to a common width and a token’s original candidate count is no longer recoverable.

Parameters:

k (int, default=None) – Number of candidates to emit per token, and so the width of the rank axis. None uses the widest present in the batch.

Examples

>>> cube = LogProbParser(k=15).transform(responses)  
>>> cube.shape  # (n_sequences, max_tokens, 15)  

Methods

fit

Validate k; learns nothing, and lets the step compose in a Pipeline.

fit_transform

Fit to data, then transform it.

get_feature_names_out

Names of the rank columns, which the entropy step maps on.

get_metadata_routing

Get metadata routing of this object.

get_params

Get parameters for this estimator.

set_output

Accepted and ignored: the (n, tokens, k) cube has no table form to convert to.

set_params

Set the parameters of this estimator.

transform

Parse responses into a NaN-padded logprob array.

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

Validate k; learns nothing, and lets the step compose in a Pipeline.

Parameters:
  • X (list) – Ignored.

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

Returns:

self

Return type:

LogProbParser

Raises:

sklearn.utils._param_validation.InvalidParameterError – If k is neither None nor an integer of at least 1.

get_feature_names_out(input_features=None)[source]#

Names of the rank columns, which the entropy step maps on.

Parameters:

input_features (array-like of str, default=None) – If given, its first k entries name the columns instead of rank0, …

Returns:

One name per rank column.

Return type:

ndarray of str

Raises:

ValueError – If k is None and no input_features are given, since the width is then only known from data.

set_output(*, transform=None)[source]#

Accepted and ignored: the (n, tokens, k) cube has no table form to convert to.

Output wrapping is off for this step. Pipeline.set_output calls every step, and the entropy step after this one is the first that yields a table.

Parameters:

transform (str, default=None) – Ignored.

Returns:

self

Return type:

LogProbParser

transform(X)[source]#

Parse responses into a NaN-padded logprob array.

Parameters:

X (list) – One completion response, or a sequence of them. Each yields one row per sampled sequence.

Returns:

NaN where a sequence is shorter than the longest in the batch or a token position carried no candidates. Empty input gives an empty (0, 0, 0) array.

Return type:

ndarray of shape (n_sequences, max_tokens, k)

Raises:
  • TypeError – If a response is not a recognised completion format, including a batch line whose body is not one.

  • ValueError – If a logprob is missing, non-finite or positive, if no response carries any log-probabilities, if a response carries fewer than k candidates per token, or if a batch line carries no completion at all.