lineapy
Graph
¶
Bases: object
Source code in lineapy/data/graph.py
14 15 16 17 18 19 20 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 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
__init__(nodes, session_context)
¶
Graph is the core abstraction in LineaPy that is automatically generated by capturing and analyzing user code. Nodes in Graph correspond to variables and function calls from user code, and edges indicate dependencies. This is the common IR upon which all LineaPy applications, such as code cleanup and DAG generation, are built.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
List[Node]
|
list of LineaPy Nodes that make up the graph. |
required |
session_context |
SessionContext
|
the session context associated with the graph |
required |
Note
The information in session_context
is semantically important to
the notion of a Graph. Concretely, we are starting to also use the code
entry from the session_context.
Source code in lineapy/data/graph.py
15 16 17 18 19 20 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 |
|
get_subgraph(nodes)
¶
Get a subgraph of the current graph induced by the input nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
List[Node]
|
The nodes in the subgraph |
required |
Returns:
Type | Description |
---|---|
Graph
|
A new |
Source code in lineapy/data/graph.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
get_subgraph_from_id(nodeids)
¶
Get subgraph from list of LineaID
Source code in lineapy/data/graph.py
215 216 217 218 219 220 221 222 223 224 |
|
visit_order()
¶
Just using the line number as the tie-breaker for now since we don't have a good way to track dependencies. Note that we cannot just use the line number to sort because there are nodes created by us that do not have line numbers.
Source code in lineapy/data/graph.py
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 |
|
SessionType
¶
Bases: Enum
Session types allow the tracer to know what to expect - JUPYTER: the tracer need to progressively add more nodes to the graph - SCRIPT: the easiest case, run everything until the end
Source code in lineapy/data/types.py
13 14 15 16 17 18 19 20 21 |
|
Tracer
dataclass
¶
Source code in lineapy/instrumentation/tracer.py
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 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 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 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 520 521 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 |
|
values: Dict[str, object]
property
¶
Returns a mapping of variable names to their values, by joining the scoping information with the executor values.
__post_init__(session_type, session_name, globals_)
¶
Tracer is internal to Linea and it implements the "hidden APIs"
that are setup by the transformer.
It performs the following key functionalities:
- Creates the graph nodes and inserts into the database.
- Maintains data structures to help creating the graph IR
that is used later, which includes:
- variable_name_to_id
: for tracking variable/function/module
to the ID responsible for its creation
- Executes the program, using the Executor
.
Note that we don't currently maintain the variable names in the persisted
graph (we used to at some point in the past), but we can add a serialized
version of variable_name_to_id
to the session if we want to persist
the information. Which could be useful for e.g., post-hoc lifting of
linea artifacts.
Source code in lineapy/instrumentation/tracer.py
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 |
|
assign(variable_name, value_node, from_import=False)
¶
Assign updates a local mapping of variable nodes.
Source code in lineapy/instrumentation/tracer.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
call(function_node, source_location, *arguments, **keyword_arguments)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function_node |
Node
|
the function node to call/execute |
required |
source_location |
Optional[SourceLocation]
|
the source info from user code |
required |
arguments |
Union[Node, Tuple[bool, Node]]
|
positional arguments. These are passed as either Nodes (named nodes, constants, etc)
or tuples (starred, the node) where the starred is a boolean to indicate whether
the argument is supposed to be splatted before passing to the function (This is
the case where you might call a function like so |
()
|
keyword_arguments |
Node
|
keyword arguments. These are passed as a dictionary of keyword arguments to the
function. Similar to |
{}
|
Returns:
Type | Description |
---|---|
CallNode
|
a call node |
Note
- It's important for the call to return the call node so that we can programmatically chain the the nodes together, e.g., for the assignment call to modify the previous call node.
- The call looks up if it's a locally defined function. We decided that this is better for program slicing.
Source code in lineapy/instrumentation/tracer.py
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 484 485 486 487 488 |
|
import_module(name, source_location=None)
¶
Import a module. If we have already imported it, just return its ID. Otherwise, create new module nodes for each submodule in its parents and return it.
Source code in lineapy/instrumentation/tracer.py
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 |
|
lookup_node(variable_name, source_location=None)
¶
Cases for the node that we are looking up:
- user defined variable & function definitions
- imported libs
-
unknown runtime magic functions — special case to LookupNode
-
builtin functions, e.g., min
- custom runtime, e.g., get_ipython
Source code in lineapy/instrumentation/tracer.py
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 |
|
process_node(node)
¶
Execute a node, and adds it to the database.
Source code in lineapy/instrumentation/tracer.py
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 184 185 |
|
trace_import(name, source_location=None, alias=None, attributes=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the name of the module |
required |
alias |
Optional[str]
|
the module could be aliased, e.g., import pandas as pd |
None
|
attributes |
Optional[Dict[str, str]]
|
a list of functions imported from the library. It keys the aliased name to the original name. |
None
|
Note
- The input args would either have alias or attributes, but not both
- Didn't call the function import because I think that's a protected name
note that version and path will be introspected at runtime
Source code in lineapy/instrumentation/tracer.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 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 390 391 392 393 |
|
ValueType
¶
Bases: Enum
Lower case because the API with the frontend assume the characters "chart" exactly as is.
Source code in lineapy/data/types.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
artifact_store()
¶
Returns:
Type | Description |
---|---|
LineaArtifactStore
|
An object of the class |
Source code in lineapy/api/api.py
364 365 366 367 368 369 370 371 372 373 374 |
|
delete(artifact_name, version)
¶
Deletes an artifact from artifact store. If no other artifacts refer to the value, the value is also deleted from both the value node store and the pickle store.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_name |
str
|
Key used to while saving the artifact. |
required |
version |
Union[int, str]
|
Version number or "latest" or "all". |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If artifact not found or version invalid. |
Source code in lineapy/api/api.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
|
get(artifact_name, version=None)
¶
Gets an artifact from the DB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_name |
str
|
Name of the artifact. Note that if you do not remember the artifact, you can use the artifact_store to browse the options. |
required |
version |
Optional[int]
|
Version of the artifact. If |
None
|
Returns:
Type | Description |
---|---|
LineaArtifact
|
Returned value offers methods to access information we have stored about the artifact. |
Source code in lineapy/api/api.py
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 |
|
get_function(artifacts, input_parameters=[], reuse_pre_computed_artifacts=[])
¶
Extract the process that creates selected artifacts as a python function
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifacts |
List[Union[str, Tuple[str, int]]]
|
List of artifact names (with optional version) to be included in the function return. |
required |
input_parameters |
List[str]
|
List of variable names to be used in the function arguments. Currently, only accept variable from literal assignment; such as a='123'. There should be only one literal assignment for each variable within all artifact calculation code. For instance, if both a='123' and a='abc' are existing in the code, we cannot specify a as input variables since it is confusing to specify which literal assignment we want to replace. |
[]
|
reuse_pre_computed_artifacts |
List[Union[str, Tuple[str, int]]]
|
List of artifacts(name with optional version) for which we will use pre-computed values from the artifact store instead of recomputing from original code. |
[]
|
Returns:
Type | Description |
---|---|
Callable
|
A python function that takes input_parameters as args and returns a dictionary with each artifact name as the dictionary key and artifact value as the value. |
Note that:
- If an input parameter is only used to calculate artifacts in the
reuse_pre_computed_artifacts
list, that input parameter will be passed around as a dummy variable. LineaPy will create a warning. - If an artifact name has been saved multiple times within a session,
multiple sessions or mutated. You might want to specify version
number in
artifacts
orreuse_pre_computed_artifacts
. The best practice to avoid searching artifact version is don't reuse artifact name in different notebooks and don't save same artifact multiple times within the same session.
Source code in lineapy/api/api.py
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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
get_module_definition(artifacts, input_parameters=[], reuse_pre_computed_artifacts=[])
¶
Create a python module that includes the definition of get_function()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifacts |
List[Union[str, Tuple[str, int]]]
|
Same as in |
required |
input_parameters |
List[str]
|
Same as in |
[]
|
reuse_pre_computed_artifacts |
List[Union[str, Tuple[str, int]]]
|
Same as in |
[]
|
Returns:
Type | Description |
---|---|
str
|
A python module that includes the definition of |
Source code in lineapy/api/api.py
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 |
|
reload()
¶
Reloads lineapy context.
Note
Currently only reloads annotations but in the future can be a container for other items like configs, etc.
Source code in lineapy/api/api.py
351 352 353 354 355 356 357 358 359 360 361 |
|
save(reference, name, storage_backend=None, **kwargs)
¶
Publishes the object to the LineaPy DB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference |
object
|
The reference could be a variable name, in which case LineaPy will save
the value of the variable, with out default serialization mechanism.
Alternatively, it could be a "side effect" reference, which currently includes either |
required |
name |
str
|
The name is used for later retrieving the artifact and creating new versions if an artifact of the name has been created before. |
required |
storage_backend |
Optional[ARTIFACT_STORAGE_BACKEND]
|
The storage backend used to save the artifact. Currently support
lineapy and mlflow (for mlflow supported model flavors). In case of
mlflow, lineapy will use |
None
|
**kwargs |
Keyword arguments passed into underlying storage mechanism to overwrite
default behavior. For |
{}
|
Returns:
Type | Description |
---|---|
LineaArtifact
|
Returned value offers methods to access
information we have stored about the artifact (value, version),
and other automation capabilities, such as |
Source code in lineapy/api/api.py
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 184 185 186 187 188 189 |
|
to_pipeline(artifacts, framework='SCRIPT', pipeline_name=None, dependencies={}, output_dir='.', input_parameters=[], reuse_pre_computed_artifacts=[], generate_test=False, pipeline_dag_config={}, include_non_slice_as_comment=False)
¶
Writes the pipeline job to a path on disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifacts |
List[str]
|
Names of artifacts to be included in the pipeline. |
required |
framework |
str
|
Name of the framework to be used. Defined by enum PipelineTypes in lineapy/data/types.py. Defaults to "SCRIPT" if not specified. |
'SCRIPT'
|
pipeline_name |
Optional[str]
|
Name of the pipeline. |
None
|
dependencies |
TaskGraphEdge
|
Task dependencies in graphlib format, e.g., |
{}
|
output_dir |
str
|
Directory path to save DAG and other pipeline files. |
'.'
|
input_parameters |
List[str]
|
Names of variables to be used as parameters in the pipeline.
Currently, it only accepts variables from literal assignment
such as |
[]
|
reuse_pre_computed_artifacts |
List[str]
|
Names of artifacts in the pipeline for which pre-computed value is to be used (rather than recomputing the value). |
[]
|
generate_test |
bool
|
Whether to generate scaffold/template for pipeline testing.
Defaults to |
False
|
pipeline_dag_config |
Optional[Dict]
|
A dictionary of parameters to configure DAG file to be generated. Not applicable for "SCRIPT" framework as it does not generate a separate DAG file. For "AIRFLOW" framework, Airflow-native config params such as "retries" and "schedule_interval" can be passed in. For "ARGO" framework, Argo-native config params such as "namespace" and "service_account_name". |
{}
|
Returns:
Type | Description |
---|---|
Path
|
Directory path where DAG and other pipeline files are saved. |
Source code in lineapy/api/api.py
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 |
|
visualize(*, live=False)
¶
Display a visualization of the Linea graph from this session using Graphviz.
If live=True
, then this visualization will live update after cell execution.
Note that this comes with a substantial performance penalty, so it is False
by default.
Note
If the visualization is not live, it will print out the visualization
as of the previous cell execution, not the one where visualize
is executed.
Source code in lineapy/editors/ipython.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|