从python调用openlca函数的python包。
olca-ipc的Python项目详细描述
openlca提供了一个implementation 基于JSON-RPC的协议 进程间通信(IPC)。这样,就可以调用函数 在openlca中并在openlca之外处理它们的结果。那olca-ipc 包提供了一个方便的api,用于使用来自标准 python(cpython v3.6+),因此可以使用openlca作为数据存储 以及计算引擎,并将其与python中的库相结合 生态系统(努比、熊猫和朋友)。
OpenLCA IPC协议基于OpenLCA数据交换格式,即 在olca-schema中指定 储存库。olca-ipc包提供了 openlca数据交换格式和与openlca通信的api IPC服务器通过这些类的实例。
当前稳定版本的olca-ipc可通过 Python Package Index。因此,为了 使用它,您只需使用pip:
pip install -U olca-ipc
为了与openlca通信,首先需要启动openlca ipc 服务器。您可以通过openlca中的用户界面在 Window > Developer Tools > IPC Server。IPC服务器在特定的 端口,例如8080,从IPC客户端连接到该端口:
importolcaclient=olca.Client(8080)
因此,olca.Client类的实例是 调用openlca函数并处理其结果。以下 示例显示了一些典型的用例(注意,这些只是示例 没有输入检查、错误处理、代码结构和所有 通常会这样)。
创建并链接数据
olca包包含一个带有 olca-schema使用的模型 用于与openlca交换数据。使用类型注释,您应该能够 编辑器支持(类型检查和智能感知)。您可以创建、更新 和OpenLCA模式中定义的链接数据模型(例如 processes, flows,或 product systems)。 (注意,我们转换诸如属性的calculationType和 当calculation_type 生成python api)。
olca.Client类提供了get,find,insert等方法,
update,和delete来处理数据。下面的示例演示如何
创建一个新的流并将其链接到一个现有的流属性,名为
importolcaimportuuidclient=olca.Client(8080)# find the flow property 'Mass' from the databasemass=client.find(olca.FlowProperty,'Mass')# create a flow that has 'Mass' as reference flow propertysteel=olca.Flow()steel.id=str(uuid.uuid4())steel.flow_type=olca.FlowType.PRODUCT_FLOWsteel.name="Steel"steel.description="Added from the olca-ipc python API..."# in openLCA, conversion factors between different# properties/quantities of a flow are stored in# FlowPropertyFactor objects. Every flow needs at# least one flow property factor for its reference# flow property.mass_factor=olca.FlowPropertyFactor()mass_factor.conversion_factor=1.0mass_factor.flow_property=massmass_factor.reference_flow_property=Truesteel.flow_properties=[mass_factor]# save it in openLCA, you may have to refresh# (close & reopen the database to see the new flow)client.insert(steel)
运行计算
OpenLCA提供不同类型的计算,可以通过 calculation_type在 calculation setup。 在下面的示例中,使用产品系统和影响的计算设置 评估方法被创建、计算并最终导出到excel中:
importolcaclient=olca.Client(8080)# create the calculation setupsetup=olca.CalculationSetup()# define the calculation type here# see http://greendelta.github.io/olca-schema/html/CalculationType.htmlsetup.calculation_type=olca.CalculationType.CONTRIBUTION_ANALYSIS# select the product system and LCIA methodsetup.impact_method=client.find(olca.ImpactMethod,'TRACI 2.1')setup.product_system=client.find(olca.ProductSystem,'compost plant, open')# 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 systemsetup.amount=1.0# calculate the result and export it to an Excel fileresult=client.calculate(setup)client.excel_export(result,'result.xlsx')# the result remains accessible (for exports etc.) until# you dispose it, which you should always do when you do# not need it anymoreclient.dispose(result)
参数化计算设置
为了计算具有不同参数集的产品系统,可以 使用计算设置直接将一组参数重新定义传递到 计算。这样,您就不需要修改产品系统或 数据库中的参数,以便用不同的参数计算 值:
# ... same steps as abovesetup=olca.CalculationSetup()# ...forsomethinginyour.parameter_data:redef=olca.ParameterRedef()redef.name=the_parameter_nameredef.value=the_parameter_value# redef.context = ... you can also redefine process and LCIA method# parameters by providing a parameter context which# is a Ref (reference) to the respective process or# LCIA method; with no context a global parameter is# redefinedsetup.parameter_redefs.append(redef)
如名称所述,参数重新定义重新定义现有的值。 全局、进程或LCIA方法参数。
蒙特卡罗模拟
运行蒙特卡罗模拟与正常计算类似,但是 在calculate中,调用将返回引用的simulator方法 然后用于运行计算的模拟器(在每个计算中 模拟器为中的不确定度分布生成新值。 系统)。您可以获得每次迭代的结果,还可以导出 以后所有迭代都将显示为excel。对于正常计算的结果, 当模拟器不再使用时,应进行处理:
importolcaclient=olca.Client(8080)# creating the calculation setupsetup=olca.CalculationSetup()setup.calculation_type=olca.CalculationType.MONTE_CARLO_SIMULATIONsetup.impact_method=client.find(olca.ImpactMethod,'TRACI 2.1')setup.product_system=client.find(olca.ProductSystem,'compost plant')setup.amount=1.0# create the simulatorsimulator=client.simulator(setup)foriinrange(0,10):result=client.next_simulation(simulator)first_impact=result.impact_results[0]print('iteration %i: result for %s = %4.4f'%(i,first_impact.impact_category.name,first_impact.value))# we do not have to dispose the result here (it is not cached# in openLCA); but we need to dispose the simulator later (see below)# export the complete result of all simulationsclient.excel_export(simulator,'simulation_result.xlsx')# the result remains accessible (for exports etc.) until# you dispose it, which you should always do when you do# not need it anymoreclient.dispose(simulator)
有关更多信息和示例,请参见 package documentation