Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find_graph_dependents method of Database results not reproducible #214

Open
tngTUDOR opened this issue Dec 6, 2024 · 0 comments
Open

find_graph_dependents method of Database results not reproducible #214

tngTUDOR opened this issue Dec 6, 2024 · 0 comments

Comments

@tngTUDOR
Copy link
Contributor

tngTUDOR commented Dec 6, 2024

Current

In a scenario context with:

  • multiple databases ni a project, for example a background database and a foreground database,
  • the foreground database has inputs from the background database

calling the find_graph_dependents method of the foreground database does not provide the right depencies, unless calling process method.

I have observed this when creating databases as follows:

  • create foreground database -> db
  • register the database -> db.register()
  • create & save new nodes -> db.new_node(...).save()
  • create background database -> db_bg
  • register the database -> db_bg.register()
  • create & save new nodes -> db_bg.new_node(...).save()
  • create & save edges from foreground db to background db

using the following deps:

  • bw2data 4.3
  • bwcalc 2.0
  • bw2io 0.9.3

Expected

There is a difference in writing a full database with the write() method and adding nodes .
Is calling the "process" method of the database object a requirement ? Or maybe there is somewhere in the "save" method of activities and exchanges that this is being overlooked ?

# ---
# jupyter:
#   jupytext:
#     formats: py:percent
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.16.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# %%
import bw2data as bd
import bw2calc as bc
import bw2io as bi

# %%
print(bd.__version__)
print(bc.__version__)
print(bi.__version__)

# (4, 3)
# 2.0
# 0.9.3

# %%
bi.remote.install_project("ecoinvent-3.10-biosphere", "bk-project", overwrite_existing=True)

# %%
bd.projects.set_current("bk-project")

# %%
bd.databases

# %%
db = bd.Database('bike')
db.register()

# %%
bd.databases

# %%
# First activity: the bike
data = {
    'code': 'bike',
    'name': 'bike production',
    'location': 'DK',
    'unit': 'bike'
}
bike = db.new_node(**data)
bike.save()


# %%
bike.id 

# 123372841738444800


# %%
db_bg = bd.Database('materials')
db_bg.register()

# %%
# First input
data = {
    'code': 'ng',
    'name': 'natural gas production',
    'location': 'NO',
    'unit': 'MJ'
}

ng = db_bg.new_node(**data)
ng.save()

# Second input
data = {
    'code': 'cf',
    'name': 'carbon fibre production',
    'location': 'DE',
    'unit': 'kg'
}

cf = db_bg.new_node(**data)
cf.save()

# %%
bd.databases

# %%
# We add the exchange to the bike activity, from the carbon fiber prod

bike.new_edge(
    amount=2.5, 
    type='technosphere',
    input=cf
).save()

# %%
# Verify by printing the exchanges of the bike
a_bike = db.get('bike')
for e in a_bike.exchanges():
    print(e)

# %%
# Add another exchange to the carbon fiber production
cf.new_edge(
    amount=237.3,  # plus 58 kWh of electricity, in ecoinvent 3.8 
    uncertainty_type=5, 
    minimum=200, 
    maximum=300, 
    type='technosphere',
    input=ng,
).save()


# %%
# dependent dbs
db.find_graph_dependents()

# {'bike'}

# %%
db.metadata['depends']

# [ ]

# %%
db.process()

# %%
db.find_graph_dependents()

# {'bike', 'materials'}


# %%
db.metadata['depends']

# ['materials']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant