2016-11-07 97 views
0

我想在优化框架(https://brightwaylca.org/)内运行Brightway2。从其他程序运行brightway2模型

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

Brightway2似乎是唯一合格的这种项目,但我遇到了麻烦实施。基本上,我想知道最简单的方法是什么。我有外部模型和优化算法。

到目前为止,我已经为我的Brightway2模型使用了Jupyter笔记本,但是当我将笔记本转换为python模块并在IPython的Brightway2环境中运行笔记本时,我经常会遇到错误。模块在IPython中的运行方式与Jupyter笔记本中的运行方式有什么不同?

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

有没有办法导入必要的Brightway模块而无需在Brightway2环境中运行?

感谢

这里有一个错误我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() 

但是,如果我在IPython中的BW2环境中运行它,它挂断/坠毁,机上

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的:模块' '没有属性'规格'

回答

0

您遇到的问题是multiprocessing doesn't work in the (i)python shell on Windows。笔记本通过魔法避免了这个问题。 Ecospold2DataExtractor默认使用多处理来加速提取许多Ecospold2文件。这应该是可选的;现在,您可以执行下列操作之一:

  1. 您应该只需要导入的ecoinvent 3.2次,所以这可能在A)笔记本来完成,或者b)您在调用一个单独的Python脚本命令行。
  2. 使用编写导入脚本然后导入该脚本的方法,而不是直接在python会话中导入(请参阅上面的SO链接以获取更多详细信息)。

在回答一些其他问题/疑虑:

Is there a reason the modules should run differently in IPython than in Jupyter Notebooks?

号的任何一次遇到这种情况应报告as a bug

I was thinking of using PyAutoGUI to send inputs to the Brightway2 environment and 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?

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

+0

这很有道理。 我基本上遵循你的建议,制作了项目并导入了一次数据,然后使用脚本来运行它。

There is no Brightway2 environment - just a set of python packages that can be imported. You can import them separately (though some depend on each other), e.g. bw2calc can be run independent of everything else.
Jim

+0

我还在编辑评论,并过了5分钟的窗口。 我总是运行bw2-env.bat,它允许我导入brightway2,这是我无法做到的。 – Jim

+0

“图形用户界面很难 - 欢迎您写下一个!” 哦,我不打算写GUI。你可能会知道这不在我的技能范围之内。我只是在运行bw2-env.bat后使用pyautogui发送文本来运行IPython和我的脚本。 谢谢你的帮助。 – Jim