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

Cannot change exchange amount #19

Closed
aleksandra-kim opened this issue Jan 9, 2019 · 5 comments
Closed

Cannot change exchange amount #19

aleksandra-kim opened this issue Jan 9, 2019 · 5 comments
Labels
bug Something isn't working major

Comments

@aleksandra-kim
Copy link
Member

Original report by Anonymous.


I'm using brightway2 with miniconda in PyCharm IDE on Windows OS. I am trying to change the amount of an exchange, however my change is not being executed. I get no errors, the number simply does not change.

Sample code:

oActivity = bw.Database('some_db').search("some_activity")[0]
oExchange = list(oActivity.exchanges())[1]
oExchange["amount"]

0
oExchange["amount"] = 1
oExchange["amount"]
0

@aleksandra-kim
Copy link
Member Author

Original comment by Benjamin W. Portner (Bitbucket: pommespapst, GitHub: pommespapst).


Better-formatted code:

#!python

oActivity = bw.Database('some_db').search("some_activity")[0] 
oExchange = list(oActivity.exchanges())[1] 
oExchange["amount"]
# 0
oExchange["amount"] = 1 
oExchange["amount"] 
# still 0

@aleksandra-kim
Copy link
Member Author

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


Sorry, that is frustrating! I can't reproduce this:

#!python

In [6]: db = Database("ecoinvent 3.5 cutoff")

In [7]: exc = list(db.random().exchanges())[0]

In [8]: exc['amount']
Out[8]: 0.000277

In [9]: exc['amount'] = 1

In [10]: exc['amount']
Out[10]: 1

Some things to try:

  1. Make sure your version of bw2data is up to date - latest is 3.4.4.
  2. Make sure you are getting an actual activity and exchange (not sure how this wouldn't be the case, but check anyway)
  3. If you are loading the exchange in the second half, note that your changes will only be written to the database if you call oExchange.save() after you make your modifications.

@aleksandra-kim
Copy link
Member Author

Original comment by Benjamin W. Portner (Bitbucket: pommespapst, GitHub: pommespapst).


Looks like the error occurs if the handle to the exchange is not saved:

#!python

oAct = bw.Database('ecoinvent 3.5 APOS').random()

# does not work
list(oAct.exchanges())[0]["amount"]
list(oAct.exchanges())[0]["amount"] = 3
list(oAct.exchanges())[0]["amount"]

# works
oExc = list(oAct.exchanges())[0]
oExc["amount"]
oExc["amount"] = 3
oExc["amount"]

@aleksandra-kim
Copy link
Member Author

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


There are two important details here:

  1. list(oAct.exchanges())[0]["amount"] will retrieve the exchanges from the database each time, and there is no guarantee that they are in the same order (by default database queries have no defined order unless you specify one). I am not sure if Brightway gives a default order - by guess is yes - but in general SQL queries don't unless you specify ORDER BY.
  2. list(oAct.exchanges())[0]["amount"] also won't work the second time because you are recreating the object each time. The changes you make in the first instance are not saved, and so disappear. It is the same as:
#!python

In [1]: class A:
   ...:     pass
   ...:

In [2]: a = A()

In [3]: a.foo = "bar"

In [4]: a = A()

In [5]: a.foo
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-031223489f27> in <module>()
----> 1 a.foo

AttributeError: 'A' object has no attribute 'foo'

@aleksandra-kim
Copy link
Member Author

Original comment by Benjamin W. Portner (Bitbucket: pommespapst, GitHub: pommespapst).


Thanks for the clarification!

Concerning the order of exchanges: it is changing every time indeed. Thanks for the hint!

@aleksandra-kim aleksandra-kim added major bug Something isn't working labels Mar 9, 2020
@cmutel cmutel closed this as completed Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working major
Projects
None yet
Development

No branches or pull requests

2 participants