Cannot change exchange amount

Issue #19 new
Former user created an issue

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

Comments (5)

  1. Pommes Papst

    Better-formatted code:

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

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

    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.
  3. Pommes Papst

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

    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"]
    
  4. Chris Mutel repo owner

    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:
    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'
    
  5. Log in to comment