Skip to content

Instantly share code, notes, and snippets.

@tngTUDOR
Last active April 22, 2024 13:04
Show Gist options
  • Save tngTUDOR/66a83ee6a7670518f123491b8c062e0b to your computer and use it in GitHub Desktop.
Save tngTUDOR/66a83ee6a7670518f123491b8c062e0b to your computer and use it in GitHub Desktop.
bwanalyzer recursive calculation object to graph.
import bw2data as bd
import bw2analyzer as bwa
import networkx as nx
# Make sure to set the right project.
A_PROJECT = "STeamPunk"
bd.projects.set_current(A_PROJECT)
# Where does the LCA fu come from?
FG_DB = "vent_eco"
a = bd.Database(FG_DB).random()
m_key = ('EF v3.1', 'climate change', 'global warming potential (GWP100)')
df = bwa.utils.recursive_calculation_to_object(a, m_key, as_dataframe=True)
def ca_graph(df, graph=None):
data_keys = ["score", "fraction", "amount", "name", "key"]
if graph is None:
graph = nx.DiGraph()
for _, row in df.iterrows():
node_data = {k:row[k] for k in data_keys}
graph.add_node(row['label'], **node_data)
if not pd.isna(row['parent']):
graph.add_edge(row['parent'], row['label'])
return graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment