PyPSA-EUR执行solve_elec_networks规则触发AssertionError错误
问题
已按PyPSA-EUR官方教程完成部署,执行以下仅电力测试命令成功:
snakemake -call results/test-elec/networks/elec_s_6_ec_lcopt_Co2L-24H.nc --configfile config/test/config.electricity.yaml
但执行snakemake -call solve_elec_networks时触发如下AssertionError:
AssertionError: In Line cluster underground, the values of attribute underground do not agree:
完整回溯信息:
Traceback (most recent call last): File "/mnt/d/inc/pypsa_test/pypsa-eur/.snakemake/scripts/tmplkaoyzq0.cluster_network.py", line 515, in clustering = clustering_for_n_clusters( File "/mnt/d/inc/pypsa_test/pypsa-eur/.snakemake/scripts/tmplkaoyzq0.cluster_network.py", line 413, in clustering_for_n_clusters clustering = get_clustering_from_busmap( File "/home/john/anaconda3/envs/pypsa-eur/lib/python3.10/site-packages/pypsa/clustering/spatial.py", line 535, in get_clustering_from_busmap lines, lines_t, linemap = aggregatelines( File "/home/john/anaconda3/envs/pypsa-eur/lib/python3.10/site-packages/pypsa/clustering/spatial.py", line 436, in aggregatelines df = df.groupby(grouper, axis=0).agg(static_strategies) File "/home/john/.local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 1269, in aggregate result = op.agg() File "/home/john/.local/lib/python3.10/site-packages/pandas/core/apply.py", line 163, in agg return self.agg_dict_like() File "/home/john/.local/lib/python3.10/site-packages/pandas/core/apply.py", line 420, in agg_dict_like results = { File "/home/john/.local/lib/python3.10/site-packages/pandas/core/apply.py", line 421, in key: obj._gotitem(key, ndim=1).agg(how) for key, how in arg.items() File "/home/john/.local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 269, in aggregate return self._python_agg_general(func, *args, **kwargs) File "/home/john/.local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 288, in _python_agg_general result = self.grouper.agg_series(obj, f) File "/home/john/.local/lib/python3.10/site-packages/pandas/core/groupby/ops.py", line 994, in agg_series result = self._aggregate_series_pure_python(obj, func) File "/home/john/.local/lib/python3.10/site-packages/pandas/core/groupby/ops.py", line 1015, in _aggregate_series_pure_python res = func(group) File "/home/john/.local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 285, in f = lambda x: func(x, *args, **kwargs) File "/home/john/anaconda3/envs/pypsa-eur/lib/python3.10/site-packages/pypsa/clustering/spatial.py", line 136, in consense assert (x == v).all() or x.isnull().all(), ( AssertionError: In Line cluster underground, the values of attribute underground do not agree:
涉事线路的underground属性值:
Line 1380 False 4002 False 5055 False 11429 False 9397 False 5795 True 5733 False 1376 False Name: underground, dtype: bool
当前使用默认config.yaml和cbc求解器,询问执行solve_elec_networks前是否需要修改配置文件?
解决方案
需要修改配置文件,原因是:PyPSA在聚类聚合线路时,默认要求同一集群内的underground这类属性值必须完全一致,否则触发断言错误。而你当前的线路集群里同时存在True和False的underground值,导致报错。
具体修改步骤:
- 打开项目根目录下的
config.yaml - 找到
cluster_network或clustering配置块,添加/修改线路属性的聚合策略:- 方法一(适配多数PyPSA-EUR版本):
cluster_network: static_strategies: Line: underground: "any" # 只要集群内有地下线路,聚合后标记为地下;也可用"all"要求全为地下才标记,或"consense"取多数值 - 方法二(适配新版本PyPSA-EUR):
clustering: attribute_strategies: Line: underground: lambda x: x.any() # 自定义逻辑:存在True则返回True
- 方法一(适配多数PyPSA-EUR版本):
- 保存配置文件后,重新执行
snakemake -call solve_elec_networks即可。
如果不想修改聚合策略,也可以先检查原始线路数据的underground标注是否有误,但调整配置是更快捷的解决方式。
内容的提问来源于stack exchange,提问作者Dakait
相关产品推荐
相关产品推荐

