Traversal
src.brightwebapp.traversal
¶
_add_branch_information_to_edges_dataframe(df)
¶
Given a dataframe of graph edges with columns consumer_unique_id and producer_unique_id
adds a column branch containing a list of
For example, for the following graph:
graph TD
0 --> 1
0 --> 2
0 --> 3
2 --> 4
3 --> 5
5 --> 6
which can be represented as a DataFrame of edges:
consumer_unique_id |
producer_unique_id |
Comment |
|---|---|---|
| 0 | 1 | # 1 is terminal producer node of this branch |
| 0 | 2 | |
| 0 | 3 | |
| 2 | 4 | # 4 is terminal producer node of this branch |
| 3 | 5 | |
| 5 | 6 | # 6 is terminal producer node of this branch |
the function returns a DataFrame of edges with an additional column branch:
consumer_unique_id |
producer_unique_id |
branch |
|---|---|---|
| 0 | 1 | [0, 1] |
| 0 | 2 | [0, 2] |
| 0 | 3 | [0, 3] |
| 2 | 4 | [0, 2, 4] |
| 3 | 5 | [0, 3, 5] |
| 5 | 6 | [0, 3, 5, 6] |
See Also
[brightwebapp.traversal._trace_branch_from_last_node][]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A dataframe of graph edges. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
A dataframe of graph nodes with a column |
Source code in src/brightwebapp/traversal.py
_edges_dict_to_dataframe(edges)
¶
Returns a Pandas DataFrame with the edges of the graph traversal. Every node in the graph traversal is represented by a row in the DataFrame.
See Also
bw_graph_tools.graph_traversal.new_node_each_visit.NewNodeEachVisitGraphTraversal
[brightwebapp.traversal._traverse_graph][]
[brightwebapp.traversal._nodes_dict_to_dataframe][]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
list
|
A list of edges in the graph traversal. |
required |
Returns:
| Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
DataFrame
|
A dataframe with the edges of the graph traversal.
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/brightwebapp/traversal.py
_nodes_dict_to_dataframe(nodes)
¶
Returns a Pandas DataFrame with human-readable descriptions and emissions values of the nodes in the graph traversal. Every node in the graph traversal is represented by a row in the DataFrame.
Warnings
By default, only the node producing the functional unit is scope 1. All other nodes are scope 3.
See Also
bw_graph_tools.graph_traversal.new_node_each_visit.NewNodeEachVisitGraphTraversal
[brightwebapp.traversal._traverse_graph][]
[brightwebapp.traversal._edges_dict_to_dataframe][]
Parameters:
Returns:
| Type | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
DataFrame
|
A dataframe with human-readable descriptions and emissions values of the nodes in the graph traversal.
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/brightwebapp/traversal.py
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 | |
_trace_branch_from_last_node(df, unique_id_last_node)
¶
Given a dataframe of graph edges with columns consumer_unique_id and producer_unique_id
and the producer_unique_id of the "final node" in a branch,
returns the branch of nodes that lead to the final node.
For example, for the following graph:
graph TD
0 --> 1
0 --> 2
0 --> 3
2 --> 4
3 --> 5
5 --> 6
which can be represented as a DataFrame of edges:
consumer_unique_id |
producer_unique_id |
Comment |
|---|---|---|
| 0 | 1 | # 1 is terminal producer node of this branch |
| 0 | 2 | |
| 0 | 3 | |
| 2 | 4 | # 4 is terminal producer node of this branch |
| 3 | 5 | |
| 5 | 6 | # 6 is terminal producer node of this branch |
For unique_id_last_node = 6, the function returns [0, 3, 5, 6].
See Also
[brightwebapp.traversal._add_branch_information_to_edges_dataframe][]
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of graph edges. Must contain integer-type columns 'consumer_unique_id' and 'producer_unique_id'. |
required |
unique_id_last_node
|
int
|
The |
required |
Returns:
| Type | Description |
|---|---|
list
|
A list of integers indicating the branch of nodes that lead to the starting node. |
Source code in src/brightwebapp/traversal.py
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 | |
_traverse_graph(lca, cutoff, biosphere_cutoff, max_calc)
¶
Conducts a graph traversal of a life-cycle assessment calculation
using the bw_graph_tools library.
Warnings
This function uses the new (v0.5) API of bw_graph_tools to perform a graph traversal:
NewNodeEachVisitGraphTraversal(lca, settings) and traverse() instead of NewNodeEachVisitGraphTraversal(lca, cutoff) and .calculate().
See Also
[brightwebapp.traversal.perform_lca][]
bw_graph_tools.graph_traversal.new_node_each_visit.NewNodeEachVisitGraphTraversal
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lca
|
LCA
|
An instance of the |
required |
cutoff
|
float
|
A float representing the cutoff threshold for the graph traversal. |
required |
biosphere_cutoff
|
float
|
A float representing the biosphere cutoff threshold for the graph traversal. |
required |
max_calc
|
int
|
An integer representing the maximum number of calculations to be performed during the graph traversal. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Source code in src/brightwebapp/traversal.py
perform_graph_traversal(cutoff, biosphere_cutoff, max_calc, return_format, lca=None, method=None, demand=None)
¶
Performs a graph traversal of a life-cycle assessment calculation and returns a DataFrame with the nodes and edges of the graph traversal.
Notes
Accepts either an lca object returned by the [brightwebapp.traversal.perform_lca][] function
or the method and demand variables.
See Also
[brightwebapp.traversal.perform_lca][]
bw_graph_tools
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cutoff
|
float
|
A float representing the cutoff threshold for the graph traversal. This is used to limit the depth of the traversal and the amount of data processed. |
required |
biosphere_cutoff
|
float
|
A float representing the biosphere cutoff threshold for the graph traversal. This is used to limit the amount of biosphere data processed. |
required |
max_calc
|
int
|
An integer representing the maximum number of calculations to be performed during the graph traversal. This is used to limit the amount of data processed and the depth of the traversal. |
required |
return_format
|
str
|
A string indicating the format of the return value.
Can be either |
required |
lca
|
LCA | None
|
An instance of the |
None
|
method
|
tuple
|
None
|
|
demand
|
dict
|
A dictionary representing the reference product demand for the life-cycle assessment calculation. The key is a For example: |
None
|
Returns:
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/brightwebapp/traversal.py
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 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
perform_lca(demand, method)
¶
Performs a life-cycle assessment calculation using the bw2calc library.
See Also
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
demand
|
dict
|
A dictionary representing the reference product demand for the life-cycle assessment calculation. The key is a For example: |
required |
method
|
tuple
|
required |
Warnings
The demand dictionary must contain exactly one activity.
Returns:
| Type | Description |
|---|---|
LCA
|
An instance of the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |