sgn.base
¶
Base classes for building a graph of elements and pads.
Frame
dataclass
¶
Generic class to hold the basic unit of data that flows through a graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
EOS
|
bool
|
bool, default False, Whether this frame indicates end of stream (EOS) |
False
|
is_gap
|
bool
|
bool, default False, Whether this frame is marked as a gap |
False
|
metadata
|
dict
|
dict, optional, Metadata associated with this frame. |
dict()
|
Source code in sgn/base.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
InternalPad
dataclass
¶
Bases: UniqueID
, _InternalPadLike
A pad that sits inside an element and is called between sink and source pads. Internal pads are connected in the elements internal graph according to the below (data flows top to bottom)
snk1 ... snkN (if exist) \ ... // internal (always exists) // ... \ src1 ... srcM (if exist)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad |
required |
name
|
str
|
str, optional, The unique name for this object |
''
|
Source code in sgn/base.py
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 |
|
__call__()
async
¶
When called, an internal pad receives a Frame from the element that the pad belongs to.
Source code in sgn/base.py
338 339 340 341 |
|
SinkElement
dataclass
¶
Bases: ElementLike
Sink element represents a terminal node in a pipeline, that typically writes data to disk, etc. Sink_pads must exist but not source_pads.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str, optional, The unique name for this object |
''
|
sink_pad_names
|
Sequence[str]
|
list, optional, Set the list of sink pad names. These need to be unique for
an element but not for an application. The resulting full names will be
made with " |
list()
|
Source code in sgn/base.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
|
at_eos
property
¶
If frames on any sink pads are End of Stream (EOS), then mark this whole element as EOS.
Returns:
Type | Description |
---|---|
bool
|
bool, True if any sink pad is at EOS, False otherwise |
__post_init__()
¶
Establish the sink pads and graph attributes.
Source code in sgn/base.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
mark_eos(pad)
¶
Marks a sink pad as receiving the End of Stream (EOS). The EOS marker signals that no more frames will be received on this pad.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad
|
SinkPad
|
SinkPad, The sink pad that is receiving the |
required |
Source code in sgn/base.py
570 571 572 573 574 575 576 577 578 |
|
pull(pad, frame)
¶
Pull for a SinkElement represents the action of associating a frame with a particular input source pad a frame. This function must be provided by the subclass, and is where any "final" behavior must occur, e.g. writing to disk, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad
|
SinkPad
|
SinkPad, The sink pad that is receiving the frame |
required |
frame
|
Frame
|
Frame, The frame that is being received |
required |
Source code in sgn/base.py
580 581 582 583 584 585 586 587 588 589 590 591 592 |
|
SinkPad
dataclass
¶
Bases: UniqueID
, _SinkPadLike
A pad that receives a data Frame when called. When linked, it returns a dictionary suitable for building a graph in graphlib.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad, takes two arguments, the pad and the frame |
required |
name
|
str
|
str, optional, The unique name for this object |
''
|
Source code in sgn/base.py
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 307 308 309 310 311 312 313 |
|
__call__()
async
¶
When called, a sink pad gets a Frame from the linked source pad and then calls the element's provided call function.
Notes
Pad Call Order: pads must be called in the correct order such that the upstream sources have new information by the time call is invoked. This should be done within a directed acyclic graph such as those provided by the apps.Pipeline class.
Source code in sgn/base.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
link(other)
¶
Returns a dictionary of dependencies suitable for adding to a graphlib graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
SourcePad
|
SourcePad, The source pad to link to this sink pad |
required |
Notes
Many-to-one (source, sink) Not Supported: Only sink pads can be linked. A sink pad can be linked to only one source pad, but multiple sink pads may link to the same source pad.
Returns:
Type | Description |
---|---|
dict[Pad, set[Pad]]
|
dict[SinkPad, set[SourcePad]], a dictionary of dependencies suitable for |
dict[Pad, set[Pad]]
|
adding to a graphlib graph |
Source code in sgn/base.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
SourceElement
dataclass
¶
Bases: ElementLike
Initialize with a list of source pads. Every source pad is added to the graph with no dependencies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str, optional, The unique name for this object |
''
|
source_pad_names
|
Sequence[str]
|
list, optional, Set the list of source pad names. These need to be unique
for an element but not for an application. The resulting full names will be
made with " |
list()
|
Source code in sgn/base.py
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 |
|
__post_init__()
¶
Establish the source pads and graph attributes.
Source code in sgn/base.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
new(pad)
¶
New frames are created on "pad". Must be provided by subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad
|
SourcePad
|
SourcePad, The source pad through which the frame is passed |
required |
Returns:
Type | Description |
---|---|
Frame
|
Frame, The new frame to be passed through the source pad |
Source code in sgn/base.py
430 431 432 433 434 435 436 437 438 439 440 |
|
SourcePad
dataclass
¶
Bases: UniqueID
, _SourcePadLike
A pad that provides a data Frame when called.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element
|
Element
|
Element, The Element instance associated with this pad |
required |
call
|
Callable
|
Callable, The function that will be called during graph execution for this pad |
required |
name
|
str
|
str, optional, The unique name for this object |
''
|
Source code in sgn/base.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
__call__()
async
¶
When called, a source pad receives a Frame from the element that the pad belongs to.
Source code in sgn/base.py
251 252 253 254 255 256 257 |
|
TransformElement
dataclass
¶
Bases: ElementLike
Both "source_pads" and "sink_pads" must exist. All sink pads depend on all source pads in a transform element. If you don't want that to be true, write more than one transform element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str, optional, The unique name for this object |
''
|
source_pad_names
|
Sequence[str]
|
list, optional, Set the list of source pad names. These need to be unique
for an element but not for an application. The resulting full names will
be made with " |
list()
|
sink_pad_names
|
Sequence[str]
|
list, optional, Set the list of sink pad names. These need to be unique
for an element but not for an application. The resulting full names will
be made with " |
list()
|
Source code in sgn/base.py
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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
|
__post_init__()
¶
Establish the source pads and sink pads and graph attributes.
Source code in sgn/base.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
new(pad)
¶
New frames are created on "pad". Must be provided by subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad
|
SourcePad
|
SourcePad, The source pad through which the frame is passed |
required |
Returns:
Type | Description |
---|---|
Frame
|
Frame, The new frame to be passed through the source pad |
Source code in sgn/base.py
509 510 511 512 513 514 515 516 517 518 519 |
|
pull(pad, frame)
¶
Pull data from the input pads (source pads of upstream elements), must be implemented by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad
|
SinkPad
|
SinkPad, The sink pad that is receiving the frame |
required |
frame
|
Frame
|
Frame, The frame that is pulled from the source pad |
required |
Source code in sgn/base.py
497 498 499 500 501 502 503 504 505 506 507 |
|
UniqueID
dataclass
¶
Bases: _PostInitBase
Generic class from which all classes that participate in an execution graph should be derived. Enforces a unique name and hashes based on that name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str, optional, The unique name for this object, defaults to the objects unique uuid4 hex string if not specified |
''
|
Source code in sgn/base.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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
__eq__(other)
¶
Check if two objects are equal based on their unique id and types.
Source code in sgn/base.py
135 136 137 |
|
__hash__()
¶
Compute the hash of the object based on the unique id.
Notes
Motivation: we need the Base class to be hashable, so that it can be used as a key in a dictionary, but mutable dataclasses are not hashable by default, so we have to define our own hash function here. Stability: As currently implemented, the hash of a UniqueID object will not be stable across python sessions, and should therefore not be used for checksum purposes.
Returns:
Type | Description |
---|---|
int
|
int, hash of the object |
Source code in sgn/base.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
__post_init__()
¶
Handle setup of the UniqueID class, including the ._id
attribute.
Source code in sgn/base.py
108 109 110 111 112 113 114 |
|
_PostInitBase
¶
Mixin class used to resolve issues with the builtin object class with post_init and using multiple inheritance.
See https://stackoverflow.com/a/59987363/23231262.
Source code in sgn/base.py
82 83 84 85 86 87 88 89 90 91 |
|
__post_init__()
¶
Intercept the post_init calls so they aren't relayed to object
Source code in sgn/base.py
89 90 91 |
|
get_sgn_logger(name, levels=SGN_LOG_LEVELS)
¶
Utility function for constructing a logger with a given name and log level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str, The name of the logger |
required |
levels
|
Dict[str, int]
|
Dict[str, int], A dictionary of log levels to choose from |
SGN_LOG_LEVELS
|
Returns:
Type | Description |
---|---|
Logger
|
logging.Logger, The logger with the specified name and log level |
Source code in sgn/base.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|