0 votes
934 views

Hello everyone,

I am trying to calculate a product system with parameters from a csv-File and found the code on https://github.com/GreenDelta/openlca-python- tutorial/commit/26cd97acbeb946702e57084a6d99e2d47b8c0d7b .

So far - nothing is really working and I am trying to circle in on the mistakes I have made so far. (I have no programming experience with python and close to no experience with eclipse IDE etc.)

What I have done so far (All steps from the Tutorial "Setting up an Integrated Development Environment, https://github.com/GreenDelta/openlca-python-tutorial/blob/master/ide_setup.md)

- Install Java (sucessfull)
- Install Jython (sucessfull)
- Install Eclipse
- Install PyDev
- Configure Interpreter with jython.jar file 
- Link project to OpenLCA .jar files (PYTHONPATH)

- Start IPC-Server in OpenLCA

But so far, a lot of errors appear in the code, the first ones are "unresolved import" for the following code:

import olca
import pandas
import arrow as ar

It seems to me, that I need to install the olca and pandas library, but I have not figured out how to to this.
In case the answer is somewhere in here: https://github.com/GreenDelta/olca-ipc.py, i have not yet found/understood it.


The other erros are:

  • Undefined variable: export_and_dispose
  • Expected:)
  • Unused variable: rows
  • Undefined variable: method_name
  • Undefined variable: parameters
  • Unused parameter: setup

I would be very thankfull for any advice!

Best regards,

Dominik

in openLCA by (310 points)

1 Answer

0 votes
by (125k points)
Well this is of course not easy but you could post your code that is not working, and see if you get replies?
by (310 points)
Hello Andeas,

thank you for your response. I simply copied the code from the tutorial, found under  https://github.com/GreenDelta/openlca-python- tutorial/commit/26cd97acbeb946702e57084a6d99e2d47b8c0d7b
into eclipse.
(The Link in the original-post above does not work because of a "space" that I have accidentally written into the URL)

The most important question for me right now is, do I need to install olca and pandas for it to work and how do I do this.

(Maybe the tutorial was written by someone, who had these things already in place and forgot, that it is necessary)

Looking forward to any replies!

Best regards,

Dominik
by (310 points)
### Calculate a product system with many different parameter values that are read from a csv file, and store the results in Excel
This script uses the IPC server of openLCA for connecting via Python to openLCA.

It calculates all product systems existing in the selected database with an LCIA method
from this database that is identified by name. The calculations are done for all parameter sets in the csv file.

To use it, adapt the LCIA method name and the paths to the csv file and to the Excel file. Create the csv file with UUID of parameter, name of parameter, then the parameter values in different columns, one per set. You can also extend the number of parameters sets (in the script and, correspondingly, in the csv file). Then, start the IPC server in openLCA with port 8080. Execute the script in an external Python IDE such as PyCharm or similar.

