autora.experimentalist.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
32 33 34 35 36 37 |
|
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
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 178 179 180 181 182 183 |
|
__call__(ex=None, **params)
Successively pass the input values through the Pipe.
Source code in autora/experimentalist/pipeline.py
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 |
|
__init__(steps=None, params=None)
Initialize the pipeline with a series of Pipe objects.
Source code in autora/experimentalist/pipeline.py
107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
PipelineUnion
Bases: Pipeline
Run several Pipes in parallel and concatenate all their results.
Examples:
You can use the ParallelPipeline to parallelize a group of poolers:
>>> union_pipeline_0 = PipelineUnion([
... ("pool_1", make_pipeline([range(5)])),
... ("pool_2", make_pipeline([range(25, 30)])),
... ]
... )
>>> list(union_pipeline_0.run())
[0, 1, 2, 3, 4, 25, 26, 27, 28, 29]
>>> union_pipeline_1 = PipelineUnion([
... ("pool_1", range(5)),
... ("pool_2", range(25, 30)),
... ]
... )
>>> list(union_pipeline_1.run())
[0, 1, 2, 3, 4, 25, 26, 27, 28, 29]
You can use the ParallelPipeline to parallelize a group of pipes – each of which gets the same input.
>>> pipeline_with_embedded_union = Pipeline([
... ("pool", range(22)),
... ("filters", PipelineUnion([
... ("div_5_filter", lambda x: filter(lambda i: i % 5 == 0, x)),
... ("div_7_filter", lambda x: filter(lambda i: i % 7 == 0, x))
... ]))
... ])
>>> list(pipeline_with_embedded_union.run())
[0, 5, 10, 15, 20, 0, 7, 14, 21]
Source code in autora/experimentalist/pipeline.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 |
|
__call__(ex=None, **params)
Pass the input values in parallel through the steps.
Source code in autora/experimentalist/pipeline.py
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 |
|
Pool
Bases: Protocol
Creates an experimental sequence from scratch.
Source code in autora/experimentalist/pipeline.py
24 25 26 27 28 29 |
|
make_pipeline(steps=None, params=None, kind='serial')
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
|
kind |
Literal['serial', 'union']
|
whether the steps should run in "serial", passing data from one to the next, or in "union", where all the steps get the same data and the output is the union of all the results. |
'serial'
|
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"])]) # 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={})
It is possible to create parallel pipelines too:
>>> pl = make_pipeline([range(5), range(10,15)], kind="union")
>>> pl
PipelineUnion(steps=[('step_0', range(0, 5)), ('step_1', range(10, 15))], params={})
>>> list(pl.run())
[0, 1, 2, 3, 4, 10, 11, 12, 13, 14]
Source code in autora/experimentalist/pipeline.py
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|