[英]Running brightway2 models from other programs
I want to run Brightway2 within an optimization framework ( https://brightwaylca.org/ ). 我想在优化框架( https://brightwaylca.org/ )中运行Brightway2。
Basically, I want to create a Python script that sends an input file to an external model (also in Python) and gets the outputs. 基本上,我想创建一个Python脚本,该脚本将输入文件发送到外部模型(也在Python中)并获取输出。 The script will then write activity data into a Brightway database, and then run Brightway2 to get an LCA score.
然后,脚本将活动数据写入Brightway数据库,然后运行Brightway2以获取LCA分数。 This score will then be used to update the input file based on an optimization algorithm.
然后,该分数将用于基于优化算法更新输入文件。
Brightway2 seems uniquely qualified for this sort of project, but I'm having trouble implementing. Brightway2似乎可以胜任此类项目,但我在实施时遇到了麻烦。 Basically, I wanted to know what the easiest way to do this was.
基本上,我想知道最简单的方法是什么。 I have the external model and the optimization algorithms.
我有外部模型和优化算法。
So far, I have used Jupyter Notebooks for my Brightway2 models, but I often get errors when I convert the notebooks to python modules and run them in the Brightway2 environment in IPython. 到目前为止,我已经为我的Brightway2模型使用了Jupyter Notebooks,但是当我将笔记本转换为python模块并在IPython的Brightway2环境中运行它们时,经常会出错。 Is there a reason the modules should run differently in IPython than in Jupyter Notebooks?
这些模块在IPython中的运行方式与在Jupyter Notebooks中的运行方式不同是否有原因吗?
I was thinking of using PyAutoGUI to send inputs to the Brightway2 environment and IPython. 我当时在考虑使用PyAutoGUI将输入发送到Brightway2环境和IPython。 Is there an easier/better way to do that?
有没有更简单/更好的方法呢?
Is there a way to import the necessary Brightway modules without running in the Brightway2 environment? 有没有一种方法可以导入必需的Brightway模块而无需在Brightway2环境中运行?
Thanks 谢谢
Here's an example of an error I get with IPython, but not with Jupyter notes. 这是我使用IPython遇到的错误的示例,但没有使用Jupyter笔记发生的错误。 When I run the following code in Jupyter notes it runs fine.
当我在Jupyter笔记中运行以下代码时,它运行正常。
from brightway2 import *
def main():
project_name = "Algae_LCA"
projects.set_current(project_name)
bw2setup()
methods.load()
#Set directory for Ecoinvent v3.2 datasets and name the database.
data_directory = "E:\GOOGLE~1\ECOINV~1\ECOINV~1.2-C\datasets"
database_name = "Ecoinvent_v3.2-Conseq"
#Import the database, apply cleaning strategies, and provide statistics
ei = SingleOutputEcospold2Importer(data_directory, database_name)
ei.apply_strategies()
ei.statistics()
But if I run it in IPython in the bw2 environment, it hangs up/crashed on 但是如果我在bw2环境中的IPython中运行它,它会挂起/崩溃
ei = SingleOutputEcospold2Importer(data_directory, database_name)
It gives me the following error: 它给了我以下错误:
-------------------------------------------------------------
AttributeError Traceback (most rec
C:\bw2-python\Algae LCA\BW2_Project_Database_Setup_Test.py in
36
37 if __name__ == "__main__":
---> 38 main()
39
C:\bw2-python\Algae LCA\BW2_Project_Database_Setup_Test.py in
25 #Import the database, apply cleaning strategies,
26
---> 27 ei = SingleOutputEcospold2Importer(data_directory
28 #ei.apply_strategies()
29 #ei.statistics()
C:\bw2-python\envs\bw2\lib\site-packages\bw2io\importers\ecos
47
48 start = time()
---> 49 self.data = Ecospold2DataExtractor.extract(di
50 print(u"Extracted {} datasets in {:.2f} secon
51 len(self.data), time() - start))
C:\bw2-python\envs\bw2\lib\site-packages\bw2io\extractors\eco
77
78 if use_mp:
---> 79 with multiprocessing.Pool(processes=multi
80 print("Extracting XML data from {} da
81 results = [
C:\bw2-python\envs\bw2\lib\multiprocessing\context.py in Pool
116 from .pool import Pool
117 return Pool(processes, initializer, initargs,
--> 118 context=self.get_context())
119
120 def RawValue(self, typecode_or_type, *args):
C:\bw2-python\envs\bw2\lib\multiprocessing\pool.py in __init_
166 self._processes = processes
167 self._pool = []
--> 168 self._repopulate_pool()
169
170 self._worker_handler = threading.Thread(
C:\bw2-python\envs\bw2\lib\multiprocessing\pool.py in _repopu
231 w.name = w.name.replace('Process', 'PoolW
232 w.daemon = True
--> 233 w.start()
234 util.debug('added worker')
235
C:\bw2-python\envs\bw2\lib\multiprocessing\process.py in star
103 'daemonic processes are not allowed to
104 _cleanup()
--> 105 self._popen = self._Popen(self)
106 self._sentinel = self._popen.sentinel
107 _children.add(self)
C:\bw2-python\envs\bw2\lib\multiprocessing\context.py in _Pop
311 def _Popen(process_obj):
312 from .popen_spawn_win32 import Popen
--> 313 return Popen(process_obj)
314
315 class SpawnContext(BaseContext):
C:\bw2-python\envs\bw2\lib\multiprocessing\popen_spawn_win32.
32
33 def __init__(self, process_obj):
---> 34 prep_data = spawn.get_preparation_data(proces
35
36 # read end of pipe will be "stolen" by the ch
C:\bw2-python\envs\bw2\lib\multiprocessing\spawn.py in get_pr
171 # or through direct execution (or to leave it alo
172 main_module = sys.modules['__main__']
--> 173 main_mod_name = getattr(main_module.__spec__, "na
174 if main_mod_name is not None:
175 d['init_main_from_name'] = main_mod_name
AttributeError: module ' main ' has no attribute ' spec ' AttributeError:模块' main '没有属性' spec '
The problem you are running into is that multiprocessing doesn't work in the (i)python shell on Windows . 您遇到的问题是Windows的(i)python shell中无法进行多重处理 。 The notebook avoids this problem through, basically, magic.
笔记本基本上可以通过魔术来避免此问题。 The
Ecospold2DataExtractor
uses multiprocessing by default to speed up the extraction of many Ecospold2 files. 默认情况下,
Ecospold2DataExtractor
使用多重处理来加快许多Ecospold2文件的提取速度。 This should probably be made optional; 这可能应该设为可选; for now, you can do one of the following:
目前,您可以执行以下任一操作:
In response to some other questions/concerns: 针对其他一些问题/担忧:
Is there a reason the modules should run differently in IPython than in Jupyter Notebooks?
这些模块在IPython中的运行方式与在Jupyter Notebooks中的运行方式不同是否有原因吗?
No. Any time this happens should be reported as a bug . 否。任何时候发生的情况都应报告为错误 。
I was thinking of using PyAutoGUI to send inputs to the Brightway2 environment and IPython.
我当时在考虑使用PyAutoGUI将输入发送到Brightway2环境和IPython。 Is there an easier/better way to do that?
有没有更简单/更好的方法呢?
GUIs are hard - you are welcome to write one! GUI很难-欢迎编写一个!
Is there a way to import the necessary Brightway modules without running in the Brightway2 environment?
有没有一种方法可以导入必需的Brightway模块而无需在Brightway2环境中运行?
There is no Brightway2 environment
- just a set of python packages that can be imported. 没有
Brightway2 environment
-只有一组可以导入的python软件包。 You can import them separately (though some depend on each other), eg bw2calc
can be run independent of everything else. 您可以分别导入它们(尽管有些相互依赖),例如
bw2calc
可以独立于其他所有文件运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.