```python
import csv
import os
import sys
import olca
import pandas
import arrow as ar
def main():
    # the Excel files with the results are written to the `output` folder
    if not os.path.exists('output'):
        os.makedirs('output')
    start = ar.now()
    # make sure that you started an IPC server with the specific database in
    # openLCA (Window > Developer Tools > IPC Server)
    client = olca.Client(8080)
    # first we read the parameter sets; they are stored in a data frame where
    # each column is a different parameter set
    # 1st column: parameter UUID
    # 2nd column: parameter name
    # last column: process name, for documentation
    parameters = read_parameters(
        'relative/path/to/csvfile.csv')
    # we prepare a calculation setup for the given LCIA method and reuse it
    # for the different product systems in the database
    calculation_setup = prepare_setup(client, 'The Name of the LCIA method')
    # we run a calculation for each combination of parameter set and product
    # system that is in the database
    for system in client.get_descriptors(olca.ProductSystem):
        print('Run calculations for product system %s (%s)' %
              (system.name, system.id))
        calculation_setup.product_system = system
        for parameter_set in range(0, parameters.shape[1]):
            set_parameters(calculation_setup, parameters, parameter_set)
            try:
                calc_start = ar.now()
                print('  . run calculation for parameter set %i' % parameter_set)
                result = client.calculate(calculation_setup)
                print('  . calculation finished in', ar.now() - calc_start)
                # we store the Excel file under
                # `output/<system id>_<parameter set>.xlsx`
                excel_file = 'output/%s_%d.xlsx' % (system.id, parameter_set)
                export_and_dispose(client, result, excel_file)
            except Exception as e:
                print('  . calculation failed: %s' % e)
    print('All done; total runtime', ar.now() - start)
def read_parameters(file_path: 'file\path') -> pandas.DataFrame:
    """ Read the given parameter table into a pandas data frame where the
        parameter names are mapped to the index.
        assumption: not more than 5 sets - if there are more, the row index and the csv file can be changed
    """
    index = []
    data = []
    with open(file_path, 'r', encoding='cp1252') as stream:
        reader = csv.reader(stream, delimiter=';')
        rows = []
        for row in reader:
            index.append(row[1])
            data.append([float(x) for x in row[2:7]])
        return pandas.DataFrame(data=data, index=index)
def prepare_setup(client: olca.Client, method_name: str) -> olca.CalculationSetup:
    """ Prepare the calculation setup with the LCIA method with the given name.
        Note that this is just an example. You can of course get a method by
        ID, calculate a system with all LCIA methods in the database etc.
    """
    method = client.find(olca.ImpactMethod, method_name)
    if method is None:
        sys.exit('Could not find LCIA method %s' % method_name)
    setup = olca.CalculationSetup()
    # currently, simple calculation, contribution analysis, and upstream
    # analysis are supported
    setup.calculation_type = olca.CalculationType.CONTRIBUTION_ANALYSIS
    setup.impact_method = method
    # amount is the amount of the functional unit (fu) of the system that
    # should be used in the calculation; unit, flow property, etc. of the fu
    # can be also defined; by default openLCA will take the settings of the
    # reference flow of the product system
    setup.amount = 1.0
    return setup
def set_parameters(setup: olca.CalculationSetup, parameters: pandas.DataFrame,
                   parameter_set: int):
    """ Set the parameters of the given parameter set (which is the
        corresponding column in the data frame) to the calculation setup.
    """
    # for each parameter in the parameter set we add a parameter
    # redefinition in the calculation setup which will set the parameter
    # value for the respective parameter just for the calculation (without
    # needing to modify the database)
    setup.parameter_redefs = []
    for param in parameters.index:
        redef = olca.ParameterRedef()
        redef.name = param
        redef.value = parameters.ix[param, parameter_set]
        setup.parameter_redefs.append(redef)
def export_and_dispose(client: olca.Client, result: olca.SimpleResult, path: str):
    """ Export the given result to Excel and dispose it after the Export
        finished.
    """
    try:
        print('  . export result to', path)
        start = ar.now()
        client.excel_export(result, path)
        time = ar.now() - start
        print('  . export finished after', time)
        print('  . dispose result')
        client.dispose(result)
        print('  . done')
    except Exception as e:
        print('ERROR: Excel export or dispose of %s failed' % path)
if __name__ == '__main__':
    main()
```

The csv file can look as follows, one line per parameter:

484895ce-a443-4cc4-8864-a10a407e93aa;para1;0.014652146;0.014718301;0.017931926;0.020983646;0.020427708;;;processname 1
8fc4c8a5-8e98-46b7-9a21-d1d8481e5aa4;para2;0.030556766;0.034429313;0.044245529;0.047132534;0.049762465;;;processname 2
66a0200f-ce81-494c-8131-1aee0c0a59f2;NP3;0.016611061;0.010960006;0.005260319;0.002222134;0.00099458;;;processname 3
410ed118-4201-47d3-88bd-da6af3bfd555;NP4;0;0.00017588;0.000994777;0.001486167;0.001806687;;;processname 4
247b42d0-9715-465f-9500-61fc51bbfe84;NP4;0.015608129;0.018301197;0.017860159;0.016049407;0.015421637;;;processname 5
f90551c7-3c92-474f-9d10-03562ed8c8ae;NP5;0.008727944;0.010233887;0.009987262;0.008974703;0.008623659;;;processname 6
19063103-a7b5-4f61-9a3a-c3ec44133c67;NP6;0.005293286;0.006206604;0.006057032;0.005442939;0.00523004;;;processname 7
by (310 points)
Hi everyone,

I have solved the issue by installing Anaconda und running the code in Jupyter Notebooks.

The olca package can be installed in the Anaconda prompt by just typing: pip install -U olca-ipc

One line of code has caused issues: In the method  set_parameters I had to replace the "ix" by "loc".

I have saved the CSV file in the Folder of the Notebook and for the relative path you just type: './Name_of_CSV_File.csv' where the "." represents the current directory (the Folder of the Notebook).

That and the LCIA-Method name are the only lines of code you have to adjust. If you want to calculate more variations of one parameter then 5 you have to also adjust the line "data.append([float(x) for x in row[2:7]])"

The code will create a new folder in the work directory called "output" where the csv files will be stored.

Best regards,

Dominik
by (125k points)
Thank you for the update, for sharing your finding, and happy to hear that it is working for you.
...