Brightway2, Wurst - KeyError: "Can't find this location" in geomatcher.intersects(('IMAGE', reg)) function replicating Mendoza Beltran et al., 2018

Brightway2, Wurst - KeyError: "Can't find this location" in geomatcher.intersects(('IMAGE', reg)) function replicating Mendoza Beltran et al., 2018

我目前正在学习 BW2 和 Wurst 软件包,我是 Python 的新手。我一直在尝试复制 Mendoza Beltran 等人的 “当背景很重要时:在前瞻性生命周期评估中使用综合评估模型中的场景”。 (2018) 使用 EcoInvent 3.6。如果有人也复制了该研究,那么对以下问题的任何帮助都会很棒!:)

在创建附加数据集的区域版本时,正在调用 add_new_locations_to_added_datasets(input_db) 调用 geomatcher.intersects(('IMAGE', reg)) 为综合评估模型中的每个 IMAGE 区域创建所有添加的发电数据集的新版本。但是,我似乎一直收到 KeyError: "Can't find this location" 并且我认为 regions/locations 中有一个明显的 adjustment/fix 我需要制作从提供的代码,但我的初学者大脑似乎无法倒退并解决这个问题。如果有任何不清楚或需要进一步定义的地方,我可以提供更多信息。

KeyError                                  Traceback (most recent call last)
Input In [89], in <module>
----> 1 add_new_locations_to_added_datasets(input_db)

File ~\AppData\Local\Temp\ipykernel_1930844958206.py:13, in add_new_locations_to_added_datasets(db)
     11 possibles = {}
     12 for reg in REGIONS[:-1]:
---> 13     temp= [x for x in geomatcher.intersects(('IMAGE', reg))if type(x) !=tuple]
     14     possibles[reg] = [x for x in temp if len(ecoinvent_to_image_locations(x)) ==1 ]
     15     if not len(possibles[reg]): print(reg, ' has no good candidate')

File ~\Miniconda3\envs\bw2\lib\site-packages\constructive_geometries\geomatcher.py:152, in Geomatcher.intersects(self, key, include_self, exclusive, biggest_first, only)
    149 if key == "RoW" and "RoW" not in self:
    150     return ["RoW"] if "RoW" in possibles else []
--> 152 faces = self[key]
    153 lst = [
    154     (k, (len(v.intersection(faces)), len(v)))
    155     for k, v in possibles.items()
    156     if (faces.intersection(v))
    157 ]
    158 return self._finish_filter(lst, key, include_self, exclusive, biggest_first)

File ~\Miniconda3\envs\bw2\lib\site-packages\constructive_geometries\geomatcher.py:72, in Geomatcher.__getitem__(self, key)
     70 if key == "RoW" and "RoW" not in self.topology:
     71     return set()
---> 72 return self.topology[self._actual_key(key)]

File ~\Miniconda3\envs\bw2\lib\site-packages\constructive_geometries\geomatcher.py:105, in Geomatcher._actual_key(self, key)
    102             print("Geomatcher: Used '{}' for '{}'".format(new, key))
    103         return new
--> 105 raise KeyError("Can't find this location")

KeyError: "Can't find this location"

IMAGE 区域标签为 changed in 2021。不知道为什么,但我想这就是导致错误的原因。可能 wurst 适合使用较新的 IMAGE 场景或类似的东西。

您可以使用 %debug 启动调试器打印导致错误的确切位置,并将其添加到您的问题中。

  1. Wurst 中的图像区域标签已更新,因此,在提供的 图像变量 names-correct.csv 电子表格中,名称需要更新,正如 Chris Mutel 在评论中所建议的那样。

  2. 在正在使用的_Functions_to_modify_ecoinvent笔记本中,函数为add_new_locations_to_added_datasets(db) 下面,[x for x in temp if len(ecoinvent_to_image_locations(x)) ==1] 需要列表理解改为==2以匹配EcoInvent的两个字母代码提供可能。不确定这是由于更新还是意外拼写错误。

def add_new_locations_to_added_datasets(db):
    # We create a new version of all added electricity generation datasets for each IMAGE region. 
    # We allow the upstream production to remain global, as we are mostly interested in regionalizing 
    # to take advantage of the regionalized IMAGE data.
    
    # step 1: make copies of all datasets for new locations
    # best would be to regionalize datasets for every location with an electricity market like this:
    # locations = {x['location'] for x in get_many(db, *electricity_market_filter_high_voltage)}  
    
    # but this takes quite a long time. For now, we just use 1 location that is uniquely in each IMAGE region.
    possibles = {}
    for reg in REGIONS[:-1]:
        temp= [x for x in geomatcher.intersects(('IMAGE', reg))if type(x) !=tuple]
        possibles[reg] = [x for x in temp if len(ecoinvent_to_image_locations(x)) ==2 ]
        if not len(possibles[reg]): print(reg, ' has no good candidate')
    locations = [v[0] for v in possibles.values()]
  1. 对于使用较新版本 EcoInvent 的任何人,以前 IAI Area 区域需要在 rename_locations(input_db, fix_names) 因为它们已经更新,所以不再需要了。此外,为了将结果写回数据库,rename_locations(db, fix_names_back) 函数需要 rename_locations(db, fix_names)
# These locations aren't found correctly by the constructive geometries library - we correct them here:
fix_names= {'CSG' : 'CN-CSG',
            'SGCC': 'CN-SGCC',
             
             'RFC' : 'US-RFC',
             'SERC' : 'US-SERC',
             'TRE': 'US-TRE',
             'ASCC': 'US-ASCC',
             'HICC': 'US-HICC',
             'FRCC': 'US-FRCC',
             'SPP' : 'US-SPP',
            'MRO, US only' : 'US-MRO', 
             'NPCC, US only': 'US-NPCC', 
             'WECC, US only': 'US-WECC',
             
             'IAI Area 1, Africa':'IAI Area, Africa',
             'IAI Area 3, South America':'IAI Area, South America',
             'IAI Area 4&5, without China':'IAI Area, Asia, without China and GCC',
             'IAI Area 2, without Quebec':'IAI Area, North America, without Quebec',
             'IAI Area 8, Gulf':'IAI Area, Gulf Cooperation Council',
            }


fix_names_back = {v:k for k,v in fix_names.items()}


def rename_locations(db, name_dict):
    for ds in db:
        if ds['location'] in name_dict:
            ds['location'] = name_dict[ds['location']]

        for exc in w.technosphere(ds):
            if exc['location'] in name_dict:
                exc['location'] = name_dict[exc['location']]
    
rename_locations(input_db, fix_names)
for key in database_dict.keys():
    db = database_dict[key]['db']
    rename_locations(db, fix_names_back)
    write_brightway2_database(db, key)