GreenDelta / olca-ipc.py

Mozilla Public License 2.0
29 stars 17 forks source link

AttributeError when using olca.ipc.Client.get on OpenLCA alpha build #22

Closed jacobtriestocode closed 1 month ago

jacobtriestocode commented 2 years ago

An error occurs when using Client.get(olca.Process, process.id) on some processes and doesn't occur for other processes. This error occurs in the latest alpha version of OpenLCA (2.0.0.alpha4). When the same database (before upgrading for the new version of OpenLCA) is used in the stable version (1.11), it doesn't. This error can be replicated in at least one of the free databases from openLCA Nexus by using the example code that I have included below. The Nexus database is the ELCD database and can be downloaded from here:

https://nexus.openlca.org/database/ELCD

A process from another database (used internally) has also been attached and can be imported into OpenLCA to demonstrate a process where the error does not occur in the 2.0.0.alpha4 build. The example code below tries to access a process from ELCD and the importable process, getting the AttributeError each time when using Client.get() on the ELCD process and not getting the error for the imported process (you might need to comment out the error throwing code if testing outside of a Jupyter notebook):

#%%
import olca

# Before this code runs, you need to open OpenLCA and start the IPC server. Set the port to 8080.
# Then, open the ELCD database

client = olca.Client(8080)

# This process will return an error after using client.get
pName = 'Pine log with bark, production mix entry to saw mill, at plant, refostered managed forest, 44% water content'

# This should work fine
pList = client.find(olca.Process, pName)

# Print just to verify 'id' is a field in the JSON
print('Process properties:\n')
print(pList)

# The code will fail at this step. The AttributeError will say there is no "get" property for the client "string"
proc = client.get(olca.Process, pList.id)

# This code will also fail, despite referencing ID directly
pID = '8fd15266-4930-4a33-b9c2-e8698923132d'
proc = client.get(olca.Process, pID)

# Using the name argument also fails
proc = client.get(olca.Process, name=pName)

# %%
# The following code can be run after importing the process attached to the Github issue.
# This process was exported from a project database that we have internal to our team.
# client.get seems to work properly on it, for both the stable and alpha OpenLCA programs.
pName = 'Corn Grain_1'

pList = client.find(olca.Process, pName)

# Verify 'id' is a field in the JSON
print('Process properties:\n')
print(pList)

# This code should work
proc = client.get(olca.Process, pList.id)

# %%
# Close out IPC before code file ends
client.close()

The 'Corn Grain_1' process can be imported with this OpenLCA format zip

CropLifeAmerica.zip

msrocka commented 2 years ago

Many thanks for reporting this error. The reason for this is that the underlying JSON format for categories changed now in openLCA 2 (the example works because it has no category). We need to update the IPC model for this which is generated from the JSON schema. As this is a breaking change for the Python class model, we need to do this in a different branch / version. For now, the only workaround is to switch to an older dev-version of openLCA 2 in the archive directory of the distribution folder. But we will update this very soon...

vhasik commented 2 years ago

For now, I found the last version where this wasn't an issue was the 2022-02-18 alpha release.

jacobtriestocode commented 2 years ago

Thank you guys for the response, I'll watch for updates to the IPC code.