+3 votes
1.0k views
How I can change Uncertainty value like "Normal distribution" mean, standard deviation by python code.
in openLCA by (230 points)

1 Answer

+2 votes
by (1.5k points)
selected by
 
Best answer
If you use the ipc server it's pretty straight forward.  Here I update the first exchange in my process called test to normal distribution with a mean of 3 and standard deviation of .25.

import olca
client = olca.Client(8080)

ref = client.find(olca.Process,'test')
process = client.get(olca.Process, ref.id)
process.exchanges[0].uncertainty.distribution_type = olca.UncertaintyType('NORMAL_DISTRIBUTION')
process.exchanges[0].uncertainty.mean = 3
process.exchanges[0].uncertainty.sd = 0.25

client.update(process)
by (1.5k points)
Also, you can use vars() function to see what attributes are available to you and their current state.  In my example:
vars(process.exchanges[0].uncertainty)

returns:
{'id': None,
 'olca_type': None,
 'distribution_type': 'NORMAL_DISTRIBUTION',
 'mean': 3,
 'mean_formula': None,
 'geom_mean': 1.0,
 'geom_mean_formula': None,
 'minimum': None,
 'minimum_formula': None,
 'sd': 0.25,
 'sd_formula': None,
 'geom_sd': None,
 'geom_sd_formula': None,
 'mode': None,
 'mode_formula': None,
 'maximum': None,
 'maximum_formula': None}
by (230 points)
First, Thank you for your help.
I did the modifications, but it doesn't work.
Here is part of the code I have

def updateProcess(id,List_flow):
    pro = olca.getProcess(id)
    #exs = pro.getExchanges()
    exs = pro.exchanges
    for ex in exs:
        k=0
        for j in List_flow:  
            if ex.flow.name == List_flow[k][0] and ex.flow.category.name==List_flow[k][2]:                
                ex.amount = float(List_flow[k][1])
             # the modifications here:
               log.info("{} uncertainty", ex.uncertainty)
               
                log.info("{} uncertainty", ex.UncertaintyType)
               
                break
            k=k+1
    olca.updateProcess(pro)
    log.info("{} has been updated",pro.name)
    return
by (1.5k points)
https://ask.openlca.org/1689/python-script-in-developer-tools-no-longer-working-in-v1-8-0
https://ask.openlca.org/1688/how-i-can-use-getexchanges-in-openlca-1-8

I think the syntax you are using is deprecated. My example uses the olca-ipc.py library.  I found it very easy to use, but you may need to refactor your code to make it work.

https://github.com/GreenDelta/olca-ipc.py
by (230 points)
Hi
I try to add these commands, but it does not work.
import olca
client = olca.Client(8080)
by (1.5k points)
Yes, I would not expect it to.  If you are using jython to access the java classes directly you have to use the correct syntax.  For 1.8, it's here:
https://github.com/GreenDelta/olca-modules/blob/master/olca-core/src/main/java/org/openlca/core/model/Uncertainty.java

My example uses the ipc server which I shared the link with.
by (230 points)
Thank you for your help.
This the procedure which I want to add two statements (Mean&SD), but it does not work.

def updateProcess(id,List_flow):  
    pro = olca.getProcess(id)
    #exs = pro.getExchanges()
    exs = pro.exchanges
    for ex in exs:
        k=0
        for j in List_flow:  
            if ex.flow.name == List_flow[k][0] and ex.flow.category.name==List_flow[k][2]:                
                ex.amount = float(List_flow[k][1])             
                log.info("{} uncertainty", ex.uncertainty)

                #here the problem
                ex.uncertainty.mean=3
                break
            k=k+1
    olca.updateProcess(pro)
    log.info("{} has been updated",pro.name)

    return
I hope you can help me.
...