Brightway2-analyzer

Contribution analysis

class bw2analyzer.ContributionAnalysis
annotate(sorted_data, rev_mapping)

Reverse the mapping from database ids to array indices

annotated_top_emissions(lca, names=True, **kwargs)

Get list of most damaging biosphere flows in an LCA, sorted by abs(direct impact).

Returns a list of tuples: (lca score, inventory amount, activity). If names is False, they returns the process key as the last element.

annotated_top_processes(lca, names=True, **kwargs)

Get list of most damaging processes in an LCA, sorted by abs(direct impact).

Returns a list of tuples: (lca score, supply, activity). If names is False, they returns the process key as the last element.

d3_treemap(matrix, rev_bio, rev_techno, limit=0.025, limit_type='percent')

Construct treemap input data structure for LCA result. Output like:

{
"name": "LCA result",
"children": [{
    "name": process 1,
    "children": [
        {"name": emission 1, "size": score},
        {"name": emission 2, "size": score},
        ],
    }]
}
sort_array(data, limit=25, limit_type='number', total=None)

Common sorting function for all top methods. Sorts by highest value first.

Operates in either number or percent mode. In number mode, return limit values. In percent mode, return all values >= (total * limit); where 0 < limit <= 1.

Returns 2-d numpy array of sorted values and row indices, e.g.:

ContributionAnalysis().sort_array((1., 3., 2.))

returns

(
    (3, 1),
    (2, 2),
    (1, 0)
)
Args:
  • data (numpy array): A 1-d array of values to sort.
  • limit (number, default=25): Number of values to return, or percentage cutoff.
  • limit_type (str, default=``number``): Either number or percent.
  • total (number, default=None): Optional specification of summed data total.
Returns:
2-d numpy array of values and row indices.
top_emissions(matrix, **kwargs)

Return an array of [value, index] biosphere emissions.

top_matrix(matrix, rows=5, cols=5)

Find most important (i.e. highest summed) rows and columns in a matrix, as well as the most corresponding non-zero individual elements in the top rows and columns.

Only returns matrix values which are in the top rows and columns. Element values are returned as a tuple: (row, col, row index in top rows, col index in top cols, value).

Example:

matrix = [
    [0, 0, 1, 0],
    [2, 0, 4, 0],
    [3, 0, 1, 1],
    [0, 7, 0, 1],
]

In this matrix, the row sums are (1, 6, 5, 8), and the columns sums are (5, 7, 6, 2). Therefore, the top rows are (3, 1) and the top columns are (1, 2). The result would therefore be:

(
    (
        (3, 1, 0, 0, 7),
        (3, 2, 0, 1, 1),
        (1, 2, 1, 1, 4)
    ),
    (3, 1),
    (1, 2)
)
Args:
  • matrix (array or matrix): Any Python object that supports the .sum(axis=) syntax.
  • rows (int): Number of rows to select.
  • cols (int): Number of columns to select.
Returns:
(elements, top rows, top columns)
top_processes(matrix, **kwargs)

Return an array of [value, index] technosphere processes.

Supply chain traversal

bw2analyzer.traverse_tagged_databases(functional_unit, method, label='tag', default_tag='other')

Traverse a functional unit throughout its foreground database(s), and group impacts by tag label.

Contribution analysis work by linking impacts to individual activities. However, you also might want to group impacts in other ways. For example, give individual biosphere exchanges their own grouping, or aggregate two activities together.

Consider this example system, where the letters are the tag labels, and the numbers are exchange amounts. The functional unit is one unit of the tree root.

Example tagged supply chain

In this supply chain, tags are applied to activities and biosphere exchanges. If a biosphere exchange is not tagged, it inherits the tag of its producing activity. Similarly, links to other databases are assessed with the usual LCA machinery, and the total LCA score is tagged according to its consuming activity. If an activity does not have a tag, a default tag is applied.

We can change our visualization to show the use of the default tags:

Example tagged supply chain

And then we can manually calculate the tagged impacts. Normally we would need to know the actual biosphere flows and their respective characterization factors (CF), but in this example we assume that each CF is one. Our result, group by tags, would therefore be:

  • A: \(6 + 27 = 33\)
  • B: \(30 + 44 = 74\)
  • C: \(5 + 16 + 48 = 69\)
  • D: \(14\)

This function will only traverse the foreground database, i.e. the database of the functional unit activity. A functional unit can have multiple starting nodes; in this case, all foreground databases are traversed.

Input arguments:
  • functional_unit: A functional unit dictionary, e.g. {("foo", "bar"): 42}.
  • method: A method name, e.g. ("foo", "bar")
  • label: The label of the tag classifier. Default is "tag"
  • default_tag: The tag classifier to use if none was given. Default is "other"

Returns:

Aggregated tags dictionary from aggregate_tagged_graph, and tagged supply chain graph from recurse_tagged_database.

This function uses the following help functions:

bw2analyzer.tagged.aggregate_tagged_graph(graph)

Aggregate a graph produced by recurse_tagged_database by the provided tags.

Outputs a dictionary with keys of tags and numeric values.

{'a tag': summed LCIA scores}
bw2analyzer.tagged.recurse_tagged_database(activity, amount, method_dict, lca, label, default_tag)

Traverse a foreground database and assess activities and biosphere flows by tags.

Input arguments:

  • activity: Activity tuple or object
  • amount: float
  • method_dict: Dictionary of biosphere flow tuples to CFs, e.g. {("biosphere", "foo"): 3}
  • lca: An LCA object that is already initialized, i.e. has already calculated LCI and LCIA with same method as in method_dict
  • label: string
  • default_tag: string

Returns:

{
    'activity': activity object,
    'amount': float,
    'tag': string,
    'impact': float (impact of inputs from outside foreground database),
    'biosphere': [{
        'amount': float,
        'impact': float,
        'tag': string
    }],
    'technosphere': [this data structure]
}

LCA reports

class bw2analyzer.SerializedLCAReport(activity, method, iterations=10000, cpus=None, outliers=0.025)

A complete LCA report (i.e. LCA score, Monte Carlo uncertainty analysis, contribution analysis) that can be serialized to a defined standard.

calculate()

Calculate LCA report data

get_force_directed(nodes, edges, lca)

Get graph traversal results

get_monte_carlo()

Get Monte Carlo results

upload()

Upload report data if allowed

write()

Write report data to file

PageRank algorithm

class bw2analyzer.PageRank(database)
page_rank(technosphere, alpha=0.85, max_iter=100, tol=1e-06)

Return the PageRank of the nodes in the graph.

Adapted from http://networkx.lanl.gov/svn/networkx/trunk/networkx/algorithms/link_analysis/pagerank_alg.py

PageRank computes a ranking of the nodes in the graph G based on the structure of the incoming links. It was originally designed as an algorithm to rank web pages.

The eigenvector calculation uses power iteration with a SciPy sparse matrix representation.

Args:
  • technosphere (scipy sparse matrix): The technosphere matrix.
  • alpha (float, optional): Damping parameter for PageRank, default=0.85
Returns:
  • Dictionary of nodes (activity codes) with value as PageRank

References

[1]A. Langville and C. Meyer, “A survey of eigenvector methods of web information retrieval.” http://citeseer.ist.psu.edu/713792.html
[2]Page, Lawrence; Brin, Sergey; Motwani, Rajeev and Winograd, Terry, The PageRank citation ranking: Bringing order to the Web. 1999 http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf