Skip to content

pipeline

Provides tools to chain functions used to create experiment sequences.

Pipe

Bases: Protocol

Takes in an _ExperimentalSequence and modifies it before returning it.

Source code in autora/experimentalist/pipeline.py
30
31
32
33
34
35
@runtime_checkable
class Pipe(Protocol):
    """Takes in an _ExperimentalSequence and modifies it before returning it."""

    def __call__(self, ex: _ExperimentalSequence) -> _ExperimentalSequence:
        ...

Pipeline

Processes ("pipelines") a series of ExperimentalSequences through a pipeline.

Examples:

A pipeline which filters even values 0 to 9:

>>> p = Pipeline(
... [("is_even", lambda values: filter(lambda i: i % 2 == 0, values))]  # a "pipe" function
... )
>>> list(p(range(10)))
[0, 2, 4, 6, 8]

A pipeline which filters for square, odd numbers:

>>> from math import sqrt
>>> p = Pipeline([
... ("is_odd", lambda values: filter(lambda i: i % 2 != 0, values)),
... ("is_sqrt", lambda values: filter(lambda i: sqrt(i) % 1 == 0., values))
... ])
>>> list(p(range(100)))
[1, 9, 25, 49, 81]
>>> from itertools import product
>>> Pipeline([("pool", lambda: product(range(5), ["a", "b"]))])
Pipeline(steps=[('pool', <function <lambda> at 0x...>)], params={})
>>> Pipeline([
... ("pool", lambda: product(range(5), ["a", "b"])),
... ("filter", lambda values: filter(lambda i: i[0] % 2 == 0, values))
... ])
Pipeline(steps=[('pool', <function <lambda> at 0x...>),         ('filter', <function <lambda> at 0x...>)],         params={})
>>> pipeline = Pipeline([
... ("pool", lambda maximum: product(range(maximum), ["a", "b"])),
... ("filter", lambda values, divisor: filter(lambda i: i[0] % divisor == 0, values))
... ] ,
... params = {"pool": {"maximum":5}, "filter": {"divisor": 2}})
>>> pipeline
Pipeline(steps=[('pool', <function <lambda> at 0x...>),         ('filter', <function <lambda> at 0x...>)],         params={'pool': {'maximum': 5}, 'filter': {'divisor': 2}})
>>> list(pipeline.run())
[(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b'), (4, 'a'), (4, 'b')]
>>> pipeline.params = {"pool": {"maximum":7}, "filter": {"divisor": 3}}
>>> list(pipeline())
[(0, 'a'), (0, 'b'), (3, 'a'), (3, 'b'), (6, 'a'), (6, 'b')]
>>> pipeline.params = {"pool": {"maximum":7}}
>>> list(pipeline())
Traceback (most recent call last):
...
TypeError: <lambda>() missing 1 required positional argument: 'divisor'
Source code in autora/experimentalist/pipeline.py
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 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
108
109
110
111
112
113
114
115
116
117
118
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class Pipeline:
    """
    Processes ("pipelines") a series of ExperimentalSequences through a pipeline.

    Examples:
        A pipeline which filters even values 0 to 9:
        >>> p = Pipeline(
        ... [("is_even", lambda values: filter(lambda i: i % 2 == 0, values))]  # a "pipe" function
        ... )
        >>> list(p(range(10)))
        [0, 2, 4, 6, 8]

        A pipeline which filters for square, odd numbers:
        >>> from math import sqrt
        >>> p = Pipeline([
        ... ("is_odd", lambda values: filter(lambda i: i % 2 != 0, values)),
        ... ("is_sqrt", lambda values: filter(lambda i: sqrt(i) % 1 == 0., values))
        ... ])
        >>> list(p(range(100)))
        [1, 9, 25, 49, 81]


        >>> from itertools import product
        >>> Pipeline([("pool", lambda: product(range(5), ["a", "b"]))]) # doctest: +ELLIPSIS
        Pipeline(steps=[('pool', <function <lambda> at 0x...>)], params={})

        >>> Pipeline([
        ... ("pool", lambda: product(range(5), ["a", "b"])),
        ... ("filter", lambda values: filter(lambda i: i[0] % 2 == 0, values))
        ... ]) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('pool', <function <lambda> at 0x...>), \
        ('filter', <function <lambda> at 0x...>)], \
        params={})

        >>> pipeline = Pipeline([
        ... ("pool", lambda maximum: product(range(maximum), ["a", "b"])),
        ... ("filter", lambda values, divisor: filter(lambda i: i[0] % divisor == 0, values))
        ... ] ,
        ... params = {"pool": {"maximum":5}, "filter": {"divisor": 2}})
        >>> pipeline # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('pool', <function <lambda> at 0x...>), \
        ('filter', <function <lambda> at 0x...>)], \
        params={'pool': {'maximum': 5}, 'filter': {'divisor': 2}})
        >>> list(pipeline.run())
        [(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b'), (4, 'a'), (4, 'b')]

        >>> pipeline.params = {"pool": {"maximum":7}, "filter": {"divisor": 3}}
        >>> list(pipeline())
        [(0, 'a'), (0, 'b'), (3, 'a'), (3, 'b'), (6, 'a'), (6, 'b')]

        >>> pipeline.params = {"pool": {"maximum":7}}
        >>> list(pipeline()) # doctest: +ELLIPSIS
        Traceback (most recent call last):
        ...
        TypeError: <lambda>() missing 1 required positional argument: 'divisor'


    """

    def __init__(
        self,
        steps: Optional[Sequence[_StepType]] = None,
        params: Optional[Dict[str, Any]] = None,
    ):
        """Initialize the pipeline with a series of Pipe objects."""
        if steps is None:
            steps = list()
        self.steps = steps

        if params is None:
            params = dict()
        self.params = params

    def __repr__(self):
        return f"Pipeline(steps={self.steps}, params={self.params})"

    def __call__(
        self,
        ex: Optional[_ExperimentalSequence] = None,
        **params,
    ) -> _ExperimentalSequence:
        """Successively pass the input values through the Pipe."""

        # Initialize the parameters objects.
        pipeline_params = _parse_params_to_nested_dict(
            self.params, divider=PARAM_DIVIDER
        )
        call_params = _parse_params_to_nested_dict(params, divider=PARAM_DIVIDER)
        merged_params = _merge_dicts(pipeline_params, call_params)

        try:
            # Check we have steps to use
            assert len(self.steps) > 0
        except AssertionError:
            # If the pipeline doesn't have any steps...
            if ex is not None:
                # ...the output is the input
                return ex
            elif ex is None:
                # ... unless the input was None, in which case it's an emtpy list
                return []

        # Make an iterator from the steps, so that we can be sure to only go through them once
        # (Otherwise if we handle the "pool" as a special case, we have to track our starting point)
        pipes_iterator = iter(self.steps)

        # Initialize our results object
        if ex is None:
            # ... there's no input, so presumably the first element in the steps is a pool
            # which should generate our initial values.
            name, pool = next(pipes_iterator)
            if isinstance(pool, Pool):
                # Here, the pool is a Pool callable, which we can pass parameters.
                all_params_for_pool = merged_params.get(name, dict())
                results = [pool(**all_params_for_pool)]
            elif isinstance(pool, Iterable):
                # Otherwise, the pool should be an iterable which we can just use as is.
                results = [pool]

        else:
            # ... there's some input, so we can use that as the initial value
            results = [ex]

        # Run the successive steps over the last result
        for name, pipe in pipes_iterator:
            assert isinstance(pipe, Pipe)
            all_params_for_pipe = merged_params.get(name, dict())
            results.append(pipe(results[-1], **all_params_for_pipe))

        return results[-1]

    run = __call__

__call__(ex=None, **params)

Successively pass the input values through the Pipe.

Source code in autora/experimentalist/pipeline.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def __call__(
    self,
    ex: Optional[_ExperimentalSequence] = None,
    **params,
) -> _ExperimentalSequence:
    """Successively pass the input values through the Pipe."""

    # Initialize the parameters objects.
    pipeline_params = _parse_params_to_nested_dict(
        self.params, divider=PARAM_DIVIDER
    )
    call_params = _parse_params_to_nested_dict(params, divider=PARAM_DIVIDER)
    merged_params = _merge_dicts(pipeline_params, call_params)

    try:
        # Check we have steps to use
        assert len(self.steps) > 0
    except AssertionError:
        # If the pipeline doesn't have any steps...
        if ex is not None:
            # ...the output is the input
            return ex
        elif ex is None:
            # ... unless the input was None, in which case it's an emtpy list
            return []

    # Make an iterator from the steps, so that we can be sure to only go through them once
    # (Otherwise if we handle the "pool" as a special case, we have to track our starting point)
    pipes_iterator = iter(self.steps)

    # Initialize our results object
    if ex is None:
        # ... there's no input, so presumably the first element in the steps is a pool
        # which should generate our initial values.
        name, pool = next(pipes_iterator)
        if isinstance(pool, Pool):
            # Here, the pool is a Pool callable, which we can pass parameters.
            all_params_for_pool = merged_params.get(name, dict())
            results = [pool(**all_params_for_pool)]
        elif isinstance(pool, Iterable):
            # Otherwise, the pool should be an iterable which we can just use as is.
            results = [pool]

    else:
        # ... there's some input, so we can use that as the initial value
        results = [ex]

    # Run the successive steps over the last result
    for name, pipe in pipes_iterator:
        assert isinstance(pipe, Pipe)
        all_params_for_pipe = merged_params.get(name, dict())
        results.append(pipe(results[-1], **all_params_for_pipe))

    return results[-1]

__init__(steps=None, params=None)

Initialize the pipeline with a series of Pipe objects.

Source code in autora/experimentalist/pipeline.py
105
106
107
108
109
110
111
112
113
114
115
116
117
def __init__(
    self,
    steps: Optional[Sequence[_StepType]] = None,
    params: Optional[Dict[str, Any]] = None,
):
    """Initialize the pipeline with a series of Pipe objects."""
    if steps is None:
        steps = list()
    self.steps = steps

    if params is None:
        params = dict()
    self.params = params

Pool

Bases: Protocol

Creates an experimental sequence from scratch.

Source code in autora/experimentalist/pipeline.py
22
23
24
25
26
27
@runtime_checkable
class Pool(Protocol):
    """Creates an experimental sequence from scratch."""

    def __call__(self) -> _ExperimentalSequence:
        ...

make_pipeline(steps=None, params=None)

A factory function to make pipeline objects.

The pipe objects' names will be set to the lowercase of their types, plus an index starting from 0 for non-unique names.

Parameters:

Name Type Description Default
steps Optional[Sequence[Union[Pool, Pipe]]]

a sequence of Pipe-compatible objects

None
params Optional[Dict[str, Any]]

a dictionary of parameters passed to each Pipe by its inferred name

None

Returns:

Type Description
Pipeline

A pipeline object

Examples:

You can create pipelines using purely anonymous functions:

>>> from itertools import product
>>> make_pipeline([lambda: product(range(5), ["a", "b"])])
Pipeline(steps=[('<lambda>', <function <lambda> at 0x...>)], params={})

You can create pipelines with normal functions.

>>> def ab_pool(maximum=5): return product(range(maximum), ["a", "b"])
>>> def even_filter(values): return filter(lambda i: i[0] % 2 == 0, values)
>>> make_pipeline([ab_pool, even_filter])
Pipeline(steps=[('ab_pool', <function ab_pool at 0x...>),         ('even_filter', <function even_filter at 0x...>)], params={})

You can create pipelines with generators as their first elements functions.

>>> ab_pool_gen = product(range(3), ["a", "b"])
>>> pl = make_pipeline([ab_pool_gen, even_filter])
>>> pl
Pipeline(steps=[('step', <itertools.product object at 0x...>),
('even_filter', <function even_filter at 0x...>)], params={})
>>> list(pl.run())
[(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b')]

You can pass parameters into the different steps of the pl using the "params" argument:

>>> def divisor_filter(x, divisor): return filter(lambda i: i[0] % divisor == 0, x)
>>> pl = make_pipeline([ab_pool, divisor_filter],
... params = {"ab_pool": {"maximum":5}, "divisor_filter": {"divisor": 2}})
>>> pl
Pipeline(steps=[('ab_pool', <function ab_pool at 0x...>),         ('divisor_filter', <function divisor_filter at 0x...>)],         params={'ab_pool': {'maximum': 5}, 'divisor_filter': {'divisor': 2}})

You can evaluate the pipeline means calling its run method:

>>> list(pl.run())
[(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b'), (4, 'a'), (4, 'b')]

... or calling it directly:

>>> list(pl())
[(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b'), (4, 'a'), (4, 'b')]

You can update the parameters and evaluate again, giving different results:

>>> pl.params = {"ab_pool": {"maximum": 7}, "divisor_filter": {"divisor": 3}}
>>> list(pl())
[(0, 'a'), (0, 'b'), (3, 'a'), (3, 'b'), (6, 'a'), (6, 'b')]

If the pipeline needs parameters, then removing them will break the pipeline:

>>> pl.params = {}
>>> list(pl())
Traceback (most recent call last):
...
TypeError: divisor_filter() missing 1 required positional argument: 'divisor'

If multiple steps have the same inferred name, then they are given a suffix automatically, which has to be reflected in the params if used:

>>> pl = make_pipeline([ab_pool, divisor_filter, divisor_filter])
>>> pl.params = {
...     "ab_pool": {"maximum": 22},
...     "divisor_filter_0": {"divisor": 3},
...     "divisor_filter_1": {"divisor": 7}
... }
>>> list(pl())
[(0, 'a'), (0, 'b'), (21, 'a'), (21, 'b')]

You can also use "partial" functions to include Pipes with defaults in the pipeline. Because the partial function doesn't inherit the name of the original function, these steps are renamed to "step".

>>> from functools import partial
>>> pl = make_pipeline([partial(ab_pool, maximum=100)])
>>> pl
Pipeline(steps=[('step', functools.partial(<function ab_pool at 0x...>, maximum=100))],         params={})

If there are multiple steps with the same name, they get suffixes as usual:

>>> pl = make_pipeline([partial(range, stop=10), partial(divisor_filter, divisor=3)])
>>> pl
Pipeline(steps=[('step_0', functools.partial(<class 'range'>, stop=10)),         ('step_1', functools.partial(<function divisor_filter at 0x...>, divisor=3))],         params={})
Source code in autora/experimentalist/pipeline.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
def make_pipeline(
    steps: Optional[Sequence[Union[Pool, Pipe]]] = None,
    params: Optional[Dict[str, Any]] = None,
) -> Pipeline:
    """
    A factory function to make pipeline objects.

    The pipe objects' names will be set to the lowercase of their types, plus an index
    starting from 0 for non-unique names.

    Args:
        steps: a sequence of Pipe-compatible objects
        params: a dictionary of parameters passed to each Pipe by its inferred name

    Returns:
        A pipeline object

    Examples:

        You can create pipelines using purely anonymous functions:
        >>> from itertools import product
        >>> make_pipeline([lambda: product(range(5), ["a", "b"])]) # doctest: +ELLIPSIS
        Pipeline(steps=[('<lambda>', <function <lambda> at 0x...>)], params={})

        You can create pipelines with normal functions.
        >>> def ab_pool(maximum=5): return product(range(maximum), ["a", "b"])
        >>> def even_filter(values): return filter(lambda i: i[0] % 2 == 0, values)
        >>> make_pipeline([ab_pool, even_filter]) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('ab_pool', <function ab_pool at 0x...>), \
        ('even_filter', <function even_filter at 0x...>)], params={})

        You can create pipelines with generators as their first elements functions.
        >>> ab_pool_gen = product(range(3), ["a", "b"])
        >>> pl = make_pipeline([ab_pool_gen, even_filter])
        >>> pl # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('step', <itertools.product object at 0x...>),
        ('even_filter', <function even_filter at 0x...>)], params={})
        >>> list(pl.run())
        [(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b')]

        You can pass parameters into the different steps of the pl using the "params"
        argument:
        >>> def divisor_filter(x, divisor): return filter(lambda i: i[0] % divisor == 0, x)
        >>> pl = make_pipeline([ab_pool, divisor_filter],
        ... params = {"ab_pool": {"maximum":5}, "divisor_filter": {"divisor": 2}})
        >>> pl # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('ab_pool', <function ab_pool at 0x...>), \
        ('divisor_filter', <function divisor_filter at 0x...>)], \
        params={'ab_pool': {'maximum': 5}, 'divisor_filter': {'divisor': 2}})

        You can evaluate the pipeline means calling its `run` method:
        >>> list(pl.run())
        [(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b'), (4, 'a'), (4, 'b')]

        ... or calling it directly:
        >>> list(pl())
        [(0, 'a'), (0, 'b'), (2, 'a'), (2, 'b'), (4, 'a'), (4, 'b')]

        You can update the parameters and evaluate again, giving different results:
        >>> pl.params = {"ab_pool": {"maximum": 7}, "divisor_filter": {"divisor": 3}}
        >>> list(pl())
        [(0, 'a'), (0, 'b'), (3, 'a'), (3, 'b'), (6, 'a'), (6, 'b')]

        If the pipeline needs parameters, then removing them will break the pipeline:
        >>> pl.params = {}
        >>> list(pl()) # doctest: +ELLIPSIS
        Traceback (most recent call last):
        ...
        TypeError: divisor_filter() missing 1 required positional argument: 'divisor'

        If multiple steps have the same inferred name, then they are given a suffix automatically,
        which has to be reflected in the params if used:
        >>> pl = make_pipeline([ab_pool, divisor_filter, divisor_filter])
        >>> pl.params = {
        ...     "ab_pool": {"maximum": 22},
        ...     "divisor_filter_0": {"divisor": 3},
        ...     "divisor_filter_1": {"divisor": 7}
        ... }
        >>> list(pl())
        [(0, 'a'), (0, 'b'), (21, 'a'), (21, 'b')]

        You can also use "partial" functions to include Pipes with defaults in the pipeline.
        Because the `partial` function doesn't inherit the __name__ of the original function,
        these steps are renamed to "step".
        >>> from functools import partial
        >>> pl = make_pipeline([partial(ab_pool, maximum=100)])
        >>> pl # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('step', functools.partial(<function ab_pool at 0x...>, maximum=100))], \
        params={})

        If there are multiple steps with the same name, they get suffixes as usual:
        >>> pl = make_pipeline([partial(range, stop=10), partial(divisor_filter, divisor=3)])
        >>> pl # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Pipeline(steps=[('step_0', functools.partial(<class 'range'>, stop=10)), \
        ('step_1', functools.partial(<function divisor_filter at 0x...>, divisor=3))], \
        params={})



    """

    if steps is None:
        steps = []
    steps_: List[_StepType] = []
    raw_names_ = [getattr(pipe, "__name__", "step").lower() for pipe in steps]
    names_tally_ = dict([(name, raw_names_.count(name)) for name in set(raw_names_)])
    names_index_ = dict([(name, 0) for name in set(raw_names_)])

    for name, pipe in zip(raw_names_, steps):
        assert isinstance(pipe, get_args(Union[Pipe, Pool, Iterable]))

        if names_tally_[name] > 1:
            current_index_for_this_name = names_index_.get(name, 0)
            name_in_pipeline = f"{name}_{current_index_for_this_name}"
            names_index_[name] += 1
        else:
            name_in_pipeline = name

        steps_.append((name_in_pipeline, pipe))

    pipeline = Pipeline(steps_, params=params)

    return pipeline