Search code examples
brightway

how to use the redo lci method in brightway25


I'm trying to use the redo_lci() method as I was told it would speed up my code. I keep getting an error that I dont understand.

I'm using the latest bw25 modules

i followed this old notebook

I can just do lci() then lcia() then score, and everything works fine, only when i try to do it like the above i face problems.

what am i doing wrong here?

methods = [bd.methods.random() for _ in range(10)]
activities = [db.random() for _ in range(3)]
def reuse_characterization_matrices():
    lca = bc.LCA({activities[0]: 1}, method=bd.methods.random())
    lca.lci()
    lca.lcia()
    
    methods_list = []
    for method in methods:
        lca.switch_method(method)
        methods_list.append(lca.characterization_matrix.copy())
    
    for act in activities:
        lca.redo_lci({act: 1})
        for matrix in methods_list:
            score = (matrix * lca.inventory).sum()
reuse_characterization_matrices()
KeyError                                  Traceback (most recent call last)
/home/Desktop/code/test_redo.ipynb Cell 9 line 1
----> 1 reuse_characterization_matrices()

/home/Desktop/code/test_redo.ipynb Cell 9 line 1
      9     methods_list.append(lca.characterization_matrix.copy())
     11 for act in activities:
---> 12     lca.redo_lci({act: 1})
     13     for matrix in methods_list:
     14         score = (matrix * lca.inventory).sum()

File ~/anaconda3/envs/lcdna/lib/python3.11/site-packages/bw2calc/lca.py:620, in LCA.redo_lci(self, demand)
    609 """Redo LCI with same databases but different demand.
    610 
    611 Args:
   (...)
    617 
    618 """
    619 warnings.warn('Please use .lci(demand=demand) instead of `redo_lci`.', DeprecationWarning)
--> 620 self.lci(demand=demand)

File ~/anaconda3/envs/lcdna/lib/python3.11/site-packages/bw2calc/lca.py:402, in LCA.lci(self, demand, factorize)
    400     self.load_lci_data()
    401 if demand is not None:
...
    604 for key in demand:
    605     if key not in self.dicts.product and not isinstance(key, int):
--> 606         raise KeyError(f"Key '{key}' not in product dictionary; make sure to pass the integer id, not a key like `('foo', 'bar')` or an `Actiivity` or `Node` object.")

KeyError: "Key ''electricity production, solar thermal parabolic trough, 50 MW' (kilowatt hour, US-WECC, None)' not in product dictionary; make sure to pass the integer id, not a key like `('foo', 'bar')` or an `Actiivity` or `Node` object.

Solution

  • Instead of:

    lca.redo_lci({act: 1})
    

    Do this:

    lca.redo_lci({act.id: 1})
    

    This is explained in the error message text:

    make sure to pass the integer id, not a key like `('foo', 'bar')` 
    or an `Actiivity` or `Node` object.