Pipeline
Meta-estimators that combine branches, pick fields, work on every item of split samples, and retrieve items.
Field(name)
Bases: TransformerMixin, BaseEstimator
Return one field of every multimodal sample or region, or a dict of several fields.
It is stateless, so it can sit anywhere in a pipeline, for example after an extractor's predict or inside
ForEach to pick the part of every region that the next step needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | list[str]
|
The name of the field, or a list of names to return dicts of those fields. |
required |
Examples:
>>> from skai.pipeline import Field
>>> Field('visual').fit_transform([{'visual': 'a boat', 'narration': 'At sea.'}]).tolist()
['a boat']
Source code in src/skai/pipeline/_field.py
34 35 | |
__sklearn_tags__()
Declare that the transformer is stateless.
Source code in src/skai/pipeline/_field.py
37 38 39 40 41 42 | |
fit(X=None, y=None)
Validate the parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike | None
|
Ignored. |
None
|
y
|
ArrayLike | None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The transformer. |
Source code in src/skai/pipeline/_field.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
transform(X)
Pick the field of every sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
Dicts, such as multimodal samples, extracted records or regions. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
The value of the field per sample, or a dict of the fields when |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a sample lacks a field. |
Source code in src/skai/pipeline/_field.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
ForEach(estimator)
Bases: TransformerMixin, BaseEstimator
Apply an estimator to every item of list-valued samples, such as the regions of a segmenter.
The items of all samples are flattened and processed in one call of the estimator, so a backend receives them as
one batch, and the outputs are split back into lists of the original lengths. fit fits the estimator on all the
items with y=None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
BaseEstimator
|
The estimator or pipeline applied to every item. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
estimator_ |
BaseEstimator
|
The fitted estimator. |
Examples:
>>> from skai.generation import TextToTextGenerator
>>> from skai.pipeline import ForEach
>>> generator = TextToTextGenerator(instructions='Shorten.', max_length=1, backend='dummy')
>>> ForEach(generator).fit_transform([['One two', 'Three four'], []]).tolist()
[['One', 'Three'], []]
Source code in src/skai/pipeline/_for_each.py
66 67 | |
__sklearn_tags__()
Declare that the samples are lists.
Source code in src/skai/pipeline/_for_each.py
69 70 71 72 73 | |
fit(X, y=None)
Fit the estimator on the items of all samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The list-valued samples. |
required |
y
|
ArrayLike | None
|
Ignored, since items have no targets. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The fitted transformer. |
Source code in src/skai/pipeline/_for_each.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
fit_transform(X, y=None, **fit_params)
Fit the estimator on the items of all samples and return their outputs, running every item once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The list-valued samples. |
required |
y
|
ArrayLike | None
|
Ignored, since items have no targets. |
None
|
**fit_params
|
Any
|
Ignored. |
{}
|
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One list of outputs per sample. |
Source code in src/skai/pipeline/_for_each.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
predict(X)
Predict on the items of all samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The list-valued samples. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One list of predictions per sample, or of outputs when the estimator only transforms. |
Source code in src/skai/pipeline/_for_each.py
139 140 141 142 143 144 145 146 147 148 149 150 | |
transform(X)
Transform the items of all samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The list-valued samples. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One list of outputs per sample. |
Source code in src/skai/pipeline/_for_each.py
127 128 129 130 131 132 133 134 135 136 137 | |
MediaUnion(transformer_list)
Bases: TransformerMixin, _BaseComposition
Run named branches on the same samples and return one multimodal sample, a dict keyed by branch name, per sample.
It is modeled on FeatureUnion: every branch is an estimator, a pipeline or 'passthrough', branches are fitted
through fit_transform in pipelines, and their parameters are routed as <branch>__<parameter>. A branch that
cannot transform, such as one ending in a classifier, contributes its predictions. Instead of
concatenating columns, it keeps the output of every branch under its name, so later steps such as
MultimodalToVideoGenerator receive several inputs per sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transformer_list
|
list[tuple[str, BaseEstimator | str]]
|
The branches, as |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
transformers_ |
list
|
The fitted branches, as |
Examples:
>>> from skai.generation import TextToTextGenerator
>>> from skai.pipeline import MediaUnion
>>> summary = TextToTextGenerator(instructions='Summarize.', max_length=1, backend='dummy')
>>> union = MediaUnion([('text', 'passthrough'), ('summary', summary)])
>>> union.fit_transform(['Hello world'])[0]
{'text': 'Hello world', 'summary': 'Hello'}
Source code in src/skai/pipeline/_union.py
66 67 | |
fit(X, y=None)
Fit every branch on the samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The samples. |
required |
y
|
ArrayLike | None
|
The targets, passed to every branch. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The fitted union. |
Source code in src/skai/pipeline/_union.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
fit_transform(X, y=None, **fit_params)
Fit every branch through its fit_transform and return the multimodal samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The samples. |
required |
y
|
ArrayLike | None
|
The targets, passed to every branch. |
None
|
**fit_params
|
Any
|
Ignored. |
{}
|
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One dict per sample, keyed by branch name. |
Source code in src/skai/pipeline/_union.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
get_params(deep=True)
Return the parameters of the union and, with deep, of its branches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
Whether to include the parameters of the branches as |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The parameters. |
Source code in src/skai/pipeline/_union.py
69 70 71 72 73 74 75 76 77 78 79 | |
predict(X)
Run every branch and return the multimodal samples, so the union can be the final step of a pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The samples. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One dict per sample, keyed by branch name. |
Source code in src/skai/pipeline/_union.py
168 169 170 171 172 173 174 175 176 177 178 | |
set_params(**params)
Set the parameters of the union or of its branches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
Any
|
The parameters, with |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
The union. |
Source code in src/skai/pipeline/_union.py
81 82 83 84 85 86 87 88 89 90 91 92 | |
transform(X)
Run every branch and return the multimodal samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The samples. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One dict per sample, keyed by branch name. |
Source code in src/skai/pipeline/_union.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
Retriever(items, queries, *, metric='cosine')
Bases: BaseEstimator
Index the items of samples with an item pipeline and find the nearest items of queries.
The item pipeline returns, per sample, either one vector, or a list of records with a vector key, such as the
shots of a video with their times and embeddings. fit indexes every item with the index of its sample, and
kneighbors embeds queries with the query pipeline, which is fitted without data, and returns the records of the
nearest items. With a joint text and image model, texts find images, shots or videos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
BaseEstimator
|
The pipeline that turns every sample into a vector or a list of records with vectors. |
required |
queries
|
BaseEstimator
|
The pipeline that turns every query into a vector. |
required |
metric
|
str
|
The distance between vectors. |
'cosine'
|
Attributes:
| Name | Type | Description |
|---|---|---|
items_ |
BaseEstimator
|
The fitted item pipeline. |
queries_ |
BaseEstimator
|
The fitted query pipeline. |
records_ |
list
|
The records of the indexed items, without their vectors. |
index_ |
NearestNeighbors
|
The index of the item vectors. |
Examples:
>>> from skai.embedding import TextEmbedder
>>> from skai.pipeline import Retriever
>>> retriever = Retriever(TextEmbedder(center=False, backend='dummy'), TextEmbedder(backend='dummy'))
>>> retriever.fit(['a goal', 'a corner', 'a foul']).kneighbors(['a goal'], n_neighbors=1)[0][0]['sample']
0
Source code in src/skai/pipeline/_retriever.py
83 84 85 86 | |
__sklearn_tags__()
Declare that samples are media, not a numeric matrix.
Source code in src/skai/pipeline/_retriever.py
88 89 90 91 92 | |
fit(X, y=None)
Index the items of the samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The samples, such as videos. |
required |
y
|
ArrayLike | None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The fitted retriever. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the samples have no items. |
Source code in src/skai/pipeline/_retriever.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
kneighbors(queries, n_neighbors=5)
Find the nearest items of every query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
ArrayLike
|
The queries, such as texts. |
required |
n_neighbors
|
int
|
The number of items per query, at most the number of indexed items. |
5
|
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One list of item records per query, nearest first, each with its |
Source code in src/skai/pipeline/_retriever.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
TopK(scorer, *, label=None, k=None, threshold=None)
Bases: TransformerMixin, BaseEstimator
Keep the k items of every list-valued sample with the highest scores, or those above threshold.
The kept items stay in their original order. A score comes from a callable on one item, or from the
predict_proba column of label of a classifier, which fit fits zero-shot on the items. The items of all
samples are scored in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scorer
|
Callable[[Any], float] | BaseEstimator
|
A callable that scores one item, or a classifier with |
required |
label
|
str | int | None
|
The class whose probability is the score of a classifier. |
None
|
k
|
int | None
|
The number of items to keep per sample. All items above |
None
|
threshold
|
float | None
|
The minimum score of a kept item. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
scorer_ |
Callable | BaseEstimator
|
The callable, or the fitted classifier. |
Examples:
>>> from skai.pipeline import TopK
>>> TopK(len, k=2).fit_transform([['a', 'abc', 'ab', 'abcd']]).tolist()
[['abc', 'abcd']]
Source code in src/skai/pipeline/_top_k.py
60 61 62 63 64 65 66 67 68 69 70 71 | |
__sklearn_tags__()
Declare that the samples are lists.
Source code in src/skai/pipeline/_top_k.py
73 74 75 76 77 | |
fit(X=None, y=None)
Fit a classifier scorer zero-shot, without targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike | None
|
The list-valued samples. |
None
|
y
|
ArrayLike | None
|
Ignored, since items have no targets. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The fitted transformer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Source code in src/skai/pipeline/_top_k.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
transform(X)
Keep the best items of every sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ArrayLike
|
The list-valued samples. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[object_]
|
One list of kept items per sample, in their original order. |
Source code in src/skai/pipeline/_top_k.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |