0

我想在优化框架内运行 Brightway2 ( https://brightwaylca.org/ ) 中运行 Brightway2。

基本上,我想创建一个 Python 脚本,将输入文件发送到外部模型(也在 Python 中)并获取输出。然后该脚本会将活动数据写入 Brightway 数据库,然后运行 ​​Brightway2 以获得 LCA 分数。然后,该分数将用于根据优化算法更新输入文件。

Brightway2 似乎特别适合此类项目,但我在实施时遇到了麻烦。基本上,我想知道最简单的方法是什么。我有外部模型和优化算法。

到目前为止,我已经将 Jupyter Notebooks 用于我的 Brightway2 模型,但是当我将笔记本转换为 python 模块并在 IPython 的 Brightway2 环境中运行它们时,我经常会出错。模块在 IPython 中的运行方式与在 Jupyter Notebooks 中的运行方式不同吗?

我正在考虑使用 PyAutoGUI 将输入发送到 Brightway2 环境和 IPython。有没有更简单/更好的方法来做到这一点?

有没有办法在不运行 Brightway2 环境的情况下导入必要的 Brightway 模块?

谢谢

这是我在 IPython 中遇到的错误示例,但在 Jupyter 注释中没有。当我在 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()

但是如果我在 bw2 环境中的 IPython 中运行它,它会挂起/崩溃

 ei = SingleOutputEcospold2Importer(data_directory, database_name)

它给了我以下错误:

-------------------------------------------------------------
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: 模块“ main ”没有属性“ spec

4

1 回答 1

0

您遇到的问题是多处理在 Windows 上的 (i)python shell 中不起作用。笔记本基本上通过魔术避免了这个问题。默认情况下Ecospold2DataExtractor使用多处理来加速许多 Ecospold2 文件的提取。这可能应该是可选的;现在,您可以执行以下操作之一:

  1. 您应该只需要导入一次 ecoinvent 3.2,因此可以在 a) 笔记本或 b) 在命令行上调用的单独 python 脚本中完成。
  2. 使用编写导入脚本然后导入它的技巧,而不是直接在 python 会话中进行导入(有关更多详细信息,请参见上面的 SO 链接)。

针对其他一些问题/疑虑:

模块在 IPython 中的运行方式与在 Jupyter Notebooks 中的运行方式不同吗?

不,任何时候发生这种情况都应该报告为错误

我正在考虑使用 PyAutoGUI 将输入发送到 Brightway2 环境和 IPython。有没有更简单/更好的方法来做到这一点?

GUI 很难——欢迎你写一个!

有没有办法在不运行 Brightway2 环境的情况下导入必要的 Brightway 模块?

没有Brightway2 environment——只有一组可以导入的 python 包。您可以单独导入它们(尽管有些相互依赖),例如bw2calc可以独立于其他所有内容运行。

于 2016-11-08T10:21:54.463 回答