0 votes
181 views

Hi,

We're trying to get the Contribution Tree results into Excel. As they are not exported, I was thinking that I could perhaps access them with python and save them in a format that I can use.

But it seems that IPC only has methods for the SimpleResults, even when I use CalculationType.UPSTREAM_ANALYSIS.

Is there any way to get the contribution tree, either through export or through a script?

Thanks!

in openLCA by (420 points)

3 Answers

0 votes
by (420 points)
 
Best answer

I found a way to get the UpstreamTree. Don't ask me what changed, but now it works:

system = dao.getForName(prod_system)[0]
solver = JuliaSolver()
m_cache = MatrixCache.createLazy(db)
calculator = SystemCalculator(m_cachesolver)
setup = CalculationSetup(CalculationType.UPSTREAM_ANALYSIS, system)
impactMethod = method_dao.getForName('My_Method')[0]
setup.impactMethod = Descriptors.toDescriptor(impactMethod)
result = calculator.calculateFull(setup)
for cat in impactMethod.impactCategories:
      tree = result.getTree(Descriptors.toDescriptor(cat))
0 votes
by (420 points)

Using python inside openLCA 1.10, this works for the full result. But then it fails when I try to get the UpstreamTree. Any ideas why?

from java.io import File
from org.openlca.core.database.derby import DerbyDatabase
from org.openlca.core.database import ProductSystemDao, EntityCache, ImpactMethodDao, ImpactCategoryDao
from org.openlca.core.matrix.cache import MatrixCache
from org.openlca.eigen import NativeLibrary
from org.openlca.julia import JuliaSolver
from org.openlca.core.math import CalculationSetup, SystemCalculator, CalculationType
 
if __name__ == '__main__':
    # load the product system
    db_dir = File('C:/xxxxxxxxxxx')
    db = DerbyDatabase(db_dir)
    dao = ProductSystemDao(db)
    system = dao.getForName('my product system')[0]
    
    # caches, native lib., solver
    m_cache = MatrixCache.createLazy(db)
    e_cache = EntityCache.create(db)
    NativeLibrary.loadFromDir(File('../native'))
    solver = JuliaSolver()
    
    # calculation    
    setup = CalculationSetup(CalculationType.UPSTREAM_ANALYSIS, system)
 
    # impact method
    impact_dao = ImpactMethodDao(db)
    impactMethod = impact_dao.getForName('my impact method')[0]   
    
    # get the right impact category from the method and dao
    impactCategories = impactMethod.impactCategories
    impactCategory = None
    for ic in impactCategories:
      if ic.referenceUnit == "kg CO2 eq":
        impactCategory = ic
    # get the descriptor
    ic_dao = ImpactCategoryDao(db)
    icDesc = ic_dao.getDescriptor(impactCategory.id)
    
    # run calculation
    calculator = SystemCalculator(m_cache, solver)
    result = calculator.calculateFull(setup)
 
    # this one fails with a java.lang.NullPointerException:
    # File "<string>", line 84, in <module>
    # at org.openlca.core.results.FullResult.getTree(FullResult.java:238)
    tree = result.getTree(icDesc)
by (420 points)
Thank you for the reply. This is what I thought with IPC, that's why I tried executing the above script directly inside openLCA to have access to SystemCalculator. Are you saying that even with the above script accessing the "native" openLCA methods, I don't have access to the FullResult?
0 votes
by (350 points)

To whom it may be useful:

I've created a script that will automatically expand the contribution tree above a specified threshold after which the data can easily be copied and pasted into any other program.

You can find the script here https://ask.openlca.org/4499/how-to-export-contribution-tree-data

...