Brightway2:如何通过名称检索特定活动

时间:2018-09-05 22:53:00

标签: python brightway

这是我的新手,请耐心等待。

我已经初始化了新的BW2安装并添加了Ecoinvent数据库(为清晰起见,对某些输出进行了修剪):

>>> from brightway2 import bw2setup, Database, SingleOutputEcospold2Importer
>>> bw2setup()
>>> ei = SingleOutputEcospold2Importer('/path/to/datasets', 'ei3.4 cutoff')
>>> ei.apply_strategies()
>>> ei.statistics()
14889 datasets
520205 exchanges
0 unlinked exchanges
>>> ei.write_database()

到目前为止,效果很好(尤其要注意已加载的数据集数量)。但是现在-我该如何使用它?具体来说,我想检索具有market for transport, freight, lorry, unspecified空间范围的名为GLO的过程。但我不知道此活动的关键,而且令人惊讶的是,它没有出现在搜索中

>>> Database('ei3.4 cutoff').search('market for transport, freight, lorry, unspecified')
[]
>>> Database('ei3.4 cutoff').search('market for transport')
[]

!非常令人惊讶,没有运输市场吗?通过检查,我发现有139个活动以Ecoinvent 3.4截止日期中的market for transport开头。

实际上,尽管EI 3.4截止中有3,966个“市场”过程,但我的Brightway安装只知道大约两个:

>>> Database('ei3.4 cutoff').search('market')
['market for paris market carrot' (kilogram, GLO, None),
 'paris market carrot production' (kilogram, GLO, None),
 'market for acetonitrile' (kilogram, GLO, None),
 'market for sulfur' (kilogram, GLO, None),
 'market for whey' (kilogram, GLO, None),
 'market for heptane' (kilogram, GLO, None),
 'market for straw' (kilogram, GLO, None),
 'market for clay' (kilogram, CH, None),
 'market for pitch' (kilogram, CH, None),
 'market for brass' (kilogram, CH, None),
 'market for platinum' (kilogram, GLO, None),
 'market for polycarbonate' (kilogram, GLO, None),
 'market for pitch' (kilogram, RoW, None),
 'market for tetrafluoroethylene' (kilogram, GLO, None),
 'market for dimethenamide' (kilogram, GLO, None),
 'market for glyphosate' (kilogram, GLO, None),
 'market for styrene' (kilogram, GLO, None),
 'market for ferrite' (kilogram, GLO, None),
 'market for folpet' (kilogram, GLO, None),
 'market for magnetite' (kilogram, GLO, None),
 'market for metamitron' (kilogram, GLO, None),
 'market for nylon 6-6' (kilogram, GLO, None),
 'market for atrazine' (kilogram, GLO, None),
 'market for magnesium' (kilogram, GLO, None),
 'market for metaldehyde' (kilogram, GLO, None)]

如何查找未在搜索中显示的数据集?相反,该文档似乎严格地使用random()来使用market for transport, freight, lorry, unspecified [GLO]来检索活动(例如:http://nbviewer.jupyter.org/urls/bitbucket.org/cmutel/brightway2/raw/default/notebooks/Databases.ipynb),这无助于我回答这个问题。

所以-两个问题-

  1. 我如何找到感兴趣的活动,{{1}}?

  2. 为什么根据搜索结果我的数据库缺少3,940个市场?

谢谢。

4 个答案:

答案 0 :(得分:1)

  1. 您可以使用列表推导来实现搜索。它应该非常有效。

db_bd = bw.Database('ei3.4 cutoff')

market_brandon_want = [act for act in db_bd 
                    if 'market for transport, freight, lorry, unspecified' in act['name'] 
                    and 'GLO' in act['location']
     ][0]
  1. 然后,如果您这样做:

len([act for act in db_bd if 'market' in act['name'] ])

您应该得到4183,这似乎是一个更准确的数字。我相信您可以优化过滤条件,使其更接近您的数字。

您可以阅读2017年在苏黎世举行的Brightway2 seminar上的教材。这对我的情况确实很有帮助。

答案 1 :(得分:1)

尝试在搜索字段中添加更多关键字,不带逗号

Database("ecoinvent 3.4 conseq").search('market transport freight lorry unspecified')

在我的版本中返回:

['transport, freight, lorry, all sizes, EURO3 to generic market for 
transport, freight, lorry, unspecified' (ton kilometer, RER, None), 
'transport, freight, lorry, all sizes, EURO3 to generic market for transport, 
freight, lorry, unspecified' (ton kilometer, RoW, None),
'transport, freight, lorry, all sizes, EURO4 to generic market for transport, 
freight, lorry, unspecified' (ton kilometer, RoW, None),
'transport, freight, lorry, all sizes, EURO5 to generic market for transport, 
freight, lorry, unspecified' (ton kilometer, RoW, None),
...

您也可以使用过滤器查找位置:

Database("ecoinvent 3.4 conseq").search('market transport freight lorry unspecified', filter={"location" : 'GLO'})

返回:

['market for transport, freight, lorry, unspecified' (ton kilometer, GLO, None)]

如果要自动执行搜索(例如查找一系列过程的代码),不确定是否可以使用。

答案 2 :(得分:1)

1) I always use list comprehension as Laurent. Note that activities have a field called activity type. The possible values are 'market activity', 'market group', 'ordinary transforming activity'

db_bd = bw.Database('ei3.4 cutoff')
set([ds['activity type'] for ds in bd_db])

This field is useful to distinguish markets and ordinary transforming activities

market_brandon_want = [act for act in db_bd
                if 'freight, lorry, unspecified' in act['name'] 
                and 'GLO' in act['location']
                and act['activity type']!='ordinary transforming activity'
 ]
market_brandon_want

2) search does not find all the markets because there is a limit to the number of results. You can change it with the argument limit (documentation).

bw.Database('ei3.4 cutoff').search('market',limit=5)

答案 3 :(得分:0)

因此,一种可行的有效方法是使用数据库的内置迭代器:

>>> m_t_f_l_u = next(_a for _a in Database('ei3.4 cutoff') \
                     if _a['name'] == 'market for transport, freight, lorry, unspecified' \
                     and _a['location'] == 'GLO')

这有效。不过,它似乎效率不高,需要几秒钟才能运行。