GreenDelta / olca-ipc.py

Mozilla Public License 2.0
29 stars 17 forks source link

What is the equivalent to Build complete flow supply chain in IPC? #31

Closed dt-woods closed 1 month ago

dt-woods commented 8 months ago

I am attempting to generate Product systems using the IPC server and Python. The command, client.create_product_system takes two arguments: reference object to process and the linking configuration. I set the linking configuration settings and run the IPC command, which generates a reference object to the new product system, which is also generated in openLCA's database. I query the product system and look at the processes list and processLinks list and find only the reference process listed for processes and an empty list for the processLinks.

>>> import olca_ipc as ipc 
>>> import olca_schema as o
>>> client = ipc.Client(8080)
>>> pid = 'b7b80030-6ce5-3b80-b91d-ae199f4a2c2d'
>>> p_obj = client.get(o.Process, pid)
>>> p_obj.to_ref()
Ref(
  id='b7b80030-6ce5-3b80-b91d-ae199f4a2c2d', 
  category='22: Utilities/2211: Electric Power Generation, Transmission and Distribution', 
  description=None, 
  flow_type=<FlowType.PRODUCT_FLOW: 'PRODUCT_FLOW'>, 
  location='Associated Electric Cooperative, Inc.', 
  name='Electricity; at user; consumption mix - Associated Electric Cooperative, Inc. - BA', 
  process_type=<ProcessType.UNIT_PROCESS: 'UNIT_PROCESS'>, 
  ref_unit=None, 
  ref_type=<RefType.Process: 'Process'>)
>>> lc_gen = o.LinkingConfig(
...     prefer_unit_processes=False,
...     provider_linking=o.ProviderLinking.ONLY_DEFAULTS)
>>> ps_ref = client.create_product_system(
...     process=p_obj.to_ref(),
...     config=lc_gen)
>>> ps_obj = client.get(o.ProductSystem, ps_ref.id)
>>> len(ps_obj.processes)
1
>>> len(ps_obj.process_links)
0

I don't see where the link configuration is doing anything. If I stop the IPC service and open the product system, the model graph is plain (see below).

model_graph_1

If I right-click on the input flow, I get a context menu with the option to "Build flow supply chain" with two options: Complete and Build next tier. The options under "Complete" look quite similar to the options I set in the LinkConfiguration.

model_graph_2

If I click the option I set in the IPC, the processes and process_links lists are updated. Of course, I have to click "Save" in openLCA first. I'm not sure what is running when I do this.

model_graph_3

Why this doesn't work in IPC?

Is there something I need to do to force this completion, a method I missed, or must I write my own methods to build these lists?

Thanks for your time and assistance.

msrocka commented 7 months ago

Which openLCA and olca-ipc version and which database do you use? I tested it with LCA Commons /electricity baseline and this process:

import olca_ipc as ipc
import olca_schema as o

def main():
    client = ipc.Client(8080)
    process_ref = client.get_descriptor(
        o.Process, "0082cc36-0aa6-3ff2-acf5-a2765411b57a"
    )
    sys_ref = client.create_product_system(
        process_ref, o.LinkingConfig(
            provider_linking=o.ProviderLinking.ONLY_DEFAULTS
        )
    )
    system = client.get(o.ProductSystem, sys_ref.id)
    print(f"created system {system.id}")
    print(f" with {len(system.processes)} processes")
    print(f" and {len(system.process_links)} process links")

if __name__ == "__main__":
    main()

It works as expected:

created system eced4bde-0d77-42d0-8a68-abf88deb3c23
 with 174 processes
 and 342 process links
dt-woods commented 7 months ago

I'll double check version numbers and get back to you.

I think the errors creep in when you try to edit something existing. What happens when you modify the product system you created and try to update it (e.g., using the put command)?

On Wed, Feb 28, 2024, 06:51 Michael Srocka @.***> wrote:

Which openLCA and olca-ipc version and which database do you use? I tested it with LCA Commons /electricity baseline https://www.lcacommons.gov/lca-collaboration/search/page=1&repositoryId=Federal_LCA_Commons%2FUS_electricity_baseline and this process https://www.lcacommons.gov/lca-collaboration/Federal_LCA_Commons/US_electricity_baseline/dataset/PROCESS/0082cc36-0aa6-3ff2-acf5-a2765411b57a :

import olca_ipc as ipcimport olca_schema as o

def main(): client = ipc.Client(8080) process_ref = client.get_descriptor( o.Process, "0082cc36-0aa6-3ff2-acf5-a2765411b57a" ) sys_ref = client.create_product_system( process_ref, o.LinkingConfig( provider_linking=o.ProviderLinking.ONLY_DEFAULTS ) ) system = client.get(o.ProductSystem, sys_ref.id) print(f"created system {system.id}") print(f" with {len(system.processes)} processes") print(f" and {len(system.process_links)} process links")

if name == "main": main()

It works as expected:

created system eced4bde-0d77-42d0-8a68-abf88deb3c23 with 174 processes and 342 process links

— Reply to this email directly, view it on GitHub https://github.com/GreenDelta/olca-ipc.py/issues/31#issuecomment-1968819567, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHCFB5I6BDIGAIN44JE2LYLYV4K5RAVCNFSM6AAAAABCLGH4EGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRYHAYTSNJWG4 . You are receiving this because you authored the thread.Message ID: @.***>

dt-woods commented 7 months ago

I'm running:

I'm not reading from a database; rather, I'm trying to build one.

msrocka commented 7 months ago

openLCA 2.1.0 ; olca-ipc 2.0.2

It should work then. The requirements are that the processes need to be all in the database before calling create_product_system and for the linking option ONLY_DEFAULTS they must have the default providers set in the processes. When this is the case, I cannot reproduce this.

msrocka commented 7 months ago

When you are building the database via IPC, you may need to check the default providers in the processes; they are only linked when they are already present in the database, so the import order is important then.

dt-woods commented 7 months ago

Thanks. My order of operations matches yours: first build all the processes with default providers, then build the product systems.

Are there any common data errors you've encountered that prevent product systems from being created?

dt-woods commented 7 months ago

I found the error in my code and I got product systems working again. Thanks for your assistance!